{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import zipfile, os\n\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2024-04-21T09:36:44.849197Z","iopub.execute_input":"2024-04-21T09:36:44.849966Z","iopub.status.idle":"2024-04-21T09:36:52.007706Z","shell.execute_reply.started":"2024-04-21T09:36:44.849929Z","shell.execute_reply":"2024-04-21T09:36:52.006877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing Data","metadata":{}},{"cell_type":"code","source":"img_width, img_height = 150, 150\ninput_shape = (img_width, img_height, 3)\nbatch_size = 15\nn_train_samples = 20000\nn_validation_samples = 5000\nepochs = 10\nn_images = 25000\nn_classes = 2\nn_class_images = n_images // n_classes\n\nval_data_portion = 0.2","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:36:52.010215Z","iopub.execute_input":"2024-04-21T09:36:52.010922Z","iopub.status.idle":"2024-04-21T09:36:52.016480Z","shell.execute_reply.started":"2024-04-21T09:36:52.010874Z","shell.execute_reply":"2024-04-21T09:36:52.015512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_directory(dir_name):\n    if os.path.exists(dir_name):\n        shutil.rmtree(dir_name)\n    os.makedirs(dir_name)\n    os.makedirs(os.path.join(dir_name, \"cats\"))\n    os.makedirs(os.path.join(dir_name, \"dogs\"))","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:36:52.017731Z","iopub.execute_input":"2024-04-21T09:36:52.018107Z","iopub.status.idle":"2024-04-21T09:36:52.546903Z","shell.execute_reply.started":"2024-04-21T09:36:52.018048Z","shell.execute_reply":"2024-04-21T09:36:52.545921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = '/kaggle/working/train'\ntrain_dir = '/kaggle/working/set/train' \nval_dir = '/kaggle/working/set/val' ","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:36:52.548341Z","iopub.execute_input":"2024-04-21T09:36:52.548748Z","iopub.status.idle":"2024-04-21T09:36:52.558300Z","shell.execute_reply.started":"2024-04-21T09:36:52.548710Z","shell.execute_reply":"2024-04-21T09:36:52.557364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_directory(train_dir)\ncreate_directory(val_dir)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:36:52.560992Z","iopub.execute_input":"2024-04-21T09:36:52.561284Z","iopub.status.idle":"2024-04-21T09:36:53.511376Z","shell.execute_reply.started":"2024-04-21T09:36:52.561259Z","shell.execute_reply":"2024-04-21T09:36:53.510244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\ndef copy_images(start_index, end_index, source_dir, dest_dir):\n    for i in range(start_index, end_index):\n        shutil.copy2(os.path.join(source_dir, \"cat.\" + str(i) + \".jpg\"), \n                     os.path.join(dest_dir, \"cats\"))\n        shutil.copy2(os.path.join(source_dir, \"dog.\" + str(i) + \".jpg\"),\n                     os.path.join(dest_dir, \"dogs\"))","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:36:53.512540Z","iopub.execute_input":"2024-04-21T09:36:53.512822Z","iopub.status.idle":"2024-04-21T09:36:53.520701Z","shell.execute_reply.started":"2024-04-21T09:36:53.512797Z","shell.execute_reply":"2024-04-21T09:36:53.519573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start_val_data_idx = int(n_class_images * (1-val_data_portion))\n\n\ncopy_images(0, start_val_data_idx, data_dir, train_dir)\ncopy_images(start_val_data_idx, 12500, data_dir, val_dir)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:36:53.522257Z","iopub.execute_input":"2024-04-21T09:36:53.522616Z","iopub.status.idle":"2024-04-21T09:36:57.245191Z","shell.execute_reply.started":"2024-04-21T09:36:53.522583Z","shell.execute_reply":"2024-04-21T09:36:57.244246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Cats in train set: ', len(os.listdir(train_dir + '/cats')))\nprint('Dogs in train set: ', len(os.listdir(train_dir + '/dogs')), '\\n')\n\nprint('Cats in validation set: ', len(os.listdir(val_dir + '/cats')))\nprint('Cats in validation set: ', len(os.listdir(val_dir + '/dogs')), '\\n')","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:36:57.246432Z","iopub.execute_input":"2024-04-21T09:36:57.246749Z","iopub.status.idle":"2024-04-21T09:36:57.269188Z","shell.execute_reply.started":"2024-04-21T09:36:57.246721Z","shell.execute_reply":"2024-04-21T09:36:57.268268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom tensorflow.keras.preprocessing import image","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:36:57.270562Z","iopub.execute_input":"2024-04-21T09:36:57.270944Z","iopub.status.idle":"2024-04-21T09:36:57.275849Z","shell.execute_reply.started":"2024-04-21T09:36:57.270909Z","shell.execute_reply":"2024-04-21T09:36:57.274852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Showing some cats","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor i in range(25):\n    plt.subplot(5,5,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    image_file_name = train_dir + '/cats/' + os.listdir(train_dir+\"/cats\")[i] \n    img = image.load_img(image_file_name, target_size=(150, 150))\n    plt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:36:57.277106Z","iopub.execute_input":"2024-04-21T09:36:57.277353Z","iopub.status.idle":"2024-04-21T09:36:58.946931Z","shell.execute_reply.started":"2024-04-21T09:36:57.277331Z","shell.execute_reply":"2024-04-21T09:36:58.945862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Showing some dogs","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor i in range(25):\n    plt.subplot(5,5,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    image_file_name = train_dir + '/dogs/' + os.listdir(train_dir+\"/dogs\")[i] \n    img = image.load_img(image_file_name, target_size=(150, 150))\n    plt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:36:58.948220Z","iopub.execute_input":"2024-04-21T09:36:58.948522Z","iopub.status.idle":"2024-04-21T09:37:00.932755Z","shell.execute_reply.started":"2024-04-21T09:36:58.948495Z","shell.execute_reply":"2024-04-21T09:37:00.929221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\ntrain_datagen = ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest',\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:37:00.934389Z","iopub.execute_input":"2024-04-21T09:37:00.934812Z","iopub.status.idle":"2024-04-21T09:37:00.941036Z","shell.execute_reply.started":"2024-04-21T09:37:00.934781Z","shell.execute_reply":"2024-04-21T09:37:00.939899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_file_name = train_dir + '/cats/cat.11.jpg'\nimg = image.load_img(image_file_name, target_size=(150, 150))\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:37:00.942426Z","iopub.execute_input":"2024-04-21T09:37:00.942754Z","iopub.status.idle":"2024-04-21T09:37:01.362207Z","shell.execute_reply.started":"2024-04-21T09:37:00.942715Z","shell.execute_reply":"2024-04-21T09:37:01.361202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = image.img_to_array(img)\nx = x.reshape((1,) + x.shape)\ni = 0\nfor batch in train_datagen.flow(x, batch_size=1):\n    plt.figure(i)\n    imgplot = plt.imshow(image.array_to_img(batch[0]))\n    i += 1\n    if i == 5:\n        break\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:37:01.366638Z","iopub.execute_input":"2024-04-21T09:37:01.366985Z","iopub.status.idle":"2024-04-21T09:37:03.154006Z","shell.execute_reply.started":"2024-04-21T09:37:01.366956Z","shell.execute_reply":"2024-04-21T09:37:03.152837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating model using transfer learning with ResNet_V2","metadata":{}},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization\nfrom tensorflow.keras.layers import Activation, Dropout, Flatten, Dense\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.applications import resnet_v2","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:37:03.155530Z","iopub.execute_input":"2024-04-21T09:37:03.156298Z","iopub.status.idle":"2024-04-21T09:37:03.162920Z","shell.execute_reply.started":"2024-04-21T09:37:03.156255Z","shell.execute_reply":"2024-04-21T09:37:03.161886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transfer_net = resnet_v2.ResNet50V2(weights ='imagenet', include_top = False, input_shape = input_shape)\ntransfer_net.trainable = False","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:37:03.164276Z","iopub.execute_input":"2024-04-21T09:37:03.164596Z","iopub.status.idle":"2024-04-21T09:37:04.323863Z","shell.execute_reply.started":"2024-04-21T09:37:03.164569Z","shell.execute_reply":"2024-04-21T09:37:04.322714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(transfer_net)\nmodel.add(Flatten())\nmodel.add(Dense(512, activation = 'relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1, activation = 'sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:37:04.325173Z","iopub.execute_input":"2024-04-21T09:37:04.325491Z","iopub.status.idle":"2024-04-21T09:37:04.337446Z","shell.execute_reply.started":"2024-04-21T09:37:04.325465Z","shell.execute_reply":"2024-04-21T09:37:04.336475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:37:04.338820Z","iopub.execute_input":"2024-04-21T09:37:04.339353Z","iopub.status.idle":"2024-04-21T09:37:04.370695Z","shell.execute_reply.started":"2024-04-21T09:37:04.339316Z","shell.execute_reply":"2024-04-21T09:37:04.369746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = Adam(learning_rate=0.00001), loss ='binary_crossentropy', metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:37:04.371812Z","iopub.execute_input":"2024-04-21T09:37:04.372107Z","iopub.status.idle":"2024-04-21T09:37:04.387267Z","shell.execute_reply.started":"2024-04-21T09:37:04.372062Z","shell.execute_reply":"2024-04-21T09:37:04.386463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale = 1./255)\ntrain_gen = train_datagen.flow_from_directory(train_dir, target_size = (img_width, img_height), batch_size = batch_size, class_mode ='binary')\nval_gen = datagen.flow_from_directory(val_dir, target_size = (img_width, img_height), batch_size = batch_size, class_mode ='binary')","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:37:04.388345Z","iopub.execute_input":"2024-04-21T09:37:04.388670Z","iopub.status.idle":"2024-04-21T09:37:05.113978Z","shell.execute_reply.started":"2024-04-21T09:37:04.388645Z","shell.execute_reply":"2024-04-21T09:37:05.113030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_gen, \n                    steps_per_epoch = n_train_samples // epochs, \n                    epochs = epochs, \n                    validation_data = val_gen, \n                    validation_steps = n_validation_samples // epochs)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T09:37:05.115271Z","iopub.execute_input":"2024-04-21T09:37:05.116319Z","iopub.status.idle":"2024-04-21T10:00:53.907149Z","shell.execute_reply.started":"2024-04-21T09:37:05.116289Z","shell.execute_reply":"2024-04-21T10:00:53.906119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(2,1)\nax[0].plot(history.history['loss'], color='b', label=\"Training loss\")\nax[0].plot(history.history['val_loss'], color='r', label=\"validation loss\",axes =ax[0])\nlegend = ax[0].legend(loc='best', shadow=True)\n\nax[1].plot(history.history['accuracy'], color='b', label=\"Training accuracy\")\nax[1].plot(history.history['val_accuracy'], color='r',label=\"Validation accuracy\")\nlegend = ax[1].legend(loc='best', shadow=True)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T10:02:27.691191Z","iopub.execute_input":"2024-04-21T10:02:27.691619Z","iopub.status.idle":"2024-04-21T10:02:28.157234Z","shell.execute_reply.started":"2024-04-21T10:02:27.691590Z","shell.execute_reply":"2024-04-21T10:02:28.156109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving model","metadata":{}},{"cell_type":"code","source":"model.save_weights(\"resnet.weights.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-04-21T10:00:54.494946Z","iopub.execute_input":"2024-04-21T10:00:54.495407Z","iopub.status.idle":"2024-04-21T10:00:55.723041Z","shell.execute_reply.started":"2024-04-21T10:00:54.495366Z","shell.execute_reply":"2024-04-21T10:00:55.721821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing test images","metadata":{}},{"cell_type":"code","source":"import zipfile, os\n\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2024-04-21T10:00:55.724716Z","iopub.execute_input":"2024-04-21T10:00:55.725128Z","iopub.status.idle":"2024-04-21T10:01:01.914789Z","shell.execute_reply.started":"2024-04-21T10:00:55.725094Z","shell.execute_reply":"2024-04-21T10:01:01.913541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ntest_filenames = os.listdir(\"/kaggle/working/test1\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nn_samples = test_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2024-04-21T10:01:01.916222Z","iopub.execute_input":"2024-04-21T10:01:01.916561Z","iopub.status.idle":"2024-04-21T10:01:01.933959Z","shell.execute_reply.started":"2024-04-21T10:01:01.916533Z","shell.execute_reply":"2024-04-21T10:01:01.932643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 10\n\ntest_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"/kaggle/working/test1\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=(150, 150),\n    batch_size=batch_size,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T10:01:01.935729Z","iopub.execute_input":"2024-04-21T10:01:01.936127Z","iopub.status.idle":"2024-04-21T10:01:02.094908Z","shell.execute_reply.started":"2024-04-21T10:01:01.936086Z","shell.execute_reply":"2024-04-21T10:01:02.093813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predicting on test images","metadata":{}},{"cell_type":"code","source":"predict = model.predict(test_generator, steps=n_samples//batch_size)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T10:01:02.096396Z","iopub.execute_input":"2024-04-21T10:01:02.096811Z","iopub.status.idle":"2024-04-21T10:01:33.868296Z","shell.execute_reply.started":"2024-04-21T10:01:02.096772Z","shell.execute_reply":"2024-04-21T10:01:33.867129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-21T10:01:33.870121Z","iopub.execute_input":"2024-04-21T10:01:33.871094Z","iopub.status.idle":"2024-04-21T10:01:33.877499Z","shell.execute_reply.started":"2024-04-21T10:01:33.871025Z","shell.execute_reply":"2024-04-21T10:01:33.876516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"predict","metadata":{"execution":{"iopub.status.busy":"2024-04-21T04:58:39.337502Z","iopub.status.idle":"2024-04-21T04:58:39.337821Z","shell.execute_reply.started":"2024-04-21T04:58:39.337665Z","shell.execute_reply":"2024-04-21T04:58:39.337679Z"}}},{"cell_type":"code","source":"category = np.rint(predict)\ntest_df['label'] = category","metadata":{"execution":{"iopub.status.busy":"2024-04-21T10:01:33.879000Z","iopub.execute_input":"2024-04-21T10:01:33.879386Z","iopub.status.idle":"2024-04-21T10:01:33.887998Z","shell.execute_reply.started":"2024-04-21T10:01:33.879357Z","shell.execute_reply":"2024-04-21T10:01:33.886851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2024-04-21T10:01:33.889584Z","iopub.execute_input":"2024-04-21T10:01:33.890332Z","iopub.status.idle":"2024-04-21T10:01:33.917822Z","shell.execute_reply.started":"2024-04-21T10:01:33.890291Z","shell.execute_reply":"2024-04-21T10:01:33.916749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving result","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nsample_test = test_df.sample(n=9).reset_index()\nsample_test.head()\nplt.figure(figsize=(12, 12))\nfor index, row in sample_test.iterrows():\n    filename = row['filename']\n    category = 'Cat'if row['label']==0 else 'Dog'\n    img = np.asarray(Image.open(\"/kaggle/working/test1/\"+filename))\n    plt.subplot(3, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename + '  Category: (' + \"{}\".format(category) + ')')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-21T10:04:23.096050Z","iopub.execute_input":"2024-04-21T10:04:23.096840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = test_df.copy()\nsubmission['id'] = submission['filename'].str.split('.').str[0]\nsubmission.drop(['filename'], axis=1, inplace=True)\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T10:01:33.919110Z","iopub.execute_input":"2024-04-21T10:01:33.919464Z","iopub.status.idle":"2024-04-21T10:01:33.977290Z","shell.execute_reply.started":"2024-04-21T10:01:33.919436Z","shell.execute_reply":"2024-04-21T10:01:33.976149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}