diff --git a/Copy_of_C2_W1_Lab_2_Transfer_Learning.ipynb b/Copy_of_C2_W1_Lab_2_Transfer_Learning.ipynb new file mode 100644 index 0000000..87393a1 --- /dev/null +++ b/Copy_of_C2_W1_Lab_2_Transfer_Learning.ipynb @@ -0,0 +1,915 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "accelerator": "GPU", + "colab": { + "private_outputs": true, + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.5" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "zX4Kg8DUTKWO" + }, + "source": [ + "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", + "# you may not use this file except in compliance with the License.\n", + "# You may obtain a copy of the License at\n", + "#\n", + "# https://www.apache.org/licenses/LICENSE-2.0\n", + "#\n", + "# Unless required by applicable law or agreed to in writing, software\n", + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", + "# See the License for the specific language governing permissions and\n", + "# limitations under the License." + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Za8-Nr5k11fh" + }, + "source": [ + "##### Copyright 2018 The TensorFlow Authors." + ] + }, + { + "cell_type": "code", + "metadata": { + "cellView": "form", + "id": "Eq10uEbw0E4l" + }, + "source": [ + "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", + "# you may not use this file except in compliance with the License.\n", + "# You may obtain a copy of the License at\n", + "#\n", + "# https://www.apache.org/licenses/LICENSE-2.0\n", + "#\n", + "# Unless required by applicable law or agreed to in writing, software\n", + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", + "# See the License for the specific language governing permissions and\n", + "# limitations under the License." + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oYM61xrTsP5d" + }, + "source": [ + "# Transfer Learning with TensorFlow Hub for TFLite" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bL54LWCHt5q5" + }, + "source": [ + "## Setup " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "110fGB18UNJn" + }, + "source": [ + "try:\n", + " %tensorflow_version 2.x\n", + "except:\n", + " pass" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "dlauq-4FWGZM" + }, + "source": [ + "import numpy as np\n", + "import matplotlib.pylab as plt\n", + "\n", + "import tensorflow as tf\n", + "import tensorflow_hub as hub\n", + "import tensorflow_datasets as tfds\n", + "tfds.disable_progress_bar()\n", + "\n", + "from tqdm import tqdm\n", + "\n", + "print(\"\\u2022 Using TensorFlow Version:\", tf.__version__)\n", + "print(\"\\u2022 Using TensorFlow Hub Version: \", hub.__version__)\n", + "print('\\u2022 GPU Device Found.' if tf.test.is_gpu_available() else '\\u2022 GPU Device Not Found. Running on CPU')" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mmaHHH7Pvmth" + }, + "source": [ + "## Select the Hub/TF2 Module to Use\n", + "\n", + "Hub modules for TF 1.x won't work here, please use one of the selections provided." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "FlsEcKVeuCnf" + }, + "source": [ + "module_selection = (\"mobilenet_v2\", 224, 1280) #@param [\"(\\\"mobilenet_v2\\\", 224, 1280)\", \"(\\\"inception_v3\\\", 299, 2048)\"] {type:\"raw\", allow-input: true}\n", + "handle_base, pixels, FV_SIZE = module_selection\n", + "MODULE_HANDLE =\"https://tfhub.dev/google/tf2-preview/{}/feature_vector/4\".format(handle_base)\n", + "IMAGE_SIZE = (pixels, pixels)\n", + "print(\"Using {} with input size {} and output dimension {}\".format(MODULE_HANDLE, IMAGE_SIZE, FV_SIZE))" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sYUsgwCBv87A" + }, + "source": [ + "## Data Preprocessing" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8nqVX3KYwGPh" + }, + "source": [ + "Use [TensorFlow Datasets](http://tensorflow.org/datasets) to load the cats and dogs dataset.\n", + "\n", + "This `tfds` package is the easiest way to load pre-defined data. If you have your own data, and are interested in importing using it with TensorFlow see [loading image data](../load_data/images.ipynb)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YkF4Boe5wN7N" + }, + "source": [ + "The `tfds.load` method downloads and caches the data, and returns a `tf.data.Dataset` object. These objects provide powerful, efficient methods for manipulating data and piping it into your model.\n", + "\n", + "Since `\"cats_vs_dog\"` only has one defined split, `train`, we are going to divide that into (train, validation, test) with 80%, 10%, 10% of the data respectively." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "SQ9xK9F2wGD8" + }, + "source": [ + "(train_examples, validation_examples, test_examples), info = tfds.load('cats_vs_dogs', \n", + " with_info=True, \n", + " as_supervised=True, \n", + " split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'])\n", + "\n", + "num_examples = info.splits['train'].num_examples\n", + "num_classes = info.features['label'].num_classes" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pmXQYXNWwf19" + }, + "source": [ + "### Format the Data\n", + "\n", + "Use the `tf.image` module to format the images for the task.\n", + "\n", + "Resize the images to a fixes input size, and rescale the input channels" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "y7UyXblSwkUS" + }, + "source": [ + "def format_image(image, label):\n", + " image = tf.image.resize(image, IMAGE_SIZE) / 255.0\n", + " return image, label" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1nrDR8CnwrVk" + }, + "source": [ + "Now shuffle and batch the data\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "zAEUG7vawxLm" + }, + "source": [ + "BATCH_SIZE = 32 #@param {type:\"integer\"}" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "fHEC9mbswxvM" + }, + "source": [ + "train_batches = train_examples.shuffle(num_examples // 4).map(format_image).batch(BATCH_SIZE).prefetch(1)\n", + "validation_batches = validation_examples.map(format_image).batch(BATCH_SIZE).prefetch(1)\n", + "test_batches = test_examples.map(format_image).batch(1)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ghQhZjgEw1cK" + }, + "source": [ + "Inspect a batch" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "gz0xsMCjwx54" + }, + "source": [ + "for image_batch, label_batch in train_batches.take(1):\n", + " pass\n", + "\n", + "image_batch.shape" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FS_gVStowW3G" + }, + "source": [ + "## Defining the Model\n", + "\n", + "All it takes is to put a linear classifier on top of the `feature_extractor_layer` with the Hub module.\n", + "\n", + "For speed, we start out with a non-trainable `feature_extractor_layer`, but you can also enable fine-tuning for greater accuracy." + ] + }, + { + "cell_type": "code", + "metadata": { + "cellView": "form", + "id": "RaJW3XrPyFiF" + }, + "source": [ + "do_fine_tuning = False #@param {type:\"boolean\"}" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wd0KfstqaUmE" + }, + "source": [ + "Load TFHub Module" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "svvDrt3WUrrm" + }, + "source": [ + "feature_extractor = hub.KerasLayer(MODULE_HANDLE,\n", + " input_shape=IMAGE_SIZE + (3,), \n", + " output_shape=[FV_SIZE],\n", + " trainable=do_fine_tuning)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "50FYNIb1dmJH" + }, + "source": [ + "print(\"Building model with\", MODULE_HANDLE)\n", + "\n", + "model = tf.keras.Sequential([\n", + " feature_extractor,\n", + " tf.keras.layers.Dense(num_classes, activation='softmax')\n", + "])\n", + "\n", + "model.summary()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "LvhaW-IAMO7i" + }, + "source": [ + "#@title (Optional) Unfreeze some layers\n", + "NUM_LAYERS = 10 #@param {type:\"slider\", min:1, max:50, step:1}\n", + " \n", + "if do_fine_tuning:\n", + " feature_extractor.trainable = True\n", + " \n", + " for layer in model.layers[-NUM_LAYERS:]:\n", + " layer.trainable = True\n", + "\n", + "else:\n", + " feature_extractor.trainable = False" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "u2e5WupIw2N2" + }, + "source": [ + "## Training the Model" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9f3yBUvkd_VJ" + }, + "source": [ + "if do_fine_tuning:\n", + " model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.002, momentum=0.9),\n", + " loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n", + " metrics=['accuracy'])\n", + "else:\n", + " model.compile(optimizer='adam',\n", + " loss='sparse_categorical_crossentropy',\n", + " metrics=['accuracy'])" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "w_YKX2Qnfg6x" + }, + "source": [ + "EPOCHS = 5\n", + "\n", + "hist = model.fit(train_batches,\n", + " epochs=EPOCHS,\n", + " validation_data=validation_batches)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "u_psFoTeLpHU" + }, + "source": [ + "## Export the Model" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "XaSb5nVzHcVv" + }, + "source": [ + "CATS_VS_DOGS_SAVED_MODEL = \"exp_saved_model\"" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fZqRAg1uz1Nu" + }, + "source": [ + "Export the SavedModel" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "yJMue5YgnwtN" + }, + "source": [ + "tf.saved_model.save(model, CATS_VS_DOGS_SAVED_MODEL)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "SOQF4cOan0SY" + }, + "source": [ + "%%bash -s $CATS_VS_DOGS_SAVED_MODEL\n", + "saved_model_cli show --dir $1 --tag_set serve --signature_def serving_default" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "FY7QGBgBytwX" + }, + "source": [ + "loaded = tf.saved_model.load(CATS_VS_DOGS_SAVED_MODEL)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "tIhPyMISz952" + }, + "source": [ + "print(list(loaded.signatures.keys()))\n", + "infer = loaded.signatures[\"serving_default\"]\n", + "print(infer.structured_input_signature)\n", + "print(infer.structured_outputs)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XxLiLC8n0H16" + }, + "source": [ + "## Convert Using TFLite's Converter" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1aUYvCpfWmrQ" + }, + "source": [ + "Load the TFLiteConverter with the SavedModel" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "dqJRyIg8Wl1n" + }, + "source": [ + "converter = tf.lite.TFLiteConverter.from_saved_model(CATS_VS_DOGS_SAVED_MODEL)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AudcNjT0UtfF" + }, + "source": [ + "### Post-Training Quantization\n", + "The simplest form of post-training quantization quantizes weights from floating point to 8-bits of precision. This technique is enabled as an option in the TensorFlow Lite converter. At inference, weights are converted from 8-bits of precision to floating point and computed using floating-point kernels. This conversion is done once and cached to reduce latency.\n", + "\n", + "To further improve latency, hybrid operators dynamically quantize activations to 8-bits and perform computations with 8-bit weights and activations. This optimization provides latencies close to fully fixed-point inference. However, the outputs are still stored using floating point, so that the speedup with hybrid ops is less than a full fixed-point computation." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "WmSr2-yZoUhz" + }, + "source": [ + "converter.optimizations = [tf.lite.Optimize.DEFAULT]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YpCijI08UxP0" + }, + "source": [ + "### Post-Training Integer Quantization\n", + "We can get further latency improvements, reductions in peak memory usage, and access to integer only hardware accelerators by making sure all model math is quantized. To do this, we need to measure the dynamic range of activations and inputs with a representative data set. You can simply create an input data generator and provide it to our converter." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "clM_dTIkWdIa" + }, + "source": [ + "def representative_data_gen():\n", + " for input_value, _ in test_batches.take(100):\n", + " yield [input_value]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "0oPkAxDvUias" + }, + "source": [ + "converter.representative_dataset = representative_data_gen" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IGUAVTqXVfnu" + }, + "source": [ + "The resulting model will be fully quantized but still take float input and output for convenience.\n", + "\n", + "Ops that do not have quantized implementations will automatically be left in floating point. This allows conversion to occur smoothly but may restrict deployment to accelerators that support float. " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cPVdjaEJVkHy" + }, + "source": [ + "### Full Integer Quantization\n", + "\n", + "To require the converter to only output integer operations, one can specify:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "eQi1aO2cVhoL" + }, + "source": [ + "converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "snwssESbVtFw" + }, + "source": [ + "### Finally convert the model" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "tUEgr46WVsqd" + }, + "source": [ + "tflite_model = converter.convert()\n", + "tflite_model_file = 'converted_model.tflite'\n", + "\n", + "with open(tflite_model_file, \"wb\") as f:\n", + " f.write(tflite_model)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BbTF6nd1KG2o" + }, + "source": [ + "## Test the TFLite Model Using the Python Interpreter" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "dg2NkVTmLUdJ" + }, + "source": [ + "# Load TFLite model and allocate tensors.\n", + " \n", + "interpreter = tf.lite.Interpreter(model_path=tflite_model_file)\n", + "interpreter.allocate_tensors()\n", + "\n", + "input_index = interpreter.get_input_details()[0][\"index\"]\n", + "output_index = interpreter.get_output_details()[0][\"index\"]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "snJQVs9JNglv" + }, + "source": [ + "# Gather results for the randomly sampled test images\n", + "predictions = []\n", + "\n", + "test_labels, test_imgs = [], []\n", + "for img, label in tqdm(test_batches.take(10)):\n", + " interpreter.set_tensor(input_index, img)\n", + " interpreter.invoke()\n", + " predictions.append(interpreter.get_tensor(output_index))\n", + " \n", + " test_labels.append(label.numpy()[0])\n", + " test_imgs.append(img)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "YMTWNqPpNiAI" + }, + "source": [ + "#@title Utility functions for plotting\n", + "# Utilities for plotting\n", + "\n", + "class_names = ['cat', 'dog']\n", + "\n", + "def plot_image(i, predictions_array, true_label, img):\n", + " predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]\n", + " plt.grid(False)\n", + " plt.xticks([])\n", + " plt.yticks([])\n", + " \n", + " img = np.squeeze(img)\n", + "\n", + " plt.imshow(img, cmap=plt.cm.binary)\n", + " \n", + " predicted_label = np.argmax(predictions_array)\n", + " \n", + " if predicted_label == true_label:\n", + " color = 'green'\n", + " else:\n", + " color = 'red'\n", + " \n", + " plt.xlabel(\"{} {:2.0f}% ({})\".format(class_names[predicted_label],\n", + " 100*np.max(predictions_array),\n", + " class_names[true_label]), color=color)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fK_CTyL3XQt1" + }, + "source": [ + "NOTE: Colab runs on server CPUs. At the time of writing this, TensorFlow Lite doesn't have super optimized server CPU kernels. For this reason post-training full-integer quantized models may be slower here than the other kinds of optimized models. But for mobile CPUs, considerable speedup can be observed." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1-lbnicPNkZs" + }, + "source": [ + "#@title Visualize the outputs { run: \"auto\" }\n", + "index = 0 #@param {type:\"slider\", min:0, max:9, step:1}\n", + "plt.figure(figsize=(6,3))\n", + "plt.subplot(1,2,1)\n", + "plot_image(index, predictions, test_labels, test_imgs)\n", + "plt.show()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PmZRieHmKLY5" + }, + "source": [ + "Create a file to save the labels." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "NMIjdLOsMO7v" + }, + "source": [ + "labels = ['cat', 'dog']\n", + "\n", + "with open('labels.txt', 'w') as f:\n", + " f.write('\\n'.join(labels))" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4eqPI2WaMO7v" + }, + "source": [ + "If you are running this notebook in a Colab, you can run the cell below to download the model and labels to your local disk.\n", + "\n", + "**Note**: If the files do not download when you run the cell, try running the cell a second time. Your browser might prompt you to allow multiple files to be downloaded. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "0jJAxrQB2VFw" + }, + "source": [ + "try:\n", + " from google.colab import files\n", + " files.download('converted_model.tflite')\n", + " files.download('labels.txt')\n", + "except:\n", + " pass" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BDlmpjC6VnFZ" + }, + "source": [ + "# Prepare the Test Images for Download (Optional)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_1ja_WA0WZOH" + }, + "source": [ + "This part involves downloading additional test images for the Mobile Apps only in case you need to try out more samples" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "fzLKEBrfTREA" + }, + "source": [ + "!mkdir -p test_images" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "Qn7ukNQCSewb" + }, + "source": [ + "from PIL import Image\n", + "\n", + "for index, (image, label) in enumerate(test_batches.take(50)):\n", + " image = tf.cast(image * 255.0, tf.uint8)\n", + " image = tf.squeeze(image).numpy()\n", + " pil_image = Image.fromarray(image)\n", + " pil_image.save('test_images/{}_{}.jpg'.format(class_names[label[0]], index))" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "xVKKWUG8UMO5" + }, + "source": [ + "!ls test_images" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "l_w_-UdlS9Vi" + }, + "source": [ + "!zip -qq cats_vs_dogs_test_images.zip -r test_images/" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5uzX0MO5MO7x" + }, + "source": [ + "If you are running this notebook in a Colab, you can run the cell below to download the Zip file with the images to your local disk. \n", + "\n", + "**Note**: If the Zip file does not download when you run the cell, try running the cell a second time." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Giva6EHwWm6Y" + }, + "source": [ + "try:\n", + " files.download('cats_vs_dogs_test_images.zip')\n", + "except:\n", + " pass" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "from google.colab import drive\n", + "drive.mount('/content/drive')" + ], + "metadata": { + "id": "Mk8KS46ZrgYK" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file