{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Initialisation"},{"metadata":{"trusted":true},"cell_type":"code","source":"print('V1')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport shutil\nfrom tensorflow import keras\nimport cv2\nimport random\nimport numpy as np\n#import seaborn as sns\n#%matplotlib inline \n#import matplotlib.pyplot as plt\n#from matplotlib import ticker\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.applications.resnet50 import preprocess_input\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport pandas as pd\n\n\n\n# Global variables\nNUM_CLASSES = 2\nTRAIN_DIR_ORIGIN = '../input/train'\nTRAIN_DIR = './train_data'\nTEST_DIR_ORIGIN = '../input/test'\nTEST_DIR = './test_data'\nCHANNELS = 3\n\n# Global hyperparameters\nIMG_WIDTH = 224\nIMG_HEIGHT = 224\nNUM_EPOCHS = 5\nSTEPS_PER_EPOCH_TRAINING = 25\nSTEPS_PER_EPOCH_VALIDATION = 125\nRESNET50_POOLING = 'avg'\nDENSE_LAYER_ACTIVATION = 'softmax'\nMODEL_OPTIMIZER = 'sgd'\nLOSS_FCT = 'categorical_crossentropy'\nMETRICS = ['accuracy']\nBATCH_SIZE_TRAINING = 100\nBATCH_SIZE_VALIDATION = 100\nBATCH_SIZE_TESTING = 1 # More would make the submission needing to be formatted\nVALIDATION_SPLIT = 0.25\nDROP_IMAGES = [dog.11731.jpg,\ndog.4334.jpg,\ncat.4688.jpg,\ncat.11222.jpg,\ncat.1450.jpg,\ncat.2159.jpg,\ncat.3822.jpg,\ncat.4104.jpg,\ncat.5355.jpg,\ncat.7194.jpg,\ncat.7920.jpg,\ncat.9250.jpg,\ncat.9444.jpg,\ncat.9882.jpg,\ndog.11538.jpg,\ndog.11724.jpg,\ndog.8507.jpg,\ncat.2939.jpg,\ncat.3216.jpg,\ncat.4833.jpg,\ncat.7968.jpg,\ncat.8470.jpg,\ndog.10161.jpg,\ndog.10190.jpg,\ndog.11186.jpg,\ndog.1308.jpg,\ndog.1895.jpg,\ndog.9188.jpg,\ncat.5351.jpg,\ncat.5418.jpg,\ncat.9171.jpg,\ndog.10747.jpg,\ndog.2614.jpg,\ndog.4367.jpg,\ndog.8736.jpg,\ncat.7377.jpg,\ndog.12376.jpg,\ndog.1773.jpg,\ncat.10712.jpg,\ncat.11184.jpg,\ncat.7564.jpg,\ncat.8456.jpg,\ndog.10237.jpg,\ndog.1043.jpg,\ndog.1194.jpg,\ndog.5604.jpg,\ndog.9517.jpg,\ncat.11565.jpg,\ndog.10797.jpg,\ndog.2877.jpg,\ndog.8898.jpg]\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Preliminary: moving files around"},{"metadata":{"trusted":true},"cell_type":"code","source":"os.makedirs(os.path.join(TRAIN_DIR, 'dogs'), exist_ok=True)\nos.makedirs(os.path.join(TRAIN_DIR, 'cats'), exist_ok=True)\n\ntry:\n    os.listdir(TRAIN_DIR_ORIGIN)\nexcept FileNotFoundError:\n    train_dir_tmp = './tmp'\n    os.makedirs(train_dir_tmp, exist_ok=True)\n    shutil.unpack_archive(TRAIN_DIR_ORIGIN + '.zip', train_dir_tmp, 'zip')\n    train_dir_tmp = os.path.join(train_dir_tmp, os.path.basename(TRAIN_DIR_ORIGIN))\n    print('Unpacked train set')\nelse:\n    train_dir_tmp = TRAIN_DIR_ORIGIN\n    print('There was no need to unpack train set')\n    \nprint(\"Copying the training images...\")\nfor i, f in enumerate(os.listdir(train_dir_tmp)):\n    if 'cat' in f:\n        shutil.copy(os.path.join(train_dir_tmp, f), os.path.join(TRAIN_DIR, 'cats', f))\n    elif 'dog' in f:\n        shutil.copy(os.path.join(train_dir_tmp, f), os.path.join(TRAIN_DIR, 'dogs', f))\n    if (i + 1) % 1000 == 0:\n        print('Copied', i + 1, 'train images')\n\n\ndef pad(s):\n    return s.split('.')[0].zfill(5) + '.' + s.split('.')[1]\n        \nos.makedirs(TEST_DIR, exist_ok=True)\ntry:\n    os.listdir(TEST_DIR_ORIGIN)\nexcept FileNotFoundError:\n    shutil.unpack_archive(TEST_DIR_ORIGIN + '.zip', TEST_DIR, 'zip')\n    print('Unpacked test set')\n    test_img_dir = os.path.join(TEST_DIR, os.listdir(TEST_DIR)[0])\n    for i, f in enumerate(os.listdir(test_img_dir)):\n        os.rename(os.path.join(test_img_dir, f), os.path.join(test_img_dir, pad(f)))\n        if (i + 1) % 1000 == 0:\n            print('Renamed', i + 1, 'test images')\nelse:\n    print('There was no need to unpack test set')\n    for i, f in enumerate(os.listdir(TEST_DIR_ORIGIN)):\n        shutil.copy(os.path.join(TEST_DIR_ORIGIN, f), os.path.join(TEST_DIR, 'all', pad(f)))\n        if (i + 1) % 1000 == 0:\n            print('Copied', i + 1, 'test images')\n\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Reading input"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_image_gen = ImageDataGenerator(preprocessing_function=preprocess_input,\n                                     validation_split=VALIDATION_SPLIT)\n\ntrain_generator = train_image_gen.flow_from_directory(TRAIN_DIR,\n                                                      target_size=(IMG_WIDTH, IMG_HEIGHT),\n                                                      batch_size=BATCH_SIZE_TRAINING,\n                                                      seed=0,\n                                                      subset='training',\n                                                      class_mode='categorical',\n                                                      shuffle=True)\n\nval_generator = train_image_gen.flow_from_directory(TRAIN_DIR,\n                                                    target_size=(IMG_WIDTH, IMG_HEIGHT),\n                                                    batch_size=BATCH_SIZE_VALIDATION,\n                                                    seed=0,\n                                                    subset='validation',\n                                                    class_mode='categorical',\n                                                    shuffle=True)\n\ntest_image_gen = ImageDataGenerator(preprocessing_function=preprocess_input)\ntest_generator = test_image_gen.flow_from_directory(TEST_DIR,\n                                                    target_size=(IMG_WIDTH, IMG_HEIGHT),\n                                                    batch_size=BATCH_SIZE_TESTING,\n                                                    class_mode=None,\n                                                    seed=0,\n                                                    shuffle=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Creating model"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\nmodel.add(ResNet50(include_top=False, pooling=RESNET50_POOLING, weights='imagenet'))\nmodel.add(Dense(NUM_CLASSES, activation=DENSE_LAYER_ACTIVATION))\n\n# Indicate whether the first layer should be trained/changed or not.\nmodel.layers[0].trainable = False\n\nmodel.compile(optimizer=MODEL_OPTIMIZER,\n              loss=LOSS_FCT, \n              metrics=METRICS)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Train model"},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit_generator(train_generator,\n                    steps_per_epoch=STEPS_PER_EPOCH_TRAINING,\n                    epochs=NUM_EPOCHS,\n                    validation_data=val_generator,\n                    validation_steps=STEPS_PER_EPOCH_VALIDATION)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Save model"},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save_weights('model_weights.h5')\nmodel.save('model_keras.h5')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Make predictions"},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = model.predict_generator(test_generator, steps = len(test_generator), verbose = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_labs = list(map(lambda x: x[1] / (x[0] + x[1]), predictions))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"counter = range(1, len(test_generator) + 1)\nsolution = pd.DataFrame({\"id\": counter, \"label\": pred_labs})\nsolution.to_csv(\"submission.csv\", index = False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}