{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# import packages","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nimport PIL\nfrom IPython.display import Image, display\nfrom keras.applications.vgg16 import VGG16,preprocess_input\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom keras.applications.vgg19 import VGG19,preprocess_input, decode_predictions\nfrom sklearn.metrics import cohen_kappa_score\nfrom sklearn.model_selection import train_test_split\nfrom keras.models import Sequential, Model,load_model\n#from keras.applications.resnet50 import ResNet50\n\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Input, Flatten,BatchNormalization,Activation\nfrom keras.layers import GlobalMaxPooling2D\nfrom keras.models import Model\nfrom keras.optimizers import Adam, SGD, RMSprop\nfrom keras.callbacks import ModelCheckpoint, Callback, EarlyStopping\nfrom keras.utils import to_categorical\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport keras\nimport tensorflow as tf\nfrom tensorflow import random\nimport matplotlib.pyplot as plt\nfrom tensorflow.python.keras import backend as K\n#from livelossplot import PlotLossesKeras\nfrom sklearn.utils import resample # resampling unbalanced data","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:58:50.668328Z","iopub.execute_input":"2023-08-13T12:58:50.668712Z","iopub.status.idle":"2023-08-13T12:59:00.238338Z","shell.execute_reply.started":"2023-08-13T12:58:50.668681Z","shell.execute_reply":"2023-08-13T12:59:00.237188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow\nprint(tensorflow.__version__)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:00.240521Z","iopub.execute_input":"2023-08-13T12:59:00.241187Z","iopub.status.idle":"2023-08-13T12:59:00.247225Z","shell.execute_reply.started":"2023-08-13T12:59:00.241157Z","shell.execute_reply":"2023-08-13T12:59:00.246267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset directorys","metadata":{}},{"cell_type":"code","source":"train_dir='/kaggle/input/siim-isic-melanoma-classification/jpeg/train/'\ntest_dir='/kaggle/input/siim-isic-melanoma-classification/jpeg/test/'\ntrain=pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/train.csv')\n#test=pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:00.248797Z","iopub.execute_input":"2023-08-13T12:59:00.249517Z","iopub.status.idle":"2023-08-13T12:59:00.359262Z","shell.execute_reply.started":"2023-08-13T12:59:00.249482Z","shell.execute_reply":"2023-08-13T12:59:00.358067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:00.363494Z","iopub.execute_input":"2023-08-13T12:59:00.364233Z","iopub.status.idle":"2023-08-13T12:59:00.380124Z","shell.execute_reply.started":"2023-08-13T12:59:00.364204Z","shell.execute_reply":"2023-08-13T12:59:00.378972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:00.382009Z","iopub.execute_input":"2023-08-13T12:59:00.382629Z","iopub.status.idle":"2023-08-13T12:59:00.404031Z","shell.execute_reply.started":"2023-08-13T12:59:00.382596Z","shell.execute_reply":"2023-08-13T12:59:00.402960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preprocessing\n1. df_0 contien benign data(target=0) 32542/33126\n1. df_1 contien malign data(target=1) 584/33126","metadata":{}},{"cell_type":"code","source":"df_0=train[train['target']==0]\ndf_1_all=train[train['target']==1]","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:00.405618Z","iopub.execute_input":"2023-08-13T12:59:00.406023Z","iopub.status.idle":"2023-08-13T12:59:00.418000Z","shell.execute_reply.started":"2023-08-13T12:59:00.405990Z","shell.execute_reply":"2023-08-13T12:59:00.416940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=df_0['patient_id'].value_counts().index\nvalues=df_0['patient_id'].value_counts().values","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:00.623658Z","iopub.execute_input":"2023-08-13T12:59:00.624661Z","iopub.status.idle":"2023-08-13T12:59:00.643770Z","shell.execute_reply.started":"2023-08-13T12:59:00.624620Z","shell.execute_reply":"2023-08-13T12:59:00.642540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_0_all=df_0.drop_duplicates(subset=[\"patient_id\",\"anatom_site_general_challenge\"])","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:01.653575Z","iopub.execute_input":"2023-08-13T12:59:01.654312Z","iopub.status.idle":"2023-08-13T12:59:01.675534Z","shell.execute_reply.started":"2023-08-13T12:59:01.654280Z","shell.execute_reply":"2023-08-13T12:59:01.674367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=df_0_all['patient_id'].value_counts().index\nvalues=df_0_all['patient_id'].value_counts().values","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:02.196533Z","iopub.execute_input":"2023-08-13T12:59:02.196900Z","iopub.status.idle":"2023-08-13T12:59:02.207434Z","shell.execute_reply.started":"2023-08-13T12:59:02.196869Z","shell.execute_reply":"2023-08-13T12:59:02.206465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_0_all.shape,df_1_all.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:02.625096Z","iopub.execute_input":"2023-08-13T12:59:02.625490Z","iopub.status.idle":"2023-08-13T12:59:02.634198Z","shell.execute_reply.started":"2023-08-13T12:59:02.625457Z","shell.execute_reply":"2023-08-13T12:59:02.633187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Creation of the TEST database****** \n20% from train dataset**/AND/** 116 class1 1254 class0","metadata":{}},{"cell_type":"code","source":"#class 0 benign\ndf_0_test=df_0_all.sample(1254)\ndf_0_train=df_0_all.drop(df_0_test.index)\n#class 1 malignant\ndf_1_test=df_1_all.sample(116)\ndf_1_train=df_1_all.drop(df_1_test.index)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:03.223366Z","iopub.execute_input":"2023-08-13T12:59:03.224507Z","iopub.status.idle":"2023-08-13T12:59:03.234770Z","shell.execute_reply.started":"2023-08-13T12:59:03.224462Z","shell.execute_reply":"2023-08-13T12:59:03.233649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_0_test.shape,df_1_test.shape\n","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:03.473413Z","iopub.execute_input":"2023-08-13T12:59:03.473777Z","iopub.status.idle":"2023-08-13T12:59:03.480762Z","shell.execute_reply.started":"2023-08-13T12:59:03.473745Z","shell.execute_reply":"2023-08-13T12:59:03.479723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_0_train.shape,df_1_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:03.695034Z","iopub.execute_input":"2023-08-13T12:59:03.695389Z","iopub.status.idle":"2023-08-13T12:59:03.704697Z","shell.execute_reply.started":"2023-08-13T12:59:03.695359Z","shell.execute_reply":"2023-08-13T12:59:03.703522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Preparing the Datasets**","metadata":{}},{"cell_type":"code","source":"train=pd.concat([df_0_train,df_1_train])","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:04.091066Z","iopub.execute_input":"2023-08-13T12:59:04.092162Z","iopub.status.idle":"2023-08-13T12:59:04.100998Z","shell.execute_reply.started":"2023-08-13T12:59:04.092114Z","shell.execute_reply":"2023-08-13T12:59:04.099934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=[]\ndata=[]\nfor i in range(train.shape[0]):\n    data.append(train_dir + train['image_name'].iloc[i]+'.jpg')\n    labels.append(train['target'].iloc[i])\ndf_train=pd.DataFrame(data)\ndf_train.columns=['images']\ndf_train['target']=labels","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:04.286861Z","iopub.execute_input":"2023-08-13T12:59:04.287289Z","iopub.status.idle":"2023-08-13T12:59:04.498285Z","shell.execute_reply.started":"2023-08-13T12:59:04.287259Z","shell.execute_reply":"2023-08-13T12:59:04.497256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:04.500029Z","iopub.execute_input":"2023-08-13T12:59:04.500443Z","iopub.status.idle":"2023-08-13T12:59:04.508531Z","shell.execute_reply.started":"2023-08-13T12:59:04.500384Z","shell.execute_reply":"2023-08-13T12:59:04.507584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['images'].iloc[0]","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:04.714321Z","iopub.execute_input":"2023-08-13T12:59:04.714936Z","iopub.status.idle":"2023-08-13T12:59:04.726371Z","shell.execute_reply.started":"2023-08-13T12:59:04.714886Z","shell.execute_reply":"2023-08-13T12:59:04.721875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Creation of the Validation database****** \n20% from train dataset(5487 images)","metadata":{}},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(df_train['images'],df_train['target'], test_size=0.2, random_state=1234)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:05.135344Z","iopub.execute_input":"2023-08-13T12:59:05.135724Z","iopub.status.idle":"2023-08-13T12:59:05.145835Z","shell.execute_reply.started":"2023-08-13T12:59:05.135693Z","shell.execute_reply":"2023-08-13T12:59:05.144875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.DataFrame(X_train)\ntrain.columns=['images']\ntrain['target']=y_train\n\nvalidation=pd.DataFrame(X_val)\nvalidation.columns=['images']\nvalidation['target']=y_val","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:05.345872Z","iopub.execute_input":"2023-08-13T12:59:05.346241Z","iopub.status.idle":"2023-08-13T12:59:05.355084Z","shell.execute_reply.started":"2023-08-13T12:59:05.346213Z","shell.execute_reply":"2023-08-13T12:59:05.353976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* normalizing\n* reshaping\n* augmentation(only for tarin data)","metadata":{}},{"cell_type":"code","source":"# define parameters for model training\nbatch_size = 32 # the total number of images processed per iteration\nnum_classes = 2 # we have two classes; benign and malignant\nnb_epochs = 50 # the number of iteration over the entire training set\ninput_shape = (224, 224, 3)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:06.523233Z","iopub.execute_input":"2023-08-13T12:59:06.523600Z","iopub.status.idle":"2023-08-13T12:59:06.528800Z","shell.execute_reply.started":"2023-08-13T12:59:06.523570Z","shell.execute_reply":"2023-08-13T12:59:06.527572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"###############################\"\"\ntrain_datagen = ImageDataGenerator(rescale=1./255,rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,horizontal_flip=True)\nval_datagen=ImageDataGenerator(rescale=1./255)\n#############################################################\ntrain_generator = train_datagen.flow_from_dataframe(train,\n    x_col='images',\n    y_col='target',\n    target_size=(224, 224),\n    batch_size=batch_size,\n    shuffle=True,\n    class_mode='raw')\n\nvalidation_generator = val_datagen.flow_from_dataframe(validation,\n    x_col='images',\n    y_col='target',\n    target_size=(224, 224),\n    shuffle=False,\n    batch_size=batch_size,\n    class_mode='raw')","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:06.811629Z","iopub.execute_input":"2023-08-13T12:59:06.812136Z","iopub.status.idle":"2023-08-13T12:59:24.511283Z","shell.execute_reply.started":"2023-08-13T12:59:06.812106Z","shell.execute_reply":"2023-08-13T12:59:24.510301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modeling","metadata":{}},{"cell_type":"code","source":"vgg_model = VGG19(weights='imagenet', include_top=False, input_shape=(224, 224, 3))","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:24.513206Z","iopub.execute_input":"2023-08-13T12:59:24.513656Z","iopub.status.idle":"2023-08-13T12:59:29.664657Z","shell.execute_reply.started":"2023-08-13T12:59:24.513623Z","shell.execute_reply":"2023-08-13T12:59:29.663574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Remove the last layer, labeled predictions\nlast_layer = str(vgg_model.layers[-1])\n# creating an instance of Sequential model\nmodel=Sequential() \n# add the VGG16 model\nfor layer in vgg_model.layers:\n    #if str(layer) != last_layer:\n        model.add(layer)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:29.666215Z","iopub.execute_input":"2023-08-13T12:59:29.666559Z","iopub.status.idle":"2023-08-13T12:59:29.794162Z","shell.execute_reply.started":"2023-08-13T12:59:29.666525Z","shell.execute_reply":"2023-08-13T12:59:29.793176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### set all layers to not be trained\nfor layer in model.layers:\n    #print(layer)\n    layer.trainable=False","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:29.796445Z","iopub.execute_input":"2023-08-13T12:59:29.796859Z","iopub.status.idle":"2023-08-13T12:59:29.802418Z","shell.execute_reply.started":"2023-08-13T12:59:29.796826Z","shell.execute_reply":"2023-08-13T12:59:29.801519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# add dense and dropout layers\nmodel.add(Flatten())\nmodel.add(Dense(4096, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(2048, activation='relu'))\nmodel.add(Dropout(0.5))\n#model.add(Dense(2048, activation='relu'))\nmodel.add(Dense(1024, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dropout(0.4))\nmodel.add(Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:29.803940Z","iopub.execute_input":"2023-08-13T12:59:29.804513Z","iopub.status.idle":"2023-08-13T12:59:29.908221Z","shell.execute_reply.started":"2023-08-13T12:59:29.804481Z","shell.execute_reply":"2023-08-13T12:59:29.907358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:34.097756Z","iopub.execute_input":"2023-08-13T12:59:34.098161Z","iopub.status.idle":"2023-08-13T12:59:34.157175Z","shell.execute_reply.started":"2023-08-13T12:59:34.098128Z","shell.execute_reply":"2023-08-13T12:59:34.156438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape[0],validation.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:41.434359Z","iopub.execute_input":"2023-08-13T12:59:41.434729Z","iopub.status.idle":"2023-08-13T12:59:41.441410Z","shell.execute_reply.started":"2023-08-13T12:59:41.434699Z","shell.execute_reply":"2023-08-13T12:59:41.440516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape[0]//batch_size,validation.shape[0]//batch_size","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:42.369648Z","iopub.execute_input":"2023-08-13T12:59:42.370381Z","iopub.status.idle":"2023-08-13T12:59:42.377742Z","shell.execute_reply.started":"2023-08-13T12:59:42.370342Z","shell.execute_reply":"2023-08-13T12:59:42.376554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = Adam(lr=1e-5)\nmodel.compile(optimizer=opt, loss='binary_focal_crossentropy',metrics=['accuracy','Precision','AUC'])","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:45.883158Z","iopub.execute_input":"2023-08-13T12:59:45.883612Z","iopub.status.idle":"2023-08-13T12:59:45.899154Z","shell.execute_reply.started":"2023-08-13T12:59:45.883577Z","shell.execute_reply":"2023-08-13T12:59:45.897873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nb_train_steps = train.shape[0]//batch_size\nnb_val_steps=validation.shape[0]//batch_size\nprint(\"Number of training and validation steps: {} and {}\".format(nb_train_steps,nb_val_steps))","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:51.261356Z","iopub.execute_input":"2023-08-13T12:59:51.261726Z","iopub.status.idle":"2023-08-13T12:59:51.267827Z","shell.execute_reply.started":"2023-08-13T12:59:51.261693Z","shell.execute_reply":"2023-08-13T12:59:51.266825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#cb=[PlotLossesKeras()]\nhistory=model.fit_generator(\n    train_generator,\n    steps_per_epoch=nb_train_steps,\n    epochs=nb_epochs,\n    validation_data=validation_generator,\n    #callbacks=cb,\n    validation_steps=nb_val_steps)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T12:59:52.726829Z","iopub.execute_input":"2023-08-13T12:59:52.727505Z","iopub.status.idle":"2023-08-13T18:52:54.906575Z","shell.execute_reply.started":"2023-08-13T12:59:52.727472Z","shell.execute_reply":"2023-08-13T18:52:54.905530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(history.history.keys())\n#  \"Accuracy\"\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()\nprint(history.history.keys())\n# \"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', 'validation'], loc='upper left')\nplt.show()\n#  \"Precision\"\nplt.plot(history.history['precision'])\nplt.plot(history.history['val_precision'])\nplt.title('precision')\nplt.ylabel('precision')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()\n#  \"AUC\"\nplt.plot(history.history['auc'])\nplt.plot(history.history['val_auc'])\nplt.title('AUC')\nplt.ylabel('AUC')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:01:15.001015Z","iopub.execute_input":"2023-08-13T19:01:15.001981Z","iopub.status.idle":"2023-08-13T19:01:16.522691Z","shell.execute_reply.started":"2023-08-13T19:01:15.001949Z","shell.execute_reply":"2023-08-13T19:01:16.521794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist_data= pd.DataFrame(history.history) \nhist_data.to_excel('history.xlsx', index=True)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:01:16.524066Z","iopub.execute_input":"2023-08-13T19:01:16.525039Z","iopub.status.idle":"2023-08-13T19:01:16.559058Z","shell.execute_reply.started":"2023-08-13T19:01:16.525002Z","shell.execute_reply":"2023-08-13T19:01:16.558192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from keras.preprocessing import image\n#from tensorflow.keras.utils import img_to_array,load_img\nfrom tqdm import tqdm\nimport time\nimport cv2\n#from keras.preprocessing.image import ImageDataGenerator,load_img, img_to_array","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:01:16.560304Z","iopub.execute_input":"2023-08-13T19:01:16.560716Z","iopub.status.idle":"2023-08-13T19:01:16.568145Z","shell.execute_reply.started":"2023-08-13T19:01:16.560686Z","shell.execute_reply":"2023-08-13T19:01:16.567190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test Evaluation","metadata":{}},{"cell_type":"code","source":"df_0_test.shape,df_1_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:01:16.569594Z","iopub.execute_input":"2023-08-13T19:01:16.569951Z","iopub.status.idle":"2023-08-13T19:01:16.581806Z","shell.execute_reply.started":"2023-08-13T19:01:16.569894Z","shell.execute_reply":"2023-08-13T19:01:16.580854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.concat([df_0_test,df_1_test])","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:01:16.582944Z","iopub.execute_input":"2023-08-13T19:01:16.583928Z","iopub.status.idle":"2023-08-13T19:01:16.591888Z","shell.execute_reply.started":"2023-08-13T19:01:16.583875Z","shell.execute_reply":"2023-08-13T19:01:16.590967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:01:16.598849Z","iopub.execute_input":"2023-08-13T19:01:16.599165Z","iopub.status.idle":"2023-08-13T19:01:16.606728Z","shell.execute_reply.started":"2023-08-13T19:01:16.599141Z","shell.execute_reply":"2023-08-13T19:01:16.605832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_labels=[]\ntest_data=[]\nfor i in range(test.shape[0]):\n    test_data.append(train_dir + test['image_name'].iloc[i]+'.jpg')\n    test_labels.append(test['target'].iloc[i])\ndf_test=pd.DataFrame(test_data)\ndf_test.columns=['images']\ndf_test['target']=test_labels","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:01:16.608108Z","iopub.execute_input":"2023-08-13T19:01:16.608805Z","iopub.status.idle":"2023-08-13T19:01:16.667198Z","shell.execute_reply.started":"2023-08-13T19:01:16.608680Z","shell.execute_reply":"2023-08-13T19:01:16.666286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:01:16.670226Z","iopub.execute_input":"2023-08-13T19:01:16.670499Z","iopub.status.idle":"2023-08-13T19:01:16.679049Z","shell.execute_reply.started":"2023-08-13T19:01:16.670475Z","shell.execute_reply":"2023-08-13T19:01:16.678172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target=[]\nfor path in df_test['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]) ","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:01:16.680548Z","iopub.execute_input":"2023-08-13T19:01:16.681151Z","iopub.status.idle":"2023-08-13T19:06:17.329697Z","shell.execute_reply.started":"2023-08-13T19:01:16.681119Z","shell.execute_reply":"2023-08-13T19:06:17.328781Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(target)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:06:17.331201Z","iopub.execute_input":"2023-08-13T19:06:17.331578Z","iopub.status.idle":"2023-08-13T19:06:17.340668Z","shell.execute_reply.started":"2023-08-13T19:06:17.331543Z","shell.execute_reply":"2023-08-13T19:06:17.339563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission=pd.DataFrame(df_test)\n\ndf_submission['target_prediction']=target","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:06:17.342436Z","iopub.execute_input":"2023-08-13T19:06:17.342802Z","iopub.status.idle":"2023-08-13T19:06:17.351776Z","shell.execute_reply.started":"2023-08-13T19:06:17.342770Z","shell.execute_reply":"2023-08-13T19:06:17.350843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:06:17.353260Z","iopub.execute_input":"2023-08-13T19:06:17.353792Z","iopub.status.idle":"2023-08-13T19:06:17.367676Z","shell.execute_reply.started":"2023-08-13T19:06:17.353761Z","shell.execute_reply":"2023-08-13T19:06:17.366460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission.to_excel('submission.xlsx', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:06:17.369392Z","iopub.execute_input":"2023-08-13T19:06:17.369894Z","iopub.status.idle":"2023-08-13T19:06:17.529406Z","shell.execute_reply.started":"2023-08-13T19:06:17.369855Z","shell.execute_reply":"2023-08-13T19:06:17.528476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\n##############################\"\"\ntest_data_generator = test_datagen.flow_from_dataframe(df_test,\n    x_col='images',\n    y_col='target',\n    target_size=(224, 224),\n    batch_size=batch_size,\n    shuffle=False,\n    class_mode='raw')","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:06:17.530971Z","iopub.execute_input":"2023-08-13T19:06:17.531319Z","iopub.status.idle":"2023-08-13T19:06:18.095081Z","shell.execute_reply.started":"2023-08-13T19:06:17.531287Z","shell.execute_reply":"2023-08-13T19:06:18.094155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(test_data_generator, verbose=0)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:06:18.096306Z","iopub.execute_input":"2023-08-13T19:06:18.096671Z","iopub.status.idle":"2023-08-13T19:07:53.188685Z","shell.execute_reply.started":"2023-08-13T19:06:18.096637Z","shell.execute_reply":"2023-08-13T19:07:53.187587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Test loss:', score[0])\nprint('Test accuracy:', score[1])","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:07:53.190402Z","iopub.execute_input":"2023-08-13T19:07:53.191460Z","iopub.status.idle":"2023-08-13T19:07:53.198313Z","shell.execute_reply.started":"2023-08-13T19:07:53.191424Z","shell.execute_reply":"2023-08-13T19:07:53.197136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = model.evaluate_generator(test_data_generator)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:07:53.199857Z","iopub.execute_input":"2023-08-13T19:07:53.200277Z","iopub.status.idle":"2023-08-13T19:09:09.150988Z","shell.execute_reply.started":"2023-08-13T19:07:53.200244Z","shell.execute_reply":"2023-08-13T19:09:09.149847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:09:09.154706Z","iopub.execute_input":"2023-08-13T19:09:09.155384Z","iopub.status.idle":"2023-08-13T19:09:09.164464Z","shell.execute_reply.started":"2023-08-13T19:09:09.155346Z","shell.execute_reply":"2023-08-13T19:09:09.163378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scor_data= pd.DataFrame(score) \noutput_data= pd.DataFrame(output) ","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:09:09.166508Z","iopub.execute_input":"2023-08-13T19:09:09.167456Z","iopub.status.idle":"2023-08-13T19:09:09.173170Z","shell.execute_reply.started":"2023-08-13T19:09:09.167423Z","shell.execute_reply":"2023-08-13T19:09:09.171982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scor_data.to_excel('score.xlsx', index=True)\noutput_data.to_excel('output.xlsx', index=True)","metadata":{"execution":{"iopub.status.busy":"2023-08-13T19:09:09.174495Z","iopub.execute_input":"2023-08-13T19:09:09.175661Z","iopub.status.idle":"2023-08-13T19:09:09.204842Z","shell.execute_reply.started":"2023-08-13T19:09:09.175626Z","shell.execute_reply":"2023-08-13T19:09:09.203974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}