{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"5b9c1870-2ac7-859c-73e4-63d8d9a93dff"},"outputs":[],"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, cv2, random\n\nimport matplotlib.pyplot as plt\nfrom matplotlib import ticker\nimport seaborn as sns\n%matplotlib inline \n\nfrom IPython.display import display, Image, HTML\n\nTRAIN_DIR = '../input/train/'\nTEST_DIR = '../input/test/'"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"9649a589-5061-319a-d2ce-3f76988ead5d"},"outputs":[],"source":"ROWS = 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\n\n# slice datasets for memory efficiency on Kaggle Kernels, delete if using full dataset\ntrain_images = train_dogs[:12000] + train_cats[:12000]\nrandom.shuffle(train_images)\ntest_images =  test_images[:100]\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))"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"b9a3dd78-2232-1d70-566b-0411ab95a6eb"},"outputs":[],"source":"labels = []\nfor i in train_images:\n    if 'dog' in i:\n        labels.append(1)\n    else:\n        labels.append(0)\n\nsns.countplot(labels)\nsns.plt.title('Cats and Dogs')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"c35f77fd-8cdc-3666-a228-fa33af0deaa6"},"outputs":[],"source":"def show_cats_and_dogs(idx):\n    cat = read_image(train_cats[idx])\n    dog = read_image(train_dogs[idx])\n    pair = np.concatenate((cat, dog), axis=1)\n    plt.figure(figsize=(10,5))\n    plt.imshow(pair)\n    plt.show()\n    \nfor idx in range(0,2):\n    show_cats_and_dogs(idx)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"5d4ca5f3-b9b5-59ce-3ba9-032c80125883"},"outputs":[],"source":"dog_avg = np.array([dog[0].T for i, dog in enumerate(train) if labels[i]==1]).mean(axis=0)\nplt.imshow(dog_avg)\nplt.title('Your Average Dog')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"820b37d4-2bfa-ef21-56eb-eae5c1a43f4e"},"outputs":[],"source":"cat_avg = np.array([cat[0].T for i, cat in enumerate(train) if labels[i]==0]).mean(axis=0)\nplt.imshow(cat_avg)\nplt.title('Your Average Cat')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"2a45f96b-9610-b1c9-9fb4-3c26cf73eb2b"},"outputs":[],"source":"import tensorflow as tf\nimage_size = 64 # TODO: redundant, consolidate\nnum_labels = 2\nnum_channels = 3 # rg\n\ndef reformat(dataset, labels):\n  dataset = dataset.reshape(\n    (-1, image_size, image_size, num_channels)).astype(np.float32)\n  labels = (labels=='cats').astype(np.float32); # set dogs to 0 and cats to 1\n  labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)\n  return dataset, labels\ntrain_dataset, train_labels = reformat(train, labels)\nvalid_dataset, valid_labels = reformat(valid_dataset, valid_labels)\ntest_dataset, test_labels = reformat(test_dataset, test_labels)\nprint ('Training set', train_dataset.shape, train_labels.shape)\nprint ('Validation set', valid_dataset.shape, valid_labels.shape)\nprint ('Test set', test_dataset.shape, test_labels.shape)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"7cf3a1ec-d556-9a0e-8840-a834a9ac998c"},"outputs":[],"source":""}],"metadata":{"_change_revision":0,"_is_fork":false,"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.0"}},"nbformat":4,"nbformat_minor":0}