{"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":"code","source":"import tensorflow.compat.v1 as tf\nimport os\nos.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\ngpu_options = tf.GPUOptions(allow_growth=True)\nsess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))\n\nimport numpy as np, pandas as pd, os, gc\nimport matplotlib.pyplot as plt, time\nfrom PIL import Image \nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\npath = '../input/severstal-steel-defect-detection/'\ntrain = pd.read_csv(path + 'train.csv')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-18T09:35:12.308200Z","iopub.execute_input":"2022-07-18T09:35:12.309164Z","iopub.status.idle":"2022-07-18T09:35:15.403683Z","shell.execute_reply.started":"2022-07-18T09:35:12.309116Z","shell.execute_reply":"2022-07-18T09:35:15.402707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import libraries\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nfrom time import time\nfrom datetime import datetime\n\nimport pandas as pd\nimport numpy as np\nimport os\nfrom cv2 import cv2\nfrom PIL import Image\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split\n\nimport keras\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import backend as K\nfrom keras.layers import GlobalAveragePooling2D, Dense, Conv2D, BatchNormalization, Dropout\nfrom keras.models import Model, load_model\nimport tensorflow as tf\nfrom tensorflow.python.keras.callbacks import TensorBoard\nfrom keras.callbacks import ModelCheckpoint\n\nfrom sklearn.metrics import recall_score\nfrom random import random\nfrom random import seed\n\n!git clone https://github.com/qubvel/segmentation_models\n! pip install segmentation-models\nimport segmentation_models\nprint(segmentation_models.__version__)\n\nimport segmentation_models as sm\nfrom segmentation_models import Unet\nfrom segmentation_models import get_preprocessing\nfrom tensorflow.keras.utils import plot_model\nimport tensorflow.compat.v1 as tf\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport skimage.io\nimport os \nimport tqdm\nimport glob\nimport tensorflow \nimport warnings\nfrom tqdm import tqdm\nimport sklearn\nfrom sklearn.utils import shuffle\nfrom sklearn import metrics\nfrom sklearn.metrics import confusion_matrix, classification_report\nfrom sklearn.model_selection import train_test_split\nimport keras\nfrom skimage.io import imread, imshow\nfrom skimage.transform import resize\nfrom skimage.color import grey2rgb\nimport cv2\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import InputLayer, BatchNormalization, Dropout, Flatten, Dense, Activation, MaxPool2D, Conv2D\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.utils import to_categorical\nfrom keras import optimizers\nfrom tensorflow.keras.optimizers import Adam\nfrom keras.applications.nasnet import NASNetLarge, NASNetMobile\nfrom keras.callbacks import Callback,ModelCheckpoint,ReduceLROnPlateau\nfrom sklearn.preprocessing import OneHotEncoder,LabelEncoder\nfrom tensorflow.keras.utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.losses import binary_crossentropy\nfrom keras.layers import Dense,Conv2D,Flatten,MaxPooling2D,Dropout,GlobalAveragePooling2D\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import InputLayer, BatchNormalization, Dropout, Flatten, Dense, Activation, MaxPool2D\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom keras.models import Sequential,load_model\nfrom keras.layers import Dense, Dropout\nfrom keras.wrappers.scikit_learn import KerasClassifier\nimport keras.backend as K\nfrom keras.applications.xception import Xception, preprocess_input\nimport time\nimport sklearn.metrics as metrics\nfrom sklearn.preprocessing import MultiLabelBinarizer\nfrom sklearn.metrics import jaccard_score","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:35:15.407854Z","iopub.execute_input":"2022-07-18T09:35:15.408146Z","iopub.status.idle":"2022-07-18T09:35:31.323163Z","shell.execute_reply.started":"2022-07-18T09:35:15.408120Z","shell.execute_reply":"2022-07-18T09:35:31.322127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/severstal-steel-defect-detection/train.csv\")\ntrain.fillna(0,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:35:31.325029Z","iopub.execute_input":"2022-07-18T09:35:31.325379Z","iopub.status.idle":"2022-07-18T09:35:31.510191Z","shell.execute_reply.started":"2022-07-18T09:35:31.325342Z","shell.execute_reply":"2022-07-18T09:35:31.509216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Number of unique images in Training Dataset with defect\",len(train.ImageId.unique()))\npath, dirs, files = next(os.walk(\"../input/severstal-steel-defect-detection/train_images\"))\nfile_count = len(files)\nfiles.sort()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:35:31.512915Z","iopub.execute_input":"2022-07-18T09:35:31.513276Z","iopub.status.idle":"2022-07-18T09:35:41.004617Z","shell.execute_reply.started":"2022-07-18T09:35:31.513230Z","shell.execute_reply":"2022-07-18T09:35:41.003636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a new dataframe similar to train.csv\ndf=pd.DataFrame(columns=[\"ImageId\",\"ClassId\",\"EncodedPixels\"]) # Combining the data of Images with defect\nind=0\nfor i in train.ImageId.unique(): # Create seperate section of each class with respect to image\n  for k in range(1,5): # Class Range (1-4)\n    df.loc[ind]={'ImageId':i,'ClassId':k,'EncodedPixels':0}\n    ind=ind+1\nfor i in range(0,len(train[\"ImageId\"])): # Merging Data from train.csv\n  image_id=train[\"ImageId\"].iloc[i]\n  class_id=train[\"ClassId\"].iloc[i]\n  df.loc[(df.ImageId==image_id) & (df.ClassId == class_id),\"EncodedPixels\"]= train[\"EncodedPixels\"].iloc[i]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:35:41.006898Z","iopub.execute_input":"2022-07-18T09:35:41.007654Z","iopub.status.idle":"2022-07-18T09:38:33.281348Z","shell.execute_reply.started":"2022-07-18T09:35:41.007613Z","shell.execute_reply":"2022-07-18T09:38:33.279680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Since train.csv contains images with defect, so we need to merge data of images without defect as well\nind=0\nif files[0]!=df[\"ImageId\"].iloc[0]: # For First Iteration only (To handle Edge case)\n  for k in range(1,5):\n    df.iloc[k-1]={'ImageId':files[0],'ClassId':k,'EncodedPixels':0}\n    ind+=1\nind=ind+4\nfor i in range(1,file_count): \n  if files[i]!= df[\"ImageId\"].iloc[ind]: \n    for k in range(1,5):\n      data = pd.DataFrame({'ImageId':files[i],'ClassId':k,'EncodedPixels':0}, index=[ind-0.5+k-1]) \n      df = df.append(data, ignore_index=False)\n      df = df.sort_index().reset_index(drop=True)\n  ind=ind+4","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:38:33.287373Z","iopub.execute_input":"2022-07-18T09:38:33.290322Z","iopub.status.idle":"2022-07-18T09:41:01.135536Z","shell.execute_reply.started":"2022-07-18T09:38:33.290282Z","shell.execute_reply":"2022-07-18T09:41:01.134512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Seperation of Data based on Class for further Analysis\ncount=0\nclass_list=[]\nclass_count=[]\nmulti_class=[]\ndummy=[]\nm_class=[]\nclass_1=[] # Only 1 defect \nclass_2=[] # Only 2 Defects\nclass_3=[]\nclass_4=[]\nno_class=[]\nfor i in range(0,len(df['EncodedPixels']),4): # Read df Dataframe with each step is of 4 (Number of Classes)\n    for k in range(0+i,4+i):\n        if(df['EncodedPixels'][k]!=0): # Check for image with index i has Class k type defect or not  \n            count+=1\n            dummy.append(int(df['ClassId'][k])) # save Class type for a image index and save in Dummy array\n            pass\n        if(k==i+3 and count>=1): # Defect with 1 or more class\n            multi_class.append(dummy) # Tracks types of Multi class detection like - ([1],[2]), ([1],[3]), ([2],[4]) etc.\n            class_count=class_count+dummy\n            class_list.append(i) # List of Df index containing defects (1,2 or more number)\n            A=0\n            B=0\n            C=0\n            D=0\n            for x in dummy:\n                if(x==1):A=1 \n                if(x==2):B=1 \n                if(x==3):C=1 \n                if(x==4):D=1 \n                pass\n            m_class.append([A,B,C,D]) # Define n-array for classification \n            if(count==1):\n                class_1.append(i) # save Index of Dataframe inshort save image ID which is extracted later \n                pass\n            elif(count==2):\n                class_2.append(i)\n                pass\n            elif(count==3):\n                class_3.append(i)\n                pass\n            elif(count==4):\n                class_4.append(i)\n                pass\n            pass\n        elif(k==i+3 and count==0): # If No defect Class found \n            no_class.append(i)\n            \n    count=0\n    dummy=[]\n    pass","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:41:01.137145Z","iopub.execute_input":"2022-07-18T09:41:01.137488Z","iopub.status.idle":"2022-07-18T09:41:01.696706Z","shell.execute_reply.started":"2022-07-18T09:41:01.137453Z","shell.execute_reply":"2022-07-18T09:41:01.695768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dff=pd.DataFrame({'class_types':['Defect_class','Non_defect_class'],'class_count':[len(class_list),(len(df)/4)-len(class_list)]},index=['Defect_classes','Non_defect_classes'])\ndff.plot.bar(x='class_types',y='class_count',figsize=(5,5)).set_title('Distribution Based on Binary Class')\n\nprint(round(len(class_list)/(len(df)/4)*100,2),'%  of the Images are Defective')\nprint(round(100-(len(class_list)/(len(df)/4))*100,2),'%  of the Images are Non-Defective')\ndff","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:41:01.698049Z","iopub.execute_input":"2022-07-18T09:41:01.698408Z","iopub.status.idle":"2022-07-18T09:41:01.991275Z","shell.execute_reply.started":"2022-07-18T09:41:01.698374Z","shell.execute_reply":"2022-07-18T09:41:01.983044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_multi = pd.pivot_table(df, values='EncodedPixels', index='ImageId',columns='ClassId', aggfunc=np.sum).astype(str)\ndf_multi = df_multi.reset_index()\ndf_multi.columns = ['ImageId','Loc_Defect_1','Loc_Defect_2','Loc_Defect_3','Loc_Defect_4']\ndf_multi.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:41:01.992616Z","iopub.execute_input":"2022-07-18T09:41:01.992965Z","iopub.status.idle":"2022-07-18T09:41:02.137476Z","shell.execute_reply.started":"2022-07-18T09:41:01.992929Z","shell.execute_reply":"2022-07-18T09:41:02.136527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = []\nfor i in range(len(df_multi)):\n    if all((df_multi['Loc_Defect_1'][i]=='0',df_multi['Loc_Defect_2'][i]=='0',df_multi['Loc_Defect_3'][i]=='0',df_multi['Loc_Defect_4'][i]=='0')):\n        tmp.append(0)\n    else:\n        tmp.append(1)\ndf_multi['any_class'] = tmp\n\ntmp = []\nfor i in range(len(df_multi)):\n    if df_multi['Loc_Defect_1'][i]=='0':\n        tmp.append(0)\n    else:\n        tmp.append(1)\ndf_multi['class1'] = tmp\n\ntmp = []\nfor i in range(len(df_multi)):\n    if df_multi['Loc_Defect_2'][i]=='0':\n        tmp.append(0)\n    else:\n        tmp.append(1)\ndf_multi['class2'] = tmp\n\ntmp = []\nfor i in range(len(df_multi)):\n    if df_multi['Loc_Defect_3'][i]=='0':\n        tmp.append(0)\n    else:\n        tmp.append(1)\ndf_multi['class3'] = tmp\n\ntmp = []\nfor i in range(len(df_multi)):\n    if df_multi['Loc_Defect_4'][i]=='0':\n        tmp.append(0)\n    else:\n        tmp.append(1)\ndf_multi['class4'] = tmp\n\ndf_multi.sample(n=5)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:41:02.142823Z","iopub.execute_input":"2022-07-18T09:41:02.143519Z","iopub.status.idle":"2022-07-18T09:41:02.966564Z","shell.execute_reply.started":"2022-07-18T09:41:02.143483Z","shell.execute_reply":"2022-07-18T09:41:02.965024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# For stratified sampling, stratified based on minority label priority\n# class_1 769\n# class_2 195\n# class_3 4759\n# class_4 516\ntmp = []\nfor i in range(len(df_multi)):\n    if df_multi['class2'].iloc[i]==1:\n        tmp.append(2)\n    elif df_multi['class4'].iloc[i]==1:\n        tmp.append(4)\n    elif df_multi['class1'].iloc[i]==1:\n        tmp.append(1)\n    elif df_multi['class3'].iloc[i]==1:\n        tmp.append(3)\n    else:\n        tmp.append(0)\ndf_multi['stratify']=tmp\ndf_multi.sample(n=5)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:41:02.968299Z","iopub.execute_input":"2022-07-18T09:41:02.968658Z","iopub.status.idle":"2022-07-18T09:41:03.506586Z","shell.execute_reply.started":"2022-07-18T09:41:02.968624Z","shell.execute_reply":"2022-07-18T09:41:03.505592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Conversion from int64 to float32 for training\ndf_multi['class1'] = df_multi['class1'].astype('float32')\ndf_multi['class2'] = df_multi['class2'].astype('float32')\ndf_multi['class3'] = df_multi['class3'].astype('float32')\ndf_multi['class4'] = df_multi['class4'].astype('float32')\ndf_multi['any_class'] = df_multi['any_class'].astype('float32')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:41:03.508180Z","iopub.execute_input":"2022-07-18T09:41:03.508783Z","iopub.status.idle":"2022-07-18T09:41:03.518137Z","shell.execute_reply.started":"2022-07-18T09:41:03.508724Z","shell.execute_reply":"2022-07-18T09:41:03.517207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dice_coef(y_true, y_pred, smooth=K.epsilon()):\n    \n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f * y_pred_f)\n    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n\n# For clasification\ndef recall_m(y_true, y_pred):\n  \n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) # calculates number of true positives\n    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))      # calculates number of actual positives\n    recall = true_positives / (possible_positives + K.epsilon())   # K.epsilon takes care of non-zero divisions\n    return recall\n\ndef precision_m(y_true, y_pred):\n    \n    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))  # calculates number of true positives\n    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))      # calculates number of predicted positives   \n    precision = true_positives /(predicted_positives + K.epsilon()) # K.epsilon takes care of non-zero divisions\n    return precision\n    \ndef f1_score_m(y_true, y_pred):\n    \n    precision = precision_m(y_true, y_pred)  # calls precision metric and takes the score of precision of the batch\n    recall = recall_m(y_true, y_pred)        # calls recall metric and takes the score of precision of the batch\n    return 2*((precision*recall)/(precision+recall+K.epsilon()))\n\ndependencies = {\n    'recall_m':recall_m,\n    'precision_m':precision_m,\n    'dice_coef':dice_coef,\n    'f1_score_m':f1_score_m,\n    'dice_loss':sm.losses.dice_loss\n}\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:41:03.520802Z","iopub.execute_input":"2022-07-18T09:41:03.521084Z","iopub.status.idle":"2022-07-18T09:41:03.534158Z","shell.execute_reply.started":"2022-07-18T09:41:03.521061Z","shell.execute_reply":"2022-07-18T09:41:03.533203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns =['any_class'] # any_class defines whether the image contains defect or not\n\nmtr_df, mtest_df = train_test_split( df_multi, random_state=42, test_size=0.45)\nmtr_df_atr, mtest_df_str = train_test_split( df_multi,stratify = df_multi['stratify'], random_state=42, test_size=0.65)\nprint('train_data shape:',mtr_df.shape,'test_data:',mtest_df.shape)\n\ndatagen=ImageDataGenerator(rescale=1./255.,\n                           shear_range=0.1,\n                           zoom_range=0.1,\n                           brightness_range=[0.6,1.0],\n                           rotation_range=60,\n                           horizontal_flip=True,\n                           vertical_flip=True\n                           )\ntest_gen=datagen.flow_from_dataframe(\ndataframe=mtest_df,\ndirectory=\"../input/severstal-steel-defect-detection/train_images\",\nx_col=\"ImageId\",\ny_col=columns,\nbatch_size=16,\nseed=42,\nshuffle=False,\nclass_mode=\"other\",\ntarget_size=(128,800))\n\n# Data Generator for stratified samples\ntest_gen_str=datagen.flow_from_dataframe(\ndataframe=mtest_df_str,\ndirectory=\"../input/severstal-steel-defect-detection/train_images\",\nx_col=\"ImageId\",\ny_col=columns,\nbatch_size=16,\nseed=42,\nshuffle=False,\nclass_mode=\"other\",\ntarget_size=(128,800))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:41:03.536544Z","iopub.execute_input":"2022-07-18T09:41:03.537160Z","iopub.status.idle":"2022-07-18T09:41:07.465614Z","shell.execute_reply.started":"2022-07-18T09:41:03.537122Z","shell.execute_reply":"2022-07-18T09:41:07.464646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Binary Class detection Model trained with Stratified Sampling and Dense Model","metadata":{}},{"cell_type":"code","source":"from keras.models import Model, load_model\nbinary= tf.keras.applications.inception_resnet_v2.InceptionResNetV2(include_top = False, input_shape = (128,800,3))\n\nbinary.trainable=False\n\nx=binary.output\n\nx=GlobalAveragePooling2D()(x)\nx = Dense(512, activation='relu')(x)\nx = BatchNormalization()(x)\nx = Dropout(0.2)(x)\n\nx = Dense(256, activation='relu')(x)\nx = BatchNormalization()(x)\nx = Dropout(0.2)(x)\n\nx = Dense(128, activation='relu')(x)\nx = BatchNormalization()(x)\nx = Dropout(0.1)(x)\n\nx = Dense(64, activation='relu')(x)\n\n# and the prediction layer\nout = Dense(1, activation='sigmoid')(x)\nmodel_binary_1=tf.keras.Model(inputs=binary.input,outputs=out)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:41:07.467115Z","iopub.execute_input":"2022-07-18T09:41:07.467466Z","iopub.status.idle":"2022-07-18T09:41:14.125727Z","shell.execute_reply.started":"2022-07-18T09:41:07.467431Z","shell.execute_reply":"2022-07-18T09:41:14.124782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_binary_1.compile(optimizer='Adam', loss='binary_crossentropy',metrics=['acc',f1_score_m,precision_m,recall_m])\nmodel_binary_1.load_weights('../input/inception-binary-class-stratify-128/Inception_Binary_Class_Run_10_Stratify.h5')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:41:14.127152Z","iopub.execute_input":"2022-07-18T09:41:14.127516Z","iopub.status.idle":"2022-07-18T09:41:17.568199Z","shell.execute_reply.started":"2022-07-18T09:41:14.127480Z","shell.execute_reply":"2022-07-18T09:41:17.567143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# With using stratified samples","metadata":{}},{"cell_type":"code","source":"# preds=model_binary.predict_generator(test_gen,verbose=1)\neval1_str=model_binary_1.evaluate_generator(test_gen_str,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:41:17.573566Z","iopub.execute_input":"2022-07-18T09:41:17.576208Z","iopub.status.idle":"2022-07-18T09:46:43.185215Z","shell.execute_reply.started":"2022-07-18T09:41:17.576165Z","shell.execute_reply":"2022-07-18T09:46:43.183985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/models-plots-tables/history_Inception_BinaryClass_Stratify_final.csv')\n\nplt.subplots(figsize=(20, 5))\nplt.suptitle('Binary Class with Stratify',size=20)\nplt.subplot(1, 3, 1)  # row 1, column 2, count 1\nplt.plot(df['epoch'], df['acc'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_acc'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Accuracy VS Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n\nplt.subplot(1, 3, 2)\nplt.plot(df['epoch'], df['loss'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['f1_score_m'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Loss & F1-Score')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n\nplt.subplot(1, 3, 3)\nplt.plot(df['epoch'], df['val_loss'], 'r',linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_f1_score_m'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Validation Loss & Validation F1-Score')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:54:47.661700Z","iopub.execute_input":"2022-07-18T10:54:47.662655Z","iopub.status.idle":"2022-07-18T10:54:48.641347Z","shell.execute_reply.started":"2022-07-18T10:54:47.662618Z","shell.execute_reply":"2022-07-18T10:54:48.640429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Binary Class detection Model without Stratified Sampling","metadata":{}},{"cell_type":"code","source":"binary= tf.keras.applications.inception_resnet_v2.InceptionResNetV2(include_top = False, input_shape = (128,800,3))\nbinary.trainable=False\nx=binary.output\n\nx=GlobalAveragePooling2D()(x) \nx=Dense(128,activation='relu')(x)\nx=Dense(64,activation='relu')(x) \nout=Dense(1,activation='sigmoid')(x)\n\nmodel_binary_2=tf.keras.Model(inputs=binary.input,outputs=out)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:46:43.186945Z","iopub.execute_input":"2022-07-18T09:46:43.187375Z","iopub.status.idle":"2022-07-18T09:46:48.345463Z","shell.execute_reply.started":"2022-07-18T09:46:43.187334Z","shell.execute_reply":"2022-07-18T09:46:48.344451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_binary_2.compile(optimizer='Adam', loss='binary_crossentropy',metrics=['acc',f1_score_m,precision_m,recall_m])\nmodel_binary_2.load_weights('../input/binary-inc/Inception_Binary_Class_Run_10_non_defect.h5')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:46:48.346987Z","iopub.execute_input":"2022-07-18T09:46:48.347318Z","iopub.status.idle":"2022-07-18T09:46:51.622960Z","shell.execute_reply.started":"2022-07-18T09:46:48.347285Z","shell.execute_reply":"2022-07-18T09:46:51.621948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# preds=model_binary.predict_generator(test_gen,verbose=1)\neval2=model_binary_2.evaluate_generator(test_gen,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:46:51.624204Z","iopub.execute_input":"2022-07-18T09:46:51.624555Z","iopub.status.idle":"2022-07-18T09:50:18.738005Z","shell.execute_reply.started":"2022-07-18T09:46:51.624519Z","shell.execute_reply":"2022-07-18T09:50:18.736988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/models-plots-tables/History_inception_binary_non_stra_epoch.csv')\n\nplt.subplots(figsize=(20, 5))\nplt.suptitle('Binary Class with Non-Stratify',size=20)\nplt.subplot(1, 3, 1)  # row 1, column 2, count 1\nplt.plot(df['epoch'], df['acc'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_acc'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Accuracy VS Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n\nplt.subplot(1, 3, 2)\nplt.plot(df['epoch'], df['loss'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['f1_score_m'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Loss & F1-Score')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n\nplt.subplot(1, 3, 3)\nplt.plot(df['epoch'], df['val_loss'], 'r',linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_f1_score_m'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Validation Loss & Validation F1-Score')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:55:30.686117Z","iopub.execute_input":"2022-07-18T10:55:30.686825Z","iopub.status.idle":"2022-07-18T10:55:31.087566Z","shell.execute_reply.started":"2022-07-18T10:55:30.686783Z","shell.execute_reply":"2022-07-18T10:55:31.086673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To transfer data for further process\npreds=model_binary_2.predict_generator(test_gen,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:50:18.741668Z","iopub.execute_input":"2022-07-18T09:50:18.742439Z","iopub.status.idle":"2022-07-18T09:53:22.054613Z","shell.execute_reply.started":"2022-07-18T09:50:18.742410Z","shell.execute_reply":"2022-07-18T09:53:22.053648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = (preds > 0.5).astype(np.float32)\nbinary_result = np.column_stack([mtest_df, preds])\n\n# Convert to Binary Classification to new Dataframe for further analysis\nbinary_result=pd.DataFrame(binary_result,columns =['ImageId','Loc_Defect_1','Loc_Defect_2','Loc_Defect_3','Loc_Defect_4','any_class','class1','class2','class3','class4','stratify','Binary_prediction'])","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:53:22.056352Z","iopub.execute_input":"2022-07-18T09:53:22.056855Z","iopub.status.idle":"2022-07-18T09:53:22.069392Z","shell.execute_reply.started":"2022-07-18T09:53:22.056816Z","shell.execute_reply":"2022-07-18T09:53:22.068444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Analysis of Binary Classification\nimage_with_defect_correct=[]\nimage_with_non_defect_correct=[]\nimage_incorrect=[]\nfor ind in binary_result.index:\n    if (binary_result['Binary_prediction'].iloc[ind] == 1.0):\n        if (binary_result['any_class'].iloc[ind] == 1.0):\n            image_with_defect_correct.append(binary_result['ImageId'].iloc[ind])\n        else:\n            image_incorrect.append(binary_result['ImageId'].iloc[ind])\n    if (binary_result['Binary_prediction'].iloc[ind] == 0.0):\n        if (binary_result['any_class'].iloc[ind] == 0.0):\n            image_with_non_defect_correct.append(binary_result['ImageId'].iloc[ind])\n        else:\n            image_incorrect.append(binary_result['ImageId'].iloc[ind])\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:53:22.070835Z","iopub.execute_input":"2022-07-18T09:53:22.071948Z","iopub.status.idle":"2022-07-18T09:53:22.312057Z","shell.execute_reply.started":"2022-07-18T09:53:22.071910Z","shell.execute_reply":"2022-07-18T09:53:22.311132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"binary_result = binary_result[binary_result['ImageId'].isin(image_with_defect_correct)]\n# Conversion from int64 to float32 for Training and Testing\nbinary_result['class1'] = binary_result['class1'].astype('float32')\nbinary_result['class2'] = binary_result['class2'].astype('float32')\nbinary_result['class3'] = binary_result['class3'].astype('float32')\nbinary_result['class4'] = binary_result['class4'].astype('float32')\nbinary_result['any_class'] = binary_result['any_class'].astype('float32')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:53:22.313353Z","iopub.execute_input":"2022-07-18T09:53:22.313682Z","iopub.status.idle":"2022-07-18T09:53:22.332197Z","shell.execute_reply.started":"2022-07-18T09:53:22.313647Z","shell.execute_reply":"2022-07-18T09:53:22.331331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Multi Class","metadata":{}},{"cell_type":"code","source":"columns =['class1','class2','class3','class4']\n\nmtr_df, mval_df = train_test_split( binary_result,random_state=4205, test_size=0.85)\nmtr_df_str, mval_df_str = train_test_split( binary_result,stratify = binary_result['stratify'], random_state=4205, test_size=0.95)\n\nprint('df_multi shape:',mtr_df.shape,'val_data:',mval_df.shape)\n\ndatagen=ImageDataGenerator(rescale=1./255.,\n                           brightness_range=[0.7,1.0],\n                           rotation_range=50,\n                           horizontal_flip=True,\n                           vertical_flip=True\n                           )\n# Convert Image from original size of (1600,256) to (299,299)\n\nval_gen=datagen.flow_from_dataframe(\ndataframe=mval_df,\ndirectory=\"../input/severstal-steel-defect-detection/train_images\",\nx_col=\"ImageId\",\ny_col=columns,\nbatch_size=16,\nseed=42,\nshuffle=False,\nclass_mode=\"other\",\ntarget_size=(299,299))\n\n# Data Generator after Stratified Sampling\nval_gen_str=datagen.flow_from_dataframe(\ndataframe=mval_df_str,\ndirectory=\"../input/severstal-steel-defect-detection/train_images\",\nx_col=\"ImageId\",\ny_col=columns,\nbatch_size=16,\nseed=42,\nshuffle=False,\nclass_mode=\"other\",\ntarget_size=(299,299))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:26:00.897751Z","iopub.execute_input":"2022-07-18T10:26:00.898460Z","iopub.status.idle":"2022-07-18T10:26:04.220624Z","shell.execute_reply.started":"2022-07-18T10:26:00.898423Z","shell.execute_reply":"2022-07-18T10:26:04.219560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Common Xception model for both Stratifed and Non-Stratified based sampling training","metadata":{}},{"cell_type":"code","source":"from keras.models import Model, load_model\nBaseModel = keras.applications.xception.Xception(include_top = False, input_shape = (299,299,3))\nBaseModel.trainable=False\n\nmodel=Sequential()\nmodel.add(BaseModel)\nmodel.add(Dropout(0.5))\nmodel.add(Flatten())\nmodel.add(Dense(512,activation=\"relu\"))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(128,activation=\"relu\"))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(256,activation=\"relu\"))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(128,activation=\"relu\"))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(64,activation=\"relu\"))\nmodel.add(Dense(4,activation=\"sigmoid\")) \n\nmodel_m_1=model","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:53:23.306684Z","iopub.execute_input":"2022-07-18T09:53:23.307373Z","iopub.status.idle":"2022-07-18T09:53:25.187638Z","shell.execute_reply.started":"2022-07-18T09:53:23.307332Z","shell.execute_reply":"2022-07-18T09:53:25.186674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lrd = ReduceLROnPlateau(monitor = 'val_loss',patience = 20,verbose = 1,factor = 0.50, min_lr = 1e-10)\nes = EarlyStopping(verbose=1, patience=5)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:53:25.189801Z","iopub.execute_input":"2022-07-18T09:53:25.190485Z","iopub.status.idle":"2022-07-18T09:53:25.195608Z","shell.execute_reply.started":"2022-07-18T09:53:25.190448Z","shell.execute_reply":"2022-07-18T09:53:25.194706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Non Stratified based model","metadata":{}},{"cell_type":"code","source":"# Adding Best Model weights for Multi_lable Classifier (Non-Stratified)\nmodel_m_1.load_weights('../input/xception-multi-80per-type2/Xception_Multi_Class_Run2_5_non_defect_299_299_type2.h5')\nmodel_m_1.compile(optimizer='Adam', loss='binary_crossentropy',metrics=['acc',f1_score_m,precision_m,recall_m])","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:53:25.202398Z","iopub.execute_input":"2022-07-18T09:53:25.202689Z","iopub.status.idle":"2022-07-18T09:53:29.687635Z","shell.execute_reply.started":"2022-07-18T09:53:25.202665Z","shell.execute_reply":"2022-07-18T09:53:29.686670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/models-plots-tables/history_Xcpetion_MultiClass_non_defect_299_299_type2_final.csv')\n\nplt.subplots(figsize=(20, 5))\nplt.suptitle('Multi Class with Non-Stratify',size=20)\nplt.subplot(1, 3, 1)  # row 1, column 2, count 1\nplt.plot(df['epoch'], df['acc'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_acc'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Accuracy VS Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n\nplt.subplot(1, 3, 2)\nplt.plot(df['epoch'], df['loss'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['f1_score_m'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Loss & F1-Score')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n\nplt.subplot(1, 3, 3)\nplt.plot(df['epoch'], df['val_loss'], 'r',linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_f1_score_m'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Validation Loss & Validation F1-Score')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:56:12.313618Z","iopub.execute_input":"2022-07-18T10:56:12.314315Z","iopub.status.idle":"2022-07-18T10:56:12.739289Z","shell.execute_reply.started":"2022-07-18T10:56:12.314276Z","shell.execute_reply":"2022-07-18T10:56:12.738400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get Multi_label Defect Predictions (Non-Stratified)\neval_m_non_str=model_m_1.evaluate_generator(val_gen,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:53:29.689188Z","iopub.execute_input":"2022-07-18T09:53:29.689525Z","iopub.status.idle":"2022-07-18T09:54:36.411125Z","shell.execute_reply.started":"2022-07-18T09:53:29.689490Z","shell.execute_reply":"2022-07-18T09:54:36.410213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Stratified based model","metadata":{}},{"cell_type":"code","source":"# Adding Best Model weights for Multi_lable Classifier (Stratified)\nmodel_m_1.load_weights('../input/xception-multi-class-run3-10-stratify-299-lab-a70/Xception_Multi_Class_Run3_10_Stratify_299_Label_above70.h5')\nmodel_m_1.compile(optimizer='Adam', loss='binary_crossentropy',metrics=['acc',f1_score_m,precision_m,recall_m])","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:54:36.412876Z","iopub.execute_input":"2022-07-18T09:54:36.413219Z","iopub.status.idle":"2022-07-18T09:54:40.761018Z","shell.execute_reply.started":"2022-07-18T09:54:36.413183Z","shell.execute_reply":"2022-07-18T09:54:40.760048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/models-plots-tables/history_Xception_Multi_Class_Stratify_299_Label_final_csv.csv')\n\nplt.subplots(figsize=(20, 5))\nplt.suptitle('Multi Class with Stratify',size=20)\nplt.subplot(1, 3, 1)  # row 1, column 2, count 1\nplt.plot(df['epoch'], df['acc'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_acc'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Accuracy VS Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n\nplt.subplot(1, 3, 2)\nplt.plot(df['epoch'], df['loss'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['f1_score_m'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Loss & F1-Score')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n\nplt.subplot(1, 3, 3)\nplt.plot(df['epoch'], df['val_loss'], 'r',linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_f1_score_m'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Validation Loss & Validation F1-Score')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:56:40.270882Z","iopub.execute_input":"2022-07-18T10:56:40.271254Z","iopub.status.idle":"2022-07-18T10:56:40.680775Z","shell.execute_reply.started":"2022-07-18T10:56:40.271223Z","shell.execute_reply":"2022-07-18T10:56:40.679697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get Multi_label Defect Predictions (Stratified)\neval_m_str=model_m_1.evaluate_generator(val_gen_str,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:54:40.762449Z","iopub.execute_input":"2022-07-18T09:54:40.762792Z","iopub.status.idle":"2022-07-18T09:56:04.320462Z","shell.execute_reply.started":"2022-07-18T09:54:40.762743Z","shell.execute_reply":"2022-07-18T09:56:04.319449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_m=model_m_1.predict_generator(val_gen_str,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:26:07.026787Z","iopub.execute_input":"2022-07-18T10:26:07.027852Z","iopub.status.idle":"2022-07-18T10:27:16.175338Z","shell.execute_reply.started":"2022-07-18T10:26:07.027803Z","shell.execute_reply":"2022-07-18T10:27:16.174397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Getting data for Segmentation","metadata":{}},{"cell_type":"code","source":"# Adding defects to new Dataframe\n# Round off the predictions\npreds_tester=preds_m.tolist()\nmulti_class_result=mval_df_str.copy()\nmulti_class_result.insert(12, \"pred_Multi_class_labels\",preds_tester)\nfor ind in multi_class_result.index:\n    multi_class_result['pred_Multi_class_labels'][ind] = [ round(elem, 2) for elem in multi_class_result['pred_Multi_class_labels'][ind] ]\n\n# Adding a True_label class for Jaccard score\nlist_true_class=[]\n\nfor ind in multi_class_result.index:\n    temp_list=[]\n    temp_list.append(int(multi_class_result['class1'][ind]))\n    temp_list.append(int(multi_class_result['class2'][ind]))\n    temp_list.append(int(multi_class_result['class3'][ind]))\n    temp_list.append(int(multi_class_result['class4'][ind]))\n    list_true_class.append(temp_list)\n    \nmulti_class_result.insert(13, \"true_Multi_class_labels\", list_true_class)\nsum_vals=[sum(x) for x in zip(*multi_class_result['pred_Multi_class_labels'])]\nthres=[round(x / float(len(multi_class_result)),2) for x in sum_vals]\n\nlist_pred_class_hamming=[]\n\nfor ind in multi_class_result.index:\n    temp_list=[]\n    if multi_class_result['pred_Multi_class_labels'][ind][0] > thres[0]:\n        temp_list.append(int(1))\n    else:\n        temp_list.append(int(0))\n    if multi_class_result['pred_Multi_class_labels'][ind][1] > thres[1]:\n        temp_list.append(int(1))\n    else:\n        temp_list.append(int(0))\n    if multi_class_result['pred_Multi_class_labels'][ind][2] > thres[2]:\n        temp_list.append(int(1))\n    else:\n        temp_list.append(int(0))\n    if multi_class_result['pred_Multi_class_labels'][ind][3] > thres[3]:\n        temp_list.append(int(1))\n    else:\n        temp_list.append(int(0))\n\n    list_pred_class_hamming.append(temp_list)\n    \nmulti_class_result.insert(14, \"pred_Multi_class_labels_hamming\", list_pred_class_hamming)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:28:34.701464Z","iopub.execute_input":"2022-07-18T10:28:34.701827Z","iopub.status.idle":"2022-07-18T10:28:36.138233Z","shell.execute_reply.started":"2022-07-18T10:28:34.701794Z","shell.execute_reply":"2022-07-18T10:28:36.136950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"segment_data = pd.read_csv('../input/severstal-steel-defect-detection/train.csv')\ntrain1=pd.DataFrame(segment_data)\ndf_segment=pd.DataFrame({'ImageId':segment_data['ImageId'][::],'e1':'','e2':'','e3':'','e4':''})\nfor i in range(0,train1['ClassId'].size):\n    if train1['ClassId'][i] == 1:\n        df_segment['e1'][i] = train1['EncodedPixels'][i]\n    elif train1['ClassId'][i] == 2:\n        df_segment['e2'][i] = train1['EncodedPixels'][i]\n    elif train1['ClassId'][i] == 3:\n        df_segment['e3'][i] = train1['EncodedPixels'][i]\n    elif train1['ClassId'][i] == 4:\n        df_segment['e4'][i] = train1['EncodedPixels'][i]\n        \nImages_list_for_segment=[]\n\nfor ind in multi_class_result.index:\n    if multi_class_result['pred_Multi_class_labels_hamming'][ind][0] == 1 & multi_class_result['true_Multi_class_labels'][ind][0] == 1:\n        Images_list_for_segment.append(multi_class_result['ImageId'][ind])\n        \n    elif multi_class_result['pred_Multi_class_labels_hamming'][ind][1] == 1 & multi_class_result['true_Multi_class_labels'][ind][1] == 1:\n        Images_list_for_segment.append(multi_class_result['ImageId'][ind])\n        \n    elif multi_class_result['pred_Multi_class_labels_hamming'][ind][2] == 1 & multi_class_result['true_Multi_class_labels'][ind][2] == 1:\n        Images_list_for_segment.append(multi_class_result['ImageId'][ind])\n        \n    elif multi_class_result['pred_Multi_class_labels_hamming'][ind][3] == 1 & multi_class_result['true_Multi_class_labels'][ind][3] == 1:\n        Images_list_for_segment.append(multi_class_result['ImageId'][ind])\n    else:\n        continue\n\nprint('Images ready for Segementation ',len(Images_list_for_segment))\nSegment_result = df_segment[df_segment['ImageId'].isin(Images_list_for_segment)]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:28:36.140070Z","iopub.execute_input":"2022-07-18T10:28:36.140390Z","iopub.status.idle":"2022-07-18T10:28:37.439981Z","shell.execute_reply.started":"2022-07-18T10:28:36.140354Z","shell.execute_reply":"2022-07-18T10:28:37.438340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import backend as K\nclass DataGenerator(tf.keras.utils.Sequence):\n    def __init__(self, df, batch_size = 16, subset=\"train\", shuffle=False, \n                 preprocess=None, info={}):\n        super().__init__()\n        self.df = df\n        self.shuffle = shuffle\n        self.subset = subset\n        self.batch_size = batch_size\n        self.preprocess = preprocess\n        self.info = info\n        \n        if self.subset == \"train\":\n            self.data_path = path + '/'\n        elif self.subset == \"test\":\n            self.data_path = path + '/test_images/'\n        self.on_epoch_end()\n\n    def __len__(self):\n        return int(np.floor(len(self.df) / self.batch_size))\n    \n    def on_epoch_end(self):\n        self.indexes = np.arange(len(self.df))\n        if self.shuffle == True:\n            np.random.shuffle(self.indexes)\n    \n    def __getitem__(self, index): \n        X = np.empty((self.batch_size,128,800,3),dtype=np.float32)\n        y = np.empty((self.batch_size,128,800,4),dtype=np.int8)\n        indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]\n        for i,f in enumerate(self.df['ImageId'].iloc[indexes]):\n            self.info[index*self.batch_size+i]=f\n            X[i,] = Image.open(self.data_path + f).resize((800,128))\n            if self.subset == 'train': \n                for j in range(4):\n                    y[i,:,:,j] = rle2maskResize(self.df['e'+str(j+1)].iloc[indexes[i]])\n        if self.preprocess!=None: X = self.preprocess(X)\n        if self.subset == 'train': return X, y\n        else: return X\n\ndef dice_coef(y_true, y_pred, smooth=1):\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f * y_pred_f)\n    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:28:46.897304Z","iopub.execute_input":"2022-07-18T10:28:46.897642Z","iopub.status.idle":"2022-07-18T10:28:46.914011Z","shell.execute_reply.started":"2022-07-18T10:28:46.897612Z","shell.execute_reply":"2022-07-18T10:28:46.912708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# https://www.kaggle.com/titericz/building-and-visualizing-masks\ndef rle2maskResize(rle):\n    # CONVERT RLE TO MASK \n    if (pd.isnull(rle))|(rle==''): \n        return np.zeros((128,800) ,dtype=np.uint8)\n    \n    height= 256\n    width = 1600\n    mask= np.zeros( width*height ,dtype=np.uint8)\n\n    array = np.asarray([int(x) for x in rle.split()])\n    starts = array[0::2]-1\n    lengths = array[1::2]    \n    for index, start in enumerate(starts):\n        mask[int(start):int(start+lengths[index])] = 1\n    \n    return mask.reshape( (height,width), order='F' )[::2,::2]\n\ndef mask2contour(mask, width=3):\n    # CONVERT MASK TO ITS CONTOUR\n    w = mask.shape[1]\n    h = mask.shape[0]\n    mask2 = np.concatenate([mask[:,width:],np.zeros((h,width))],axis=1)\n    mask2 = np.logical_xor(mask,mask2)\n    mask3 = np.concatenate([mask[width:,:],np.zeros((width,w))],axis=0)\n    mask3 = np.logical_xor(mask,mask3)\n    return np.logical_or(mask2,mask3) \n\ndef mask2pad(mask, pad=2):\n    # ENLARGE MASK TO INCLUDE MORE SPACE AROUND DEFECT\n    w = mask.shape[1]\n    h = mask.shape[0]\n    \n    # MASK UP\n    for k in range(1,pad,2):\n        temp = np.concatenate([mask[k:,:],np.zeros((k,w))],axis=0)\n        mask = np.logical_or(mask,temp)\n    # MASK DOWN\n    for k in range(1,pad,2):\n        temp = np.concatenate([np.zeros((k,w)),mask[:-k,:]],axis=0)\n        mask = np.logical_or(mask,temp)\n    # MASK LEFT\n    for k in range(1,pad,2):\n        temp = np.concatenate([mask[:,k:],np.zeros((h,k))],axis=1)\n        mask = np.logical_or(mask,temp)\n    # MASK RIGHT\n    for k in range(1,pad,2):\n        temp = np.concatenate([np.zeros((h,k)),mask[:,:-k]],axis=1)\n        mask = np.logical_or(mask,temp)\n    \n    return mask ","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:28:47.346866Z","iopub.execute_input":"2022-07-18T10:28:47.347685Z","iopub.status.idle":"2022-07-18T10:28:47.363721Z","shell.execute_reply.started":"2022-07-18T10:28:47.347641Z","shell.execute_reply":"2022-07-18T10:28:47.362652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from segmentation_models import Unet\nfrom segmentation_models import get_preprocessing\nimport segmentation_models as sm\nsm.set_framework('tf.keras')\nsm.framework()\n\n# LOAD UNET WITH PRETRAINING FROM IMAGENET\npreprocess = get_preprocessing('resnet50') # for resnet, img = (img-110.0)/1.0\nmodel_all = Unet('resnet50', input_shape=(128, 800, 3), classes=4, activation='sigmoid')\nmodel_all.compile(optimizer='SGD', loss='binary_crossentropy', metrics=[dice_coef])\nmodel_all.load_weights('../input/segment-all-classes/UNET_Run_60_90.h5')\n\nmodel_class1 = Unet('resnet50', input_shape=(128, 800, 3), classes=4, activation='sigmoid')\nmodel_class1.compile(optimizer='SGD', loss='binary_crossentropy', metrics=[dice_coef])\nmodel_class1.load_weights('../input/segment-all-classes/Segment_Class1_10_adam_run3_slight.h5')\n\nmodel_class2 = Unet('resnet50', input_shape=(128, 800, 3), classes=4, activation='sigmoid')\nmodel_class2.compile(optimizer='SGD', loss='binary_crossentropy', metrics=[dice_coef])\nmodel_class2.load_weights('../input/segment-all-classes/Segment_Class2_20_actual_adam_run3.h5')\n\nmodel_class3 = Unet('resnet50', input_shape=(128, 800, 3), classes=4, activation='sigmoid')\nmodel_class3.compile(optimizer='SGD', loss='binary_crossentropy', metrics=[dice_coef])\nmodel_class3.load_weights('../input/segment-all-classes/Segment_Class3_10_adam_run2.h5')\n\nmodel_class4 = Unet('resnet50', input_shape=(128, 800, 3), classes=4, activation='sigmoid')\nmodel_class4.compile(optimizer='SGD', loss='binary_crossentropy', metrics=[dice_coef])\nmodel_class4.load_weights('../input/segment-all-classes/Segment_Class4_20_adam_run2.h5')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:28:47.807123Z","iopub.execute_input":"2022-07-18T10:28:47.807442Z","iopub.status.idle":"2022-07-18T10:29:02.885633Z","shell.execute_reply.started":"2022-07-18T10:28:47.807415Z","shell.execute_reply":"2022-07-18T10:29:02.884595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create batch with only class\ndefects_1 = list(Segment_result[Segment_result['e1']!=''].index)\nbatch_for_defect1=Segment_result[Segment_result.index.isin(defects_1)]\n\ndefects_2 = list(Segment_result[Segment_result['e2']!=''].index)\nbatch_for_defect2=Segment_result[Segment_result.index.isin(defects_2)]\n\ndefects_3 = list(Segment_result[Segment_result['e3']!=''].index)\nbatch_for_defect3=Segment_result[Segment_result.index.isin(defects_3)]\n\ndefects_4 = list(Segment_result[Segment_result['e4']!=''].index)\nbatch_for_defect4=Segment_result[Segment_result.index.isin(defects_4)]\n\ndefects_all = list(Segment_result.index)\nbatch_for_defect_all=Segment_result[Segment_result.index.isin(defects_all)]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:29:02.889735Z","iopub.execute_input":"2022-07-18T10:29:02.890037Z","iopub.status.idle":"2022-07-18T10:29:02.908571Z","shell.execute_reply.started":"2022-07-18T10:29:02.890011Z","shell.execute_reply":"2022-07-18T10:29:02.907610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_batches_1 = DataGenerator(batch_for_defect1.iloc[:80],preprocess=preprocess)\nprint(\"Segmentation for Class 1\")\nsegment_eval_1 = model_class1.evaluate_generator(valid_batches_1,verbose=1)\n\nvalid_batches_2 = DataGenerator(batch_for_defect2.iloc[:80],preprocess=preprocess)\nprint(\"Segmentation for Class 2\")\nsegment_eval_2 = model_class2.evaluate_generator(valid_batches_2,verbose=1)\n\nvalid_batches_3 = DataGenerator(batch_for_defect3.iloc[:80],preprocess=preprocess)\nprint(\"Segmentation for Class 3\")\nsegment_eval_3 = model_class3.evaluate_generator(valid_batches_3,verbose=1)\n\nvalid_batches_4 = DataGenerator(batch_for_defect4.iloc[:80],preprocess=preprocess)\nprint(\"Segmentation for Class 4\")\nsegment_eval_4 = model_class4.evaluate_generator(valid_batches_4,verbose=1)\n\nvalid_batches_all = DataGenerator(batch_for_defect_all.iloc[:80],preprocess=preprocess)\nprint(\"Segmentation for Class all\")\nsegment_eval_all = model_all.evaluate_generator(valid_batches_all,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:29:51.963618Z","iopub.execute_input":"2022-07-18T10:29:51.964759Z","iopub.status.idle":"2022-07-18T10:30:01.891236Z","shell.execute_reply.started":"2022-07-18T10:29:51.964697Z","shell.execute_reply":"2022-07-18T10:30:01.890254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/models-plots-tables/History_all4_90_epochs.csv')\n\nplt.subplots(figsize=(20, 5))\nplt.suptitle('Segment All Class',size=20)\nplt.subplot(1, 2, 1)  # row 1, column 2, count 1\nplt.plot(df['epoch'], df['dice_coef'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_dice_coef'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Dice-Coef. VS Validation Dice-Coef.')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n\nplt.subplot(1, 2, 2)\nplt.plot(df['epoch'], df['loss'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_loss'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Loss & Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:57:16.972383Z","iopub.execute_input":"2022-07-18T10:57:16.973521Z","iopub.status.idle":"2022-07-18T10:57:17.298238Z","shell.execute_reply.started":"2022-07-18T10:57:16.973473Z","shell.execute_reply":"2022-07-18T10:57:17.294838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/models-plots-tables/Segment_class1_epoch40.csv')\n\nplt.subplots(figsize=(20, 5))\nplt.suptitle('Segment Class-1',size=20)\nplt.subplot(1, 2, 1)  # row 1, column 2, count 1\nplt.plot(df['epoch'], df['dice_coef'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_dice_coef'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Dice-Coef. VS Validation Dice-Coef.')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n\nplt.subplot(1, 2, 2)\nplt.plot(df['epoch'], df['loss'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_loss'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Loss & Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:57:24.690289Z","iopub.execute_input":"2022-07-18T10:57:24.690635Z","iopub.status.idle":"2022-07-18T10:57:25.004594Z","shell.execute_reply.started":"2022-07-18T10:57:24.690606Z","shell.execute_reply":"2022-07-18T10:57:25.003651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/models-plots-tables/Segment_class2_adam_epoch60_csv.csv')\n\nplt.subplots(figsize=(20, 5))\nplt.suptitle('Segment Class-2',size=20)\nplt.subplot(1, 2, 1)  # row 1, column 2, count 1\nplt.plot(df['epoch'], df['dice_coef'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_dice_coef'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Dice-Coef. VS Validation Dice-Coef.')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n\nplt.subplot(1, 2, 2)\nplt.plot(df['epoch'], df['loss'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_loss'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Loss & Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:57:33.039436Z","iopub.execute_input":"2022-07-18T10:57:33.039813Z","iopub.status.idle":"2022-07-18T10:57:33.348659Z","shell.execute_reply.started":"2022-07-18T10:57:33.039774Z","shell.execute_reply":"2022-07-18T10:57:33.347676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/models-plots-tables/Segment_class3_adam_epoch30.csv')\n\nplt.subplots(figsize=(20, 5))\nplt.suptitle('Segment Class-3',size=20)\nplt.subplot(1, 2, 1)  # row 1, column 2, count 1\nplt.plot(df['epoch'], df['dice_coef'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_dice_coef'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Dice-Coef. VS Validation Dice-Coef.')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n\nplt.subplot(1, 2, 2)\nplt.plot(df['epoch'], df['loss'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_loss'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Loss & Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:57:39.458439Z","iopub.execute_input":"2022-07-18T10:57:39.459018Z","iopub.status.idle":"2022-07-18T10:57:39.812593Z","shell.execute_reply.started":"2022-07-18T10:57:39.458975Z","shell.execute_reply":"2022-07-18T10:57:39.811698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/models-plots-tables/Segment_class4_adam_epoch30_csv.csv')\n\nplt.subplots(figsize=(20, 5))\nplt.suptitle('Segment Class-4',size=20)\nplt.subplot(1, 2, 1)  # row 1, column 2, count 1\nplt.plot(df['epoch'], df['dice_coef'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_dice_coef'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Dice-Coef. VS Validation Dice-Coef.')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n\nplt.subplot(1, 2, 2)\nplt.plot(df['epoch'], df['loss'], 'r', linewidth=2, linestyle='solid')\nplt.plot(df['epoch'], df['val_loss'], 'b', linewidth=2, linestyle='dashed')\nplt.title('Loss & Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Value')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:57:45.716115Z","iopub.execute_input":"2022-07-18T10:57:45.716470Z","iopub.status.idle":"2022-07-18T10:57:46.018436Z","shell.execute_reply.started":"2022-07-18T10:57:45.716439Z","shell.execute_reply":"2022-07-18T10:57:46.017520Z"},"trusted":true},"execution_count":null,"outputs":[]}]}