{"cells": [{"execution_count": null, "cell_type": "markdown", "outputs": [], "metadata": {"collapsed": false, "_execution_state": "idle", "_uuid": "cf6460a4a09c335282228420c349d6f1bff2060f", "_cell_guid": "ed61ef31-a216-4440-8bc4-cb181c991edc"}, "source": "# Cats vs Dogs:\n## Binary Probabilistic Image Classification with a CNN\n\n---\n"}, {"execution_count": null, "cell_type": "code", "outputs": [], "metadata": {"trusted": false, "_execution_state": "idle", "_uuid": "c48b7fb785980502f9e014bcdd790de8788d5ed1", "_cell_guid": "dd66dd15-b08d-49cd-8fc3-ae3d1f62dabd"}, "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)\nimport os, random, cv2\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\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\nTRAIN_DIR = '../input/train/'\nTEST_DIR = '../input/test/'\n\nROWS = 64\nCOLS = 64\nCHANNELS = 3\n\ntrain_images = [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR)] # use this for full dataset\ntrain_dogs =   [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'dog' in i]\ntrain_cats =   [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'cat' in i]\n\ntest_images =  [TEST_DIR+i for i in os.listdir(TEST_DIR)]\n"}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": "print('train_images.shape | {0}'.format(len(train_images)))\nprint('train_dogs.shape   | {0}'.format(len(train_images)))\nprint('train_cats.shape   | {0}'.format(len(train_images)))", "metadata": {"_execution_state": "idle", "_uuid": "05f73b57f527be134c15057f594ee633c90d621f", "collapsed": false}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": "# slice datasets for memory efficiency on Kaggle Kernels, delete if using full dataset\ntrain_images = train_dogs[:1000] + train_cats[:1000]\nrandom.shuffle(train_images)\ntest_images =  test_images[:25]\n\ndef 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\ndef prep_data(images):\n    count = len(images)\n    data = np.ndarray((count, CHANNELS, ROWS, COLS), dtype=np.uint8)\n\n    for i, image_file in enumerate(images):\n        image = read_image(image_file)\n        data[i] = image.T\n        if i%250 == 0: print('Processed {} of {}'.format(i, count))\n    \n    return data\n\ntrain = prep_data(train_images)\ntest = prep_data(test_images)\n\nprint(\"Train shape: {}\".format(train.shape))\nprint(\"Test shape: {}\".format(test.shape))\n", "metadata": {"_execution_state": "idle", "_uuid": "89068ab9f7fd6c423ef2d07ea8499f7866f023ea", "collapsed": false}}], "nbformat": 4, "metadata": {"language_info": {"version": "3.6.1", "pygments_lexer": "ipython3", "mimetype": "text/x-python", "nbconvert_exporter": "python", "file_extension": ".py", "name": "python", "codemirror_mode": {"version": 3, "name": "ipython"}}, "kernelspec": {"display_name": "Python 3", "name": "python3", "language": "python"}}, "nbformat_minor": 0}