Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<compute_test_metric>
!pip install -q efficientnet
cvscore = np.sqrt(((y_pred_lgb - y_train.values)** 2 ).mean())
 print("CV score = " + str(cvscore))<import_modules>
import numpy as np
import pandas as pd
import os
import re
import cv2
import math
import time
from tqdm import tqdm_notebook as tqdm
from sklearn.model_selection import train_test_split
from kaggle_datasets import KaggleDatasets
import tensorflow as tf
import tensorflow.keras.backend as K
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model,Sequential
from tensorflow.keras import optimizers
import efficientnet.tfkeras as efn
import seaborn as sns
import matplotlib.pyplot as plt
from plotly.subplots import make_subplots
import plotly.express as px
import xgboost as xgb<init_hyperparams>
print("Tensorflow version " + tf.__version__)
 AUTO = tf.data.experimental.AUTOTUNE
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync)
 DATASET = '512x512-melanoma-tfrecords-70k-images'
GCS_PATH = KaggleDatasets().get_gcs_path(DATASET )
xgbParams = {'learning_rate': 0.005,
'max_depth': 10,
'sabsample': 0.9,
'colsample_bytree': 0.9,
'n_estimators': 1000,
'objective': 'reg:linear',
'eval_metric': 'rmse',
'alpha': 0.1,
'verbosity': 0,
}<train_model>
train_data = pd.read_csv('.. /input/siim-isic-melanoma-classification/train.csv')
 train_data.head()
trainX, testX, trainY, testY = train_test_split(X_train, y_train, test_size=0.33, random_state=42)
 d_train = xgb.DMatrix(trainX, trainY)
 d_eval = xgb.DMatrix(testX, testY)
 watchlist = [(d_eval, 'eval'),(d_train, 'train')]
n_rounds = 5000
XGBmodel = xgb.train(xgbParams, d_train, n_rounds, watchlist, early_stopping_rounds=100, verbose_eval=1000)
 importance = np.asarray(list(XGBmodel.get_score(importance_type='gain' ).values()))
ranking = np.argsort(-importance)
 fig, ax = plt.subplots(figsize=(11, 9))
sns.barplot(x=importance[ranking], y=X_train.columns.values[ranking], orient='h')
 ax.set_xlabel("feature importance")
 plt.tight_layout()<train_model>
train_data.isna().sum()
y_pred_xgb = np.zeros(np.shape(X_train)[0])
 predictions_xgb = np.zeros(np.shape(X_test)[0])
 for train_idx, test_idx in kf.split(X_train):
trainX, testX = X_train.iloc[train_idx], X_train.iloc[test_idx]
trainY, testY = y_train.iloc[train_idx], y_train.iloc[test_idx]
d_train = xgb.DMatrix(trainX, trainY)
 d_eval = xgb.DMatrix(testX, testY)
 watchlist = [(d_eval, 'eval'),(d_train, 'train')]
XGBmodel = xgb.train(xgbParams, d_train, n_rounds, watchlist, early_stopping_rounds=100, verbose_eval=1000)
 y_pred_xgb[test_idx] = XGBmodel.predict(xgb.DMatrix(testX))
predictions_xgb += XGBmodel.predict(xgb.DMatrix(X_test)) / n_folds<compute_test_metric>
SEED = 42
BATCH_SIZE = 32 * strategy.num_replicas_in_sync
SIZE = [512,512]
LR = 0.00004
EPOCHS = 12
WARMUP = 5
WEIGHT_DECAY = 0
LABEL_SMOOTHING = 0.05
TTA = 4
cvscore = np.sqrt(((y_pred_xgb - y_train.values)** 2 ).mean())
 print("CV score = " + str(cvscore))<import_modules>
def seed_everything(SEED):
np.random.seed(SEED)
 tf.random.set_seed(SEED)
 seed_everything(SEED)
 train_filenames = tf.io.gfile.glob(GCS_PATH + '/train*.tfrec')
 test_filenames = tf.io.gfile.glob(GCS_PATH + '/test*.tfrec' )
from catboost import CatBoostRegressor<choose_model_class>
train_filenames,valid_filenames = train_test_split(train_filenames,test_size = 0.2,random_state = SEED )
Catmodel = CatBoostRegressor(iterations=2000,
learning_rate = 0.005,
use_best_model = True,
eval_metric = 'RMSE',
loss_function = 'RMSE',
boosting_type = 'Ordered',
verbose = 0 )<predict_on_test>
def decode_image(image):
image = tf.image.decode_jpeg(image, channels=3)
 image = tf.cast(image, tf.float32)/255.0
image = tf.reshape(image, [*SIZE, 3])
 return image
def data_augment(image, label=None, seed=SEED):
image = tf.image.rot90(image,k=np.random.randint(4))
image = tf.image.random_flip_left_right(image, seed=seed)
 image = tf.image.random_flip_up_down(image, seed=seed)
 if label is None:
return image
else:
return image, label
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'])
 image_name = example['image_name']
return image, image_name
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)
.with_options(ignore_order)
.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=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 plot_transform(num_images):
plt.figure(figsize=(30,10))
x = load_dataset(train_filenames, labeled=False)
 image,_ = iter(x ).next()
for i in range(1,num_images+1):
plt.subplot(1,num_images+1,i)
 plt.axis('off')
 image = data_augment(image=image)
 plt.imshow(image )
y_pred_cat = np.zeros(np.shape(X_train)[0])
 predictions_cat = np.zeros(np.shape(X_test)[0])
 for train_idx, test_idx in kf.split(X_train):
trainX, testX = X_train.iloc[train_idx], X_train.iloc[test_idx]
trainY, testY = y_train.iloc[train_idx], y_train.iloc[test_idx]
Catmodel.fit(trainX, trainY, eval_set=[(testX, testY)], early_stopping_rounds=100)
 y_pred_cat[test_idx] = Catmodel.predict(testX)
 predictions_cat += Catmodel.predict(X_test)/ n_folds<compute_test_metric>
plot_transform(7 )
cvscore = np.sqrt(((y_pred_cat - y_train.values)** 2 ).mean())
 print("CV score(CatBoost)= " + str(cvscore))<prepare_output>
train_dataset =(load_dataset(train_filenames, labeled=True)
.map(data_augment, num_parallel_calls=AUTO)
.shuffle(SEED)
.batch(BATCH_SIZE,drop_remainder=True)
.repeat() 
.prefetch(AUTO))
valid_dataset =(load_dataset(valid_filenames, labeled=True)
.batch(BATCH_SIZE)
.cache() 
.prefetch(AUTO))
X1 = pd.DataFrame({'LGB': y_pred_lgb, 'XGB': y_pred_xgb, 'CAT': y_pred_cat})
 X1.head(7 )<predict_on_test>
with strategy.scope() :
model = tf.keras.Sequential([
efn.EfficientNetB7(input_shape=(*SIZE, 3),weights='imagenet',pooling='avg',include_top=False),
Dense(1, activation='sigmoid')
 ])
 model.compile(
 optimizer='adam',
loss = tf.keras.losses.BinaryCrossentropy(label_smoothing = LABEL_SMOOTHING),
metrics=['accuracy',tf.keras.metrics.AUC(name='auc')])
linearModel1 = BayesianRidge()
linearModel1.fit(X1, y_train)
 X2 = pd.DataFrame({'LGB': predictions_lgb, 'XGB': predictions_xgb, 'CAT': predictions_cat})
 predictions1 = linearModel1.predict(X2 )<define_variables>
def train() :
STEPS_PER_EPOCH = count_data_items(train_filenames)// BATCH_SIZE
history = model.fit(
 train_dataset,
epochs=EPOCHS,
callbacks=[lr_schedule],
steps_per_epoch=STEPS_PER_EPOCH,
validation_data=valid_dataset)
 string = 'Train acc:{:.4f} Train loss:{:.4f} AUC: {:.4f}, Val acc:{:.4f} Val loss:{:.4f} Val AUC: {:.4f}'.format(\
model.history.history['accuracy'][-1],model.history.history['loss'][-1],\
model.history.history['auc'][-1],\
model.history.history['val_accuracy'][-1],model.history.history['val_loss'][-1],\
model.history.history['val_auc'][-1])
 return string
MLPmodel = MLPRegressor(random_state=1220, activation='relu', solver='sgd', learning_rate='adaptive', tol=1e-06, hidden_layer_sizes=(250,))
SVRmodel = NuSVR(kernel='rbf', degree=4, gamma='auto', nu=0.59, coef0=0.053, tol=1e-6)
 X_train =(X_train - np.mean(X_train)) / np.std(X_train)
 X_test =(X_test - np.mean(X_test)) / np.std(X_test)
 y_pred_nn = np.zeros(np.shape(X_train)[0])
 predictions_nn = np.zeros(np.shape(X_test)[0])
 y_pred_svr = np.zeros(np.shape(X_train)[0])
 predictions_svr = np.zeros(np.shape(X_test)[0])
 for train_idx, test_idx in kf.split(X_train):
trainX, testX = X_train.iloc[train_idx], X_train.iloc[test_idx]
trainY, testY = y_train.iloc[train_idx], y_train.iloc[test_idx]
MLPmodel.fit(trainX, trainY)
 SVRmodel.fit(trainX, trainY)
 y_pred_nn[test_idx] = MLPmodel.predict(testX)
 y_pred_svr[test_idx] = SVRmodel.predict(testX)
 predictions_nn += MLPmodel.predict(X_test)/ n_folds
predictions_svr += SVRmodel.predict(X_test)/ n_folds<compute_test_metric>
train()
cvscore = np.sqrt(((y_pred_nn - y_train.values)** 2 ).mean())
 print("CV score(Neural network)= " + str(cvscore))
cvscore = np.sqrt(((y_pred_svr - y_train.values)** 2 ).mean())
 print("CV score(SVR)= " + str(cvscore))<choose_model_class>
num_test_images = count_data_items(test_filenames)
 submission_df = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/sample_submission.csv')
 for i in range(TTA):
test_dataset =(load_dataset(test_filenames, labeled=False,ordered=True)
.map(data_augment, num_parallel_calls=AUTO)
.batch(BATCH_SIZE))
test_dataset_images = test_dataset.map(lambda image, image_name: image)
 test_dataset_image_name = test_dataset.map(lambda image, image_name: image_name ).unbatch()
test_ids = next(iter(test_dataset_image_name.batch(num_test_images)) ).numpy().astype('U')
 test_pred = model.predict(test_dataset_images, verbose=1)
 pred_df = pd.DataFrame({'image_name': test_ids, 'target': np.concatenate(test_pred)})
 temp = submission_df.copy()
del temp['target']
submission_df['target'] += temp.merge(pred_df,on="image_name")['target']/TTA
<save_to_csv><EOS> 
submission_df.to_csv('submission.csv', index=False)
 pd.Series(np.round(submission_df['target'].values)).value_counts()