Kaggle competition: RANZCR CLiP - Catheter and Line Position Challenge
<SOS> metric: MCAUC Kaggle data source: ranzcr-clip-catheter-and-line-position-challenge<train_model>
!pip install /kaggle/input/kerasapplications -q
!pip install /kaggle/input/efficientnet-keras-source-code/ -q --no-deps
def train_mnist() :
Reached 99.9% accuracy so cancelling training!")
 callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)
 X_train, X_val, y_train, y_val = train_test_split(X,y,random_state=0,test_size=0.2,shuffle=True)
 X_val, X_test, y_val, y_test = train_test_split(X_val,y_val,random_state=0,test_size=0.5,shuffle=True)
 X_train, X_val =(X_train / 255.0 ).reshape(len(X_train),28,28,1),(X_val / 255.0 ).reshape(len(X_val),28,28,1)
 datagen.fit(X_train)
 model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64,(3,3), input_shape=(28, 28, 1)) ,
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(64,(3,3)) ,
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(1024),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Dropout(rate=0.5),
tf.keras.layers.Dense(1024),
tf.keras.layers.LeakyReLU(alpha=0.1),
tf.keras.layers.Dropout(rate=0.5),
tf.keras.layers.Dense(10, activation='softmax')
 ])
 optimizer = tf.keras.optimizers.SGD(lr=0.01, momentum=0.9,decay=1e-6)
 model.compile(optimizer=optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
 batch_size = 32
history = model.fit_generator(datagen.flow(X_train,y_train, batch_size=batch_size),
validation_data =(X_val,y_val),
epochs=60,
steps_per_epoch=X_train.shape[0] // batch_size,
callbacks=[callback])
 return history, model, X_test, y_test<train_model>
import os
import efficientnet.tfkeras as efn
import numpy as np
import pandas as pd
import tensorflow as tf
<prepare_x_and_y>
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=(512, 512), 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
history, model, X_test, y_test = train_mnist()<prepare_x_and_y>
COMPETITION_NAME = "ranzcr-clip-catheter-line-classification"
strategy = auto_select_accelerator()
BATCH_SIZE = strategy.num_replicas_in_sync * 16
X_test =(X_test / 255.0 ).reshape(len(X_test),28,28,1)
 test =(test / 255.0 ).reshape(len(test),28,28,1 )<compute_test_metric>
IMSIZE =(224, 240, 260, 300, 380, 456, 528, 600, 512)
 load_dir = f"/kaggle/input/{COMPETITION_NAME}/"
sub_df = pd.read_csv(load_dir + 'sample_submission.csv')
 test_paths = load_dir + "test/" + sub_df['StudyInstanceUID'] + '.jpg'
label_cols = sub_df.columns[1:]
test_decoder = build_decoder(with_labels=False, target_size=(IMSIZE[-2], IMSIZE[-2]))
dtest = build_dataset(
 test_paths, bsize=BATCH_SIZE, repeat=False,
shuffle=False, augment=False, cache=False,
decode_fn=test_decoder
)
loss, acc = model.evaluate(X_test,y_test)
 print(f"Accuracy: {acc}")
 print(f"Loss: {loss}" )<predict_on_test>
def create_model(w, input_shape=[IMSIZE[-2], IMSIZE[-2],3], classes=11):
base_model = efn.EfficientNetB7(include_top=False,
weights=None,
input_shape=input_shape)
 inputs = tf.keras.Input(shape=input_shape)
 x = base_model(inputs)
 x = tf.keras.layers.GlobalAveragePooling2D()(x)
 x = tf.keras.layers.Dense(classes )(x)
 outputs = tf.keras.layers.Activation('sigmoid', dtype='float32' )(x)
 model = tf.keras.Model(inputs, outputs)
 model.load_weights(w)
 return model
<create_dataframe><EOS> 
sub_df[label_cols] = model.predict(dtest, verbose=1)
 sub_df.to_csv('submission.csv', index=False)
 sub_df.head()