{"metadata":{"colab":{"provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Tensorflow Convolutions with Transfer Learning\n","metadata":{"id":"A7UWJCiUr3dv"}},{"cell_type":"code","source":"# Download the pre-trained weights. No top means it excludes the fully connected layer it uses for classification.\n!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":{"id":"QMu8RJWsr66n","outputId":"729965b0-0d75-4552-8547-2c92e9ad0ed9","tags":[],"execution":{"iopub.status.busy":"2024-01-18T19:15:20.955603Z","iopub.execute_input":"2024-01-18T19:15:20.956048Z","iopub.status.idle":"2024-01-18T19:15:25.359052Z","shell.execute_reply.started":"2024-01-18T19:15:20.956012Z","shell.execute_reply":"2024-01-18T19:15:25.357732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras import layers\n\nlocal_wieghts_file = '/tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'\n\n# \"include_top = False\" means we are not using Dense layer of InceptionV3 (We'll train our own dense layers)\npre_trained_model = InceptionV3(input_shape=(150,150,3),\n                                include_top=False,\n                                weights=None)\n\n#Loading already trained weights which we dowloaded earlier\npre_trained_model.load_weights(local_wieghts_file)\n\n# Freezing/locking the weight of the layer (bit similar to LLM finetuning)\nfor layer in pre_trained_model.layers:\n  layer.trainable = False","metadata":{"id":"8S4ghdzSs5jH","tags":[],"execution":{"iopub.status.busy":"2024-01-18T19:16:20.051655Z","iopub.execute_input":"2024-01-18T19:16:20.052044Z","iopub.status.idle":"2024-01-18T19:16:23.065262Z","shell.execute_reply.started":"2024-01-18T19:16:20.052014Z","shell.execute_reply":"2024-01-18T19:16:23.064206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre_trained_model.summary()","metadata":{"id":"4vOaysUjvHsx","outputId":"1dfe306b-f7c2-4a83-b5b4-153aaf84e9b3","execution":{"iopub.status.busy":"2024-01-18T19:16:38.322212Z","iopub.execute_input":"2024-01-18T19:16:38.322601Z","iopub.status.idle":"2024-01-18T19:16:39.18309Z","shell.execute_reply.started":"2024-01-18T19:16:38.322571Z","shell.execute_reply":"2024-01-18T19:16:39.176892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Choosing `mixed7` as the last layer of base model\n\nlast_layer = pre_trained_model.get_layer('mixed7')\nprint('last layer output shape: ', last_layer.output_shape)\nlast_output = last_layer.output\n","metadata":{"id":"7kgnMFDVwOqB","outputId":"d3a1c1a8-f02f-4f89-92d0-da0a34b1c9a8","execution":{"iopub.status.busy":"2024-01-18T19:17:13.735924Z","iopub.execute_input":"2024-01-18T19:17:13.736366Z","iopub.status.idle":"2024-01-18T19:17:13.743229Z","shell.execute_reply.started":"2024-01-18T19:17:13.736331Z","shell.execute_reply":"2024-01-18T19:17:13.741971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Adding Dense layer (with Dropour) for our classifier","metadata":{"id":"sxK44amYw_8n"}},{"cell_type":"code","source":"from tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.models import Model\n\nx = layers.Flatten()(last_output)\nx = layers.Dense(1024, activation='relu')(x)\nx = layers.Dropout(0.2)(x)\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\nmodel.summary()\n","metadata":{"id":"A9xOv_cmwyPP","outputId":"740602ca-ce66-4cf1-b4ca-6d6b6b490efa","execution":{"iopub.status.busy":"2024-01-18T19:17:26.1788Z","iopub.execute_input":"2024-01-18T19:17:26.179189Z","iopub.status.idle":"2024-01-18T19:17:27.156573Z","shell.execute_reply.started":"2024-01-18T19:17:26.179145Z","shell.execute_reply":"2024-01-18T19:17:27.155747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy',\n              optimizer=RMSprop(learning_rate=1e-4),\n              metrics=['accuracy'])","metadata":{"id":"2tBncvPQyPmP","execution":{"iopub.status.busy":"2024-01-18T19:17:33.28462Z","iopub.execute_input":"2024-01-18T19:17:33.285026Z","iopub.status.idle":"2024-01-18T19:17:33.309963Z","shell.execute_reply.started":"2024-01-18T19:17:33.284991Z","shell.execute_reply":"2024-01-18T19:17:33.308945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Prepare the dataset\nNow you will prepare the dataset. Cat vs Dog dataset.\nThis is basically the same code as the one you used in the data augmentation lab.","metadata":{"id":"VQUw6jOmzV9L"}},{"cell_type":"code","source":"# Download the dataset\n#!wget https://storage.googleapis.com/tensorflow-1-public/course2/cats_and_dogs_filtered.zip\nimport zipfile\n\nlocal_zip = '/kaggle/input/dogs-vs-cats/train.zip'\nzip_ref = zipfile.ZipFile(local_zip, 'r')\nzip_ref.extractall()\nzip_ref.close()\n\nimport os\n\n#os.makedirs('/kaggle/working/cats_and_dogs_filtered')\nos.makedirs('/kaggle/working/cats_and_dogs_filtered/train')\nos.makedirs('/kaggle/working/cats_and_dogs_filtered/train/cats')\nos.makedirs('/kaggle/working/cats_and_dogs_filtered/train/dogs')\nos.makedirs('/kaggle/working/cats_and_dogs_filtered/validation')\nos.makedirs('/kaggle/working/cats_and_dogs_filtered/validation/cats')\nos.makedirs('/kaggle/working/cats_and_dogs_filtered/validation/dogs')\n\nimport re\nimport shutil\nc= 0\nfor file in os.listdir('/kaggle/working/train/'):\n    dog = re.search('dog', file, re.IGNORECASE)\n    cat = re.search('cat', file, re.IGNORECASE)\n    if dog:\n        shutil.copy(f'/kaggle/working/train/{str(file)}','/kaggle/working/cats_and_dogs_filtered/train/dogs')\n    elif cat:\n        shutil.copy(f'/kaggle/working/train/{str(file)}','/kaggle/working/cats_and_dogs_filtered/train/cats')\n        \n        \nfor file in os.listdir('/kaggle/working/cats_and_dogs_filtered/train/dogs')[:2500]:\n    shutil.move(f'/kaggle/working/cats_and_dogs_filtered/train/dogs/{str(file)}', '/kaggle/working/cats_and_dogs_filtered/validation/dogs')\n    \nfor file in os.listdir('/kaggle/working/cats_and_dogs_filtered/train/cats')[:2500]:\n    shutil.move(f'/kaggle/working/cats_and_dogs_filtered/train/cats/{str(file)}', '/kaggle/working/cats_and_dogs_filtered/validation/cats')\n    \n\n    \nbase_dir = os.path.join('/kaggle/working/cats_and_dogs_filtered')\ntrain_dir = os.path.join(base_dir,'train')\nvalidation_dir = os.path.join(base_dir,'validation')\n\ntrain_cats_dir = os.path.join(train_dir, 'cats')\ntrain_dogs_dir = os.path.join(train_dir, 'dogs')\n\nvalidation_cats_dir = os.path.join(validation_dir,'cats')\nvalidation_dogs_dir = os.path.join(validation_dir, 'dogs')   \n\ntrain_cat_fnames = os.listdir( train_cats_dir )\ntrain_dog_fnames = os.listdir( train_dogs_dir )\n\nprint(train_cat_fnames[:10])\nprint(train_dog_fnames[:10])\n\nprint('total training cat images :', len(os.listdir(      train_cats_dir ) ))\nprint('total training dog images :', len(os.listdir(      train_dogs_dir ) ))\n\nprint('total validation cat images :', len(os.listdir( validation_cats_dir ) ))\nprint('total validation dog images :', len(os.listdir( validation_dogs_dir ) ))\n","metadata":{"id":"HZRV_MTBzinq","outputId":"bc9db1fa-f85d-4b7f-f529-20001bd0adb3","execution":{"iopub.status.busy":"2024-01-18T19:20:55.677749Z","iopub.execute_input":"2024-01-18T19:20:55.678248Z","iopub.status.idle":"2024-01-18T19:21:10.986483Z","shell.execute_reply.started":"2024-01-18T19:20:55.678202Z","shell.execute_reply":"2024-01-18T19:21:10.985246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagenerator = ImageDataGenerator(rescale=1./255,\n                                         rotation_range=40,\n                                         width_shift_range=0.2,\n                                         height_shift_range=0.2,\n                                         shear_range=0.2,\n                                         zoom_range=0.2,\n                                         horizontal_flip=True,\n                                         )\n\ntest_dataenerator = ImageDataGenerator(rescale=1./255)\n\ntrain_generator = train_datagenerator.flow_from_directory(train_dir,\n                                                          batch_size=20,\n                                                          target_size=(150,150),\n                                                          class_mode='binary')\ntest_generator = test_dataenerator.flow_from_directory(validation_dir,\n                                                       batch_size=20,\n                                                       target_size=(150,150),\n                                                       class_mode='binary')","metadata":{"id":"48wNCFj8znFG","outputId":"5187dfb1-f2f0-41e9-e0f0-76275041efca","execution":{"iopub.status.busy":"2024-01-18T19:22:14.267677Z","iopub.execute_input":"2024-01-18T19:22:14.268087Z","iopub.status.idle":"2024-01-18T19:22:14.768069Z","shell.execute_reply.started":"2024-01-18T19:22:14.268052Z","shell.execute_reply":"2024-01-18T19:22:14.767206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train the model\nWith that, we can now train the model. We will do 20 epochs and plot the results afterwards","metadata":{"id":"wjHskhZZ2yOH"}},{"cell_type":"code","source":"history = model.fit(train_generator,\n                    validation_data=test_generator,\n                    epochs=20,\n                    steps_per_epoch=100,\n                    validation_steps=50,\n                    verbose=1)","metadata":{"id":"ITKduEC92w9v","outputId":"f7cfe7b5-b522-4429-d91b-5de42f711525","execution":{"iopub.status.busy":"2024-01-18T19:22:39.275486Z","iopub.execute_input":"2024-01-18T19:22:39.27587Z","iopub.status.idle":"2024-01-18T20:05:22.385008Z","shell.execute_reply.started":"2024-01-18T19:22:39.27584Z","shell.execute_reply":"2024-01-18T20:05:22.383751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate the results\nWe will use the same code to plot the results. As we can see, the validation accuracy is also trending upwards as your training accuracy improves. This is a good sign that our model is no longer overfitting!\n\n","metadata":{"id":"Hy4jXPTx3VyA"}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'r', label='Training Accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation Accuracy')\nplt.title('Training vs Validation Accuracy')\nplt.legend(loc=0)\n\nplt.figure()\n\nplt.plot(epochs, loss, 'r', label='Training Loss')\nplt.plot(epochs, val_loss, 'b', label='Validation Loss')\nplt.title('raining vs Validation Loss')\nplt.legend()\n\n\n\nplt.show()","metadata":{"id":"wN0OTBbX3T1a","execution":{"iopub.status.busy":"2024-01-18T20:09:23.241147Z","iopub.execute_input":"2024-01-18T20:09:23.24156Z","iopub.status.idle":"2024-01-18T20:09:23.827522Z","shell.execute_reply.started":"2024-01-18T20:09:23.241526Z","shell.execute_reply":"2024-01-18T20:09:23.826266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_zip = '/kaggle/input/dogs-vs-cats/test1.zip'\nzip_ref_test = zipfile.ZipFile(test_zip, 'r')\nzip_ref_test.extractall()\nzip_ref_test.close()","metadata":{"execution":{"iopub.status.busy":"2024-01-18T20:12:21.287439Z","iopub.execute_input":"2024-01-18T20:12:21.287898Z","iopub.status.idle":"2024-01-18T20:12:26.987899Z","shell.execute_reply.started":"2024-01-18T20:12:21.287863Z","shell.execute_reply":"2024-01-18T20:12:26.986214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.utils import load_img, img_to_array\nimport numpy as np\n","metadata":{"execution":{"iopub.status.busy":"2024-01-18T20:20:33.937722Z","iopub.execute_input":"2024-01-18T20:20:33.938116Z","iopub.status.idle":"2024-01-18T20:20:33.943383Z","shell.execute_reply.started":"2024-01-18T20:20:33.938084Z","shell.execute_reply":"2024-01-18T20:20:33.942366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_list = os.listdir('/kaggle/working/test1')\nos.listdir('/kaggle/working/test1')[9]","metadata":{"execution":{"iopub.status.busy":"2024-01-18T20:31:51.364667Z","iopub.execute_input":"2024-01-18T20:31:51.36504Z","iopub.status.idle":"2024-01-18T20:31:51.383553Z","shell.execute_reply.started":"2024-01-18T20:31:51.365009Z","shell.execute_reply":"2024-01-18T20:31:51.382379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img=load_img('/kaggle/working/test1/3114.jpg', target_size=(150, 150))\n  \nx=img_to_array(img)\nx /= 255\nx=np.expand_dims(x, axis=0)\nimages = np.vstack([x])\n  \nclasses = model.predict(images, batch_size=10)\n  \nprint(classes[0])\nif classes[0]>0.5:\n    print('9587.jpg' + \" is a dog\")\nelse:\n    print('9587.jpg' + \" is a cat\")","metadata":{"execution":{"iopub.status.busy":"2024-01-18T20:23:50.111214Z","iopub.execute_input":"2024-01-18T20:23:50.111594Z","iopub.status.idle":"2024-01-18T20:23:50.230407Z","shell.execute_reply.started":"2024-01-18T20:23:50.111565Z","shell.execute_reply":"2024-01-18T20:23:50.229605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Image\nImage(filename='/kaggle/working/test1/2782.jpg') ","metadata":{"execution":{"iopub.status.busy":"2024-01-18T20:31:56.858534Z","iopub.execute_input":"2024-01-18T20:31:56.858913Z","iopub.status.idle":"2024-01-18T20:31:56.866353Z","shell.execute_reply.started":"2024-01-18T20:31:56.858885Z","shell.execute_reply":"2024-01-18T20:31:56.865192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nts = pd.DataFrame(ans, columns=['label'])\nts.index = np.arange(1, len(ts) + 1)\nts.to_csv('submission.csv', index_label='id')","metadata":{"execution":{"iopub.status.busy":"2024-01-18T20:56:39.407158Z","iopub.execute_input":"2024-01-18T20:56:39.407615Z","iopub.status.idle":"2024-01-18T20:56:39.443062Z","shell.execute_reply.started":"2024-01-18T20:56:39.407583Z","shell.execute_reply":"2024-01-18T20:56:39.441908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans = []\n\nfor image in test_image_list:\n    img=load_img(f'/kaggle/working/test1/{image}', target_size=(150, 150))\n  \n    x=img_to_array(img)\n    x /= 255\n    x=np.expand_dims(x, axis=0)\n    images = np.vstack([x])\n  \n    classes = model.predict(images, batch_size=10)\n  \n    #print(classes[0])\n    if classes[0]>0.5:\n        #print(image + \" is a dog\")\n        ans.append(1)\n    else:\n        #print(image + \" is a cat\")\n        ans.append(0)\n    \nans","metadata":{"execution":{"iopub.status.busy":"2024-01-18T20:32:42.971955Z","iopub.execute_input":"2024-01-18T20:32:42.972368Z","iopub.status.idle":"2024-01-18T20:56:39.405247Z","shell.execute_reply.started":"2024-01-18T20:32:42.972337Z","shell.execute_reply":"2024-01-18T20:56:39.404127Z"},"trusted":true},"execution_count":null,"outputs":[]}]}