{"cells":[{"metadata":{"_uuid":"a0af326b-6198-44bf-96c7-20f095130ea8","_cell_guid":"c0002ff3-dade-410c-9519-a9f14fcccb22","trusted":true},"cell_type":"markdown","source":"# Importing Library","execution_count":null},{"metadata":{"_uuid":"d1bfcab4-a073-44a2-be6d-7c66d1eaaca4","_cell_guid":"b8294d18-ae48-404a-a0bc-ff1411d96fce","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\n\nimport tensorflow.keras as tk\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras.applications import VGG16\nfrom tensorflow.keras.layers import GlobalMaxPooling2D, Dense, Flatten,MaxPooling2D, GlobalAveragePooling2D, Dropout, Input, Concatenate, BatchNormalization, Conv2D\nfrom tensorflow.keras import Model\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6da5936f-66e5-4a43-8edf-49107c05919e","_cell_guid":"cca84b35-e1ed-48a2-bcbc-056fd67db2a3","trusted":true},"cell_type":"markdown","source":"# Reference Directory","execution_count":null},{"metadata":{"_uuid":"d9a422c2-67f8-482d-8752-0c0ef8c753d9","_cell_guid":"74c0867c-223f-4926-9c85-d2fa23f17642","trusted":true},"cell_type":"code","source":"train_dir = '../input/siim-isic-melanoma-classification/jpeg/train/'    \ntest_dir = '../input/siim-isic-melanoma-classification/jpeg/test/'\n\ntrain_csv_dir = '../input/siim-isic-melanoma-classification/train.csv'\ntest_csv_dir = '../input/siim-isic-melanoma-classification/test.csv'\n\ntrain_csv = pd.read_csv(train_csv_dir)\ntest_csv = pd.read_csv(test_csv_dir)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9cb7d6a1-530c-4b0b-a96a-81d346d87cd0","_cell_guid":"89f97c54-f6d1-4410-89f7-6e1152a971ac","trusted":true},"cell_type":"code","source":"train_df = []\ntrain_list = os.listdir(train_dir)\n\nfor i in train_list:\n    train_df.append(train_dir + i)\n\ntrain_df = pd.DataFrame(train_df)    \ntrain_df.columns = ['images']\ntrain_df['y'] = train_csv['target']","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f3c3675c-8497-4b8f-bc68-d51906dfbea7","_cell_guid":"76f32685-976d-4356-9f71-25831f210e5f","trusted":true},"cell_type":"markdown","source":"# Function - Digital Hair Remove (not used)","execution_count":null},{"metadata":{"_uuid":"a1aba7c8-3ec6-41f6-81b8-2b7f471b130c","_cell_guid":"11043d9b-5a4c-4357-880d-efb29067b9b7","trusted":true},"cell_type":"code","source":"def hair_remove(image):\n    \n    # convert image to grayScale\n    grayScale = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)\n    \n    # kernel for morphologyEx\n    kernel = cv2.getStructuringElement(1,(224,224))\n    \n    # apply MORPH_BLACKHAT to grayScale image\n    blackhat = cv2.morphologyEx(grayScale, cv2.MORPH_BLACKHAT, kernel)\n    \n    # apply thresholding to blackhat\n    _,threshold = cv2.threshold(blackhat,10,255,cv2.THRESH_BINARY)\n    \n    # inpaint with original image and threshold image\n    final_image = cv2.inpaint(image,threshold,3,cv2.INPAINT_TELEA)\n    \n    return final_image","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9cda6add-b0d3-4504-a71a-0add489a88d9","_cell_guid":"b58a2527-0a89-4c25-9643-a557b5be3b2a","trusted":true},"cell_type":"markdown","source":"# Data Generators","execution_count":null},{"metadata":{"_uuid":"26a09662-6c48-45b3-b9c5-a6dfb1839b68","_cell_guid":"e9d8fd07-a4ca-46b7-bd6a-01e756770286","trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale = 1./255, \n                                   horizontal_flip = True, \n                                   vertical_flip = True, \n                                   rotation_range = 45, \n                                   shear_range = 19,\n                                   validation_split = 0.15)\n\ntrain_generator = train_datagen.flow_from_dataframe(train_df,\n                                                    x_col='images',\n                                                    y_col='y',\n                                                    target_size = (224, 224), \n                                                    class_mode = 'raw',\n                                                    batch_size = 8,\n                                                    shuffle = True,\n                                                    subset = 'training')\n\nval_generator = train_datagen.flow_from_dataframe(train_df,\n                                                  x_col='images',\n                                                  y_col='y',\n                                                  target_size = (224, 224),\n                                                  class_mode = 'raw',\n                                                  batch_size = 8,\n                                                  shuffle = True,\n                                                  subset = 'validation')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4aa2c4fb-b18c-41a5-bc12-bb40fc441c10","_cell_guid":"f0b59ca4-ea9e-4b89-8fd9-7b2f518952fb","trusted":true},"cell_type":"markdown","source":"# Model","execution_count":null},{"metadata":{"_uuid":"56b555b6-124d-4df2-a8f1-f074986be6e8","_cell_guid":"b09573e6-9773-49f9-b351-1ba76f7673eb","trusted":true},"cell_type":"code","source":"\ninputs = Input((224, 224, 3))\npretrained_model= VGG16(include_top= False)\nx = pretrained_model(inputs)\noutput1 = GlobalMaxPooling2D()(x)\noutput2 = GlobalAveragePooling2D()(x)\noutput3 = Flatten()(x)\n\noutputs = Concatenate(axis=-1)([output1, output2, output3])\noutputs = Dropout(0.5)(outputs)\noutputs = BatchNormalization()(outputs)\noutput = Dense(1, activation= 'sigmoid')(outputs)\n\nmodel = Model(inputs, output)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6cf37b79-4f46-45ff-a76f-f7986102fcdc","_cell_guid":"82a18fc9-99fe-4763-b746-e8b33aac6a0e","trusted":true},"cell_type":"markdown","source":"# Callback Functions","execution_count":null},{"metadata":{"_uuid":"03011c9d-af01-4b0e-b799-a8d9cd709d62","_cell_guid":"d04c8981-38a3-4cff-8511-5385583625c5","trusted":true},"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, EarlyStopping, Callback\n\n\n# autosave best Model\nbest_model = ModelCheckpoint(\"model\", monitor='val_accuracy', mode='max',verbose=1, save_best_only=True)\n\nearlystop = EarlyStopping(monitor = 'val_accuracy',\n                          patience = 3,\n                          mode = 'auto',\n                          verbose = 1,\n                          restore_best_weights = True)\n\nacc_thresh = 0.998\n\nclass myCallback(Callback): \n    def on_epoch_end(self, epoch, logs={}): \n        if(logs.get('accuracy') > acc_thresh):   \n          print(\"\\nWe have reached %2.2f%% accuracy, so we will stopping training.\" %(acc_thresh*100))   \n          self.model.stop_training = True\n\ncallbacks = [myCallback(), best_model, earlystop]","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"672a49a9-4a0a-4414-8a91-aa3f91ffe6e0","_cell_guid":"bd328eef-1e19-4a23-8596-929b559fdb34","trusted":true},"cell_type":"markdown","source":"# Compiling and Training...","execution_count":null},{"metadata":{"_uuid":"194fba90-407e-4f11-bc7b-c4229469a79f","_cell_guid":"ad4b44e2-715f-4bb3-b775-8e4b1d1512b6","trusted":true},"cell_type":"code","source":"model.compile(optimizer='RMSProp', loss= 'binary_crossentropy', metrics= ['accuracy'])\nhistory = model.fit_generator(train_generator,\n                              epochs = 20,\n                              steps_per_epoch = len(train_generator),\n                              validation_data = val_generator,\n                              validation_steps = len(val_generator),\n                              callbacks = [callbacks, best_model],\n                              verbose= 1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1f1c3ded-cd06-4b29-b402-be66e2dd0ea3","_cell_guid":"72623178-e620-45c8-84d7-ad5691732b8b","trusted":true},"cell_type":"markdown","source":"# Plotting Accuracy","execution_count":null},{"metadata":{"_uuid":"9b1d4e7d-c4d6-437e-a526-4da4f6a02b57","_cell_guid":"579ca48f-6ff6-4f91-aa14-b9545ce51fda","trusted":true},"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='best')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"9ea62946-14c5-430d-9e4d-54cb9c0e8e6b","_cell_guid":"8ae391fa-43dd-4dc6-9585-6fe6202d3293","trusted":true},"cell_type":"markdown","source":"# Prediction","execution_count":null},{"metadata":{"_uuid":"d2f8a03a-8268-4ca8-99a7-f5fb1af3e343","_cell_guid":"254178e5-4cf8-4fdd-a424-b3a39bcac686","trusted":true},"cell_type":"code","source":"test_df = []\ntest_list = os.listdir(test_dir)\n\nfor i in test_list:\n    test_df.append(test_dir + i)\n\n\ntest_df = pd.DataFrame(test_df)    \ntest_df.columns = ['images']","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"276a3d81-32b8-45d8-9662-3ed8d37b0c11","_cell_guid":"0dab486a-5b93-4a5e-83fe-33d8aad5e9c0","trusted":true},"cell_type":"code","source":"target=[]\nfor path in test_df['images']:\n    img=cv2.imread(str(path))\n    img = cv2.resize(img, (224,224))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = img.astype(np.float32)/255.\n    img = np.reshape(img,(1,224,224,3))\n    prediction = model.predict(img)\n    target.append(prediction[0][0])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"557ce716-0f8a-4db9-8e08-1052a3ed4f16","_cell_guid":"a59701a4-15e3-4fe3-be10-473cc39df30a","trusted":true},"cell_type":"markdown","source":"# Submission","execution_count":null},{"metadata":{"_uuid":"ba559094-82c5-451b-a8bd-c0a3c85c4246","_cell_guid":"ee5b2bd2-b1f0-4cf3-b9fd-a842d3edfc88","trusted":true},"cell_type":"code","source":"submission=pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/sample_submission.csv')\n\nsubmission['target']=target\n\nsubmission.to_csv('submission.csv', index=False)\nsubmission.head()","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}