{"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":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom keras.preprocessing.image import ImageDataGenerator , load_img\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\n\nimport os\nprint(os.listdir('../input/dogs-vs-cats'))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-20T07:32:55.694936Z","iopub.execute_input":"2022-11-20T07:32:55.695687Z","iopub.status.idle":"2022-11-20T07:33:03.521854Z","shell.execute_reply.started":"2022-11-20T07:32:55.695586Z","shell.execute_reply":"2022-11-20T07:33:03.520823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\ntrain_zip = zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\")\ntrain_zip.extractall(\"/kaggle/working/dataset\")\ntrain_zip.close()","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:33:05.620495Z","iopub.execute_input":"2022-11-20T07:33:05.621113Z","iopub.status.idle":"2022-11-20T07:33:17.147718Z","shell.execute_reply.started":"2022-11-20T07:33:05.621079Z","shell.execute_reply":"2022-11-20T07:33:17.146592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\ntrain_zip = zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\")\ntrain_zip.extractall(\"/kaggle/working/dataset\")\ntrain_zip.close()","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:33:19.410510Z","iopub.execute_input":"2022-11-20T07:33:19.410896Z","iopub.status.idle":"2022-11-20T07:33:25.582730Z","shell.execute_reply.started":"2022-11-20T07:33:19.410862Z","shell.execute_reply":"2022-11-20T07:33:25.581670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image_width = 128\nImage_height = 128\nImage_size = (Image_width , Image_height)\nImage_channel = 3\n\nImage_rgb_size = (Image_width ,Image_height , 3 )","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:33:28.237831Z","iopub.execute_input":"2022-11-20T07:33:28.238216Z","iopub.status.idle":"2022-11-20T07:33:28.244405Z","shell.execute_reply.started":"2022-11-20T07:33:28.238186Z","shell.execute_reply":"2022-11-20T07:33:28.243252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir('./dataset/train')\ncategories = []\nfor filename in filenames:\n    category = filename.split('.')[0]\n    if category == 'dog':\n        categories.append(1)\n    else:\n        categories.append(0)\ndata = pd.DataFrame({'filename' : filenames , 'category' : categories})\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:33:31.918851Z","iopub.execute_input":"2022-11-20T07:33:31.919391Z","iopub.status.idle":"2022-11-20T07:33:32.032232Z","shell.execute_reply.started":"2022-11-20T07:33:31.919333Z","shell.execute_reply":"2022-11-20T07:33:32.030825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['category'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:33:33.546482Z","iopub.execute_input":"2022-11-20T07:33:33.546881Z","iopub.status.idle":"2022-11-20T07:33:33.573616Z","shell.execute_reply.started":"2022-11-20T07:33:33.546850Z","shell.execute_reply":"2022-11-20T07:33:33.572538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nsample = random.choice(filenames)\nimage = load_img('./dataset/train/{}'.format(sample))\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:33:34.488829Z","iopub.execute_input":"2022-11-20T07:33:34.489182Z","iopub.status.idle":"2022-11-20T07:33:34.769628Z","shell.execute_reply.started":"2022-11-20T07:33:34.489154Z","shell.execute_reply":"2022-11-20T07:33:34.768658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['category1'] = data['category'].apply(lambda x: 'cat' if x== 0 else 'dog')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:33:35.534959Z","iopub.execute_input":"2022-11-20T07:33:35.535309Z","iopub.status.idle":"2022-11-20T07:33:35.552236Z","shell.execute_reply.started":"2022-11-20T07:33:35.535282Z","shell.execute_reply":"2022-11-20T07:33:35.551247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.drop(['category'] , inplace = True , axis = 1)\ndata.rename(columns  = {'category1' : 'category'} , inplace =True)\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:33:37.694452Z","iopub.execute_input":"2022-11-20T07:33:37.695505Z","iopub.status.idle":"2022-11-20T07:33:37.711740Z","shell.execute_reply.started":"2022-11-20T07:33:37.695456Z","shell.execute_reply":"2022-11-20T07:33:37.710818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df , val_df = train_test_split(data , test_size = 0.2 , random_state = 42)\ntrain_df = train_df.reset_index(drop = True)\nval_df = val_df.reset_index(drop = True)","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:33:39.387522Z","iopub.execute_input":"2022-11-20T07:33:39.388349Z","iopub.status.idle":"2022-11-20T07:33:39.617136Z","shell.execute_reply.started":"2022-11-20T07:33:39.388309Z","shell.execute_reply":"2022-11-20T07:33:39.616064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape , val_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:33:41.193417Z","iopub.execute_input":"2022-11-20T07:33:41.193794Z","iopub.status.idle":"2022-11-20T07:33:41.201900Z","shell.execute_reply.started":"2022-11-20T07:33:41.193745Z","shell.execute_reply":"2022-11-20T07:33:41.200845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size  = 15\nepochs = 7 #due to speed\ntotal_train = train_df.shape[0]\ntotal_validate = val_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:33:42.691984Z","iopub.execute_input":"2022-11-20T07:33:42.692342Z","iopub.status.idle":"2022-11-20T07:33:42.700612Z","shell.execute_reply.started":"2022-11-20T07:33:42.692310Z","shell.execute_reply":"2022-11-20T07:33:42.699390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dategen = ImageDataGenerator(rotation_range = 15 , \n                                  rescale = 1.0/255 ,\n                                  shear_range = 0.1,\n                                  zoom_range = 0.2 , \n                                  horizontal_flip = True , \n                                  width_shift_range = 0.1 , \n                                  height_shift_range = 0.1\n                                  )","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:33:43.654868Z","iopub.execute_input":"2022-11-20T07:33:43.655775Z","iopub.status.idle":"2022-11-20T07:33:43.661919Z","shell.execute_reply.started":"2022-11-20T07:33:43.655725Z","shell.execute_reply":"2022-11-20T07:33:43.660818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_dategen.flow_from_dataframe(\n    train_df, \n    \"./dataset/train\", \n    x_col='filename',\n    y_col='category',\n    target_size=Image_size,\n    class_mode='categorical',\n    batch_size=batch_size\n)  ","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:33:45.213906Z","iopub.execute_input":"2022-11-20T07:33:45.214286Z","iopub.status.idle":"2022-11-20T07:33:45.401366Z","shell.execute_reply.started":"2022-11-20T07:33:45.214256Z","shell.execute_reply":"2022-11-20T07:33:45.400260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale = 1./255)\nval_generator = validation_datagen.flow_from_dataframe(\n    val_df, \n    \"./dataset/train\", \n    x_col='filename',\n    y_col='category',\n    target_size=Image_size,\n    class_mode='categorical',\n    batch_size=batch_size\n)  ","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:33:46.374020Z","iopub.execute_input":"2022-11-20T07:33:46.374376Z","iopub.status.idle":"2022-11-20T07:33:46.432263Z","shell.execute_reply.started":"2022-11-20T07:33:46.374345Z","shell.execute_reply":"2022-11-20T07:33:46.431171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import GlobalAveragePooling2D\nfrom keras.models import Model","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:33:47.447184Z","iopub.execute_input":"2022-11-20T07:33:47.447998Z","iopub.status.idle":"2022-11-20T07:33:47.453360Z","shell.execute_reply.started":"2022-11-20T07:33:47.447961Z","shell.execute_reply":"2022-11-20T07:33:47.451919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:35:03.433218Z","iopub.execute_input":"2022-11-20T07:35:03.433581Z","iopub.status.idle":"2022-11-20T07:35:03.439034Z","shell.execute_reply.started":"2022-11-20T07:35:03.433548Z","shell.execute_reply":"2022-11-20T07:35:03.437990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from  tensorflow.keras.models import Sequential\nfrom  tensorflow.keras.layers import Conv2D , MaxPooling2D , Dropout , Flatten , Dense , Activation , BatchNormalization\n\nbase = tensorflow.keras.applications.resnet50.ResNet50(weights = 'imagenet' , include_top = False , input_shape = Image_rgb_size)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:35:07.443936Z","iopub.execute_input":"2022-11-20T07:35:07.444330Z","iopub.status.idle":"2022-11-20T07:35:13.490532Z","shell.execute_reply.started":"2022-11-20T07:35:07.444298Z","shell.execute_reply":"2022-11-20T07:35:13.489486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = base.output\nx = GlobalAveragePooling2D()(x)\nx = Dropout(0.7)(x)\npredictions = Dense(2, activation= 'softmax')(x)\nmodel = Model(inputs = base.input, outputs = predictions)","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:35:19.971535Z","iopub.execute_input":"2022-11-20T07:35:19.972175Z","iopub.status.idle":"2022-11-20T07:35:20.004349Z","shell.execute_reply.started":"2022-11-20T07:35:19.972137Z","shell.execute_reply":"2022-11-20T07:35:20.003422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam \n# sgd = SGD(lr=lrate, momentum=0.9, decay=decay, nesterov=False)\nadam = Adam(learning_rate=0.0001)\nmodel.compile(optimizer= adam, loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:35:21.504547Z","iopub.execute_input":"2022-11-20T07:35:21.504939Z","iopub.status.idle":"2022-11-20T07:35:21.526541Z","shell.execute_reply.started":"2022-11-20T07:35:21.504907Z","shell.execute_reply":"2022-11-20T07:35:21.525353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:35:22.738363Z","iopub.execute_input":"2022-11-20T07:35:22.739336Z","iopub.status.idle":"2022-11-20T07:35:22.767038Z","shell.execute_reply.started":"2022-11-20T07:35:22.739290Z","shell.execute_reply":"2022-11-20T07:35:22.765939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau\nearlystop = EarlyStopping(patience = 10)\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=2, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)\ncallbacks = [earlystop , learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:35:27.884474Z","iopub.execute_input":"2022-11-20T07:35:27.884845Z","iopub.status.idle":"2022-11-20T07:35:27.891408Z","shell.execute_reply.started":"2022-11-20T07:35:27.884814Z","shell.execute_reply":"2022-11-20T07:35:27.889795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(\n    train_generator , \n    epochs = epochs , \n    validation_data=val_generator,\n    validation_steps=total_validate//batch_size,\n    steps_per_epoch=total_train//batch_size,\n    callbacks=callbacks\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-20T07:35:29.673874Z","iopub.execute_input":"2022-11-20T07:35:29.676098Z","iopub.status.idle":"2022-11-20T08:03:09.123324Z","shell.execute_reply.started":"2022-11-20T07:35:29.676042Z","shell.execute_reply":"2022-11-20T08:03:09.118466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('model.h5')","metadata":{"execution":{"iopub.status.busy":"2022-11-20T08:03:20.000900Z","iopub.execute_input":"2022-11-20T08:03:20.001259Z","iopub.status.idle":"2022-11-20T08:03:20.368143Z","shell.execute_reply.started":"2022-11-20T08:03:20.001228Z","shell.execute_reply":"2022-11-20T08:03:20.367118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))\n# ax1.plot(history.history['loss'], color='b', label=\"Training loss\")\n# ax1.plot(history.history['val_loss'], color='r', label=\"validation loss\")\n# ax1.set_xticks(np.arange(1, epochs, 1))\n# ax1.set_yticks(np.arange(0, 1, 0.1))\n\n# ax2.plot(history.history['acc'], color='b', label=\"Training accuracy\")\n# ax2.plot(history.history['val_acc'], color='r',label=\"Validation accuracy\")\n# ax2.set_xticks(np.arange(1, epochs, 1))\n\n# legend = plt.legend(loc='best', shadow=True)\n# plt.tight_layout()\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-20T08:03:54.862161Z","iopub.execute_input":"2022-11-20T08:03:54.862562Z","iopub.status.idle":"2022-11-20T08:03:54.867173Z","shell.execute_reply.started":"2022-11-20T08:03:54.862528Z","shell.execute_reply":"2022-11-20T08:03:54.866164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = os.listdir('./dataset/test1')\ntest = pd.DataFrame({'filename' : test_filenames})\nnb_samples = test.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-11-20T08:05:42.770607Z","iopub.execute_input":"2022-11-20T08:05:42.771398Z","iopub.status.idle":"2022-11-20T08:05:42.786134Z","shell.execute_reply.started":"2022-11-20T08:05:42.771352Z","shell.execute_reply":"2022-11-20T08:05:42.785259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-20T08:06:14.550272Z","iopub.execute_input":"2022-11-20T08:06:14.550623Z","iopub.status.idle":"2022-11-20T08:06:14.561718Z","shell.execute_reply.started":"2022-11-20T08:06:14.550592Z","shell.execute_reply":"2022-11-20T08:06:14.560803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test , \n    './dataset/test1' ,\n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=Image_size,\n    batch_size=batch_size,\n    shuffle=False\n    )","metadata":{"execution":{"iopub.status.busy":"2022-11-20T08:06:22.874340Z","iopub.execute_input":"2022-11-20T08:06:22.874708Z","iopub.status.idle":"2022-11-20T08:06:22.979682Z","shell.execute_reply.started":"2022-11-20T08:06:22.874676Z","shell.execute_reply":"2022-11-20T08:06:22.978622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict_generator(test_generator , steps = np.ceil(nb_samples/batch_size))\ntest['category'] = np.argmax(predict  , axis= -1)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-20T08:06:33.946439Z","iopub.execute_input":"2022-11-20T08:06:33.946916Z","iopub.status.idle":"2022-11-20T08:07:17.271815Z","shell.execute_reply.started":"2022-11-20T08:06:33.946876Z","shell.execute_reply":"2022-11-20T08:07:17.270694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = load_img('./dataset/test1/{}'.format('1366.jpg'))\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2022-11-20T08:13:07.657592Z","iopub.execute_input":"2022-11-20T08:13:07.658550Z","iopub.status.idle":"2022-11-20T08:13:07.932043Z","shell.execute_reply.started":"2022-11-20T08:13:07.658518Z","shell.execute_reply":"2022-11-20T08:13:07.931196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(test['category'])","metadata":{"execution":{"iopub.status.busy":"2022-11-20T08:14:28.798837Z","iopub.execute_input":"2022-11-20T08:14:28.799237Z","iopub.status.idle":"2022-11-20T08:14:28.996825Z","shell.execute_reply.started":"2022-11-20T08:14:28.799196Z","shell.execute_reply":"2022-11-20T08:14:28.995782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = test.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-20T08:16:43.245788Z","iopub.execute_input":"2022-11-20T08:16:43.246364Z","iopub.status.idle":"2022-11-20T08:16:43.298990Z","shell.execute_reply.started":"2022-11-20T08:16:43.246321Z","shell.execute_reply":"2022-11-20T08:16:43.298000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.drop(['filename' , 'category'] , axis = 1 , inplace = True)\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-20T08:17:45.829563Z","iopub.execute_input":"2022-11-20T08:17:45.830317Z","iopub.status.idle":"2022-11-20T08:17:45.864502Z","shell.execute_reply.started":"2022-11-20T08:17:45.830277Z","shell.execute_reply":"2022-11-20T08:17:45.863331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}