{"cells":[{"metadata":{"trusted":true,"_uuid":"b8921714f9d2707effcb74466a433f5a6b229b81"},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4ef67c36e505927547d813f1a17be421b13c3dc3"},"cell_type":"code","source":"import cv2                 \nimport os                  \nfrom tqdm import tqdm\nfrom random import shuffle\nfrom random import shuffle\nfrom keras.preprocessing import image\nimport matplotlib.pyplot as plt\nTRAIN_DIR = '../input/train'\nTEST_DIR = '../input/test'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8081c3e78ba95a068d0658016c3afc746a7add70"},"cell_type":"code","source":"IMG_SIZE = 299","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"79e5ec671ba612fdbb92bceb1ee89a4311701dc8"},"cell_type":"code","source":"train_list = os.listdir(TRAIN_DIR)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c404d23b598e860645398ebf9c5aa6ad2521f608"},"cell_type":"code","source":"cat_list = [i for i in train_list if \"cat\" in i]\ndog_list = [i for i in train_list if \"dog\" in i]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d87d2b8a77f7baeba1c991bfa95bed3d980dd025"},"cell_type":"code","source":"len(cat_list), len(dog_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9ec70c4ae40464d3ff14eb3e5bc69b37b9549c2"},"cell_type":"code","source":"#Creating training and validation splits\ntrain_list = cat_list[:10000] + dog_list[:10000]\nval_list = cat_list[10000:] + dog_list[10000:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"09bfbfcc35ff2e9ce6bc0652a373fa63f304e802"},"cell_type":"code","source":"shuffle(train_list)\nshuffle(val_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a085f7ea3623290293256678aa3296d855764bce"},"cell_type":"code","source":"len(train_list), len(val_list)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"207221332d9833f302ff46d27effaeec3b6ce3e0"},"cell_type":"code","source":"#Function for defining label\ndef label_img(img):\n    if 'cat' in img: return [0, 1]\n    elif 'dog' in img: return [1, 0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fbebb09b34c6f63b395955123458d0fb64563820"},"cell_type":"code","source":"#Function for resizing images\ndef create_train_data(train_list):\n    training_data = []\n    for img in train_list:\n        label = label_img(img)\n        path = os.path.join(TRAIN_DIR, img)\n        img = cv2.imread(path)\n        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n        training_data.append([np.array(img),label])\n    #np.save('train_data.npy', training_data)\n    return training_data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"85bfc027706ad63e0cf550bb6d987e42097ff962"},"cell_type":"code","source":"#Importing keras libraries\nimport keras\nfrom keras.applications.inception_v3 import InceptionV3\nfrom keras.layers import Input, Dense, Flatten, Dropout\nfrom keras.layers import MaxPooling2D, Conv2D, GlobalAveragePooling2D\nfrom keras.optimizers import SGD, Adam\nfrom keras.models import Model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"70df24c2209550fcb5223be249906282ca6c4886"},"cell_type":"code","source":"from keras import backend as K\nK.set_image_data_format('channels_last')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a8e103b49aa8ecf9be04a190cdb1f650c92a7189"},"cell_type":"code","source":"# create the base pre-trained model\nbase_model = InceptionV3(weights='imagenet', include_top=False)\n\n# add a global spatial average pooling layer\nx = base_model.output\nx = GlobalAveragePooling2D()(x)\n# let's add a fully-connected layer\nx = Dense(64, activation='relu')(x)\npredictions = Dense(2, activation='softmax')(x)\n\n# this is the model we will train\nmodel = Model(inputs=base_model.input, outputs=predictions)\n\n# first: train only the top layers (which were randomly initialized)\n# i.e. freeze all convolutional InceptionV3 layers\nfor layer in base_model.layers:\n    layer.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"92f1f9a7fd3a51d6746740c0ebe8cf3389f0e185"},"cell_type":"code","source":"for i, layer in enumerate(base_model.layers):\n    print(i, layer.name)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"48623cd7fac49ed68040868947b640cd17631edd"},"cell_type":"code","source":"from keras.utils import plot_model\nplot_model(model, to_file='model.png', show_shapes=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b5ffa627e9710478b715865e3414e38381fd7d91"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9cb4f3adff09e2f81a926c97f529dbcb2534b659"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}