{"cells":[{"metadata":{"trusted":true,"_uuid":"99d88f29ec0337fbcf9c778a0bceaa92dca4ee04"},"cell_type":"code","source":"import os \nimport numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt \nfrom keras import models\nfrom keras import layers\nfrom keras import optimizers\nfrom keras.preprocessing import image\nfrom keras.applications import VGG16","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_dir = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/'\ntrain_dir = os.path.join(base_dir, 'train/')\ntest_dir = os.path.join(base_dir, 'test/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images = []\nfor i in os.listdir(train_dir):\n    train_images.append(train_dir+i)\n    \ntest_images = []\nfor i in os.listdir(test_dir):\n    test_images.append(test_dir+i)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"conv_m = VGG16(weights='imagenet',\n               include_top=False,\n               input_shape=(150,150,3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"conv_m.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = models.Sequential()\nmodel.add(conv_m)\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(256, activation='relu'))\nmodel.add(layers.Dense(1, activation='sigmoid'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"conv_m.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(loss='binary_crossentropy',\n             optimizer=optimizers.RMSprop(lr=2e-5),\n             metrics=['acc'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = train_images[:2000]\ntrain_numpy = []\nfor i in train:\n    img = image.load_img(i, target_size=(150,150))\n    img = image.img_to_array(img)\n    img = img/255\n    train_numpy.append(img)\n    \ntrain_numpy = np.array(train_numpy)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_y = []\nfor i in train:\n    if 'dog.' in i:\n        train_y.append(1)\n    else:\n        train_y.append(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(train_numpy, train_y, batch_size=100, epochs=20, validation_split=0.25)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"loss = history.history['loss']\nval_loss = history.history['val_loss']\nacc = history.history['acc']\nval_acc = history.history['val_acc']\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"epochs = range(1, len(loss)+1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(epochs, loss, 'bo', label='Loss')\nplt.plot(epochs, val_loss, 'b', label='Val_loss')\nplt.title(\"Loss\")\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(epochs, acc, 'bo', label='Acc')\nplt.plot(epochs, val_acc, 'b', label='Val_Acc')\nplt.title(\"Acc\")\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = model.predict(train_numpy[1:2])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"if pred >= 0.5: \n    print('I am {:.2%} sure this is a Dog'.format(pred[0][0]))\nelse: \n    print('I am {:.2%} sure this is a Cat'.format((1-pred[0])[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image.load_img(train[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntest_numpy = []\nfor i in test_images:\n    if '/kaggle/input/dogs-vs-cats-redux-kernels-edition/test/test' in i :\n        continue\n    test_img = image.load_img(i, target_size=(150,150))\n    test_img = image.img_to_array(test_img)\n    test_img = test_img/255\n    test_numpy.append(test_img)\n    \ntest_numpy = np.array(test_numpy)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_answer = model.predict(test_numpy)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_id = []\nfor i in os.listdir(test_dir):\n    if 'test' in i :\n        continue    \n    num = i.split('.')[0]\n    test_id.append(num)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_id_sub = pd.Series(test_id, name='id')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results = pd.Series(test_answer.reshape(12500,), name='label')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.concat([test_id_sub, results], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv(\"Cats_and_Dogs_CNN_VGG.csv\", index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}