{"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 the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true,"scrolled":true},"cell_type":"code","source":"import sys\nimport os\nfrom keras.layers import *\nfrom keras.optimizers import *\nfrom keras.applications import *\nfrom keras.models import Model, Sequential, load_model\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom keras import backend as k","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bb46387823f858f88dcce879fc6bc037c8685850"},"cell_type":"code","source":"# fix seed for reproducible results (only works on CPU, not GPU)\nseed = 9\nnp.random.seed(seed=seed)\ntf.set_random_seed(seed=seed)\n\n# hyper parameters for model\nnb_classes = 6  # number of classes\nbased_model_last_block_layer_number = 86  # value is based on based model selected.\nimg_width, img_height = 150, 150  # change based on the shape/structure of your images\nbatch_size = 128  # try 4, 8, 16, 32, 64, 128, 256 dependent on CPU/GPU memory capacity (powers of 2 values).\nnb_epoch = 50  # number of iteration the algorithm gets trained.\nlearn_rate = 1e-4  # sgd learning rate\nmomentum = .9  # sgd momentum to avoid local minimum\ntransformation_ratio = .2  # how aggressive will be the data augmentation/transformation","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03224a186d9127b58c0e18190b72c1bc2b27741e","scrolled":false},"cell_type":"code","source":"data_dir = '../working'\ntrain_data_dir = os.path.abspath('../input/seg_train/seg_train')  # Inside, each class should have it's own folder\nvalidation_data_dir = os.path.abspath('../input/seg_test/seg_test')  # each class should have it's own folder\nmodel_path = '../working'\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"37fa39a9cc2b5bbc259db3251e5da9611cc1855f","scrolled":false},"cell_type":"code","source":"# Read Data and Augment it: Make sure to select augmentations that are appropriate to your images.\n# To save augmentations un-comment save lines and add to your flow parameters.\ntrain_datagen = ImageDataGenerator(rescale=1. / 255,\n                                    shear_range=transformation_ratio,\n                                    zoom_range=transformation_ratio,\n                                   rotation_range=20,\n                                    width_shift_range=transformation_ratio,\n                                    height_shift_range=transformation_ratio,\n                                   cval=transformation_ratio,\n                                   horizontal_flip=True,\n                                   vertical_flip=True)\n\nvalidation_datagen = ImageDataGenerator(rescale=1. / 255)\n\ntrain_generator = train_datagen.flow_from_directory(train_data_dir,\n                                                    target_size=(img_width, img_height),\n                                                    batch_size=batch_size,\n                                                    class_mode='categorical')\nlabels = (train_generator.class_indices)\nprint(labels)\n\n# save_to_dir=os.path.join(os.path.abspath(train_data_dir), '../preview')\n# save_prefix='aug',\n# save_format='jpeg')\n# use the above 3 commented lines if you want to save and look at how the data augmentations look like\n\nvalidation_generator = validation_datagen.flow_from_directory(validation_data_dir,\n                                                              target_size=(img_width, img_height),\n                                                              batch_size=batch_size,\n                                                              class_mode='categorical')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"62872e8964f43b94fe6ca6c2d403b7267e0d4c7e"},"cell_type":"code","source":"# create the base pre-trained model\nbase_model = InceptionV3(weights='imagenet', include_top=False)\n\n# add a global spatial average pooling layer\nx = base_model.output\nx = GlobalAveragePooling2D()(x)\n# let's add a fully-connected layer\nx = Dense(1024, activation='relu')(x)\n# and a logistic layer -- let's say we have 200 classes\npredictions = Dense(nb_classes, activation='softmax')(x)\n\n# this is the model we will train\nmodel = Model(inputs=base_model.input, outputs=predictions)\n\n# first: train only the top layers (which were randomly initialized)\n# i.e. freeze all convolutional InceptionV3 layers\nfor layer in base_model.layers:\n    layer.trainable = False\n\n# compile the model (should be done *after* setting layers to non-trainable)\nmodel.compile(optimizer='rmsprop', loss='categorical_crossentropy',metrics=['accuracy'])\n\ntop_weights_path = os.path.join(os.path.abspath(model_path), 'top_model_weights.h5')\ncallbacks_list = [\n    ModelCheckpoint(top_weights_path, monitor='val_acc', verbose=1, save_best_only=True),\n    EarlyStopping(monitor='val_acc', patience=5, verbose=0)\n]\n\n# Train Simple CNN\nhistory = model.fit_generator(train_generator,\n                    steps_per_epoch=train_generator.samples // batch_size,\n                    epochs=20,\n                    validation_data=validation_generator,\n                    validation_steps=validation_generator.samples // batch_size,\n                    callbacks=callbacks_list)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3317c2b6dc2bb46eecb6dbaa266e22bf3becf1ea"},"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nprint(history.history.keys())\n# summarize history for accuracy\nplt.plot(history.history['acc'])\nplt.plot(history.history['val_acc'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()\n# summarize history for loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d5af5216895f5cf5a2f100a7c46f145d6dbc4af7","scrolled":false},"cell_type":"code","source":"# at this point, the top layers are well trained and we can start fine-tuning\n# convolutional layers from inception V3. We will freeze the bottom N layers\n# and train the remaining top layers.\n\n# let's visualize layer names and layer indices to see how many layers\n# we should freeze:\n# for i, layer in enumerate(base_model.layers):\n#    print(i, layer.name)\n\n# we chose to train the top 2 inception blocks, i.e. we will freeze\n# the first 249 layers and unfreeze the rest:\nfor layer in model.layers[:249]:\n   layer.trainable = False\nfor layer in model.layers[249:]:\n   layer.trainable = True\n\n# we need to recompile the model for these modifications to take effect\n# we use SGD with a low learning rate\nfrom keras.optimizers import SGD\nmodel.compile(optimizer=SGD(lr=0.0001, momentum=0.9), \n              loss='categorical_crossentropy', \n              metrics=['accuracy'])\n\ntop_weights_path = os.path.join(os.path.abspath(model_path), 'model_weights.h5')\ncallbacks_list = [\n    ModelCheckpoint(top_weights_path, monitor='val_acc', verbose=1, save_best_only=True),\n    EarlyStopping(monitor='val_acc', patience=10, verbose=0)\n]\n\n# Train Simple CNN\nhistory = model.fit_generator(train_generator,\n                    steps_per_epoch=train_generator.samples // batch_size,\n                    epochs=50,\n                    validation_data=validation_generator,\n                    validation_steps=validation_generator.samples // batch_size,\n                    callbacks=callbacks_list)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"506d02379ae68fbae4a7dd6c0657e155e0420332"},"cell_type":"code","source":"\n\nprint(history.history.keys())\n# summarize history for accuracy\nplt.plot(history.history['acc'])\nplt.plot(history.history['val_acc'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()\n# summarize history for loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"32a6305b3cb1fe3f56743f8eb765d547274091c8"},"cell_type":"code","source":"# save model\nmodel_json = model.to_json()\nwith open(os.path.join(os.path.abspath(model_path), 'model.json'), 'w') as json_file:\n    json_file.write(model_json)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e41c0c8243db4bf6c55270bccde1c7737855503"},"cell_type":"code","source":"# release memory\nk.clear_session()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"00d314eed82f19269a9d9f51fcc610374c6fdff6"},"cell_type":"code","source":"from keras.models import model_from_json\n\n# load json and create model\njson_file = open('../working/model.json', 'r')\nloaded_model_json = json_file.read()\njson_file.close()\nloaded_model = model_from_json(loaded_model_json)\n# load weights into new model\nloaded_model.load_weights(\"../working/top_model_weights.h5\")\nprint(\"Loaded model from disk\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f683947043c3b3cc9537391c3705a6da69c385f"},"cell_type":"code","source":"import numpy as np # linear algebra\nfrom tqdm import tqdm\nimport os\nimport cv2\n\n#Validating model on real senarios\n#print(testFiles)\ntestImg = []\n\n\n# Obtain images and resizing, obtain labels\nfileName=[]\ni=0\nfor img in tqdm(os.listdir('../input/seg_pred/seg_pred/')):\n  fileName.append(img)\n  testImg.append(cv2.resize(cv2.imread('../input/seg_pred/seg_pred/'+img), (150, 150)))\n  i=i+1\n\ntestImg = np.asarray(testImg)  # Train images set\ntestImg = testImg / 255\n#print(testImg)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e7b21f8cd8abcfa52bfa58d4ba9b3436f93e5e36"},"cell_type":"code","source":"print(testImg.shape)\ntestY = loaded_model.predict(testImg)\n#print(testY)\npredY = np.argmax(testY, axis = 1)\nd = []\ni=0\nfor pred in predY:\n    d.append({'image_name': fileName[i], 'label': pred})\n    i=i+1\noutput = pd.DataFrame(d)\noutput.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.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}