{"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-12-12T15:57:26.070069Z","iopub.execute_input":"2022-12-12T15:57:26.070668Z","iopub.status.idle":"2022-12-12T15:57:33.291909Z","shell.execute_reply.started":"2022-12-12T15:57:26.070501Z","shell.execute_reply":"2022-12-12T15:57:33.290022Z"},"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-12-12T15:57:33.294118Z","iopub.execute_input":"2022-12-12T15:57:33.294986Z","iopub.status.idle":"2022-12-12T15:57:45.346309Z","shell.execute_reply.started":"2022-12-12T15:57:33.294945Z","shell.execute_reply":"2022-12-12T15:57:45.345256Z"},"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-12-12T15:57:45.347858Z","iopub.execute_input":"2022-12-12T15:57:45.348215Z","iopub.status.idle":"2022-12-12T15:57:50.941675Z","shell.execute_reply.started":"2022-12-12T15:57:45.348178Z","shell.execute_reply":"2022-12-12T15:57:50.940689Z"},"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-12-12T15:57:50.944387Z","iopub.execute_input":"2022-12-12T15:57:50.944784Z","iopub.status.idle":"2022-12-12T15:57:50.950858Z","shell.execute_reply.started":"2022-12-12T15:57:50.944747Z","shell.execute_reply":"2022-12-12T15:57:50.949397Z"},"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-12-12T15:57:50.952728Z","iopub.execute_input":"2022-12-12T15:57:50.953568Z","iopub.status.idle":"2022-12-12T15:57:51.014173Z","shell.execute_reply.started":"2022-12-12T15:57:50.953516Z","shell.execute_reply":"2022-12-12T15:57:51.013068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['category'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T15:57:51.015871Z","iopub.execute_input":"2022-12-12T15:57:51.016248Z","iopub.status.idle":"2022-12-12T15:57:51.029146Z","shell.execute_reply.started":"2022-12-12T15:57:51.016196Z","shell.execute_reply":"2022-12-12T15:57:51.027992Z"},"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-12-12T15:57:51.030407Z","iopub.execute_input":"2022-12-12T15:57:51.031895Z","iopub.status.idle":"2022-12-12T15:57:51.337150Z","shell.execute_reply.started":"2022-12-12T15:57:51.031833Z","shell.execute_reply":"2022-12-12T15:57:51.336261Z"},"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-12-12T15:57:51.338475Z","iopub.execute_input":"2022-12-12T15:57:51.338849Z","iopub.status.idle":"2022-12-12T15:57:51.356115Z","shell.execute_reply.started":"2022-12-12T15:57:51.338815Z","shell.execute_reply":"2022-12-12T15:57:51.355505Z"},"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-12-12T15:57:51.357465Z","iopub.execute_input":"2022-12-12T15:57:51.358104Z","iopub.status.idle":"2022-12-12T15:57:51.373816Z","shell.execute_reply.started":"2022-12-12T15:57:51.358069Z","shell.execute_reply":"2022-12-12T15:57:51.372996Z"},"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-12-12T15:57:51.378482Z","iopub.execute_input":"2022-12-12T15:57:51.378748Z","iopub.status.idle":"2022-12-12T15:57:51.389133Z","shell.execute_reply.started":"2022-12-12T15:57:51.378725Z","shell.execute_reply":"2022-12-12T15:57:51.388288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape , val_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-12T15:57:51.390722Z","iopub.execute_input":"2022-12-12T15:57:51.391133Z","iopub.status.idle":"2022-12-12T15:57:51.398476Z","shell.execute_reply.started":"2022-12-12T15:57:51.391098Z","shell.execute_reply":"2022-12-12T15:57:51.397598Z"},"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-12-12T15:57:51.399652Z","iopub.execute_input":"2022-12-12T15:57:51.400476Z","iopub.status.idle":"2022-12-12T15:57:51.406950Z","shell.execute_reply.started":"2022-12-12T15:57:51.400420Z","shell.execute_reply":"2022-12-12T15:57:51.406318Z"},"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-12-12T15:57:51.408139Z","iopub.execute_input":"2022-12-12T15:57:51.409216Z","iopub.status.idle":"2022-12-12T15:57:51.418616Z","shell.execute_reply.started":"2022-12-12T15:57:51.409184Z","shell.execute_reply":"2022-12-12T15:57:51.417475Z"},"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-12-12T15:57:51.420091Z","iopub.execute_input":"2022-12-12T15:57:51.420710Z","iopub.status.idle":"2022-12-12T15:57:51.611292Z","shell.execute_reply.started":"2022-12-12T15:57:51.420671Z","shell.execute_reply":"2022-12-12T15:57:51.610267Z"},"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-12-12T15:57:51.612814Z","iopub.execute_input":"2022-12-12T15:57:51.613257Z","iopub.status.idle":"2022-12-12T15:57:51.669958Z","shell.execute_reply.started":"2022-12-12T15:57:51.613222Z","shell.execute_reply":"2022-12-12T15:57:51.668992Z"},"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-12-12T15:57:51.671178Z","iopub.execute_input":"2022-12-12T15:57:51.671513Z","iopub.status.idle":"2022-12-12T15:57:51.678065Z","shell.execute_reply.started":"2022-12-12T15:57:51.671480Z","shell.execute_reply":"2022-12-12T15:57:51.676812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow","metadata":{"execution":{"iopub.status.busy":"2022-12-12T15:57:51.679919Z","iopub.execute_input":"2022-12-12T15:57:51.680231Z","iopub.status.idle":"2022-12-12T15:57:51.688142Z","shell.execute_reply.started":"2022-12-12T15:57:51.680205Z","shell.execute_reply":"2022-12-12T15:57:51.687199Z"},"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-12-12T15:57:51.689663Z","iopub.execute_input":"2022-12-12T15:57:51.690080Z","iopub.status.idle":"2022-12-12T15:57:57.694065Z","shell.execute_reply.started":"2022-12-12T15:57:51.690047Z","shell.execute_reply":"2022-12-12T15:57:57.693039Z"},"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-12-12T15:57:57.695401Z","iopub.execute_input":"2022-12-12T15:57:57.695767Z","iopub.status.idle":"2022-12-12T15:57:57.727916Z","shell.execute_reply.started":"2022-12-12T15:57:57.695731Z","shell.execute_reply":"2022-12-12T15:57:57.726829Z"},"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-12-12T15:57:57.729360Z","iopub.execute_input":"2022-12-12T15:57:57.729913Z","iopub.status.idle":"2022-12-12T15:57:57.746750Z","shell.execute_reply.started":"2022-12-12T15:57:57.729878Z","shell.execute_reply":"2022-12-12T15:57:57.745906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T15:57:57.748182Z","iopub.execute_input":"2022-12-12T15:57:57.748531Z","iopub.status.idle":"2022-12-12T15:57:57.776559Z","shell.execute_reply.started":"2022-12-12T15:57:57.748499Z","shell.execute_reply":"2022-12-12T15:57:57.775689Z"},"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-12-12T15:57:57.777949Z","iopub.execute_input":"2022-12-12T15:57:57.778561Z","iopub.status.idle":"2022-12-12T15:57:57.784668Z","shell.execute_reply.started":"2022-12-12T15:57:57.778509Z","shell.execute_reply":"2022-12-12T15:57:57.783588Z"},"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-12-12T15:57:57.786430Z","iopub.execute_input":"2022-12-12T15:57:57.786800Z","iopub.status.idle":"2022-12-12T16:20:49.127814Z","shell.execute_reply.started":"2022-12-12T15:57:57.786767Z","shell.execute_reply":"2022-12-12T16:20:49.126837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('model.h5')","metadata":{"execution":{"iopub.status.busy":"2022-12-12T16:20:49.129505Z","iopub.execute_input":"2022-12-12T16:20:49.130180Z","iopub.status.idle":"2022-12-12T16:20:49.487841Z","shell.execute_reply.started":"2022-12-12T16:20:49.130143Z","shell.execute_reply":"2022-12-12T16:20:49.486875Z"},"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-12-12T16:20:49.489432Z","iopub.execute_input":"2022-12-12T16:20:49.489813Z","iopub.status.idle":"2022-12-12T16:20:49.494396Z","shell.execute_reply.started":"2022-12-12T16:20:49.489771Z","shell.execute_reply":"2022-12-12T16:20:49.493452Z"},"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-12-12T16:20:49.495922Z","iopub.execute_input":"2022-12-12T16:20:49.496523Z","iopub.status.idle":"2022-12-12T16:20:49.514275Z","shell.execute_reply.started":"2022-12-12T16:20:49.496490Z","shell.execute_reply":"2022-12-12T16:20:49.513321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T16:20:49.515571Z","iopub.execute_input":"2022-12-12T16:20:49.516132Z","iopub.status.idle":"2022-12-12T16:20:49.525360Z","shell.execute_reply.started":"2022-12-12T16:20:49.516080Z","shell.execute_reply":"2022-12-12T16:20:49.524234Z"},"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-12-12T16:20:49.533344Z","iopub.execute_input":"2022-12-12T16:20:49.534043Z","iopub.status.idle":"2022-12-12T16:20:49.635163Z","shell.execute_reply.started":"2022-12-12T16:20:49.534018Z","shell.execute_reply":"2022-12-12T16:20:49.633986Z"},"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-12-12T16:20:49.636447Z","iopub.execute_input":"2022-12-12T16:20:49.636781Z","iopub.status.idle":"2022-12-12T16:21:31.898628Z","shell.execute_reply.started":"2022-12-12T16:20:49.636749Z","shell.execute_reply":"2022-12-12T16:21:31.897622Z"},"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-12-12T16:21:31.900063Z","iopub.execute_input":"2022-12-12T16:21:31.900936Z","iopub.status.idle":"2022-12-12T16:21:32.182771Z","shell.execute_reply.started":"2022-12-12T16:21:31.900899Z","shell.execute_reply":"2022-12-12T16:21:32.181769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(test['category'])","metadata":{"execution":{"iopub.status.busy":"2022-12-12T16:21:32.184339Z","iopub.execute_input":"2022-12-12T16:21:32.184948Z","iopub.status.idle":"2022-12-12T16:21:32.379500Z","shell.execute_reply.started":"2022-12-12T16:21:32.184914Z","shell.execute_reply":"2022-12-12T16:21:32.378496Z"},"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-12-12T16:21:32.381106Z","iopub.execute_input":"2022-12-12T16:21:32.381448Z","iopub.status.idle":"2022-12-12T16:21:32.412143Z","shell.execute_reply.started":"2022-12-12T16:21:32.381414Z","shell.execute_reply":"2022-12-12T16:21:32.411119Z"},"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-12-12T16:21:32.413769Z","iopub.execute_input":"2022-12-12T16:21:32.414126Z","iopub.status.idle":"2022-12-12T16:21:32.433873Z","shell.execute_reply.started":"2022-12-12T16:21:32.414074Z","shell.execute_reply":"2022-12-12T16:21:32.433054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}