Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<count_missing_values>
!pip install -q efficientnet
!pip install -q git+https://github.com/AmedeoBiolatti/dsqol
train.select_dtypes(include='object' ).isnull().sum()<count_missing_values>
import os, re, time, tqdm
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import metrics, model_selection
import tensorflow as tf
import tensorflow_addons as tfa
from tensorflow import keras
from tensorflow.keras import backend as K
from efficientnet import tfkeras as efnet
from kaggle_datasets import KaggleDatasets
train.select_dtypes(exclude='object' ).isnull().sum()<drop_column>
from dsqol.tf import imgaug
from dsqol.tf.data import balance
from dsqol.tf.utils import average
from dsqol.tf import losses
train.drop(['Alley', 'PoolQC','Fence', 'MiscFeature'], axis=1, inplace=True)
 test.drop(['Alley', 'PoolQC','Fence', 'MiscFeature'], axis=1, inplace=True)
<data_type_conversions>
SEED = 42
tf.random.set_seed(SEED)
 np.random.seed(SEED)
 TIME_BUDGET = 2.99 * 3600
FOLDS = 5
INCLUDE_2019 = 0
INCLUDE_2018 = 1
INCLUDE_MALIGNANT = 1
IMG_READ_SIZE = 384
IMG_SIZE = 384
BALANCE_POS_RATIO = False
EFF_NET = 5
LOSS_TYPE = 'BCE'
LOSS_PARAMS = dict(label_smoothing=0.05)
 BATCH_SIZE = 32
EPOCHS = 14
TBM = 6
TTA = 20
VALID_FREQ = 1
N_SWA = 3
SWA_DECAY = 0.9
for col in('BsmtQual', 'BsmtCond', 'BsmtExposure','BsmtFinType1',
'BsmtFinType2', 'FireplaceQu', 'GarageType', 'GarageFinish',
'GarageQual', 'GarageCond', 'PavedDrive', 'Electrical','MasVnrType'):
train[col]=train[col].fillna('Unknown')
 test[col]=test[col].fillna('Unknown')
<count_missing_values>
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
GLOBAL_BATCH_SIZE = BATCH_SIZE * REPLICAS
print("REPLICAS: %d" % REPLICAS )
print(train.GarageType.isnull().sum())
 print(test.GarageType.isnull().sum() )<data_type_conversions>
GCS_PATH1 = KaggleDatasets().get_gcs_path('melanoma-%ix%i' %(IMG_READ_SIZE, IMG_READ_SIZE))
GCS_PATH2 = KaggleDatasets().get_gcs_path('isic2019-%ix%i' %(IMG_READ_SIZE, IMG_READ_SIZE))
GCS_PATH3 = KaggleDatasets().get_gcs_path('malignant-v2-%ix%i' %(IMG_READ_SIZE, IMG_READ_SIZE))
train['LotFrontage']=train['LotFrontage'].fillna(train.LotFrontage.mean())
 test['LotFrontage']= test['LotFrontage'].fillna(test.LotFrontage.mean())
 train['MasVnrArea']= train['MasVnrArea'].fillna(train.MasVnrArea.mean())
 test['MasVnrArea']= test['MasVnrArea'].fillna(test.MasVnrArea.mean())
 train['GarageYrBlt']= train['GarageYrBlt'].fillna(0)
 test['GarageYrBlt']= test['GarageYrBlt'].fillna(0)
 test['BsmtFinSF1']= test['BsmtFinSF1'].fillna(test.BsmtFinSF1.mean())
 test['BsmtFinSF2']= test['BsmtFinSF2'].fillna(test.BsmtFinSF2.mean())
 test['BsmtUnfSF']= test['BsmtUnfSF'].fillna(test.BsmtUnfSF.mean())
 test['BsmtFullBath']= test['BsmtFullBath'].fillna(test.BsmtFullBath.mean())
 test['BsmtHalfBath']= test['BsmtHalfBath'].fillna(test.BsmtHalfBath.mean())
 test['GarageCars']= test['GarageCars'].fillna(test.GarageCars.mean())
 test['TotalBsmtSF']= test['TotalBsmtSF'].fillna(test.TotalBsmtSF.mean())
 test['GarageArea'] = test['GarageArea'].fillna(test.GarageArea.mean())
<data_type_conversions>
df_base_train = pd.read_csv(".. /input/siim-isic-melanoma-classification/train.csv")
 df_base_test = pd.read_csv(".. /input/siim-isic-melanoma-classification/test.csv" )
test.columns[test.isnull().any() ].tolist() 
<filter>
train_files = tf.io.gfile.glob(os.path.join(GCS_PATH1, "train*.tfrec"))
if INCLUDE_2019:
train_files += tf.io.gfile.glob([os.path.join(GCS_PATH2, "train%.2i*.tfrec" % i)for i in range(1, 30, 2)])
 if INCLUDE_2018:
train_files += tf.io.gfile.glob([os.path.join(GCS_PATH2, "train%.2i*.tfrec" % i)for i in range(0, 30, 2)])
 if INCLUDE_MALIGNANT:
train_files += tf.io.gfile.glob([os.path.join(GCS_PATH3, "train%.2i*.tfrec" % i)for i in range(15, 30, 1)])
 print("%d train files found" % len(train_files))
train = train[train['GrLivArea']<4000]<define_variables>
test_files = tf.io.gfile.glob(os.path.join(GCS_PATH1, "test*.tfrec"))
print("%d test files found" % len(test_files))
features = ['MSSubClass', 'LotFrontage', 'LotArea', 'OverallQual',
'OverallCond', 'YearBuilt', 'YearRemodAdd', 'MasVnrArea', 'BsmtFinSF1',
'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', '1stFlrSF', '2ndFlrSF',
'LowQualFinSF', 'GrLivArea', 'BsmtFullBath', 'BsmtHalfBath', 'FullBath',
'HalfBath', 'BedroomAbvGr', 'KitchenAbvGr', 'TotRmsAbvGrd',
'Fireplaces', 'GarageYrBlt', 'GarageCars', 'GarageArea', 'WoodDeckSF',
'OpenPorchSF', 'EnclosedPorch', '3SsnPorch', 'ScreenPorch', 'PoolArea',
'MiscVal', 'MoSold', 'YrSold']
x_train, x_test, y_train, y_test = train_test_split(train[features],train['SalePrice'], train_size = 0.8,
test_size = 0.2, random_state=3)
 lm = LinearRegression()
lm.fit(x_train, y_train)
 print(lm.score(x_train, y_train))
print(lm.score(x_test, y_test))<train_model>
def read_labeled_tfrecord(example):
tfrec_format = {
'image' : tf.io.FixedLenFeature([], tf.string),
'image_name' : tf.io.FixedLenFeature([], tf.string),
'target' : tf.io.FixedLenFeature([], tf.int64)
 }
example = tf.io.parse_single_example(example, tfrec_format)
 return example['image'], example['target']
def read_unlabeled_tfrecord(example, return_image_name=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)
 return example['image'], example['image_name'] if return_image_name else 0
def prepare_image(img):
img = tf.image.decode_jpeg(img, channels=3)
 img = tf.cast(img, tf.float32)/ 255.0
return img
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1))
for filename in filenames]
return np.sum(n )
most_corr_features = ['OverallQual', 'YearBuilt', 'YearRemodAdd', 'MasVnrArea',
'BsmtFinSF1', 'TotalBsmtSF', 'GrLivArea',
'FullBath', 'Fireplaces', 'GarageCars']
steps = [ 
('scalar', StandardScaler()), 
('poly', PolynomialFeatures(degree=3)) , 
('model', Lasso(alpha=1000, fit_intercept=True))
]
pipeline = Pipeline(steps)
 x_train2, x_test2, y_train2, y_test2 = train_test_split(train[most_corr_features], train["SalePrice"], train_size=0.8, test_size=0.2, random_state=3)
 pipeline.fit(x_train2, y_train2)
 print(pipeline.score(x_train2, y_train2))
print(pipeline.score(x_test2, y_test2)) 
<concatenate>
def dropout(image, DIM=256, PROBABILITY = 0.75, CT = 8, SZ = 0.2):
P = tf.cast(tf.random.uniform([],0,1)<PROBABILITY, tf.int32)
 if(P==0)|(CT==0)|(SZ==0):
return image
for k in range(CT):
x = tf.cast(tf.random.uniform([],0,DIM),tf.int32)
 y = tf.cast(tf.random.uniform([],0,DIM),tf.int32)
 WIDTH = tf.cast(SZ*DIM,tf.int32)* P
ya = tf.math.maximum(0,y-WIDTH//2)
 yb = tf.math.minimum(DIM,y+WIDTH//2)
 xa = tf.math.maximum(0,x-WIDTH//2)
 xb = tf.math.minimum(DIM,x+WIDTH//2)
 one = image[ya:yb,0:xa,:]
two = tf.zeros([yb-ya,xb-xa,3])
 three = image[ya:yb,xb:DIM,:]
middle = tf.concat([one,two,three],axis=1)
 image = tf.concat([image[0:ya,:,:],middle,image[yb:DIM,:,:]],axis=0)
 image = tf.reshape(image,[DIM,DIM,3])
 return image
def ClasswiseMixup(probability: float=1.0, batch_size: int = 16, alpha: float=1.0, beta: float=1.0):
def classwise_mixup(img, tar):
do = tf.cast(tf.random.uniform([batch_size], 0, 1)< probability, tf.float32)
 gamma = tf.reshape(tf.random.uniform([batch_size], 0, 1)* do, [-1, 1, 1, 1])
 shuffled_img = tf.random.shuffle(img)
 img_new =(1 - gamma)* img + gamma * shuffled_img
return img_new
return classwise_mixup
len_train=train.shape[0]
houses=pd.concat([train,test], sort=False)
 for col in('MSZoning', 'Utilities', 'Exterior1st', 'Exterior2nd', 'KitchenQual', 'Functional', 'SaleType'):
houses[col]=houses[col].fillna('Unknown')
<data_type_conversions>
AUG_BS = 64
def base_aug(img):
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
dropout_aug = lambda img, o: dropout(img, DIM=IMG_READ_SIZE, PROBABILITY=0.75, CT=8, SZ=0.15)
 transform_aug = imgaug.Transform(dim=IMG_READ_SIZE, hzoom_mult=10.0, wzoom_mult=10.0)
 cw_mixup_aug = ClasswiseMixup(probability=0.1, batch_size=AUG_BS)
 def basic_augmentation_pipeline(ds: tf.data.Dataset, dim=None, batch_size=None)-> tf.data.Dataset:
ds = ds.map(lambda i, o:(transform_aug(i), o), num_parallel_calls=AUTO)
 ds = ds.map(lambda i, o:(base_aug(i), o), num_parallel_calls=AUTO)
 return ds
houses["MSSubClass"] = houses["MSSubClass"].apply(str)
 houses["MoSold"] = houses["MoSold"].apply(str)
<feature_engineering>
def get_dataset(files, augment=False, repeat=False, shuffle=False, labeled=True, batch_size=16, drop_remainder=False,
dim=256, read_dim=None 
)-> tf.data.Dataset:
if read_dim is None:
read_dim = dim
ds = tf.data.TFRecordDataset(files, num_parallel_reads=AUTO)
 ds = ds.cache()
if repeat:
ds = ds.repeat()
if shuffle:
ds = ds.shuffle(1024 * 8)
 opt = tf.data.Options()
opt.experimental_deterministic = False
ds = ds.with_options(opt)
 if labeled:
ds = ds.map(read_labeled_tfrecord, num_parallel_calls=AUTO)
 else:
ds = ds.map(read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 ds = ds.map(lambda i, o:(prepare_image(i), o), num_parallel_calls=AUTO)
 if augment:
ds = basic_augmentation_pipeline(ds, batch_size=8 * batch_size, dim=read_dim)
 if isinstance(augment, list):
for a in augment:
ds = ds.map(lambda i, o:(a(i, o), o), num_parallel_calls=AUTO)
 ds = ds.map(lambda i, o:(tf.image.resize(i, [dim, dim]), o), num_parallel_calls=AUTO)
 ds = ds.batch(batch_size, drop_remainder=drop_remainder)
 ds = ds.prefetch(AUTO)
 return ds
def get_balanced_dataset(files, augment=False, cw_augment=False, repeat=False, shuffle=False, batch_size=16, drop_remainder=False,
dim=256, read_dim=None, pos_ratio=False)-> tf.data.Dataset:
if read_dim is None:
read_dim = dim
ds = tf.data.TFRecordDataset(files, num_parallel_reads=AUTO)
 ds = ds.cache()
if repeat:
ds = ds.repeat()
ds = ds.map(read_labeled_tfrecord, num_parallel_calls=AUTO)
 ds0, ds1 = balance.separate_by_target(ds)
 ds0 = ds.map(lambda i, o:(prepare_image(i), o), num_parallel_calls=AUTO)
 ds1 = ds.map(lambda i, o:(prepare_image(i), o), num_parallel_calls=AUTO)
 if cw_augment:
ds0 = ds0.batch(AUG_BS, drop_remainder=drop_remainder); ds1 = ds1.batch(AUG_BS, drop_remainder=drop_remainder)
 for a in cw_augment:
ds0 = ds0.map(lambda i, o:(a(i, o), o), num_parallel_calls=AUTO)
 ds1 = ds1.map(lambda i, o:(a(i, o), o), num_parallel_calls=AUTO)
 ds0 = ds0.unbatch() ; ds1 = ds1.unbatch()
ds = balance.merge_ds(ds0, ds1, pos_ratio)
 if shuffle:
ds = ds.shuffle(1024)
 opt = tf.data.Options()
opt.experimental_deterministic = False
ds = ds.with_options(opt)
 if augment:
ds = basic_augmentation_pipeline(ds, batch_size=8 * batch_size, dim=read_dim)
 if isinstance(augment, list):
for a in augment:
ds = ds.map(lambda i, o:(a(i, o), o), num_parallel_calls=AUTO)
 ds = ds.map(lambda i, o:(tf.image.resize(i, [dim, dim]), o), num_parallel_calls=AUTO)
 ds = ds.batch(batch_size, drop_remainder=drop_remainder)
 ds = ds.prefetch(AUTO)
 return ds
houses["YrSold"] = houses["YrSold"].apply(str)
<prepare_x_and_y>
def show_dataset(thumb_size, cols, rows, ds):
mosaic = PIL.Image.new(mode='RGB', size=(thumb_size*cols +(cols-1),
thumb_size*rows +(rows-1)))
 for idx, data in enumerate(iter(ds)) :
img, target_or_imgid = data
ix = idx % cols
iy = idx // cols
img = np.clip(img.numpy() * 255, 0, 255 ).astype(np.uint8)
 img = PIL.Image.fromarray(img)
 img = img.resize(( thumb_size, thumb_size), resample=PIL.Image.BILINEAR)
 mosaic.paste(img,(ix*thumb_size + ix,
iy*thumb_size + iy))
display(mosaic)
 pass
houses.drop("GarageArea", axis=1, inplace=True)
 houses=pd.get_dummies(houses)
 train=houses[:len_train]
test=houses[len_train:]
train.drop('Id', axis=1, inplace=True)
 test.drop('Id', axis=1, inplace=True)
 train['SalePrice']=np.log(train['SalePrice'])*100
x=train.drop('SalePrice', axis=1)
 y=train['SalePrice']
test=test.drop('SalePrice', axis=1 )<train_model>
show_dataset(128, 8, 2, get_balanced_dataset(train_files, augment=[dropout_aug], cw_augment=[cw_mixup_aug] ).take(10 ).unbatch() )
steps = [ 
('poly', PolynomialFeatures(degree=1)) , 
('model', Lasso(alpha=0.15, fit_intercept=True))
]
pipeline = Pipeline(steps)
 x_train2, x_test2, y_train2, y_test2 = train_test_split(x, y, train_size=0.8, test_size=0.2, random_state=3)
 pipeline.fit(x_train2, y_train2)
 print(rmse(actual=y_test2/100, predicted=pipeline.predict(x_test2)/100))<predict_on_test>
def build_model(dim=128, ef=0):
inp = keras.layers.Input(shape=(dim,dim,3))
base = getattr(efnet, 'EfficientNetB%d' % ef )(input_shape=(dim, dim, 3), weights='imagenet', include_top=False)
 x = base(inp)
 x = keras.layers.GlobalAveragePooling2D()(x)
 x = keras.layers.Dense(1 )(x)
 x = keras.layers.Activation('sigmoid', dtype='float32' )(x)
 model = keras.Model(inputs=inp,outputs=x)
 opt = keras.optimizers.Adam(learning_rate=1e-3)
 if LOSS_TYPE.upper() == 'BCE':
loss = keras.losses.BinaryCrossentropy(**LOSS_PARAMS)
 elif LOSS_TYPE.upper() == 'FOCAL':
loss = losses.BinaryFocalLoss(**LOSS_PARAMS)
 model.compile(optimizer=opt, loss=loss, metrics=['AUC'])
 return model
predictions1 = pipeline.predict(test )<train_model>
mult = 1
lr_start = 5e-6
lr_max = 1.25e-6 * GLOBAL_BATCH_SIZE
lr_min = 1e-6
lr_ramp_ep = 5
lr_sus_ep = 0
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 * mult
plt.figure(figsize=(10, 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');
params = {
'n_estimators': 300,
'max_depth': 3,
'learning_rate': 0.1,
'criterion': 'mae',
'min_impurity_decrease': 0.005,
'loss': 'huber',
'alpha': 0.99
}
gradient_boosting_regressor = GradientBoostingRegressor(**params)
 gradient_boosting_regressor.fit(x_train2, y_train2)
 print(rmse(actual=y_test2/100, predicted=gradient_boosting_regressor.predict(x_test2)/100))<save_to_csv>
CKPT_FOLDER = ".. /working/ckpt"
if not os.path.exists(CKPT_FOLDER):
os.mkdir(CKPT_FOLDER)
 folds = list(model_selection.KFold(n_splits=FOLDS, shuffle=True, random_state=SEED ).split(np.arange(15)))
 testiness = pd.read_csv(".. /input/spicv-spicy-vi-make-your-cv-more-testy/testiness.csv")
 TOTAL_POS = 581 + 2858 * INCLUDE_2019 + 1651 * INCLUDE_2018 + 580 * INCLUDE_MALIGNANT
predictions2 = gradient_boosting_regressor.predict(test)
 predictions = np.exp(( predictions1 + predictions2)/200)
 my_sub = pd.DataFrame({'Id':test2.Id, 'SalePrice':predictions})
 my_sub.to_csv('submission.csv', index = False)
<set_options>
VERBOSE = 1
PLOT = 1
histories = []
df_oof = pd.DataFrame() ; df_res = pd.DataFrame()
t_start = time.time()
for fold,(idTrain, idValid)in enumerate(folds):
print("
print(( "
print("
if DEVICE == 'TPU':
if tpu:
tf.tpu.experimental.initialize_tpu_system(tpu)
 fold_valid_files = [f for f in train_files if any([int(re.match("^train([0-9]+)", f.split("/")[-1] ).group(1)) % 15 == i for i in idValid])]
fold_valid_files = [f for f in fold_valid_files if GCS_PATH1 in f]
fold_train_files = [f for f in train_files if f not in fold_valid_files]
np.random.shuffle(fold_train_files)
 print("Train files: %d\t\t Valid files: %d" %(len(fold_train_files), len(fold_valid_files)))
 K.clear_session()
if DEVICE == 'TPU':
keras.mixed_precision.experimental.set_policy('mixed_bfloat16')
 with strategy.scope() :
model = build_model(dim=IMG_SIZE, ef=EFF_NET)
 FOLD_CKPT_FOLDER = os.path.join(CKPT_FOLDER, "fold%d" % fold)
 if not os.path.exists(FOLD_CKPT_FOLDER):
os.mkdir(FOLD_CKPT_FOLDER)
 callbacks =[
keras.callbacks.ModelCheckpoint(os.path.join(FOLD_CKPT_FOLDER, "model_fold%d_e{epoch:02d}.h5" % fold), save_weights_only=True,
monitor='val_acc',
mode='min',
save_best_only=True),
keras.callbacks.LearningRateScheduler(lrfn)
 ]
if BALANCE_POS_RATIO:
print("Using balanced dataset with pos_ratio = %d%%" % int(100 * BALANCE_POS_RATIO))
ds_train = get_balanced_dataset(fold_train_files, repeat=True, augment=[dropout_aug], drop_remainder=True, shuffle=True,
pos_ratio=BALANCE_POS_RATIO,
dim=IMG_SIZE, read_dim=IMG_READ_SIZE, batch_size=GLOBAL_BATCH_SIZE)
 FOLD_POS = TOTAL_POS *(FOLDS - 1)/ FOLDS
STEPS = int(FOLD_POS / BALANCE_POS_RATIO / GLOBAL_BATCH_SIZE)
 else:
print("Using unbalanced dataset")
 ds_train = get_dataset(fold_train_files, repeat=True, augment=[dropout_aug], drop_remainder=True, shuffle=True,
dim=IMG_SIZE, read_dim=IMG_READ_SIZE, batch_size=GLOBAL_BATCH_SIZE)
 STEPS = int(count_data_items(fold_train_files)/ GLOBAL_BATCH_SIZE)
 ds_valid = get_dataset(fold_valid_files, repeat=False, augment=False, drop_remainder=False, shuffle=False,
dim=IMG_SIZE, read_dim=IMG_READ_SIZE, batch_size=GLOBAL_BATCH_SIZE * TBM)
 print("Training...")
 history = model.fit(
 ds_train,
validation_data = ds_valid,
epochs = EPOCHS,
steps_per_epoch = STEPS,
verbose = VERBOSE,
callbacks = callbacks,
validation_freq = VALID_FREQ 
)
 histories.append(history)
 ckpt_files = np.sort(tf.io.gfile.glob(os.path.join(FOLD_CKPT_FOLDER, "*.h5")))
 ckpt_files_fow_swa = ckpt_files[-N_SWA:]
if len(ckpt_files_fow_swa)> 1:
with strategy.scope() :
model = average.average_weights(ckpt_files_fow_swa, decay=SWA_DECAY, model=model)
 for f in ckpt_files:
os.remove(f)
 model.save(os.path.join(CKPT_FOLDER, "model_fold%d.h5" % fold))
ds_valid = get_dataset(fold_valid_files, augment=TTA >= 1, repeat=True, dim=IMG_SIZE, read_dim=IMG_READ_SIZE, batch_size=GLOBAL_BATCH_SIZE * TBM, drop_remainder=True)
 ct_valid = count_data_items(fold_valid_files); STEPS = int(np.ceil(TTA * ct_valid / GLOBAL_BATCH_SIZE / TBM))
fold_valid_pred = model.predict(ds_valid, steps=STEPS, verbose=1)
 fold_valid_pred = fold_valid_pred[:ct_valid * TTA,]
ds_valid = get_dataset(fold_valid_files, augment=False, repeat=False, dim=IMG_SIZE, batch_size=GLOBAL_BATCH_SIZE * TBM, drop_remainder=False, labeled=False)
 fold_valid_names = np.concatenate([np.array([ni.decode("utf-8")for ni in n.numpy() ])for n in ds_valid.map(lambda i, n: n)], 0)
 fold_df = pd.DataFrame({'image_name': np.tile(fold_valid_names, [TTA]), 'pred': fold_valid_pred.squeeze() , 'fold': fold})
 df_oof = pd.concat([df_oof, fold_df])
 fold_df['image_name'] = fold_df['image_name'].str.replace('_downsampled', '')
 fold_df = fold_df.groupby('image_name' ).mean().reset_index()
fold_df = fold_df.merge(df_base_train[['image_name', 'patient_id', 'target']], on='image_name' ).merge(testiness, on='image_name')
 fold_df['fold'] = fold
auc = metrics.roc_auc_score(fold_df.target, fold_df.pred)
 ds_test = get_dataset(test_files, augment=TTA >= 1, repeat=True, dim=IMG_SIZE, read_dim=IMG_READ_SIZE, batch_size=GLOBAL_BATCH_SIZE * TBM, drop_remainder=True, labeled=False)
 ct_test = count_data_items(test_files); STEPS = int(np.ceil(TTA * ct_test / GLOBAL_BATCH_SIZE / TBM))
fold_test_pred = model.predict(ds_test.map(lambda i, l: i), steps=STEPS, verbose=1)
 fold_test_pred = fold_test_pred[:ct_test * TTA,]
ds_test = get_dataset(test_files, augment=False, repeat=False, dim=IMG_SIZE, batch_size=GLOBAL_BATCH_SIZE * TBM, drop_remainder=False, labeled=False)
 fold_test_names = np.concatenate([np.array([ni.decode("utf-8")for ni in n.numpy() ])for n in ds_test.map(lambda i, n: n)], 0)
 fold_res = pd.DataFrame({'image_name': np.tile(fold_test_names, [TTA]), 'pred': fold_test_pred.squeeze() , 'fold': fold})
 df_res = pd.concat([df_res, fold_res])
 used_time_till_now = time.time() - t_start
time_per_fold = used_time_till_now /(fold + 1)
 print("Validation AUC last epoch = %.4f" % history.history['val_auc'][-1])
 print("Validation AUC(TTA %2d)= %.4f" %(TTA, auc))
print("Total time = %ds\t\tTime per fold = %ds" %(int(used_time_till_now), int(time_per_fold)))
 if PLOT:
plt.figure(figsize=(16, 4))
plt.subplot(1, 2, 1)
 plt.plot(history.history['loss'], color='tab:blue', marker='o')
 plt.plot(range(VALID_FREQ - 1, EPOCHS, VALID_FREQ), history.history['val_loss'], color='tab:blue', marker='x', linestyle=':')
 plt.yscale('log')
 plt.subplot(1, 2, 2)
 plt.plot(history.history['auc'], color='tab:red', marker='o')
 plt.plot(range(VALID_FREQ - 1, EPOCHS, VALID_FREQ), history.history['val_auc'], color='tab:red', marker='x', linestyle=':')
 plt.show()
del model, ds_train, ds_valid, ds_test
print("
")
 if(fold + 1)< FOLDS:
time_till_next_fold = used_time_till_now + time_per_fold
if time_till_next_fold > TIME_BUDGET:
print("Forecasted total time till next fold completion = %d(budget = %d)" %(time_till_next_fold, TIME_BUDGET))
break
pass
warnings.filterwarnings('ignore' )<load_from_csv>
xxx = df_oof.groupby('image_name' ).mean().reset_index().merge(df_base_train, on='image_name')
 print("OOF AUC(TTA %d)= %.4f" %(TTA, metrics.roc_auc_score(xxx.target, xxx.pred)) )
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 print(train_df.shape)
 print(test_df.shape )<count_missing_values>
df_res.to_csv('.. /working/test_res_all.csv', index=False)
 df_oof.to_csv('.. /working/oof_res_all.csv', index=False )
<train_model><EOS> 
df_res[['image_name', 'pred']].groupby('image_name' ).mean().reset_index().rename({'pred': 'target'}, axis=1 ).to_csv("submission.csv", index=False )