Kaggle competition: RANZCR CLiP - Catheter and Line Position Challenge
<SOS> metric: MCAUC Kaggle data source: ranzcr-clip-catheter-and-line-position-challenge<choose_model_class>
sys.path.append('.. /input/pytorch-images-seresnet')
 warnings.filterwarnings('ignore')
 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu' )
def build_model(embedding_matrix):
words = Input(shape=(None,))
x = Embedding(*embedding_matrix.shape, weights=[embedding_matrix], trainable=False )(words)
 x1 = SpatialDropout1D(0.2 )(x)
 x = Bidirectional(CuDNNGRU(LSTM_UNITS, return_sequences = True))(x1)
 x = Conv1D(64, kernel_size = 2, padding = "valid", kernel_initializer = "he_uniform" )(x)
 y = Bidirectional(CuDNNLSTM(LSTM_UNITS, return_sequences = True))(x1)
 y = Conv1D(64, kernel_size = 2, padding = "valid", kernel_initializer = "he_uniform" )(y)
 avg_pool1 = GlobalAveragePooling1D()(x)
 max_pool1 = GlobalMaxPooling1D()(x)
 avg_pool2 = GlobalAveragePooling1D()(y)
 max_pool2 = GlobalMaxPooling1D()(y)
 x = concatenate([avg_pool1, max_pool1, avg_pool2, max_pool2])
 x = Dense(6, activation = "sigmoid" )(x)
 model = Model(inputs = words, outputs = x)
 model.compile(loss = "binary_crossentropy", optimizer = "adam", metrics = ["accuracy"])
 return model<train_model>
IMAGE_SIZE = 640
BATCH_SIZE = 64
TEST_PATH = '.. /input/ranzcr-clip-catheter-line-classification/test'
MODEL_PATH_resnet200d = '.. /input/resnet200d-public/resnet200d_320_CV9632.pth'
MODEL_PATH_seresnet152d = '.. /input/seresnet152d-cv9615/seresnet152d_320_CV96.15.pth'
MODEL_PATH_efficientnet_b5 = '.. /input/efficientnetb5cv9621/tf_efficientnet_b5_ns_CV96.21.pth'
EPOCHS = 5
SEEDS = 10
pred = 0
for ii in range(SEEDS):
model = build_model(embedding_matrix)
 for global_epoch in range(EPOCHS):
print(global_epoch)
 model.fit(
 x_train,
y_train,
validation_data =(x_valid, y_valid),
batch_size=128,
epochs=1,
verbose=2,
callbacks=[
LearningRateScheduler(lambda _: 1e-3 *(0.5 ** global_epoch))
] 
)
 val_preds = model.predict(x_valid)
 AUC = 0
for i in range(6):
AUC += roc_auc_score(y_valid[:,i], val_preds[:,i])/6.
 print(AUC)
 pred += model.predict(x_test, batch_size = 1024, verbose = 1)/SEEDS
np.save('pred', pred)
 model.save_weights('model_weights_'+str(ii)+'.h5')
 os.system('gzip '+'model_weights_'+str(ii)+'.h5' )<save_to_csv>
test = pd.read_csv('.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv' )
submission[list_classes] =(pred)
 submission.to_csv("submission.csv", index = False )<set_options>
def get_transforms() :
return Compose([
Resize(IMAGE_SIZE, IMAGE_SIZE),
Normalize() ,
ToTensorV2() ,
] )
print(tf.__version__)
 tf.test.is_gpu_available(
 cuda_only=False,
min_cuda_compute_capability=None
)<define_variables>
class ResNet200D(nn.Module):
def __init__(self, model_name='resnet200d_320'):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.fc.in_features
self.model.global_pool = nn.Identity()
self.model.fc = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.fc = nn.Linear(n_features, 11)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.fc(pooled_features)
 return output
class SeResNet152D(nn.Module):
def __init__(self, model_name='seresnet152d_320'):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.fc.in_features
self.model.global_pool = nn.Identity()
self.model.fc = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.fc = nn.Linear(n_features, 11)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.fc(pooled_features)
 return output
class EfficientNetB5(nn.Module):
def __init__(self, model_name='tf_efficientnet_b5_ns'):
super().__init__()
self.model = timm.create_model(model_name, pretrained=False)
 n_features = self.model.classifier.in_features
self.model.global_pool = nn.Identity()
self.model.classifier = nn.Identity()
self.pooling = nn.AdaptiveAvgPool2d(1)
 self.classifier = nn.Linear(n_features, 11)
 def forward(self, x):
bs = x.size(0)
 features = self.model(x)
 pooled_features = self.pooling(features ).view(bs, -1)
 output = self.classifier(pooled_features)
 return output
EMBEDDING_FILES = [
'.. /input/fasttext-crawl-300d-2m/crawl-300d-2M.vec',
'.. /input/glove840b300dtxt/glove.840B.300d.txt'
]
BATCH_SIZE = 512
LSTM_UNITS = 128
DENSE_HIDDEN_UNITS = 4 * LSTM_UNITS
EPOCHS = 4
MAX_LEN = 220
TEXT_COLUMN = 'comment_text'
list_classes = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]
CHARS_TO_REMOVE = '!"
“”’'∞θ÷α•à−β∅³π‘₹´°£€\×™√²—'<load_from_csv>
def inference(models, test_loader, device):
tk0 = tqdm(enumerate(test_loader), total=len(test_loader))
probs = []
for i,(images)in tk0:
images = images.to(device)
 avg_preds = []
for model in models:
with torch.no_grad() :
y_preds1 = model(images)
 y_preds2 = model(images.flip(-1))
y_preds =(y_preds1.sigmoid().to('cpu' ).numpy() + y_preds2.sigmoid().to('cpu' ).numpy())/ 2
avg_preds.append(y_preds)
 avg_preds = np.mean(avg_preds, axis=0)
 probs.append(avg_preds)
 probs = np.concatenate(probs)
 return probs
train_df = pd.read_csv('.. /input/jigsaw-toxic-comment-classification-challenge/train.csv')
 test_df = pd.read_csv('.. /input/jigsaw-toxic-comment-classification-challenge/test.csv')
 submission = pd.read_csv(".. /input/jigsaw-toxic-comment-classification-challenge/sample_submission.csv")
 y = train_df[list_classes].values
x_train = train_df[TEXT_COLUMN].astype(str)
 y_train = y
x_test = test_df[TEXT_COLUMN].astype(str )<categorify>
models200D = []
model = ResNet200D()
model.load_state_dict(torch.load(MODEL_PATH_resnet200d)['model'])
 model.eval()
model.to(device)
 models200D.append(model)
 models152D = []
model = SeResNet152D()
model.load_state_dict(torch.load(MODEL_PATH_seresnet152d)['model'])
 model.eval()
model.to(device)
 models152D.append(model)
 modelsB5 = []
model = EfficientNetB5()
model.load_state_dict(torch.load(MODEL_PATH_efficientnet_b5)['model'])
 model.eval()
model.to(device)
 modelsB5.append(model )
def get_coefs(word, *arr):
return word, np.asarray(arr, dtype='float32')
 def load_embeddings(path):
with open(path)as f:
return dict(get_coefs(*line.strip().split(' ')) for line in f)
 def build_matrix(word_index, path):
embedding_index = load_embeddings(path)
 embedding_matrix = np.zeros(( len(word_index)+ 1, 300))
for word, i in word_index.items() :
try:
embedding_matrix[i] = embedding_index[word]
except KeyError:
pass
return embedding_matrix
def build_model(embedding_matrix):
words = Input(shape=(None,))
x = Embedding(*embedding_matrix.shape, weights=[embedding_matrix], trainable=False )(words)
 x1 = SpatialDropout1D(0.2 )(x)
 x = Bidirectional(CuDNNGRU(LSTM_UNITS, return_sequences = True))(x1)
 x = Conv1D(64, kernel_size = 2, padding = "valid", kernel_initializer = "he_uniform" )(x)
 y = Bidirectional(CuDNNLSTM(LSTM_UNITS, return_sequences = True))(x1)
 y = Conv1D(64, kernel_size = 2, padding = "valid", kernel_initializer = "he_uniform" )(y)
 avg_pool1 = GlobalAveragePooling1D()(x)
 max_pool1 = GlobalMaxPooling1D()(x)
 avg_pool2 = GlobalAveragePooling1D()(y)
 max_pool2 = GlobalMaxPooling1D()(y)
 x = concatenate([avg_pool1, max_pool1, avg_pool2, max_pool2])
 x = Dense(6, activation = "sigmoid" )(x)
 model = Model(inputs = words, outputs = x)
 model.compile(loss = "binary_crossentropy", optimizer = "adam", metrics = ["accuracy"])
 return model<string_transform>
test_dataset = TestDataset(test, transform=get_transforms())
 test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False,
num_workers=4 , pin_memory=True)
 predictions200d = inference(models200D, test_loader, device)
 del models200D
gc.collect()
predictions152d = inference(models152D, test_loader, device)
 del models152D
gc.collect()
predictionsB5 = inference(modelsB5, test_loader, device)
%%time
tokenizer = text.Tokenizer(filters=CHARS_TO_REMOVE)
 tokenizer.fit_on_texts(list(x_train)+ list(x_test))
x_train = tokenizer.texts_to_sequences(x_train)
 x_test = tokenizer.texts_to_sequences(x_test)
 x_train = sequence.pad_sequences(x_train, maxlen=MAX_LEN)
 x_test = sequence.pad_sequences(x_test, maxlen=MAX_LEN )<concatenate>
variable_list = %who_ls
for _ in variable_list:
if _ not in("predictions200d", "predictions152d", "predictionsB5"):
del globals() [_]
%who_ls
%%time
embedding_matrix = np.concatenate(
 [build_matrix(tokenizer.word_index, f)for f in EMBEDDING_FILES], axis=-1 )<split>
ROOT = Path.cwd().parent
INPUT = ROOT / "input"
OUTPUT = ROOT / "output"
DATA = INPUT / "ranzcr-clip-catheter-line-classification"
TRAIN = DATA / "train"
TEST = DATA / "test"
TRAINED_MODEL = INPUT / "ranzcr-clip-weights-for-multi-head-model-v2"
TMP = ROOT / "tmp"
TMP.mkdir(exist_ok=True)
 RANDAM_SEED = 1086
N_CLASSES = 11
FOLDS = [0, 1, 2, 3, 4]
N_FOLD = len(FOLDS)
 IMAGE_SIZE =(512, 512)
 FAST_COMMIT = False
CLASSES = [
'ETT - Abnormal',
'ETT - Borderline',
'ETT - Normal',
'NGT - Abnormal',
'NGT - Borderline',
'NGT - Incompletely Imaged',
'NGT - Normal',
'CVC - Abnormal',
'CVC - Borderline',
'CVC - Normal',
'Swan Ganz Catheter Present'
]
X_train, X_valid, Y_train, Y_valid = train_test_split(x_train, y_train, test_size = 0.1)
<train_model>
for p in DATA.iterdir() :
print(p.name)
 train = pd.read_csv(DATA / "train.csv")
 smpl_sub = pd.read_csv(DATA / "sample_submission.csv")
 smpl_sub.shape
EPOCHS = 5
SEEDS = 10
pred = 0
for ii in range(SEEDS):
model = build_model(embedding_matrix)
 for global_epoch in range(EPOCHS):
print(global_epoch)
 model.fit(
 X_train,
Y_train,
validation_data =(X_valid, Y_valid),
batch_size=128,
epochs=1,
verbose=2,
callbacks=[
LearningRateScheduler(lambda _: 1e-3 *(0.55 ** global_epoch))
] 
)
 val_preds_3 = model.predict(X_valid)
 AUC = 0
for i in range(6):
AUC += roc_auc_score(Y_valid[:,i], val_preds_3[:,i])/6.
 print(AUC)
 pred += model.predict(x_test, batch_size = 1024, verbose = 1)/SEEDS
model.save_weights('model_weights_'+str(ii)+'.h5')
 os.system('gzip '+'model_weights_'+str(ii)+'.h5')
<save_to_csv>
if FAST_COMMIT and len(smpl_sub)== 3582:
smpl_sub = smpl_sub.iloc[:64 * 3].reset_index(drop=True )
submission[list_classes] =(pred)
 submission.to_csv("submission.csv", index = False )<import_modules>
def multi_label_stratified_group_k_fold(label_arr: np.array, gid_arr: np.array, n_fold: int, seed: int=42):

 np.random.seed(seed)
 random.seed(seed)
 start_time = time.time()
n_train, n_class = label_arr.shape
gid_unique = sorted(set(gid_arr))
n_group = len(gid_unique)
 gid2aid = dict(zip(gid_unique, range(n_group)))
 aid_arr = np.vectorize(lambda x: gid2aid[x] )(gid_arr)
 cnts_by_class = label_arr.sum(axis=0)
 col, row = np.array(sorted(enumerate(aid_arr), key=lambda x: x[1])).T
cnts_by_group = coo_matrix(
(np.ones(len(label_arr)) ,(row, col))
).dot(coo_matrix(label_arr)).toarray().astype(int)
 del col
del row
cnts_by_fold = np.zeros(( n_fold, n_class), int)
 groups_by_fold = [[] for fid in range(n_fold)]
group_and_cnts = list(enumerate(cnts_by_group))
np.random.shuffle(group_and_cnts)
 print("finished preparation", time.time() - start_time)
 for aid, cnt_by_g in sorted(group_and_cnts, key=lambda x: -np.std(x[1])) :
best_fold = None
min_eval = None
for fid in range(n_fold):
cnts_by_fold[fid] += cnt_by_g
fold_eval =(cnts_by_fold / cnts_by_class ).std(axis=0 ).mean()
cnts_by_fold[fid] -= cnt_by_g
if min_eval is None or fold_eval < min_eval:
min_eval = fold_eval
best_fold = fid
cnts_by_fold[best_fold] += cnt_by_g
groups_by_fold[best_fold].append(aid)
 print("finished assignment.", time.time() - start_time)
 gc.collect()
idx_arr = np.arange(n_train)
 for fid in range(n_fold):
val_groups = groups_by_fold[fid]
val_indexs_bool = np.isin(aid_arr, val_groups)
 train_indexs = idx_arr[~val_indexs_bool]
val_indexs = idx_arr[val_indexs_bool]
print("[fold {}]".format(fid), end=" ")
 print("n_group:(train, val)=({}, {})".format(n_group - len(val_groups), len(val_groups)) , end=" ")
 print("n_sample:(train, val)=({}, {})".format(len(train_indexs), len(val_indexs)))
 yield train_indexs, val_indexs
import os
import shutil
import numpy as np
import pandas as pd
from PIL import Image
from cv2 import imread, resize, IMREAD_GRAYSCALE
from tensorflow.keras.utils import Sequence
from tensorflow import convert_to_tensor as to_T
from tensorflow.keras.models import load_model 
<prepare_x_and_y>
label_arr = train[CLASSES].values
group_id = train.PatientID.values
train_val_indexs = list(
 multi_label_stratified_group_k_fold(
 label_arr, group_id, N_FOLD, RANDAM_SEED 
)
 )
def bbox(image):

 HEIGHT = image.shape[0]
WIDTH = image.shape[1]
for i in range(image.shape[1]):
if(image[:, i] > 0 ).sum() >= 1:
x_min = i - 1 if(i > 1)else 0
break
for i in reversed(range(image.shape[1])) :
if(image[:, i] > 0 ).sum() >= 1:
x_max = i + 2 if(i < WIDTH - 2)else WIDTH
break
for i in range(image.shape[0]):
if(image[i] > 0 ).sum() >= 1:
y_min = i - 1 if(i > 1)else 0
break
for i in reversed(range(image.shape[0])) :
if(image[i] > 0 ).sum() >= 1:
y_max = i + 2 if(i < HEIGHT - 2)else HEIGHT
break
return x_min, y_min, x_max, y_max
return x_min, y_min, x_max, y_max<categorify>
train["fold"] = -1
for fold_id,(trn_idx, val_idx)in enumerate(train_val_indexs):
train.loc[val_idx, "fold"] = fold_id
train.groupby("fold")[CLASSES].sum()
class Dataset(Sequence):
def __init__(self
, image_list
, batch_size
, dimensions 
):

 self.image_list = image_list
self.batch_size = batch_size
self.dimensions = dimensions
self.on_epoch_end()
def __len__(self):

 return int(np.ceil(len(self.image_list)/ self.batch_size))
def __getitem__(self, index):

 indices = self.indices[index * self.batch_size:(index + 1)* self.batch_size]
images = [self.image_list[k] for k in indices]
X = self.__data_generation(images)
 return X
def __data_generation(self, images):

 X = np.empty(( len(images), self.dimensions["height"], self.dimensions["width"], self.dimensions["channels"]))
for idx, image in enumerate(images):
image = imread(image, IMREAD_GRAYSCALE)
 image = resize(image
,(self.dimensions["width"], self.dimensions["height"])
)
 image = np.expand_dims(image, axis=2)
 X[idx,] = image
X = X / 255.0
return to_T(X)
 def on_epoch_end(self):

 self.indices = np.arange(len(self.image_list)) 
<load_from_csv>
def resize_images(img_id, input_dir, output_dir, resize_to=(512, 512), ext="png"):
img_path = input_dir / f"{img_id}.jpg"
save_path = output_dir / f"{img_id}.{ext}"
img = cv2.imread(str(img_path), cv2.IMREAD_GRAYSCALE)
 img = cv2.resize(img, resize_to)
 cv2.imwrite(str(save_path), img,)
 TEST_RESIZED = TMP / "test_{0}x{1}".format(*IMAGE_SIZE)
 TEST_RESIZED.mkdir(exist_ok=True)
 TEST_RESIZED
_ = Parallel(n_jobs=2, verbose=5 )([
delayed(resize_images )(img_id, TEST, TEST_RESIZED, IMAGE_SIZE, "png")
 for img_id in smpl_sub.StudyInstanceUID.values
] )
df = pd.read_csv(".. /input/Kannada-MNIST/test.csv")
 ids = []
image_list = []
os.makedirs("images")
 for i,row in df.iterrows() :
id = "{}".format(i)
 img = np.reshape(row[1:].values, newshape=(28,28)).astype(np.uint8)
 x_min, y_min, x_max, y_max = bbox(img)
 img = img[y_min:y_max, x_min:x_max]
x_min, y_min, x_max, y_max = bbox(img)
 img = img[y_min:y_max, x_min:x_max]
img = Image.fromarray(img)
 img = img.resize(( 28,28), Image.ANTIALIAS)
 img.save("images/{}.png".format(id))
ids.append(id)
 image_list.append("images/{}.png".format(id))
test_df = pd.DataFrame({"id": ids, "label": 0} )<create_dataframe>
def get_activation(activ_name: str="relu"):
act_dict = {
"relu": nn.ReLU(inplace=True),
"tanh": nn.Tanh() ,
"sigmoid": nn.Sigmoid() ,
"identity": nn.Identity() }
if activ_name in act_dict:
return act_dict[activ_name]
else:
raise NotImplementedError
class Conv2dBNActiv(nn.Module):

 def __init__(
 self, in_channels: int, out_channels: int,
kernel_size: int, stride: int=1, padding: int=0,
bias: bool=False, use_bn: bool=True, activ: str="relu" 
):
super(Conv2dBNActiv, self ).__init__()
layers = []
layers.append(nn.Conv2d(
 in_channels, out_channels,
kernel_size, stride, padding, bias=bias))
if use_bn:
layers.append(nn.BatchNorm2d(out_channels))
layers.append(get_activation(activ))
self.layers = nn.Sequential(*layers)
 def forward(self, x):

 return self.layers(x)
 class SpatialAttentionBlock(nn.Module):

 def __init__(
 self, in_channels: int,
out_channels_list: tp.List[int], 
):

 super(SpatialAttentionBlock, self ).__init__()
self.n_layers = len(out_channels_list)
 channels_list = [in_channels] + out_channels_list
assert self.n_layers > 0
assert channels_list[-1] == 1
for i in range(self.n_layers - 1):
in_chs, out_chs = channels_list[i: i + 2]
layer = Conv2dBNActiv(in_chs, out_chs, 3, 1, 1, activ="relu")
 setattr(self, f"conv{i + 1}", layer)
 in_chs, out_chs = channels_list[-2:]
layer = Conv2dBNActiv(in_chs, out_chs, 3, 1, 1, activ="sigmoid")
 setattr(self, f"conv{self.n_layers}", layer)
 def forward(self, x):

 h = x
for i in range(self.n_layers):
h = getattr(self, f"conv{i + 1}" )(h)
 h = h * x
return h
ds = Dataset(image_list=image_list
, batch_size=512
, dimensions={"height":28, "width":28, "channels":1}
)<load_pretrained>
class MultiHeadResNet200D(nn.Module):
def __init__(
 self, out_dims_head: tp.List[int]=[3, 4, 3, 1], pretrained=False 
):
self.base_name = "resnet200d_320"
self.n_heads = len(out_dims_head)
 super(MultiHeadResNet200D, self ).__init__()
base_model = timm.create_model(
 self.base_name, num_classes=sum(out_dims_head), pretrained=False)
 in_features = base_model.num_features
if pretrained:
pretrained_model_path = '.. /input/startingpointschestx/resnet200d_320_chestx.pth'
state_dict = dict()
for k, v in torch.load(pretrained_model_path, map_location='cpu')["model"].items() :
if k[:6] == "model.":
k = k.replace("model.", "")
 state_dict[k] = v
base_model.load_state_dict(state_dict)
 base_model.reset_classifier(0, '')
 self.backbone = base_model
for i, out_dim in enumerate(out_dims_head):
layer_name = f"head_{i}"
layer = nn.Sequential(
 SpatialAttentionBlock(in_features, [64, 32, 16, 1]),
nn.AdaptiveAvgPool2d(output_size=1),
nn.Flatten(start_dim=1),
nn.Linear(in_features, in_features),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(in_features, out_dim))
setattr(self, layer_name, layer)
 def forward(self, x):
h = self.backbone(x)
 hs = [
getattr(self, f"head_{i}" )(h)for i in range(self.n_heads)]
y = torch.cat(hs, axis=1)
 return y
m = MultiHeadResNet200D([3, 4, 3, 1], False)
 m = m.eval()
x = torch.rand(1, 3, 256, 256)
 with torch.no_grad() :
y = m(x)
 print("[forward test]")
 print("input:\t{}
output:\t{}".format(x.shape, y.shape))
del m; del x; del y
gc.collect()
net = load_model(".. /input/kannada-mnist-tensorflow-2/CustomNetV2.hdf5" )<predict_on_test>
class LabeledImageDataset(data.Dataset):

 def __init__(
 self,
file_list: tp.List[
tp.Tuple[tp.Union[str, Path], tp.Union[int, float, np.ndarray]]
],
transform_list: tp.List[tp.Dict], 
):

 self.file_list = file_list
self.transform = ImageTransformForCls(transform_list)
 def __len__(self):

 return len(self.file_list)
 def __getitem__(self, index):

 img_path, label = self.file_list[index]
img = self._read_image_as_array(img_path)
 img, label = self.transform(( img, label))
return img, label
def _read_image_as_array(self, path: str):

 img_arr = cv2.imread(str(path))
img_arr = cv2.cvtColor(img_arr, cv2.COLOR_BGR2RGB)
 return img_arr
predictions = net.predict(ds ).argmax(-1 )<feature_engineering>
def get_dataloaders_for_inference(
 file_list: tp.List[tp.List], batch_size=64 
):

 dataset = LabeledImageDataset(
 file_list,
transform_list=[
[
"Normalize",
{
"always_apply": True,
"max_pixel_value": 255.0,
"mean": ["0.4887381077884414"],
"std": ["0.23064819430546407"]
}
],
["ToTensorV2", {"always_apply": True}],
] 
)
 loader = data.DataLoader(
 dataset,
batch_size=batch_size,
shuffle=False,
num_workers=2,
pin_memory=True,
drop_last=False 
)
 return loader
test_df["label"] = predictions<save_to_csv>
class ImageTransformBase:

 def __init__(self, data_augmentations: tp.List[tp.Tuple[str, tp.Dict]]):

 augmentations_list = [
self._get_augmentation(aug_name )(**params)
 for aug_name, params in data_augmentations]
self.data_aug = albumentations.Compose(augmentations_list)
 def __call__(self, pair: tp.Tuple[np.ndarray])-> tp.Tuple[np.ndarray]:

 raise NotImplementedError
def _get_augmentation(self, aug_name: str)-> tp.Tuple[ImageOnlyTransform, DualTransform]:

 if hasattr(albumentations, aug_name):
return getattr(albumentations, aug_name)
 else:
return eval(aug_name)
 class ImageTransformForCls(ImageTransformBase):

 def __init__(self, data_augmentations: tp.List[tp.Tuple[str, tp.Dict]]):

 super(ImageTransformForCls, self ).__init__(data_augmentations)
 def __call__(self, in_arrs: tp.Tuple[np.ndarray])-> tp.Tuple[np.ndarray]:

 img, label = in_arrs
augmented = self.data_aug(image=img)
 img = augmented["image"]
return img, label
test_df.to_csv("submission.csv",index=False )<set_options>
def load_setting_file(path: str):

 with open(path)as f:
settings = yaml.safe_load(f)
 return settings
def set_random_seed(seed: int = 42, deterministic: bool = False):

 random.seed(seed)
 np.random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = deterministic
def run_inference_loop(stgs, model, loader, device):
model.to(device)
 model.eval()
pred_list = []
with torch.no_grad() :
for x, t in tqdm(loader):
y_1 = model(x.to(device))
y_2 = model(x.to(device ).flip(-1))
y_preds =(
 y_1.sigmoid().detach().cpu().numpy() +
y_2.sigmoid().detach().cpu().numpy() 
)/ 2
pred_list.append(y_preds)
 pred_arr = np.concatenate(pred_list)
 del pred_list
return pred_arr
shutil.rmtree("images" )<import_modules>
if not torch.cuda.is_available() :
device = torch.device("cpu")
 else:
device = torch.device("cuda")
 print(device )
import tensorflow as tf
import keras
from keras.datasets import mnist
from keras import backend as K
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from keras import layers, callbacks
from keras.layers import AveragePooling2D,Add, concatenate, Convolution2D, Conv2D, Input, MaxPooling2D, UpSampling2D, Activation, ZeroPadding2D,Dropout,Conv2DTranspose
from keras.layers.core import Reshape, Permute, Flatten
from keras.layers.normalization import BatchNormalization
from keras.layers import LeakyReLU, Dense, MaxPool2D
from keras.optimizers import RMSprop,SGD,Adam
from keras.models import Model
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from keras.models import Sequential
from sklearn import metrics
from keras.callbacks import LearningRateScheduler, ModelCheckpoint
from sklearn.utils import shuffle<load_from_csv>
model_dir = TRAINED_MODEL
test_dir = TEST_RESIZED
test_file_list = [ 
(test_dir / f"{img_id}.png", [-1] * 11)
 for img_id in smpl_sub["StudyInstanceUID"].values
]
test_loader = get_dataloaders_for_inference(test_file_list, batch_size=64)
 test_preds_arr = np.zeros(( N_FOLD, len(smpl_sub), N_CLASSES))
for fold_id in FOLDS:
print(f"[fold {fold_id}]")
 stgs = load_setting_file(model_dir / f"fold{fold_id}" / "settings.yml")
 stgs["model"]["params"]["pretrained"] = False
model = MultiHeadResNet200D(**stgs["model"]["params"])
 model_path = model_dir / f"best_model_fold{fold_id}.pth"
model.load_state_dict(torch.load(model_path, map_location=device))
test_pred = run_inference_loop(stgs, model, test_loader, device)
 test_preds_arr[fold_id] = test_pred
del model
torch.cuda.empty_cache()
gc.collect()
<split><EOS> 
sub = smpl_sub.copy()
sub[CLASSES] =(
 0.50 *(test_preds_arr.argsort(axis=1 ).argsort(axis=1 ).mean(axis=0))
+ 0.30 *(predictions200d.argsort(axis=0 ).argsort(axis=0))
+ 0.08 *(predictions152d.argsort(axis=0 ).argsort(axis=0))
+ 0.12 *(predictionsB5.argsort(axis=0 ).argsort(axis=0)) 
)
 sub.to_csv("submission.csv", index=False )