Kaggle competition: TGS Salt Identification Challenge
<SOS> metric: IntersectionOverUnionObjectSegmentation Kaggle data source: tgs-salt-identification-challenge<categorify>
plt.style.use('seaborn-white')
 sns.set_style("white")
 %matplotlib inline
def one_hot_encode_truth(truth, num_class=4):
one_hot = truth.repeat(1,num_class,1,1)
 arange = torch.arange(1,num_class+1 ).view(1,num_class,1,1 ).to(truth.device)
 one_hot =(one_hot == arange ).float()
return one_hot
def one_hot_encode_predict(predict, num_class=4):
value, index = torch.max(predict, 1, keepdim=True)
 value = value.repeat(1,num_class,1,1)
 index = index.repeat(1,num_class,1,1)
 arange = torch.arange(1,num_class+1 ).view(1,num_class,1,1 ).to(predict.device)
 one_hot =(index == arange ).float()
value = value*one_hot
return value
def remove_small_one(predict, min_size):
H,W = predict.shape
num_component, component = cv2.connectedComponents(predict.astype(np.uint8))
predict = np.zeros(( H,W), np.bool)
 for c in range(1,num_component):
p =(component==c)
 if p.sum() >min_size:
predict[p] = True
return predict
def remove_small(predict, min_size):
for b in range(len(predict)) :
for c in range(4):
predict[b,c] = remove_small_one(predict[b,c], min_size[c])
 return predict
def run_length_encode(mask):
m = mask.T.flatten()
if m.sum() ==0:
rle=''
else:
m = np.concatenate([[0], m, [0]])
 run = np.where(m[1:] != m[:-1])[0] + 1
run[1::2] -= run[::2]
rle = ' '.join(str(r)for r in run)
 return rle
def time_to_str(t, mode='min'):
if mode=='min':
t = int(t)/60
hr = t//60
min = t%60
return '%2d hr %02d min'%(hr,min)
 elif mode=='sec':
t = int(t)
 min = t//60
sec = t%60
return '%2d min %02d sec'%(min,sec)
 else:
raise NotImplementedError<define_variables>
im_width = 101
im_height = 101
im_chan = 1
basicpath = '.. /input/'
path_train = basicpath + 'train/'
path_test = basicpath + 'test/'
path_train_images = path_train + 'images/'
path_train_masks = path_train + 'masks/'
path_test_images = path_test + 'images/'
def do_evaluate_save(mode, net, test_dataset, augment=[], out_dir=None):
csv_file = 'submission.csv'
image_id_class_id = []
encoded_pixel = []
threshold_pixel = [0.50,0.50,0.50,0.50,]
threshold_size = [200,1500,1500,2000,]
test_loader = DataLoader(
 test_dataset,
sampler = SequentialSampler(test_dataset),
batch_size = 8,
drop_last = False,
num_workers = 0,
pin_memory = True,
collate_fn = null_collate 
)
 def sharpen(p,t=0):
if t!=0:
return p**t
else:
return p
test_num = 0
start = timer()
for t,(input, infor)in enumerate(test_loader):
batch_size,C,H,W = input.shape
input = input.cuda()
with torch.no_grad() :
net.eval()
num_augment = 0
if 1:
logit = data_parallel(net,input)
 probability = torch.softmax(logit,1)
 probability_mask = sharpen(probability,0)
 num_augment+=1
if 'flip_lr' in augment:
logit = data_parallel(net,torch.flip(input,dims=[3]))
probability = torch.softmax(torch.flip(logit,dims=[3]),1)
 probability_mask += sharpen(probability)
 num_augment+=1
if 'flip_ud' in augment:
logit = data_parallel(net,torch.flip(input,dims=[2]))
probability = torch.softmax(torch.flip(logit,dims=[2]),1)
 probability_mask += sharpen(probability)
 num_augment+=1
probability_mask = probability_mask/num_augment
probability = probability_mask.clone()
probability_mask = one_hot_encode_predict(probability_mask)
 batch_size = len(infor)
 probability_mask = probability_mask.data.cpu().numpy()
probability = probability.data.cpu().numpy()
predict_mask = probability_mask>np.array(threshold_pixel ).reshape(1,4,1,1)
 predict_mask = remove_small(predict_mask, threshold_size)
 predict_label =(( predict_mask.sum(-1 ).sum(-1)) >0 ).astype(np.int32)
 for b in range(len(infor)) :
for c in range(4):
image_id_class_id.append(infor[b].image_id+'_%d'%(c+1))
if predict_label[b,c]==0:
rle=''
else:
rle = run_length_encode(predict_mask[b,c])
 encoded_pixel.append(rle)
 test_num += batch_size
print('\r %4d / %4d %s'%(
 test_num, len(test_loader.dataset), time_to_str(( timer() - start),'min')
),end='',flush=True)
 assert(test_num == len(test_loader.dataset))
print('')
 df = pd.DataFrame(zip(image_id_class_id, encoded_pixel), columns=['ImageId_ClassId', 'EncodedPixels'])
 df.to_csv(csv_file, index=False)
 print('>>>csv saved!' )<load_from_csv>
img_size_ori = 101
img_size_target = 101
def upsample(img):
if img_size_ori == img_size_target:
return img
return resize(img,(img_size_target, img_size_target), mode='constant', preserve_range=True)
 def downsample(img):
if img_size_ori == img_size_target:
return img
return resize(img,(img_size_ori, img_size_ori), mode='constant', preserve_range=True)
def run_submit() :
out_dir = \
'/kaggle/input'
initial_checkpoint = \
'/kaggle/input/resnetmodel/00114000_model.pth'
mode = 'test'
augment = ['null', 'flip_lr','flip_ud']
test_dataset = SteelDataset()
net = Net().cuda()
net.load_state_dict(torch.load(initial_checkpoint, map_location=lambda storage, loc: storage), strict=False)
 do_evaluate_save(mode, net, test_dataset, augment,out_dir)
 def test4() :
csv_data = pd.read_csv("/kaggle/input/uploadcsv/resnet18-softmax-tta-0.50.csv")
 csv_data.to_csv('submission.csv', index=False)
 if __name__ == "__main__":
run_submit()
print('>>>Done!' )<load_from_csv>
train_df = pd.read_csv(".. /input/train.csv", index_col="id", usecols=[0])
 depths_df = pd.read_csv(".. /input/depths.csv", index_col="id")
 train_df = train_df.join(depths_df)
 test_df = depths_df[~depths_df.index.isin(train_df.index)]
len(train_df )
s = pd.read_csv('.. /input/colab1/submission_3classifiers_11segmenters.csv')
 s.to_csv('submission.csv',index=False)
 s.head()<load_pretrained>
train_df["images"] = [np.array(load_img(".. /input/train/images/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]
model = torch.jit.load(".. /input/mixmocrjit/_ckpt_epoch_64_jit.pth")
 model.eval() ;
model.cuda() ;<create_dataframe>
train_df["masks"] = [np.array(load_img(".. /input/train/masks/{}.png".format(idx), grayscale=True)) / 255 for idx in tqdm_notebook(train_df.index)]
test_data_folder = ".. /input/severstal-steel-defect-detection/test_images"
dataset = TestSegDataset(
 imgdir=test_data_folder,
color_channel="RGB",
normalize=True,
use_tta=False,
ignore_background=True,
input_size=(256,1600), 
)
 print(len(dataset))
dataloader = DataLoader(
 dataset=dataset,
batch_size=14,
shuffle=False,
num_workers=2,
pin_memory=True,
collate_fn=dataset.collate_fn, 
)
 print(len(dataloader))<categorify>
train_df["coverage"] = train_df.masks.map(np.sum)/ pow(img_size_ori, 2)
 def cov_to_class(val):
for i in range(0, 11):
if val * 10 <= i :
return i
train_df["coverage_class"] = train_df.coverage.map(cov_to_class)
def fuse_mask_score(cls_score, obj_score,segm_scores, use_sigmoid=True, cls_thresh_prob=0.45):
masks_scores = segm_scores*obj_score
if not use_sigmoid:
masks_scores[:,0,:,:] +=(1-obj_score[:,0,:,:])
 background_proposal_cls = cls_score.squeeze() < cls_thresh_prob
if use_sigmoid:
masks_scores[background_proposal_cls] = 0.0
else:
masks_scores[background_proposal_cls,0,:,:]=1.0
masks_scores[background_proposal_cls,1:,:,:]=0.0
return masks_scores 
<compute_train_metric>
ids_train, ids_valid, x_train, x_valid, y_train, y_valid, cov_train, cov_test, depth_train, depth_test = train_test_split(
 train_df.index.values,
np.array(train_df.images.map(upsample ).tolist() ).reshape(-1, img_size_target, img_size_target, 1),
np.array(train_df.masks.map(upsample ).tolist() ).reshape(-1, img_size_target, img_size_target, 1),
train_df.coverage.values,
train_df.z.values,
test_size=0.2, stratify=train_df.coverage_class, random_state= 1234 )
predictions = []
with torch.no_grad() :
for batch in tqdm(dataloader, total=len(dataloader)) :
filenames = batch['fnames']
imgs = batch['images']
imgs = imgs.cuda()
cls_score, obj_score, segm_scores = model(imgs)
 mask_score = fuse_mask_score(cls_score, obj_score,segm_scores, use_sigmoid=False, cls_thresh_prob=0.45)
 for dim_tta in [(2,),(3,),(2,3)]:
imgs_tta = imgs.flip(dims=dim_tta)
 cls_score, obj_score, segm_scores = model(imgs_tta)
 mask_score_tta = fuse_mask_score(cls_score, obj_score,segm_scores, use_sigmoid=False, cls_thresh_prob=0.45)
 mask_score_tta = mask_score_tta.flip(dim_tta)
 mask_score +=mask_score_tta
mask_score /=4
N,num_class,H,W = mask_score.shape
masks = torch.argmax(mask_score,dim=1)
 masks = masks.squeeze()
masks_binary = F.one_hot(masks,num_class)
 masks_binary = masks_binary.permute(0,3,1,2)[:,1:,:,:]
masks_binary = masks_binary.cpu().numpy()
for i in range(N):
fname = filenames[i].split('/')[-1]
mask_i = masks_binary[i]
for cls_idx in range(num_class-1):
rle = mask2rle(mask_i[cls_idx].astype('uint8'))
name = f'{fname}_{cls_idx+1}'
predictions.append(( name, rle))<save_to_csv>
def convolution_block(x, filters, size, strides=(1,1), padding='same', activation=True):
x = Conv2D(filters, size, strides=strides, padding=padding )(x)
 x = BatchNormalization()(x)
 if activation == True:
x = Activation('relu' )(x)
 return x
def residual_block(blockInput, num_filters=16):
x = Activation('relu' )(blockInput)
 x = BatchNormalization()(x)
 x = convolution_block(x, num_filters,(3,3))
x = convolution_block(x, num_filters,(3,3), activation=False)
 x = Add()([x, blockInput])
 return x
sample_submission_path = 'submission.csv'
df = pd.DataFrame(predictions, columns=['ImageId_ClassId', 'EncodedPixels'])
 df.to_csv(sample_submission_path, index=False )<load_from_csv>
def build_model(input_layer, start_neurons, DropoutRatio = 0.5):
conv1 = Conv2D(start_neurons * 1,(3, 3), activation=None, padding="same" )(input_layer)
 conv1 = residual_block(conv1,start_neurons * 1)
 conv1 = residual_block(conv1,start_neurons * 1)
 conv1 = Activation('relu' )(conv1)
 pool1 = MaxPooling2D(( 2, 2))(conv1)
 pool1 = Dropout(DropoutRatio/2 )(pool1)
 conv2 = Conv2D(start_neurons * 2,(3, 3), activation=None, padding="same" )(pool1)
 conv2 = residual_block(conv2,start_neurons * 2)
 conv2 = residual_block(conv2,start_neurons * 2)
 conv2 = Activation('relu' )(conv2)
 pool2 = MaxPooling2D(( 2, 2))(conv2)
 pool2 = Dropout(DropoutRatio )(pool2)
 conv3 = Conv2D(start_neurons * 4,(3, 3), activation=None, padding="same" )(pool2)
 conv3 = residual_block(conv3,start_neurons * 4)
 conv3 = residual_block(conv3,start_neurons * 4)
 conv3 = Activation('relu' )(conv3)
 pool3 = MaxPooling2D(( 2, 2))(conv3)
 pool3 = Dropout(DropoutRatio )(pool3)
 conv4 = Conv2D(start_neurons * 8,(3, 3), activation=None, padding="same" )(pool3)
 conv4 = residual_block(conv4,start_neurons * 8)
 conv4 = residual_block(conv4,start_neurons * 8)
 conv4 = Activation('relu' )(conv4)
 pool4 = MaxPooling2D(( 2, 2))(conv4)
 pool4 = Dropout(DropoutRatio )(pool4)
 convm = Conv2D(start_neurons * 16,(3, 3), activation=None, padding="same" )(pool4)
 convm = residual_block(convm,start_neurons * 16)
 convm = residual_block(convm,start_neurons * 16)
 convm = Activation('relu' )(convm)
 deconv4 = Conv2DTranspose(start_neurons * 8,(3, 3), strides=(2, 2), padding="same" )(convm)
 uconv4 = concatenate([deconv4, conv4])
 uconv4 = Dropout(DropoutRatio )(uconv4)
 uconv4 = Conv2D(start_neurons * 8,(3, 3), activation=None, padding="same" )(uconv4)
 uconv4 = residual_block(uconv4,start_neurons * 8)
 uconv4 = residual_block(uconv4,start_neurons * 8)
 uconv4 = Activation('relu' )(uconv4)
 deconv3 = Conv2DTranspose(start_neurons * 4,(3, 3), strides=(2, 2), padding="valid" )(uconv4)
 uconv3 = concatenate([deconv3, conv3])
 uconv3 = Dropout(DropoutRatio )(uconv3)
 uconv3 = Conv2D(start_neurons * 4,(3, 3), activation=None, padding="same" )(uconv3)
 uconv3 = residual_block(uconv3,start_neurons * 4)
 uconv3 = residual_block(uconv3,start_neurons * 4)
 uconv3 = Activation('relu' )(uconv3)
 deconv2 = Conv2DTranspose(start_neurons * 2,(3, 3), strides=(2, 2), padding="same" )(uconv3)
 uconv2 = concatenate([deconv2, conv2])
 uconv2 = Dropout(DropoutRatio )(uconv2)
 uconv2 = Conv2D(start_neurons * 2,(3, 3), activation=None, padding="same" )(uconv2)
 uconv2 = residual_block(uconv2,start_neurons * 2)
 uconv2 = residual_block(uconv2,start_neurons * 2)
 uconv2 = Activation('relu' )(uconv2)
 deconv1 = Conv2DTranspose(start_neurons * 1,(3, 3), strides=(2, 2), padding="valid" )(uconv2)
 uconv1 = concatenate([deconv1, conv1])
 uconv1 = Dropout(DropoutRatio )(uconv1)
 uconv1 = Conv2D(start_neurons * 1,(3, 3), activation=None, padding="same" )(uconv1)
 uconv1 = residual_block(uconv1,start_neurons * 1)
 uconv1 = residual_block(uconv1,start_neurons * 1)
 uconv1 = Activation('relu' )(uconv1)
 uconv1 = Dropout(DropoutRatio/2 )(uconv1)
 output_layer = Conv2D(1,(1,1), padding="same", activation="sigmoid" )(uconv1)
 return output_layer
df_results = pd.read_csv("submission.csv")
 num_samples = len(df_results)
 num_images = int(num_samples / 4)
 print(df_results.shape)
 print(df_results.head(18))<install_modules>
x_train2 = np.append(x_train, [np.fliplr(x)for x in x_train], axis=0)
 y_train2 = np.append(y_train, [np.fliplr(x)for x in y_train], axis=0)
 print(x_train2.shape)
 print(y_valid.shape )
!pip install.. /input/segmentation-models/image_classifiers-1.0.0b1/image_classifiers-1.0.0b1
!pip install.. /input/segmentation-models/efficientnet-1.0.0b3/efficientnet-1.0.0b3
!pip install.. /input/segmentation-models/segmentation_models/segmentation_models<set_options>
input_layer = Input(( img_size_target, img_size_target, 1))
output_layer = build_model(input_layer, 16,0.5)
 model = Model(input_layer, output_layer)
 model.compile(loss="binary_crossentropy", optimizer="adam", metrics=[my_iou_metric])
 model.summary()
set_random_seed(2)
 np.random.seed(0)
<define_search_space>
early_stopping = EarlyStopping(monitor='val_my_iou_metric', mode = 'max',patience=20, verbose=1)
 model_checkpoint = ModelCheckpoint("./unet_best1.model",monitor='val_my_iou_metric',
mode = 'max', save_best_only=True, verbose=1)
 reduce_lr = ReduceLROnPlateau(monitor='val_my_iou_metric', mode = 'max',factor=0.2, patience=5, min_lr=0.00001, verbose=1)
 epochs = 200
batch_size = 32
history = model.fit(x_train2, y_train2,
validation_data=[x_valid, y_valid],
epochs=epochs,
batch_size=batch_size,
callbacks=[early_stopping, model_checkpoint, reduce_lr],
verbose=2 )
IMG_SIZE =(256, 1600, 3)
 CLF_IMG_SIZE =(128, 800, 3)
 clf_model = 'resnet34'
unet_encoder = 'resnet34'<choose_model_class>
model = load_model("./unet_best1.model",custom_objects={'my_iou_metric': my_iou_metric})
M, preprocess_input = Classifiers.get(clf_model)
 base_clf = M(input_shape=CLF_IMG_SIZE, weights=None, include_top=False)
 x = base_clf.output
x = GlobalAveragePooling2D()(x)
 x = Dense(4, activation='sigmoid', kernel_initializer='he_normal' )(x)
 clf = Model(inputs=base_clf.input, outputs=x)
 clf.load_weights('.. /input/severstal-clf/resnet34_clf_fold_4.h5' )<categorify>
def predict_result(model,x_test,img_size_target):
x_test_reflect = np.array([np.fliplr(x)for x in x_test])
 preds_test1 = model.predict(x_test ).reshape(-1, img_size_target, img_size_target)
 preds_test2_refect = model.predict(x_test_reflect ).reshape(-1, img_size_target, img_size_target)
 preds_test2 = np.array([ np.fliplr(x)for x in preds_test2_refect])
 preds_avg =(preds_test1 +preds_test2)/2
return preds_avg
unet = sm.Unet(unet_encoder, input_shape=IMG_SIZE, classes=5, activation='softmax', encoder_weights=None)
 unet.load_weights('.. /input/unet-resnet34-severstal/unet_resnet34_fold_1_mixup_full_size.h5' )<categorify>
preds_valid = predict_result(model,x_valid,img_size_target)
 preds_valid2 = np.array([downsample(x)for x in preds_valid])
 y_valid2 = np.array([downsample(x)for x in y_valid] )
def rle_encoding(mask):
pixels = mask.T.flatten()
pixels = np.concatenate([[0], pixels,[0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
if len(runs)% 2:
runs = np.append(runs,len(pixels))
runs[1::2] -= runs[0::2]
return ' '.join(str(x)for x in runs )<create_dataframe>
thresholds = np.linspace(0.3, 0.7, 31)
 ious = np.array([iou_metric_batch(y_valid2, np.int32(preds_valid2 > threshold)) for threshold in tqdm_notebook(thresholds)])
load_dir = '.. /input/severstal-steel-defect-detection/test_images/'
test_fns = os.listdir(load_dir)
 d = { 'x': test_fns}
df = pd.DataFrame(data=d )<load_pretrained>

 def rle_encode(im):

 pixels = im.flatten(order = 'F')
 pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs )
ctf_loader = ImageDataGenerator(rescale=1/255.)
unet_loader = ImageDataGenerator(rescale=1/255.)
clf_loader = ctf_loader.flow_from_dataframe(df, directory=load_dir, x_col='x', target_size=(128, 800), shuffle=False, class_mode=None, batch_size=8)
 unet_loader = unet_loader.flow_from_dataframe(df, directory=load_dir, x_col='x', target_size=(256, 1600), shuffle=False, class_mode=None, batch_size=8 )<predict_on_test>
preds_test = predict_result(model,x_test,img_size_target )
%%time
ImageId_ClassId = []
EncodedPixels = []
it = 0
for i in range(len(clf_loader)) :
has_defect = clf.predict(next(clf_loader))
masks = unet.predict(next(unet_loader))
for j in range(len(has_defect)) :
hd = has_defect[j].flatten()
mask = masks[j]
mask = np.argmax(mask, axis=-1)
 mask = keras.utils.to_categorical(mask, num_classes=5, dtype='uint8')
 tmp = np.asarray([ test_fns[it] +'_'+str(id)for id in range(1,5)])
 it += 1
for idx in range(1, 5):
ImageId_ClassId.append(tmp[idx - 1])
 if hd[idx - 1] > 0.5:
EncodedPixels.append(rle_encoding(mask[:,:,idx]))
else:
EncodedPixels.append('' )<prepare_output>
t1 = time.time()
pred_dict = {idx: rle_encode(np.round(downsample(preds_test[i])> threshold_best)) for i, idx in enumerate(tqdm_notebook(test_df.index.values)) }
t2 = time.time()
print(f"Usedtime = {t2-t1} s" )
<save_to_csv><EOS> 
sub = pd.DataFrame.from_dict(pred_dict,orient='index')
 sub.index.names = ['id']
sub.columns = ['rle_mask']
sub.to_csv('submission13.csv' )