Kaggle competition: RANZCR CLiP - Catheter and Line Position Challenge
<SOS> metric: MCAUC Kaggle data source: ranzcr-clip-catheter-and-line-position-challenge<set_options>
warnings.filterwarnings("ignore")
optimizers = {
'sgd': opt.SGD() ,
'sgd+momentum': opt.SGD(nesterov=True),
'rmsprop': opt.RMSprop() ,
'adam': opt.Adam() ,
} 
<choose_model_class>
batch_size = 1024
epochs = 5
datagen_train = ImageDataGenerator(
 rotation_range = 10,
width_shift_range = 0.25,
height_shift_range = 0.25,
shear_range = 0.1,
zoom_range = 0.4,
horizontal_flip = False 
)
 datagen_val = ImageDataGenerator()
learning_rate_reduction = ReduceLROnPlateau(
 monitor='loss',
factor=0.25,
patience=2,
verbose=1,
mode="auto",
min_delta=0.0001,
cooldown=0,
min_lr=0.00001 
)
 es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=300, restore_best_weights=True)
 history = {}
for name, optimizer in optimizers.items() :
model = get_model()
model.compile(
 loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'] 
)
 h = model.fit_generator(
 datagen_train.flow(X_train, y_train, batch_size=batch_size),
steps_per_epoch=len(X_train)//batch_size,
epochs=epochs,
validation_data=datagen_val.flow(X_valid, y_valid),
validation_steps=50,
callbacks=[learning_rate_reduction, es],
verbose=1 
)
 history[name] = h.history 
<set_options>
%matplotlib inline<load_from_csv>
train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv' )<prepare_x_and_y>
X = train.drop(['label'],axis=1)
 y = train['label']
display(X.head() ,y.head() )<filter>
<init_hyperparams><EOS> 
img_size = 600
def auto_select_accelerator() :
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print("Running on TPU:", tpu.master())
 except ValueError:
strategy = tf.distribute.get_strategy()
print(f"Running on {strategy.num_replicas_in_sync} replicas")
 return strategy
def build_decoder(with_labels=True, target_size=(img_size, img_size), 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)
 img = tf.image.random_saturation(img, 0.8, 1.2)
 img = tf.image.random_brightness(img, 0.1)
 img = tf.image.random_contrast(img, 0.9, 1.2)
 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
COMPETITION_NAME = "ranzcr-clip-catheter-line-classification"
strategy = auto_select_accelerator()
batch_size = strategy.num_replicas_in_sync * 16
print('batch size', batch_size)
 with strategy.scope() :
model = tf.keras.models.load_model('.. /input/ranzcr-tpu-weights/model.h5', custom_objects={'FixedDropout': tf.keras.layers.Dropout})
 test_decoder = build_decoder(with_labels=False, target_size=(img_size, img_size))
load_dir = '/kaggle/input/ranzcr-clip-catheter-line-classification/'
df_sub = pd.read_csv(load_dir + 'sample_submission.csv')
 test_paths = load_dir + "test/" + df_sub['StudyInstanceUID'] + '.jpg'
dtest = build_dataset(
 test_paths, bsize=batch_size, repeat=False,
shuffle=False, augment=False, cache=False,
decode_fn=test_decoder 
)
 y_preds = model.predict(dtest, verbose=1)
 df_sub.iloc[:, 1:] = y_preds
display(df_sub)
 df_sub.to_csv('submission.csv',index=False )