{"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-17T09:35:36.578132Z","iopub.execute_input":"2022-07-17T09:35:36.578813Z","iopub.status.idle":"2022-07-17T09:35:45.314137Z","shell.execute_reply.started":"2022-07-17T09:35:36.578686Z","shell.execute_reply":"2022-07-17T09:35:45.312596Z"},"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\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\nfrom sklearn.model_selection import train_test_split\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\nfrom sklearn.metrics import recall_score\nfrom random import random\nfrom random import seed\n!git clone https://github.com/qubvel/segmentation_models\n! pip install segmentation-models\nimport segmentation_models\nprint(segmentation_models.__version__)\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-17T09:35:45.317770Z","iopub.execute_input":"2022-07-17T09:35:45.318102Z","iopub.status.idle":"2022-07-17T09:36:05.544508Z","shell.execute_reply.started":"2022-07-17T09:35:45.318071Z","shell.execute_reply":"2022-07-17T09:36:05.543004Z"},"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-17T09:36:05.546595Z","iopub.execute_input":"2022-07-17T09:36:05.547487Z","iopub.status.idle":"2022-07-17T09:36:05.734876Z","shell.execute_reply.started":"2022-07-17T09:36:05.547437Z","shell.execute_reply":"2022-07-17T09:36:05.733548Z"},"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-17T09:36:05.738904Z","iopub.execute_input":"2022-07-17T09:36:05.739364Z","iopub.status.idle":"2022-07-17T09:36:20.243545Z","shell.execute_reply.started":"2022-07-17T09:36:05.739317Z","shell.execute_reply":"2022-07-17T09:36:20.242042Z"},"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-17T09:36:20.245843Z","iopub.execute_input":"2022-07-17T09:36:20.246722Z","iopub.status.idle":"2022-07-17T09:40:10.488401Z","shell.execute_reply.started":"2022-07-17T09:36:20.246678Z","shell.execute_reply":"2022-07-17T09:40:10.487041Z"},"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-17T09:40:10.491724Z","iopub.execute_input":"2022-07-17T09:40:10.492619Z","iopub.status.idle":"2022-07-17T09:42:55.633428Z","shell.execute_reply.started":"2022-07-17T09:40:10.492571Z","shell.execute_reply":"2022-07-17T09:42:55.632078Z"},"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-17T09:42:55.635313Z","iopub.execute_input":"2022-07-17T09:42:55.635777Z","iopub.status.idle":"2022-07-17T09:42:56.247394Z","shell.execute_reply.started":"2022-07-17T09:42:55.635735Z","shell.execute_reply":"2022-07-17T09:42:56.246033Z"},"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-17T09:42:56.249353Z","iopub.execute_input":"2022-07-17T09:42:56.249894Z","iopub.status.idle":"2022-07-17T09:42:56.522276Z","shell.execute_reply.started":"2022-07-17T09:42:56.249853Z","shell.execute_reply":"2022-07-17T09:42:56.520971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pf=pd.DataFrame({'index':['class1_Count','class2_Count','class3_Count','class4_Count'],'class_count':[multi_class.count([1]),multi_class.count([2]),multi_class.count([3]),multi_class.count([4])]},index=['class_1','class_2','class_3','class_4'])\nplt.bar(pf.index, pf.class_count, color ='maroon',width = 0.4)\n\nprint('Majority class is Class_3 with',pf['class_count'].max(),'Data points')\nprint('Minority class is Class_2 with',pf['class_count'].min(),'Data points')\n\npf","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:42:56.524163Z","iopub.execute_input":"2022-07-17T09:42:56.524526Z","iopub.status.idle":"2022-07-17T09:42:56.711944Z","shell.execute_reply.started":"2022-07-17T09:42:56.524498Z","shell.execute_reply":"2022-07-17T09:42:56.710567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from prettytable import PrettyTable\ntable=PrettyTable()\ntable.field_names =[\"Type\", \"No.of.classes\",\"Class Id's\",\"Class Data\",'Total_Data','~% of Data']\ntable.add_row(['one class',4,[[1],[2],[3],[4]],list(pf['class_count'].values),len(class_1),float(str(len(class_1)*100/(len(df)/4))[:6]) ])\nt=(multi_class.count([1,2])+multi_class.count([1,3])+multi_class.count([2,3])+multi_class.count([2,4])+multi_class.count([3,4]))\ntable.add_row(['two class',5,[[1,2],[1,3],[2,3],[2,4],[3,4]],[multi_class.count([1,2]),multi_class.count([1,3]),multi_class.count([2,3]),multi_class.count([2,4]),multi_class.count([3,4])],t,float(str((t*100/(len(df)/4)))[:6])])\ntable.add_row(['Three class',1,[[1,2,3]],len(class_3), len(class_3),float(str(len(class_3)*100/(len(df)/4))[:6])])\ntable.add_row(['four class',0,np.nan,np.nan, len(class_4),float(str(len(class_4)*100/(len(df)/4))[:6])])\nprint(table)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:42:56.717565Z","iopub.execute_input":"2022-07-17T09:42:56.718333Z","iopub.status.idle":"2022-07-17T09:42:56.737974Z","shell.execute_reply.started":"2022-07-17T09:42:56.718275Z","shell.execute_reply":"2022-07-17T09:42:56.736469Z"},"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-17T09:42:56.739687Z","iopub.execute_input":"2022-07-17T09:42:56.740876Z","iopub.status.idle":"2022-07-17T09:42:56.859876Z","shell.execute_reply.started":"2022-07-17T09:42:56.740846Z","shell.execute_reply":"2022-07-17T09:42:56.858488Z"},"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-17T09:42:56.861471Z","iopub.execute_input":"2022-07-17T09:42:56.862052Z","iopub.status.idle":"2022-07-17T09:42:57.710720Z","shell.execute_reply.started":"2022-07-17T09:42:56.862020Z","shell.execute_reply":"2022-07-17T09:42:57.709417Z"},"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-17T09:42:57.712326Z","iopub.execute_input":"2022-07-17T09:42:57.713618Z","iopub.status.idle":"2022-07-17T09:42:58.375736Z","shell.execute_reply.started":"2022-07-17T09:42:57.713574Z","shell.execute_reply":"2022-07-17T09:42:58.374398Z"},"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')","metadata":{"execution":{"iopub.status.busy":"2022-07-17T09:42:58.377870Z","iopub.execute_input":"2022-07-17T09:42:58.378378Z","iopub.status.idle":"2022-07-17T09:42:58.391530Z","shell.execute_reply.started":"2022-07-17T09:42:58.378308Z","shell.execute_reply":"2022-07-17T09:42:58.390216Z"},"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-17T09:42:58.406808Z","iopub.execute_input":"2022-07-17T09:42:58.408087Z","iopub.status.idle":"2022-07-17T09:42:58.423313Z","shell.execute_reply.started":"2022-07-17T09:42:58.408045Z","shell.execute_reply":"2022-07-17T09:42:58.421795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns =['any_class'] # any_class defines whether the image contains defect or not\n\n# mtr_df, mtest_df = train_test_split( df_multi,stratify = df_multi['stratify'], random_state=42, test_size=0.45)\nmtr_df, mtest_df = train_test_split( df_multi, random_state=42, test_size=0.45)\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))","metadata":{"execution":{"iopub.status.busy":"2022-07-17T11:01:26.373243Z","iopub.execute_input":"2022-07-17T11:01:26.373979Z","iopub.status.idle":"2022-07-17T11:01:33.336588Z","shell.execute_reply.started":"2022-07-17T11:01:26.373918Z","shell.execute_reply":"2022-07-17T11:01:33.334996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Non stratified Binary","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=tf.keras.Model(inputs=binary.input,outputs=out)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T11:17:08.033486Z","iopub.execute_input":"2022-07-17T11:17:08.033842Z","iopub.status.idle":"2022-07-17T11:17:14.313508Z","shell.execute_reply.started":"2022-07-17T11:17:08.033812Z","shell.execute_reply":"2022-07-17T11:17:14.311997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_binary.compile(optimizer='Adam', loss='binary_crossentropy',metrics=['acc',f1_score_m,precision_m,recall_m])\nmodel_binary.load_weights('../input/binary-inc/Inception_Binary_Class_Run_10_non_defect.h5')","metadata":{"execution":{"iopub.status.busy":"2022-07-17T11:17:14.316081Z","iopub.execute_input":"2022-07-17T11:17:14.316611Z","iopub.status.idle":"2022-07-17T11:17:18.275851Z","shell.execute_reply.started":"2022-07-17T11:17:14.316567Z","shell.execute_reply":"2022-07-17T11:17:18.274575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds=model_binary.predict_generator(test_gen,verbose=1)\n# eval=model_binary.evaluate_generator(test_gen,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T11:19:43.228956Z","iopub.execute_input":"2022-07-17T11:19:43.229702Z","iopub.status.idle":"2022-07-17T11:23:20.172053Z","shell.execute_reply.started":"2022-07-17T11:19:43.229655Z","shell.execute_reply":"2022-07-17T11:23:20.170691Z"},"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'])\nbinary_result.head(10)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-17T11:24:46.997105Z","iopub.execute_input":"2022-07-17T11:24:46.997508Z","iopub.status.idle":"2022-07-17T11:24:47.030959Z","shell.execute_reply.started":"2022-07-17T11:24:46.997477Z","shell.execute_reply":"2022-07-17T11:24:47.029703Z"},"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    \nprint(\"Images correctly indentifed with defect \", len(image_with_defect_correct))\nprint(\"Images correctly indentifed with non_defect \", len(image_with_non_defect_correct))\nprint(\"Images incorrectly indentifed \", len(image_incorrect))","metadata":{"execution":{"iopub.status.busy":"2022-07-17T11:39:35.342927Z","iopub.execute_input":"2022-07-17T11:39:35.343575Z","iopub.status.idle":"2022-07-17T11:39:35.873464Z","shell.execute_reply.started":"2022-07-17T11:39:35.343531Z","shell.execute_reply":"2022-07-17T11:39:35.872224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"binary_result = binary_result[binary_result['ImageId'].isin(image_with_defect_correct)]","metadata":{"execution":{"iopub.status.busy":"2022-07-17T11:39:38.738542Z","iopub.execute_input":"2022-07-17T11:39:38.739284Z","iopub.status.idle":"2022-07-17T11:39:38.748985Z","shell.execute_reply.started":"2022-07-17T11:39:38.739219Z","shell.execute_reply":"2022-07-17T11:39:38.747251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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-17T11:39:41.828219Z","iopub.execute_input":"2022-07-17T11:39:41.828993Z","iopub.status.idle":"2022-07-17T11:39:41.844628Z","shell.execute_reply.started":"2022-07-17T11:39:41.828926Z","shell.execute_reply":"2022-07-17T11:39:41.843364Z"},"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,stratify = binary_result['stratify'], random_state=4205, test_size=0.85)\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\ntrain_gen=datagen.flow_from_dataframe(\ndataframe=mtr_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)) # 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))","metadata":{"execution":{"iopub.status.busy":"2022-07-17T11:40:53.518172Z","iopub.execute_input":"2022-07-17T11:40:53.518563Z","iopub.status.idle":"2022-07-17T11:40:54.995170Z","shell.execute_reply.started":"2022-07-17T11:40:53.518531Z","shell.execute_reply":"2022-07-17T11:40:54.993661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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=model","metadata":{"execution":{"iopub.status.busy":"2022-07-17T11:41:01.090907Z","iopub.execute_input":"2022-07-17T11:41:01.091324Z","iopub.status.idle":"2022-07-17T11:41:03.101090Z","shell.execute_reply.started":"2022-07-17T11:41:01.091293Z","shell.execute_reply":"2022-07-17T11:41:03.099724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Call backs for Training \n# Reduce Learning Rate on Platue\nlrd = ReduceLROnPlateau(monitor = 'val_loss',patience = 20,verbose = 1,factor = 0.50, min_lr = 1e-10)\n# Early Stopping\nes = EarlyStopping(verbose=1, patience=5)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T11:41:04.558040Z","iopub.execute_input":"2022-07-17T11:41:04.559728Z","iopub.status.idle":"2022-07-17T11:41:04.570431Z","shell.execute_reply.started":"2022-07-17T11:41:04.559680Z","shell.execute_reply":"2022-07-17T11:41:04.568968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Adding Best Mdoel weights for Multi_lable Classifier\nmodel_m.load_weights('../input/xception-multi-class-run3-10-stratify-299-lab-a70/Xception_Multi_Class_Run3_10_Stratify_299_Label_above70.h5')\nmodel_m.compile(optimizer='Adam', loss='binary_crossentropy',metrics=['acc',f1_score_m,precision_m,recall_m])","metadata":{"execution":{"iopub.status.busy":"2022-07-17T11:41:06.021522Z","iopub.execute_input":"2022-07-17T11:41:06.021890Z","iopub.status.idle":"2022-07-17T11:41:12.087593Z","shell.execute_reply.started":"2022-07-17T11:41:06.021860Z","shell.execute_reply":"2022-07-17T11:41:12.086193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get Multi_label Defect Predictions\npreds_m=model_m.predict_generator(val_gen,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T11:41:12.091591Z","iopub.execute_input":"2022-07-17T11:41:12.092325Z","iopub.status.idle":"2022-07-17T11:42:35.243518Z","shell.execute_reply.started":"2022-07-17T11:41:12.092279Z","shell.execute_reply":"2022-07-17T11:42:35.241909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Adding defects to new Dataframe\n# Round off the predictions\npreds_tester=preds_m.tolist()\nmulti_class_result=mval_df.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] ]\nmulti_class_result.head(10)\nmulti_class_result.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T12:05:56.373324Z","iopub.execute_input":"2022-07-17T12:05:56.373744Z","iopub.status.idle":"2022-07-17T12:05:57.271644Z","shell.execute_reply.started":"2022-07-17T12:05:56.373714Z","shell.execute_reply":"2022-07-17T12:05:57.270399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 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]\nmulti_class_result.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T12:06:00.241829Z","iopub.execute_input":"2022-07-17T12:06:00.243052Z","iopub.status.idle":"2022-07-17T12:06:00.372658Z","shell.execute_reply.started":"2022-07-17T12:06:00.243019Z","shell.execute_reply":"2022-07-17T12:06:00.371347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_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)\nmulti_class_result.head(10)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T12:06:05.193347Z","iopub.execute_input":"2022-07-17T12:06:05.194536Z","iopub.status.idle":"2022-07-17T12:06:05.330522Z","shell.execute_reply.started":"2022-07-17T12:06:05.194489Z","shell.execute_reply":"2022-07-17T12:06:05.329367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true=multi_class_result[\"true_Multi_class_labels\"].copy()\ny_true=np.array([np.array(xi) for xi in y_true])\n\ny_pred=multi_class_result[\"pred_Multi_class_labels_hamming\"].copy()\ny_pred=np.array([np.array(xi) for xi in y_pred])\nmetrics.hamming_loss(y_true,y_pred)\njaccard_score(y_true, y_pred, average='samples')","metadata":{"execution":{"iopub.status.busy":"2022-07-17T12:09:29.258312Z","iopub.execute_input":"2022-07-17T12:09:29.258747Z","iopub.status.idle":"2022-07-17T12:09:29.290748Z","shell.execute_reply.started":"2022-07-17T12:09:29.258716Z","shell.execute_reply":"2022-07-17T12:09:29.289403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Segementation of Defects","metadata":{}},{"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        \ndf_segment.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T12:15:38.370522Z","iopub.execute_input":"2022-07-17T12:15:38.370918Z","iopub.status.idle":"2022-07-17T12:15:39.621780Z","shell.execute_reply.started":"2022-07-17T12:15:38.370885Z","shell.execute_reply":"2022-07-17T12:15:39.620369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Images_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-17T12:15:39.625232Z","iopub.execute_input":"2022-07-17T12:15:39.626000Z","iopub.status.idle":"2022-07-17T12:15:39.784805Z","shell.execute_reply.started":"2022-07-17T12:15:39.625949Z","shell.execute_reply":"2022-07-17T12:15:39.783399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class 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","metadata":{"execution":{"iopub.status.busy":"2022-07-17T12:15:42.049460Z","iopub.execute_input":"2022-07-17T12:15:42.049844Z","iopub.status.idle":"2022-07-17T12:15:42.067564Z","shell.execute_reply.started":"2022-07-17T12:15:42.049814Z","shell.execute_reply":"2022-07-17T12:15:42.066049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# https://www.kaggle.com/titericz/building-and-visualizing-masks\nfrom keras import backend as K\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 \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-17T12:15:44.299970Z","iopub.execute_input":"2022-07-17T12:15:44.300385Z","iopub.status.idle":"2022-07-17T12:15:44.320485Z","shell.execute_reply.started":"2022-07-17T12:15:44.300325Z","shell.execute_reply":"2022-07-17T12:15:44.319191Z"},"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-17T12:15:53.086284Z","iopub.execute_input":"2022-07-17T12:15:53.086774Z","iopub.status.idle":"2022-07-17T12:16:12.279129Z","shell.execute_reply.started":"2022-07-17T12:15:53.086741Z","shell.execute_reply":"2022-07-17T12:16:12.277728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Classwise Segementation and plot generation","metadata":{}},{"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-17T12:16:12.283007Z","iopub.execute_input":"2022-07-17T12:16:12.285505Z","iopub.status.idle":"2022-07-17T12:16:12.304164Z","shell.execute_reply.started":"2022-07-17T12:16:12.285474Z","shell.execute_reply":"2022-07-17T12:16:12.302985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_batches_plot_1 = DataGenerator(batch_for_defect1.iloc[:16],preprocess=preprocess)\nsegment_preds_plot_1 = model_class1.predict_generator(valid_batches_plot_1,verbose=1)\n\nvalid_batches_plot_2 = DataGenerator(batch_for_defect2.iloc[:16],preprocess=preprocess)\nsegment_preds_plot_2 = model_class2.predict_generator(valid_batches_plot_2,verbose=1)\n\nvalid_batches_plot_3 = DataGenerator(batch_for_defect3.iloc[:16],preprocess=preprocess)\nsegment_preds_plot_3 = model_class3.predict_generator(valid_batches_plot_3,verbose=1)\n\nvalid_batches_plot_4 = DataGenerator(batch_for_defect4.iloc[:16],preprocess=preprocess)\nsegment_preds_plot_4 = model_class4.predict_generator(valid_batches_plot_4,verbose=1)\n\nvalid_batches_plot_all = DataGenerator(batch_for_defect_all.iloc[:16],preprocess=preprocess)\nsegment_preds_plot_all = model_all.predict_generator(valid_batches_plot_all,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T12:16:12.306556Z","iopub.execute_input":"2022-07-17T12:16:12.306834Z","iopub.status.idle":"2022-07-17T12:16:20.994855Z","shell.execute_reply.started":"2022-07-17T12:16:12.306807Z","shell.execute_reply":"2022-07-17T12:16:20.993390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PLOT PREDICTIONS Function\ndef plot_segements(valid_batches_plot,segment_preds_plot):\n    print('Plotting predictions...')\n    print('KEY: yellow=defect1, green=defect2, blue=defect3, magenta=defect4')\n\n    for i,batch in enumerate(valid_batches_plot):\n        plt.figure(figsize=(20,36))\n        for k in range(16):\n            plt.subplot(16,2,2*k+1)\n            img = batch[0][k,]\n            img = Image.fromarray(img.astype('uint8'))\n            img = np.array(img)\n            dft = 0\n            extra = '  has defect '\n            for j in range(4):\n                msk = batch[1][k,:,:,j]\n                if np.sum(msk)!=0: \n                    dft=j+1\n                    extra += ' '+str(j+1)\n                msk = mask2pad(msk,pad=2)\n                msk = mask2contour(msk,width=3)\n                if j==0: # yellow\n                    img[msk==1,0] = 235 \n                    img[msk==1,1] = 235\n                elif j==1: img[msk==1,1] = 210 # green\n                elif j==2: img[msk==1,2] = 255 # blue\n                elif j==3: # magenta\n                    img[msk==1,0] = 255\n                    img[msk==1,2] = 255\n            if extra=='  has defect ': extra =''\n            plt.title('Train '+df_segment.iloc[16*i+k,0]+extra)\n            plt.axis('off') \n            plt.imshow(img)\n            plt.subplot(16,2,2*k+2) \n            if dft!=0:\n                msk = segment_preds_plot[16*i+k,:,:,dft-1]\n                plt.imshow(msk)\n            else:\n                plt.imshow(np.zeros((128,800)))\n            plt.axis('off')\n            mx = np.round(np.max(msk),3)\n            plt.title('Predict Defect '+str(dft)+'  (max pixel = '+str(mx)+')')\n        plt.subplots_adjust(wspace=0.05)\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-17T12:16:20.999303Z","iopub.execute_input":"2022-07-17T12:16:21.000410Z","iopub.status.idle":"2022-07-17T12:16:21.019267Z","shell.execute_reply.started":"2022-07-17T12:16:21.000361Z","shell.execute_reply":"2022-07-17T12:16:21.017849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model with all class as train data","metadata":{}},{"cell_type":"code","source":"plot_segements(valid_batches_plot_all,segment_preds_plot_all)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T12:16:21.021233Z","iopub.execute_input":"2022-07-17T12:16:21.021718Z","iopub.status.idle":"2022-07-17T12:16:23.863077Z","shell.execute_reply.started":"2022-07-17T12:16:21.021676Z","shell.execute_reply":"2022-07-17T12:16:23.859955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model of Class 1 Output","metadata":{}},{"cell_type":"code","source":"plot_segements(valid_batches_plot_1,segment_preds_plot_1)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T12:16:23.865018Z","iopub.execute_input":"2022-07-17T12:16:23.865771Z","iopub.status.idle":"2022-07-17T12:16:26.956811Z","shell.execute_reply.started":"2022-07-17T12:16:23.865714Z","shell.execute_reply":"2022-07-17T12:16:26.955533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model of Class 2 Output","metadata":{}},{"cell_type":"code","source":"plot_segements(valid_batches_plot_2,segment_preds_plot_2)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T12:16:26.958661Z","iopub.execute_input":"2022-07-17T12:16:26.959402Z","iopub.status.idle":"2022-07-17T12:16:30.777470Z","shell.execute_reply.started":"2022-07-17T12:16:26.959340Z","shell.execute_reply":"2022-07-17T12:16:30.776353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model of Class 3 Output","metadata":{}},{"cell_type":"code","source":"plot_segements(valid_batches_plot_3,segment_preds_plot_3)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T12:16:30.779535Z","iopub.execute_input":"2022-07-17T12:16:30.780049Z","iopub.status.idle":"2022-07-17T12:16:33.639144Z","shell.execute_reply.started":"2022-07-17T12:16:30.779992Z","shell.execute_reply":"2022-07-17T12:16:33.637909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model of Class 4 Output","metadata":{}},{"cell_type":"code","source":"plot_segements(valid_batches_plot_4,segment_preds_plot_4)","metadata":{"execution":{"iopub.status.busy":"2022-07-17T12:16:33.640826Z","iopub.execute_input":"2022-07-17T12:16:33.642370Z","iopub.status.idle":"2022-07-17T12:16:37.334565Z","shell.execute_reply.started":"2022-07-17T12:16:33.642303Z","shell.execute_reply":"2022-07-17T12:16:37.333264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# End of File","metadata":{}}]}