Kaggle competition: SIIM-ISIC Melanoma Classification
<SOS> metric: MCAUC Kaggle data source: siim-isic-melanoma-classification<load_from_csv>
colab=0
show_files=0
tstamp=0
digit_recon_tran_csv = pd.read_csv('/kaggle/input/digit-recognizer/train.csv',dtype = np.float32)
 digit_recon_test_csv = pd.read_csv('/kaggle/input/digit-recognizer/test.csv',dtype = np.float32 )<train_model>
if colab:
drive.mount('/content/gdrive' )
print('tran dataset size: ',digit_recon_tran_csv.size,'
')
 print('test dataset size: ',digit_recon_test_csv.size,'
' )<categorify>
!pip install -q efficientnet
tran_label = digit_recon_tran_csv.label.values
tran_image = digit_recon_tran_csv.loc[:,digit_recon_tran_csv.columns != "label"].values/255
test_image = digit_recon_test_csv.values/255<split>
print("Tensorflow version " + tf.__version__)
 AUTO = tf.data.experimental.AUTOTUNE
if not colab:
train_image, valid_image, train_label, valid_label = train_test_split(tran_image,
tran_label,
test_size = 0.2,
random_state = 42 )<normalization>
def seed_everything(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 tf.random.set_seed(seed )
print(torch.__version__)
 class MNIST_data(Dataset):

 def __init__(self,
data,
transform = transforms.Compose([transforms.ToPILImage() ,
transforms.RandomAffine(30,(0.1,0.1)) ,
transforms.ToTensor()
])
):
if len(data)== 1:
self.X = data[0].reshape(-1,28,28)
 self.y = None
else:
self.X = data[0].reshape(-1,28,28)
 self.y = data[1].astype(np.long)
 self.transform = transform
def __len__(self):
return len(self.X)
 def __getitem__(self, idx):
if self.y is not None:
return self.transform(self.X[idx]), self.y[idx]
else:
return self.transform(self.X[idx] )<load_pretrained>
NAME='EffNB0_512'
NFOLDS=5
NBEST=2
SEED=311
n_max=7
if colab:
PATH=Path('/content/gdrive/My Drive/kaggle/input/siim-isic-melanoma-classification/')
 train=pd.read_csv(PATH/'train.csv.zip')
 else:
PATH=Path('/kaggle/input/siim-isic-melanoma-classification/')
 train=pd.read_csv(PATH/'train.csv')
 test=pd.read_csv(PATH/'test.csv')
 sub=pd.read_csv(PATH/'sample_submission.csv')
 seed_everything(SEED )
batch_size = 64
train_dataset = MNIST_data(( train_image,train_label))
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size, shuffle=True)
 valid_dataset = MNIST_data(( valid_image,valid_label))
valid_loader = torch.utils.data.DataLoader(dataset=valid_dataset,
batch_size=batch_size, shuffle=False )<choose_model_class>
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 )
class YANNet(nn.Module):
def __init__(self):
super(YANNet,self ).__init__()
self.conv = nn.Sequential(
 nn.Conv2d(1,8,3,1,1),
nn.BatchNorm2d(8),
nn.ReLU() ,
nn.Conv2d(8,16,3,1,1),
nn.BatchNorm2d(16),
nn.ReLU() ,
nn.MaxPool2d(2),
nn.Conv2d(16,16,3,1,1),
nn.BatchNorm2d(16),
nn.ReLU() ,
nn.Conv2d(16,8,3,1,1),
nn.BatchNorm2d(8),
nn.ReLU() ,
nn.MaxPool2d(2)
)
 self.fc = nn.Sequential(
 nn.Linear(8*7*7,256),
nn.BatchNorm1d(256),
nn.ReLU() ,
nn.Dropout(0.5),
nn.Linear(256,256),
nn.BatchNorm1d(256),
nn.ReLU() ,
nn.Dropout(0.5),
nn.Linear(256,10)
)
 def forward(self, img):
x = self.conv(img)
 o = self.fc(x.view(x.shape[0],-1))
return o<choose_model_class>
GCS_PATH={}
if colab:
GCS_PATH['train']='gs://kds-4169ce1e5a624d4b5bd866480682658f2063b18c5cf995cd3316a7e9'
GCS_PATH['test']='gs://kds-147ff1400c195e2f8c7b01492e04fd2769e0305e9ef6c01afc9c5870'
GCS_PATH['hairs']='gs://kds-9e567a6e6d1255950cf43b26db187228f633ac7ec6bb6dcf70c1b511'
else:
GCS_PATH['train']=KaggleDatasets().get_gcs_path('siim-tfrec-cc-512-train')
 GCS_PATH['test']=KaggleDatasets().get_gcs_path('siim-tfrec-cc-512-test')
 GCS_PATH['hairs']=KaggleDatasets().get_gcs_path('melanoma-hairs')
 print(GCS_PATH['train'])
 print(GCS_PATH['test'])
 print(GCS_PATH['hairs'] )
model = YANNet()
error = nn.CrossEntropyLoss()
if torch.cuda.is_available() :
model = model.cuda()
error = error.cuda()
optimizer = torch.optim.SGD(model.parameters() , lr=0.1)
 scheduler = lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.1 )<train_model>
%%time
IMAGE_SIZE = [512, 512]
EPOCHS=10
BATCH_SIZE = 8 * strategy.num_replicas_in_sync
CLASSES = ['benign', 'malignant']
num_epoc = 120
for epoch in range(num_epoc):
epoc_train_loss = 0.0
epoc_train_corr = 0.0
epoc_valid_corr = 0.0
print('Epoch:{}/{}'.format(epoch,num_epoc))
model.train()
scheduler.step()
for batch_idx,(images, labels)in enumerate(train_loader):
if torch.cuda.is_available() :
images = images.cuda()
labels = labels.cuda()
images = Variable(images)
 labels = Variable(labels)
 outputs = model(images)
 optimizer.zero_grad()
loss = error(outputs,labels)
 loss.backward()
optimizer.step()
epoc_train_loss += loss.data
outputs = torch.max(outputs.data,1)[1]
epoc_train_corr += torch.sum(outputs==labels.data)
 with torch.no_grad() :
model.eval()
for batch_idx,(images, labels)in enumerate(valid_loader):
if torch.cuda.is_available() :
images = images.cuda()
labels = labels.cuda()
images = Variable(images)
 labels = Variable(labels)
 outputs = model(images)
 outputs = torch.max(outputs.data,1)[1]
epoc_valid_corr += torch.sum(outputs==labels.data)
 print("loss is :{:.4f},Train Accuracy is:{:.4f}%,Test Accuracy is:{:.4f}%".format(epoc_train_loss/len(train_dataset),100*epoc_train_corr/len(train_dataset),100*epoc_valid_corr/len(valid_dataset)) )<find_best_params>
ALL_TRAIN=tf.io.gfile.glob(GCS_PATH['train'] + '/*.tfrec')
 VAL_FNAMES={}
for fn in range(1, NFOLDS+1):
VAL_FNAMES[f"fold_{fn}"]=[path for path in ALL_TRAIN if f"fold_{fn}" in path]
print("Fold", f'{fn}:', len(VAL_FNAMES[f'fold_{fn}']), "elements in total.")
 TRAIN_FNAMES={f'fold_{i}': list(set(ALL_TRAIN)-set(VAL_FNAMES[f'fold_{i}']))
for i in range(1, NFOLDS+1)}
TEST_FNAMES = tf.io.gfile.glob(GCS_PATH['test'] + '/*.tfrec')
 hair_images=tf.io.gfile.glob(GCS_PATH['hairs'] + '/*.png' )
model = model.cpu()
model.eval()<load_from_csv>
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n )
digit_recon_submission_csv = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv',dtype = np.float32)
 print(digit_recon_submission_csv.head(10))<categorify>
%%time
N_TRAIN_IMGS = {f'fold_{i}': count_data_items(TRAIN_FNAMES[f'fold_{i}'])
 for i in range(1, NFOLDS+1)}
N_VAL_IMGS = {f'fold_{i}': count_data_items(VAL_FNAMES[f'fold_{i}'])
 for i in range(1, NFOLDS+1)}
N_TEST_IMGS = count_data_items(TEST_FNAMES)
 STEPS_PER_EPOCH = {f'fold_{i}': N_TRAIN_IMGS[f'fold_{i}'] // BATCH_SIZE
for i in range(1, NFOLDS+1)}
print("="*75)
 print(f"The number of unlabeled test image is {N_TEST_IMGS}.It is common for all folds.")
 for i in range(1, NFOLDS+1):
print("="*75)
 print(f"Fold {i}: {N_TRAIN_IMGS[f'fold_{i}']} training and {N_VAL_IMGS[f'fold_{i}']} validation images.")
 print("="*75 )
for i in range(test_image.shape[0]):
one_image = torch.from_numpy(test_image[i] ).view(1,1,28,28)
 one_output = model(one_image)
 test_results[i,0] = i+1
test_results[i,1] = torch.max(one_output.data,1)[1].numpy() 
<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
Data = {'ImageId': test_results[:, 0], 'Label': test_results[:, 1]}
DataFrame = pd.DataFrame(Data)
 DataFrame.to_csv('submission.csv', index=False, sep=',' )<import_modules>
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.string),
"benign_malignant": tf.io.FixedLenFeature([], tf.string),
"age": tf.io.FixedLenFeature([], tf.int64),
"sex_female": tf.io.FixedLenFeature([], tf.int64),
"sex_male": tf.io.FixedLenFeature([], tf.int64),
"sex_unknown": tf.io.FixedLenFeature([], tf.int64),
"site_head/neck": tf.io.FixedLenFeature([], tf.int64),
"site_lower extremity": tf.io.FixedLenFeature([], tf.int64),
"site_oral/genital": tf.io.FixedLenFeature([], tf.int64),
"site_palms/soles": tf.io.FixedLenFeature([], tf.int64),
"site_torso": tf.io.FixedLenFeature([], tf.int64),
"site_unknown": tf.io.FixedLenFeature([], tf.int64),
"site_upper extremity": tf.io.FixedLenFeature([], tf.int64),
"height": tf.io.FixedLenFeature([], tf.int64),
"width": tf.io.FixedLenFeature([], tf.int64),
"target": tf.io.FixedLenFeature([], tf.int64),
"age_scaled": tf.io.FixedLenFeature([], tf.float32),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 data={}
data['image_name']=image_name=tf.cast(example['image_name'], tf.string)
 data['patient_id']=tf.cast(example['patient_id'], tf.string)
 data['age']=tf.cast(example['age'], tf.int32)
 data['sex_female']=tf.cast(example['sex_female'], tf.int32)
 data['sex_male']=tf.cast(example['sex_male'], tf.int32)
 data['sex_unknown']=tf.cast(example['sex_unknown'], tf.int32)
 data['site_head/neck']=tf.cast(example['site_head/neck'], tf.int32)
 data['site_lower extremity']=tf.cast(example['site_lower extremity'], tf.int32)
 data['site_oral/genital']=tf.cast(example['site_oral/genital'], tf.int32)
 data['site_palms/soles']=tf.cast(example['site_palms/soles'], tf.int32)
 data['site_torso']=tf.cast(example['site_torso'], tf.int32)
 data['site_unknown']=tf.cast(example['site_unknown'], tf.int32)
 data['site_upper extremity']=tf.cast(example['site_upper extremity'], tf.int32)
 data['age_scaled']=tf.cast(example['age_scaled'], tf.float32)
 label=tf.cast(example['target'], tf.int32)
 label_name=tf.cast(example['benign_malignant'], tf.string)
 return image, label, data, label_name
print(os.listdir(".. /input"))
seed = 4529
np.random.seed(seed )<load_from_csv>
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.string),
"age": tf.io.FixedLenFeature([], tf.int64),
"sex_female": tf.io.FixedLenFeature([], tf.int64),
"sex_male": tf.io.FixedLenFeature([], tf.int64),
"sex_unknown": tf.io.FixedLenFeature([], tf.int64),
"site_head/neck": tf.io.FixedLenFeature([], tf.int64),
"site_lower extremity": tf.io.FixedLenFeature([], tf.int64),
"site_oral/genital": tf.io.FixedLenFeature([], tf.int64),
"site_palms/soles": tf.io.FixedLenFeature([], tf.int64),
"site_torso": tf.io.FixedLenFeature([], tf.int64),
"site_unknown": tf.io.FixedLenFeature([], tf.int64),
"site_upper extremity": tf.io.FixedLenFeature([], tf.int64),
"height": tf.io.FixedLenFeature([], tf.int64),
"width": tf.io.FixedLenFeature([], tf.int64),
"age_scaled": tf.io.FixedLenFeature([], tf.float32),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 data={}
data['image_name']=image_name=tf.cast(example['image_name'], tf.string)
 data['patient_id']=tf.cast(example['patient_id'], tf.string)
 data['age']=tf.cast(example['age'], tf.int32)
 data['sex_female']=tf.cast(example['sex_female'], tf.int32)
 data['sex_male']=tf.cast(example['sex_male'], tf.int32)
 data['sex_unknown']=tf.cast(example['sex_unknown'], tf.int32)
 data['site_head/neck']=tf.cast(example['site_head/neck'], tf.int32)
 data['site_lower extremity']=tf.cast(example['site_lower extremity'], tf.int32)
 data['site_oral/genital']=tf.cast(example['site_oral/genital'], tf.int32)
 data['site_palms/soles']=tf.cast(example['site_palms/soles'], tf.int32)
 data['site_torso']=tf.cast(example['site_torso'], tf.int32)
 data['site_unknown']=tf.cast(example['site_unknown'], tf.int32)
 data['site_upper extremity']=tf.cast(example['site_upper extremity'], tf.int32)
 data['age_scaled']=tf.cast(example['age_scaled'], tf.float32)
 return image, data
base_dir = os.path.join(".. ", "input")
 train_df = pd.read_csv(os.path.join(base_dir, "train.csv"))
test_df = pd.read_csv(os.path.join(base_dir, "test.csv"))
len(train_df )<set_options>
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
%load_ext tensorboard.notebook
%tensorboard --logdir logs<prepare_x_and_y>
%%time
training_dataset = load_dataset(TRAIN_FNAMES['fold_2'])
 print("Example of the training data:")
 for image, label, data, label_name in training_dataset.take(1):
print("The image batch size:", image.numpy().shape)
 print("Label:", label.numpy())
 print("Label name:", label_name.numpy())
 print("Age:", data['age'].numpy())
 print("Age(scaled):", data['age_scaled'].numpy() )
x = train_df.drop(['label'], axis=1 ).values
y = train_df['label'].values
test_x = test_df.values<split>
%%time
validation_dataset = load_dataset(VAL_FNAMES['fold_2'])
 print("Examples of the validation data:")
 for image, label, data, label_name in validation_dataset.take(1):
print("The image batch size:", image.numpy().shape)
 print("Label:", label.numpy())
 print("Label name:", label_name.numpy())
 print("Age:", data['age'].numpy())
 print("Age(scaled):", data['age_scaled'].numpy() )
x = x.reshape(-1, 28, 28, 1)
 x = x / 255.0
test_x = test_x.reshape(-1, 28, 28, 1)
 test_x = test_x / 255.0
y = to_categorical(y, num_classes=10)
 x_train, x_val, y_train, y_val = train_test_split(x, y, test_size = 0.1, random_state=seed )<choose_model_class>
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FNAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
model = Sequential([
Conv2D(128,(3,3), activation="relu", input_shape=(28, 28, 1)) ,
BatchNormalization() ,
Conv2D(128,(3,3), activation="relu"),
BatchNormalization() ,
MaxPooling2D(2,2),
Dropout(0.2),
Conv2D(64,(3,3), activation="relu"),
BatchNormalization() ,
Conv2D(64,(3,3), activation="relu"),
BatchNormalization() ,
MaxPooling2D(2,2),
Dropout(0.2),
Flatten() ,
Dense(units=256, activation='relu'),
Dropout(0.4),
Dense(units=256, activation='relu'),
Dropout(0.4),
Dense(units=10, activation='softmax')
 ])
 model.compile(optimizer=Adam(lr=0.001),
loss='categorical_crossentropy',
metrics=['acc'])
 model.summary()<train_model>
%%time
test_dataset = get_test_dataset()
print("Examples of the test data:")
 for image, data in test_dataset.take(1):
print("The image batch size:", image.numpy().shape)
 print("Ages, 5 examples:", data['age'].numpy() [:5])
 print("Age(scaled), 5 examples:", data['age_scaled'].numpy() [:5])
batch_size = 128
epochs = 30
datagen = ImageDataGenerator(rotation_range=15,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.15,
shear_range=0.15)
 datagen.fit(x_train)
 history = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),
validation_data=(x_val, y_val),
steps_per_epoch = x_train.shape[0] // batch_size,
epochs=epochs, callbacks=callbacks)
<save_to_csv>
LR_START = 0.000005
LR_MAX = 0.00000725 * strategy.num_replicas_in_sync
LR_MIN = 0.000005
LR_RAMPUP_EPOCHS = 5
LR_SUSTAIN_EPOCHS = 4
LR_EXP_DECAY =.8
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
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose = True)
 rng = [i for i in range(EPOCHS)]
y = [lrfn(x)for x in rng]
plt.plot(rng, y)
 print("Learning rate schedule: {:.3g} to {:.3g} to {:.3g}".format(y[0], max(y), y[-1]))
pred = model.predict(test_x)
 pred = np.argmax(pred, axis=1)
 pred = pd.Series(pred, name="Label")
 test_df = pd.concat([pd.Series(range(1,28001), name = "ImageId"), pred],axis = 1)
 test_df.to_csv('mnist-submission.csv', index = False )<set_options>
train['target'].values
class_weights = class_weight.compute_class_weight(class_weight='balanced',
classes=np.unique(train['target'].values),
y=train['target'].values, 
)
 class_weights = {i : class_weights[i] for i in range(len(class_weights)) }
print(class_weights )
%matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )<load_from_csv>
tab_feats=['age_scaled',
'sex_female',
'sex_male',
'sex_unknown',
'site_head/neck',
'site_lower extremity',
'site_oral/genital',
'site_palms/soles',
'site_torso',
'site_unknown',
'site_upper extremity',
]
N_TAB_FEATS=len(tab_feats)
 print(f"The number of tabular features is {N_TAB_FEATS}." )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<feature_engineering>
%time
def get_model() :
with strategy.scope() :
pretrained_model = efn.EfficientNetB0(input_shape=(*IMAGE_SIZE, 3),
weights='imagenet',
include_top=False 
)
 pretrained_model.trainable = True
inp1 = tf.keras.layers.Input(shape=(*IMAGE_SIZE, 3), name='inp1')
 inp2 = tf.keras.layers.Input(shape=(N_TAB_FEATS), name='inp2')
 x=pretrained_model(inp1)
 x=tf.keras.layers.GlobalAveragePooling2D()(x)
 x=tf.keras.layers.Dense(512,
kernel_regularizer=tf.keras.regularizers.l2(l=0.01),
activation='relu' )(x)
 x=tf.keras.layers.Dropout(0.2 )(x)
 x=tf.keras.layers.Dense(256,
kernel_regularizer=tf.keras.regularizers.l2(l=0.01),
activation='relu' )(x)
 x=tf.keras.layers.Dropout(0.2 )(x)
 x=tf.keras.layers.Dense(128,
kernel_regularizer=tf.keras.regularizers.l2(l=0.01),
activation='relu' )(x)
 x=tf.keras.layers.Dropout(0.2 )(x)
 x=tf.keras.layers.Dense(64, kernel_regularizer=tf.keras.regularizers.l2(l=0.01),
activation='relu' )(x)
 x=tf.keras.layers.Dropout(0.2 )(x)
 y=tf.keras.layers.Dense(100,
kernel_regularizer=tf.keras.regularizers.l2(l=0.01),
activation='relu' )(inp2)
 concat=tf.keras.layers.concatenate([y, x])
 output = tf.keras.layers.Dense(1, activation='sigmoid', name='output' )(concat)
 model = tf.keras.models.Model(inputs=[inp1,inp2], outputs=[output])
 model.compile(
 optimizer='adam',
loss = 'binary_crossentropy',
metrics=[tf.keras.metrics.AUC() ], 
)
 return model
X_train = X_train / 255.0
test = test / 255.0<categorify>
del model
gc.collect()
Y_train = to_categorical(Y_train, num_classes = 10 )<define_variables>
if colab:
SAVE_FOLDER=NAME
if tstamp:
time_zone = pytz.timezone('America/Chicago')
 current_datetime = datetime.now(time_zone)
 ts=current_datetime.strftime("%m%d%H%M%S")
 SAVE_FOLDER+='_'+ts
SAVE_FOLDER=PATH/SAVE_FOLDER
if not os.path.exists(SAVE_FOLDER):
os.mkdir(SAVE_FOLDER)
 else:
SAVE_FOLDER=Path('/kaggle/working' )
random_seed = 2<split>
class save_best_n(tf.keras.callbacks.Callback):
def __init__(self, fn, model):
self.fn = fn
self.model = model
def on_epoch_end(self, epoch, logs=None):
if(epoch>0):
score=logs.get("val_auc")
 else:
score=-1
if(score > best_score[fold_num].min()):
idx_min=np.argmin(best_score[fold_num])
 best_score[fold_num][idx_min]=score
best_epoch[fold_num][idx_min]=epoch+1
path_best_model=f'best_model_fold_{self.fn}_{idx_min}.hdf5'
self.model.save(SAVE_FOLDER/path_best_model)
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.05, random_state=random_seed )<choose_model_class>
def setup_input(image, label, data, label_name):
tab_data=[tf.cast(data[tfeat], dtype=tf.float32)for tfeat in tab_feats]
tabular=tf.stack(tab_data)
 return {'inp1': image, 'inp2': tabular}, label
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(Dropout(0.5))
model.add(Dense(10, activation = "softmax")) 
<choose_model_class>
hair_images_tf=tf.convert_to_tensor(hair_images)
 scale=tf.cast(IMAGE_SIZE[0]/256, dtype=tf.int32 )
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )<choose_model_class>
def hair_aug(data, label):
img=tf.identity(data['inp1'])
 n_hairs = tf.random.uniform(shape=[], maxval=tf.constant(n_max)+1,
dtype=tf.int32)
 im_height=tf.shape(img)[1]
im_width=tf.shape(img)[0]
if n_hairs == 0:
return data, label
for _ in tf.range(n_hairs):
i=tf.random.uniform(shape=[], maxval=tf.shape(hair_images_tf)[0],
dtype=tf.int32)
 fname=hair_images_tf[i]
bits = tf.io.read_file(fname)
 hair = tf.image.decode_jpeg(bits)
 new_width=scale*tf.shape(hair)[1]
new_height=scale*tf.shape(hair)[0]
hair = tf.image.resize(hair, [new_height, new_width])
 hair = tf.image.random_flip_left_right(hair)
 hair = tf.image.random_flip_up_down(hair)
 n_rot=tf.random.uniform(shape=[], maxval=4,
dtype=tf.int32)
 hair = tf.image.rot90(hair, k=n_rot)
 h_height=tf.shape(hair)[0]
h_width=tf.shape(hair)[1]
roi_h0 = tf.random.uniform(shape=[], maxval=im_height - h_height + 1,
dtype=tf.int32)
 roi_w0 = tf.random.uniform(shape=[], maxval=im_width - h_width + 1,
dtype=tf.int32)
 roi = img[roi_h0:(roi_h0 + h_height), roi_w0:(roi_w0 + h_width)]
hair2gray = tf.image.rgb_to_grayscale(hair[:, :, :3])
 mask=hair2gray>10
img_bg = tf.multiply(roi, tf.cast(tf.image.grayscale_to_rgb(~mask),
dtype=tf.float32))
hair_fg = tf.multiply(tf.cast(hair[:, :, :3], dtype=tf.int32),
tf.cast(tf.image.grayscale_to_rgb(mask),
dtype=tf.int32 
)
)
 dst = tf.add(img_bg, tf.cast(hair_fg, dtype=tf.float32)/255)
 paddings = tf.stack([
[roi_h0, im_height-(roi_h0 + h_height)],
[roi_w0, im_width-(roi_w0 + h_width)],
[0, 0]
])
 dst_padded=tf.pad(dst, paddings, "CONSTANT")
 mask_img=tf.pad(tf.ones_like(dst), paddings, "CONSTANT")
 mask_img=~tf.cast(mask_img, dtype=tf.bool)
 img_hole=tf.multiply(img, tf.cast(mask_img, dtype=tf.float32))
img=tf.add(img_hole, dst_padded)
 return {'inp1': img, 'inp2': data['inp2']}, label
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )<choose_model_class>
def get_training_dataset(dataset):
dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.map(hair_aug, num_parallel_calls=AUTO)
 dataset = dataset.repeat()
dataset = dataset.shuffle(512)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<define_variables>
def get_validation_dataset(dataset):
dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset
epochs = 30
batch_size = 86<init_hyperparams>
np.set_printoptions(threshold=15, linewidth=80 )
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>
def batch_to_numpy_images_and_labels(databatch, ds='train'):
if ds=='train':
data, labels = databatch
numpy_images = data['inp1'].numpy()
numpy_labels = labels.numpy()
else:
data = databatch
numpy_images = data['inp1'].numpy()
numpy_labels = [None for _ in enumerate(numpy_images)]
return numpy_images, numpy_labels
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] )<predict_on_test>
def display_batch_of_images(databatch, predictions=None, ds='train'):

 images, labels = batch_to_numpy_images_and_labels(databatch, ds=ds)
 if labels is None:
labels = [None for _ in enumerate(images)]
rows = int(math.sqrt(len(images)))
 cols = len(images)//rows
FIGSIZE = 13.0
SPACING = 0.1
subplot=(rows,cols,1)
 if rows < cols:
plt.figure(figsize=(FIGSIZE,FIGSIZE/cols*rows))
else:
plt.figure(figsize=(FIGSIZE/rows*cols,FIGSIZE))
for i,(image, label)in enumerate(zip(images[:rows*cols], labels[:rows*cols])) :
title = '' if label is None else CLASSES[label]
correct = True
if predictions is not None:
title, correct = title_from_label_and_target(predictions[i], label)
 dynamic_titlesize = FIGSIZE*SPACING/max(rows,cols)*40+3
subplot = display_one_image(image, title, subplot,
not correct, titlesize=dynamic_titlesize)
 plt.tight_layout()
if label is None and predictions is None:
plt.subplots_adjust(wspace=0, hspace=0)
 else:
plt.subplots_adjust(wspace=SPACING, hspace=SPACING)
 plt.show()
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )<save_to_csv>
training_dataset = training_dataset.map(setup_input, num_parallel_calls=AUTO)
 training_dataset = get_training_dataset(training_dataset)
 training_dataset = training_dataset.unbatch().batch(20)
 train_batch = iter(training_dataset )
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("submission.csv",index=False )<load_from_csv>
display_batch_of_images(next(train_batch))
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 print(train.shape, test.shape )<prepare_x_and_y>
del training_dataset, train_batch
gc.collect()
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
 del train<feature_engineering>
validation_dataset = validation_dataset.map(setup_input, num_parallel_calls=AUTO)
 validation_dataset = get_validation_dataset(validation_dataset)
 validation_dataset = validation_dataset.unbatch().batch(20)
 validation_batch = iter(validation_dataset )
X_train = X_train / 255.0
test = test / 255.0<categorify>
display_batch_of_images(next(validation_batch))
X_train = X_train.values.reshape(X_train.shape[0], 28, 28, 1)
 X_test = test.values.reshape(test.shape[0], 28, 28, 1)
 Y_train = to_categorical(Y_train, num_classes = 10)
 Y_train<import_modules>
del validation_dataset, validation_batch
gc.collect()
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization
from keras.utils import np_utils<choose_model_class>
def setup_test_image(image, data):
tab_data=[tf.cast(data[tfeat], dtype=tf.float32)for tfeat in tab_feats]
tabular=tf.stack(tab_data)
 return {'inp1': image, 'inp2': tabular}
model = Sequential()
dim = 28
nclasses = 10
model.add(Conv2D(filters=32, kernel_size=(5,5), padding='same', activation='relu', input_shape=(dim,dim,1)))
 model.add(Conv2D(filters=32, kernel_size=(5,5), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.2))
model.add(Conv2D(filters=64, kernel_size=(5,5), padding='same', activation='relu'))
model.add(Conv2D(filters=64, kernel_size=(5,5), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(120, activation='relu'))
model.add(Dense(84, activation='relu'))
model.add(Dense(nclasses, activation='softmax'))<choose_model_class>
def get_test_dataset(dataset):
dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'] )<choose_model_class>
test_dataset = load_dataset(TEST_FNAMES, labeled=False, ordered=True)
 test_dataset = test_dataset.map(setup_test_image, num_parallel_calls=AUTO)
 test_dataset = get_test_dataset(test_dataset)
 test_dataset = test_dataset.unbatch().batch(20)
 test_batch = iter(test_dataset )
learning_rate_reduction = ReduceLROnPlateau(monitor='acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=1e-7)
 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)
 history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=512),
epochs=50,
verbose=1,
steps_per_epoch=X_train.shape[0]/512,
callbacks=[learning_rate_reduction] )<compute_test_metric>
display_batch_of_images(next(test_batch), ds='test' )
loss_and_metrics = model.evaluate(X_train, Y_train, batch_size=128)
 print(loss_and_metrics )<save_to_csv>
del test_dataset, test_batch
gc.collect()
pred = model.predict_classes(X_test)
 submission = pd.DataFrame({
"ImageId": list(range(1,len(pred)+ 1)) ,
"Label": pred})
 submission.to_csv("submission.csv", index=False, header=True )<load_from_csv>
%%time
debug=0
histories = []
best_epoch={fn: np.zeros(NBEST)for fn in range(1, NFOLDS+1)}
best_score={fn: np.zeros(NBEST)for fn in range(1, NFOLDS+1)}
for fold_num in range(1, NFOLDS+1):
tf.keras.backend.clear_session()
tf.tpu.experimental.initialize_tpu_system(tpu)
 print("="*50)
 print(f"Starting fold {fold_num} out of {NFOLDS}...")
 files_trn=TRAIN_FNAMES[f"fold_{fold_num}"]
files_val=VAL_FNAMES[f"fold_{fold_num}"]
if debug:
files_trn=files_trn[0:2]
files_val=files_val[0:2]
EPOCHS=3
train_dataset = load_dataset(files_trn)
 train_dataset = train_dataset.map(setup_input, num_parallel_calls=AUTO)
 val_dataset = load_dataset(files_val, ordered = True)
 val_dataset = val_dataset.map(setup_input, num_parallel_calls=AUTO)
 model = get_model()
STEPS_PER_EPOCH = count_data_items(files_trn)// BATCH_SIZE
print(f'STEPS_PER_EPOCH = {STEPS_PER_EPOCH}')
 lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose = True)
 history = model.fit(get_training_dataset(train_dataset),
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
callbacks=[lr_callback,
save_best_n(fold_num, model),
],
validation_data=get_validation_dataset(val_dataset),
class_weight=class_weights,
verbose=2, 
)
 idx_sorted=np.argsort(best_score[fold_num])
 best_score[fold_num]=np.array(best_score[fold_num])[idx_sorted]
best_epoch[fold_num]=np.array(best_epoch[fold_num])[idx_sorted]
print(f"
Fold {fold_num} is finished.The best epochs: {[int(best_epoch[fold_num][i])for i in range(len(best_epoch[fold_num])) ]}")
 print(f"The corresponding scores: {[round(best_score[fold_num][i], 5)for i in range(len(best_epoch[fold_num])) ]}")
 histories.append(history )
d_train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 d_test = pd.read_csv(".. /input/digit-recognizer/test.csv" )<predict_on_test>
for fn in range(1, NFOLDS+1):
display_training_curves(fn, data=histories[fn-1].history )
d_test.head() 
<data_type_conversions>
def setup_test_image(image, data):
tab_data=[tf.cast(data[tfeat], dtype=tf.float32)for tfeat in tab_feats]
tabular=tf.stack(tab_data)
 return {'inp1': image, 'inp2': tabular}
x_train = d_train.iloc[:, 1:].to_numpy(dtype = np.float)
 x_train = np.reshape(x_train, newshape =(len(x_train), 1, 28, 28))
y_train = d_train['label'].to_numpy()
print(x_train.shape)
 print(y_train.shape)
 x_test = d_test.to_numpy(dtype = np.float)
 x_test = np.reshape(x_test, newshape =(len(x_test), 1, 28, 28))
print(x_test.shape )<categorify>
def average_predictions(X, fn):
y_probas=[]
for idx in range(NBEST):
tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 gc.collect()
print(f"Predicting: fold {fn}, model {idx+1} out of {NBEST}...")
 with strategy.scope() :
path_best_model=f'best_model_fold_{fn}_{idx}.hdf5'
model=tf.keras.models.load_model(SAVE_FOLDER/path_best_model)
 y=model.predict(X)
 y = rankdata(y)/len(y)
 y_probas.append(y)
 y_probas=np.average(y_probas, axis=0)
 return y_probas
x_train = x_train / 255
x_test = x_test / 255
print(x_train[0])
 print(x_test[0] )<split>
%%time
preds = pd.DataFrame({'image_name': np.zeros(len(test)) , 'target': np.zeros(len(test)) })
 test_ds = load_dataset(TEST_FNAMES, labeled=False, ordered=True)
 test_images_ds = test_ds.map(setup_test_image, num_parallel_calls=AUTO)
 test_images_ds = get_test_dataset(test_images_ds)
 test_ds = get_test_dataset(test_ds)
 test_ids_ds = test_ds.map(setup_test_name, num_parallel_calls=AUTO ).unbatch()
preds['image_name'] = next(iter(test_ids_ds.batch(N_TEST_IMGS)) ).numpy().astype('U')
 preds['target'] = np.average([average_predictions(test_images_ds, fn)for fn in range(1, NFOLDS+1)], axis = 0 )
X_tr = torch.from_numpy(x_train ).type(torch.FloatTensor)
 Y_tr = torch.from_numpy(y_train)
 X_te = torch.from_numpy(x_test ).type(torch.FloatTensor)
 Y_te = torch.from_numpy(np.zeros(x_test.shape))
X_train, X_va, Y_train, Y_va = train_test_split(X_tr, Y_tr, test_size = 0.2)
 train_set = torch.utils.data.TensorDataset(X_train, Y_train)
 val_set = torch.utils.data.TensorDataset(X_va, Y_va)
 test_set = torch.utils.data.TensorDataset(X_te, Y_te)
 trainloader = torch.utils.data.DataLoader(train_set, batch_size = 32, shuffle = False)
 valloader = torch.utils.data.DataLoader(val_set, batch_size = 32, shuffle = False)
 testloader = torch.utils.data.DataLoader(test_set, batch_size = 32, shuffle = False )<import_modules>
del sub['target']
sub = sub.merge(preds, on='image_name')
 sub.head()
class NMIST_CNN(nn.Module):
def __init__(self):
super(NMIST_CNN, self ).__init__()
self.cn1 = nn.Conv2d(1, 16, kernel_size = 4, padding = 2)
 nn.init.xavier_normal_(self.cn1.weight)
 nn.init.zeros_(self.cn1.bias)
 self.bn1 = nn.BatchNorm2d(num_features = 16)
 self.cn2 = nn.Conv2d(16, 32, kernel_size = 4)
 nn.init.xavier_normal_(self.cn2.weight)
 nn.init.zeros_(self.cn2.bias)
 self.bn2 = nn.BatchNorm2d(num_features = 32)
 self.fc1 = nn.Linear(800, 200)
 nn.init.xavier_normal_(self.fc1.weight)
 nn.init.zeros_(self.fc1.bias)
 self.bn3 = nn.BatchNorm1d(num_features = 200)
 self.fc2 = nn.Linear(200, 10)
 self.bn4 = nn.BatchNorm1d(num_features = 10)
 self.dropout = nn.Dropout(p = 0.2)
 self.pooling = nn.MaxPool2d(( 2,2))
def forward(self, x):
x = F.relu(self.bn1(self.cn1(x)))
 x = self.pooling(x)
 x = self.dropout(x)
 x = F.relu(self.bn2(self.cn2(x)))
 x = self.pooling(x)
 x = self.dropout(x)
 x = x.view(-1, 800)
 x = F.relu(self.bn3(self.fc1(x)))
 x = self.dropout(x)
 x = F.log_softmax(self.bn4(self.fc2(x)) , dim = 1)
 return x<choose_model_class>
print(f"The lengths of the submission file and `test` are {len(sub)} and {len(test)}, respectively.")
 print(f"The number of NA's in the submission file is {sub.isna().sum().sum() }." )
model = NMIST_CNN()
print(model )<choose_model_class>
if colab:
OUT_FOLDER=SAVE_FOLDER
else:
OUT_FOLDER=Path('')
 sub.to_csv(OUT_FOLDER/'submission.csv', index=False )
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters() , lr=0.003)
 epochs = 30
train_losses, test_losses, train_accuracies, test_accuracies = [], [], [], []
for e in range(epochs):
tr_loss = 0
tr_accuracy = 0
model.train()
for images, labels in trainloader:
optimizer.zero_grad()
results = model(images)
 loss = criterion(results, labels)
 loss.backward()
optimizer.step()
tr_loss += loss.item()
top_p, top_class = results.topk(1, dim=1)
 equals = top_class == labels.view(*top_class.shape)
 tr_accuracy += torch.mean(equals.type(torch.FloatTensor))
else:
te_loss = 0
te_accuracy = 0
with torch.no_grad() :
model.eval()
for images, labels in valloader:
test_results = model(images)
 loss2 = criterion(test_results, labels)
 te_loss += loss2.item()
top_p, top_class = test_results.topk(1, dim=1)
 equals = top_class == labels.view(*top_class.shape)
 te_accuracy += torch.mean(equals.type(torch.FloatTensor))
train_losses.append(tr_loss/len(trainloader))
train_accuracies.append(tr_accuracy.item() /len(trainloader))
test_losses.append(te_loss/len(valloader))
test_accuracies.append(te_accuracy.item() /len(valloader))
print("Epoch: " + str(e+1))
print("Train loss: " + str(tr_loss/len(trainloader)))
 print(f'Train accuracy: {tr_accuracy.item() *100/len(trainloader)}%')
 print("Validation loss: " + str(te_loss/len(valloader)))
 print(f'Validation accuracy: {te_accuracy.item() *100/len(valloader)}%')
 print('' )<prepare_output>
def setup_val_image(image, label, data, label_name):
tab_data=[tf.cast(data[tfeat], dtype=tf.float32)for tfeat in tab_feats]
tabular=tf.stack(tab_data)
 return {'inp1': image, 'inp2': tabular}
prediction = []
with torch.no_grad() :
model.eval()
for images, x in testloader:
predictions = model(images)
 top_p, top_class = predictions.topk(1, dim=1)
 for n in range(len(predictions)) :
prediction.append(top_class[n].item())
 submit = pd.DataFrame(data={'Label':prediction})
 submit.insert(0, 'ImageId', range(1, 1 + len(submit)) , True)
 submit.head()<save_to_csv>
def setup_val_label(image, label, data, label_name):
return label
submit.to_csv('submission.csv', index = False)
 submit.head()<import_modules>
oof= pd.DataFrame({'image_name': train['image_name'].values})
 auc=[]
oof_all_folds=[]
for fold_num in range(1, NFOLDS+1):
tf.keras.backend.clear_session()
print("="*50)
 print(f"Starting fold {fold_num}...")
 print(f"The best epochs: {[int(best_epoch[fold_num][i])for i in range(len(best_epoch[fold_num])) ]}")
 print(f"The corresponding scores: {[round(best_score[fold_num][i], 5)for i in range(len(best_epoch[fold_num])) ]}")
 files_val=VAL_FNAMES[f"fold_{fold_num}"]
if debug:
files_val=files_val[0:2]
val_ds = load_dataset(files_val, ordered = True)
 val_images_ds = val_ds.map(setup_val_image,
num_parallel_calls=AUTO)
 val_images_ds = get_validation_dataset(val_images_ds)
 val_ds = get_validation_dataset(val_ds)
 val_label_ds = val_ds.map(setup_val_label,
num_parallel_calls=AUTO ).unbatch()
val_ids_ds = val_ds.map(setup_val_name,
num_parallel_calls=AUTO ).unbatch()
n_val_fold = count_data_items(files_val)
 print(f'The
oof_fold= pd.DataFrame()
oof_fold['image_name'] = next(iter(val_ids_ds.batch(n_val_fold)) ).numpy().astype('U')
 oof_fold['target'] = average_predictions(val_images_ds, fold_num)
 oof_all_folds.append(oof_fold)
 y_true = next(iter(val_label_ds.batch(n_val_fold)) ).numpy()
auc_fold=roc_auc_score(y_true, oof_fold['target'].values)
 auc.append(auc_fold)
 print(f"Fold {fold_num} is done! ROC AUC = {auc_fold:.5f}")
 oof=oof.merge(pd.concat(oof_all_folds),
on='image_name',
how='left' ).reset_index(drop=True)
 if debug:
oof=oof.fillna(0)
 auc=np.array(auc)
 auc_av=auc.mean()
auc_std=auc.std()
print(f"ROC AUC = {auc_av:.5f}, STDEV = {auc_std:.5f}(average across the folds)")
 auc_oof=roc_auc_score(train['target'].values, oof['target'].values)
 print(f"ROC AUC = {auc_oof:.5f}(out of folds)" )
import keras
from keras.datasets import mnist
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd<define_variables>
print(f"The lengths of the oof data frame and `train` are {len(oof)} and {len(train)}, respectively.")
 print(f"The number of NA's in the oof data frame is {oof.isna().sum().sum() }." )
batch_size = 128
num_classes = 10
epochs = 30<load_from_csv>
oof.to_csv(OUT_FOLDER/'oof.csv', index=False )
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv" )<prepare_x_and_y>
y_train = train["label"]
x_train = train.drop(labels=["label"], axis=1 )<split>
sub_v9=pd.read_csv('/kaggle/input/siim-benchmark-submission/submission_v9.csv')['target'].values
sub_v10=pd.read_csv('/kaggle/input/siim-benchmark-submission/submission_v10.csv')['target'].values
sub_v13=pd.read_csv('/kaggle/input/siim-benchmark-submission/submission_v10.csv')['target'].values
sub_v9=rankdata(sub_v9)/len(sub_v9)
 sub_v10=rankdata(sub_v10)/len(sub_v10)
 sub_v13=rankdata(sub_v13)/len(sub_v13)
 oof_v9=pd.read_csv('/kaggle/input/siim-benchmark-submission/oof_v9.csv')['target'].values
oof_v10=pd.read_csv('/kaggle/input/siim-benchmark-submission/oof_v10.csv')['target'].values
oof_v13=pd.read_csv('/kaggle/input/siim-benchmark-submission/oof_v13.csv')['target'].values
oof_v9=rankdata(oof_v9)/len(oof_v9)
 oof_v10=rankdata(oof_v10)/len(oof_v10)
 oof_v13=rankdata(oof_v13)/len(oof_v13)
 preds_blend=1/2*1/3*(sub_v9+
sub_v10+
sub['target'].values 
)+1/2*sub_v13
oof_blend=1/2*1/3*(oof_v9+
oof_v10+
oof['target'].values 
)+1/2*oof_v13
auc_blend=roc_auc_score(train['target'].values, oof_blend)
 print(f"ROC AUC score of the blend = {auc_blend:.5f}" )
x_train, x_test, y_train, y_test = train_test_split(x_train, y_train , random_state = 42, test_size=0.3 )<import_modules>
sub_blend=sub.copy()
sub_blend['target']=preds_blend
sub_blend.to_csv(OUT_FOLDER/'submission_blend.csv', index=False )
from keras import backend<data_type_conversions>
corr=np.corrcoef(rankdata(sub_v9)/len(sub_v9),
rankdata(sub['target'].values)/len(sub))
print(f"The correlation with the benchmark submission:
{corr}." )
x_train = x_train.astype('float')
 x_test = x_test.astype('float')
 test = test.astype('float')
 x_train /= 255
x_test /= 255
test/= 255<categorify>
del auc
gc.collect()
<choose_model_class><EOS> 
from sklearn.metrics import roc_curve, auc