{"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":"# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nimport pandas as pd\nimport numpy as np\nimport os\nfrom matplotlib import pyplot\nfrom tensorflow.keras.utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D\nfrom keras.layers import MaxPooling2D\nfrom keras.layers import Dense\nfrom keras.layers import Flatten\nfrom tensorflow.keras.optimizers import SGD\nfrom keras.preprocessing.image import ImageDataGenerator\n\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom keras.layers import Conv2D,MaxPooling2D,Dropout,Flatten,Dense,Activation,BatchNormalization\n","metadata":{"execution":{"iopub.status.busy":"2022-08-04T16:23:46.280136Z","iopub.execute_input":"2022-08-04T16:23:46.280464Z","iopub.status.idle":"2022-08-04T16:23:46.289014Z","shell.execute_reply.started":"2022-08-04T16:23:46.280432Z","shell.execute_reply":"2022-08-04T16:23:46.287922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1.0/255.0,\n\t\twidth_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True,validation_split = 0.2)\nvalidation_datagen = ImageDataGenerator(rescale=1.0/255.0,validation_split = 0.2)\n\n# prepare iterators\ntrain_it = train_datagen.flow_from_directory('../input/dog-vs-cat-classification/train/train',\n\t\tclass_mode='binary', batch_size=64, target_size=(200, 200),subset = 'training')\nvalidation_it = validation_datagen.flow_from_directory('../input/dog-vs-cat-classification/train/train',\n\t\tclass_mode='binary', batch_size=64, target_size=(200, 200), subset = 'validation')","metadata":{"execution":{"iopub.status.busy":"2022-08-04T13:31:51.310697Z","iopub.execute_input":"2022-08-04T13:31:51.310994Z","iopub.status.idle":"2022-08-04T13:31:58.421951Z","shell.execute_reply.started":"2022-08-04T13:31:51.310963Z","shell.execute_reply":"2022-08-04T13:31:58.420874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def define_model():\n\tmodel = Sequential()\n\tmodel.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(200, 200, 3)))\n\tmodel.add(MaxPooling2D((2, 2)))\n\tmodel.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))\n\tmodel.add(MaxPooling2D((2, 2)))\n\tmodel.add(Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))\n\tmodel.add(MaxPooling2D((2, 2)))\n\tmodel.add(Flatten())\n\tmodel.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\n\tmodel.add(Dense(1, activation='sigmoid'))\n\t# compile model\n\topt = SGD(lr=0.001, momentum=0.9)\n\tmodel.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'])\n\treturn model","metadata":{"execution":{"iopub.status.busy":"2022-08-04T13:41:24.494938Z","iopub.execute_input":"2022-08-04T13:41:24.495245Z","iopub.status.idle":"2022-08-04T13:41:24.504569Z","shell.execute_reply.started":"2022-08-04T13:41:24.495215Z","shell.execute_reply":"2022-08-04T13:41:24.503150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# earlystop = EarlyStopping(patience = 3)\n# learning_rate_reduction = ReduceLROnPlateau(monitor = 'val_acc',patience = 2,verbose = 1,factor = 0.5,min_lr = 0.00001)\n# call = [earlystop, learning_rate_reduction]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def summarize_diagnostics(history):\n\t# plot loss\n\tpyplot.subplot(211)\n\tpyplot.title('Cross Entropy Loss')\n\tpyplot.plot(history.history['loss'], color='blue', label='train')\n\tpyplot.plot(history.history['val_loss'], color='orange', label='validation')\n\t# plot accuracy\n\tpyplot.subplot(212)\n\tpyplot.title('Classification Accuracy')\n\tpyplot.plot(history.history['accuracy'], color='blue', label='train')\n\tpyplot.plot(history.history['val_accuracy'], color='orange', label='validation')\n\t# save plot to file\n\t#filename = sys.argv[0].split('/')[-1]\n\t#pyplot.savefig(filename + '_plot.png')\n\t#pyplot.close()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T13:39:55.443017Z","iopub.execute_input":"2022-08-04T13:39:55.443362Z","iopub.status.idle":"2022-08-04T13:39:55.450779Z","shell.execute_reply.started":"2022-08-04T13:39:55.443300Z","shell.execute_reply":"2022-08-04T13:39:55.449462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fit model\nmodel = define_model()\nhistory = model.fit_generator(train_it, steps_per_epoch=len(train_it),\n\t\tvalidation_data=validation_it, validation_steps=len(validation_it), epochs=20, verbose=1)\n                    #, callbacks = call)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T13:41:38.988037Z","iopub.execute_input":"2022-08-04T13:41:38.988373Z","iopub.status.idle":"2022-08-04T15:07:07.843487Z","shell.execute_reply.started":"2022-08-04T13:41:38.988341Z","shell.execute_reply":"2022-08-04T15:07:07.842436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# evaluate model\n_, acc = model.evaluate_generator(validation_it, steps=len(validation_it), verbose= 1)\nprint('> %.3f' % (acc * 100.0))","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:38:12.330622Z","iopub.execute_input":"2022-08-04T15:38:12.330971Z","iopub.status.idle":"2022-08-04T15:38:32.306964Z","shell.execute_reply.started":"2022-08-04T15:38:12.330915Z","shell.execute_reply":"2022-08-04T15:38:32.305348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# learning curves\nsummarize_diagnostics(history)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T15:53:41.395350Z","iopub.execute_input":"2022-08-04T15:53:41.395763Z","iopub.status.idle":"2022-08-04T15:53:41.772538Z","shell.execute_reply.started":"2022-08-04T15:53:41.395728Z","shell.execute_reply":"2022-08-04T15:53:41.771419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Making predictions...","metadata":{}},{"cell_type":"code","source":"test_filenames = os.listdir(\"../input/dog-vs-cat-classification/test/test\")\ntest_df = pd.DataFrame({\n    'id': test_filenames\n})","metadata":{"execution":{"iopub.status.busy":"2022-08-04T16:09:28.949714Z","iopub.execute_input":"2022-08-04T16:09:28.950128Z","iopub.status.idle":"2022-08-04T16:09:28.964318Z","shell.execute_reply.started":"2022-08-04T16:09:28.950051Z","shell.execute_reply":"2022-08-04T16:09:28.962840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-04T16:09:32.091181Z","iopub.execute_input":"2022-08-04T16:09:32.091794Z","iopub.status.idle":"2022-08-04T16:09:32.102380Z","shell.execute_reply.started":"2022-08-04T16:09:32.091758Z","shell.execute_reply":"2022-08-04T16:09:32.101031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = test_datagen.flow_from_dataframe(\n    test_df,\n    \"../input/dog-vs-cat-classification/test/test\",\n    x_col='id',\n    y_col=None,\n    class_mode=None,\n    target_size=(200, 200),\n    batch_size=64)\n    #shuffle=False\n","metadata":{"execution":{"iopub.status.busy":"2022-08-04T16:19:08.059022Z","iopub.execute_input":"2022-08-04T16:19:08.059691Z","iopub.status.idle":"2022-08-04T16:19:12.061505Z","shell.execute_reply.started":"2022-08-04T16:19:08.059656Z","shell.execute_reply":"2022-08-04T16:19:12.060341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(test_generator)          # Here you will observe that we are directly using Data Iterator in predict function.\n","metadata":{"execution":{"iopub.status.busy":"2022-08-04T16:22:18.364001Z","iopub.execute_input":"2022-08-04T16:22:18.364343Z","iopub.status.idle":"2022-08-04T16:23:22.003025Z","shell.execute_reply.started":"2022-08-04T16:22:18.364310Z","shell.execute_reply":"2022-08-04T16:23:22.001703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['labels'] = np.argmax(predict, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T16:23:55.408087Z","iopub.execute_input":"2022-08-04T16:23:55.408596Z","iopub.status.idle":"2022-08-04T16:23:55.422595Z","shell.execute_reply.started":"2022-08-04T16:23:55.408488Z","shell.execute_reply":"2022-08-04T16:23:55.421064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('firstNN.h5')","metadata":{"execution":{"iopub.status.busy":"2022-08-04T16:26:12.969638Z","iopub.execute_input":"2022-08-04T16:26:12.969937Z","iopub.status.idle":"2022-08-04T16:26:13.289861Z","shell.execute_reply.started":"2022-08-04T16:26:12.969906Z","shell.execute_reply":"2022-08-04T16:26:13.288709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.set_index('id')\ntest_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-04T16:31:55.584657Z","iopub.execute_input":"2022-08-04T16:31:55.584971Z","iopub.status.idle":"2022-08-04T16:31:55.606580Z","shell.execute_reply.started":"2022-08-04T16:31:55.584940Z","shell.execute_reply":"2022-08-04T16:31:55.605479Z"},"trusted":true},"execution_count":null,"outputs":[]}]}