{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom keras.layers import Input, Lambda, Dense, Flatten\nfrom keras.models import Model\nfrom keras.applications.vgg16 import VGG16\nfrom keras.applications.vgg16 import preprocess_input\nfrom keras.preprocessing import image\nfrom keras.models import Sequential\nfrom glob import glob\nimport matplotlib.pyplot as plt\n\nfrom keras.optimizers import Adam, SGD, RMSprop\nimport tensorflow as tf\nimport cv2\nimport glob\nfrom keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array, array_to_img\nfrom tensorflow.python.keras import backend as K\nimport plotly.graph_objects as go\nimport plotly.offline as py\nautosize =False\n\nfrom plotly.subplots import make_subplots\nimport plotly.graph_objects as go\n\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:38:44.656914Z","iopub.execute_input":"2023-09-06T03:38:44.657814Z","iopub.status.idle":"2023-09-06T03:38:49.841045Z","shell.execute_reply.started":"2023-09-06T03:38:44.657755Z","shell.execute_reply":"2023-09-06T03:38:49.840220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:38:49.843044Z","iopub.execute_input":"2023-09-06T03:38:49.843392Z","iopub.status.idle":"2023-09-06T03:38:49.847915Z","shell.execute_reply.started":"2023-09-06T03:38:49.843356Z","shell.execute_reply":"2023-09-06T03:38:49.847081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir='/kaggle/input/siim-isic-melanoma-classification/jpeg/train/'\ntest_dir='/kaggle/input/siim-isic-melanoma-classification/jpeg/test/'\ntest=pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/test.csv')\n","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:38:49.849303Z","iopub.execute_input":"2023-09-06T03:38:49.849909Z","iopub.status.idle":"2023-09-06T03:38:49.916736Z","shell.execute_reply.started":"2023-09-06T03:38:49.849870Z","shell.execute_reply":"2023-09-06T03:38:49.915871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelling - VGG16 (Transfer Learning)","metadata":{}},{"cell_type":"code","source":"x1 = \"96\"\nx2 = \"96\"\nx_train_dim = \"x_train_\" + x1\nx_test_dim = \"x_test_\" + x1","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:38:49.917793Z","iopub.execute_input":"2023-09-06T03:38:49.918110Z","iopub.status.idle":"2023-09-06T03:38:49.931441Z","shell.execute_reply.started":"2023-09-06T03:38:49.918080Z","shell.execute_reply":"2023-09-06T03:38:49.930499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.load('../input/siimisic-melanoma-resized-images/' + x_train_dim + \".npy\")\nx_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:38:49.935287Z","iopub.execute_input":"2023-09-06T03:38:49.935819Z","iopub.status.idle":"2023-09-06T03:38:57.695882Z","shell.execute_reply.started":"2023-09-06T03:38:49.935777Z","shell.execute_reply":"2023-09-06T03:38:57.694954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/train.csv').target\n","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:38:57.698689Z","iopub.execute_input":"2023-09-06T03:38:57.699111Z","iopub.status.idle":"2023-09-06T03:38:57.779172Z","shell.execute_reply.started":"2023-09-06T03:38:57.699069Z","shell.execute_reply":"2023-09-06T03:38:57.778283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils import class_weight\n \nclass_weights = class_weight.compute_class_weight('balanced',\n                                                 np.unique(y),\n                                                 y)","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:38:57.781217Z","iopub.execute_input":"2023-09-06T03:38:57.781850Z","iopub.status.idle":"2023-09-06T03:38:58.310541Z","shell.execute_reply.started":"2023-09-06T03:38:57.781806Z","shell.execute_reply":"2023-09-06T03:38:58.309019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_weights = dict(enumerate(class_weights))","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:38:58.312048Z","iopub.execute_input":"2023-09-06T03:38:58.312402Z","iopub.status.idle":"2023-09-06T03:38:58.317018Z","shell.execute_reply.started":"2023-09-06T03:38:58.312362Z","shell.execute_reply":"2023-09-06T03:38:58.315807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_imgs, validation_imgs, y_train, y_val = train_test_split(x_train,y, test_size=0.2, random_state=1234)","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:38:58.318516Z","iopub.execute_input":"2023-09-06T03:38:58.318894Z","iopub.status.idle":"2023-09-06T03:38:58.630867Z","shell.execute_reply.started":"2023-09-06T03:38:58.318856Z","shell.execute_reply":"2023-09-06T03:38:58.629904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ndel x_train\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:38:58.633907Z","iopub.execute_input":"2023-09-06T03:38:58.634507Z","iopub.status.idle":"2023-09-06T03:38:58.754020Z","shell.execute_reply.started":"2023-09-06T03:38:58.634465Z","shell.execute_reply":"2023-09-06T03:38:58.752838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define parameters for model training\nbatch_size = 128\nnum_classes = 2\nepochs = 50\ninput_shape = (96,96,3)","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:38:58.755609Z","iopub.execute_input":"2023-09-06T03:38:58.756130Z","iopub.status.idle":"2023-09-06T03:38:58.765307Z","shell.execute_reply.started":"2023-09-06T03:38:58.756091Z","shell.execute_reply":"2023-09-06T03:38:58.764348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we will use Adam optimizer\nopt = Adam(lr=1e-5)\n\nnb_train_steps = train_imgs.shape[0]//batch_size\nnb_val_steps=validation_imgs.shape[0]//batch_size\n\nprint(\"Number of training and validation steps: {} and {}\".format(nb_train_steps,nb_val_steps))","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:38:58.766566Z","iopub.execute_input":"2023-09-06T03:38:58.766966Z","iopub.status.idle":"2023-09-06T03:38:58.777574Z","shell.execute_reply.started":"2023-09-06T03:38:58.766922Z","shell.execute_reply":"2023-09-06T03:38:58.776707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pixel Normalization and Image Augmentation\ntrain_datagen = ImageDataGenerator(rescale=1./255, zoom_range=0.3, rotation_range=50,\n                                   width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, \n                                   horizontal_flip=True, fill_mode='nearest')\n\n# no need to create augmentation images for validation data, only rescaling the pixels\nval_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_generator = train_datagen.flow(train_imgs, y_train, batch_size=32)\nval_generator = val_datagen.flow(validation_imgs, y_val, batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:38:58.779124Z","iopub.execute_input":"2023-09-06T03:38:58.779663Z","iopub.status.idle":"2023-09-06T03:38:59.656918Z","shell.execute_reply.started":"2023-09-06T03:38:58.779624Z","shell.execute_reply":"2023-09-06T03:38:59.655933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train_imgs,validation_imgs\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:38:59.658471Z","iopub.execute_input":"2023-09-06T03:38:59.658861Z","iopub.status.idle":"2023-09-06T03:38:59.778429Z","shell.execute_reply.started":"2023-09-06T03:38:59.658810Z","shell.execute_reply":"2023-09-06T03:38:59.777478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications import vgg16\nfrom keras.models import Model\nimport keras\n\nvgg = vgg16.VGG16(include_top=False, weights='imagenet', \n                                     input_shape=input_shape)\n\noutput = vgg.layers[-1].output\noutput = keras.layers.Flatten()(output)\nvgg_model = Model(vgg.input, output)\n\nvgg_model.trainable = False\nfor layer in vgg_model.layers:\n    layer.trainable = False\n    \nimport pandas as pd\npd.set_option('max_colwidth', -1)\nlayers = [(layer, layer.name, layer.trainable) for layer in vgg_model.layers]\npd.DataFrame(layers, columns=['Layer Type', 'Layer Name', 'Layer Trainable'])    ","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:38:59.780040Z","iopub.execute_input":"2023-09-06T03:38:59.780659Z","iopub.status.idle":"2023-09-06T03:39:03.048083Z","shell.execute_reply.started":"2023-09-06T03:38:59.780618Z","shell.execute_reply":"2023-09-06T03:39:03.047113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg_model.trainable = True\n\nset_trainable = False\nfor layer in vgg_model.layers:\n    if layer.name in ['block5_conv1', 'block4_conv1']:\n        set_trainable = True\n    if set_trainable:\n        layer.trainable = True\n    else:\n        layer.trainable = False\n        \nlayers = [(layer, layer.name, layer.trainable) for layer in vgg_model.layers]\npd.DataFrame(layers, columns=['Layer Type', 'Layer Name', 'Layer Trainable'])    ","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:39:03.049532Z","iopub.execute_input":"2023-09-06T03:39:03.050165Z","iopub.status.idle":"2023-09-06T03:39:03.071627Z","shell.execute_reply.started":"2023-09-06T03:39:03.050126Z","shell.execute_reply":"2023-09-06T03:39:03.070673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, InputLayer\nfrom keras.models import Sequential\nfrom keras import optimizers\n\nmodel = Sequential()\nmodel.add(vgg_model)\nmodel.add(Dense(512, activation='relu', input_dim=input_shape))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(1, activation='sigmoid'))\n\n\nmodel.compile(loss=\"binary_crossentropy\", metrics=[tf.keras.metrics.AUC()],optimizer=opt)","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:39:03.073043Z","iopub.execute_input":"2023-09-06T03:39:03.073422Z","iopub.status.idle":"2023-09-06T03:39:03.243287Z","shell.execute_reply.started":"2023-09-06T03:39:03.073384Z","shell.execute_reply":"2023-09-06T03:39:03.242490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(train_generator, steps_per_epoch=nb_train_steps, epochs=epochs,\n                              validation_data=val_generator, validation_steps=nb_val_steps,class_weight=class_weights, \n                              verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:39:03.244561Z","iopub.execute_input":"2023-09-06T03:39:03.244918Z","iopub.status.idle":"2023-09-06T03:55:22.117908Z","shell.execute_reply.started":"2023-09-06T03:39:03.244882Z","shell.execute_reply":"2023-09-06T03:55:22.116862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], color='r',label= 'Training loss')\nplt.plot(history.history['val_loss'], color='g', label= 'validation loss')\nplt.title('Training and Validation loss')\nplt.figure()\nplt.plot(history.history['auc'], color='r', label= 'Training accuracy' )\nplt.plot(history.history['val_auc'], color='g', label= 'validation accuracy')\nplt.title('Training and Validation Accuracy')\n","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:55:22.121917Z","iopub.execute_input":"2023-09-06T03:55:22.122257Z","iopub.status.idle":"2023-09-06T03:55:22.448551Z","shell.execute_reply.started":"2023-09-06T03:55:22.122218Z","shell.execute_reply":"2023-09-06T03:55:22.447605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train_generator,val_generator\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:55:22.449979Z","iopub.execute_input":"2023-09-06T03:55:22.450537Z","iopub.status.idle":"2023-09-06T03:55:22.623101Z","shell.execute_reply.started":"2023-09-06T03:55:22.450493Z","shell.execute_reply":"2023-09-06T03:55:22.622032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = np.load(\"../input/siimisic-melanoma-resized-images/\" + x_test_dim + \".npy\")","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:55:22.624595Z","iopub.execute_input":"2023-09-06T03:55:22.625002Z","iopub.status.idle":"2023-09-06T03:55:26.916413Z","shell.execute_reply.started":"2023-09-06T03:55:22.624958Z","shell.execute_reply":"2023-09-06T03:55:26.915457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from numpy import expand_dims\ndef tta_prediction(datagen, model, image, n_examples):\n    # convert image into dataset\n    samples = expand_dims(image, 0)\n    # prepare iterator\n    it = datagen.flow(samples, batch_size=n_examples)\n    # make predictions for each augmented image\n    probs = model.predict_generator(it, steps=n_examples, verbose=0)\n    #print(len(probs))    \n    prob = np.mean(probs, axis=1)    \n    return prob","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:55:26.919916Z","iopub.execute_input":"2023-09-06T03:55:26.920273Z","iopub.status.idle":"2023-09-06T03:55:26.926790Z","shell.execute_reply.started":"2023-09-06T03:55:26.920234Z","shell.execute_reply":"2023-09-06T03:55:26.925919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n# configure image data augmentation\ntest_datagen = ImageDataGenerator(rescale=1./255, zoom_range=0.3, rotation_range=50,\n                                   width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, \n                                   horizontal_flip=True, fill_mode='nearest')\ntarget=[]\n#i = 0\nfor img in tqdm(x_test):\n    prediction=tta_prediction(test_datagen,model,img,32)    \n    target.append(prediction[0])","metadata":{"execution":{"iopub.status.busy":"2023-09-06T03:55:26.928210Z","iopub.execute_input":"2023-09-06T03:55:26.928831Z","iopub.status.idle":"2023-09-06T04:03:36.623497Z","shell.execute_reply.started":"2023-09-06T03:55:26.928790Z","shell.execute_reply":"2023-09-06T04:03:36.621988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# submission file\nsub=pd.read_csv(\"../input/siim-isic-melanoma-classification/sample_submission.csv\")\nsub['target']=target\nsub.to_csv('submission.csv', index=False)\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2023-09-06T04:03:36.625079Z","iopub.execute_input":"2023-09-06T04:03:36.625458Z","iopub.status.idle":"2023-09-06T04:03:36.921092Z","shell.execute_reply.started":"2023-09-06T04:03:36.625416Z","shell.execute_reply":"2023-09-06T04:03:36.920108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in tqdm(range(int(9e6))): \n    pass","metadata":{"execution":{"iopub.status.busy":"2023-09-06T04:03:36.922339Z","iopub.execute_input":"2023-09-06T04:03:36.922807Z","iopub.status.idle":"2023-09-06T04:03:40.815177Z","shell.execute_reply.started":"2023-09-06T04:03:36.922725Z","shell.execute_reply":"2023-09-06T04:03:40.814235Z"},"trusted":true},"execution_count":null,"outputs":[]}],"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"}}