{"cells":[{"source":"import os, cv2, random\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nfrom matplotlib import ticker\nimport seaborn as sns\n%matplotlib inline \n\nfrom keras.models import Sequential\nfrom keras.layers import Input, Dropout, Flatten, Convolution2D, MaxPooling2D, Dense, Activation\nfrom keras.optimizers import RMSprop\nfrom keras.callbacks import ModelCheckpoint, Callback, EarlyStopping\nfrom keras.utils import np_utils","metadata":{"_uuid":"428539ca45e4b4a829c36857d2ee424b93114255","_cell_guid":"569a9821-06eb-4a88-aef8-c56f9f66cad1","collapsed":true},"cell_type":"code","outputs":[],"execution_count":null},{"source":"TRAIN_DIR = '../input/train/'\nTEST_DIR = '../input/test/'\n\nROWS = 64\nCOLS = 64\nCHANNELS = 3\n\ntrain_images = []\ntrain_dogs = []\ntrain_cats = []\ntest_images = []\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\nlen(train_images)","metadata":{"_uuid":"4a3c7715f521fa17b4b5eb8a16fba8efe50eb375","_cell_guid":"74191b94-f4b3-43b0-b577-16d73bf3f669","collapsed":true},"cell_type":"code","outputs":[],"execution_count":null},{"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]\nlen(train_images)\nprint(train_images[0])","metadata":{"_uuid":"440b4c19990a2ecdc82d8d30c0390212cad4100e","_cell_guid":"d64add67-eaa6-4da8-8a00-470a3d87c25f","collapsed":true},"cell_type":"code","outputs":[],"execution_count":null},{"source":"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\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))","metadata":{"_uuid":"02bc3a0c8449e19ad9f866cf539850b6bac72c75","_cell_guid":"f449b4c6-2476-418d-8b3f-823106eb1b7e","collapsed":true},"cell_type":"code","outputs":[],"execution_count":null},{"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)\nplt.title('Cats and Dogs')","metadata":{"_uuid":"464721b17a54f6ed22e4ab038f7c5a3e1177aab8","_cell_guid":"71e495ac-7b5c-43e5-a8e0-36af05dabd93","collapsed":true},"cell_type":"code","outputs":[],"execution_count":null},{"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,5):\n    show_cats_and_dogs(idx)","metadata":{"_uuid":"c21a7a065b6a56e488107ccf6615373480a5a0bc","_cell_guid":"d1a7625c-9ca5-459a-baa3-d3a2b551de84","collapsed":true},"cell_type":"code","outputs":[],"execution_count":null},{"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')","metadata":{"_uuid":"8feddcb1e1e20124d9cf30ff988d4e7d589deb94","_cell_guid":"f16c0514-8917-44a5-b893-21d304caeda7","collapsed":true},"cell_type":"code","outputs":[],"execution_count":null},{"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')","metadata":{"_uuid":"bf1ed12a12bdcd791e51ff9c6e2ab0f18f320611","_cell_guid":"f88cc315-b262-49a7-bfe6-aed2923b7635","collapsed":true},"cell_type":"code","outputs":[],"execution_count":null},{"source":"optimizer = RMSprop(lr=1e-4)\nobjective = 'binary_crossentropy'\n\n\ndef catdog():\n    \n    model = Sequential()\n\n    model.add(Convolution2D(32, 3, 3, border_mode='same', input_shape=(3, ROWS, COLS), activation='relu'))\n    model.add(Convolution2D(32, 3, 3, border_mode='same', activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2),dim_ordering=\"th\"))\n\n    model.add(Convolution2D(64, 3, 3, border_mode='same', activation='relu'))\n    model.add(Convolution2D(64, 3, 3, border_mode='same', activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2),dim_ordering=\"th\"))\n    \n    model.add(Convolution2D(128, 3, 3, border_mode='same', activation='relu'))\n    model.add(Convolution2D(128, 3, 3, border_mode='same', activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2),dim_ordering=\"th\"))\n    \n    model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))\n    model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))\n#     model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2),dim_ordering=\"th\"))\n\n#     model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))\n#     model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))\n#     model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))\n#     model.add(MaxPooling2D(pool_size=(2, 2)))\n\n    model.add(Flatten())\n    model.add(Dense(256, activation='relu'))\n    model.add(Dropout(0.5))\n    \n    model.add(Dense(256, activation='relu'))\n    model.add(Dropout(0.5))\n\n    model.add(Dense(1))\n    model.add(Activation('sigmoid'))\n\n    model.compile(loss=objective, optimizer=optimizer, metrics=['accuracy'])\n    return model\n\n\nmodel = catdog()","metadata":{"_uuid":"91740596a12bd9f28d40fe0b51ca1044a21e638d","_cell_guid":"62ec7061-1170-4e11-aa58-bcd461173c54","collapsed":true},"cell_type":"code","outputs":[],"execution_count":null},{"source":"nb_epoch = 10\nbatch_size = 16\n\n## Callback for loss logging per epoch\nclass LossHistory(Callback):\n    def on_train_begin(self, logs={}):\n        self.losses = []\n        self.val_losses = []\n        \n    def on_epoch_end(self, batch, logs={}):\n        self.losses.append(logs.get('loss'))\n        self.val_losses.append(logs.get('val_loss'))\n\nearly_stopping = EarlyStopping(monitor='val_loss', patience=3, verbose=1, mode='auto')        \n        \ndef run_catdog():\n    \n    history = LossHistory()\n    model.fit(train, labels, batch_size=batch_size, nb_epoch=nb_epoch,\n              validation_split=0.25, verbose=0, shuffle=True, callbacks=[history, early_stopping])\n    \n\n    predictions = model.predict(test, verbose=0)\n    return predictions, history\n\npredictions, history = run_catdog()","metadata":{"_uuid":"69b24f2ada27aa6271b67ca95e04edcb44ba7b8d","_cell_guid":"2ed17a45-d016-4a43-8ea8-3d1b808f911b","collapsed":true},"cell_type":"code","outputs":[],"execution_count":null},{"source":"loss = history.losses\nval_loss = history.val_losses\n\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.title('VGG-16 Loss Trend')\nplt.plot(loss, 'blue', label='Training Loss')\nplt.plot(val_loss, 'green', label='Validation Loss')\nplt.xticks(range(0,nb_epoch)[0::2])\nplt.legend()\nplt.show()","metadata":{"_uuid":"f694cd3bccbd2d899fcdbcbb52902d21f0cf8219","_cell_guid":"d56b2bc1-16f8-45c9-8e58-62a2d94b355b","collapsed":true},"cell_type":"code","outputs":[],"execution_count":null},{"source":"for i in range(0,10):\n    if predictions[i, 0] >= 0.5: \n        print('I am {:.2%} sure this is a Dog'.format(predictions[i][0]))\n    else: \n        print('I am {:.2%} sure this is a Cat'.format(1-predictions[i][0]))\n        \n    plt.imshow(test[i].T)\n    plt.show()","metadata":{"_uuid":"4f555540e1143de8e08df9d6064554a0f1c56082","_cell_guid":"2a1fa42e-df01-4b8b-ba6d-57537afe1577","collapsed":true},"cell_type":"code","outputs":[],"execution_count":null},{"source":"","metadata":{"_uuid":"bdedc46c49853d1305334bcbb1369c5da853c702","_cell_guid":"fa86c1f0-48a3-41b6-907b-bb3747fa5a8a","collapsed":true},"cell_type":"code","outputs":[],"execution_count":null}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"codemirror_mode":{"version":3,"name":"ipython"},"mimetype":"text/x-python","pygments_lexer":"ipython3","version":"3.6.5","file_extension":".py","name":"python","nbconvert_exporter":"python"}},"nbformat":4,"nbformat_minor":1}