Kaggle competition: RANZCR CLiP - Catheter and Line Position Challenge
<SOS> metric: MCAUC Kaggle data source: ranzcr-clip-catheter-and-line-position-challenge<save_to_csv>
!pip install /kaggle/input/kerasapplications -q
!pip install /kaggle/input/efficientnet-keras-source-code/ -q --no-deps
test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])
 test_df.to_csv('submission.csv', index=False)
 test_df.head()<import_modules>
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
warnings.simplefilter('ignore' )
import logging
import os
import re
import gc
import json
from tqdm.auto import tqdm
import numpy as np
import pandas as pd
import cv2
import matplotlib.pyplot as plt<install_modules>
MIXED_PRECISION = True
XLA_ACCELERATE = False
GPUS = tf.config.experimental.list_physical_devices('GPU')
 if GPUS:
try:
for GPU in GPUS:
tf.config.experimental.set_memory_growth(GPU, True)
 logical_gpus = tf.config.experimental.list_logical_devices('GPU')
 print(len(GPUS), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
 except RuntimeError as RE:
print(RE)
 if MIXED_PRECISION:
policy = tf.keras.mixed_precision.experimental.Policy('mixed_float16')
 tf.keras.mixed_precision.experimental.set_policy(policy)
 print('Mixed precision enabled')
 if XLA_ACCELERATE:
tf.config.optimizer.set_jit(True)
 print('Accelerated Linear Algebra enabled')
 strategy = tf.distribute.get_strategy()
REPLICAS = strategy.num_replicas_in_sync
print(f'REPLICAS: {REPLICAS}')
 print("Tensorflow version " + tf.__version__ )
!pip install.. /input/pytorch-16/torch-1.6.0cu101-cp37-cp37m-linux_x86_64.whl<install_modules>
class BaseConfig(object):
SEED = 101
TRAIN_DF = '.. /input/ranzcr-clip-catheter-line-classification/train.csv'
TRAIN_IMG_PATH = '.. /input/ranzcr224/RANZCR_224/'
TEST_IMG_PATH = '.. /input/ranzcr-clip-catheter-line-classification/test/'
CLASS_MAP = '.. /input/ranzcr-clip-catheter-line-classification/train_annotations.csv'
SUBMIT = '.. /input/ranzcr-clip-catheter-line-classification/sample_submission.csv'
!pip install.. /input/pytorch-16/torchvision-0.7.0cu101-cp37-cp37m-linux_x86_64.whl<install_modules>
df = pd.read_csv(BaseConfig.TRAIN_DF)
 submit = pd.read_csv(BaseConfig.SUBMIT)
 target_cols = submit.columns[1:]
df.head()
!pip install.. /input/pretrainedmodels/pretrainedmodels-0.7.4/pretrainedmodels-0.7.4/ > /dev/null<install_modules>
target_cols = ['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']
!pip install.. /input/wheat-pkgs/EfficientNet-PyTorch-master/EfficientNet-PyTorch-master/ > /dev/null<install_modules>
def build_decoder(with_labels=True, target_size=(300, 300), ext='jpg'):
def decode(path):
file_bytes = tf.io.read_file(path)
 if ext == 'png':
img = tf.image.decode_png(file_bytes, channels=3)
 elif ext in ['jpg', 'jpeg']:
img = tf.image.decode_jpeg(file_bytes, channels=3)
 else:
raise ValueError("Image extension not supported")
 img = tf.cast(img, tf.float32)/ 255.0
img = tf.image.resize(img, target_size)
 return img
def decode_with_labels(path, label):
return decode(path), label
return decode_with_labels if with_labels else decode
def build_augmenter(with_labels=True):
def augment(img):
img = tf.image.random_flip_left_right(img)
 img = tf.image.random_flip_up_down(img)
 return img
def augment_with_labels(img, label):
return augment(img), label
return augment_with_labels if with_labels else augment
def build_dataset(paths, labels=None, bsize=32, cache=True,
decode_fn=None, augment_fn=None,
augment=True, repeat=True, shuffle=1024,
cache_dir=""):
if cache_dir != "" and cache is True:
os.makedirs(cache_dir, exist_ok=True)
 if decode_fn is None:
decode_fn = build_decoder(labels is not None)
 if augment_fn is None:
augment_fn = build_augmenter(labels is not None)
 AUTO = tf.data.experimental.AUTOTUNE
slices = paths if labels is None else(paths, labels)
 dset = tf.data.Dataset.from_tensor_slices(slices)
 dset = dset.map(decode_fn, num_parallel_calls=AUTO)
 dset = dset.cache(cache_dir)if cache else dset
dset = dset.map(augment_fn, num_parallel_calls=AUTO)if augment else dset
dset = dset.repeat() if repeat else dset
dset = dset.shuffle(shuffle)if shuffle else dset
dset = dset.batch(bsize ).prefetch(AUTO)
 return dset
!pip install.. /input/wheat-pkgs/timm-0.1.20-py3-none-any.whl > /dev/null<install_modules>
class TrainConfig(BaseConfig):
EPOCH = 3
FOLDS = 5
TTA = 5
VERBOSITY = 0
WORKERS = 2
LABEL_SMOOTH = 0.0
MULTIPROCESS = False
LR_RATE = {
'0' : 1e-3,
'1' : 1e-3,
'2' : 1e-4,
'3' : 1e-4,
'4' : 1e-4
}
BATCH_SIZE = {
'0' : 32,
'1' : 128,
'2' : 86,
'3' : 128,
'4' : 128
}
IMG_SIZE = {
'0': 850,
'1': 240,
'2': 260,
'3': 224,
'4': 240
}
BASE_NETS = {
'0' : [efn.EfficientNetB0,
'noisy-student'],
'1' : [efn.EfficientNetB1,
'noisy-student'],
'2' : [efn.EfficientNetB2,
'imagenet'],
'3' : [efn.EfficientNetB0,
'imagenet'],
'4' : [efn.EfficientNetB1,
'imagenet'],
}
!pip install.. /input/wheat-pkgs/segmentation_models.pytorch-master/segmentation_models.pytorch-master > /dev/null<import_modules>
class SpatialAttentionModule(tf.keras.layers.Layer):
def __init__(self, kernel_size=3):

 super(SpatialAttentionModule, self ).__init__()
self.conv1 = tf.keras.layers.Conv2D(64, kernel_size=kernel_size,
use_bias=False,
kernel_initializer='he_normal',
strides=1, padding='same',
activation=tf.nn.relu6)
 self.conv2 = tf.keras.layers.Conv2D(32, kernel_size=kernel_size,
use_bias=False,
kernel_initializer='he_normal',
strides=1, padding='same',
activation=tf.nn.relu6)
 self.conv3 = tf.keras.layers.Conv2D(16, kernel_size=kernel_size,
use_bias=False,
kernel_initializer='he_normal',
strides=1, padding='same',
activation=tf.nn.relu6)
 self.conv4 = tf.keras.layers.Conv2D(1, kernel_size=kernel_size,
use_bias=False,
kernel_initializer='he_normal',
strides=1, padding='same',
activation=tf.math.sigmoid)
 def call(self, inputs):
avg_out = tf.reduce_mean(inputs, axis=3)
 max_out = tf.reduce_max(inputs, axis=3)
 x = tf.stack([avg_out, max_out], axis=3)
 x = self.conv1(x)
 x = self.conv2(x)
 x = self.conv3(x)
 return self.conv4(x)
 class ChannelAttentionModule(tf.keras.layers.Layer):
def __init__(self, ratio=8):

 super(ChannelAttentionModule, self ).__init__()
self.ratio = ratio
self.gapavg = tf.keras.layers.GlobalAveragePooling2D()
self.gmpmax = tf.keras.layers.GlobalMaxPooling2D()
def build(self, input_shape):
self.conv1 = tf.keras.layers.Conv2D(input_shape[-1]//self.ratio,
kernel_size=1,
strides=1, padding='same',
use_bias=True, activation=tf.nn.relu)
 self.conv2 = tf.keras.layers.Conv2D(input_shape[-1],
kernel_size=1,
strides=1, padding='same',
use_bias=True, activation=tf.nn.relu)
 super(ChannelAttentionModule, self ).build(input_shape)
 def call(self, inputs):
gapavg = self.gapavg(inputs)
 gmpmax = self.gmpmax(inputs)
 gapavg = tf.keras.layers.Reshape(( 1, 1, gapavg.shape[1]))(gapavg)
 gmpmax = tf.keras.layers.Reshape(( 1, 1, gmpmax.shape[1]))(gmpmax)
 gapavg_out = self.conv2(self.conv1(gapavg))
gmpmax_out = self.conv2(self.conv1(gmpmax))
return tf.math.sigmoid(gapavg_out + gmpmax_out)
 def get_output_shape_for(self, input_shape):
return self.compute_output_shape(input_shape)
 def compute_output_shape(self, input_shape):
output_len = input_shape[3]
return(input_shape[0], output_len )
set_seed,
create_logging,
WheatDataset,
FastDataLoader,
collate,
ModleWithLoss,
CtdetLoss,
ModelEMA,
get_cosine_schedule_with_warmup,
train_one_epoch,
get_train_transforms
)<define_variables>
class AttentionWeightedAverage2D(tf.keras.layers.Layer):
def __init__(self, **kwargs):
self.init = tf.keras.initializers.get('uniform')
 super(AttentionWeightedAverage2D, self ).__init__(** kwargs)
 def build(self, input_shape):
self.input_spec = [tf.keras.layers.InputSpec(ndim=4)]
assert len(input_shape)== 4
self.W = self.add_weight(shape=(input_shape[3], 1),
name='{}_W'.format(self.name),
initializer=self.init)
 self._trainable_weights = [self.W]
super(AttentionWeightedAverage2D, self ).build(input_shape)
 def call(self, x):
logits = K.dot(x, self.W)
 x_shape = K.shape(x)
 logits = K.reshape(logits,(x_shape[0], x_shape[1], x_shape[2]))
ai = K.exp(logits - K.max(logits, axis=[1,2], keepdims=True))
att_weights = ai /(K.sum(ai, axis=[1,2], keepdims=True)+ K.epsilon())
 weighted_input = x * K.expand_dims(att_weights)
 result = K.sum(weighted_input, axis=[1,2])
 return result
def get_output_shape_for(self, input_shape):
return self.compute_output_shape(input_shape)
 def compute_output_shape(self, input_shape):
output_len = input_shape[3]
return(input_shape[0], output_len )
bifpn_path_0 = '.. /input/wheat-weights/model_centernet_effnetb5_bifpn_00099.pth'
bifpn_path_1 = '.. /input/wheat-weights/model_centernet_effnetb5_bifpn_fold1_00099.pth'
bifpn_path_3 = '.. /input/wheat-weights/model_centernet_effnetb5_bifpn_fold3_lb_ema_00099.pth'<init_hyperparams>
class RANZCRClassifier(tf.keras.Model):
def __init__(self, dim):
super(RANZCRClassifier, self ).__init__()
self.Base = efn.EfficientNetB5(
 input_shape=(TrainConfig.IMG_SIZE['0'],
TrainConfig.IMG_SIZE['0'], 3),
weights=None,
include_top=False)
 self.GAP1 = tf.keras.layers.GlobalAveragePooling2D()
self.GAP2 = tf.keras.layers.GlobalAveragePooling2D()
self.BAT = tf.keras.layers.BatchNormalization()
self.ADD = tf.keras.layers.Add()
self.AVG = tf.keras.layers.Average()
self.DROP = tf.keras.layers.Dropout(rate=0.5)
 self.CAN = ChannelAttentionModule()
self.SPN1 = SpatialAttentionModule()
self.SPN2 = SpatialAttentionModule()
self.AWG = AttentionWeightedAverage2D()
self.DENS = tf.keras.layers.Dense(512, activation=tf.nn.relu)
 self.OUT = tf.keras.layers.Dense(len(target_cols),
activation='sigmoid',
dtype=tf.float32)
 def call(self, input_tensor, training=False):
x = self.Base(input_tensor)
 canx = self.CAN(x)*x
spnx = self.SPN1(canx)*canx
gapx = self.GAP1(spnx)
 wvgx = self.GAP2(self.SPN2(canx))
gapavg = self.AVG([gapx, wvgx])
 awgavg = self.AWG(x)
 x = self.ADD([gapavg, awgavg])
 x = self.BAT(x)
 x = self.DENS(x)
 x = self.DROP(x, training=training)
 return self.OUT(x)
 def build_graph(self):
x = tf.keras.layers.Input(shape=(TrainConfig.IMG_SIZE['0'],
TrainConfig.IMG_SIZE['0'],3))
return tf.keras.Model(inputs=[x], outputs=self.call(x))
model = RANZCRClassifier(( TrainConfig.IMG_SIZE['0'],
TrainConfig.IMG_SIZE['0'], 3))
tf.keras.utils.plot_model(
 model.build_graph() , show_shapes=True,
show_layer_names=True, expand_nested=False
)
class Config:
arch = 'timm-efficientnet-b5'
heads = {'hm': 1,
'wh': 2,
'reg': 2}
head_conv = 64
reg_offset = True
cat_spec_wh = False
img_size = 1024
in_scale = 1024 / img_size
down_ratio = 4
mean = [0.315290, 0.317253, 0.214556],
std = [0.245211, 0.238036, 0.193879]
num_classes = 1
pad = 63
batch_size = 8
K = 128
max_per_image = 128
fix_res = False
test_scales = [1]
flip_test = False
nms = False
gpus = [0]
amp = True
opt = Config()
device = torch.device('cuda')if opt.gpus[0] >= 0 else torch.device('cpu' )<feature_engineering>
model = RANZCRClassifier(( TrainConfig.IMG_SIZE['0'],TrainConfig.IMG_SIZE['0'], 3))
model.build(( None, *(TrainConfig.IMG_SIZE['0'],TrainConfig.IMG_SIZE['0'], 3)))
 model.load_weights('.. /input/multiattentioncheckwg/model.h5' )
def change_key(d):
for _ in range(len(d)) :
k, v = d.popitem(False)
 d['.'.join(k.split('.')[1:])] = v<load_from_csv>
test_paths = BaseConfig.TEST_IMG_PATH + submit['StudyInstanceUID'] + '.jpg'
test_decoder = build_decoder(with_labels=False,
target_size=(TrainConfig.IMG_SIZE['0'],
TrainConfig.IMG_SIZE['0']))
dtest = build_dataset(
 test_paths, bsize=TrainConfig.BATCH_SIZE['0'],
repeat=False, shuffle=False, augment=False,
cache=False, decode_fn=test_decoder
)
DIR_INPUT = '.. /input/global-wheat-detection'
DIR_TRAIN = f'{DIR_INPUT}/train'
DIR_TEST = f'{DIR_INPUT}/test'
train_df = pd.read_csv(f'{DIR_INPUT}/train.csv')
 train_df.shape<data_type_conversions>
submit[target_cols] = model.predict(dtest, verbose=1)
 submit.to_csv('submission.csv', index=False)
 submit.head()