{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport zipfile  # for processing the zip files\n\nimport cv2  # for image processing\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport random\nimport gc  # garbage collector","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN_ZIP = '../input/dogs-vs-cats-redux-kernels-edition/train.zip'\nzip_ref = zipfile.ZipFile(TRAIN_ZIP, 'r')\nzip_ref.extractall('training')\nzip_ref.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TEST_ZIP = '../input/dogs-vs-cats-redux-kernels-edition/test.zip'\nzip_ref = zipfile.ZipFile(TEST_ZIP, 'r')\nzip_ref.extractall('test_all')\nzip_ref.close()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"TRAIN_DIR = '/kaggle/working/training/train/'\nTEST_DIR = '/kaggle/working/test_all/test/'\n\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_imgs = [TEST_DIR + i for i in os.listdir(TEST_DIR)]\n\n# take trianing images from both classes\ntrain_imgs = train_dogs[:2500] + train_cats[:2500]\nrandom.shuffle(train_imgs)  # randomly shuffle the images\n\ndel train_dogs\ndel train_cats\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# view some sample images\nimport matplotlib.image as mpimg\n\nfor image in train_imgs[:10]:\n    img = mpimg.imread(image)\n    plt.figure()\n    plt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Resizing the images","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def process_images(list_of_images):\n    x = []  # holds images\n    y = []  # hold labels\n    \n    for image in list_of_images:\n        x.append(\n            cv2.resize(\n                cv2.imread(image, cv2.IMREAD_COLOR),\n                (height, width),\n                interpolation=cv2.INTER_CUBIC\n            )\n        )\n        \n        if 'dog' in image:\n            y.append(1)\n        if 'cat' in image:\n            y.append(0)\n    \n    return x, y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"height = 200\nwidth = 200\nplanes = 3  # for color image\n\nX, y = process_images(train_imgs)\n\ndel train_imgs\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = np.asarray(X)\ny = np.asarray(y)\n\nimport seaborn as sns\nsns.countplot(y)\nplt.title('Labels')\n\nprint(\"Train image array shape:\", X.shape)\nprint(\"Label array shape:\", y.shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Train test splitting","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, Y_train, Y_val = train_test_split(X, y, test_size=0.2, random_state=2)\n\ndel X\ndel y\ngc.collect()\n\nnum_of_train_imgs = len(X_train)\nnum_of_val_imgs = len(X_val)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Create the model","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dropout, Dense\nfrom keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img\nfrom tensorflow.keras.optimizers import RMSprop","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32, (3,3), activation='relu', input_shape=(200, 200, 3)))\nmodel.add(MaxPooling2D((2,2)))\n\nmodel.add(Conv2D(64, (3,3), activation='relu'))\nmodel.add(MaxPooling2D((2,2)))\n\nmodel.add(Conv2D(128, (3,3), activation='relu'))\nmodel.add(MaxPooling2D((2,2)))\n\nmodel.add(Conv2D(128, (3,3), activation='relu'))\nmodel.add(MaxPooling2D((2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dropout(0.3))\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dense(1, activation='sigmoid'))\n\nmodel.compile(loss='binary_crossentropy', optimizer=RMSprop(lr=0.001), metrics=['acc'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# image generation\n\ntrain_image_gen = ImageDataGenerator(rescale=1.0/255,\n                                    rotation_range=50,\n                                    width_shift_range=0.2,\n                                    height_shift_range=0.2,\n                                    shear_range=0.2,\n                                    zoom_range=0.2,\n                                    horizontal_flip=True,\n                                    vertical_flip=True)\n\nval_image_gen= ImageDataGenerator(rescale=1.0/255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_size = 32\n\ntrain_image_generator = train_image_gen.flow(X_train, Y_train, batch_size=batch_size)\nval_image_generator = val_image_gen.flow(X_val, Y_val, batch_size=batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit_generator(train_image_generator,\n                     steps_per_epoch=num_of_train_imgs // batch_size,\n                     epochs=50,\n                     validation_data=val_image_generator,\n                     validation_steps=num_of_val_imgs // batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_loss = history.history['loss']\nval_loss = history.history['val_loss']\n\ntrain_acc = history.history['acc']\nval_acc = history.history['val_acc']\n\nepochs = range(1, len(train_acc) + 1)\n\n# train and val acc\nplt.plot(epochs, train_acc, 'b', label='Training acc')\nplt.plot(epochs, val_acc, 'r', label='Val acc')\nplt.title('Training and Validation accuracy')\nplt.legend()\n\n# train and val loss\nplt.figure()\nplt.plot(epochs, train_loss, 'b', label='Training loss')\nplt.plot(epochs, val_loss, 'r', label='Val loss')\nplt.title('Training and Validation loss')\nplt.legend()\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del X_train\ndel Y_train\ndel X_val\ndel Y_val\n\ngc.collect()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# prediction","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# print(test_imgs[0][30:-4])\nids = [a[30:-4] for a in test_imgs]\nprint(ids[:10])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_test, y_test = process_images(test_imgs)\nX = np.asarray(x_test)\n\ndel x_test\ngc.collect()\n\ntest_datagen = ImageDataGenerator(rescale=1.0/255)\ntest_generator = test_datagen.flow(X, batch_size=1, shuffle=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = model.predict_generator(test_generator, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = predictions.flatten()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"res = []\nfor i in predictions:\n    if i >=0.5:\n        res.append(1)\n    else:\n        res.append(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub_df = pd.DataFrame({\"id\": ids, \"label\": predictions})\nsub_df.to_csv(\"dogs_cats_predict.csv\", index = False)\nsub_df.head()","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}