Kaggle competition: TGS Salt Identification Challenge
def random_model(inputs):
activationList = ["swish", "selu", "elu", "relu"]
x = inputs
for _ in range(random.randint(1, 5)) :
x = tf.keras.layers.Dense(random.randint(1, 1000),activation=random.choice(activationList))(x)
 x = tf.keras.layers.Dropout(random.uniform(0, 0.5))(x)
 outputs = tf.keras.layers.Dense(206,activation='sigmoid' )(x)
 model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
 model.compile(optimizer=tf.keras.optimizers.Adam() , loss="binary_crossentropy")
 history = model.fit(X,Y,epochs=random.randint(20, 60),batch_size=2000,verbose=0)
 return model<choose_model_class>
train = pd.read_csv(".. /input/train.csv",
index_col = 0)
 print(train.shape)
 train.head()
def ensemble(models, model_input):
outputs = [model.outputs[0] for model in models]
y = keras.layers.Average()(outputs)
 model = keras.Model(model_input, y, name='ensemble')
 return model<save_to_csv>
haveMask = ~(train.rle_mask.isna())
 haveMask.sum()
inputs = tf.keras.layers.Input(shape =(num_columns,))
all_models = []
num_models = 3
for i in range(num_models):
print("training model: ", i+1)
 all_models.append(random_model(inputs))
model_x = ensemble(all_models, inputs)
 preds = model_x.predict(X_test,verbose=0)
 sample_sub.iloc[:,1:] = preds
display(sample_sub.head())
 sample_sub.to_csv("submission.csv",index=False)
<choose_model_class>
tgs = ".. /input"
tgs1 = ".. /input/tgs-salt-identification-challenge"
def getImage(imgId):
path = Path(tgs+"/train/images/")/ '{}'.format(imgId)
 img = imread(path)
 return img.astype(np.uint8)
 def getGrayImage(imgId):
path = Path(tgs+"/train/images/")/'{}'.format(imgId)
 img = imread(path ).astype(np.uint8)
 img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
 return img
def getGrayTestImage(imgId):
path = Path(tgs+"/test/images/")/'{}'.format(imgId)
 img = imread(path ).astype(np.uint8)
 img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
 return img
def getMask(imgId):
path = Path(tgs+"/train/masks/")/ '{}'.format(imgId)
 img = imread(path)
 return img.astype(np.uint8 )
def random_model_no_train(inputs):
activationList = ["swish", "selu", "elu", "relu"]
x = inputs
for _ in range(random.randint(1, 5)) :
x = tf.keras.layers.Dense(random.randint(1, 1000),activation=random.choice(activationList))(x)
 x = tf.keras.layers.Dropout(random.uniform(0, 0.5))(x)
 outputs = tf.keras.layers.Dense(206,activation='sigmoid' )(x)
 model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
 model.compile(optimizer=tf.keras.optimizers.Adam() , loss="binary_crossentropy")
 return model
num_graph = 20
all_graph = []
for i in range(num_graph):
print("training model: ", i+1)
 all_graph.append(random_model_no_train(inputs))
model_graph = ensemble(all_graph, inputs)
 tf.keras.utils.plot_model(model_graph, to_file='model_plot.png', show_shapes=True, show_layer_names=True)
<train_on_grid>
thresholds = [0.5, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95]
n_thresholds = len(thresholds)
 def IoUhelper(TrueMask, predictedMask):
intersection = cv2.bitwise_and(TrueMask, predictedMask)
 union = cv2.bitwise_or(TrueMask, predictedMask)
 intersectionCnt = cv2.countNonZero(intersection)
 unionCnt = cv2.countNonZero(union)
 return intersectionCnt, unionCnt
def meanHit(TrueMask, predictedMask):
hitCnt = 0
intersectionCnt, unionCnt = IoUhelper(TrueMask, predictedMask)
 if(intersectionCnt == 0 and unionCnt == 0):
return 1
if(intersectionCnt == 0 and unionCnt != 0):
return 0
IoU = intersectionCnt/unionCnt
for t in thresholds:
hitCnt+=int(IoU > t)
 return hitCnt/n_thresholds
def accuracy(mask, mask_pred):
num = mask.shape[0]
scores = []
for i in range(num):
score = meanHit(mask[i], mask_pred[i])
 scores.append(score)
 return np.mean(scores)
 def mean_iou(y_true, y_pred):
prec = []
y_pred_ = tf.to_int32(y_pred > 0.55)
 y_true = tf.to_int32(y_true > 0.55)
 for t in np.arange(0.5, 1.0, 0.05):
score, up_opt = tf.metrics.mean_iou(y_true, y_pred_, 2)
 K.get_session().run(tf.local_variables_initializer())
 with tf.control_dependencies([up_opt]):
score = tf.identity(score)
 prec.append(score)
 return K.mean(K.stack(prec), axis=0)
 def get_iou_vector(A, B):
batch_size = A.shape[0]
metric = []
for batch in range(batch_size):
t, p = A[batch]>0, B[batch]>0
intersection = np.logical_and(t, p)
 union = np.logical_or(t, p)
 iou =(np.sum(intersection > 0)+ 1e-10)/(np.sum(union > 0)+ 1e-10)
 thresholds = np.arange(0.5, 1, 0.05)
 s = []
for thresh in thresholds:
s.append(iou > thresh)
 metric.append(np.mean(s))
return np.mean(metric)
 def my_iou_metric(label, pred):
return -tf.py_func(get_iou_vector, [label, pred>0.5], tf.float64)
 def IoU(y_true, y_pred, eps=1e-6):
if np.max(y_true)== 0.0:
return IoU(1-y_true, 1-y_pred)
 intersection = K.sum(y_true * y_pred, axis=[1,2,3])
 union = K.sum(y_true, axis=[1,2,3])+ K.sum(y_pred, axis=[1,2,3])- intersection
return -K.mean(( intersection + eps)/(union + eps), axis=0 )
def random_model_last_epoch_loss(inputs):
activationList = ["swish", "selu", "elu", "relu"]
x = inputs
for _ in range(random.randint(1, 5)) :
x = tf.keras.layers.Dense(random.randint(1, 1000),activation=random.choice(activationList))(x)
 x = tf.keras.layers.Dropout(random.uniform(0, 0.5))(x)
 outputs = tf.keras.layers.Dense(206,activation='sigmoid' )(x)
 model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
 model.compile(optimizer=tf.keras.optimizers.Adam() , loss="binary_crossentropy")
 history = model.fit(X,Y,epochs=random.randint(20, 60),batch_size=2000,verbose=0)
 return history.history["loss"]
results = []
model_range = range(2, 4)
 for num_models in model_range:
all_models = []
for i in range(num_models):
print("training model: ", i+1)
 all_models.append(random_model_last_epoch_loss(inputs))
results.append(( sum(all_models[:][-1])/ len(all_models)))
 plt.plot(model_range, results)
 plt.xlabel('number of models', fontsize=18)
 plt.ylabel('averaged last epoch lost', fontsize=16)
<load_from_csv>
def rle_encoding(mask):
mask = mask.ravel()
encoding = ""
i = 0
while(i<len(mask)) :
currCnt = 0
start = i
if(mask[i] == 255):
while(i < len(mask)and mask[i] == 255):
currCnt+=1
i+=1
encoding+=(" "+ str(start+1)+ " " + str(currCnt))
else: i+=1
return encoding.strip()
PROBE_COLUMN = 0
sub = pd.read_csv('.. /input/lish-moa/sample_submission.csv')
 print('Submission file has shape', sub.shape)
 print('We will probe target',sub.columns[1+PROBE_COLUMN] )<save_to_csv>
imgId = train[haveMask].index[18]
rle_orig = train[haveMask].rle_mask[18]
img1 = getImage(imgId+".png")
 img2 = getMask(imgId+".png")
 rle1 = rle_encoding(np.transpose(img2))
print("Original Encoding
", rle_orig)
 print("Len1 = ", len(rle_orig.split()))
print("-------")
 print("Encoding
", rle1)
 print("Len2 = ", len(rle1.split()))
sub.iloc[:,1:] = 0
sub.iloc[:,1+PROBE_COLUMN] = 1
sub.to_csv('submission_probe_%i.csv'%PROBE_COLUMN)
 sub.head()<feature_engineering>
TRAIN_IMAGE_DIR = tgs+'/train/images'
TRAIN_MASK_DIR = tgs+'/train/masks'
TEST_MASK_DIR = tgs+'/test/images'
im_height = 128
im_width = 128
train_image_list = os.listdir(TRAIN_IMAGE_DIR)
 train_mask_list = os.listdir(TRAIN_MASK_DIR)
 test_image_list = os.listdir(TEST_MASK_DIR )
np.round(( 0.29790 - 0.13073)/(-np.log(1e-15)/820292))<load_from_csv>
X_train_image = np.zeros(( len(train_image_list),
im_height, im_width, 1), dtype = np.uint8)
 Y_train_mask = np.zeros(( len(train_mask_list),
im_height, im_width, 1), dtype = np.uint8)
 for i in tqdm(range(len(train_image_list))):
imgId = train_image_list[i]
img = getGrayImage(imgId)
 img = cv2.resize(img,(im_height, im_width))
X_train_image[i] = img.reshape(im_height, im_width, 1)
 img = getMask(imgId)
 img = cv2.resize(img,(im_height, im_width))
Y_train_mask[i] = img.reshape(im_height, im_width, 1)
 print("X_train Shape = ", X_train_image.shape, "Y_train Shape = ", Y_train_mask.shape )
test_counts = pd.read_csv('.. /input/moa-test-target-means/target_counts.csv')
 test_counts.head()<load_from_csv>
train_targets = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv' )<filter>
print(np.count_nonzero(Y_train_mask[2]))
print(cntOne[2] )
test_counts.loc[test_counts.train_ct==6].target.iloc[0]<compute_test_metric>
x = np.array([0, 1.0, 3.0, 1.6])
 bins = np.array([0, 1.0, 2.5, 4.0, 10.0])
 inds = np.digitize(x, bins)
 np.unique(inds )
log_loss(train_targets['diuretic'], np.ones(23814)*6/23814 )<filter>
bins = np.array([0, 1600, 3200, 4800, 6400, 8000, 9600, 11200, 12800, 14400])
 labels = np.digitize(cntOne, bins)
 print(np.unique(labels), labels.shape )
test_counts.loc[test_counts.train_ct==36].target.iloc[0]<compute_test_metric>
train_X, val_X, train_Y, val_Y = train_test_split(X_train_image, Y_train_mask,
test_size = 0.1,
random_state = 43,
stratify = labels)
 print("TrainX shape = ", train_X.shape, "TrainY Shape = ", train_Y.shape)
 print("ValidationX Shape = ", val_X.shape, "ValidationY Shape = ", val_Y.shape )
log_loss(train_targets['casein_kinase_inhibitor'], np.ones(23814)*36/23814 )<feature_engineering>
def augment(X, Y):
print("FlipLR")
 X1 = np.append(X, [np.fliplr(x)for x in X], axis = 0)
 Y1 = np.append(Y, [np.fliplr(y)for y in Y], axis = 0)
 print("Roll")
 X = np.append(X1, [np.roll(x, 40, axis = 1)for x in X1], axis = 0)
 Y = np.append(Y1, [np.roll(y, 40, axis = 1)for y in Y1], axis = 0)
 m = X.shape[0]
np.random.seed(2000)
 index = np.arange(0, m)
 np.random.shuffle(index)
 print("Shuffle")
 X = X[index]
Y = Y[index]
return X, Y
print("Training Augmentation")
 train_X, train_Y = augment(train_X, train_Y)
 val_X, val_Y = augment(val_X, val_Y)
 print(train_X.shape, train_Y.shape)
 print(val_X.shape, val_Y.shape)
( 0.01132 - 0.00234)* 60 / 206<load_from_csv>
print(train_Y[70, :, :, 0])
 train_X =(train_X.astype(np.float32)/255.0)
 train_Y =(train_Y/255.0 ).astype(np.bool ).astype(np.uint8)
 val_X =(val_X.astype(np.float32)/255.0)
 val_Y =(val_Y/255.0 ).astype(np.bool ).astype(np.uint8)
 print(train_X.dtype, train_Y.dtype)
 print(train_Y[70, :, :, 0] )
! python -W ignore.. /input/finalmoascripts/inference-tabnet-1830.py
sub = pd.read_csv('submission_tabnet_01830.csv')
 test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv' )<normalization>
import keras
def scale(x,k):
x = x.copy()
idx = np.where(x!=1)[0]
y = k * x[idx] /(1-x[idx])
 x[idx] = y/(1+y)
 return x<feature_engineering>
from keras.models import *
from keras.layers import *
from keras.optimizers import *
from keras.callbacks import *
public = pd.read_csv('.. /input/moa-test-target-means/sample_public_submission.csv' ).sig_id.values
SZ = 3982; SZ2 = 3982*3
for c in test_counts.target.values:
m2 = sub.loc[sub.sig_id.isin(public),c].mean()
m3 = sub.loc[~sub.sig_id.isin(public),c].mean()
m = test_counts.loc[test_counts.target==c,'public_ct'].values[0]/SZ /m2
sub.loc[sub.sig_id.isin(public),c] = scale(sub.loc[sub.sig_id.isin(public),c].values,m)
 if len(sub)>len(public):
m = test_counts.loc[test_counts.target==c,'private_ct'].values[0]/SZ2 /m3
sub.loc[~sub.sig_id.isin(public),c] = scale(sub.loc[~sub.sig_id.isin(public),c].values,m)
 print('Updated',len(test_counts),'targets' )<save_to_csv>
def batchActivate(x):
x = BatchNormalization()(x)
 x = Activation('relu' )(x)
 return x
def residualBlock(blockInput, numChannel, matchChannel = False):
x = batchActivate(blockInput)
 x = Conv2D(numChannel,(3, 3), activation= None, padding = "same",
use_bias = False )(x)
 x = batchActivate(x)
 x = Conv2D(numChannel,(3, 3), activation=None, padding = "same",
use_bias = False )(x)
 if matchChannel:
blockInput = Conv2D(numChannel,(1, 1), activation= None, padding = "same",
use_bias = False )(x)
 x = Add()([x, blockInput])
 return x
s = sub.loc[(test.cp_type=='ctl_vehicle' ).values].mean().sum()
print('Control rows in submission have mean = %i'%s)
 sub.to_csv('submission.csv',index=False)
 sub.head()<import_modules>
inputs = Input(shape =(im_height, im_width, 1))
init = Conv2D(16,(7, 7), activation="relu", padding = "same" )(inputs)
 conv1 = residualBlock(init, 16)
 conv1 = residualBlock(conv1, 16)
 c1 = residualBlock(conv1, 16)
 c1 = BatchNormalization()(c1)
 p1 = MaxPooling2D(pool_size=(2, 2))(c1)
 conv2 = residualBlock(p1, 32, True)
 c2 = residualBlock(conv2, 32)
 c2 = BatchNormalization()(c2)
 p2 = MaxPooling2D(pool_size =(2, 2))(c2)
 conv3 = residualBlock(p2, 64, True)
 c3 = residualBlock(conv3, 64)
 c3 = BatchNormalization()(c3)
 p3 = MaxPooling2D(pool_size =(2, 2))(c3)
 conv4 = residualBlock(p3, 128, True)
 c4 = residualBlock(conv4, 128)
 c4 = BatchNormalization()(c4)
 p4 = MaxPooling2D(pool_size=(2, 2))(c4)
 conv5 = residualBlock(p4, 256, True)
 conv5 = residualBlock(conv5, 256)
 conv5 = residualBlock(conv5, 256)
 c5 = residualBlock(conv5, 256)
 c5 = BatchNormalization()(c5)
 u6 = Conv2DTranspose(128,(2, 2), strides=(2, 2), activation = "relu",
padding = "same" )(c5)
 u6 = concatenate([u6, c4])
 conv6 = residualBlock(u6, 128, True)
 conv6 = residualBlock(conv6, 128)
 u7 = Conv2DTranspose(64,(2, 2), strides =(2, 2,), activation = "relu",
padding = "same" )(conv6)
 u7 = concatenate([u7, c3])
 conv7 = residualBlock(u7, 64, True)
 conv7 = residualBlock(conv7, 64)
 u8 = Conv2DTranspose(32,(2, 2), strides =(2, 2), activation = "relu",
padding = "same" )(conv7)
 u8 = concatenate([u8, c2])
 conv8 = residualBlock(u8, 32, True)
 conv8 = residualBlock(conv8, 32)
 u9 = Conv2DTranspose(16,(2, 2), strides =(2, 2), activation = "relu",
padding = "same" )(conv8)
 u9 = concatenate([u9, c1], axis=3)
 conv9 = residualBlock(u9, 16, True)
 conv9 = residualBlock(conv9, 16)
 conv9 = residualBlock(conv9, 16)
 outputs1 = Conv2D(16,(1, 1), activation = "relu", padding = "same" )(conv9)
 outputs = Conv2D(1,(1, 1), activation = "sigmoid" )(outputs1)
 model = Model(inputs = [inputs], outputs = [outputs])
 optimizer = keras.optimizers.Adam()
model.compile(optimizer = optimizer, loss = "binary_crossentropy",
metrics = [my_iou_metric])
 model.summary()
sys.path.append('.. /input/iterativestratification')
 warnings.filterwarnings('ignore')
 os.listdir('.. /input/lish-moa')
 pd.set_option('max_columns', 2000 )<load_from_csv>
earlyStopping = EarlyStopping(patience = 20, verbose = 1)
 checkpointer = ModelCheckpoint('model-tgs-salt-1.h5',monitor = 'val_my_iou_metric',
verbose=1, save_best_only=True)
 reducelr=ReduceLROnPlateau(monitor='val_my_iou_metric',patience=5,
min_lr=0.00001, verbose=1,factor=0.5)
 epochs = 50
batch_size = 64
history = model.fit(train_X , train_Y, epochs = epochs,
batch_size = batch_size,
validation_data =(val_X, val_Y),
callbacks=[earlyStopping, checkpointer, reducelr] )
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets_scored = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 train_targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 sample_submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv')
 train = train_features.merge(train_targets_scored, on='sig_id')
 drug = pd.read_csv('/kaggle/input/lish-moa/train_drug.csv')
 train = train.merge(drug, on="sig_id")
 train["drug_id_cnt"] = train["drug_id"].map(train["drug_id"].value_counts().to_dict())
 train = train[train['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 test = test_features[test_features['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 target_cols = train_targets_scored.drop('sig_id', axis=1 ).columns.values.tolist()
GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]
feature_cols = [c for c in train_features.columns if c not in target_cols]
feature_cols = [c for c in feature_cols if c not in ['fold','sig_id', 'drug_id']] 
<groupby>
model1 = load_model("./model-tgs-salt-1.h5",
custom_objects = {'my_iou_metric':my_iou_metric} )
SEED = 1337
FOLDS = 5
vc = train.drug_id.value_counts()
vc1 = vc.loc[(vc==6)|(vc==12)|(vc==18)].index.sort_values()
vc2 = vc.loc[(vc!=6)&(vc!=12)&(vc!=18)].index.sort_values()
dct1 = {}; dct2 = {}
skf = MultilabelStratifiedKFold(n_splits=FOLDS, shuffle=True, random_state=SEED)
 tmp = train.groupby('drug_id')[target_cols].mean().loc[vc1]
for fold,(idxT,idxV)in enumerate(skf.split(tmp,tmp[target_cols])) :
dd = {k:fold for k in tmp.index[idxV].values}
dct1.update(dd)
 skf = MultilabelStratifiedKFold(n_splits=FOLDS, shuffle=True, random_state=SEED)
 tmp = train.loc[train.drug_id.isin(vc2)].reset_index(drop=True)
 for fold,(idxT,idxV)in enumerate(skf.split(tmp,tmp[target_cols])) :
dd = {k:fold for k in tmp.sig_id[idxV].values}
dct2.update(dd)
 train['fold'] = train.drug_id.map(dct1)
 train.loc[train.fold.isna() ,'fold'] = train.loc[train.fold.isna() ,'sig_id'].map(dct2)
 train.fold = train.fold.astype('int8')
<count_values>
history.history
folds = train["fold"]
train["fold"].value_counts()<define_variables>
lr = history.history['lr']
x = -1
lr1 = []
lrepoch = []
for(n,r)in enumerate(lr):
if r!=x:
lr1.append(r)
 lrepoch.append(n + 1)
 x = r
kf = []
for f in range(FOLDS):
trn_idx = np.where(folds.values!=f)[0]
val_idx = np.where(folds.values==f)[0]
kf.append([trn_idx, val_idx] )<define_variables>
totIoU = 0.0
m = val_X.shape[0]
for i in tqdm(range(m)) :
img = val_X[i:i+1, :, :]
origMask = val_Y[i:i+1, :, :]
predMask = model1.predict(img)
 origMask = cv2.inRange(origMask[0, :, :, 0], 0.55, 255)
 predMask = cv2.inRange(predMask[0, :, :, 0], 0.55, 255)
 totIoU += meanHit(origMask, predMask)
 print("mean IoU = ", totIoU/m )
DEVICE =('cuda' if torch.cuda.is_available() else 'cpu')
 class MoADataset:
def __init__(self, genes, cells, features, sample_weights=None, targets=None):
self.genes = genes
self.cells = cells
self.features = features
self.sample_weights = sample_weights
self.targets = targets
if self.sample_weights is None:
self.sample_weights = np.zeros(len(self.genes))
if self.targets is None:
self.targets = np.zeros(( len(self.genes),1))
def __len__(self):
return(self.genes.shape[0])
 def __getitem__(self, idx):
dct = {
'genes' : torch.tensor(self.genes[idx,:], dtype=torch.float),
'cells' : torch.tensor(self.cells[idx,:], dtype=torch.float),
'features': torch.tensor(self.features[idx,:], dtype=torch.float),
'sample_weights': torch.tensor(self.sample_weights[idx], dtype=torch.float),
'y' : torch.tensor(self.targets[idx, :], dtype=torch.float)
 }
return dct
class FE() :
def __init__(self):
self.fnames = []
return
def fit(self, df):
''
self.ohe = {}
for f in ["cp_time", "cp_dose"]:
self.ohe[f] = OneHotEncoder(sparse=False ).fit(df[f].values.reshape(-1,1))
def transform(self, df, is_train=True):
if is_train:
y = df[target_cols]
else:
y = None
X = pd.DataFrame()
X[GENES] = df[GENES]
X[CELLS] = df[CELLS]
for f in self.ohe.keys() :
oh = self.ohe[f].transform(df[f].values.reshape(-1,1))
oh = pd.DataFrame(oh, columns=[f"{f}_{c}" for c in self.ohe[f].get_feature_names() ])
 X = pd.concat([X, oh], axis=1)
 self.fnames += list(oh.columns)
 return X, y
fe = FE()
fe.fit(pd.concat([train, test], axis=0))
X_train, Y_train = fe.transform(train)
 X_test, Y_test = fe.transform(test, is_train=False)
<categorify>
X_test_image = X_test_image/255.0
def metric(y, p):
loss = 0
y_pred_clip = np.clip(p, 1e-15, 1 - 1e-15)
 for i in range(p.shape[1]):
loss += - np.mean(y[:, i] * np.log(y_pred_clip[:, i])+(1 - y[:, i])* np.log(1 - y_pred_clip[:, i]))
return loss / p.shape[1]
train["sample_weight"] = 1
def get_activation(act):
if act == "leaky_relu":
return nn.LeakyReLU()
if act == "relu":
return nn.ReLU()
if act == "prelu":
return nn.PReLU()
def get_bn(bn, l):
if bn == True:
return nn.BatchNorm1d(l)
 else:
return nn.Identity()
class Model(nn.Module):
def __init__(self, num_targets):
super(Model, self ).__init__()
hidden1 = 128
hidden2 = 256
dropout1 = 0.2749655379470675
dropout2 = 0.059365935529546235
dropout3 = 0.33205409463214886
activation1 = "leaky_relu"
activation2 = "prelu"
activation3 = "prelu"
bn1 = True
bn2 = False
bn3 = False
bn4 = True
self.genes = nn.Sequential(
 get_bn(bn1, len(GENES)) ,
nn.utils.weight_norm(nn.Linear(len(GENES), hidden1, bias=True)) ,
get_activation(activation1),
get_bn(bn2, hidden1),
nn.Dropout(dropout1), 
)
 self.cells = nn.Sequential(
 get_bn(bn3, len(CELLS)) ,
nn.utils.weight_norm(nn.Linear(len(CELLS), hidden2, bias=True)) ,
get_activation(activation2),
get_bn(bn4, hidden2),
nn.Dropout(dropout2), 
)
 self.out = nn.Sequential(
 nn.utils.weight_norm(nn.Linear(hidden1+hidden2, hidden1+hidden2, bias=False)) ,
get_activation(activation3),
nn.BatchNorm1d(hidden1+hidden2),
nn.Dropout(dropout3),
nn.utils.weight_norm(nn.Linear(hidden1+hidden2, num_targets, bias=True)) , 
)
 def forward(self, data):
genes = data["genes"].to(DEVICE)
 cells = data["cells"].to(DEVICE)
 genes = self.genes(genes)
 cells = self.cells(cells)
 x = torch.cat([genes, cells], dim=1)
 out = self.out(x)
 return out
EPOCHS = 29
BATCH_SIZE = 512
NFOLDS = 5
NBAGS = 5
if len(test)> 3624:
FULL_FIT = False
else:
FULL_FIT = False
num_features=X_train.shape[1]
num_targets=Y_train.shape[1]
seed = 42
oof = np.zeros(( len(train), num_targets))
p_test = []
for fold,(trn_idx, val_idx)in enumerate(kf):
if FULL_FIT:
trn_idx = np.arange(len(X_train))
preds_bag = []
for bag in range(NBAGS):
GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]
FNAMES = fe.fnames
x_train, y_train = X_train.iloc[trn_idx].reset_index(drop=True ).copy() , Y_train.values[trn_idx].copy()
x_valid, y_valid = X_train.iloc[val_idx].reset_index(drop=True ).copy() , Y_train.values[val_idx].copy()
x_test = X_test.copy()
pipe = Pipeline([('scaler', QuantileTransformer(n_quantiles=1000, random_state=np.random.randint(10_000), output_distribution="normal")) ])
 pipe.fit(np.concatenate([X_train[GENES], X_test[GENES]], axis=0))
x_train[GENES] = pipe.transform(x_train[GENES])
 x_valid[GENES] = pipe.transform(x_valid[GENES])
 x_test[GENES] = pipe.transform(x_test[GENES])
 pipe = Pipeline([('scaler', RobustScaler(quantile_range=(25.0, 75.0)))])
 pipe.fit(np.concatenate([X_train[CELLS], X_test[CELLS]], axis=0))
x_train[CELLS] = pipe.transform(x_train[CELLS])
 x_valid[CELLS] = pipe.transform(x_valid[CELLS])
 x_test[CELLS] = pipe.transform(x_test[CELLS])
 train_dataset = MoADataset(x_train[GENES].values, x_train[CELLS].values, x_train[FNAMES].values, train["sample_weight"].values[trn_idx], y_train)
 valid_dataset = MoADataset(x_valid[GENES].values, x_valid[CELLS].values, x_valid[FNAMES].values, train["sample_weight"].values[val_idx], y_valid)
 test_dataset = MoADataset(x_test[GENES].values, x_test[CELLS].values, x_test[FNAMES].values)
 trainloader = torch.utils.data.DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
 validloader = torch.utils.data.DataLoader(valid_dataset, batch_size=BATCH_SIZE, shuffle=False)
 testloader = torch.utils.data.DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False)
 model = Model(
 num_targets=num_targets 
)
 model.to(DEVICE)
 optimizer = torch.optim.Adam(model.parameters() , lr=0.04647353847564317, weight_decay=8.087569236449597e-06)
 scheduler = get_cosine_schedule_with_warmup(optimizer, num_warmup_steps=len(trainloader)*EPOCHS//2, num_training_steps=len(trainloader)*EPOCHS)
 loss_fn = nn.BCEWithLogitsLoss()
best_loss = np.inf
best_preds = None
for epoch in range(EPOCHS):
start_time = time.time()
model.train()
train_loss = 0
progress_bar = tqdm(trainloader, total=len(trainloader), disable=True)
 for data in progress_bar:
optimizer.zero_grad()
outputs = model(data)
 loss = nn.BCEWithLogitsLoss(reduction="mean" )(outputs, data['y'].to(DEVICE))
loss.backward()
optimizer.step()
if scheduler is not None:
scheduler.step()
train_loss += loss.item() / len(trainloader)
 progress_bar.set_description(f"loss: {loss.item() :.2f}")
 model.eval()
valid_loss = 0
valid_targets = []
valid_preds = []
for data in validloader:
outputs = model(data)
 loss = loss_fn(outputs, data["y"].to(DEVICE))
valid_loss += loss.item() / len(validloader)
 valid_targets.append(data["y"])
 valid_preds.append(outputs.sigmoid())
 valid_targets = torch.cat(valid_targets)
 valid_preds = torch.cat(valid_preds)
 valid_preds = valid_preds.detach().cpu().numpy()
valid_score = metric(y_valid, valid_preds)
 print(f"Fold {fold} Epoch {epoch} {time.time() -start_time:.2f}s train_loss: {train_loss:.5f}, val_loss: {valid_loss:.5f}, val_score: {valid_score:.5f}")
 best_preds = valid_preds
print()
preds_bag.append(best_preds)
 bag_score = metric(y_valid, np.mean(preds_bag, axis=0))
print(f"Bag {bag} val_score: {bag_score:.5f}")
 print()
test_preds = []
for data in testloader:
outputs = model(data)
 test_preds.append(outputs.sigmoid())
 test_preds = torch.cat(test_preds ).detach().cpu().numpy()
p_test.append(test_preds)
 oof[val_idx] = np.mean(preds_bag, axis=0)
 print() 
<compute_test_metric>
gc.collect()
metric(Y_train.values, oof )<feature_engineering>
origSize =(101, 101)
 submit_names = []
submit_rleMasks = []
for i in tqdm(range(len(test_image_list))):
img = X_test_image[i:i+1,:,:]
predMask = model1.predict(img)
 predMask = cv2.inRange(predMask[0, :, :, 0], 0.55, 255)
 predMask = cv2.resize(predMask, origSize)
 submit_names.append(test_image_list[i][:-4])
 submit_rleMasks.append(rle_encoding(np.transpose(predMask)) )
p_test = np.mean(p_test, axis=0 )<feature_engineering>
sub = pd.DataFrame({'id':submit_names, 'rle_mask': submit_rleMasks})
 print(sub.shape)
 sub.head()
test[target_cols] = p_test 
<save_to_csv>
for i in tqdm(range(len(test_image_list))):
if(len(sub.iloc[i, 1])== 0):
sub.iloc[i, 1] = np.nan
sub = sample_submission.drop(columns=target_cols ).merge(test[['sig_id']+target_cols], on='sig_id', how='left' ).fillna(0)
 sub.to_csv('submission.csv', index=False )<import_modules>
sub.to_csv("tgsModel1.csv", index = False )