Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<predict_on_test>
!pip install -q efficientnet
!pip install -q pyyaml h5py
predict_val = model.predict(X_val)
 y_val_pred=(np.argmax(predict_val,axis=1))
y_true = np.argmax(Y_val,axis = 1 )<compute_test_metric>
train_df = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/train.csv')
 test_df = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/test.csv')
 sub = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/sample_submission.csv' )
results = confusion_matrix(y_true,y_val_pred)
 print('Confusion Matrix :')
 print(results)
 print('Accuracy Score :',accuracy_score(y_true,y_val_pred))
print('Report : ')
 print(classification_report(y_true,y_val_pred))<compute_train_metric>
AUTO = tf.data.experimental.AUTOTUNE
GCS_PATH_ORIGINAL = KaggleDatasets().get_gcs_path('siim-isic-melanoma-classification')
 GCS_PATH_MERGED = KaggleDatasets().get_gcs_path('512x512-melanoma-tfrecords-70k-images')
 EPOCHS = 20
BATCH_SIZE = 8 * strategy.num_replicas_in_sync
IMAGE_SIZE = [512, 512]
TRAINING = True
TRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH_MERGED + '/train*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH_MERGED + '/test*.tfrec')
 CLASSES = [0,1]
print('Training filenames
', list(TRAINING_FILENAMES))
print('Test file names
', list(TEST_FILENAMES))
val_lr_probs = model.predict_proba(X_val)
 val_lr_auc =(roc_auc_score(y_true, val_lr_probs, multi_class="ovr",average="macro")) *100
print("AUC :%.2f%%"%(val_lr_auc))<predict_on_test>
VALIDATION_FILENAMES = TRAINING_FILENAMES[0:5]
TRAINING_FILENAMES = TRAINING_FILENAMES[5:]
predictions = model.predict(test)
 y_pred= np.argmax(predictions,axis=1 )<save_to_csv>
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"target": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['target'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"image_name": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['image_name']
return image, idnum
def load_dataset(filenames, labeled=True, ordered=False):
ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset
def get_training_dataset() :
dataset = load_dataset(TRAINING_FILENAMES, labeled=True)
 dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.repeat()
dataset = dataset.shuffle(2048)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_validation_dataset(ordered=False):
dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 def visualize_training(history, lw = 3):
plt.figure(figsize=(10,6))
plt.plot(history.history['accuracy'], label = 'training', marker = '*', linewidth = lw)
 plt.plot(history.history['val_accuracy'], label = 'validation', marker = 'o', linewidth = lw)
 plt.title('Training Accuracy vs Validation Accuracy')
 plt.xlabel('Epochs')
 plt.ylabel('Accuracy')
 plt.legend(fontsize = 'x-large')
 plt.show()
plt.figure(figsize=(10,6))
plt.plot(history.history['loss'], label = 'training', marker = '*', linewidth = lw)
 plt.plot(history.history['val_loss'], label = 'validation', marker = 'o', linewidth = lw)
 plt.title('Training Loss vs Validation Loss')
 plt.xlabel('Epochs')
 plt.ylabel('Loss')
 plt.legend(fontsize = 'x-large')
 plt.show()
plt.figure(figsize=(10,6))
plt.plot(history.history['lr'], label = 'lr', marker = '*',linewidth = lw)
 plt.title('Learning Rate')
 plt.xlabel('Epochs')
 plt.ylabel('Learning Rate')
 plt.show()
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("submission.csv",index=False )<set_options>
def build_lrfn(lr_start=0.00001, lr_max=0.000075,
lr_min=0.000001, lr_rampup_epochs=8,
lr_sustain_epochs=0, lr_exp_decay=.8):
lr_max = lr_max * strategy.num_replicas_in_sync
def lrfn(epoch):
if epoch < lr_rampup_epochs:
lr =(lr_max - lr_start)/ lr_rampup_epochs * epoch + lr_start
elif epoch < lr_rampup_epochs + lr_sustain_epochs:
lr = lr_max
else:
lr =(lr_max - lr_min)* lr_exp_decay**(epoch - lr_rampup_epochs - lr_sustain_epochs)+ lr_min
return lr
return lrfn
lrfn = build_lrfn()
lr_schedule = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True )
%matplotlib inline
np.random.seed(2)
 dense_regularizer = L1L2(l2=0.0001)
 sns.set(style='white', context='notebook', palette='deep' )<load_from_csv>
early_stopper = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy',
patience=4,
mode='max',
baseline=None,
restore_best_weights=True)
 model_checkpoint_callback_efnB7 = tf.keras.callbacks.ModelCheckpoint(
 filepath='model_efnB7_all_four_data_best_val_acc.hdf5',
save_weights_only=True,
monitor='val_accuracy',
mode='max',
save_best_only=True)
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<feature_engineering>
test_ds = get_test_dataset(ordered=True)
 print('Generating test file list')
 test_images_ds = test_ds.map(lambda image, idnum: image )
X_train = X_train / 255.0
test = test / 255.0<categorify>
with strategy.scope() :
model_efn_b7 = tf.keras.Sequential([
efn.EfficientNetB7(
 input_shape=(*IMAGE_SIZE, 3),
weights='imagenet',
include_top=False 
),
Layers.GlobalAveragePooling2D() ,
Layers.Dense(1, activation='sigmoid')
 ])
 model_efn_b7.compile(
 optimizer='adam',
loss = 'binary_crossentropy',
metrics=['accuracy'] 
)
 model_efn_b7.summary()
if TRAINING:
history_efn_b7 = model_efn_b7.fit(
 get_training_dataset() ,
epochs=EPOCHS,
callbacks=[lr_schedule, model_checkpoint_callback_efnB7, early_stopper],
steps_per_epoch=NUM_TRAINING_IMAGES // BATCH_SIZE,
validation_data=get_validation_dataset() 
)
 if TRAINING:
pd.DataFrame.from_dict(history_efn_b7.history ).to_csv('history_efn_b7.csv' , index=False)
 visualize_training(history_efn_b7)
 else:
model_efn_b7.load_weights('/kaggle/input/efficent-net-pretrained-24-epoch/model_efnB7_all_four_data_best_val_acc.hdf5')
 probabilities_efn_b7 = model_efn_b7.predict(test_images_ds)
Y_train = to_categorical(Y_train, num_classes = 10 )<define_variables>
test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
random_seed = 2<split>
pred_efn_b7 = pd.DataFrame({'image_name': test_ids, 'target': np.concatenate(probabilities_efn_b7)})
 pred_efn_b7.to_csv('submission_efn_b7_all_four_datasets.csv', index = False)
<choose_model_class><EOS>