Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<create_dataframe>
DEVICE = "TPU"
BASEPATH = ".. /input/siim-isic-melanoma-classification"
out_df = pd.DataFrame(np.c_[np.arange(1, len(test_dataset)+1)[:,None], test_pred.numpy() ], columns=['ImageId', 'Label'] )<save_to_csv>
!pip install -q efficientnet
out_df.to_csv('submission.csv', index=False )<set_options>
random.seed(a=42)
%matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )<load_from_csv>
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')))
 print("REPLICAS: ", strategy.num_replicas_in_sync)
 AUTO = tf.data.experimental.AUTOTUNE
df_train=pd.read_csv('.. /input/digit-recognizer/train.csv')
 test=pd.read_csv('.. /input/digit-recognizer/test.csv' )<train_model>
EPOCHS = 6
BATCH_SIZE = 8 * strategy.num_replicas_in_sync
IMAGE_SIZE = [384, 384]
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )<feature_engineering>
df_train = pd.read_csv(os.path.join(BASEPATH, 'train.csv'))
df_test = pd.read_csv(os.path.join(BASEPATH, 'test.csv'))
sub = pd.read_csv(os.path.join(BASEPATH, 'sample_submission.csv'))
GCS_PATH = KaggleDatasets().get_gcs_path('melanoma-384x384')
 TRAINING_FILENAMES = np.array(tf.io.gfile.glob(GCS_PATH + '/train*.tfrec'))
TEST_FILENAMES = np.array(tf.io.gfile.glob(GCS_PATH + '/test*.tfrec'))
CLASSES = [0,1]
X_train = X_train / 255.0
test = test / 255.0<categorify>
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 data_augment(image, label):
image = tf.image.random_flip_left_right(image)
 image = tf.image.random_flip_up_down(image)
 image = tf.image.random_hue(image, 0.01)
 image = tf.image.random_saturation(image, 0.7, 1.3)
 image = tf.image.random_contrast(image, 0.8, 1.2)
 image = tf.image.random_brightness(image, 0.1)
 return image, label
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)
 NUM_TRAINING_IMAGES = count_data_items(TRAINING_FILENAMES)
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
print('Dataset: {} training images, {} unlabeled test images'.format(NUM_TRAINING_IMAGES, NUM_TEST_IMAGES))
Y_train = to_categorical(Y_train, num_classes = 10)
 print(Y_train[0] )<split>
def lrfn(epoch):
LR_START = 0.000005
LR_MAX = 0.000020 * strategy.num_replicas_in_sync
LR_MIN = 0.000001
LR_RAMPUP_EPOCHS = 5
LR_SUSTAIN_EPOCHS = 0
LR_EXP_DECAY =.8
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
random_seed=2
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=random_seed )<choose_model_class>
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, BatchNormalization, Activation, Dropout
from tensorflow.keras.regularizers import l2
model = Sequential()<choose_model_class>
with strategy.scope() :
model0 = tf.keras.Sequential([
efn.EfficientNetB0(
 input_shape=(*IMAGE_SIZE, 3),
weights='imagenet',
include_top=False 
),
GlobalAveragePooling2D() ,
Dense(1, activation='sigmoid')
 ])
 model0.compile(
 optimizer='adam',
loss = 'binary_crossentropy',
metrics=['accuracy'] 
)
 with strategy.scope() :
model1 = tf.keras.Sequential([
efn.EfficientNetB1(
 input_shape=(*IMAGE_SIZE, 3),
weights='imagenet',
include_top=False 
),
GlobalAveragePooling2D() ,
Dense(1, activation='sigmoid')
 ])
 model1.compile(
 optimizer='adam',
loss = 'binary_crossentropy',
metrics=['accuracy'] 
)
 with strategy.scope() :
model2 = tf.keras.Sequential([
efn.EfficientNetB2(
 input_shape=(*IMAGE_SIZE, 3),
weights='imagenet',
include_top=False 
),
GlobalAveragePooling2D() ,
Dense(1, activation='sigmoid')
 ])
 model2.compile(
 optimizer='adam',
loss = 'binary_crossentropy',
metrics=['accuracy'] 
)
 with strategy.scope() :
model3 = tf.keras.Sequential([
efn.EfficientNetB3(
 input_shape=(*IMAGE_SIZE, 3),
weights='imagenet',
include_top=False 
),
GlobalAveragePooling2D() ,
Dense(1, activation='sigmoid')
 ])
 model3.compile(
 optimizer='adam',
loss = 'binary_crossentropy',
metrics=['accuracy'] 
)
 with strategy.scope() :
model4 = tf.keras.Sequential([
efn.EfficientNetB4(
 input_shape=(*IMAGE_SIZE, 3),
weights='imagenet',
include_top=False 
),
GlobalAveragePooling2D() ,
Dense(1, activation='sigmoid')
 ])
 model4.compile(
 optimizer='adam',
loss = 'binary_crossentropy',
metrics=['accuracy'] 
)
 with strategy.scope() :
model6 = tf.keras.Sequential([
efn.EfficientNetB6(
 input_shape=(*IMAGE_SIZE, 3),
weights='imagenet',
include_top=False 
),
GlobalAveragePooling2D() ,
Dense(1, activation='sigmoid')
 ])
 model6.compile(
 optimizer='adam',
loss = 'binary_crossentropy',
metrics=['accuracy']
)
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dense(10, activation = "softmax"))<choose_model_class>
lr_schedule = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=1)
 history0 = model0.fit(get_training_dataset() , steps_per_epoch=STEPS_PER_EPOCH, epochs=EPOCHS, callbacks=[lr_schedule])
 history1 = model1.fit(get_training_dataset() , steps_per_epoch=STEPS_PER_EPOCH, epochs=EPOCHS, callbacks=[lr_schedule])
 history2 = model2.fit(get_training_dataset() , steps_per_epoch=STEPS_PER_EPOCH, epochs=EPOCHS, callbacks=[lr_schedule])
 history3 = model3.fit(get_training_dataset() , steps_per_epoch=STEPS_PER_EPOCH, epochs=EPOCHS, callbacks=[lr_schedule])
 history4 = model4.fit(get_training_dataset() , steps_per_epoch=STEPS_PER_EPOCH, epochs=EPOCHS, callbacks=[lr_schedule])
 history6 = model6.fit(get_training_dataset() , steps_per_epoch=STEPS_PER_EPOCH, epochs=EPOCHS, callbacks=[lr_schedule] )
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )<choose_model_class>
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities0 = model0.predict(test_images_ds)
 probabilities1 = model1.predict(test_images_ds)
 probabilities2 = model2.predict(test_images_ds)
 probabilities3 = model3.predict(test_images_ds)
 probabilities4 = model4.predict(test_images_ds)
 probabilities6 = model6.predict(test_images_ds )
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )<choose_model_class>
print('Generating submission.csv files...')
 print('Generating submission.csv file...')
 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' )
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 epochs = 30
batch_size = 86<init_hyperparams>
pred_df0 = pd.DataFrame({'image_name': test_ids, 'target': np.concatenate(probabilities0)})
 pred_df1 = pd.DataFrame({'image_name': test_ids, 'target': np.concatenate(probabilities1)})
 pred_df2 = pd.DataFrame({'image_name': test_ids, 'target': np.concatenate(probabilities2)})
 pred_df3 = pd.DataFrame({'image_name': test_ids, 'target': np.concatenate(probabilities3)})
 pred_df4 = pd.DataFrame({'image_name': test_ids, 'target': np.concatenate(probabilities4)})
 pred_df6 = pd.DataFrame({'image_name': test_ids, 'target': np.concatenate(probabilities6)})
 pred_df6.head()
datagen = 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)
 datagen.fit(X_train )<train_model>
sub0 = sub.copy()
sub1 = sub.copy()
sub2 = sub.copy()
sub3 = sub.copy()
sub4 = sub.copy()
sub6 = sub.copy()
sub4.head()
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )<train_model>
del sub0['target']
sub0 = sub0.merge(pred_df0, on='image_name')
 sub0.to_csv('submission0.csv', index=False)
 del sub1['target']
sub1 = sub1.merge(pred_df1, on='image_name')
 sub1.to_csv('submission1.csv', index=False)
 del sub2['target']
sub2 = sub2.merge(pred_df2, on='image_name')
 sub2.to_csv('submission2.csv', index=False)
 del sub3['target']
sub3 = sub3.merge(pred_df3, on='image_name')
 sub3.to_csv('submission3.csv', index=False)
 del sub4['target']
sub4 = sub4.merge(pred_df4, on='image_name')
 sub4.to_csv('submission4.csv', index=False)
 del sub6['target']
sub6 = sub6.merge(pred_df6, on='image_name')
 sub6.to_csv('submission6.csv', index=False)
 sub4.head()
<compute_test_metric><EOS> 
ensemble1 =(sub0['target'] + sub1['target'] + sub2['target'] + sub3['target'] + sub4['target'] + sub6['target'])/6
ensemble_img1 = sub1['image_name']
ensemble_sub1 = pd.concat([ensemble_img1, ensemble1], axis = 1)
 ensemble_sub1.to_csv('submission7.csv', index=False)
 ensemble_sub1.head()