Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<choose_model_class>
!pip install -q efficientnet
model.add(Conv2D(filters = 128, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 128, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)) )<choose_model_class>
warnings.filterwarnings('ignore')
 seed_val = 42
random.seed(seed_val)
 np.random.seed(seed_val )
model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(512, activation = "relu", use_bias= True))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))<choose_model_class>
orange_black = [
'
]
plt.style.use('ggplot' )
model.compile(optimizer = 'adam' , loss = "categorical_crossentropy", metrics=["accuracy"] )<train_model>
base_path = '/kaggle/input/siim-isic-melanoma-classification'
train_img_path = '/kaggle/input/siim-isic-melanoma-classification/jpeg/train/'
test_img_path = '/kaggle/input/siim-isic-melanoma-classification/jpeg/test/'
img_stats_path = '/kaggle/input/melanoma2020imgtabular'
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.2,
width_shift_range=0.2,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_t )<train_model>
train = pd.read_csv(os.path.join(base_path, 'train.csv'))
test = pd.read_csv(os.path.join(base_path, 'test.csv'))
sample = pd.read_csv(os.path.join(base_path, 'sample_submission.csv'))
model.fit_generator(datagen.flow(X_t,Y_t, batch_size= 82),
epochs = 60, validation_data =(X_v,Y_v),
verbose = 2, steps_per_epoch=X_t.shape[0] // 82)
<predict_on_test>
print(
 f'Train data has {train.shape[1]} features, {train.shape[0]} observations and Test data {test.shape[1]} features, {test.shape[0]} observations.
 Train features are:
{train.columns.tolist() }
Test features are:
{test.columns.tolist() }'
)
ewsult=model.predict(X_test )<prepare_output>
train.columns = [
'img_name', 'id', 'sex', 'age', 'location', 'diagnosis',
'benign_malignant', 'target'
]
test.columns = ['img_name', 'id', 'sex', 'age', 'location']
ewsult=np.argmax(ewsult,axis=1 )<save_to_csv>
for df in [train, test]:
df['location'].fillna('unknown', inplace=True )
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),ewsult],axis = 1)
 submission.to_csv("cnnmodel.csv",index=False )<load_from_csv>
ids_train = train.location.values
ids_test = test.location.values
ids_train_set = set(ids_train)
 ids_test_set = set(ids_test)
 location_not_overlap = list(ids_train_set.symmetric_difference(ids_test_set))
n_overlap = len(location_not_overlap)
 if n_overlap == 0:
print(
 f'There are no different body parts occuring between train and test set...' 
)
 else:
print('There are some not overlapping values between train and test set!' )
x_train = pd.read_csv('.. /input/train.csv')
 x_train.head()<categorify>
train['sex'].fillna(train['sex'].mode() [0], inplace=True)
 train['age'].fillna(train['age'].median() , inplace=True )
dim_x = 28
dim_y = 28
batch_size=32
x_train.shape
y_train = np.array(x_train['label'])
 x_train.drop('label', axis = 1, inplace = True)
 x_train = np.array(x_train.values)
 print("data shapes", x_train.shape, y_train.shape, "classes: ",len(np.unique(y_train)))
 classes = len(np.unique(y_train))
x_train = x_train.reshape(( -1, dim_x,dim_y,1))
print(np.unique(y_train))
y = np.zeros(( np.shape(y_train)[0],len(np.unique(y_train))))
for ii in range(len(y_train)) :
y[ii,y_train[ii]] = 1
y_train = y<train_model>
print(
 f'Train missing value count: {train.isnull().sum().sum() }
Test missing value count: {train.isnull().sum().sum() }'
)
no_validation = int(0.1 *(x_train.shape[0]))
x_val = x_train[0:no_validation,...]
y_val = y_train[0:no_validation,...]
x_train = x_train[no_validation:,...]
y_train = y_train[no_validation:,...]
print(x_train.shape, y_train.shape, x_val.shape, y_val.shape)
 train_datagen = ImageDataGenerator(rescale = 1./255,\
rotation_range=30,\
width_shift_range=0.025,\
height_shift_range=0.025,\
shear_range=0.35,\
zoom_range=0.075)
 train_generator = train_datagen.flow(x=x_train,\
y=y_train,\
batch_size=batch_size,\
shuffle=True)
 test_datagen = ImageDataGenerator(rescale=1./255)
 val_generator = test_datagen.flow(x=x_val,\
y=y_val,\
batch_size=batch_size,\
shuffle=True )<choose_model_class>
print(
 f'Number of unique Patient ID's in train set: {train.id.nunique() }, Total: {train.id.count() }
Number of unique Patient ID's in test set: {test.id.nunique() }, Total: {test.id.count() }'
)
model = Sequential()
model.add(Conv2D(filters=96, kernel_size=(5,5), strides=1,input_shape=(dim_x,dim_y,1), activation=tf.nn.relu))
model.add(MaxPooling2D(pool_size=2, strides=2))
model.add(Conv2D(filters=256, kernel_size=(5,5), strides=1, activation=tf.nn.relu))
model.add(Conv2D(filters=384, kernel_size=(3,3), strides=1, activation=tf.nn.relu))
model.add(Conv2D(filters=384, kernel_size=(3,3), strides=1, activation=tf.nn.relu))
model.add(SpatialDropout2D(rate=0.67))
model.add(Conv2D(filters=250, kernel_size=(3,3), strides=1, activation=tf.nn.relu))
model.add(MaxPooling2D(pool_size=2, strides=2))
model.add(Flatten())
 model.add(Reshape(( 250,1)))
 model.add(AveragePooling1D(pool_size=25,strides=25))
model.add(Reshape(( [10])))
 model.add(Activation(tf.nn.softmax))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=1e-4), metrics=['accuracy'] )<choose_model_class>
train['age_min'] = train['id'].map(train.groupby(['id'] ).age.min())
 train['age_max'] = train['id'].map(train.groupby(['id'] ).age.max())
 test['age_min'] = test['id'].map(test.groupby(['id'] ).age.min())
 test['age_max'] = test['id'].map(test.groupby(['id'] ).age.max() )
def learning_schedule(epoch):
if epoch <= 1:
lr = 3e-4
elif epoch <= 10:
lr = 1e-5
elif epoch <= 50:
lr = 3e-6
elif epoch <= 150:
lr = 1e-6
else:
lr = 1e-8
return lr
lrate = LearningRateScheduler(learning_schedule)
 early = EarlyStopping(monitor='val_acc', min_delta=0, patience=600, verbose=1, mode='auto' )<train_model>
train['n_images'] = train.id.map(train.groupby(['id'] ).img_name.count())
 test['n_images'] = test.id.map(test.groupby(['id'] ).img_name.count() )
steps_per_epoch = int(len(y_train)/batch_size)
 max_epochs = 4096
history = model.fit_generator(generator=train_generator,\
steps_per_epoch=steps_per_epoch,\
validation_data=val_generator,\
validation_steps=50,\
epochs=max_epochs,\
callbacks=[early, lrate],\
verbose=2 )<load_from_csv>
for data, location in zip([train, test], [train_img_path, test_img_path]):
images = data['img_name'].values
sizes = np.zeros(images.shape[0])
 for i, path in enumerate(tqdm(images)) :
sizes[i] = os.path.getsize(os.path.join(location, f'{path}.jpg'))
data['image_size'] = sizes
x_test = pd.read_csv('.. /input/test.csv')
 x_test.head()
x_test = np.array(x_test.values)
 x_test = x_test / 255.
 print("data shape", x_test.shape)
 x_test = x_test.reshape(( -1, dim_x,dim_y,1)) 
<predict_on_test>
y_pred = model.predict(x_test )<save_to_csv>
train_attr = pd.read_csv(
 os.path.join(img_stats_path, 'train_mean_colorres.csv'))
test_attr = pd.read_csv(os.path.join(img_stats_path, 'test_mean_colorres.csv'))
results = np.argmax(y_pred,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_datagen.csv",index=False )<load_from_csv>
train = pd.concat([train, train_attr], axis=1)
 test = pd.concat([test, test_attr], axis=1)
 train['res'] = train['width'].astype(str)+ 'x' + train['height'].astype(str)
 test['res'] = test['width'].astype(str)+ 'x' + test['height'].astype(str )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 print(train.shape)
 print(test.shape)
 n_train_img = train.shape[0]
MAX_LR=3e-3
VALID_PCT=3000/n_train_img
VALID_PCT=.1
VALID_PCT<categorify>
mystery = test[test['res'] == '1920x1080']
mystimages = mystery['img_name'].values
nonmystery = test[test['res'] != '1920x1080']
nonmystimages = nonmystery['img_name'].values
random_myst_images = [np.random.choice(mystimages+'.jpg')for i in range(12)]
random_nmyst_images = [np.random.choice(nonmystimages+'.jpg')for i in range(12)]
img_dir = '.. /input/siim-isic-melanoma-classification/jpeg/test'
class ArrayDataset(Dataset):
"Sample numpy array dataset"
def __init__(self, x, y):
self.x, self.y = x, y
self.c = 10
def __len__(self):
return len(self.x)
 def __getitem__(self, i):
return self.x[i], self.y[i]
def reshape_and_normalize(arr):
return arr.astype(np.float32 ).reshape([-1, 1, 28, 28])/255.0
def conv2(ni,nf):
return conv_layer(ni,nf,stride=2)
 def conv_and_res(ni,nf):
return nn.Sequential(conv2(ni, nf), res_block(nf))
def create_nn(n_channel=1):
model = nn.Sequential(
 conv_and_res(n_channel, 8),
conv_and_res(8, 16),
conv_and_res(16, 32),
conv_and_res(32, 16),
conv2(16, 10),
Flatten() 
)
 return model
def get_preds(learn):
pred, _ = learn.get_preds(ds_type=DatasetType.Test)
 print(len(pred))
pred = pd.Series(np.argmax(pred, 1))
print(pred.unique())
 return pred
def get_callbacks() :
return [partial(EarlyStoppingCallback,
patience=3 
)]
def create_learner(data, model):
learn = Learner(data, model, loss_func = nn.CrossEntropyLoss() ,
metrics=accuracy,
callback_fns=get_callbacks())
 return learn
def create_submission(learn, file='learn.csv'):
pred = get_preds(learn)
 submission = pd.DataFrame({'ImageId': range(1,1+len(pred)) , 'Label': pred})
 submission.to_csv(file, index=False)
 return<train_model>
train40 = pd.read_csv('.. /input/melanoma2020imgtabular/train40Features.csv')
 test40 = pd.read_csv('.. /input/melanoma2020imgtabular/test40Features.csv')
 trainmet = pd.read_csv('.. /input/melanoma2020imgtabular/trainMetrics.csv')
 testmet = pd.read_csv('.. /input/melanoma2020imgtabular/testMetrics.csv' )
%%time
data = prepare_dataset(train, test)
 learn = create_learner(data, create_nn())
 learn.fit_one_cycle(1, max_lr=MAX_LR)
 learn.lr_find(end_lr=10)
 learn.recorder.plot()<split>
train40.drop(['sex', 'age_approx', 'anatom_site_general_challenge'],
axis=1,
inplace=True)
 test40.drop(['sex', 'age_approx', 'anatom_site_general_challenge'],
axis=1,
inplace=True )
def prepare_dataset(train, test):
X = reshape_and_normalize(train.drop('label', axis=1 ).values)
 y = train.label.values
train_x, valid_x, train_y, valid_y = train_test_split(X, y,test_size=VALID_PCT)
 test_x = reshape_and_normalize(test.values)
 train_ds, valid_ds = ArrayDataset(train_x, train_y), ArrayDataset(valid_x, valid_y)
 test_ds = ArrayDataset(test_x, [EmptyLabel() ]*len(test_x))
data = ImageDataBunch.create(train_ds, valid_ds,
test_ds,
bs=100, num_workers=1)
 print("Train/Valid/Test sizes:", len(train_ds), len(valid_ds), len(test_ds))
return data<categorify>
train = pd.concat([train, train40, trainmet], axis=1)
 test = pd.concat([test, test40, testmet], axis=1 )
tfms = get_transforms(do_flip=False)
 len(tfms)
 tfms 
<define_variables>
sex_dummies = pd.get_dummies(train['sex'], prefix='sex')
 train = pd.concat([train, sex_dummies], axis=1)
 sex_dummies = pd.get_dummies(test['sex'], prefix='sex')
 test = pd.concat([test, sex_dummies], axis=1)
 train.drop(['sex','res','img_name','id','diagnosis','benign_malignant'], axis=1, inplace=True)
 test.drop(['sex','res','img_name','id'], axis=1, inplace=True )
%%time
data = ImageDataBunch.from_folder('png/train',
bs=100,
ds_tfms=tfms,
valid_pct=VALID_PCT 
)
 data.add_test(ImageList.from_df(test_df, '.'))
data.normalize()
data<define_variables>
anatom_dummies = pd.get_dummies(train['location'], prefix='anatom')
 train = pd.concat([train, anatom_dummies], axis=1)
 anatom_dummies = pd.get_dummies(test['location'], prefix='anatom')
 test = pd.concat([test, anatom_dummies], axis=1)
 train.drop('location', axis=1, inplace=True)
 test.drop('location', axis=1, inplace=True )
data.show_batch(figsize=(7,6))<train_model>
import xgboost as xgb
from sklearn.model_selection import StratifiedKFold, train_test_split, cross_val_score, cross_validate
from sklearn.metrics import roc_auc_score, roc_curve
%%time
learn1 = create_learner(data, create_nn(3))
learn1.fit_one_cycle(1, max_lr=MAX_LR)
 learn1.lr_find(end_lr=10)
 learn1.recorder.plot()<train_model>
X = train.drop('target', axis=1)
 y = train.target
learn1.fit_one_cycle(100, max_lr=MAX_LR)
 learn1.recorder.plot()<train_on_grid>
X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size=0.2,
stratify=y,
random_state=42)
 cv = StratifiedKFold(5, shuffle=True, random_state=42 )
%%time
learn2 = cnn_learner(data,
models.resnet50,
metrics=[accuracy], callback_fns=get_callbacks())
 learn2.fit_one_cycle(1, max_lr=MAX_LR)
 learn2.lr_find(end_lr=10)
 learn2.recorder.plot()<train_model>
xg = xgb.XGBClassifier(
 n_estimators=750,
min_child_weight=0.81,
learning_rate=0.025,
max_depth=2,
subsample=0.80,
colsample_bytree=0.42,
gamma=0.10,
random_state=42,
n_jobs=-1,
)
%%time
learn2.fit_one_cycle(100, max_lr=MAX_LR)
 learn2.recorder.plot()<set_options>
estimators = [xg]
!rm -Rf png<load_from_csv>
def model_check(X_train, y_train, estimators, cv):
model_table = pd.DataFrame()
row_index = 0
for est in estimators:
MLA_name = est.__class__.__name__
model_table.loc[row_index, 'Model Name'] = MLA_name
cv_results = cross_validate(est,
X_train,
y_train,
cv=cv,
scoring='roc_auc',
return_train_score=True,
n_jobs=-1)
 model_table.loc[row_index,
'Train roc Mean'] = cv_results['train_score'].mean()
model_table.loc[row_index,
'Test roc Mean'] = cv_results['test_score'].mean()
model_table.loc[row_index, 'Test Std'] = cv_results['test_score'].std()
model_table.loc[row_index, 'Time'] = cv_results['fit_time'].mean()
row_index += 1
model_table.sort_values(by=['Test roc Mean'],
ascending=False,
inplace=True)
 return model_table
X_full = pd.read_csv(".. /input/train.csv",index_col=0)
 X_full_test = pd.read_csv(".. /input/test.csv",index_col=0)
 X_full.shape<split>
raw_models = model_check(X_train, y_train, estimators, cv)
 display(raw_models )
y = X_full.Survived
features = ['Pclass','Sex','Age','SibSp','Parch']
X = X_full[features].copy()
X_test = X_full_test[features].copy()
X_train, X_valid, y_train, y_valid = train_test_split(X,y,train_size=0.8,test_size=0.2,random_state=0)
 X.isnull().sum() 
<data_type_conversions>
xg.fit(X_train, y_train)
 validation = xg.predict_proba(X_test)[:, 1]
roc_auc_score(y_test, validation )
X_train.fillna(X_train.mean() ,inplace=True)
 X_valid.fillna(X_valid.mean() ,inplace=True)
 X_test.fillna(X_valid.mean() ,inplace=True)
 X_train.head() 
<categorify>
predictions = xg.predict_proba(test)[:, 1]
label_encoder = LabelEncoder()
X_train['Sex'] = label_encoder.fit_transform(X_train['Sex'])
 X_valid['Sex'] = label_encoder.transform(X_valid['Sex'])
 X_test['Sex'] = label_encoder.fit_transform(X_test['Sex'])
 X_train.head()<train_model>
meta_df = pd.DataFrame(columns=['image_name', 'target'])
 meta_df['image_name'] = sample['image_name']
meta_df['target'] = predictions
my_model = XGBClassifier(n_estimators=1000, learning_rate=0.05)
 my_model.fit(X_train, y_train,
early_stopping_rounds=5,
eval_set=[(X_valid, y_valid)],
verbose=False )<predict_on_test>
meta_df.to_csv('meta_with_img_data.csv', header=True, index=False)
predictions = my_model.predict(X_valid)
 print("Accuracy Score: " + str(accuracy_score(predictions, y_valid)) )<predict_on_test>
adv_train = train.copy()
adv_train.drop('target', axis=1, inplace=True)
 adv_test = test.copy()
adv_train['dataset_label'] = 0
adv_test['dataset_label'] = 1
adv_master = pd.concat([adv_train, adv_test], axis=0)
 adv_X = adv_master.drop('dataset_label', axis=1)
 adv_y = adv_master['dataset_label']
preds = my_model.predict(X_valid)
 preds_test = my_model.predict(X_test)
<categorify>
adv_X_train, adv_X_test, adv_y_train, adv_y_test = train_test_split(adv_X,
adv_y,
test_size=0.4,
stratify=adv_y,
random_state=42 )
features = ['Pclass','Sex','Age','SibSp','Embarked']
X = X_full[features].copy()
X_test = X_full_test[features].copy()
X.fillna(X.mean() ,inplace=True)
 X_test.fillna(X_test.mean() ,inplace=True)
 X['Embarked'] = X['Embarked'].fillna('S')
 X_test['Embarked'] = X_test['Embarked'].fillna('S')
 X['Sex'] = label_encoder.fit_transform(X['Sex'])
 X_test['Sex'] = label_encoder.transform(X_test['Sex'])
 X['Embarked'] = label_encoder.fit_transform(X['Embarked'])
 X_test['Embarked'] = label_encoder.transform(X_test['Embarked'])
 X_train, X_valid, y_train, y_valid = train_test_split(X,y,train_size=0.8,test_size=0.2,random_state=0)
 my_model.fit(X_train, y_train,
early_stopping_rounds=5,
eval_set=[(X_valid, y_valid)],
verbose=False)
 predictions = my_model.predict(X_valid)
 print("Accuracy Score: " + str(accuracy_score(predictions, y_valid)))
 preds = my_model.predict(X_valid)
 preds_test = my_model.predict(X_test)
<save_to_csv>
xg_adv = xgb.XGBClassifier(
 random_state=42,
n_jobs=-1, 
)
 xg_adv.fit(adv_X_train, adv_y_train)
 validation = xg_adv.predict_proba(adv_X_test)[:,1]
output = pd.DataFrame({'PassengerId': X_test.index,
'Survived': preds_test})
 output.to_csv('submission.csv', index=False )<import_modules>
adv_X.drop(['n_images', 'image_size','width','height','total_pixels','reds','blues','greens','mean_colors', 'age_min', 'age_max'], axis=1, inplace=True)
 adv_X_train, adv_X_test, adv_y_train, adv_y_test = train_test_split(adv_X,
adv_y,
test_size=0.4,
stratify=adv_y,
random_state=42)
 xg_adv.fit(adv_X_train, adv_y_train)
 validation = xg_adv.predict_proba(adv_X_test)[:,1]
import numpy as np
import pandas as pd
from IPython.display import Image
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from tensorflow.python import keras
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropout, MaxPool2D, BatchNormalization, Activation
from tensorflow.python.keras.callbacks import ReduceLROnPlateau
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.optimizers import RMSprop, Adam
from tensorflow.python.keras.activations import relu
from tensorflow.python.keras.utils import plot_model
from tensorflow.python.keras.losses import categorical_crossentropy<load_from_csv>
X_train.drop(['n_images', 'image_size','width','height','total_pixels','reds','blues','greens','mean_colors', 'age_min', 'age_max'], axis=1, inplace=True)
 test.drop(['n_images', 'image_size','width','height','total_pixels','reds','blues','greens','mean_colors', 'age_min', 'age_max'], axis=1, inplace=True )
digit_data = pd.read_csv('.. /input/train.csv')
 digit_data.head(5 )<prepare_x_and_y>
xg= xgb.XGBClassifier(
 n_estimators=750,
learning_rate=0.015,
min_child_weight= 218,
max_delta_step= 4,
max_depth= 2,
subsample= 0.751,
colsample_bytree= 0.77,
gamma= 24,
reg_lambda= 11,
random_state=42,
n_jobs=-1,
)
img_rows, img_cols = 28,28
num_classes = 10
def data_prep_train(raw,val_frac):

 num_images = int(raw.shape[0])
 y_full = keras.utils.to_categorical(raw.label, num_classes)
 X_as_array = raw.values[:,1:]
X_shaped_array = X_as_array.reshape(num_images, img_rows, img_cols, 1)
 X_full = X_shaped_array / 255
X_train, X_val, y_train, y_val = train_test_split(X_full, y_full, test_size=val_frac)
 return X_train, X_val, y_train, y_val

 def data_prep_predict(raw):
num_images = int(raw.shape[0])
 X_as_array = raw.values
X_shaped_array = X_as_array.reshape(num_images, img_rows, img_cols, 1)
 X = X_shaped_array / 255
return X<choose_model_class>
raw_models = model_check(X_train, y_train, [xg], cv)
 display(raw_models )
def build_model(layer_sizes=[32, 32, 64, 64, 256], kernel_sizes=[5,5,3,3], activation = 'relu'):

 model = Sequential()
model.add(Conv2D(layer_sizes[0], kernel_size=kernel_sizes[0], padding = 'same', input_shape=(img_rows, img_cols, 1)))
 model.add(BatchNormalization())
 model.add(Activation(activation))
model.add(Conv2D(layer_sizes[1], kernel_size=kernel_sizes[1], padding = 'same'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(rate=0.25))
model.add(Conv2D(layer_sizes[2], kernel_size=kernel_sizes[2], padding = 'same'))
model.add(BatchNormalization())
 model.add(Activation(activation))
model.add(Conv2D(layer_sizes[3], kernel_size=kernel_sizes[3], padding = 'same'))
model.add(BatchNormalization())
 model.add(Activation(activation))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(rate=0.25))
model.add(Flatten())
 model.add(Dense(layer_sizes[4]))
model.add(BatchNormalization())
 model.add(Activation(activation))
model.add(Dropout(rate=0.5))
model.add(Dense(num_classes, activation='softmax'))
return model
my_model = build_model()
plot_model(my_model, to_file='my_model.png', show_shapes=True, show_layer_names=True)
 Image('my_model.png' )<init_hyperparams>
xg.fit(X_train, y_train)
 predictions = xg.predict_proba(test)[:, 1]
meta_df = pd.DataFrame(columns=['image_name', 'target'])
 meta_df['image_name'] = sample['image_name']
meta_df['target'] = predictions
meta_df.to_csv('meta_simplified_img_data.csv', header=True, index=False)
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, )<train_model>
tf.random.set_seed(seed_val )
def train_model(model, optimizer='adam', batch_size=64, epochs=1, verbose=1, callbacks=[]):

 model.compile(loss=categorical_crossentropy, optimizer=optimizer, metrics=['accuracy'])
 history = model.fit(datagen.flow(X_train, y_train, batch_size=batch_size),
epochs=epochs,
verbose=verbose,
validation_data=(X_val,y_val),
callbacks=callbacks)
 return history<train_model>
GCS_PATH = KaggleDatasets().get_gcs_path('melanoma-384x384')
 filenames_train = np.array(tf.io.gfile.glob(GCS_PATH + '/train*.tfrec'))
filenames_test = np.array(tf.io.gfile.glob(GCS_PATH + '/test*.tfrec'))
X_train, X_val, y_train, y_val = data_prep_train(digit_data,0.1)
 leaky_relu = lambda x: relu(x, alpha=0.1)
 optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
 lr_reduction = ReduceLROnPlateau(monitor='val_acc', patience=3, verbose=1, factor=0.5, min_lr=0.00001)
 my_model = build_model(activation=leaky_relu)
 history = train_model(my_model, optimizer=optimizer, epochs=40, batch_size = 128, verbose=1, callbacks=[lr_reduction])
 plt.figure(figsize=(20,9))
plt.plot(range(20,41),history.history['val_acc'][19:], color='red', label='validation accuracy')
 plt.plot(range(20,41),history.history['acc'][19:], color='blue', label='accuracy')
 legend = plt.legend(loc='best', shadow=True)
 plt.show()<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')))
 print('REPLICAS: ', strategy.num_replicas_in_sync)
 AUTO = tf.data.experimental.AUTOTUNE
subm_examples = pd.read_csv('.. /input/test.csv')
 X_subm = data_prep_predict(subm_examples)
 y_subm = my_model.predict(X_subm)
 n_rows = y_subm.shape[0]
y_subm = [np.argmax(y_subm[row,:])for row in range(n_rows)]
output = pd.DataFrame({'ImageId': range(1,n_rows+1), 'Label': y_subm})
 output.to_csv('submission.csv', index=False )<import_modules>
cfg = dict(
 batch_size=32,
img_size=384,
lr_start=0.000005,
lr_max=0.00000125,
lr_min=0.000001,
lr_rampup=5,
lr_sustain=0,
lr_decay=0.8,
epochs=12,
transform_prob=1.0,
rot=180.0,
shr=2.0,
hzoom=8.0,
wzoom=8.0,
hshift=8.0,
wshift=8.0,
optimizer='adam',
label_smooth_fac=0.05,
tta_steps=20
)
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader, sampler
from torchvision import transforms
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time
from random import shuffle, randint
from PIL import Image
import math<load_from_csv>
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift,
width_shift):

 rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1], dtype='float32')
 zero = tf.constant([0], dtype='float32')
 rotation_matrix = tf.reshape(
 tf.concat([c1, s1, zero, -s1, c1, zero, zero, zero, one], axis=0),
[3, 3])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = tf.reshape(
 tf.concat([one, s2, zero, zero, c2, zero, zero, zero, one], axis=0),
[3, 3])
 zoom_matrix = tf.reshape(
 tf.concat([
one / height_zoom, zero, zero, zero, one / width_zoom, zero, zero,
zero, one
],
axis=0), [3, 3])
 shift_matrix = tf.reshape(
 tf.concat(
 [one, zero, height_shift, zero, one, width_shift, zero, zero, one],
axis=0), [3, 3])
 return K.dot(K.dot(rotation_matrix, shear_matrix),
K.dot(zoom_matrix, shift_matrix))
def transform(image, cfg):

 DIM = cfg['img_size']
XDIM = DIM % 2
rot = cfg['rot'] * tf.random.normal([1], dtype='float32')
 shr = cfg['shr'] * tf.random.normal([1], dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ cfg['hzoom']
w_zoom = 1.0 + tf.random.normal([1], dtype='float32')/ cfg['wzoom']
h_shift = cfg['hshift'] * tf.random.normal([1], dtype='float32')
 w_shift = cfg['wshift'] * tf.random.normal([1], dtype='float32')
 m = get_mat(rot, shr, h_zoom, w_zoom, h_shift, w_shift)
 x = tf.repeat(tf.range(DIM // 2, -DIM // 2, -1), DIM)
 y = tf.tile(tf.range(-DIM // 2, DIM // 2), [DIM])
 z = tf.ones([DIM * DIM], dtype='int32')
 idx = tf.stack([x, y, z])
 idx2 = K.dot(m, tf.cast(idx, dtype='float32'))
idx2 = K.cast(idx2, dtype='int32')
 idx2 = K.clip(idx2, -DIM // 2 + XDIM + 1, DIM // 2)
 idx3 = tf.stack([DIM // 2 - idx2[0, ], DIM // 2 - 1 + idx2[1, ]])
 d = tf.gather_nd(image, tf.transpose(idx3))
return tf.reshape(d, [DIM, DIM, 3])
 def prepare_image(img, cfg=None, augment=True):

 img = tf.image.decode_jpeg(img, channels=3)
 img = tf.image.resize(img, [cfg['img_size'], cfg['img_size']],
antialias=True)
 img = tf.cast(img, tf.float32)/ 255.0
if augment:
if cfg['transform_prob'] > tf.random.uniform([1], minval=0, maxval=1):
img = transform(img, cfg)
 img = tf.image.random_flip_left_right(img)
 img = tf.image.random_saturation(img, 0.7, 1.3)
 img = tf.image.random_contrast(img, 0.8, 1.2)
 img = tf.image.random_brightness(img, 0.1)
 return img
class DigitDataset(Dataset):

 def __init__(self, csv_file, root_dir, train=False, transform=None):
self.digit_df = pd.read_csv(root_dir + csv_file)
 self.transform = transform
self.train = train
def __len__(self):
return len(self.digit_df)
 def __getitem__(self, item):
if self.train:
digit = self.digit_df.iloc[item, 1:].values
digit = digit.astype('float' ).reshape(( 28, 28))
label = self.digit_df.iloc[item, 0]
else:
digit = self.digit_df.iloc[item, :].values
digit = digit.astype('float' ).reshape(( 28, 28))
label = 0
sample = [digit, label]
if self.transform:
sample[0] = self.transform(sample[0])
 return sample<categorify>
def read_labeled_tfrecord(example):
LABELED_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, LABELED_TFREC_FORMAT)
 return example['image'], example['target']
def read_unlabeled_tfrecord(example):
UNLABELED_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),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 return example['image'], example['image_name']
def count_data_items(filenames):
n = [
int(re.compile(r'-([0-9]*)\.' ).search(filename ).group(1))
for filename in filenames
]
return np.sum(n )
class Regularize(object):

 def __init__(self, max_pixel=255):
self.max_pixel = max_pixel
def __call__(self, digit):
assert isinstance(digit, np.ndarray)
 digit = digit / self.max_pixel
return digit
class ToTensor(object):

 def __call__(self, digit):
assert isinstance(digit, np.ndarray)
 digit = digit.reshape(( 1, 28, 28))
digit = torch.from_numpy(digit)
 digit = digit.float()
return digit<load_from_csv>
def compileNewModel(cfg):

 with strategy.scope() :
model = get_model()
with strategy.scope() :
model.compile(optimizer=cfg['optimizer'],
loss=[
tf.keras.losses.BinaryCrossentropy(
 label_smoothing=cfg['label_smooth_fac']),
tf.keras.losses.BinaryCrossentropy(
 label_smoothing=cfg['label_smooth_fac']),
tf.keras.losses.BinaryCrossentropy(
 label_smoothing=cfg['label_smooth_fac'])
 ],
metrics=[tf.keras.metrics.AUC(name='auc')])
 return model
def getLearnRateCallback(cfg):

 lr_start = cfg['lr_start']
lr_max = cfg['lr_max'] * strategy.num_replicas_in_sync * cfg['batch_size']
lr_min = cfg['lr_min']
lr_rampup = cfg['lr_rampup']
lr_sustain = cfg['lr_sustain']
lr_decay = cfg['lr_decay']
def lrfn(epoch):
if epoch < lr_rampup:
lr =(lr_max - lr_start)/ lr_rampup * epoch + lr_start
elif epoch < lr_rampup + lr_sustain:
lr = lr_max
else:
lr =(lr_max - lr_min)* lr_decay**(epoch - lr_rampup -
lr_sustain)+ lr_min
return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=False)
 return lr_callback
def learnModel(model, ds_train, stepsTrain, cfg, ds_val=None, stepsVal=0):

 callbacks = [getLearnRateCallback(cfg)]
history = model.fit(ds_train,
validation_data=ds_val,
verbose=1,
steps_per_epoch=stepsTrain,
validation_steps=stepsVal,
epochs=cfg['epochs'],
callbacks=callbacks)
 return history
data_np = DigitDataset('train.csv', '.. /input/', train=True)
 print("Number of Training Images: ", len(data_np))
plt.imshow(data_np[5][0], cmap='gray')
 plt.show()
print("Label for the Image: ", data_np[5][1] )<load_from_csv>
ds_train = getTrainDataset(
 filenames_train, cfg ).map(lambda img, label:(img,(label, label, label)))
 stepsTrain = count_data_items(filenames_train)/ \ 
(cfg['batch_size'] * strategy.num_replicas_in_sync)
 model = compileNewModel(cfg)
 learnModel(model, ds_train, stepsTrain, cfg )
composed_transform = transforms.Compose([Regularize() , ToTensor() ])
 data_torch = DigitDataset('train.csv', '.. /input/', train=True, transform=composed_transform)
 dataloader = DataLoader(data_torch,
batch_size=4,
shuffle=True,
num_workers=4)
 for i, data in enumerate(dataloader, 0):
digits, labels = data
print("Type of Digits: ", type(digits))
print("Dimension of the Tensor: ", digits.shape)
 print("Type of Labels: ", type(labels))
print("Dimension of the Tensor: ", labels.shape)
 if i == 0:
break<feature_engineering>
steps = count_data_items(filenames_test)/ \ 
(cfg['batch_size'] * strategy.num_replicas_in_sync)
 z = np.zeros(( cfg['batch_size'] * strategy.num_replicas_in_sync))
ds_testAug = getTestDataset(
 filenames_test, cfg, augment=True,
repeat=True ).map(lambda img, label:(img,(z, z, z)))
 probs = model.predict(ds_testAug, verbose=1, steps=steps * cfg['tta_steps'])
 probs = np.stack(probs)
 probs = probs[:, :count_data_items(filenames_test)* cfg['tta_steps']]
probs = np.stack(np.split(probs, cfg['tta_steps'], axis=1), axis=1)
 probs = np.mean(probs, axis=1)
 test = pd.read_csv(os.path.join(base_path, 'test.csv'))
y_test_sorted = np.zeros(( 3, probs.shape[1]))
test = test.reset_index()
test = test.set_index('image_name')
 i = 0
ds_test = getTestDataset(filenames_test, cfg)
 for img, imgid in tqdm(iter(ds_test.unbatch())) :
imgid = imgid.numpy().decode('utf-8')
 y_test_sorted[:, test.loc[imgid]['index']] = probs[:, i, 0]
i += 1
for i in range(y_test_sorted.shape[0]):
submission = sample
submission['target'] = y_test_sorted[i]
submission.to_csv('submission_model_%s.csv' % i, index=False)
 submission = sample
submission['target'] = np.mean(y_test_sorted, axis=0)
 submission.to_csv('blended_effnets.csv', index=False )
<count_values><EOS> 
effnet = pd.read_csv('./blended_effnets.csv')
 meta = pd.read_csv('./meta_simplified_img_data.csv')
 sample['target'] =(
 effnet['target'] * 0.9 +
meta['target'] * 0.1 
)
 sample.to_csv('ensembled.csv', header=True, index=False )