{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom zipfile import ZipFile\nimport matplotlib.pyplot as plt\nfrom matplotlib.image import imread\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout, Activation\nfrom keras.layers.normalization import BatchNormalization\nfrom keras.optimizers import SGD\nfrom keras.preprocessing.image import load_img, img_to_array, ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\n\nfor 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/train.zip'\nTEST_ZIP = '../input/dogs-vs-cats/test1.zip'\nTRAIN_DIR = 'train/'\nTEST_DIR = 'test1/'\nIMG_SIZE = (100, 100)\nEPOCHS = 50\nBATCH_SIZE = 32","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"for zipdir in (TRAIN_ZIP, TEST_ZIP):\n    with ZipFile(zipdir, 'r') as zipObj:\n       zipObj.extractall()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(f'Train examples: {len(os.listdir(TRAIN_DIR))}')\nprint(f'Test examples: {len(os.listdir(TEST_DIR))}')\n\nprint(os.listdir(TRAIN_DIR)[:5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot first few train images\nfor i in range(9):\n    plt.subplot(330 + 1 + i)\n    filename = TRAIN_DIR + f'dog.{i}.jpg'\n    image = imread(filename)\n    plt.imshow(image)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# x - photos\n# y - labels; 1 stands for dogs, 0 for cats\nphotos, labels = list(), list()\nfor filename in os.listdir(TRAIN_DIR):\n    # Determine class\n    label = 1.0\n    if 'cat' in filename:\n        label = 0.0\n    # Load image\n    image = load_img(TRAIN_DIR + filename, target_size=IMG_SIZE)\n    # Convert to numpy array\n    image = img_to_array(image) / 255.0\n    # Store\n    photos.append(image)\n    labels.append(label)\n\nphotos = np.asarray(photos)\nlabels = np.asarray(labels)\n\nprint(photos.shape, labels.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(photos, labels, test_size=0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\n\n# 100x100 input\nmodel.add(Conv2D(32, (3, 3), kernel_initializer='he_uniform', padding='same', input_shape=x_train.shape[1:]))\nmodel.add(Activation('relu'))\nmodel.add(BatchNormalization())\n# 100x100\nmodel.add(Conv2D(32, (3, 3), kernel_initializer='he_uniform', padding='same'))\nmodel.add(Activation('relu'))\nmodel.add(BatchNormalization())\n# 50x50\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n# 50x50\nmodel.add(Conv2D(64, (3, 3), kernel_initializer='he_uniform', padding='same'))\nmodel.add(Activation('relu'))\nmodel.add(BatchNormalization())\n# 50x50\nmodel.add(Conv2D(64, (3, 3), kernel_initializer='he_uniform', padding='same'))\nmodel.add(Activation('relu'))\nmodel.add(BatchNormalization())\n# 25x25\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(1024, kernel_initializer='he_uniform'))\nmodel.add(Activation('relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(1024, kernel_initializer='he_uniform'))\nmodel.add(Activation('relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(1, activation='sigmoid'))\n\nsgd = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)\nmodel.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(\n    x_train, \n    y_train, \n    batch_size=BATCH_SIZE, \n    epochs=EPOCHS, \n    validation_data=[x_val, y_val])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Vizualize history\nfig, ax = plt.subplots(2,1)\n\nax[0].plot(history.history['loss'], color='b', label=\"Training loss\")\nax[0].plot(history.history['val_loss'], color='r', label=\"Validation loss\")\nlegend = ax[0].legend(loc='best', shadow=True)\n\nax[1].plot(history.history['accuracy'], color='b', label=\"Training accuracy\")\nax[1].plot(history.history['val_accuracy'], color='r', label=\"Validation accuracy\")\nlegend = ax[1].legend(loc='best', shadow=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen = ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # apply ZCA whitening\n        rotation_range=25,  # randomly rotate images in the range (degrees, 0 to 180)\n        zoom_range = 0.2, # Randomly zoom image\n        width_shift_range=0.2,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.2,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=True,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\n\ndatagen.fit(x_train)\n\nhistory = model.fit_generator(\n    datagen.flow(x_train, y_train, batch_size=BATCH_SIZE),\n    epochs=EPOCHS,\n    validation_data=(x_val, y_val),\n    verbose=1,\n    steps_per_epoch=x_train.shape[0] // BATCH_SIZE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Vizualize history with Data Augmentation\nfig, ax = plt.subplots(2,1)\n\nax[0].plot(history.history['loss'], color='b', label=\"Training loss\")\nax[0].plot(history.history['val_loss'], color='r', label=\"Validation loss\")\nlegend = ax[0].legend(loc='best', shadow=True)\n\nax[1].plot(history.history['accuracy'], color='b', label=\"Training accuracy\")\nax[1].plot(history.history['val_accuracy'], color='r', label=\"Validation accuracy\")\nlegend = ax[1].legend(loc='best', shadow=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Clear memory\ndel x_train, x_val, y_train, y_val, photos, labels, ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_data = list()\nfor i in range(7500):\n    # Load image\n    image = load_img(TEST_DIR + f'{i+1}.jpg', target_size=IMG_SIZE)\n    # Convert to numpy array\n    image = img_to_array(image) / 255.0\n    # Store\n    test_data.append(image)\n\ntest_data = np.asarray(test_data)\n\nprint(test_data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results = model.predict(test_data)\nresults = np.round(results)\n\nseries = pd.Series(results[i][0] for i in range(len(results)))\nseries = pd.Series(series, name='label')\n\nsubmission = pd.concat([pd.Series(range(1, len(results) + 1), name = \"id\"), series], axis = 1)\n\nsubmission.to_csv(\"submission.csv\", index=False)","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}