{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Intro\nIn this notebook, transfer learning is used to train a model for classification. I will be using \"Inception\" model and I will train my model on top of it.","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport random\nimport shutil\nimport zipfile\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import RMSprop\nprint(f\"TF version is: {tf.__version__}\")\nfrom tensorflow.keras.preprocessing.image import img_to_array, load_img\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-03T22:50:10.716581Z","iopub.execute_input":"2022-12-03T22:50:10.716991Z","iopub.status.idle":"2022-12-03T22:50:16.871593Z","shell.execute_reply.started":"2022-12-03T22:50:10.716958Z","shell.execute_reply":"2022-12-03T22:50:16.870702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        \nlocal_zip = '/kaggle/input/dogs-vs-cats/train.zip'\nzip_ref   = zipfile.ZipFile(local_zip, 'r')\nzip_ref.extractall('/')\nzip_ref.close()","metadata":{"execution":{"iopub.status.busy":"2022-12-03T22:50:25.324389Z","iopub.execute_input":"2022-12-03T22:50:25.325091Z","iopub.status.idle":"2022-12-03T22:50:40.165221Z","shell.execute_reply.started":"2022-12-03T22:50:25.325051Z","shell.execute_reply":"2022-12-03T22:50:40.164257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train and validation data\nSplit the data to train and validation and move them to correct folders:","metadata":{}},{"cell_type":"code","source":"train_path = '/train'\nf_names = os.listdir(train_path)\nprint(f\"count of data points: {len(f_names)}\")\n\nrandom.seed(1)\nsplit_ratio = 0.8\nsample_data = random.sample(f_names, len(f_names))\ntrain_sample  = sample_data[:int(split_ratio * len(f_names))]\nvalidation_sample = sample_data[int(split_ratio * len(f_names)):]\n\n# Directory with training cat/dog pictures\ntrain_cats_dir = '/train/cat'\ntrain_dogs_dir = '/train/dog'\n\n# Directory with validation cat/dog pictures\nvalidation_cats_dir = '/validation/cat'\nvalidation_dogs_dir = '/validation/dog'\n\n# create directories:\nos.makedirs(train_dogs_dir,exist_ok = True)\nos.makedirs(train_cats_dir,exist_ok = True)\nos.makedirs(validation_dogs_dir,exist_ok = True)\nos.makedirs(validation_cats_dir,exist_ok = True)\n\n# copy the data:\n# train data:\nfor v in train_sample:\n    if 'cat' in v.lower():\n        shutil.copy('/train/'+ v,'/train/cat/'+ v)\n    else:\n         shutil.copy('/train/' + v,'/train/dog/' + v)\n\n# validation data:\nfor v in validation_sample:\n    if 'cat' in v.lower():\n        shutil.copy('/train/'+ v,'/validation/cat/'+ v)\n    else:\n        shutil.copy('/train/'+ v,'/validation/dog/'+ v)\n\nprint(f'count of cats in training: {len(os.listdir(train_cats_dir))}')\nprint(f'count of dogs in training: {len(os.listdir(train_dogs_dir))}')","metadata":{"execution":{"iopub.status.busy":"2022-12-03T22:52:30.027955Z","iopub.execute_input":"2022-12-03T22:52:30.028356Z","iopub.status.idle":"2022-12-03T22:52:33.846909Z","shell.execute_reply.started":"2022-12-03T22:52:30.028325Z","shell.execute_reply":"2022-12-03T22:52:33.845719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data pre-processing:","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# All images will be rescaled by 1./255.\ntrain_datagen = ImageDataGenerator( rescale = 1.0/255. )\ntest_datagen  = ImageDataGenerator( rescale = 1.0/255. )\n\n# --------------------\n# Flow training images in batches of 20 using train_datagen generator\n# --------------------\ntrain_generator = train_datagen.flow_from_directory('/train',\n                                                    batch_size=50,\n                                                    class_mode='binary',\n                                                    target_size=(150, 150))     \n# --------------------\n# Flow validation images in batches of 20 using test_datagen generator\n# --------------------\nvalidation_generator =  test_datagen.flow_from_directory('/validation',\n                                                         batch_size=50,\n                                                         class_mode  = 'binary',\n                                                         target_size = (150, 150))","metadata":{"execution":{"iopub.status.busy":"2022-12-03T22:57:16.939862Z","iopub.execute_input":"2022-12-03T22:57:16.940286Z","iopub.status.idle":"2022-12-03T22:57:17.687909Z","shell.execute_reply.started":"2022-12-03T22:57:16.940253Z","shell.execute_reply":"2022-12-03T22:57:17.687004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget --no-check-certificate \\\n    https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5 \\\n    -O /tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5","metadata":{"execution":{"iopub.status.busy":"2022-12-03T22:53:07.252589Z","iopub.execute_input":"2022-12-03T22:53:07.253775Z","iopub.status.idle":"2022-12-03T22:53:12.333186Z","shell.execute_reply.started":"2022-12-03T22:53:07.253707Z","shell.execute_reply":"2022-12-03T22:53:12.331917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras import layers\n\n# Set the weights file you downloaded into a variable\nlocal_weights_file = '/tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'\n\n# Initialize the base model.\n# Set the input shape and remove the dense layers.\npre_trained_model = InceptionV3(input_shape = (150, 150, 3), \n                                include_top = False, \n                                weights = None)\n\n# Load the pre-trained weights you downloaded.\npre_trained_model.load_weights(local_weights_file)\n\n# Freeze the weights of the layers.\nfor layer in pre_trained_model.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2022-12-03T22:53:44.839300Z","iopub.execute_input":"2022-12-03T22:53:44.839713Z","iopub.status.idle":"2022-12-03T22:53:47.222195Z","shell.execute_reply.started":"2022-12-03T22:53:44.839678Z","shell.execute_reply":"2022-12-03T22:53:47.220831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Choose `mixed_7` as the last layer of your base model\nlast_layer = pre_trained_model.get_layer('mixed7')\nprint('last layer output shape: ', last_layer.output_shape)\nlast_output = last_layer.output","metadata":{"execution":{"iopub.status.busy":"2022-12-03T22:54:13.297442Z","iopub.execute_input":"2022-12-03T22:54:13.297883Z","iopub.status.idle":"2022-12-03T22:54:13.305936Z","shell.execute_reply.started":"2022-12-03T22:54:13.297847Z","shell.execute_reply":"2022-12-03T22:54:13.304319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras import Model\n\n# Flatten the output layer to 1 dimension\nx = layers.Flatten()(last_output)\n# Add a fully connected layer with 1,024 hidden units and ReLU activation\nx = layers.Dense(1024, activation='relu')(x)\n# Add a dropout rate of 0.2\nx = layers.Dropout(0.2)(x)                  \n# Add a final sigmoid layer for classification\nx = layers.Dense  (1, activation='sigmoid')(x)           \n\n# Append the dense network to the base model\nmodel = Model(pre_trained_model.input, x) \n\n# Print the model summary. See your dense network connected at the end.\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-12-03T22:55:58.371817Z","iopub.execute_input":"2022-12-03T22:55:58.372291Z","iopub.status.idle":"2022-12-03T22:55:58.608834Z","shell.execute_reply.started":"2022-12-03T22:55:58.372255Z","shell.execute_reply":"2022-12-03T22:55:58.607618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = RMSprop(learning_rate=0.0001), \n              loss = 'binary_crossentropy', \n              metrics = ['accuracy'])\n\nhistory = model.fit(\n            train_generator,\n            validation_data = validation_generator,\n            steps_per_epoch = 100,\n            epochs = 20,\n            validation_steps = 50,\n            verbose = 2)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T22:58:00.634343Z","iopub.execute_input":"2022-12-03T22:58:00.634829Z","iopub.status.idle":"2022-12-04T00:03:10.705569Z","shell.execute_reply.started":"2022-12-03T22:58:00.634788Z","shell.execute_reply":"2022-12-04T00:03:10.704272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n# function for plotting the accuracy and loss vs epochs\ndef plot_loss_acc(history):\n    '''Plots the training and validation loss and accuracy from a history object'''\n    acc = [0.]+history.history['accuracy']\n    val_acc = [0.]+history.history['val_accuracy']\n    loss = [0.]+history.history['loss']\n    val_loss = [0.]+history.history['val_loss']\n\n    epochs = range(len(acc))\n\n    plt.plot(epochs, acc, 'bo', label='Training accuracy')\n    plt.plot(epochs, val_acc, 'b', label='Validation accuracy')\n    plt.title('Training and validation accuracy')\n\n    plt.figure()\n\n    plt.plot(epochs, loss, 'bo', label='Training Loss')\n    plt.plot(epochs, val_loss, 'b', label='Validation Loss')\n    plt.title('Training and validation loss')\n    plt.legend()\n\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-04T00:06:19.476930Z","iopub.execute_input":"2022-12-04T00:06:19.477352Z","iopub.status.idle":"2022-12-04T00:06:19.486065Z","shell.execute_reply.started":"2022-12-04T00:06:19.477315Z","shell.execute_reply":"2022-12-04T00:06:19.485205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_loss_acc(history)","metadata":{"execution":{"iopub.status.busy":"2022-12-04T00:06:19.830812Z","iopub.execute_input":"2022-12-04T00:06:19.831247Z","iopub.status.idle":"2022-12-04T00:06:20.270108Z","shell.execute_reply.started":"2022-12-04T00:06:19.831211Z","shell.execute_reply":"2022-12-04T00:06:20.268816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}