{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"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","metadata":{"_uuid":"99d88f29ec0337fbcf9c778a0bceaa92dca4ee04","execution":{"iopub.status.busy":"2021-08-15T03:53:55.973885Z","iopub.execute_input":"2021-08-15T03:53:55.974605Z","iopub.status.idle":"2021-08-15T03:53:58.069978Z","shell.execute_reply.started":"2021-08-15T03:53:55.974532Z","shell.execute_reply":"2021-08-15T03:53:58.068999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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/')","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.071608Z","iopub.execute_input":"2021-08-15T03:53:58.071883Z","iopub.status.idle":"2021-08-15T03:53:58.076256Z","shell.execute_reply.started":"2021-08-15T03:53:58.071831Z","shell.execute_reply":"2021-08-15T03:53:58.075274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.077528Z","iopub.execute_input":"2021-08-15T03:53:58.077925Z","iopub.status.idle":"2021-08-15T03:53:58.348705Z","shell.execute_reply.started":"2021-08-15T03:53:58.077863Z","shell.execute_reply":"2021-08-15T03:53:58.346387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_m = VGG16(weights='imagenet',\n               include_top=False,\n               input_shape=(150,150,3))","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.349645Z","iopub.status.idle":"2021-08-15T03:53:58.35Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_m.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.350838Z","iopub.status.idle":"2021-08-15T03:53:58.351124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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'))","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.35178Z","iopub.status.idle":"2021-08-15T03:53:58.352056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.352763Z","iopub.status.idle":"2021-08-15T03:53:58.353451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_m.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.35448Z","iopub.status.idle":"2021-08-15T03:53:58.354914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy',\n             optimizer=optimizers.RMSprop(lr=2e-5),\n             metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.355788Z","iopub.status.idle":"2021-08-15T03:53:58.356203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.357085Z","iopub.status.idle":"2021-08-15T03:53:58.357723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.358587Z","iopub.status.idle":"2021-08-15T03:53:58.359057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_numpy, train_y, batch_size=100, epochs=20, validation_split=0.25)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.359956Z","iopub.status.idle":"2021-08-15T03:53:58.360463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.36144Z","iopub.status.idle":"2021-08-15T03:53:58.361844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = range(1, len(loss)+1)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.36289Z","iopub.status.idle":"2021-08-15T03:53:58.363347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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()","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.364262Z","iopub.status.idle":"2021-08-15T03:53:58.364808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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()","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.365665Z","iopub.status.idle":"2021-08-15T03:53:58.366225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(train_numpy[1:2])","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.367137Z","iopub.status.idle":"2021-08-15T03:53:58.367782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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]))","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.3686Z","iopub.status.idle":"2021-08-15T03:53:58.369011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.load_img(train[1])","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.369882Z","iopub.status.idle":"2021-08-15T03:53:58.370392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.371165Z","iopub.status.idle":"2021-08-15T03:53:58.37158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_answer = model.predict(test_numpy)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.372555Z","iopub.status.idle":"2021-08-15T03:53:58.37294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.373781Z","iopub.status.idle":"2021-08-15T03:53:58.374189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_id_sub = pd.Series(test_id, name='id')","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.375057Z","iopub.status.idle":"2021-08-15T03:53:58.375645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = pd.Series(test_answer.reshape(12500,), name='label')","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.376424Z","iopub.status.idle":"2021-08-15T03:53:58.376866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([test_id_sub, results], axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.377644Z","iopub.status.idle":"2021-08-15T03:53:58.378134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.378955Z","iopub.status.idle":"2021-08-15T03:53:58.37943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"Cats_and_Dogs_CNN_VGG.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-15T03:53:58.380346Z","iopub.status.idle":"2021-08-15T03:53:58.380645Z"},"trusted":true},"execution_count":null,"outputs":[]}]}