{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import os\nfrom os import listdir, makedirs\nimport os.path\nimport re\n# print(os.listdir(\"input\"))\n\n# organize dataset into a useful structure\nfrom shutil import copyfile\nfrom random import seed\nimport random\n\nimport keras\nimport tensorflow as tf\nfrom tensorflow.python.keras.callbacks import TensorBoard\n\nimport cv2\nimport time \nimport numpy as np\nfrom numpy import asarray\nfrom numpy import save\nimport pandas as pd\nimport datetime\nfrom random import shuffle\nfrom tqdm import tqdm\n\nfrom keras.models import Sequential, load_model\nfrom keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout, Activation\nfrom keras.optimizers import Adam, SGD, RMSprop\nfrom keras.callbacks import TensorBoard, EarlyStopping, ReduceLROnPlateau\nfrom keras.datasets import mnist\nfrom keras.utils import np_utils\nfrom keras.layers import BatchNormalization\nfrom keras import backend as K\nfrom keras.preprocessing.image import load_img\nfrom keras.preprocessing.image import img_to_array\n\nimport sys\nfrom matplotlib import pyplot\nfrom keras.utils import to_categorical\nfrom keras.applications.vgg16 import VGG16\nfrom keras.models import Model\nfrom keras.layers import Dense\nfrom keras.layers import Flatten\nfrom keras.optimizers import SGD\nfrom keras.preprocessing.image import ImageDataGenerator\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import metrics\nfrom sklearn.metrics import confusion_matrix\n\nimport matplotlib.pyplot as plt\nfrom matplotlib.image import imread\nimport seaborn as sns\n%config InlineBackend.figure_format = 'svg'\n\n# from keras import backend as K\n# K.set_image_dim_ordering('th')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# unzip the train and test file, use below command\n\n# !unzip ../input/dogs-vs-cats-redux-kernels-edition/train.zip -d train\n\n# !unzip ../input/dogs-vs-cats-redux-kernels-edition/test.zip -d test\n\n#then you can access data from output folder\n\n# ld=os.listdir('../working/train/train')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Examine some sample dog photos\n# define location of dataset\nfolder = '../working/train/train/'\n\n# plot first few images\nfor i in range(9):\n    # define subplot\n    plt.subplot(330 + 1 + i)\n    # define filename\n    filename = folder + 'dog.' + str(i) + '.jpg'\n    # load image pixels\n    image = imread(filename)\n    # plot raw pixel data\n    plt.imshow(image)\n\n    # show the figure\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Examine some sample cat photos\n# define location of dataset\nfolder = '../working/train/train/'\n\n# plot first few images\nfor i in range(9):\n    # define subplot\n    plt.subplot(330 + 1 + i)\n    # define filename\n    filename = folder + 'cat.' + str(i) + '.jpg'\n    # load image pixels\n    image = imread(filename)\n    # plot raw pixel data\n    plt.imshow(image)\n\n    # show the figure\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN_DIR = '../working/train/train/'\nTEST_DIR = '../working/test/test/'\n\nimg_width = 150\nimg_height = 150\n# TRAIN_DIR = 'train/'\n# TEST_DIR = 'test/'\ntrain_images_dogs_cats = [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR)] # use this for full dataset\ntest_images_dogs_cats = [TEST_DIR+i for i in os.listdir(TEST_DIR)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Sort image files\ndef atoi(text):\n    return int(text) if text.isdigit() else text\n\ndef natural_keys(text):\n    return [ atoi(c) for c in re.split('(\\d+)', text) ]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Sort\ntrain_images_dogs_cats.sort(key=natural_keys)\n# train_images_dogs_cats = train_images_dogs_cats[0:1300] + train_images_dogs_cats[12500:13800] \n\ntest_images_dogs_cats.sort(key=natural_keys)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Prep data\ndef prepare_data(list_of_images):\n    \"\"\"\n    Returns two arrays: \n        x is an array of resized images\n        y is an array of labels\n    \"\"\"\n    x = [] # images as arrays\n    y = [] # labels\n    \n    for image in list_of_images:\n        x.append(cv2.resize(cv2.imread(image), (img_width,img_height), interpolation=cv2.INTER_CUBIC))\n    \n    for i in list_of_images:\n        if 'dog' in i:\n            y.append(1)\n        elif 'cat' in i:\n            y.append(0)\n        #else:\n            #print('neither cat nor dog name present in images')\n            \n    return x, y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X, Y = prepare_data(train_images_dogs_cats)\nprint(K.image_data_format())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# First split the data in two sets, 80% for training, 20% for Val/Test)\nX_train, X_val, Y_train, Y_val = train_test_split(X,Y, test_size=0.2, random_state=1, shuffle=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"nb_train_samples = len(X_train)\nnb_validation_samples = len(X_val)\nbatch_size = 16","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Model 1: 3 layers with DR\n# Model definition\nmodel = Sequential()\n\nmodel.add(Conv2D(32, (3, 3), input_shape=(img_width, img_height, 3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(32, (3, 3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(64, (3, 3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(64))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1))\nmodel.add(Activation('sigmoid'))\n\nmodel.compile(loss='binary_crossentropy',\n              optimizer='rmsprop',\n              metrics=['accuracy'])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Model 2: 3 layers with DR and BN\n# Model definition\nmodel_2 = Sequential()\n\nmodel_2.add(Conv2D(32, (3, 3), input_shape=(img_width, img_height, 3)))\nmodel_2.add(Activation('relu'))\nmodel_2.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel_2.add(Conv2D(32, (3, 3)))\nmodel_2.add(Activation('relu'))\nmodel_2.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel_2.add(Conv2D(64, (3, 3)))\nmodel_2.add(Activation('relu'))\nmodel_2.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel_2.add(Flatten())\nmodel_2.add(Dense(64))\nmodel_2.add(BatchNormalization())\nmodel_2.add(Activation('relu'))\nmodel_2.add(Dropout(0.5))\nmodel_2.add(Dense(1))\nmodel_2.add(Activation('sigmoid'))\n\nmodel_2.compile(loss='binary_crossentropy',\n              optimizer='rmsprop',\n              metrics=['accuracy'])\n\nmodel_2.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Model 3: 4 layers with DR \nmodel_3 = Sequential()\n\nmodel_3.add(Conv2D(32, (3, 3), input_shape=(img_width, img_height, 3)))\nmodel_3.add(Activation('relu'))\nmodel_3.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel_3.add(Conv2D(32, (3, 3)))\nmodel_3.add(Activation('relu'))\nmodel_3.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel_3.add(Conv2D(64, (3, 3)))\nmodel_3.add(Activation('relu'))\nmodel_3.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel_3.add(Conv2D(64, (3, 3)))\nmodel_3.add(Activation('relu'))\nmodel_3.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel_3.add(Flatten())\nmodel_3.add(Dense(64))\nmodel_3.add(Activation('relu'))\nmodel_3.add(Dropout(0.5))\nmodel_3.add(Dense(1))\nmodel_3.add(Activation('sigmoid'))\n\nmodel_3.compile(loss='binary_crossentropy',\n              optimizer='rmsprop',\n              metrics=['accuracy'])\n\nmodel_3.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Model 4: 4 layers with DR and BN\nmodel_4 = Sequential()\n\nmodel_4.add(Conv2D(32, (3, 3), input_shape=(img_width, img_height, 3)))\nmodel_4.add(Activation('relu'))\nmodel_4.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel_4.add(Conv2D(32, (3, 3)))\nmodel_4.add(Activation('relu'))\nmodel_4.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel_4.add(Conv2D(64, (3, 3)))\nmodel_4.add(Activation('relu'))\nmodel_4.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel_4.add(Conv2D(64, (3, 3)))\nmodel_4.add(Activation('relu'))\nmodel_4.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel_4.add(Flatten())\nmodel_4.add(Dense(64))\nmodel_4.add(BatchNormalization())\nmodel_4.add(Activation('relu'))\nmodel_4.add(Dropout(0.5))\nmodel_4.add(Dense(1))\nmodel_4.add(Activation('sigmoid'))\n\nmodel_4.compile(loss='binary_crossentropy',\n              optimizer='rmsprop',\n              metrics=['accuracy'])\n\nmodel_4.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Augmentation\ntrain_datagen = ImageDataGenerator(\n    rescale=1. / 255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True)\n\nval_datagen = ImageDataGenerator(\n    rescale=1. / 255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Prepare generators\ntrain_generator = train_datagen.flow(np.array(X_train), Y_train, batch_size=batch_size)\nvalidation_generator = val_datagen.flow(np.array(X_val), Y_val, batch_size=batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Function for model prediction testing\ndef show_image_prediction(X, idx, model) :\n  image = X[idx].reshape(1,64,64,3)\n  image_class = classes[model.predict_classes(image).item()]\n  image = image.reshape((ROWS, COLS, CHANNELS))\n  plt.figure(figsize = (4,2))\n  plt.imshow(image)\n  plt.title(\"Test {} : I think this is {}\".format(idx, image_class))\n  plt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model 1 fitting"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Fit the model\n# model = model()\nstart_time = time.time()\nhistory = model.fit_generator(\n    train_generator, \n    steps_per_epoch=nb_train_samples // batch_size,\n    epochs=10,     # originally 30\n    validation_data=validation_generator,\n    validation_steps=nb_validation_samples // batch_size)\n# history = model.fit(X_train_norm, y_train_one_hot, validation_data=(X_val_norm, y_val_one_hot), \n#                     epochs=10, batch_size=64, verbose=2, shuffle=True)\nelapsed_time = time.time() - start_time\nprint('--------------------------')\nprint('Training time in seconds: ', round(elapsed_time,2))\nprint('--------------------------')\n\n# Saving models locally after fitting\nsave_dir = \"../working\"\nmodel_name = 'keras_cnn_model_1.h5'\nmodel_path = os.path.join(save_dir, model_name)\nmodel.save(model_path)\nprint('Saved trained model at %s ' % model_path)\nprint('--------------------------')\n\n# # Final evaluation of the model\n# # Loading saved model\n# mnist_model = load_model('results/keras_mnist_model_4.h5')\n# scores = mnist_model.evaluate(X_test, Y_test, verbose=0)\n\n# Final evaluation of the model\nscores = model.evaluate(validation_generator, verbose=0)\nprint('Testing scores:')\n# print(\"Baseline Error: %.2f%%\" % (100-scores[1]*100))\nprint(\"Accuracy score: %.4f\" % scores[1])\nprint(\"Loss: %.4f\" % scores[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot learning curves\npd.DataFrame(history.history).plot(figsize=(8, 5))\nplt.grid(True)\nplt.gca().set_ylim(0, 1) # set the vertical range to [0-1]\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image = X_train[0]\ntest_pred = model_1.predict_classes(image.reshape(1, 64, 64, 3))\n\nshow_images(X_train, Y_train, 0)\n\nprint(\"Our Model Prediction: {}\".format(test_pred))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test_lr, test_idx = X_test, test_idx\n\nfor i in np.random.randint(0, len(X_test_lr), 10) :\n  show_image_prediction(X_test_lr, i, model9)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Kaggle test data prep"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_filenames = os.listdir(\"../working/test/test\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nnb_samples = test_df.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Test generator\ntest_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"../working/test/test/\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=IMAGE_SIZE,\n    batch_size=batch_size,\n    shuffle=False\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict = model_1.predict_generator(test_generator, steps=np.ceil(nb_samples/batch_size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category'] = np.argmax(predict, axis=-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['category'] = test_df['category'].replace(label_map)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category'].value_counts().plot.bar()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission_CNN1.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model 2 fitting"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Fit the model\n# model = model()\nstart_time = time.time()\nhistory = model_2.fit_generator(\n    train_generator, \n    steps_per_epoch=nb_train_samples // batch_size,\n    epochs=10,     # originally 30\n    validation_data=validation_generator,\n    validation_steps=nb_validation_samples // batch_size)\n# history = model.fit(X_train_norm, y_train_one_hot, validation_data=(X_val_norm, y_val_one_hot), \n#                     epochs=10, batch_size=64, verbose=2, shuffle=True)\nelapsed_time = time.time() - start_time\nprint('--------------------------')\nprint('Training time in seconds: ', round(elapsed_time,2))\nprint('--------------------------')\n\n# Saving models locally after fitting\nsave_dir = \"../working\"\nmodel_name = 'keras_cnn_model_2.h5'\nmodel_path = os.path.join(save_dir, model_name)\nmodel.save(model_path)\nprint('Saved trained model at %s ' % model_path)\nprint('--------------------------')\n\n# # Final evaluation of the model\n# # Loading saved model\n# mnist_model = load_model('results/keras_mnist_model_4.h5')\n# scores = mnist_model.evaluate(X_test, Y_test, verbose=0)\n\n# Final evaluation of the model\nscores = model_2.evaluate(validation_generator, verbose=0)\nprint('Testing scores:')\n# print(\"Baseline Error: %.2f%%\" % (100-scores[1]*100))\nprint(\"Accuracy score: %.4f\" % scores[1])\nprint(\"Loss: %.4f\" % scores[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot learning curves\npd.DataFrame(history.history).plot(figsize=(8, 5))\nplt.grid(True)\nplt.gca().set_ylim(0, 1) # set the vertical range to [0-1]\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Kaggle data test prep"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_filenames = os.listdir(\"../working/test/test\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nnb_samples = test_df.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Test generator\ntest_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"../working/test/test/\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=IMAGE_SIZE,\n    batch_size=batch_size,\n    shuffle=False\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict = model_2.predict_generator(test_generator, steps=np.ceil(nb_samples/batch_size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category'] = np.argmax(predict, axis=-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['category'] = test_df['category'].replace(label_map)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category'].value_counts().plot.bar()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission_CNN2.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model 3 fitting"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Fit the model\n# model = model()\nstart_time = time.time()\nhistory = model_3.fit_generator(\n    train_generator, \n    steps_per_epoch=nb_train_samples // batch_size,\n    epochs=10,     # originally 30\n    validation_data=validation_generator,\n    validation_steps=nb_validation_samples // batch_size)\n# history = model.fit(X_train_norm, y_train_one_hot, validation_data=(X_val_norm, y_val_one_hot), \n#                     epochs=10, batch_size=64, verbose=2, shuffle=True)\nelapsed_time = time.time() - start_time\nprint('--------------------------')\nprint('Training time in seconds: ', round(elapsed_time,2))\nprint('--------------------------')\n\n# Saving models locally after fitting\nsave_dir = \"../working\"\nmodel_name = 'keras_cnn_model_3.h5'\nmodel_path = os.path.join(save_dir, model_name)\nmodel.save(model_path)\nprint('Saved trained model at %s ' % model_path)\nprint('--------------------------')\n\n# # Final evaluation of the model\n# # Loading saved model\n# mnist_model = load_model('results/keras_mnist_model_4.h5')\n# scores = mnist_model.evaluate(X_test, Y_test, verbose=0)\n\n# Final evaluation of the model\nscores = model_3.evaluate(validation_generator, verbose=0)\nprint('Testing scores:')\n# print(\"Baseline Error: %.2f%%\" % (100-scores[1]*100))\nprint(\"Accuracy score: %.4f\" % scores[1])\nprint(\"Loss: %.4f\" % scores[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot learning curves\npd.DataFrame(history.history).plot(figsize=(8, 5))\nplt.grid(True)\nplt.gca().set_ylim(0, 1) # set the vertical range to [0-1]\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Kaggle data test prep"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_filenames = os.listdir(\"../working/test/test\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nnb_samples = test_df.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Test generator\ntest_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"../working/test/test/\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=IMAGE_SIZE,\n    batch_size=batch_size,\n    shuffle=False\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict = model_3.predict_generator(test_generator, steps=np.ceil(nb_samples/batch_size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category'] = np.argmax(predict, axis=-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['category'] = test_df['category'].replace(label_map)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category'].value_counts().plot.bar()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission_CNN3.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model 4 fitting"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Fit the model\n# model = model()\nstart_time = time.time()\nhistory = model_4.fit_generator(\n    train_generator, \n    steps_per_epoch=nb_train_samples // batch_size,\n    epochs=10,     # originally 30\n    validation_data=validation_generator,\n    validation_steps=nb_validation_samples // batch_size)\n# history = model.fit(X_train_norm, y_train_one_hot, validation_data=(X_val_norm, y_val_one_hot), \n#                     epochs=10, batch_size=64, verbose=2, shuffle=True)\nelapsed_time = time.time() - start_time\nprint('--------------------------')\nprint('Training time in seconds: ', round(elapsed_time,2))\nprint('--------------------------')\n\n# Saving models locally after fitting\nsave_dir = \"../working\"\nmodel_name = 'keras_cnn_model_4.h5'\nmodel_path = os.path.join(save_dir, model_name)\nmodel.save(model_path)\nprint('Saved trained model at %s ' % model_path)\nprint('--------------------------')\n\n# # Final evaluation of the model\n# # Loading saved model\n# mnist_model = load_model('results/keras_mnist_model_4.h5')\n# scores = mnist_model.evaluate(X_test, Y_test, verbose=0)\n\n# Final evaluation of the model\nscores = model_4.evaluate(validation_generator, verbose=0)\nprint('Testing scores:')\n# print(\"Baseline Error: %.2f%%\" % (100-scores[1]*100))\nprint(\"Accuracy score: %.4f\" % scores[1])\nprint(\"Loss: %.4f\" % scores[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot learning curves\npd.DataFrame(history.history).plot(figsize=(8, 5))\nplt.grid(True)\nplt.gca().set_ylim(0, 1) # set the vertical range to [0-1]\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Kaggle data test prep"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_filenames = os.listdir(\"../working/test/test\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nnb_samples = test_df.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Test generator\ntest_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"../working/test/test/\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=IMAGE_SIZE,\n    batch_size=batch_size,\n    shuffle=False\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict = model_4.predict_generator(test_generator, steps=np.ceil(nb_samples/batch_size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category'] = np.argmax(predict, axis=-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['category'] = test_df['category'].replace(label_map)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category'] = test_df['category'].replace({ 'dog': 1, 'cat': 0 })","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df['category'].value_counts().plot.bar()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission_CNN4.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# JUNK"},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN_DIR = '../working/train/train/'\nTEST_DIR = '../working/test/test/'\n\nROWS = 64\nCOLS = 64\nCHANNELS = 3\n\ntrain_images = [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR)] # use this for full dataset\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_images =  [TEST_DIR+i for i in os.listdir(TEST_DIR)]\n\n\n# slice datasets for memory efficiency on Kaggle Kernels, delete if using full dataset\n# train_images = train_dogs[:1000] + train_cats[:1000]\n# random.shuffle(train_images)\n# test_images =  test_images[:25]\n\ndef read_image(file_path):\n    img = cv2.imread(file_path, cv2.IMREAD_COLOR) #cv2.IMREAD_GRAYSCALE\n    return cv2.resize(img, (ROWS, COLS), interpolation=cv2.INTER_CUBIC)\n\n\ndef prep_data(images):\n    count = len(images)\n    data = np.ndarray((count, CHANNELS, ROWS, COLS), dtype=np.uint8)\n\n    for i, image_file in enumerate(images):\n        image = read_image(image_file)\n        data[i] = image.T\n        if i%250 == 0: print('Processed {} of {}'.format(i, count))\n    \n    return data\n\ntrain = prep_data(train_images)\ntest = prep_data(test_images)\n\nprint(\"Train shape: {}\".format(train.shape))\nprint(\"Test shape: {}\".format(test.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Create labels\n# Dog: 1\n# Cat: 0\nlabels = []\nfor i in train_images:\n    if 'dog' in i:\n        labels.append(1)\n    else:\n        labels.append(0)\n\nsns.countplot(labels)\n# sns.title('Cats and Dogs')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Comparison of images\ndef show_cats_and_dogs(idx):\n    cat = read_image(train_cats[idx])\n    dog = read_image(train_dogs[idx])\n    pair = np.concatenate((cat, dog), axis=1)\n    plt.figure(figsize=(10,5))\n    plt.imshow(pair)\n    plt.show()\n    \nfor idx in range(0,5):\n    show_cats_and_dogs(idx)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"input_shape = (None, ROWS, COLS, 3)\n\n# [None, IMG_SIZE, IMG_SIZE, 1]\n# ROWS, COLS, 3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Model 1: 2-block VGG\n\n# opt = RMSprop(lr=1e-4)\n\n# model_1 = Sequential()\n\n# model_1.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', \n#                  input_shape=input_shape))\n# model_1.add(MaxPooling2D(pool_size=(2, 2)))\n# model_1.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))\n# model_1.add(MaxPooling2D(pool_size=(2, 2)))\n# model_1.add(Flatten())\n# model_1.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))\n# model_1.add(Dense(2, activation='sigmoid'))\n\n# # opt = SGD(lr=0.001, momentum=0.9)\n# # opt = Adam(lr=0.001)\n# model_1.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])\n# model_1.summary()\n\n\n# model.add(MaxPooling2D(poolsize=(2, 2), dim_ordering=\"th\"))\n\n# Model testing\nopt = RMSprop(lr=1e-4)\n# objective = 'binary_crossentropy'\n\nmodel_1 = Sequential()\n\n# convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')\n\nmodel_1.add(Conv2D(32, (3, 3), kernel_initializer='he_uniform', padding='same', input_shape=[None, 64, 64, 3], activation='relu'))\nmodel_1.add(Conv2D(32, (3, 3), kernel_initializer='he_uniform', padding='same', activation='relu'))\nmodel_1.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel_1.add(Conv2D(64, (3, 3), kernel_initializer='he_uniform', padding='same', activation='relu'))\nmodel_1.add(Conv2D(64, (3, 3), kernel_initializer='he_uniform', padding='same', activation='relu'))\nmodel_1.add(MaxPooling2D(pool_size=(2, 2)))\n    \nmodel_1.add(Conv2D(128, (3, 3), kernel_initializer='he_uniform', padding='same', activation='relu'))\nmodel_1.add(Conv2D(128, (3, 3), kernel_initializer='he_uniform', padding='same', activation='relu'))\nmodel_1.add(MaxPooling2D(pool_size=(2, 2)))\n    \nmodel_1.add(Conv2D(256, (3, 3), kernel_initializer='he_uniform', padding='same', activation='relu'))\nmodel_1.add(Conv2D(256, (3, 3), kernel_initializer='he_uniform', padding='same', activation='relu'))\n#     model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))\nmodel_1.add(MaxPooling2D(pool_size=(2, 2)))\n\n#     model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))\n#     model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))\n#     model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))\n#     model.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel_1.add(Flatten())\nmodel_1.add(Dense(256, activation='relu'))\nmodel_1.add(Dropout(0.5))\n    \nmodel_1.add(Dense(256, activation='relu'))\nmodel_1.add(Dropout(0.5))\n\nmodel_1.add(Dense(1))\nmodel_1.add(Activation('sigmoid'))\n\nmodel_1.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])\n\nmodel_1.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model 1 fitting"},{"metadata":{"trusted":true},"cell_type":"code","source":"input_shape = (16, CHANNELS, ROWS, COLS)\n# (batch, channels, rows, cols))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# epochs = 10\nbatch_size = 16\n\nstart_time = time.time()\nhistory = model_1.fit(train, labels, batch_size=16, epochs=10,\n              validation_split=0.25, verbose=0, shuffle=True, callbacks=[early_stopping])\n\nelapsed_time = time.time() - start_time\nprint('--------------------------')\nprint('Training time in seconds: ', round(elapsed_time,2))\nprint('--------------------------')\n\n# Saving models locally after fitting\nsave_dir = \"../working/\"\nmodel_name = 'keras_cnn_1.h5'\nmodel_path = os.path.join(save_dir, model_name)\nmodel_1.save(model_path)\nprint('Saved trained model at %s ' % model_path)\nprint('--------------------------')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"epochs=3 if FAST_RUN else 10\n\nstart_time = time.time()\nhistory = model_1.fit_generator(\n    train_generator, \n    epochs=epochs,\n    validation_data=validation_generator,\n#     validation_steps=total_validate//batch_size,\n#     steps_per_epoch=total_train//batch_size,\n    callbacks=callbacks)\n\nhistory = model.fit_generator(train_it, steps_per_epoch=len(train_generator),\n\tvalidation_data=test_it, validation_steps=len(t), epochs=20, verbose=0)\n\nelapsed_time = time.time() - start_time\nprint('--------------------------')\nprint('Training time in seconds: ', round(elapsed_time,2))\nprint('--------------------------')\n\n# Saving models locally after fitting\nsave_dir = \"../working/\"\nmodel_name = 'keras_cnn_1_adam.h5'\nmodel_path = os.path.join(save_dir, model_name)\nmodel_1.save(model_path)\nprint('Saved trained model at %s ' % model_path)\nprint('--------------------------')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"","_uuid":"","trusted":true},"cell_type":"code","source":"import pandas as pd\nsample_submission = pd.read_csv(\"../input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv\")","execution_count":0,"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":1}