Kaggle competition: TGS Salt Identification Challenge
<SOS> metric: IntersectionOverUnionObjectSegmentation Kaggle data source: tgs-salt-identification-challenge<save_to_csv>

 torch.manual_seed(60794)
 torch.backends.cudnn.deterministic = True
np.random.seed(60794)
 random.seed(60794)
sub6 = sample_submission.drop(columns=target_cols ).merge(test[['sig_id']+target_cols], on='sig_id', how='left' ).fillna(0)
 sub6.to_csv('submission.csv', index=False )<import_modules>
def set_learning_rate(optimizer, lr):
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def get_learning_rate(optimizer):
return optimizer.param_groups[0]['lr']
class LearningRate() :
def __init__(self, initial_lr, iteration_type):
self.initial_lr = initial_lr
self.iteration_type = iteration_type
def get_learning_rate(self, optimizer):
return optimizer.param_groups[0]['lr']
def set_learning_rate(self, optimizer, new_lr):
for param_group in optimizer.param_groups:
param_group['lr'] = new_lr
def adjust(self, optimizer, lr, iteration, params=None):
self.set_learning_rate(optimizer, lr)
 return lr
class FixedLR(LearningRate):
def __init__(self, initial_lr, iteration_type):
super().__init__(initial_lr, iteration_type)
 def adjust(self, optimizer, iteration, params=None):
new_lr = super().get_learning_rate(optimizer)
 return new_lr
class LinearLR(LearningRate):
def __init__(self, initial_lr, iteration_type, fixed_delta):
super().__init__(initial_lr, iteration_type)
 self.fixed_delta = fixed_delta
def adjust(self, optimizer, iteration, params=None):
lr = super().get_learning_rate(optimizer)
 new_lr = lr + self.fixed_delta
super().set_learning_rate(optimizer, new_lr)
 return new_lr
class SnapshotLR(LearningRate):

 def __init__(self, initial_lr, iteration_type,
max_lr, total_iters, n_cycles):

 super().__init__(initial_lr, iteration_type)
 self.max_lr = max_lr
self.total_iters = total_iters
self.cycles = n_cycles
def cosine_annealing(self, t):

 return self.max_lr/2 *(math.cos(
(math.pi *(t %(self.total_iters//self.cycles)))/ 
(self.total_iters//self.cycles)) + 1)
 def adjust(self, optimizer, iteration, params=None):
new_lr = self.cosine_annealing(iteration)
 self.set_learning_rate(optimizer, new_lr)
 return new_lr
class SnapshotParamsLR(LearningRate):

 def __init__(self, initial_lr, iteration_type,
total_iters, n_cycles):

 super().__init__(initial_lr, iteration_type)
 self.total_iters = total_iters
self.cycles = n_cycles
def cosine_annealing(self, t, max_lr):
return max_lr/2 *(math.cos(
(math.pi *(t %(self.total_iters//self.cycles)))/(
 self.total_iters//self.cycles)) + 1)
 def adjust(self, optimizer, iteration, params=None):
lrs = []
for param_group in optimizer.param_groups:
new_lr = self.cosine_annealing(iteration, param_group['max_lr'])
 param_group['lr'] = new_lr
lrs.append(new_lr)
 return new_lr
class DevDecayLR(LearningRate):

 def __init__(self, initial_lr, iteration_type,
decay_factor=0.9, decay_patience=1):
super().__init__(initial_lr, iteration_type)
 self.decay_factor = decay_factor
self.decay_patience = decay_patience
def adjust(self, optimizer, iteration, params):
lr = super().get_learning_rate(optimizer)
 best_iter = params['best_iter']
if(iteration - best_iter)> self.decay_patience:
print('Decaying learning rate by factor: {:.5f}'.format(
 self.decay_factor ).rstrip('0'))
lr *= self.decay_factor
super().set_learning_rate(optimizer, lr)
 return lr
class ScheduledLR(LearningRate):
def __init__(self, initial_lr, iteration_type, lr_schedule):
super().__init__(initial_lr, iteration_type)
 self.lr_schedule = lr_schedule
def adjust(self, optimizer, iteration, params=None):
if iteration in self.lr_schedule:
new_lr = self.lr_schedule[iteration]
else:
new_lr = self.get_learning_rate(optimizer)
 super().set_learning_rate(optimizer, new_lr)
 return new_lr
class DecayingLR(LearningRate):
def __init__(self, initial_lr, iteration_type, decay, n_epochs):
super().__init__(initial_lr, iteration_type)
 self.decay = decay
self.n_epochs = n_epochs
def exponential_decay(self, iteration, params=None):

 return self.initial_lr *(self.decay **(iteration // self.n_epochs))
def adjust(self, optimizer, iteration):
new_lr = self.exponential_decay(iteration)
 super().set_learning_rate(optimizer, new_lr)
 return new_lr
class CyclicalLR(LearningRate):

 def __init__(self, initial_lr, iteration_type, n_iters, cycle_length,
min_lr, max_lr):
assert initial_lr == min_lr
super(CyclicalLR, self ).__init__(initial_lr, iteration_type)
 self.n_iters = n_iters
self.cycle_length = cycle_length
self.min_lr = min_lr
self.max_lr = max_lr
def triangular(self, iteration):
iteration -= 1
cycle = math.floor(1 + iteration/self.cycle_length)
 x = abs(iteration/(self.cycle_length/2)- 2*cycle + 1)
 new_lr = self.min_lr +(self.max_lr - self.min_lr)* max(0,(1-x))
return new_lr
def adjust(self, optimizer, iteration, best_iter=1):
new_lr = self.triangular(iteration)
 super().set_learning_rate(optimizer, new_lr)
 return new_lr
def cosine_annealing(lr_max, T, M, t):

 return lr_max/2 *(math.cos(( math.pi *(t %(T//M)))/(T//M)) + 1 )
import glob 
<load_from_csv>

 def save_checkpoint(checkpoint_path, model, optimizer):
state = {'state_dict': model.state_dict() ,
'optimizer' : optimizer.state_dict() }
torch.save(state, checkpoint_path)
 print('model saved to %s' % checkpoint_path)
 def load_checkpoint(checkpoint_path, model, optimizer):
state = torch.load(checkpoint_path)
 model.load_state_dict(state['state_dict'])
 optimizer.load_state_dict(state['optimizer'])
 print('model loaded from %s' % checkpoint_path )
!python.. /input/markscripts/deepinsight_resnest_lightning_v2_infer.py
sub5 = pd.read_csv('submission_resnest_v2.csv' )<load_from_csv>
def train_model(model,epochs,
learning_rate,loss_function,
optimizer, dataset, dataset_val,
batch_size 
):

 snapshot = SnapshotLR(
 initial_lr=0.000001,max_lr=0.0001,
total_iters=epochs,n_cycles=30,
iteration_type="epochs" 
)
 scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer,
mode="max",
factor=0.97,
min_lr=0,
patience=10 
)
 lowest_train_loss = None
lowest_val_loss = None
highest_train_iou = None
highest_val_iou = None
for epoch in range(epochs):
train_loss = []
train_iou = []
for image, mask in tqdm_notebook(data.DataLoader(dataset,batch_size=batch_size,shuffle=True)) :
image = image.type(torch.FloatTensor ).cuda()
y_pred = model(Variable(image))
loss = loss_function(y_pred, Variable(mask.cuda()))
iou_train = iou_metric_batch(mask.cpu().numpy() ,y_pred.cpu().detach().numpy())
 optimizer.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_norm(model.parameters() , 1.0)
 optimizer.step()
train_loss.append(loss.data[0])
 train_iou.append(iou_train)
 val_loss = []
val_iou = []
for image, mask in data.DataLoader(dataset_val, batch_size = batch_size, shuffle = False):
image = image.cuda()
y_pred = model(Variable(image))
loss = loss_function(y_pred, Variable(mask.cuda()))
val_loss.append(loss.data[0])
 iou_val = iou_metric_batch(mask.cpu().numpy() ,y_pred.cpu().detach().numpy())
 val_iou.append(iou_val)
 if lowest_train_loss is None:
lowest_train_loss = np.mean(train_loss)
 save_checkpoint('low_train_loss_tgs.pth' ,model, optimizer)
 if lowest_val_loss is None:
lowest_val_loss = np.mean(val_loss)
 save_checkpoint('low_val_loss_tgs.pth' ,model, optimizer)
 if highest_train_iou is None:
highest_train_iou = np.mean(train_iou)
 save_checkpoint('high_train_iou_tgs.pth' ,model, optimizer)
 if highest_val_iou is None:
highest_val_iou = np.mean(val_iou)
 save_checkpoint('high_val_iou_tgs.pth' ,model, optimizer)
 print("Epoch: %d,T_Loss: %.7f, T_IOU: %.7f, V_Loss: %.7f, V_IOU: %.7f" %(
 epoch,
np.mean(train_loss),
np.mean(train_iou),
np.mean(val_loss),
np.mean(val_iou)
)
)
 if np.mean(train_loss)< lowest_train_loss:
lowest_train_loss = np.mean(train_loss)
 save_checkpoint('low_train_loss_tgs.pth' ,model, optimizer)
 if np.mean(val_loss)< lowest_val_loss:
lowest_val_loss = np.mean(val_loss)
 save_checkpoint('low_val_loss_tgs.pth' ,model, optimizer)
 if np.mean(train_iou)> highest_train_iou:
highest_train_iou = np.mean(train_iou)
 save_checkpoint('high_train_iou_tgs.pth' ,model, optimizer)
 if np.mean(val_iou)> highest_val_iou:
highest_val_iou = np.mean(val_iou)
 save_checkpoint('high_val_iou_tgs.pth' ,model, optimizer)
 scheduler.step(highest_val_iou)
 resulting_lr = snapshot.adjust(optimizer,epoch)
 print("LR",resulting_lr)
 save_checkpoint('tgs-%i.pth' % epochs, model, optimizer)
 print("Best T_L:",lowest_train_loss,
" Best T_I:",highest_train_iou,
" Best V_L:",lowest_val_loss,
" Best V_I:", highest_val_iou 
)
 return model
!python.. /input/markscripts/deepinsight_efficientnet_lightning_v7_b3_infer.py
sub4 = pd.read_csv('./submission_effnet_v7_b3.csv' )<load_from_csv>
class TGSSaltDataset(data.Dataset):
def __init__(self, root_path, file_list, is_test = False):
self.is_test = is_test
self.root_path = root_path
self.file_list = file_list
def __len__(self):
return len(self.file_list)
 def __getitem__(self, index):
if index not in range(0, len(self.file_list)) :
return self.__getitem__(np.random.randint(0, self.__len__()))
file_id = self.file_list[index]
image_folder = os.path.join(self.root_path, "images")
 image_path = os.path.join(image_folder, file_id + ".png")
 mask_folder = os.path.join(self.root_path, "masks")
 mask_path = os.path.join(mask_folder, file_id + ".png")
 image = load_image(image_path)
 if self.is_test:
return(image,)
 else:
mask = load_image(mask_path, mask = True)
 return image, mask
! python.. /input/updatedsimplenn/simpleNN_without_ns_newcv.py
sub3 = pd.read_csv('./submission.csv' )<load_from_csv>
def rle_encoding(x):

 dots = np.where(x.T.flatten() == 1)[0]
run_lengths = []
prev = -2
for b in dots:
if(b > prev+1): run_lengths.extend(( b + 1, 0))
run_lengths[-1] += 1
prev = b
return run_lengths
test = pd.read_csv('.. /input/lish-moa/test_features.csv')
 !python.. /input/python-scripts-moa/2heads_1836_oldcv.py
sub2 = pd.read_csv('./submission.csv' )<load_from_csv>
def load_image(path,pad=True, mask = False):

 flip = False
if "_gael" in path:
path = path.replace("_gael","")
 flip = True
img = cv2.imread(str(path))
if flip:
img = cv2.flip(img, 0)
 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 height, width, _ = img.shape
if pad:
if height % 32 == 0:
y_min_pad = 0
y_max_pad = 0
else:
y_pad = 32 - height % 32
y_min_pad = int(y_pad / 2)
 y_max_pad = y_pad - y_min_pad
if width % 32 == 0:
x_min_pad = 0
x_max_pad = 0
else:
x_pad = 32 - width % 32
x_min_pad = int(x_pad / 2)
 x_max_pad = x_pad - x_min_pad
img = cv2.copyMakeBorder(img, y_min_pad, y_max_pad, x_min_pad, x_max_pad, cv2.BORDER_REFLECT_101)
 if mask:
img = img[:, :, 0:1] // 255
return torch.from_numpy(np.transpose(img,(2, 0, 1)).astype('float32'))
else:
img = img / 255.0
return torch.from_numpy(np.transpose(img,(2, 0, 1)).astype('float32'))
!python.. /input/python2stagenn/2stageNN_with_ns_oldcv.py
sub1 = pd.read_csv('./submission_2stageNN_with_ns_oldcv_0.01822.csv' )<load_from_csv>
def pred_resize(predictions,height,width):

 if height % 32 == 0:
y_min_pad = 0
y_max_pad = 0
else:
y_pad = 32 - height % 32
y_min_pad = int(y_pad / 2)
 y_max_pad = y_pad - y_min_pad
if width % 32 == 0:
x_min_pad = 0
x_max_pad = 0
else:
x_pad = 32 - width % 32
x_min_pad = int(x_pad / 2)
 x_max_pad = x_pad - x_min_pad
predictions = predictions[:, y_min_pad:128 - y_max_pad, x_min_pad:128 - x_max_pad]
print("Prediction Shape: ",predictions.shape)
 return predictions
!python.. /input/simplennoldcvfinal/script_NN_836_final.py
sub7 = pd.read_csv('submission_script_simpleNN_oldcv_0.01836.csv' )<save_to_csv>
def stack_and_resize(predictions,height,width):

 preds_stacked = np.vstack(predictions)[:, 0, :, :]
return pred_resize(preds_stacked,height,width )
submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv')
 submission.iloc[:, 1:] = 0
submission.iloc[:, 1:] =(sub1.iloc[:,1:]*0.44 + sub7.iloc[:,1:]*0.15 + sub4.iloc[:,1:]*0.35 +sub2.iloc[:,1:]*0.06)*0.78 + sub6.iloc[:,1:]*0.1 + sub7.iloc[:,1:]*0.05 + sub5.iloc[:,1:]*0.07
submission.to_csv('submission.csv', index=False )<set_options>
warnings.filterwarnings("ignore" )
tf.config.optimizer.set_jit(True )<set_options>

 directory = '.. /input/tgs-salt-identification-challenge'
depths_df = pd.read_csv(os.path.join(directory, 'train.csv'))
train_path = os.path.join(directory, 'train')
 test_path = os.path.join(directory, 'test')
 ids_val = _pickle.load(open(".. /input/intermediatetgs/val_index.obj","rb"))
ids_train = _pickle.load(open(".. /input/intermediatetgs/train_index.obj","rb"))
file_list = list(depths_df['id'].values)
 file_list_val = list(ids_val)
 file_list_train = list(ids_train)
 file_list_flip = [s + "_gael" for s in file_list_train]
file_list_train = file_list_train + file_list_flip
test_file_list = glob.glob(os.path.join(test_path, 'images', '*.png'))
test_file_list = [f.split('/')[-1].split('.')[0] for f in test_file_list]
print("Training Data:")
 print("Train: ",len(file_list_train))
print("Validation: ",len(file_list_val))
print("Total: ",len(file_list))
print("Testing Data:")
 print("Images: ",len(test_file_list))
dataset = TGSSaltDataset(train_path, file_list_train)
 dataset_val = TGSSaltDataset(train_path, file_list_val)
 test_dataset = TGSSaltDataset(test_path, test_file_list, is_test = True )
def seed_everything(seed=0):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 session_conf = tf.compat.v1.ConfigProto(
 intra_op_parallelism_threads=1,
inter_op_parallelism_threads=1 
)
 sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph() , config=session_conf)
 tf.compat.v1.keras.backend.set_session(sess )<load_from_csv>
epoch =100
learning_rate = 0.0001
loss_fn = torch.nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters() , lr=learning_rate, eps=1e-7)
 load_checkpoint(".. /input/80pytorchgamma/high_val_iou_tgs.pth", model, optimizer)
 model = train_model(model,epoch,learning_rate,
loss_fn,optimizer, dataset,
dataset_val,32
)
data_root = "/kaggle/input/lish-moa/"
train_X = pd.read_csv(data_root + 'train_features.csv', index_col='sig_id')
 train_Y = pd.read_csv(data_root + 'train_targets_scored.csv', index_col='sig_id')
 test_X = pd.read_csv(data_root + 'test_features.csv', index_col='sig_id')
 sample_sub = pd.read_csv(data_root + 'sample_submission.csv')
<categorify>
val_predictions = []
val_masks = []
for image, mask in tqdm_notebook(data.DataLoader(dataset_val, batch_size = 32)) :
image = Variable(image.type(torch.FloatTensor ).cuda())
 y_pred = model(image ).cpu().data.numpy()
val_predictions.append(y_pred)
 val_masks.append(mask)
 val_predictions_stacked = stack_and_resize(val_predictions,101,101)
 val_masks_stacked = stack_and_resize(val_masks,101,101 )
le = LabelEncoder()
for columns in ['cp_type','cp_dose']:
le.fit(train_X[columns])
 train_X[columns] = le.transform(train_X[columns])
 for columns in ['cp_type','cp_dose']:
le.fit(test_X[columns])
 test_X[columns] = le.transform(test_X[columns] )<data_type_conversions>
metric_by_threshold = []
for threshold in np.linspace(0.3, 0.7, 50):
val_binary_prediction =(val_predictions_stacked > threshold ).astype(int)
 iou_values = []
for y_mask, p_mask in zip(val_masks_stacked, val_binary_prediction):
iou = jaccard_similarity_score(y_mask.flatten() , p_mask.flatten())
 iou_values.append(iou)
 iou_values = np.array(iou_values)
 accuracies = [
np.mean(iou_values > iou_threshold)
 for iou_threshold in np.linspace(0.5, 0.95, 10)
 ]
print('Threshold: %.2f, Metric: %.3f' %(threshold, np.mean(accuracies)))
 metric_by_threshold.append(( np.mean(accuracies), threshold))
best_metric, best_threshold = max(metric_by_threshold )
X = train_X.to_numpy()
Y = train_Y.to_numpy()
X_test = test_X.to_numpy()
col = train_Y.columns
num_columns=len(X.T)
<choose_model_class>
threshold = best_threshold
binary_prediction =(all_predictions_stacked > threshold ).astype(int)
 all_masks = []
for p_mask in list(binary_prediction):
p_mask = rle_encoding(p_mask)
 all_masks.append(' '.join(map(str, p_mask)) )
model = Sequential()
model.add(keras.layers.Dense(32,activation='relu',input_shape=(num_columns,)))
 model.add(keras.layers.Dense(16,activation='relu'))
model.add(keras.layers.Dense(206,activation='sigmoid'))
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['acc'])
 history = model.fit(X,Y,epochs=50,batch_size=2000,verbose=1)
<choose_model_class>
submit = pd.DataFrame([test_file_list, all_masks] ).T
submit.columns = ['id', 'rle_mask']
submit.to_csv('submit_baseline_torch.csv', index = False )