{"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":"\nfrom glob import glob\nimport re\nimport zipfile\nimport shutil\nimport pandas as pd\nfrom PIL import Image\nimport os\nfrom glob import glob\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import load_img, ImageDataGenerator\nfrom tensorflow.keras import datasets, layers, models\nfrom tensorflow.keras.regularizers import l2\nimport matplotlib.pyplot as plt\nimport numpy as np","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-18T07:07:25.45116Z","iopub.execute_input":"2021-10-18T07:07:25.451383Z","iopub.status.idle":"2021-10-18T07:07:30.279366Z","shell.execute_reply.started":"2021-10-18T07:07:25.451319Z","shell.execute_reply":"2021-10-18T07:07:30.278623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(\"../input/dogs-vs-cats-redux-kernels-edition/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"../input/dogs-vs-cats-redux-kernels-edition/test.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2021-10-18T07:07:33.446538Z","iopub.execute_input":"2021-10-18T07:07:33.447277Z","iopub.status.idle":"2021-10-18T07:07:52.676444Z","shell.execute_reply.started":"2021-10-18T07:07:33.447235Z","shell.execute_reply":"2021-10-18T07:07:52.67563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = []                                     # create empty folder name as train\nlabel = []                                     # create empty folder name as label\ndct = {'cat':0 , 'dog': 1}\n# os.listdir returns the lisкенкенкеt of files in the folder, in this case image class names\nfor i in os.listdir('./train'):                        \n    \n    train_class = i.split(\".\", 1)\n    \n    for j in train_class[0:1]:\n        \n        train.append(i)                         # append image file into the new folder train\n        \n        label.append(str(dct[j]))                     # append the name of folder as the label of the image file\n    \n        \nfull_df = pd.DataFrame({'Image': train,'Labels': label})  # create data frame from dictionary with 2 coloums Image and Labels\nfull_df","metadata":{"execution":{"iopub.status.busy":"2021-10-18T07:09:13.812685Z","iopub.execute_input":"2021-10-18T07:09:13.813389Z","iopub.status.idle":"2021-10-18T07:09:13.887287Z","shell.execute_reply.started":"2021-10-18T07:09:13.813345Z","shell.execute_reply":"2021-10-18T07:09:13.886446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_base = '/kaggle/working/train'\nb_size = 64\ntrain_data_generator = ImageDataGenerator(\n            rescale=1./255.,\n            validation_split=0.2,\n            horizontal_flip=True\n            )\n\n\ntrain_generator = train_data_generator.flow_from_dataframe(dataframe=full_df,\n            directory=\"./train/\",\n            x_col=\"Image\",\n            y_col=\"Labels\",\n            subset=\"training\",\n            batch_size=b_size,\n            shuffle=True,\n            color_mode=\"rgb\",\n            target_size=(64,64),\n            class_mode=\"categorical\")\n\n\nvalidation_generator = train_data_generator.flow_from_dataframe(dataframe=full_df,\n            directory=\"./train/\",\n            x_col=\"Image\",\n            y_col=\"Labels\",\n            subset=\"validation\",\n            batch_size=b_size,\n            shuffle=True,\n            color_mode=\"rgb\",\n            target_size=(64,64),\n            class_mode=\"categorical\")","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:16:38.187753Z","iopub.execute_input":"2021-10-17T14:16:38.188436Z","iopub.status.idle":"2021-10-17T14:16:38.591515Z","shell.execute_reply.started":"2021-10-17T14:16:38.188398Z","shell.execute_reply":"2021-10-17T14:16:38.590649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn = models.Sequential()\ncnn.add(layers.Conv2D(32, (3, 3), padding='same',\n                 input_shape=(64, 64, 3)))\ncnn.add(layers.Activation('relu'))\ncnn.add(layers.BatchNormalization())\ncnn.add(layers.Conv2D(32, (3, 3)))\ncnn.add(layers.Activation('relu'))\ncnn.add(layers.BatchNormalization())\ncnn.add(layers.MaxPooling2D(pool_size=(2, 2)))\ncnn.add(layers.Dropout(0.25))\n\ncnn.add(layers.Conv2D(64, (3, 3), padding='same'))\ncnn.add(layers.Activation('relu'))\ncnn.add(layers.BatchNormalization())\ncnn.add(layers.Conv2D(64, (3, 3)))\ncnn.add(layers.Activation('relu'))\ncnn.add(layers.BatchNormalization())\ncnn.add(layers.MaxPooling2D(pool_size=(2, 2)))\ncnn.add(layers.Dropout(0.25))\n\ncnn.add(layers.Flatten())\ncnn.add(layers.Dense(256, kernel_regularizer=l2(0.01)))\ncnn.add(layers.Activation('relu'))\ncnn.add(layers.Dropout(0.3))\ncnn.add(layers.Dense(2))\ncnn.add(layers.Activation('softmax'))\n#   categorical_crossentropy\ncnn.compile(optimizer='adam', loss = 'categorical_crossentropy', metrics=['accuracy'])\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:16:41.577312Z","iopub.execute_input":"2021-10-17T14:16:41.577887Z","iopub.status.idle":"2021-10-17T14:16:41.719198Z","shell.execute_reply.started":"2021-10-17T14:16:41.577841Z","shell.execute_reply":"2021-10-17T14:16:41.717849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = cnn.fit(train_generator, \n                    epochs= 20,\n                    validation_data=validation_generator,\n                    shuffle = True\n                   )","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:16:44.470096Z","iopub.execute_input":"2021-10-17T14:16:44.47044Z","iopub.status.idle":"2021-10-17T14:36:18.718831Z","shell.execute_reply.started":"2021-10-17T14:16:44.470367Z","shell.execute_reply":"2021-10-17T14:36:18.718126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.show()\n\n\nplt.plot(history.history['val_accuracy'])\nplt.title('model val_accuracy')\nplt.ylabel('val_accuracy')\nplt.xlabel('epoch')\nplt.show()\n\n\nplt.plot(history.history['loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.show()\n\n\nplt.plot(history.history['val_loss'])\nplt.title('model val_loss')\nplt.ylabel('val_loss')\nplt.xlabel('epoch')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:36:20.248473Z","iopub.execute_input":"2021-10-17T14:36:20.248733Z","iopub.status.idle":"2021-10-17T14:36:21.029046Z","shell.execute_reply.started":"2021-10-17T14:36:20.248705Z","shell.execute_reply":"2021-10-17T14:36:21.028345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir ./data/\n!mv ./test ./data","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:36:23.968179Z","iopub.execute_input":"2021-10-17T14:36:23.96873Z","iopub.status.idle":"2021-10-17T14:36:25.445202Z","shell.execute_reply.started":"2021-10-17T14:36:23.96869Z","shell.execute_reply":"2021-10-17T14:36:25.444222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_generator = ImageDataGenerator(rescale=1./255.)\ntest_generator = test_data_generator.flow_from_directory(directory='./data',\n            batch_size=b_size,\n            shuffle=False,color_mode='rgb',\n            target_size=(64,64),\n            class_mode='categorical')","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:37:47.402259Z","iopub.execute_input":"2021-10-17T14:37:47.402566Z","iopub.status.idle":"2021-10-17T14:37:47.836014Z","shell.execute_reply.started":"2021-10-17T14:37:47.402533Z","shell.execute_reply":"2021-10-17T14:37:47.835177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = cnn.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:37:50.958203Z","iopub.execute_input":"2021-10-17T14:37:50.958565Z","iopub.status.idle":"2021-10-17T14:38:18.414265Z","shell.execute_reply.started":"2021-10-17T14:37:50.958518Z","shell.execute_reply":"2021-10-17T14:38:18.413523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = ['cat', 'dog']\nprint(prediction)","metadata":{"execution":{"iopub.status.busy":"2021-10-17T13:47:32.747319Z","iopub.execute_input":"2021-10-17T13:47:32.747891Z","iopub.status.idle":"2021-10-17T13:47:32.753198Z","shell.execute_reply.started":"2021-10-17T13:47:32.747832Z","shell.execute_reply":"2021-10-17T13:47:32.752181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nprediction = np.argmax(prediction, axis = 1)\nsubmission = pd.DataFrame(columns=['id', 'label'], dtype=str)\nsubmission['label'] = [str(i) for i in prediction]\nsubmission['id'] = [ (''.join(filter(str.isdigit, name ))) for name in test_generator.filenames]\n\nsubmission.sort_values(by=['id'])\n","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:38:25.080378Z","iopub.execute_input":"2021-10-17T14:38:25.080644Z","iopub.status.idle":"2021-10-17T14:38:25.233964Z","shell.execute_reply.started":"2021-10-17T14:38:25.080614Z","shell.execute_reply":"2021-10-17T14:38:25.233247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test = []                                     # create empty folder name as test\n# num_id = []                                   # create empty folder name as id number\n\n# # os.listdir returns the list of files in the folder, in this case image class names\n# for i in os.listdir('./data/test'):                             \n    \n#     test_class = i.split(\".\", 1)\n    \n#     for j in test_class[0:1]:\n        \n#         test.append(i)                         # append image file into the new folder train\n        \n#         num_id.append(j)                         # append the name of folder as the label of the image file\n        \n#   # create data frame from dictionary with 2 coloums Image and Labels\n# submission_df = pd.DataFrame({'Id': num_id, 'Image': test})","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# img_id = []\n# prediction = []\n\n# from keras.preprocessing import image\n# for i in range(0, submission_df.shape[0]):  \n   \n#     path = submission_df['Image'][i]\n\n#     img = image.load_img(path, target_size=(64, 64))\n    \n#   # converting image to array\n#     img = np.asarray(img, dtype= np.float32)\n    \n#   # normalizing the image\n#     img = img /255\n    \n#   # reshaping the image in to a 4D array\n#     img = img.reshape(-1,64,64,3)\n    \n#     result_predict = cnn.predict(img)[0]\n        \n#     prediction.append(\"%.2f\"%result_predict)\n    \n#     i_id = path.split(\".\", 1)\n    \n#     img_id.append(i_id)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# submission = pd.DataFrame({'id': num_id, 'label': prediction})","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%rm -rf ./train\n%rm -rf ./data","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('my_submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:38:35.846512Z","iopub.execute_input":"2021-10-17T14:38:35.84723Z","iopub.status.idle":"2021-10-17T14:38:35.877548Z","shell.execute_reply.started":"2021-10-17T14:38:35.847193Z","shell.execute_reply":"2021-10-17T14:38:35.876918Z"},"trusted":true},"execution_count":null,"outputs":[]}]}