{"cells":[{"metadata":{},"cell_type":"markdown","source":"## *Import libraries*"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport zipfile\nimport numpy as np\nimport pandas as pd\nimport random\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img\nfrom keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\n\n\nfrom keras.applications.vgg16 import VGG16\nfrom keras.preprocessing import image\nfrom keras.applications.vgg16 import preprocess_input\n\nfrom keras.layers import Input, Flatten, Dense\nfrom keras.models import Model\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## *Exploratory Data Analysis*"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Define some parameters and constants\nFOLDER_PATH = \"../input/dogs-vs-cats/train/train/\"\nIMAGE_SIZE = (224, 224)\nEPOCHS = 5","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Create DataFrame to analyze dataset\nfilenames = os.listdir(FOLDER_PATH)\nlabels = []\nfor name in filenames:\n    label = name.split('.')[0]\n    if label == 'dog':\n        labels.append(1)\n    else:\n        labels.append(0)\n\ndf = pd.DataFrame({\n    'filename': filenames,\n    'label': labels\n})\n\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Detect class balance for dataset\ndf['label'].value_counts().plot.bar()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# See sample images\nsample_train = df.head(12)\nsample_train.head()\nplt.figure(figsize=(12, 24))\nfor index, row in sample_train.iterrows():\n    filename = row['filename']\n    category = row['label']\n    img = load_img(FOLDER_PATH+filename, target_size=IMAGE_SIZE)\n    plt.subplot(6, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename + '(' + \"{}\".format(category) + ')' )\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## *Build classifier model (VGG + TL from ImageNet)*"},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.applications import VGG16\n# Load the VGG model with trained on ImageNet\nvgg_imagenet = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n\n# Freeze the layers except last 4 layers\nfor layer in vgg_imagenet.layers[:-4]:\n    layer.trainable = False\n\n# Show a summary of the model. Check the number of trainable parameters\nvgg_imagenet.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras import models\nfrom keras import layers\nfrom keras import optimizers\n\n# Create new model (VGG ImageNet (CNN) + fully-connected layers)\nmodel = models.Sequential()\n \n# Add the VGG ImageNet model\nmodel.add(vgg_imagenet)\n \n# Add new FC layers\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(128, activation='relu', kernel_initializer='he_uniform'))\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(2, activation='softmax'))\n \n# Show a summary of the model. Check the number of trainable parameters\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## *Create data generators*"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Split dataset to validation and train\ndf['label'] = df['label'].replace({0:'cat', 1:'dog'})\n\ntrain_df, validate_df = train_test_split(df, test_size=0.20, random_state=42)\ntrain_df = train_df.reset_index(drop=True)\nvalidate_df = validate_df.reset_index(drop=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Check train dataset\ntrain_df.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Check validation dataset\nvalidate_df.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n      rescale=1./255,\n      rotation_range=20,\n      width_shift_range=0.2,\n      height_shift_range=0.2,\n      horizontal_flip=True,\n      fill_mode='nearest')\n \nvalidate_datagen = ImageDataGenerator(rescale=1./255)\n \n# Define the batchsize for train and validation datasets\ntrain_batchsize = 50\nvalidate_batchsize = 50\n\n\n# Create data generators\ntrain_generator = train_datagen.flow_from_dataframe(train_df, \n                                                    FOLDER_PATH, \n                                                    x_col = 'filename', \n                                                    y_col = 'label', \n                                                    batch_size=train_batchsize, \n                                                    class_mode='categorical', \n                                                    target_size=IMAGE_SIZE)\n\nvalidate_generator = validate_datagen.flow_from_dataframe(validate_df, \n                                                          FOLDER_PATH, \n                                                          x_col = 'filename', \n                                                          y_col = 'label', \n                                                          batch_size=validate_batchsize, \n                                                          class_mode='categorical', \n                                                          target_size=IMAGE_SIZE)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## *Train model*"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Compile the model\nmodel.compile(loss='categorical_crossentropy',\n              optimizer=optimizers.RMSprop(lr=1e-4),\n              metrics=['accuracy'])\n\n# Train the model\nhistory = model.fit_generator(\n      train_generator,\n      steps_per_epoch=train_generator.samples/train_generator.batch_size,\n      epochs=EPOCHS,\n      validation_data=validate_generator,\n      validation_steps=validate_generator.samples/validate_generator.batch_size,\n      verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save_weights(\"vgg16_weights.h5\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save('vgg16.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_json = model.to_json()\nwith open(\"vgg16.json\", \"w\") as json_file:\n    json.dump(model_json, json_file)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Let us see the loss and accuracy curves\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n \nepochs = range(len(acc))\n \nplt.plot(epochs, acc, 'b', label='Training acc')\nplt.plot(epochs, val_acc, 'r', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\n \nplt.figure()\n \nplt.plot(epochs, loss, 'b', label='Training loss')\nplt.plot(epochs, val_loss, 'r', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n \nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## *Submission* "},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.read_csv(\"../input/dogs-vs-cats/sampleSubmission.csv\")\n\nprint(submission[\"label\"][0])\npd.options.mode.chained_assignment = None  # default='warn'\n\nfor e,i in enumerate(os.listdir(\"../input/dogs-vs-cats/test1/test1\")):\n    print(i)\n    output=[]\n    img = image.load_img(os.path.join(\"../input/dogs-vs-cats/test1/test1\",i), target_size=IMAGE_SIZE)\n    img = np.asarray(img)\n    img = np.expand_dims(img, axis=0)\n    output = model.predict(img)\n    if output[0][0] > output[0][1]:\n#         print(\"cat\")\n        submission[\"id\"][e]=i\n        submission[\"label\"][e]=\"cat\"\n    else:\n#         print('dog')\n        submission[\"id\"][e]=i\n        submission[\"label\"][e]=\"dog\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv(\"my_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":1}