{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!unzip ../input/dogs-vs-cats-redux-kernels-edition/train.zip\n!unzip ../input/dogs-vs-cats-redux-kernels-edition/test.zip","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nimport random\nimport os\nimport numpy as np\n\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n%matplotlib inline\n\n\ntr_dir = '/kaggle/working/train'\nts_dir = '/kaggle/working/test'\n\ntr_imgs = [f'{tr_dir}/{i}' for i in os.listdir(tr_dir)]\nts_imgs = [f'{ts_dir}/{i}' for i in os.listdir(ts_dir)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def process_imgs(imgs, width=150, height=150):\n    x = []\n    y = []\n    for i in imgs:\n        x.append(cv2.resize(cv2.imread(i, cv2.IMREAD_COLOR),\n(width, height),\ninterpolation=cv2.INTER_CUBIC))\n        \n        label = 1 if 'dog' in i else 0\n        y.append(label)\n        \n    return np.array(x), np.array(y)\n\ntr_x, tr_y = process_imgs(tr_imgs)\n\n# plot random images\nplt.figure(figsize=(20, 10))\ncols = 5\nfor i in range(cols):\n    plt.subplot(5 / cols+1, cols, i+1)\n    plt.imshow(tr_x[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nfrom keras.preprocessing.image import img_to_array, load_img\n\ntr_data = ImageDataGenerator(rescale=1/255, \n                            rotation_range=40,\n                            width_shift_range=0.2,\n                            height_shift_range=0.2,\n                            shear_range=0.2,\n                            zoom_range=0.2,\n                            horizontal_flip=True)\n\ntr_gen = tr_data.flow(tr_x, tr_y, batch_size=32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.applications import InceptionResNetV2\nbase=InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(150, 150, 3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras import layers, models\n\nmodel = models.Sequential()\nmodel.add(base)\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(256, activation='relu'))\nmodel.add(layers.Dense(2, activation='softmax'))\n\nbase.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras import optimizers\nbatch_size = 32\nepochs = 20\nmodel.compile(loss='sparse_categorical_crossentropy',\n              optimizer=optimizers.RMSprop(lr=2e-5),\n              metrics=['acc'])\n\nhist = model.fit_generator(tr_gen,\n                           steps_per_epoch=tr_x.shape[0] // batch_size,\n                           epochs=epochs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}