Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<feature_engineering>
%matplotlib inline
print("Tensorflow version " + tf.__version__)
 !pip install -q tfa-nightly
tfa.register_all(custom_kernels=False)
 ! pip install -q efficientnet
submission_df["Label"] = results<save_to_csv>
DEVICE = 'TPU'
if DEVICE == "TPU":
print("connecting to TPU...")
 try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
print("Could not connect to TPU")
 tpu = None
if tpu:
try:
print("initializing TPU...")
 tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print("TPU initialized")
 except _:
print("failed to initialize TPU")
 else: DEVICE = "GPU"
if DEVICE != "TPU":
print("Using default strategy for CPU and single GPU")
 strategy = tf.distribute.get_strategy()
if DEVICE == "GPU":
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
 AUTO = tf.data.experimental.AUTOTUNE
REPLICAS = strategy.num_replicas_in_sync
print('REPLICAS: {}'.format(REPLICAS))
submission_df.to_csv("submission.csv", index = False )<set_options>
DATA_PATH = '.. /input/siim-isic-melanoma-classification/'
IMAGE_PATH= DATA_PATH+'jpeg/train/'
INCLUDE_2019_DATA = True
INCLUDE_2018_DATA = True
INCLUDE_CD_MALIGNANT = True
UPSAMPLE = True
DIM = 256
BATCH_SIZE = 32 * REPLICAS
EPOCHS = 20
VERBOSE = 1
TTA = 10
tf.random.set_seed(250688)
 MAX_ROT_ANGLE = 180.0
MAX_SHEAR_LEVEL = 0.1
HSHIFT, VSHIFT = 5., 5.
 MAX_ROT_ANGLE = np.pi*MAX_ROT_ANGLE/180
AUGMENT_FRAC = 0.7
NUM_CUTOUTS = 10
CUTOUT_SIZE = 15
CUTOUT_FRAC = 0.8
LR = 1e-3
LOSS_TYPE = "FOCAL"
GAMMA = 2.
 ALPHA = 0.8
OPT_TYPE = "RAdam"
GCS_PATH_ORG = KaggleDatasets().get_gcs_path("siim-isic-melanoma-classification")
 GCS_PATH = KaggleDatasets().get_gcs_path("melanoma-{}x{}".format(DIM,DIM))
GCS_PATH_2019 = KaggleDatasets().get_gcs_path("isic2019-{}x{}".format(DIM,DIM))
GCS_PATH_M = KaggleDatasets().get_gcs_path("malignant-v2-{}x{}".format(DIM,DIM))
%matplotlib inline
sns.set(style='white', context='notebook', palette='deep')
 warnings.filterwarnings('ignore')
 train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 print(train.shape)
 Y_train = train['label']
X_train = train.drop(labels = ["label"],axis = 1)
 fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111)
 sns.countplot(Y_train);
ax.set_title(f'Labelled Digits Count({Y_train.size} total)', size=20);
ax.set_xlabel('Digits');
for patch in ax.patches:
ax.annotate('{:}'.format(patch.get_height()),(patch.get_x() +0.1, patch.get_height() -150), color='w' )<count_missing_values>
train_files = tf.io.gfile.glob(GCS_PATH+'/train*.tfrec')
 if INCLUDE_2019_DATA:
train_files+= tf.io.gfile.glob([f"{GCS_PATH_2019}/train{(2*x +1):02d}-*.tfrec" for x in range(14)])
 if INCLUDE_2018_DATA:
train_files+= tf.io.gfile.glob([f"{GCS_PATH_2019}/train{(2*x):02d}-*.tfrec" for x in range(15)])
 if INCLUDE_CD_MALIGNANT:
new_malignant_tfrecords = np.arange(15,30 ).tolist()
train_files+= tf.io.gfile.glob([f"{GCS_PATH_M}/train{x}-*.tfrec" for x in new_malignant_tfrecords])
 test_files = tf.io.gfile.glob(GCS_PATH+'/test*.tfrec')
 np.random.shuffle(train_files)
def check_missing_data(df):
flag=df.isna().sum().any()
if flag==True:
total = df.isnull().sum()
percent =(df.isnull().sum())/(df.isnull().count() *100)
 output = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 data_type = []
for col in df.columns:
dtype = str(df[col].dtype)
 data_type.append(dtype)
 output['Types'] = data_type
return(np.transpose(output))
else:
return(False)
 check_missing_data(train)
 check_missing_data(test )<categorify>
data = pd.read_csv(DATA_PATH+"train.csv")
 pos = data[data.target==1].shape[0]
neg = data[data.target==0].shape[0]
total = data.shape[0]
weight_for_0 =(1 / neg)*(total)/2.0
weight_for_1 =(1 / pos)*(total)/2.0
X_train = X_train / 255.0
test = test / 255.0
mean_px = X_train.mean().astype(np.float32)
 std_px = X_train.std().astype(np.float32)
 def standardize(x):
return(x-mean_px)/std_px
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1)
 for i in range(6, 9):
plt.subplot(330 +(i+1))
plt.imshow(X_train[i,:, :, 0], cmap=plt.get_cmap('gray'))
plt.title(f'Label: {Y_train[i]}');
Y_train = to_categorical(Y_train, num_classes = 10)
 train_x, test_x, train_y, test_y = train_test_split(X_train, Y_train, test_size = 0.1 )<init_hyperparams>
def count_data_items(filenames):

 num_files = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1))
for filename in filenames]
return np.sum(num_files)
 def decode_image(image_string, label=None):

 image = tf.image.decode_jpeg(image_string, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
if DEVICE== 'TPU':
image = tf.reshape(image, [DIM,DIM, 3])
 if label is not None:
return image, label
else:
return image
def read_labelled_tfrecord(example, return_meta_data = False):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'image_name' : tf.io.FixedLenFeature([], tf.string),
'patient_id' : tf.io.FixedLenFeature([], tf.int64),
'sex' : tf.io.FixedLenFeature([], tf.int64),
'age_approx' : tf.io.FixedLenFeature([], tf.int64),
'anatom_site_general_challenge': tf.io.FixedLenFeature([], tf.int64),
'diagnosis' : tf.io.FixedLenFeature([], tf.int64),
'target' : tf.io.FixedLenFeature([], tf.int64)
 }
example = tf.io.parse_single_example(example, tfrec_format)
 if return_meta_data:
return example['image'], example['target'],example['sex'], example['age_approx'],example['anatom_site_general_challenge']
else:
return example['image'], example['target']
def read_unlabelled_tfrecord(example, return_image_names = True):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'image_name' : tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, tfrec_format)
 if return_image_names:
return example['image'], example['image_name']
else:
return example['image']
def crop_resize(x, label=None):
image = tf.image.central_crop(x, central_fraction= 0.7+0.3*tf.random.uniform(shape=[] ).numpy())
 image = tf.image.resize(image, [DIM,DIM])
 return image,label
def do_augmentation(image,label=None):
if(tf.random.uniform([],0,1)<AUGMENT_FRAC):
image = tf.image.random_flip_left_right(image)
 image = tf.image.random_brightness(image, max_delta= 0.05)
 image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
 image = tf.image.random_hue(image, 0.05)
 image = tf.image.random_contrast(image, 0.8, 1.2)
 image = tf.image.resize_with_crop_or_pad(image, tf.random.uniform(shape=[], minval= tf.cast(0.7*DIM, tf.int32), maxval=DIM, dtype=tf.int32), target_width= tf.random.uniform(shape=[], minval=tf.cast(0.7*DIM, tf.int32), maxval=DIM, dtype=tf.int32))
image = tf.image.resize(image, [DIM,DIM])
 shear_x = MAX_SHEAR_LEVEL* tf.random.uniform(shape=[],minval=-1,maxval=1)
 shear_y = MAX_SHEAR_LEVEL * tf.random.uniform(shape=[],minval=-1,maxval=1)
 image = tfa.image.transform(image, [1.0, shear_x, 0.0, shear_y, 1.0, 0.0, 0.0, 0.0])
 image = tfa.image.rotate(image, MAX_ROT_ANGLE * tf.random.uniform([], dtype=tf.float32))
image = tfa.image.translate(image, [HSHIFT * tf.random.uniform(shape=[],minval=-1, maxval=1), VSHIFT * tf.random.uniform(shape=[],minval=-1, maxval=1)])
 if(tf.random.uniform([],0,1)< CUTOUT_FRAC):
image = tfa.image.random_cutout(tf.expand_dims(image, axis=0),mask_size=CUTOUT_SIZE,constant_values= 0)
 for _ in range(NUM_CUTOUTS-1):
image = tfa.image.random_cutout(image,mask_size=CUTOUT_SIZE,constant_values= 0)
 image = tf.clip_by_value(image, 0, 1)
 image = tf.reshape(image, [DIM,DIM, 3])
 if label is not None:
return tf.squeeze(image),label
else:
return tf.squeeze(image)
 def data_loader(filenames, labelled=True, augment=True, repeat=True, shuffle=True,ordered=True,return_image_names=True, batch_size = BATCH_SIZE):

 option_no_order = tf.data.Options()
if not ordered:
option_no_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
 dataset.with_options(option_no_order)
 if labelled:
dataset = dataset.map(read_labelled_tfrecord, num_parallel_calls=AUTO)
 else:
dataset = dataset.map(lambda example: read_unlabelled_tfrecord(example,return_image_names=return_image_names), num_parallel_calls=AUTO)
 dataset = dataset.cache()
dataset = dataset.map(decode_image, num_parallel_calls=AUTO)
 if shuffle:
dataset = dataset.shuffle(2048)
 if repeat:
dataset = dataset.repeat()
if augment:
dataset = dataset.map(do_augmentation, num_parallel_calls=AUTO)
 dataset = dataset.batch(batch_size)
 dataset = dataset.prefetch(buffer_size = AUTO)
 return dataset
validation_split = 0.15
img_gen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False,
validation_split=validation_split)
 batch_size = 86
train_gen = img_gen.flow(x=train_x, y=train_y, subset='training', batch_size=batch_size)
 valid_gen = img_gen.flow(x=train_x, y=train_y, subset='validation', batch_size=batch_size)
 visualizaion_flow = img_gen.flow(train_x, train_y, batch_size=1, shuffle=False)
 fig, axs = plt.subplots(2, 4, figsize=(20,10))
fig.suptitle('Augmentation Results', size=32)
 for axs_col in range(axs.shape[1]):
idx = np.random.randint(0, train_x.shape[0])
 img = train_x[idx,:, :, 0]
aug_img, aug_label = visualizaion_flow[idx]
axs[0, axs_col].imshow(img, cmap='gray');
axs[0, axs_col].set_title(f'example
axs[1, axs_col].imshow(aug_img[0, :, :, 0], cmap='gray');
axs[1, axs_col].set_title(f'Augmented example<choose_model_class>
for img_batch, label in data_loader(train_files, labelled=True, augment=True ).take(1):
print(img_batch.shape, label)
seed = 43
np.random.seed(seed)
 learning_rate=0.00065
optimizer = Adam(lr=learning_rate)
 def conv_block(x, filters, kernel_size, strides, layer_no, use_pool=False, padding='same'):
x = Convolution2D(filters=filters, kernel_size=kernel_size, strides=strides, name=f'conv{layer_no}',
padding=padding )(x)
 x = BatchNormalization(name=f'bn{layer_no}' )(x)
 x = Activation('relu', name=f'activation{layer_no}' )(x)
 if use_pool:
x = MaxPooling2D(pool_size=[2, 2], strides=[2, 2], name=f'pool{layer_no}', padding='same' )(x)
 return x
def build_model(X):
h, w, c = X.shape[1:]
X = Input(shape=(h, w, c))
conv1 = conv_block(X, filters=8, kernel_size=[3, 3], strides=[1, 1], layer_no=1)
 conv2 = conv_block(conv1, filters=16, kernel_size=[2, 2], strides=[1, 1], layer_no=2)
 conv3 = conv_block(conv2, filters=32, kernel_size=[2, 2], strides=[1, 1], layer_no=3, use_pool=True)
 conv4 = conv_block(conv3, filters=64, kernel_size=[3, 3], strides=[2, 2], layer_no=4)
 conv5 = conv_block(conv4, filters=128, kernel_size=[2, 2], strides=[1, 1], layer_no=5)
 conv6 = conv_block(conv5, filters=256, kernel_size=[2, 2], strides=[1, 1], layer_no=6, use_pool=True)
 flat1 = Flatten(name='flatten1' )(conv6)
 drop1 = Dropout(0.35, name='Dopout1' )(flat1)
 dens1 = Dense(128, name='dense1' )(drop1)
 bn7 = BatchNormalization(name='bn7' )(dens1)
 drop2 = Dropout(0.35, name='Dopout2' )(bn7)
 relu1 = Activation('relu', name='activation7' )(drop2)
 dens1 = Dense(256, name='dense01' )(relu1)
 bn7 = BatchNormalization(name='bn07' )(dens1)
 drop2 = Dropout(0.5, name='Dopout02' )(bn7)
 relu1 = Activation('relu', name='activation07' )(drop2)
 dens2 = Dense(10, name='dense2' )(relu1)
 bn8 = BatchNormalization(name='bn8' )(dens2)
 output_layer = Activation('softmax', name='softmax' )(bn8)
 model = Model(inputs=X, outputs=output_layer)
 model.compile(optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
 return model
def build_seq_model(X):
h, w, c = X.shape[1:]
model = Sequential([
Convolution2D(32,(3,3), activation='relu', input_shape =(h, w, c)) ,
BatchNormalization(axis=1),
Convolution2D(32,(3,3), activation='relu'),
MaxPooling2D() ,
BatchNormalization(axis=1),
Convolution2D(64,(3,3), activation='relu'),
BatchNormalization(axis=1),
Convolution2D(64,(3,3), activation='relu'),
MaxPooling2D() ,
Dropout(0.25),
Flatten() ,
Dense(512, activation='relu'),
Dropout(0.25),
Dense(1024, activation='relu'),
Dropout(0.5),
Dense(10, activation='softmax')
 ])
 model.compile(optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
 return model
model = build_model(train_x)
 learning_rate_annealer = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 epochs = 10
hist = model.fit_generator(generator=train_gen, steps_per_epoch=train_x.shape[0]*(1-validation_split)//batch_size, epochs=epochs,
validation_data=valid_gen, validation_steps=train_x.shape[0]*validation_split//batch_size, callbacks=[learning_rate_annealer])
 fig, ax = plt.subplots(2,1)
 ax[0].plot(hist.history['loss'], color='b', label="Training loss")
 ax[0].plot(hist.history['val_loss'], color='r', label="validation loss",axes =ax[0])
 legend = ax[0].legend(loc='best', shadow=True)
 ax[1].plot(hist.history['acc'], color='b', label="Training accuracy")
 ax[1].plot(hist.history['val_acc'], color='r',label="Validation accuracy")
 legend = ax[1].legend(loc='best', shadow=True )<compute_test_metric>
for img, img_name in data_loader(train_files, labelled=False, augment=True, return_image_names=True ).unbatch().take(2):
print(img.shape, img_name.numpy().decode("utf-8"))
def calculate_performance(labels, pred, dataset):
pred_cat = np.argmax(pred, axis=1)
 labels_cat = np.argmax(labels, axis=1)
 corrects =(pred_cat == labels_cat)
 falses = dataset[~corrects]
falses_labels = labels_cat[~corrects]
falses_preds = pred[~corrects]
examples_num = labels.shape[0]
accuracy = np.count_nonzero(corrects)/ examples_num
return accuracy, [falses, falses_labels, falses_preds], [labels_cat, pred_cat]
test_y_pred = model.predict(test_x)
 test_accuracy,(test_falses, test_falses_labels, test_falses_preds),(true_labels, pred_labels)= calculate_performance(test_y, test_y_pred, test_x)
 test_f1 = f1_score(y_pred=pred_labels, y_true=true_labels, average='micro')
 print(f'Test Dataset Accuracy: {np.round(test_accuracy*100, 3)}%')
 print(f'F1 Score = {test_f1}')
 plt.figure(figsize=(10, 10))
test_matrix = confusion_matrix(y_pred=pred_labels, y_true=true_labels)
 sns.heatmap(data=test_matrix, annot=True, cmap='Blues', fmt=f'.0f')
 plt.title('Confusion Matrix - Test Dataset', size=24)
 plt.xlabel('Predictions', size=20);
plt.ylabel('Labels', size=20);
final_loss, final_acc = model.evaluate(test_x, test_y, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss, final_acc))<choose_model_class>
for img_batch in data_loader(train_files, labelled=False, augment=True, return_image_names=False ).take(1):
print(img_batch.shape )
styles=[':','-.','--','-',':','-.','--','-',':','-.','--','-']
nets = 3
model = [0] *nets
for j in range(3):
model[j] = Sequential()
model[j].add(Conv2D(j*16+16,kernel_size=5,activation='relu',input_shape=(28,28,1)))
 model[j].add(MaxPool2D())
 model[j].add(Conv2D(j*32+32,kernel_size=5,activation='relu'))
model[j].add(MaxPool2D())
 model[j].add(Flatten())
 model[j].add(Dense(256, activation='relu'))
model[j].add(Dense(10, activation='softmax'))
model[j].compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
 X_train2, X_val2, Y_train2, Y_val2 = train_test_split(X_train, Y_train, test_size = 0.333)
 history = [0] * nets
names = ["16 maps","32 maps","64 maps"]
epochs = 10
for j in range(nets):
history[j] = model[j].fit(X_train2,Y_train2, batch_size=80, epochs = epochs,
validation_data =(X_val2,Y_val2), verbose=0)
 print("CNN {0}: Epochs={1:d}, Train accuracy={2:.5f}, Validation accuracy={3:.5f}".format(
 names[j],epochs,max(history[j].history['acc']),max(history[j].history['val_acc'])))
 plt.figure(figsize=(15,5))
for i in range(nets):
plt.plot(history[i].history['val_acc'],linestyle=styles[i])
 plt.title('model accuracy')
 plt.ylabel('accuracy')
 plt.xlabel('epoch')
 plt.legend(names, loc='upper left')
 axes = plt.gca()
axes.set_ylim([0.98,1])
 plt.show()<choose_model_class>
c=1
for img,label in data_loader(train_files[:2], labelled=True, shuffle=False, augment=False, repeat=False, ordered=True ).unbatch() :
if c==1:
print(img[2,3].numpy() ,label.numpy())
 c+=1
model = Sequential()
model.add(Conv2D(32,kernel_size=3,activation='relu',input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32,kernel_size=3,activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32,kernel_size=5,strides=2,padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64,kernel_size=3,activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,kernel_size=3,activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,kernel_size=5,strides=2,padding='same',activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
 annealer = LearningRateScheduler(lambda x: 1e-3 * 0.95 **(x+epochs))
hist = model.fit_generator(img_gen.flow(X_train,Y_train, batch_size=64), epochs = 100,
steps_per_epoch = X_train.shape[0]//64, callbacks=[annealer], verbose=0)
 print("Train accuracy={0:.5f}, Train loss={1:.5f}".format(max(hist.history['acc']),max(hist.history['loss'])) )<save_to_csv>
def get_lr_callback(PLOT_LR = False):
lr_start = 5e-6
lr_max = 1.4e-6 * BATCH_SIZE
lr_min = 1e-6
lr_ramp_ep = 5
lr_sus_ep = 2
lr_decay = 0.8
def lrfn(epoch):
if epoch < lr_ramp_ep:
lr =(lr_max - lr_start)/ lr_ramp_ep * epoch + lr_start
elif epoch < lr_ramp_ep + lr_sus_ep:
lr = lr_max
else:
lr =(lr_max - lr_min)* lr_decay**(epoch - lr_ramp_ep - lr_sus_ep)+ lr_min
return lr
if PLOT_LR:
plt.figure(figsize=(15, 5))
plt.subplot(1, 2, 1)
 plt.plot([lrfn(e)for e in range(EPOCHS)]);
plt.xlabel('Epoch'); plt.ylabel('LR');
plt.subplot(1, 2, 2);
plt.plot([lrfn(e)for e in range(EPOCHS)]);
plt.xlabel('Epoch'); plt.ylabel('Log LR');
plt.yscale('log');
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=False)
 return lr_callback
def build_model(input_shape =(192,192,3), pretrained_model= efn.EfficientNetB2, loss_type=LOSS_TYPE, opt = OPT_TYPE):
inp = tf.keras.layers.Input(shape=input_shape)
 base_model = pretrained_model(include_top=False, weights='imagenet', input_shape=input_shape)
 print("Using {} as the base model".format(base_model.name))
if base_model.name == "resnet50":
for layer in base_model.layers[:143]:
layer.trainable = False
for layer in base_model.layers[143:]:
layer.trainable = True
x = base_model(inp)
 x = tf.keras.layers.GlobalAveragePooling2D()(x)
 x = tf.keras.layers.Dense(256, activation = 'relu' )(x)
 x = tf.keras.layers.Dropout(0.3 )(x)
 x = tf.keras.layers.Dense(128, activation = 'relu' )(x)
 x = tf.keras.layers.Dense(1, activation = 'sigmoid' )(x)
 model = tf.keras.Model(inputs= inp, outputs = x)
 if OPT_TYPE == "Adam":
optimizer = tf.keras.optimizers.Adam(learning_rate= LR)
 elif OPT_TYPE == "RAdam":
optimizer = tfa.optimizers.RectifiedAdam(lr=LR)
 if loss_type == 'BCE':
loss = tf.keras.losses.BinaryCrossentropy(label_smoothing=0.05)
 elif loss_type == 'FOCAL':
loss= tfa.losses.SigmoidFocalCrossEntropy(gamma = GAMMA, alpha = ALPHA)
 model.compile(optimizer= optimizer,loss= loss, metrics= ['AUC'])
 return model
predictions = model.predict(test)
 predictions = np.argmax(predictions,axis = 1)
 submissions=pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,
"Label": predictions})
 submissions.to_csv("submission.csv", index=False, header=True)
 print(submissions.head(5))<set_options>
_ = get_lr_callback(PLOT_LR=True )
%matplotlib inline 
<load_from_csv>
NUM_TEST_IMAGES = count_data_items(test_files)
 def train_model(kfold_n_splits=5,train_set_aug=True, valid_set_aug=True,test_set_aug=True, pretrained_model= MobileNetV2, display_training_curve = True):
preds = np.zeros(shape=(NUM_TEST_IMAGES,))
oof_labels = []
oof_preds = []
kf = KFold(n_splits=kfold_n_splits,shuffle=True,random_state= 1357)
 if DEVICE=='TPU':
print('using TPU')
 for fold,(idxT,idxV)in enumerate(kf.split(np.arange(15))):
print('
print('
print('
K.clear_session()
with strategy.scope() :
model = build_model(input_shape =(DIM,DIM,3), pretrained_model = pretrained_model)
 train_tfrecords = tf.io.gfile.glob(["{}/train{:02d}-*.tfrec".format(GCS_PATH,i)for i in idxT])
 if INCLUDE_2018_DATA:
train_tfrecords += tf.io.gfile.glob([f"{GCS_PATH_2019}/train{(2*x):02d}-*.tfrec" for x in range(15)])
 if INCLUDE_2019_DATA:
train_tfrecords += tf.io.gfile.glob([f"{GCS_PATH_2019}/train{(2*x +1):02d}-*.tfrec" for x in range(14)])
 if INCLUDE_CD_MALIGNANT:
new_malignant_tfrecords = np.arange(15,30 ).tolist()
train_tfrecords += tf.io.gfile.glob([f"{GCS_PATH_M}/train{x}-*.tfrec" for x in new_malignant_tfrecords])
 if UPSAMPLE:
train_tfrecords += tf.io.gfile.glob([f"{GCS_PATH_M}/train{x:02d}-*.tfrec" for x in idxT])
 np.random.shuffle(train_tfrecords)
 val_tfrecords = tf.io.gfile.glob(["{}/train{:02d}-*.tfrec".format(GCS_PATH,i)for i in idxV])
 train_dataset = data_loader(train_tfrecords, labelled=True, augment=train_set_aug, repeat=True, shuffle=True, ordered=False)
 valid_dataset = data_loader(val_tfrecords, labelled = True, augment=False, repeat=False, shuffle=False, ordered=False)
 NUM_TRAINING_IMAGES = count_data_items(train_tfrecords)
 NUM_VALIDATION_IMAGES = count_data_items(val_tfrecords)
 model_best_epoch = tf.keras.callbacks.ModelCheckpoint(
 "fold{}.h5".format(fold+1), monitor='val_auc', verbose=1, save_best_only=True,
save_weights_only=True, mode='max', save_freq='epoch')
 history = model.fit(train_dataset,epochs = EPOCHS,
steps_per_epoch= np.ceil(NUM_TRAINING_IMAGES/BATCH_SIZE),
verbose= 1, callbacks=[model_best_epoch, get_lr_callback() ],validation_data = valid_dataset)
 print("
model.load_weights("fold{}.h5".format(fold+1))
if valid_set_aug:
print(f"
valid_dataset_tta = data_loader(val_tfrecords, labelled=True, augment=True, shuffle=False, repeat=True)
 valid_images = valid_dataset_tta.map(lambda image, label: image)
 ypred_valid = model.predict(valid_images, steps = np.ceil(TTA*NUM_VALIDATION_IMAGES/BATCH_SIZE), verbose=1)
 ypred_valid = ypred_valid[:NUM_VALIDATION_IMAGES * TTA].reshape(( NUM_VALIDATION_IMAGES,TTA), order = 'F')
 ypred_valid = ypred_valid.mean(axis = 1)
 else:
print('
valid_dataset_no_tta = data_loader(val_tfrecords, labelled=True, augment=False, shuffle=False,repeat=False)
 valid_images = valid_dataset_no_tta.map(lambda image, label: image)
 ypred_valid = model.predict(valid_images,steps = NUM_VALIDATION_IMAGES/BATCH_SIZE, verbose =1)
 valid_labels = np.array([label.numpy() for label in data_loader(val_tfrecords, labelled=True, augment=False, shuffle=False,repeat=False ).map(lambda image, label: label ).unbatch() ])
 auc_valid = roc_auc_score(valid_labels, ypred_valid)
 print(f'AUC of validation fold {fold+1}(with TTA = {TTA})= {auc_valid}')
 oof_labels.append(valid_labels)
 oof_preds.append(ypred_valid)
 if test_set_aug:
print(f"
test_data = data_loader(test_files, labelled=False, augment=True, shuffle=False, repeat=True, return_image_names=False, batch_size = BATCH_SIZE*8)
 ypred_test = model.predict(test_data, steps = TTA*NUM_TEST_IMAGES/(BATCH_SIZE*8), verbose=1)
 ypred_test = ypred_test[:NUM_TEST_IMAGES * TTA].reshape(( NUM_TEST_IMAGES,TTA), order = 'F')
 ypred_test = ypred_test.mean(axis = 1)
 else:
print("
test_data = data_loader(test_files, labelled=False, augment=False, shuffle=False, repeat=False, return_image_names=False)
 ypred_test = model.predict(test_data, verbose=1)
 preds +=(ypred_test/kfold_n_splits)
 if display_training_curve:
print("Fold: {}".format(fold+1))
display_training_curves(history.history['loss'], history.history['val_loss'], 'loss', 211)
 display_training_curves(history.history['auc'], history.history['val_auc'], 'AUC', 212)
 return preds, oof_labels,oof_preds
training_data = pd.read_csv('.. /input/train.csv')
 testing_data = pd.read_csv('.. /input/test.csv' )<count_values>
pretrained_model = efn.EfficientNetB2
preds, oof_labels,oof_preds = train_model(train_set_aug=True,pretrained_model = pretrained_model, display_training_curve=True )
label = training_data.label.value_counts()
label<data_type_conversions>
roc_auc_score(np.concatenate(oof_labels),np.concatenate(oof_preds))
<categorify><EOS> 
test_img_names = []
test_dataset = data_loader(test_files, labelled=False, augment=False, shuffle=False, repeat=False, return_image_names=True, ordered=True)
 for _,image_name in test_dataset.unbatch() :
test_img_names.append(image_name.numpy().decode("utf-8"))
sample_sub = pd.read_csv(DATA_PATH+'/sample_submission.csv')
 sample_sub.loc[:,'image_name' ] = test_img_names
sample_sub.loc[:,'target' ] = preds
sample_sub.to_csv("submission.csv",index=False )