{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\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\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-22T03:50:58.737273Z","iopub.execute_input":"2022-08-22T03:50:58.737565Z","iopub.status.idle":"2022-08-22T03:50:58.763566Z","shell.execute_reply.started":"2022-08-22T03:50:58.737497Z","shell.execute_reply":"2022-08-22T03:50:58.762970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget --no-check-certificate https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip","metadata":{"execution":{"iopub.status.busy":"2022-08-22T03:50:58.766700Z","iopub.execute_input":"2022-08-22T03:50:58.767100Z","iopub.status.idle":"2022-08-22T03:50:59.707077Z","shell.execute_reply.started":"2022-08-22T03:50:58.767078Z","shell.execute_reply":"2022-08-22T03:50:59.705865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\ndata = zipfile.ZipFile('./cats_and_dogs_filtered.zip','r')\ndata.extractall()\ndata.close()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T03:50:59.708527Z","iopub.execute_input":"2022-08-22T03:50:59.709501Z","iopub.status.idle":"2022-08-22T03:51:00.464646Z","shell.execute_reply.started":"2022-08-22T03:50:59.709473Z","shell.execute_reply":"2022-08-22T03:51:00.463685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check content of train folder\nimport os\nprint(\"Contents of this folder are:\")\nprint(os.listdir('cats_and_dogs_filtered'))","metadata":{"execution":{"iopub.status.busy":"2022-08-22T03:51:00.468181Z","iopub.execute_input":"2022-08-22T03:51:00.468766Z","iopub.status.idle":"2022-08-22T03:51:00.475515Z","shell.execute_reply.started":"2022-08-22T03:51:00.468724Z","shell.execute_reply":"2022-08-22T03:51:00.474175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntrain_gen = ImageDataGenerator(rescale = 1.0/255)\ntrain_data = train_gen.flow_from_directory('cats_and_dogs_filtered/train',target_size = (200,200),class_mode='binary',batch_size = 32)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T03:51:00.476507Z","iopub.execute_input":"2022-08-22T03:51:00.476773Z","iopub.status.idle":"2022-08-22T03:51:05.807891Z","shell.execute_reply.started":"2022-08-22T03:51:00.476750Z","shell.execute_reply":"2022-08-22T03:51:05.807261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_gen = ImageDataGenerator(rescale = 1.0/255)\nvalid_data = valid_gen.flow_from_directory('cats_and_dogs_filtered/validation',target_size = (200,200),class_mode='binary',batch_size = 32)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T03:51:05.809082Z","iopub.execute_input":"2022-08-22T03:51:05.809632Z","iopub.status.idle":"2022-08-22T03:51:05.918482Z","shell.execute_reply.started":"2022-08-22T03:51:05.809607Z","shell.execute_reply":"2022-08-22T03:51:05.917862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nmodel = Sequential()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T03:51:05.919488Z","iopub.execute_input":"2022-08-22T03:51:05.919852Z","iopub.status.idle":"2022-08-22T03:51:05.970126Z","shell.execute_reply.started":"2022-08-22T03:51:05.919828Z","shell.execute_reply":"2022-08-22T03:51:05.969510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Conv2D, Dense, Flatten, MaxPooling2D, Dropout\nmodel.add(Conv2D(20,(3,3),activation='relu',input_shape = (200,200,3)))\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Conv2D(40,(3,3),activation='relu'))\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Conv2D(80,(3,3),activation='relu'))\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Flatten())\nmodel.add(Dense(250,activation = 'relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(150,activation = 'relu'))\nmodel.add(Dense(50,activation = 'relu'))\nmodel.add(Dropout(0.25))\nmodel.add(Dense(1,activation = 'sigmoid'))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T03:51:05.971123Z","iopub.execute_input":"2022-08-22T03:51:05.971492Z","iopub.status.idle":"2022-08-22T03:51:06.279045Z","shell.execute_reply.started":"2022-08-22T03:51:05.971470Z","shell.execute_reply":"2022-08-22T03:51:06.278153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-08-22T03:51:06.280256Z","iopub.execute_input":"2022-08-22T03:51:06.281014Z","iopub.status.idle":"2022-08-22T03:51:06.294038Z","shell.execute_reply.started":"2022-08-22T03:51:06.280972Z","shell.execute_reply":"2022-08-22T03:51:06.293061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint,EarlyStopping\nstop = EarlyStopping(patience=3)\ncheck = ModelCheckpoint('best_model.hdf5',monitor='val_loss',save_best_only=True)\nmodel.fit(train_data,epochs=20,callbacks=[stop,check],validation_data=valid_data)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T03:51:06.297396Z","iopub.execute_input":"2022-08-22T03:51:06.297684Z","iopub.status.idle":"2022-08-22T03:56:20.664342Z","shell.execute_reply.started":"2022-08-22T03:51:06.297659Z","shell.execute_reply":"2022-08-22T03:56:20.663538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport random\nfrom tensorflow.keras.preprocessing.image import img_to_array , load_img\n\nsuccessive_output = [layer.output for layer in model.layers]\nvisual_model = tf.keras.models.Model(inputs = model.input, outputs = successive_output)\nimg_path = 'cats_and_dogs_filtered/validation/dogs/dog.2140.jpg'\nimg = load_img(img_path,target_size=(200,200))\nx = img_to_array(img)\nx = x.reshape((1,) + x.shape)\nx/= 255\nsuccessive_feature_maps = visual_model.predict(x)\n\n# These are the names of the layers, so you can have them as part of our plot\nlayer_names = [layer.name for layer in model.layers]\n\n# Display the representations\nfor layer_name, feature_map in zip(layer_names, successive_feature_maps):\n  \n  if len(feature_map.shape) == 4:\n    \n    #-------------------------------------------\n    # Just do this for the conv / maxpool layers, not the fully-connected layers\n    #-------------------------------------------\n    n_features = feature_map.shape[-1]  # number of features in the feature map\n    size       = feature_map.shape[ 1]  # feature map shape (1, size, size, n_features)\n    \n    # Tile the images in this matrix\n    display_grid = np.zeros((size, size * n_features))\n    \n    #-------------------------------------------------\n    # Postprocess the feature to be visually palatable\n    #-------------------------------------------------\n    for i in range(n_features):\n      x  = feature_map[0, :, :, i]\n      x -= x.mean()\n      x /= x.std ()\n      x *=  64\n      x += 128\n      x  = np.clip(x, 0, 255).astype('uint8')\n      display_grid[:, i * size : (i + 1) * size] = x # Tile each filter into a horizontal grid\n\n    #-----------------\n    # Display the grid\n    #-----------------\n    scale = 20. / n_features\n    plt.figure( figsize=(scale * n_features, scale) )\n    plt.title ( layer_name )\n    plt.grid  ( False )\n    plt.imshow( display_grid, aspect='auto', cmap='viridis' ) ","metadata":{"execution":{"iopub.status.busy":"2022-08-22T04:15:12.002427Z","iopub.execute_input":"2022-08-22T04:15:12.002743Z","iopub.status.idle":"2022-08-22T04:15:12.997762Z","shell.execute_reply.started":"2022-08-22T04:15:12.002719Z","shell.execute_reply":"2022-08-22T04:15:12.997036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}