{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import zipfile\n\nzip_files = ['test', 'train']\n\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"../input/dogs-vs-cats-redux-kernels-edition/{}.zip\".format(zip_file),\"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport shutil\nimport random\nfrom tqdm import tqdm\n\nimport numpy as np\nimport pandas as pd\n\nimport PIL\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"TRAIN_DIR = '../working/train'\nTEST_DIR = '../working/test'\n\n\nprint('{} Train Images'.format(len(os.listdir(TRAIN_DIR))))\nprint('{} Test Images'.format(len(os.listdir(TEST_DIR))))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = PIL.Image.open(os.path.join(TRAIN_DIR, os.listdir(TRAIN_DIR)[0]))\n\nwidth, height = img.size\n\nplt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def move_imgs(img_dir, new_dir):\n    \n    if not os.path.exists(new_dir):\n        os.mkdir(new_dir)\n    \n    for img_path in tqdm(img_dir):\n        img_name = img_path.split('/')[-1]\n        shutil.move(img_path, os.path.join(new_dir,img_name))\n\n\ndef plot_imgs(paths):\n    plt.figure(figsize=(15, 8))\n    for i in range(50):\n        plt.subplot(5, 10, i+1, xticks=[], yticks=[])\n        img = PIL.Image.open(paths[i], 'r')\n        plt.imshow(np.asarray(img))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ndog_imgs = [os.path.join(TRAIN_DIR, i) for i in os.listdir(TRAIN_DIR) if i.split('.')[0] == 'dog']\ncat_imgs = [os.path.join(TRAIN_DIR, i) for i in os.listdir(TRAIN_DIR) if i.split('.')[0] == 'cat']\n\n\nmove_imgs(dog_imgs, os.path.join(TRAIN_DIR, 'dog'))\nmove_imgs(cat_imgs, os.path.join(TRAIN_DIR, 'cat'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator \n\nBATCH_SIZE = 128\n\ntrain_generator = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=90,\n    width_shift_range=0.3,\n    height_shift_range=0.3,\n    horizontal_flip=True,\n    vertical_flip=True,\n    validation_split=0.15\n)\n\ntest_generator = ImageDataGenerator(\n    rescale=1./255\n)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"traingen = train_generator.flow_from_directory('../working/train',\n                                              target_size=(224, 224),\n                                              class_mode='binary',\n                                              subset='training',\n                                              batch_size=BATCH_SIZE,\n                                              shuffle=True, seed=69)\n\nvalidgen = train_generator.flow_from_directory('../working/train',\n                                              target_size=(224, 224),\n                                              class_mode='binary',\n                                              subset='validation',\n                                              batch_size=BATCH_SIZE,\n                                              shuffle=True, seed=69)\n\nnp.save('class_indices', traingen.class_indices)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\n\ngpus = tf.config.experimental.list_physical_devices('GPU')\nif gpus:\n# Restrict TensorFlow to only use the first GPU\n    try:\n        tf.config.experimental.set_visible_devices(gpus[0], 'GPU')\n        logical_gpus = tf.config.experimental.list_logical_devices('GPU')\n        print(len(gpus), \"Physical GPUs,\", len(logical_gpus), \"Logical GPU\")    \n    except RuntimeError as e:\n        # Visible devices must be set before GPUs have been initialized\n        print(e)\n\ntf.config.set_soft_device_placement(True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import itertools\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix\n\nfrom keras.models import Model\nfrom keras.layers import Dense, Dropout, GlobalAveragePooling2D\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom keras.optimizers import Adam,  Adagrad\n\n\nfrom keras.applications.inception_v3 import InceptionV3\nfrom sklearn.metrics import accuracy_score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def compile_model(input_shape, n_classes, optimizer, fine_tune=None):\n    \n    conv_base = InceptionV3(weights='imagenet', include_top=False)\n    \n    if type(fine_tune) is int:\n        for layer in conv_base.layers[:-fine_tune]:\n            layer.trainable=False\n    else:\n        for layer in conv_base.layers:\n            layer.trainable=False\n    \n    \n    top_model = conv_base.output\n    top_model = GlobalAveragePooling2D()(top_model)\n    top_model = Dense(2048, activation='relu')(top_model)\n    top_model = Dense(1072, activation='relu')(top_model)\n    top_model = Dropout(0.3)(top_model)\n    \n    predictions = Dense(n_classes, activation='sigmoid')(top_model)\n    \n    model = Model(inputs=conv_base.input, outputs=predictions)\n    \n   \n    model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])\n    \n    return model\n\ndef plot_history(history):\n       \n    acc = history.history['accuracy']\n    val_acc = history.history['val_accuracy']\n    loss = history.history['loss']\n    val_loss = history.history['val_loss']\n    \n    plt.figure(figsize=(10, 5))\n    plt.subplot(1, 2, 1)\n    plt.plot(acc)\n    plt.plot(val_acc)\n    plt.ylabel('accuracy')\n    plt.xlabel('epoch')\n    plt.legend(['train', 'val'], loc='upper left')\n    \n    plt.subplot(1, 2, 2)\n    plt.plot(loss)\n    plt.plot(val_loss)\n    plt.ylabel('loss')\n    plt.xlabel('epoch')\n    plt.legend(['train', 'val'], loc='upper left')\n    \n    plt.show();\n\ndef plot_confusion_matrix(cm, classes, title='Confusion matrix', cmap=plt.cm.Blues):\n    \"\"\"\n    This function prints and plots the confusion matrix.\n    Source: https://deeplizard.com/learn/video/km7pxKy4UHU\n    \"\"\"\n    plt.figure(figsize=(10,8))\n    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=85)\n    plt.yticks(tick_marks, classes)\n\n    thresh = cm.max() / 2.\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n        plt.text(j, i, cm[i, j],\n            horizontalalignment=\"center\",\n            color=\"white\" if cm[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True Label')\n    plt.xlabel('Predicted Label')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"input_shape = (224, 224, 3)\noptim = Adam(lr=0.001)\n\nn_steps = traingen.samples//BATCH_SIZE\nn_val_steps = validgen.samples//BATCH_SIZE\nn_epochs = 100","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"inception = compile_model(input_shape, 1, optim, fine_tune=None)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"inception.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# model callbacks\ncheckpoint = ModelCheckpoint(filepath='../working/model.weights.best.hdf5',\n                        monitor='val_binary_crossentropy',\n                        save_best_only=True,\n                        verbose=1)\n\nearly_stop = EarlyStopping(monitor='val_binary_crossentropy',\n                           patience=10,\n                           restore_best_weights=True,\n                           mode='min')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = inception.fit(traingen,\n                    batch_size=BATCH_SIZE,\n                    epochs=5,\n                    validation_data=validgen,\n                    steps_per_epoch=n_steps, validation_steps=n_val_steps,\n                    callbacks=[checkpoint, early_stop],\n                    verbose=1\n                   )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_history(history)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#model.load_weights('../working/model.weights.best.hdf5')\n\n\nif not os.path.exists('../working/test/dog'):\n    os.mkdir('../working/test/dog')\n    \nfor i in os.listdir('../working/test'):\n    if i.endswith('jpg'):\n        shutil.move(os.path.join('../working/test', i), os.path.join('../working/test/dog',i))\n\n#os.mkdir('../working/test/cat')\n    \ntestgen = test_generator.flow_from_directory('../working/test',\n                                              target_size=(224, 224),\n                                              class_mode=None,\n                                              batch_size=BATCH_SIZE)\n\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = inception.predict(testgen, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# CSV deliverable\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"counter = range(1, len(predictions) + 1)\nsolution = pd.DataFrame({\"id\": counter, \"label\":list(predictions)})\ncols = ['label']\n\nfor col in cols:\n    solution[col] = solution[col].map(lambda x: str(x).lstrip('[').rstrip(']')).astype(float)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"solution.to_csv(\"dogs_cats_inception.csv\", index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}