{"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\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 read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\ntrain = pd.read_csv(\"../input/siim-isic-melanoma-classification/train.csv\")\ntest = pd.read_csv(\"../input/siim-isic-melanoma-classification/test.csv\")\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train['target'] = train['target'].astype(str)\ntrain.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras_preprocessing.image import ImageDataGenerator\nfrom keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras import regularizers, optimizers\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.models import Model\n\ndef append_ext(fn):\n    return fn+\".jpg\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen=ImageDataGenerator(rescale=1./255,validation_split=0.25)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train[\"image_name\"] = train[\"image_name\"].apply(append_ext)\ntest[\"image_name\"] = test[\"image_name\"].apply(append_ext)\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator = datagen.flow_from_dataframe(dataframe=train, directory=\"../input/siim-isic-melanoma-classification/jpeg/train\",\n                                               x_col=\"image_name\", y_col=\"target\",target_size=(299,299),subset=\"training\",\n                                               shuffle=True,\n                                               batch_size=64,\n                                               class_mode=\"categorical\")\n\nval_generator = datagen.flow_from_dataframe(dataframe=train, directory=\"../input/siim-isic-melanoma-classification/jpeg/train\",\n                                               x_col=\"image_name\", y_col=\"target\",target_size=(299,299),subset=\"validation\",\n                                               shuffle=True,\n                                               batch_size=64,\n                                               class_mode=\"categorical\")\n\n# test_datagen=ImageDataGenerator(rescale=1./255.)\n\n# test_generator=test_datagen.flow_from_dataframe(dataframe=test,directory=\"../input/siim-isic-melanoma-classification/jpeg/test\",\n#                                                 x_col=\"image_name\",y_col=None,\n#                                                 batch_size=32,\n#                                                 shuffle=False,\n#                                                 class_mode=None,\n#                                                 target_size=(120,160))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.applications import InceptionResNetV2\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"basemodel = InceptionResNetV2(include_top=False,weights=\"imagenet\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"from keras.layers import GlobalAveragePooling2D\nheadmodel = basemodel.output\nheadmodel = GlobalAveragePooling2D()(headmodel)\nheadmodel = Dropout(0.5)(headmodel)\nheadmodel = Dense(16, activation=\"relu\")(headmodel)\nheadmodel = Dropout(0.5)(headmodel)\nheadmodel = Dense(1, activation=\"softmax\")(headmodel)\nmodel = Model(inputs=basemodel.input, outputs=headmodel)\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.callbacks import EarlyStopping,ModelCheckpoint,TensorBoard\n\ncb_checkpoint = ModelCheckpoint(filepath = '../working/best.hd5', monitor = 'val_loss', save_best_only = True, mode = 'auto',)\ncb_stopping = EarlyStopping(monitor='val_loss',patience=3,mode='min',restore_best_weights=True)\ncb_tensorboard = TensorBoard(log_dir='./logs', histogram_freq=0, write_graph=True,write_images=False)\nmy_callback=[cb_stopping, cb_checkpoint,cb_tensorboard]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(model.layers))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Fine tune model by retraining the few end layers of the inception model\nfrom keras.optimizers import SGD\nlayer_to_Freeze=770    \nfor layer in model.layers[:layer_to_Freeze]:\n    layer.trainable =False\nfor layer in model.layers[layer_to_Freeze:]:\n    layer.trainable=True\n#Define model compile for basic transfer learning\n#Using categorical_crossentropy loss as we need to classify only 2 classes and using softmax output layer.\n#Please read difference between categorical_crossentropy and binary_crossentropy here:= https://stackoverflow.com/questions/47877083/keras-binary-crossentropy-categorical-crossentropy-confusion\nsgd = SGD(lr = 0.01, decay = 1e-6, momentum = 0.9, nesterov = True)\nmodel.compile(optimizer=sgd,loss='categorical_crossentropy',metrics=[tf.keras.metrics.AUC()])\n# model.compile(optimizer=keras.optimizers.Adam(lr=0.01), loss=\"mse\", metrics=[tf.keras.metrics.AUC()])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history_transfer_learning = model.fit(train_generator,steps_per_epoch=train_generator.n//train_generator.batch_size,validation_data=val_generator,validation_steps=val_generator.n//val_generator.batch_size,epochs=10,verbose=1,callbacks=my_callback,)\n","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}