{"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 \n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-26T01:38:37.945104Z","iopub.execute_input":"2021-11-26T01:38:37.945838Z","iopub.status.idle":"2021-11-26T01:38:37.965678Z","shell.execute_reply.started":"2021-11-26T01:38:37.945742Z","shell.execute_reply":"2021-11-26T01:38:37.964723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom keras.utils import np_utils\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Activation\nfrom keras.layers import Conv2D, MaxPooling2D\n","metadata":{"execution":{"iopub.status.busy":"2021-11-26T01:38:38.413655Z","iopub.execute_input":"2021-11-26T01:38:38.414339Z","iopub.status.idle":"2021-11-26T01:38:43.289574Z","shell.execute_reply.started":"2021-11-26T01:38:38.414292Z","shell.execute_reply":"2021-11-26T01:38:43.288803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import BatchNormalization, Flatten","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:41:17.328225Z","iopub.execute_input":"2021-11-26T02:41:17.328742Z","iopub.status.idle":"2021-11-26T02:41:17.332005Z","shell.execute_reply.started":"2021-11-26T02:41:17.328706Z","shell.execute_reply":"2021-11-26T02:41:17.331355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import load_img, img_to_array, ImageDataGenerator\nfrom keras import applications","metadata":{"execution":{"iopub.status.busy":"2021-11-26T01:38:43.291098Z","iopub.execute_input":"2021-11-26T01:38:43.291375Z","iopub.status.idle":"2021-11-26T01:38:43.299249Z","shell.execute_reply.started":"2021-11-26T01:38:43.291339Z","shell.execute_reply":"2021-11-26T01:38:43.298547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport random\nimport zipfile","metadata":{"execution":{"iopub.status.busy":"2021-11-26T01:38:43.300226Z","iopub.execute_input":"2021-11-26T01:38:43.300416Z","iopub.status.idle":"2021-11-26T01:38:43.312774Z","shell.execute_reply.started":"2021-11-26T01:38:43.300393Z","shell.execute_reply":"2021-11-26T01:38:43.312041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir(\"../input/dogs-vs-cats\"))","metadata":{"execution":{"iopub.status.busy":"2021-11-26T01:38:43.314833Z","iopub.execute_input":"2021-11-26T01:38:43.315108Z","iopub.status.idle":"2021-11-26T01:38:43.323881Z","shell.execute_reply.started":"2021-11-26T01:38:43.315062Z","shell.execute_reply":"2021-11-26T01:38:43.323184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_width = 128\nimage_height = 128\nimage_size = (image_width, image_height)\nimage_chanels = 3","metadata":{"execution":{"iopub.status.busy":"2021-11-26T01:38:43.325297Z","iopub.execute_input":"2021-11-26T01:38:43.326017Z","iopub.status.idle":"2021-11-26T01:38:43.331143Z","shell.execute_reply.started":"2021-11-26T01:38:43.32597Z","shell.execute_reply":"2021-11-26T01:38:43.330318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_files = ['test1', 'train']\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"../input/dogs-vs-cats/{}.zip\".format(zip_file), \"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped \".format(zip_file))","metadata":{"execution":{"iopub.status.busy":"2021-11-26T01:38:43.33256Z","iopub.execute_input":"2021-11-26T01:38:43.332973Z","iopub.status.idle":"2021-11-26T01:39:00.705405Z","shell.execute_reply.started":"2021-11-26T01:38:43.332935Z","shell.execute_reply":"2021-11-26T01:39:00.704557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_FOLDER_PATH=\"../working/train\"\nFILE_NAMES=os.listdir(IMAGE_FOLDER_PATH)\nWIDTH=150\nHEIGHT=150","metadata":{"execution":{"iopub.status.busy":"2021-11-26T01:39:00.708428Z","iopub.execute_input":"2021-11-26T01:39:00.708985Z","iopub.status.idle":"2021-11-26T01:39:00.727782Z","shell.execute_reply.started":"2021-11-26T01:39:00.708954Z","shell.execute_reply":"2021-11-26T01:39:00.727042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets=list()\nfull_paths=list()\nfor file_name in FILE_NAMES:\n    target=file_name.split(\".\")[0]\n    if target == \"dog\":\n        target = 1\n    else:\n        target = 0\n    full_path=os.path.join(IMAGE_FOLDER_PATH, file_name)\n    full_paths.append(full_path)\n    targets.append(target)\n\ndf=pd.DataFrame()\ndf['image_path']=full_paths\ndf['target']=targets","metadata":{"execution":{"iopub.status.busy":"2021-11-26T01:39:00.72922Z","iopub.execute_input":"2021-11-26T01:39:00.729489Z","iopub.status.idle":"2021-11-26T01:39:00.82594Z","shell.execute_reply.started":"2021-11-26T01:39:00.729453Z","shell.execute_reply":"2021-11-26T01:39:00.825274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-26T01:39:00.826956Z","iopub.execute_input":"2021-11-26T01:39:00.827244Z","iopub.status.idle":"2021-11-26T01:39:00.841801Z","shell.execute_reply.started":"2021-11-26T01:39:00.827209Z","shell.execute_reply":"2021-11-26T01:39:00.840925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['target'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:06:22.054888Z","iopub.execute_input":"2021-11-26T02:06:22.055584Z","iopub.status.idle":"2021-11-26T02:06:22.245912Z","shell.execute_reply.started":"2021-11-26T02:06:22.055547Z","shell.execute_reply":"2021-11-26T02:06:22.24515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = random.choice(full_paths)\nimage = load_img(sample)\nplt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2021-11-26T01:39:01.05782Z","iopub.execute_input":"2021-11-26T01:39:01.058058Z","iopub.status.idle":"2021-11-26T01:39:01.32024Z","shell.execute_reply.started":"2021-11-26T01:39:01.058025Z","shell.execute_reply":"2021-11-26T01:39:01.319524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split Data \n","metadata":{}},{"cell_type":"code","source":"df[\"target\"] = df[\"target\"].replace({0: 'cat', 1: 'dog'}) ","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:06:26.251451Z","iopub.execute_input":"2021-11-26T02:06:26.252303Z","iopub.status.idle":"2021-11-26T02:06:26.260349Z","shell.execute_reply.started":"2021-11-26T02:06:26.252238Z","shell.execute_reply":"2021-11-26T02:06:26.259537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, val_df = train_test_split(df, test_size = 0.2, random_state=42)\ntrain_df.shape, val_df.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:06:27.648465Z","iopub.execute_input":"2021-11-26T02:06:27.649013Z","iopub.status.idle":"2021-11-26T02:06:27.661073Z","shell.execute_reply.started":"2021-11-26T02:06:27.648974Z","shell.execute_reply":"2021-11-26T02:06:27.660339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"target\"].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:06:28.724042Z","iopub.execute_input":"2021-11-26T02:06:28.724705Z","iopub.status.idle":"2021-11-26T02:06:28.9044Z","shell.execute_reply.started":"2021-11-26T02:06:28.724669Z","shell.execute_reply":"2021-11-26T02:06:28.903696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_df[\"target\"].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:06:33.166635Z","iopub.execute_input":"2021-11-26T02:06:33.166896Z","iopub.status.idle":"2021-11-26T02:06:33.350955Z","shell.execute_reply.started":"2021-11-26T02:06:33.166866Z","shell.execute_reply":"2021-11-26T02:06:33.350212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train = train_df.shape[0]\ntotal_val = val_df.shape[0]\nbatch_size = 15","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:06:34.000519Z","iopub.execute_input":"2021-11-26T02:06:34.001358Z","iopub.status.idle":"2021-11-26T02:06:34.005729Z","shell.execute_reply.started":"2021-11-26T02:06:34.001308Z","shell.execute_reply":"2021-11-26T02:06:34.00487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir= \"./train\"\ntrain_img = ImageDataGenerator(\n    rotation_range = 15,\n    rescale=1./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\n)\ntrain_generator = train_img.flow_from_dataframe(\n    train_df,\n    batch_size = 15, \n    x_col = \"image_path\",\n    y_col = \"target\",\n    shuffle = True,\n    class_mode = \"binary\",\n    target_size = image_size\n\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:10:43.171141Z","iopub.execute_input":"2021-11-26T02:10:43.171415Z","iopub.status.idle":"2021-11-26T02:10:43.361381Z","shell.execute_reply.started":"2021-11-26T02:10:43.171384Z","shell.execute_reply":"2021-11-26T02:10:43.360587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_img = ImageDataGenerator(\n    rescale = 1./255\n)\nval_generator = val_img.flow_from_dataframe(\n    val_df,\n    batch_size = 15, \n    x_col = \"image_path\",\n    y_col = \"target\",\n    class_mode = \"binary\",\n    target_size = image_size\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:14:23.465147Z","iopub.execute_input":"2021-11-26T02:14:23.465589Z","iopub.status.idle":"2021-11-26T02:14:23.526375Z","shell.execute_reply.started":"2021-11-26T02:14:23.465556Z","shell.execute_reply":"2021-11-26T02:14:23.525554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example = train_df.sample(n= 1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2> Build Model </h2>","metadata":{}},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, (3,3), activation='relu', input_shape=(image_width, image_height, image_chanels)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128, (3,3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1, activation='sigmoid'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:47:16.84632Z","iopub.execute_input":"2021-11-26T02:47:16.84687Z","iopub.status.idle":"2021-11-26T02:47:16.966219Z","shell.execute_reply.started":"2021-11-26T02:47:16.846826Z","shell.execute_reply":"2021-11-26T02:47:16.965547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"binary_crossentropy\",\n              optimizer='rmsprop',\n              metrics = [\"accuracy\"]\n             )\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:50:11.939906Z","iopub.execute_input":"2021-11-26T02:50:11.940189Z","iopub.status.idle":"2021-11-26T02:50:11.961031Z","shell.execute_reply.started":"2021-11-26T02:50:11.940158Z","shell.execute_reply":"2021-11-26T02:50:11.960334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2> Callbacks <h2> ","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:51:32.744066Z","iopub.execute_input":"2021-11-26T02:51:32.744394Z","iopub.status.idle":"2021-11-26T02:51:32.75181Z","shell.execute_reply.started":"2021-11-26T02:51:32.744358Z","shell.execute_reply":"2021-11-26T02:51:32.750951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h4> Early Stop <h4>\n<p> To prevent over fitting we will stop the learning after 10 epochs and val_loss value not decreased <p>","metadata":{}},{"cell_type":"code","source":"early_stop = EarlyStopping(patience=10)","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:52:49.651974Z","iopub.execute_input":"2021-11-26T02:52:49.652319Z","iopub.status.idle":"2021-11-26T02:52:49.660428Z","shell.execute_reply.started":"2021-11-26T02:52:49.652279Z","shell.execute_reply":"2021-11-26T02:52:49.659657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h4> Learning rate <h4>\n<p> reduce the learning rate when then acc not increase for 2 steps <p>","metadata":{}},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(\n    monitor=\"val_acc\",\n    patience=2,\n    verbose=1,\n    factor=0.5,\n    min_lr = 0.00001\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:55:20.969358Z","iopub.execute_input":"2021-11-26T02:55:20.969638Z","iopub.status.idle":"2021-11-26T02:55:20.973742Z","shell.execute_reply.started":"2021-11-26T02:55:20.969605Z","shell.execute_reply":"2021-11-26T02:55:20.973028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"call_backs = [early_stop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:55:40.47705Z","iopub.execute_input":"2021-11-26T02:55:40.477585Z","iopub.status.idle":"2021-11-26T02:55:40.482987Z","shell.execute_reply.started":"2021-11-26T02:55:40.477549Z","shell.execute_reply":"2021-11-26T02:55:40.480328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_fit = model.fit_generator(train_generator,\n                               epochs=50,\n                                validation_data= val_generator,\n                                validation_steps=total_val//batch_size,\n                                steps_per_epoch=total_train//batch_size,\n                                callbacks = call_backs\n                               )","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:59:09.91748Z","iopub.execute_input":"2021-11-26T02:59:09.917996Z","iopub.status.idle":"2021-11-26T03:54:24.699243Z","shell.execute_reply.started":"2021-11-26T02:59:09.917957Z","shell.execute_reply":"2021-11-26T03:54:24.698548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(2,1,digsize=(12,12))\nax1.plot(model_fit.history[\"loss\"], color=\"b\", label=\"Training Loss\")\nax1.plot(model_fit.history[\"val_loss\"], color=\"r\", label=\"Validation Loss\")\nax1.set_xticks(np.arange(1, epochs, 1))\nax1.set_yticks(np.arange(0, 1, 0.1))\n\nax2.plot(model_fit.history[\"acc\"], color=\"b\", label=\"Training Acc\")\nax2.plot(model_fit.history[\"val_acc\"], color=\"r\", label=\"Validation Acc\")\nax2.set_xticks(np.arange(1, epochs, 1))\nax2.set_yticks(np.arange(0, 1, 0.1))\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2021-11-26T04:06:12.163549Z","iopub.execute_input":"2021-11-26T04:06:12.164206Z","iopub.status.idle":"2021-11-26T04:06:32.537539Z","shell.execute_reply.started":"2021-11-26T04:06:12.164156Z","shell.execute_reply":"2021-11-26T04:06:32.536117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h2> Predcit <h2>","metadata":{}},{"cell_type":"code","source":"IMAGE_FOLDER_PATH_Test=\"../working/test1\"\nfile_name_test=os.listdir(IMAGE_FOLDER_PATH_Test)\nfull_paths_test=list()\nfor file_name in file_name_test:\n    full_path=os.path.join(IMAGE_FOLDER_PATH_Test, file_name)\n    full_paths_test.append(full_path)\n\ndf_test=pd.DataFrame()\ndf_test['image_path']=full_paths_test\nn_samples_test = df_test.shape[0]","metadata":{"execution":{"iopub.status.busy":"2021-11-26T04:09:23.745079Z","iopub.execute_input":"2021-11-26T04:09:23.745845Z","iopub.status.idle":"2021-11-26T04:09:23.790766Z","shell.execute_reply.started":"2021-11-26T04:09:23.745804Z","shell.execute_reply":"2021-11-26T04:09:23.78999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img = ImageDataGenerator(rescale=1./255)\ntest_generator = test_img.flow_from_dataframe(\n    df_test,\n    batch_size = 15, \n    x_col = \"image_path\",\n    y_col = None,\n    class_mode = None,\n    target_size = image_size\n)","metadata":{"execution":{"iopub.status.busy":"2021-11-26T04:09:25.223826Z","iopub.execute_input":"2021-11-26T04:09:25.224508Z","iopub.status.idle":"2021-11-26T04:09:25.327972Z","shell.execute_reply.started":"2021-11-26T04:09:25.224468Z","shell.execute_reply":"2021-11-26T04:09:25.327191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict_generator(test_generator,  steps=np.ceil(n_samples_test/batch_size))","metadata":{"execution":{"iopub.status.busy":"2021-11-26T04:10:40.337985Z","iopub.execute_input":"2021-11-26T04:10:40.338432Z","iopub.status.idle":"2021-11-26T04:11:10.961961Z","shell.execute_reply.started":"2021-11-26T04:10:40.338394Z","shell.execute_reply":"2021-11-26T04:11:10.960445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['target'] = np.argmax(predict, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2021-11-26T04:11:10.964493Z","iopub.execute_input":"2021-11-26T04:11:10.964905Z","iopub.status.idle":"2021-11-26T04:11:10.974153Z","shell.execute_reply.started":"2021-11-26T04:11:10.964864Z","shell.execute_reply":"2021-11-26T04:11:10.973383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['target'] = df_test['target'].replace({ 'dog': 1, 'cat': 0 })","metadata":{"execution":{"iopub.status.busy":"2021-11-26T04:11:45.644179Z","iopub.execute_input":"2021-11-26T04:11:45.644698Z","iopub.status.idle":"2021-11-26T04:11:45.649566Z","shell.execute_reply.started":"2021-11-26T04:11:45.644663Z","shell.execute_reply":"2021-11-26T04:11:45.648743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test[\"target\"].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-11-26T04:12:05.688032Z","iopub.execute_input":"2021-11-26T04:12:05.688307Z","iopub.status.idle":"2021-11-26T04:12:05.864791Z","shell.execute_reply.started":"2021-11-26T04:12:05.688276Z","shell.execute_reply":"2021-11-26T04:12:05.864082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}