{"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 warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport zipfile\nimport os\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img\nfrom keras.callbacks import EarlyStopping\nfrom keras.callbacks import ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2021-11-28T07:43:35.182673Z","iopub.execute_input":"2021-11-28T07:43:35.183654Z","iopub.status.idle":"2021-11-28T07:43:42.328693Z","shell.execute_reply.started":"2021-11-28T07:43:35.183599Z","shell.execute_reply":"2021-11-28T07:43:42.327940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",'r') as z:\n    z.extractall(\"/kaggle/files/\")\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",'r') as z:\n    z.extractall(\"/kaggle/files/\")","metadata":{"execution":{"iopub.status.busy":"2021-11-28T07:43:42.330369Z","iopub.execute_input":"2021-11-28T07:43:42.330631Z","iopub.status.idle":"2021-11-28T07:44:05.439121Z","shell.execute_reply.started":"2021-11-28T07:43:42.330599Z","shell.execute_reply":"2021-11-28T07:44:05.438127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train data\nfiles = os.listdir(\"/kaggle/files/train/\")\nimages01 = []\nfor image in files :\n    category = image.split('.')[0]\n    #print(category)\n    if category == 'dog' :\n        images01.append(1)\n        #print(images01)\n    else :    \n        images01.append(0)\n        #print(images01)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T07:44:05.440201Z","iopub.execute_input":"2021-11-28T07:44:05.440438Z","iopub.status.idle":"2021-11-28T07:44:05.474069Z","shell.execute_reply.started":"2021-11-28T07:44:05.440411Z","shell.execute_reply":"2021-11-28T07:44:05.472995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'file' : files, 'category' : images01})\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T07:44:05.475176Z","iopub.execute_input":"2021-11-28T07:44:05.475434Z","iopub.status.idle":"2021-11-28T07:44:06.219107Z","shell.execute_reply.started":"2021-11-28T07:44:05.475405Z","shell.execute_reply":"2021-11-28T07:44:06.218152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T07:44:06.221233Z","iopub.execute_input":"2021-11-28T07:44:06.221488Z","iopub.status.idle":"2021-11-28T07:44:06.235358Z","shell.execute_reply.started":"2021-11-28T07:44:06.221462Z","shell.execute_reply":"2021-11-28T07:44:06.234348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential()\n\n#conv_1\nmodel.add(keras.layers.Conv2D(32, 3,input_shape = [128, 128, 3], activation = 'relu', padding = 'same'))\nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.MaxPool2D(2))\nmodel.add(keras.layers.Dropout(0.2))\n\n#conv_2\nmodel.add(keras.layers.Conv2D(64, 3, activation = 'relu', padding = 'same'))\nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.MaxPool2D(2))\nmodel.add(keras.layers.Dropout(0.2))\n\n#conv_3\nmodel.add(keras.layers.Conv2D(128, 3, activation = 'relu', padding = 'same'))\nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.MaxPool2D(2))\nmodel.add(keras.layers.Dropout(0.2))\n\n#conv_4\nmodel.add(keras.layers.Conv2D(256, 3, activation = 'relu', padding = 'same'))\nmodel.add(keras.layers.BatchNormalization())\nmodel.add(keras.layers.MaxPool2D(2))\nmodel.add(keras.layers.Dropout(0.2))\n\n\nmodel.add(keras.layers.Flatten())\n\nmodel.add(keras.layers.Dense(1024, activation = 'relu'))\nmodel.add(keras.layers.Dropout(0.25))\n\nmodel.add(keras.layers.Dense(2, activation = 'sigmoid'))\n\n\n\nmodel.compile(loss = 'categorical_crossentropy', optimizer = 'rmsprop', metrics = ['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T07:44:06.236931Z","iopub.execute_input":"2021-11-28T07:44:06.237172Z","iopub.status.idle":"2021-11-28T07:44:06.650018Z","shell.execute_reply.started":"2021-11-28T07:44:06.237144Z","shell.execute_reply":"2021-11-28T07:44:06.649026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# earlystop = EarlyStopping(patience = 3)\n# learning_rate_reduction = ReduceLROnPlateau(monitor = 'val_acc', \n#                                             patience = 2, \n#                                             #verbose = 1, \n#                                             factor = 0.5) \n#                                             #min_lr = 0.0001)\n# callbacks = [earlystop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2021-11-28T07:44:06.651238Z","iopub.execute_input":"2021-11-28T07:44:06.651458Z","iopub.status.idle":"2021-11-28T07:44:06.656135Z","shell.execute_reply.started":"2021-11-28T07:44:06.651431Z","shell.execute_reply":"2021-11-28T07:44:06.655119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['category'] = df['category'].map({0 : 'cat', 1 : 'dog'})","metadata":{"execution":{"iopub.status.busy":"2021-11-28T07:44:06.657356Z","iopub.execute_input":"2021-11-28T07:44:06.657585Z","iopub.status.idle":"2021-11-28T07:44:06.673668Z","shell.execute_reply.started":"2021-11-28T07:44:06.657548Z","shell.execute_reply":"2021-11-28T07:44:06.672717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train, val_df = train_test_split(df, test_size=0.2, random_state=42)\ntrain = train.reset_index(drop=True)\nval_df = val_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T07:44:06.674798Z","iopub.execute_input":"2021-11-28T07:44:06.675101Z","iopub.status.idle":"2021-11-28T07:44:06.690480Z","shell.execute_reply.started":"2021-11-28T07:44:06.675070Z","shell.execute_reply":"2021-11-28T07:44:06.689659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train = train.shape[0]\nprint(total_train)\ntotal_valid = val_df.shape[0]\nprint(total_valid)\nbatch_size = 10","metadata":{"execution":{"iopub.status.busy":"2021-11-28T07:44:06.691807Z","iopub.execute_input":"2021-11-28T07:44:06.692578Z","iopub.status.idle":"2021-11-28T07:44:06.698768Z","shell.execute_reply.started":"2021-11-28T07:44:06.692542Z","shell.execute_reply":"2021-11-28T07:44:06.698107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = ImageDataGenerator(rotation_range=20,\n                                rescale = 1./255,\n                                horizontal_flip = True)\ntrain_generator = train_data.flow_from_dataframe(train,\n                                                '/kaggle/files/train/',\n                                                x_col = 'file',\n                                                y_col = 'category',\n                                                target_size = [128,128],\n                                                class_mode = 'categorical',\n                                                batch_size = batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T07:44:06.700305Z","iopub.execute_input":"2021-11-28T07:44:06.700539Z","iopub.status.idle":"2021-11-28T07:44:06.956732Z","shell.execute_reply.started":"2021-11-28T07:44:06.700513Z","shell.execute_reply":"2021-11-28T07:44:06.955624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_data = ImageDataGenerator(rescale = 1./255)\n                                                         \n                                                         \nvalid_generator = valid_data.flow_from_dataframe(val_df,\n                                                '/kaggle/files/train/',\n                                                x_col = 'file',\n                                                y_col = 'category',\n                                                target_size = [128,128],\n                                                class_mode = 'categorical',\n                                                batch_size = batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T07:44:06.958129Z","iopub.execute_input":"2021-11-28T07:44:06.958927Z","iopub.status.idle":"2021-11-28T07:44:07.036247Z","shell.execute_reply.started":"2021-11-28T07:44:06.958890Z","shell.execute_reply":"2021-11-28T07:44:07.035143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epoch = 5\nhistory = model.fit_generator(\n    train_generator, \n    epochs = epoch,\n    validation_data = valid_generator,\n    #validation_steps = total_valid // batch_size,\n    steps_per_epoch = total_train // batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T07:44:07.037768Z","iopub.execute_input":"2021-11-28T07:44:07.038098Z","iopub.status.idle":"2021-11-28T08:40:36.171780Z","shell.execute_reply.started":"2021-11-28T07:44:07.038056Z","shell.execute_reply":"2021-11-28T08:40:36.170558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:40:36.178025Z","iopub.execute_input":"2021-11-28T08:40:36.178394Z","iopub.status.idle":"2021-11-28T08:40:36.187444Z","shell.execute_reply.started":"2021-11-28T08:40:36.178343Z","shell.execute_reply":"2021-11-28T08:40:36.186729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], color = 'red', label = \"train loss\")\nplt.plot(history.history['val_loss'], color = 'pink', label = \"valid loss\")\nplt.xticks(np.arange(1, epoch, 1))\nplt.yticks(np.arange(0, 1, 0.1))\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:40:36.188399Z","iopub.execute_input":"2021-11-28T08:40:36.188846Z","iopub.status.idle":"2021-11-28T08:40:36.408540Z","shell.execute_reply.started":"2021-11-28T08:40:36.188795Z","shell.execute_reply":"2021-11-28T08:40:36.407593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], color = 'red', label = \"train accuracy\")\nplt.plot(history.history['val_accuracy'], color = 'pink',label = \"valid accuracy\")\nplt.xticks(np.arange(1, epoch, 1))\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:40:36.410137Z","iopub.execute_input":"2021-11-28T08:40:36.410387Z","iopub.status.idle":"2021-11-28T08:40:36.622508Z","shell.execute_reply.started":"2021-11-28T08:40:36.410357Z","shell.execute_reply":"2021-11-28T08:40:36.621861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#testdata\nfiles = os.listdir(\"/kaggle/files/test1/\")\ntest_df = pd.DataFrame({'file' : files})    \nsamples = test_df.shape[0]\nsamples","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:40:36.623476Z","iopub.execute_input":"2021-11-28T08:40:36.623837Z","iopub.status.idle":"2021-11-28T08:40:36.647606Z","shell.execute_reply.started":"2021-11-28T08:40:36.623794Z","shell.execute_reply":"2021-11-28T08:40:36.646974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = ImageDataGenerator(rescale=1./255)\ntest_generator = test_data.flow_from_dataframe(\n                test_df, \n                \"/kaggle/files/test1\", \n                x_col = 'file',\n                y_col = 'lltegory',\n                class_mode = None,\n                target_size = [128,128],\n                batch_size = batch_size,\n                shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:40:36.648663Z","iopub.execute_input":"2021-11-28T08:40:36.649402Z","iopub.status.idle":"2021-11-28T08:40:36.877553Z","shell.execute_reply.started":"2021-11-28T08:40:36.649369Z","shell.execute_reply":"2021-11-28T08:40:36.876594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict_generator(test_generator, steps=np.ceil(samples / batch_size))","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:40:36.878969Z","iopub.execute_input":"2021-11-28T08:40:36.879266Z","iopub.status.idle":"2021-11-28T08:42:21.308297Z","shell.execute_reply.started":"2021-11-28T08:40:36.879224Z","shell.execute_reply":"2021-11-28T08:42:21.307394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'] = np.argmax(predict, axis = -1)\ntest_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })\ntest_df","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:42:21.309704Z","iopub.execute_input":"2021-11-28T08:42:21.310775Z","iopub.status.idle":"2021-11-28T08:42:21.331100Z","shell.execute_reply.started":"2021-11-28T08:42:21.310732Z","shell.execute_reply":"2021-11-28T08:42:21.330399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = test_df.copy()\noutput['id'] = output['file'].str.split('.').str[0]\noutput['label'] = output['category']\noutput.drop(['file', 'category'], axis = 1, inplace = True)\noutput.to_csv('submission.csv', index=False)\n\n# output = pd.DataFrame({'Id': test['Id'], 'SalePrice': prediction})\n# output.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:42:21.332533Z","iopub.execute_input":"2021-11-28T08:42:21.333031Z","iopub.status.idle":"2021-11-28T08:42:21.846231Z","shell.execute_reply.started":"2021-11-28T08:42:21.332993Z","shell.execute_reply":"2021-11-28T08:42:21.845176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output","metadata":{"execution":{"iopub.status.busy":"2021-11-28T08:42:39.143699Z","iopub.execute_input":"2021-11-28T08:42:39.144274Z","iopub.status.idle":"2021-11-28T08:42:39.157229Z","shell.execute_reply.started":"2021-11-28T08:42:39.144237Z","shell.execute_reply":"2021-11-28T08:42:39.156372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}