{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nimport random\n%matplotlib inline\nplt.style.use(\"seaborn\")\n\nfrom sklearn.cross_validation import train_test_split\nfrom keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras.layers import Activation, Dropout, Flatten, Dense\nfrom keras import regularizers\nfrom keras import applications\nfrom keras import backend as K\nK.set_image_dim_ordering('tf')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c01eec913bd924bcdcc2100abc5cd1a1dc23dfb"},"cell_type":"code","source":"TRAIN_DIR = '../input/train/'\nTEST_DIR = '../input/test/'\n\ndef prepare_dataset(train_dir, test_dir, m_train=1000, m_test=20, m_validation=200):\n    # load the train data\n    cat_train_data = [TRAIN_DIR+cat for cat in os.listdir(TRAIN_DIR) if 'cat' in cat]\n    dog_train_data = [TRAIN_DIR+dog for dog in os.listdir(TRAIN_DIR) if 'dog' in dog]\n    train = cat_train_data[:m_train+m_validation] + dog_train_data[:m_train+m_validation]\n    random.shuffle(train)\n    \n    train_data = []\n    for image in train:\n        temp = load_img(image, target_size=(150,150), interpolation=\"nearest\")\n        x = np.asarray(temp)\n        train_data.append(x)\n    \n    # load the test data\n    test = [TEST_DIR+i for i in os.listdir(TEST_DIR)]\n    test = test[:m_test]\n    \n    test_data = []\n    for image in test:\n        temp = load_img(image, target_size=(150,150), interpolation=\"nearest\")\n        x = np.asarray(temp)\n        test_data.append(x)\n    \n    # generating labels for training data\n    label = []\n    for img in train:\n        if 'dog' in img:\n            label.append(1)\n        elif 'cat' in img:\n            label.append(0)\n    label = np.array(label)\n    label = label.reshape((label.shape[0], 1))\n    return np.array(train_data), label, np.array(test_data)\n\ntrain, label, test = prepare_dataset(TRAIN_DIR, TEST_DIR)\nprint(\"Train shape: \", train.shape)\nprint(\"Labels shape: \", label.shape)\nprint(\"Test shape: \", test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7b0a8d3b1baebfdae19e94f9ca29202bfc131823"},"cell_type":"code","source":"print(label[0])\nplt.imshow(train[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"33e756be341d9ef47100b14e51251ca72f5f3e39"},"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(train, label, test_size=1/6, random_state=23)\n\nX_train = X_train / 255\nX_val = X_val / 255\n\nprint(X_train.shape, X_val.shape, y_train.shape, y_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4b5981afb43df2b97c1949d4035b9fac2a58c305"},"cell_type":"markdown","source":"# VGG16"},{"metadata":{"trusted":true,"_uuid":"1e83b3255cb477a2420d9f18aab82f537838f8f7"},"cell_type":"code","source":"vgg = applications.VGG16(include_top=False, weights='imagenet', input_shape=X_train.shape[1:])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"70fbfe38d7750b20603c7507e497ec6d52fe5c46"},"cell_type":"code","source":"vgg_model = Sequential()\nvgg_model.add(vgg)\nvgg_model.add(Flatten())\nvgg_model.add(Dense(256, activation='relu', kernel_regularizer=regularizers.l2(0.01)))\nvgg_model.add(Dropout(0.5))\nvgg_model.add(Dense(128, activation='relu', kernel_regularizer=regularizers.l2(0.01)))\nvgg_model.add(Dropout(0.5))\nvgg_model.add(Dense(1, activation='sigmoid'))\n\nvgg_model.layers[0].trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6acb81006a07c7d1a21762daba4b2c8340f529e6"},"cell_type":"code","source":"vgg_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n\nhistory_vgg = vgg_model.fit(X_train, y_train, epochs=15, batch_size=100, validation_data=(X_val, y_val))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7d2867ed593aaf3244331fd953f55e27923e9ae"},"cell_type":"code","source":"plt.plot(history_vgg.history['loss'])\nplt.plot(history_vgg.history['val_loss'])\nplt.title(\"Train vs Validation Loss\")\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper right')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c961880b7a946d312e11caa9ef01ef909a763842"},"cell_type":"markdown","source":"# VGG19"},{"metadata":{"trusted":true,"_uuid":"e02b64b5689003df03467237491d00b68a5af68a"},"cell_type":"code","source":"vgg19 = applications.VGG19(include_top=False, weights='imagenet', input_shape=X_train.shape[1:])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b3669c598e04fcb108728781b7802fd6bc92cbd7"},"cell_type":"code","source":"vgg19_model = Sequential()\nvgg19_model.add(vgg19)\nvgg19_model.add(Flatten())\nvgg19_model.add(Dense(256, activation='relu', kernel_regularizer=regularizers.l2(0.01)))\nvgg19_model.add(Dropout(0.5))\nvgg19_model.add(Dense(128, activation='relu', kernel_regularizer=regularizers.l2(0.01)))\nvgg19_model.add(Dropout(0.5))\nvgg19_model.add(Dense(1, activation='sigmoid'))\n\nvgg19_model.layers[0].trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1aa22f5a222e897a626e2349c24ebf102f8d075c"},"cell_type":"code","source":"vgg19_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n\nhistory_vgg19 = vgg19_model.fit(X_train, y_train, epochs=15, batch_size=100, validation_data=(X_val, y_val))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dc1d24c2bd465a2ca1bc10dab508757dc4bf6332"},"cell_type":"code","source":"plt.plot(history_vgg19.history['loss'])\nplt.plot(history_vgg19.history['val_loss'])\nplt.title(\"Train vs Validation Loss\")\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper right')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2f5dc77b24c191437fb084ddcedb4546608b9d61"},"cell_type":"markdown","source":"# Inception v3"},{"metadata":{"trusted":true,"_uuid":"d4471725860edf44411441bebb96a0bdd8e51287"},"cell_type":"code","source":"inception = applications.InceptionV3(include_top=False, weights='imagenet', input_shape=X_train.shape[1:])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"172880a386455637c9b512387707d6aa4eb2660e"},"cell_type":"code","source":"inception_model = Sequential()\ninception_model.add(vgg19)\ninception_model.add(Flatten())\ninception_model.add(Dense(256, activation='relu', kernel_regularizer=regularizers.l2(0.01)))\ninception_model.add(Dropout(0.5))\ninception_model.add(Dense(128, activation='relu', kernel_regularizer=regularizers.l2(0.01)))\ninception_model.add(Dropout(0.5))\ninception_model.add(Dense(1, activation='sigmoid'))\n\ninception_model.layers[0].trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3e2daeff2df93c67181d29ed3443fd9963406f3d"},"cell_type":"code","source":"inception_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n\nhistory_inception = inception_model.fit(X_train, y_train, epochs=15, batch_size=100, validation_data=(X_val, y_val))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9a067c54ab318a88a065a4a71f716edb44c0d6d"},"cell_type":"code","source":"plt.plot(history_inception.history['loss'])\nplt.plot(history_inception.history['val_loss'])\nplt.ylabel(\"loss\")\nplt.xlabel(\"epoch\")\nplt.title(\"Training vs Validation Loss\")\nplt.legend(['train', 'test'], loc='upper right')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9e494f7731fcea02aa6e85db32b4d73403a4bbde"},"cell_type":"markdown","source":"> # VGG16 vs VGG19 vs InceptionV3"},{"metadata":{"trusted":true,"_uuid":"87ef7d2ad9701700e7beec2771a8efdab0f8f059"},"cell_type":"code","source":"f, ax = plt.subplots(2,1,figsize=(15,15))\nax[0].plot(history_vgg.history['val_loss'])\nax[0].plot(history_vgg19.history['val_loss'])\nax[0].plot(history_inception.history['val_loss'])\nax[0].set_ylabel(\"loss\")\nax[0].set_xlabel(\"epoch\")\nax[0].set_title(\"Validation Loss\")\nax[0].legend(['VGG16', 'VGG19', 'InceptionV3'], loc='upper right')\n\nax[1].plot(history_vgg.history['val_acc'])\nax[1].plot(history_vgg19.history['val_acc'])\nax[1].plot(history_inception.history['val_acc'])\nax[1].set_ylabel(\"accuracy\")\nax[1].set_xlabel(\"epoch\")\nax[1].set_title(\"Validation Accuracy\")\nax[1].legend(['VGG16', 'VGG19', 'InceptionV3'], loc='upper right')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"73259cadf0b4fb976847706a89da576df7701c8c"},"cell_type":"markdown","source":"# Predicting using VGG16"},{"metadata":{"trusted":true,"_uuid":"a1a316cec0b3b7e9aaa2e5613636a8a9f386f85f"},"cell_type":"code","source":"num_test = 5\npredictions = vgg_model.predict(test[:num_test,:,:,:])\nfor i in range(num_test):\n    if predictions[i] == 0:\n        print(\"Predicted: cat \", predictions[i])\n    else:\n        print(\"predicted: dog\", predictions[i])\n    plt.imshow(test[i])\n    plt.show()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}