{"metadata": {"kernelspec": {"name": "python3", "language": "python", "display_name": "Python 3"}, "language_info": {"name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1", "mimetype": "text/x-python", "file_extension": ".py", "codemirror_mode": {"name": "ipython", "version": 3}}}, "nbformat_minor": 1, "nbformat": 4, "cells": [{"metadata": {"_uuid": "6156c4394094f6ed1b896f30e22c8fdfcd7a5233", "collapsed": true, "_cell_guid": "71628e4a-3810-494d-b823-75f2c009cad2"}, "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", "\n", "import numpy as np # linear algebra\n", "import 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", "\n", "from subprocess import check_output\n", "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n", "\n", "# Any results you write to the current directory are saved as output."], "outputs": [], "execution_count": null}, {"metadata": {"_uuid": "7150412c4a16940d79ea93dc636059af91b9e451", "_cell_guid": "6098d251-e4b3-4a7e-bc47-2ad6ffe41e04"}, "cell_type": "code", "source": ["import os\n", "import numpy as np\n", "import pandas as pd\n", "import random\n", "import cv2\n", "from keras.preprocessing.image import ImageDataGenerator\n", "from keras.models import Sequential\n", "from keras.layers import Conv2D, MaxPooling2D\n", "from keras.layers import Activation, Dropout, Flatten, Dense\n", "from keras import backend as K"], "outputs": [], "execution_count": 34}, {"metadata": {"_uuid": "3989cc71200e99556ea9a1ef406419ae2eaac2b1", "_cell_guid": "4ea17c97-b7b3-426d-9435-baec104dd053"}, "cell_type": "code", "source": ["TRAIN_DIR = '../input/train/'\n", "TEST_DIR = '../input/test/'\n", "\n", "ROWS = 150\n", "COLS = 150\n", "CHANNELS = 3\n", "\n", "train_images = [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR)] # use this for full dataset\n", "train_dogs =   [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'dog' in i]\n", "train_cats =   [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'cat' in i]\n", "\n", "test_images =  [TEST_DIR+i for i in os.listdir(TEST_DIR)]\n", "\n", "\n", "# slice datasets for memory efficiency on Kaggle Kernels, delete if using full dataset\n", "train_images = train_dogs[:1000] + train_cats[:1000]\n", "validation = train_dogs[1000:1400] + train_cats[1000:1400]\n", "random.shuffle(train_images)\n", "test_images =  test_images[:25]\n", "\n", "def read_image(file_path):\n", "    img = cv2.imread(file_path, cv2.IMREAD_COLOR) #cv2.IMREAD_GRAYSCALE\n", "    return cv2.resize(img, (ROWS, COLS), interpolation=cv2.INTER_CUBIC)\n", "\n", "\n", "def prep_data(images):\n", "    count = len(images)\n", "    data = np.ndarray((count, ROWS, COLS, CHANNELS), dtype=np.uint8)\n", "\n", "    for i, image_file in enumerate(images):\n", "        image = read_image(image_file)\n", "        data[i] = image\n", "    #print(data[1].shape)\n", "    return data\n", "\n", "train = prep_data(train_images)\n", "test = prep_data(test_images)\n", "validation = prep_data(validation)\n"], "outputs": [], "execution_count": 35}, {"metadata": {"_uuid": "1fe721ea27a16ff4785f0c3cc4a651ce5f20189c", "_cell_guid": "ca044746-32ab-45c0-8905-89eac7abff94"}, "cell_type": "code", "source": ["labels = []\n", "for i in train_images:\n", "    if 'dog' in i:\n", "        labels.append(1)\n", "    else:\n", "        labels.append(0)\n", "print(labels)"], "outputs": [], "execution_count": null}, {"metadata": {"_uuid": "8c32c8ef456ad6f1892884a592dd8973016343b1", "_cell_guid": "d2474d19-79c3-40fd-a127-2e1382310413"}, "cell_type": "code", "source": ["img_width, img_height = 150, 150\n", "nb_train_samples = 2000\n", "nb_validation_samples = 800\n", "epochs = 50\n", "batch_size = 16\n", "input_shape = (img_width, img_height, 3)\n", "\n", "\n", "\n", "model = Sequential()\n", "model.add(Conv2D(32, (3, 3), input_shape=input_shape,border_mode='same'))\n", "model.add(Activation('relu'))\n", "model.add(MaxPooling2D(pool_size=(2, 2)))\n", "\n", "model.add(Conv2D(32, (3, 3),border_mode='same'))\n", "model.add(Activation('relu'))\n", "model.add(MaxPooling2D(pool_size=(2, 2)))\n", "\n", "model.add(Conv2D(64, (3, 3),border_mode='same'))\n", "model.add(Activation('relu'))\n", "model.add(MaxPooling2D(pool_size=(2, 2)))\n", "\n", "model.add(Flatten())\n", "model.add(Dense(64))\n", "model.add(Activation('relu'))\n", "model.add(Dropout(0.5))\n", "model.add(Dense(1))\n", "\n", "model.add(Activation('sigmoid'))\n", "model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])\n", "# this is the augmentation configuration we will use for training\n", "\"\"\"train_datagen = ImageDataGenerator(rescale=1. / 255,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)\n", "\n", "# this is the augmentation configuration we will use for testing:\n", "# only rescaling\n", "test_datagen = ImageDataGenerator(rescale=1. / 255)\n", "train_generator = train_datagen.flow(train ,batch_size=batch_size)\n", "\n", "validation_generator = test_datagen.flow(validation , batch_size=batch_size)\"\"\"\n", "\"\"\"model.fit_generator(\n", "        train_generator,\n", "        steps_per_epoch=2000 // batch_size,\n", "        epochs=50,\n", "        validation_data=validation_generator,\n", "        validation_steps=800 // batch_size)\n", "\"\"\"\n", "model.fit(train, labels, batch_size=batch_size, epochs=epochs)"], "outputs": [], "execution_count": 26}, {"metadata": {}, "cell_type": "code", "source": ["predictions = model.predict(test, verbose=0)\n", "print(predictions)\n", "c=0\n", "for i in range(0,len(predictions)):\n", "    if predictions[i]==0:\n", "        c=c+1\n", "print(c)"], "outputs": [], "execution_count": 31}]}