{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"# https://www.tensorflow.org/tutorials/images/transfer_learning","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport os\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Import tensorflow_datasets to fetch data, we need to use pip to install it first\n# Note that it requires enabling the internet connection (see \"Settings\")\n!pip install tensorflow_datasets\nimport tensorflow_datasets as tfds\ntfds.disable_progress_bar()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"(raw_train, raw_test, raw_validation), metadata = tfds.load(\"cats_vs_dogs\", split=[\"train[:80%]\", \"train[80%:90%]\", \"train[90%:]\"], with_info=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"metadata","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(raw_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMG_SIZE = 160","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"i = 0\nplt.figure(figsize=(10, 10))\nfor img in raw_train.take(2):\n    i += 1\n    plt.subplot(2, 2, i)\n    plt.title(metadata.features['label'].int2str(img[\"label\"]) + \" original\")\n    plt.imshow(img[\"image\"])\n    \n    i += 1\n    plt.subplot(2, 2, i)\n    resized = tf.image.resize(img[\"image\"], [IMG_SIZE, IMG_SIZE])\n    plt.title(metadata.features['label'].int2str(img[\"label\"]) + \" resized\")\n    plt.imshow(resized.numpy().astype(np.uint8))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def format_example(sample):\n    image, label = sample[\"image\"], sample[\"label\"]\n    image = tf.cast(image, tf.float32)\n    image = (image / 127.5) - 1\n    return tf.image.resize(image, [IMG_SIZE, IMG_SIZE]), label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for img in raw_train.take(1):\n    img[\"image\"].numpy()\n    formatted = format_example(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"raw_train = raw_train.map(format_example)\nraw_test = raw_test.map(format_example)\nraw_validation = raw_validation.map(format_example)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"SHUFFLE_BUFFER_SIZE = 1000\nBATCH_SIZE = 32\nraw_train = raw_train.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)\nraw_test = raw_test.batch(BATCH_SIZE)\nraw_validation = raw_validation.batch(BATCH_SIZE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# take(1) returns a single batches (i.e. BATCH_SIZE samples)\nfor image_batch, labels in raw_train.take(1):\n    print(image_batch.shape)\n    print(labels.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)\n\nbase_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE, include_top=False, weights='imagenet')\nfull_base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE, weights='imagenet')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"full_base_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"converted_batch = base_model(image_batch)\nprint(converted_batch.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"global_average_layer = tf.keras.layers.GlobalAveragePooling2D()\nfeature_batch_average = global_average_layer(converted_batch)\nprint(converted_batch.shape)\nprint(feature_batch_average.shape)\nconverted_batch[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"feature_batch_average[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"prediction_layer = tf.keras.layers.Dense(1, activation=\"sigmoid\")\npredicted = prediction_layer(feature_batch_average)\nprint(predicted)\nprint(\"Max probability: %f, min probability %f\" % (max(predicted), min(predicted)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_model.trainable = False\nmodel = tf.keras.Sequential([\n    base_model,\n    global_average_layer,\n    prediction_layer,\n])\n\nmodel.compile(optimizer=\"adam\", loss=\"binary_crossentropy\", metrics=[\"accuracy\"])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# What is trainable variables?\nlen(model.trainable_variables)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"initial_epochs = 10\nvalidation_steps=20\n\nloss0, accuracy0 = model.evaluate(raw_validation, steps = validation_steps)\nprint(\"initial loss: {:.2f}\".format(loss0))\nprint(\"initial accuracy: {:.2f}\".format(accuracy0))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(raw_train,\n                    epochs=initial_epochs,\n                    validation_data=raw_validation)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.subplot(211)\nplt.plot(history.history[\"accuracy\"], label=\"accuracy\")\nplt.plot(history.history[\"val_accuracy\"], label=\"val_accuracy\")\nplt.ylabel(\"Accuracy\")\nplt.xlabel(\"Epoch\")\nplt.legend()\n\nplt.subplot(212)\nplt.plot(history.history[\"loss\"], label=\"loss\")\nplt.plot(history.history[\"val_loss\"], label=\"val_loss\")\nplt.ylabel(\"Loss\")\nplt.xlabel(\"Epoch\")\nplt.legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_model.trainable = True\nprint(len(base_model.layers))\n\nfine_tune_from = 100\nfor layer in base_model.layers[:fine_tune_from]:\n    layer.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(loss=\"binary_crossentropy\", optimizer=tf.keras.optimizers.RMSprop(lr=0.00001), metrics=[\"accuracy\"])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(model.trainable_variables)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history.epoch[-1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history_fine_tune = model.fit(raw_train, epochs=initial_epochs + 10, initial_epoch=history.epoch[-1], validation_data=raw_validation)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"accuracy = history.history[\"accuracy\"] + history_fine_tune.history[\"accuracy\"]\nval_accuracy = history.history[\"val_accuracy\"] + history_fine_tune.history[\"val_accuracy\"]\n\nloss = history.history[\"loss\"] + history_fine_tune.history[\"loss\"]\nval_loss = history.history[\"val_loss\"] + history_fine_tune.history[\"val_loss\"]\n\nplt.figure(figsize=(10, 10))\nplt.subplot(211)\nplt.title(\"Accuracy\")\nplt.plot(accuracy, label=\"accuracy\")\nplt.plot(val_accuracy, label=\"val_accuracy\")\nplt.plot([initial_epochs, initial_epochs], plt.ylim())\nplt.legend()\n\nplt.subplot(212)\nplt.title(\"Loss\")\nplt.plot(loss, label=\"loss\")\nplt.plot(val_loss, label=\"val_loss\")\nplt.plot([initial_epochs, initial_epochs], plt.ylim())\nplt.legend()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}