{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# Import libraries\nimport numpy as np\nimport keras\nimport cv2\n\nimport matplotlib.pyplot as plt\n%matplotlib inline \n\nimport os        # To see our directory\nimport random\nimport gc        # Garbage collector","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dir = \"../input/dogs-vs-cats-redux-kernels-edition/train\"\ntest_dir = \"../input/dogs-vs-cats-redux-kernels-edition/test\"\n\ntrain_cats = ['../input/dogs-vs-cats-redux-kernels-edition/train/{}'.format(i) for i in os.listdir(train_dir) if 'cat' in i]\ntrain_dogs = ['../input/dogs-vs-cats-redux-kernels-edition/train/{}'.format(i) for i in os.listdir(train_dir) if 'dog' in i]\n\ntrain_images = train_cats[:2000] + train_dogs[:2000]\nrandom.shuffle(train_images)\n\n\ndel train_cats, train_dogs\ngc.collect()\n\n#import matplotlib.image as mpimg\n#for ima in train_images[0:5]:\n#    img = mpimg.imread(ima)\n#    imgplot = plt.imshow(img)\n#    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Image dimensions\n\nnrows = 200\nncolumns = 200\n# RGB are 3 channels\n# For greyscale, 1 channel\nchannels = 3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def read_and_process_image(list_of_images):\n    # X is an array of resized images\n    # y is an array of labels\n    \n    X = []\n    y = []\n    \n    for image in list_of_images:\n        # Read the image\n        X.append(cv2.resize(cv2.imread(image, cv2.IMREAD_COLOR), (nrows, ncolumns), interpolation=cv2.INTER_CUBIC))\n        \n        # Get the labels\n        if 'dog' in image:\n            y.append(1)\n        elif 'cat' in image:\n            y.append(0)\n            \n        return X, y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X, y = read_and_process_image(train_images)\n\ndel train_images\ngc.collect()\n\nX = np.array(X)\ny = np.array(y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.20, train_size=0.80, random_state=2)\n\nprint(\"Shape of train images is:\", X_train.shape)\nprint(\"Shape of validation images is:\", X_val.shape)\nprint(\"Shape of train labels is:\", y_train.shape)\nprint(\"Shape of validation labels is:\", y_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Built and fit CNN\n\n\nmodel = keras.models.Sequential()\n\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape=(200, 200, 3)))\nmodel.add(Flatten())\nmodel.add(Dense(2, activation='softmax'))\n\nmodel.compile(Adam(lr=0.001), loss=categorical_crossentropy, metrics=['accuracy'])\nmodel.fit_generator(train_images, steps_per_epoch=14, epochs=5, verbose=2)\n          ","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":1}