{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\nimport os\nos.listdir('/kaggle/input/dogs-vs-cats')\n# 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\n\n\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\n\n# library for reading and showing images\nimport matplotlib.image as mpimg\n\n# importing all the required sub-modules from keras\nfrom keras.optimizers import Adam\nfrom keras.callbacks import EarlyStopping\nfrom keras.models import Sequential, Model\nfrom keras.applications.vgg16 import VGG16\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau,ModelCheckpoint\n\nfrom keras.preprocessing.image import img_to_array, load_img\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense,Input,Dropout,GlobalAveragePooling2D","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-26T05:52:19.450195Z","iopub.execute_input":"2021-06-26T05:52:19.450580Z","iopub.status.idle":"2021-06-26T05:52:24.768140Z","shell.execute_reply.started":"2021-06-26T05:52:19.450479Z","shell.execute_reply":"2021-06-26T05:52:24.767251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile('../input/dogs-vs-cats/test1.zip', 'r') as zip_ref:\n    zip_ref.extractall('./')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:24.769622Z","iopub.execute_input":"2021-06-26T05:52:24.769979Z","iopub.status.idle":"2021-06-26T05:52:31.604483Z","shell.execute_reply.started":"2021-06-26T05:52:24.769915Z","shell.execute_reply":"2021-06-26T05:52:31.603409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile('../input/dogs-vs-cats/train.zip', 'r') as zip_ref:\n    zip_ref.extractall('./')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:31.609264Z","iopub.execute_input":"2021-06-26T05:52:31.609612Z","iopub.status.idle":"2021-06-26T05:52:41.778404Z","shell.execute_reply.started":"2021-06-26T05:52:31.609577Z","shell.execute_reply":"2021-06-26T05:52:41.777607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Alphabetical ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_datset = []\nx_datset = []\nmin_dim = 0\nfor file in os.listdir('./train'):\n    x_datset.append(file)   \n    if file.startswith('cat'):\n        y_datset.append('0')\n    else:\n        y_datset.append('1')\n\ntrain_df = pd.DataFrame({'filename': x_datset, 'class': y_datset})\nprint(train_df.head())","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:41.779965Z","iopub.execute_input":"2021-06-26T05:52:41.780301Z","iopub.status.idle":"2021-06-26T05:52:41.824775Z","shell.execute_reply.started":"2021-06-26T05:52:41.780260Z","shell.execute_reply":"2021-06-26T05:52:41.823965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_val= train_test_split(train_df, test_size=0.33, random_state=42)\nx_train = x_train.reset_index(drop=True)\nx_val = x_val.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:41.827363Z","iopub.execute_input":"2021-06-26T05:52:41.827617Z","iopub.status.idle":"2021-06-26T05:52:41.835716Z","shell.execute_reply.started":"2021-06-26T05:52:41.827590Z","shell.execute_reply":"2021-06-26T05:52:41.834834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valImg = plt.imread('./train/'+x_val.iloc[0,0])\n#print(x_testImg[0])\nplt.imshow(valImg)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:41.836973Z","iopub.execute_input":"2021-06-26T05:52:41.837488Z","iopub.status.idle":"2021-06-26T05:52:42.017041Z","shell.execute_reply.started":"2021-06-26T05:52:41.837452Z","shell.execute_reply":"2021-06-26T05:52:42.016197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainImg = plt.imread('./train/'+x_train.iloc[0,0])\nplt.imshow(trainImg)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:42.018018Z","iopub.execute_input":"2021-06-26T05:52:42.018308Z","iopub.status.idle":"2021-06-26T05:52:42.197675Z","shell.execute_reply.started":"2021-06-26T05:52:42.018279Z","shell.execute_reply":"2021-06-26T05:52:42.196804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_val.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:42.200330Z","iopub.execute_input":"2021-06-26T05:52:42.201048Z","iopub.status.idle":"2021-06-26T05:52:42.222029Z","shell.execute_reply.started":"2021-06-26T05:52:42.201007Z","shell.execute_reply":"2021-06-26T05:52:42.219608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create two generators to augment/scale input data\ntrain_datagen = ImageDataGenerator(rescale=1./255,\n                                   horizontal_flip=True)\nval_datagen = ImageDataGenerator(rescale=1./255)\n\n# create batches of data\ndir_train = './train'\nx_col = 'filename'\ny_col='class'\nclass_mode = 'binary'\ntarget_size=(224,224)\ntrain_generator = train_datagen.flow_from_dataframe(x_train, directory=dir_train, x_col=x_col,\n                                                    y_col=y_col, class_mode=class_mode, \n                                                    target_size=target_size)\nval_generator = val_datagen.flow_from_dataframe(x_val, directory=dir_train, x_col=x_col, \n                                                  y_col=y_col, class_mode=class_mode, \n                                                  target_size=target_size)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:42.224600Z","iopub.execute_input":"2021-06-26T05:52:42.225051Z","iopub.status.idle":"2021-06-26T05:52:42.649685Z","shell.execute_reply.started":"2021-06-26T05:52:42.225015Z","shell.execute_reply":"2021-06-26T05:52:42.648279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Another method of train/val split -- without using sklearn train_test split","metadata":{}},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:42.650970Z","iopub.execute_input":"2021-06-26T05:52:42.651345Z","iopub.status.idle":"2021-06-26T05:52:42.660873Z","shell.execute_reply.started":"2021-06-26T05:52:42.651307Z","shell.execute_reply":"2021-06-26T05:52:42.659796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create two generators to augment/scale input data\ntrain_datagen = ImageDataGenerator(rescale=1./255,validation_split=0.33,\n                                   horizontal_flip=True)\n\n# create batches of data\ndir_train = './train'\nx_col = 'filename'\ny_col='class'\nclass_mode = 'binary'\ntarget_size=(224,224)\ntrain_generator = train_datagen.flow_from_dataframe(train_df, directory=dir_train, x_col=x_col,\n                                                    y_col=y_col, class_mode=class_mode, \n                                                    target_size=target_size,subset='training')\nval_generator = train_datagen.flow_from_dataframe(train_df, directory=dir_train, x_col=x_col, \n                                                  y_col=y_col, class_mode=class_mode, \n                                                  target_size=target_size,subset='validation')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:42.662511Z","iopub.execute_input":"2021-06-26T05:52:42.662860Z","iopub.status.idle":"2021-06-26T05:52:43.061270Z","shell.execute_reply.started":"2021-06-26T05:52:42.662825Z","shell.execute_reply":"2021-06-26T05:52:43.060373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import optimizers, applications\n","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:43.062354Z","iopub.execute_input":"2021-06-26T05:52:43.062620Z","iopub.status.idle":"2021-06-26T05:52:43.066675Z","shell.execute_reply.started":"2021-06-26T05:52:43.062595Z","shell.execute_reply":"2021-06-26T05:52:43.065581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model(input_shape):\n    input_tensor = Input(shape=input_shape)\n\n    base_model = applications.ResNet152(weights='imagenet', \n                                       include_top=False,input_tensor=input_tensor)\n    \n    \n#     base_model = applications.VGG16(weights='imagenet', \n#                                        include_top=False,input_tensor=input_tensor)\n\n    \n    \n    #base_model.load_weights('../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')\n    \n    #x = BatchNormalization()(base_model.output)\n    x = Dense(2048,activation = 'relu')(base_model.output)\n    #x = Conv2D(128,(7,7), activation='relu', padding='same')(x)\n\n    #x = GlobalAveragePooling2D()(x)#(base_model.output)\n    x = Dropout(0.5)(x)\n    x = Dense(600, activation='relu')(x)\n    x = Dropout(0.5)(x)\n    x = GlobalAveragePooling2D()(x)\n    final_output = Dense(1, activation='sigmoid', name='final_output')(x)\n    model = Model(input_tensor, final_output)\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:43.068186Z","iopub.execute_input":"2021-06-26T05:52:43.068734Z","iopub.status.idle":"2021-06-26T05:52:43.077298Z","shell.execute_reply.started":"2021-06-26T05:52:43.068692Z","shell.execute_reply":"2021-06-26T05:52:43.076292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 10\nEPOCHS = 100\nWARMUP_EPOCHS = 1\nLEARNING_RATE = 0.01\nWARMUP_LEARNING_RATE = 1e-3\nHEIGHT = 128\nWIDTH = 128\nCHANNEL = 3\nES_PATIENCE = 10\nRLROP_PATIENCE = 3\nDECAY_DROP = 0.4","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:43.078878Z","iopub.execute_input":"2021-06-26T05:52:43.079257Z","iopub.status.idle":"2021-06-26T05:52:43.087893Z","shell.execute_reply.started":"2021-06-26T05:52:43.079223Z","shell.execute_reply":"2021-06-26T05:52:43.087041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model(input_shape=(HEIGHT, WIDTH, CHANNEL))\n","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:43.090872Z","iopub.execute_input":"2021-06-26T05:52:43.091172Z","iopub.status.idle":"2021-06-26T05:52:57.080199Z","shell.execute_reply.started":"2021-06-26T05:52:43.091136Z","shell.execute_reply":"2021-06-26T05:52:57.079122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in model.layers:\n    layer.trainable = False\n\nfor i in range(-6, 0):\n    model.layers[i].trainable = True\n","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:57.081426Z","iopub.execute_input":"2021-06-26T05:52:57.081784Z","iopub.status.idle":"2021-06-26T05:52:57.108146Z","shell.execute_reply.started":"2021-06-26T05:52:57.081748Z","shell.execute_reply":"2021-06-26T05:52:57.107428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metric_list = [\"accuracy\"]\n\nes = EarlyStopping(monitor='val_loss', mode='min', \n                   patience=ES_PATIENCE, restore_best_weights=True, verbose=1)\nrlrop = ReduceLROnPlateau(monitor='val_loss', mode='min', \n                          patience=RLROP_PATIENCE, factor=DECAY_DROP, min_lr=1e-6, verbose=1)\n\nfilepath=\"./weights.best_{epoch:02d}-{val_loss:.2f}.hdf5\"\ncheckpoint = ModelCheckpoint(filepath, monitor='val_loss', \n                             verbose=1, save_best_only=True, mode='auto')\ncallback_list = [checkpoint,es,rlrop]\noptimizer = optimizers.Adam(lr=LEARNING_RATE)\nmodel.compile(optimizer=optimizer, loss=\"binary_crossentropy\",  metrics=metric_list)\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:57.109465Z","iopub.execute_input":"2021-06-26T05:52:57.109818Z","iopub.status.idle":"2021-06-26T05:52:57.306737Z","shell.execute_reply.started":"2021-06-26T05:52:57.109783Z","shell.execute_reply":"2021-06-26T05:52:57.305911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_TRAIN = train_generator.n//train_generator.batch_size\nSTEP_SIZE_VALID = val_generator.n//val_generator.batch_size","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:57.307904Z","iopub.execute_input":"2021-06-26T05:52:57.308419Z","iopub.status.idle":"2021-06-26T05:52:57.313082Z","shell.execute_reply.started":"2021-06-26T05:52:57.308380Z","shell.execute_reply":"2021-06-26T05:52:57.311959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_finetunning = model.fit_generator(generator=train_generator,\n                              steps_per_epoch=STEP_SIZE_TRAIN,\n                              validation_data=val_generator,\n                              validation_steps=STEP_SIZE_VALID,\n                              epochs=5,\n                              callbacks=callback_list,\n                              verbose=1).history\n","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:52:57.314498Z","iopub.execute_input":"2021-06-26T05:52:57.314935Z","iopub.status.idle":"2021-06-26T05:55:10.755536Z","shell.execute_reply.started":"2021-06-26T05:52:57.314900Z","shell.execute_reply":"2021-06-26T05:55:10.754617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimg = cv2.imread('./train/'+x_train.iloc[0,0])\n\nim_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\nplt.imshow(im_rgb)","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:59:45.567076Z","iopub.execute_input":"2021-06-26T05:59:45.567391Z","iopub.status.idle":"2021-06-26T05:59:45.901148Z","shell.execute_reply.started":"2021-06-26T05:59:45.567361Z","shell.execute_reply":"2021-06-26T05:59:45.900315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# modelTrained = create_model(input_shape=(HEIGHT, WIDTH, CHANNEL))\n# modelTrained.load_weights(\"./weights.best_01-0.68.hdf5\")\n","metadata":{"execution":{"iopub.status.busy":"2021-06-26T11:02:17.832778Z","iopub.execute_input":"2021-06-26T11:02:17.833027Z","iopub.status.idle":"2021-06-26T11:02:17.836148Z","shell.execute_reply.started":"2021-06-26T11:02:17.833004Z","shell.execute_reply":"2021-06-26T11:02:17.835318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# layer_outputs = [layer.output for layer in modelTrained.layers[1:7]]\n# activation_model = Model(inputs=modelTrained.input,outputs=layer_outputs)\n# activations = activation_model.predict(cv2.resize(im_rgb,(128,128)).reshape(1,128,128,3))\n# layer_names = []\n# for layer in modelTrained.layers[1:7]:\n#     layer_names.append(layer.name) # Names of the layers, so you can have them as part of your plot\n    \n\n# images_per_row = 16\n# for layer_name, layer_activation in zip(layer_names, activations): # Displays the feature maps\n#     n_features = layer_activation.shape[-1] # Number of features in the feature map\n#     size = layer_activation.shape[1] #The feature map has shape (1, size, size, n_features).\n#     n_cols = n_features // images_per_row # Tiles the activation channels in this matrix\n#     display_grid = np.zeros((size * n_cols, images_per_row * size))\n#     for col in range(n_cols): # Tiles each filter into a big horizontal grid\n#         for row in range(images_per_row):\n#             channel_image = layer_activation[0,\n#                                              :, :,\n#                                              col * images_per_row + row]\n#             channel_image -= channel_image.mean() # Post-processes the feature to make it visually palatable\n#             channel_image /= channel_image.std()\n#             channel_image *= 64\n#             channel_image += 128\n#             channel_image = np.clip(channel_image, 0, 255).astype('uint8')\n#             display_grid[col * size : (col + 1) * size, # Displays the grid\n#                          row * size : (row + 1) * size] = channel_image\n#     scale = 1. / size\n#     plt.figure(figsize=(scale * display_grid.shape[1],\n#                         scale * display_grid.shape[0]))\n#     plt.title(layer_name)\n#     plt.grid(False)\n#     plt.imshow(display_grid, aspect='auto', cmap='viridis')","metadata":{"execution":{"iopub.status.busy":"2021-06-26T05:59:48.614363Z","iopub.execute_input":"2021-06-26T05:59:48.614733Z","iopub.status.idle":"2021-06-26T05:59:48.749388Z","shell.execute_reply.started":"2021-06-26T05:59:48.614685Z","shell.execute_reply":"2021-06-26T05:59:48.746480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}