Kaggle competition: Petals to the Metal - Flower Classification on TPU
<SOS> metric: macrofscore Kaggle data source: petals-to-the-metal-flower-classification-on-tpu<save_to_csv>
!pip install -U git+https://github.com/qubvel/efficientnet >> /dev/null
y_pred = sq.predict(X_test_fin)
 y_final =(y_pred > 0.5 ).astype(int ).reshape(X_test_fin.shape[0])
 sub = pd.DataFrame()
sub['PassengerId'] = test['PassengerId']
sub['Survived'] = y_final
sub['Survived'] = sub.apply(lambda r: leaks[int(r['PassengerId'])] if int(r['PassengerId'])in leaks else r['Survived'], axis=1)
 sub.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!")
 sub.head() 
<import_modules>
AUTO = tf.data.experimental.AUTOTUNE
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import to_categorical
from keras.preprocessing import image
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical
from tqdm import tqdm<load_from_csv>
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 )
train=pd.read_csv('.. /input/train.csv')
 test=pd.read_csv('.. /input/test.csv' )<prepare_x_and_y>
MIXED_PRECISION = True
XLA_ACCELERATE = True
if MIXED_PRECISION:
if tpu: policy = tf.keras.mixed_precision.experimental.Policy('mixed_bfloat16')
 else: policy = tf.keras.mixed_precision.experimental.Policy('mixed_float16')
 mixed_precision.set_policy(policy)
 print('Mixed precision enabled')
 if XLA_ACCELERATE:
tf.config.optimizer.set_jit(True)
 print('Accelerated Linear Algebra enabled' )
features = [c for c in train.columns if c not in ['label']]
y=train['label']
X = train.drop(labels = ["label"],axis = 1)
 X = X.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1)
<categorify>
IMAGE_SIZES = [[512, 512], [331, 331], [224, 224], [192, 192]]
IMAGE_SIZE = IMAGE_SIZES[2]
BATCH_SIZE = 32 * strategy.num_replicas_in_sync
print('Using IMAGE_SIZE', IMAGE_SIZE, 'and BATCH_SIZE', BATCH_SIZE )
y = to_categorical(y )<feature_engineering>
use_external = True
BASE_PATH = KaggleDatasets().get_gcs_path('tpu-getting-started')
 EXT_PATH = KaggleDatasets().get_gcs_path('tf-flower-photo-tfrec')
 def get_path(image_size=IMAGE_SIZE):
return f'{BASE_PATH}/tfrecords-jpeg-{image_size[0]}x{image_size[1]}'
def get_ext_path(folder, image_size=IMAGE_SIZE):
return f'{EXT_PATH}/{folder}/tfrecords-jpeg-{image_size[0]}x{image_size[1]}'
TRAINING_FILES = tf.io.gfile.glob(get_path() + '/train/*.tfrec')
 VALIDATION_FILES = tf.io.gfile.glob(get_path() + '/val/*.tfrec')
 TEST_FILES = tf.io.gfile.glob(get_path() + '/test/*.tfrec')
 if use_external:
INATURALIST = tf.io.gfile.glob(get_ext_path('inaturalist_no_test')+ '/*.tfrec')
 TRAINING_FILES += INATURALIST
OXFORD = tf.io.gfile.glob(get_ext_path('oxford_102_no_test')+ '/*.tfrec')
 TRAINING_FILES += OXFORD
TFFLOWERS = tf.io.gfile.glob(get_ext_path('tf_flowers_no_test')+ '/*.tfrec')
 TRAINING_FILES += TFFLOWERS
print('Loaded Training:', len(TRAINING_FILES), 'files, Validation:', len(VALIDATION_FILES), 'files, Test:', len(TEST_FILES), 'files' )
X = X / 255.0
test = test / 255.0 
<split>
CLASSES = ['pink primrose', 'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea', 'wild geranium', 'tiger lily', 'moon orchid', 'bird of paradise', 'monkshood', 'globe thistle',
'snapdragon', "colt's foot", 'king protea', 'spear thistle', 'yellow iris', 'globe-flower', 'purple coneflower', 'peruvian lily', 'balloon flower', 'giant white arum lily',
'fire lily', 'pincushion flower', 'fritillary', 'red ginger', 'grape hyacinth', 'corn poppy', 'prince of wales feathers', 'stemless gentian', 'artichoke', 'sweet william',
'carnation', 'garden phlox', 'love in the mist', 'cosmos', 'alpine sea holly', 'ruby-lipped cattleya', 'cape flower', 'great masterwort', 'siam tulip', 'lenten rose',
'barberton daisy', 'daffodil', 'sword lily', 'poinsettia', 'bolero deep blue', 'wallflower', 'marigold', 'buttercup', 'daisy', 'common dandelion',
'petunia', 'wild pansy', 'primula', 'sunflower', 'lilac hibiscus', 'bishop of llandaff', 'gaura', 'geranium', 'orange dahlia', 'pink-yellow dahlia',
'cautleya spicata', 'japanese anemone', 'black-eyed susan', 'silverbush', 'californian poppy', 'osteospermum', 'spring crocus', 'iris', 'windflower', 'tree poppy',
'gazania', 'azalea', 'water lily', 'rose', 'thorn apple', 'morning glory', 'passion flower', 'lotus', 'toad lily', 'anthurium',
'frangipani', 'clematis', 'hibiscus', 'columbine', 'desert-rose', 'tree mallow', 'magnolia', 'cyclamen ', 'watercress', 'canna lily',
'hippeastrum ', 'bee balm', 'pink quill', 'foxglove', 'bougainvillea', 'camellia', 'mallow', 'mexican petunia', 'bromelia', 'blanket flower',
'trumpet creeper', 'blackberry lily', 'common tulip', 'wild rose']
print(len(CLASSES))
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.1 )<choose_model_class>
class DS() :
def __init__(self, files, training=True, shuffle=False, augment=False, batch_size=16, repeat=False):
self.files = files
self.augment = augment
self.ds = tf.data.TFRecordDataset(files, num_parallel_reads = AUTO)
 self.ds = self.ds.map(self.read_with_label if training else self.read_without_label)
 if self.augment:
self.ds = self.ds.map(self.augment_image)
 if shuffle:
options = tf.data.Options()
options.experimental_deterministic = False
self.ds = self.ds.with_options(options)
 self.ds = self.ds.shuffle(2048)
 if repeat:
self.ds = self.ds.repeat()
self.ds = self.ds.batch(batch_size)
 self.ds = self.ds.prefetch(AUTO)
 def data(self):
return self.ds
def read_with_label(self, example):
example = tf.io.parse_single_example(example, {
'image': tf.io.FixedLenFeature([], tf.string),
'class': tf.io.FixedLenFeature([], tf.int64),
})
 return self.decode_image(example['image']), tf.cast(example['class'], tf.int32)
 def read_without_label(self, example):
example = tf.io.parse_single_example(example, {
'image': tf.io.FixedLenFeature([], tf.string),
'id': tf.io.FixedLenFeature([], tf.string),
})
 return self.decode_image(example['image']), example['id']
def decode_image(self, img):
img = tf.io.decode_jpeg(img, channels=3)
 img = tf.cast(img, tf.float32)/ 255.0
return tf.reshape(img, [*IMAGE_SIZE, 3])
 def augment_image(self, img, label):
return Augmentor.process(img, IMAGE_SIZE[0]), label
def count(self):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in self.files]
return np.sum(n)
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3), padding='same',activation='relu',input_shape=(28,28,1)))
 model.add(Conv2D(64,(3, 3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64, 5, padding='same', activation='relu'))
model.add(Conv2D(64, 5, padding='same', activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(Dropout(0.5))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))<choose_model_class>
items = 0
batches = 0
times = {
'batches': list() ,
'items': list() ,
'batch_speed': list() ,
'item_speed': list() ,
}
def loop(items, batches, times):
train_ds = DS(TRAINING_FILES, augment=True, batch_size=128 ).data()
start = time.time()
for element in train_ds:
so_far = time.time() - start
batches += 1
items += len(element[0])
 times['batches'].append(batches)
 times['items'].append(items)
 times['batch_speed'].append(( batches+1)/so_far)
 times['item_speed'].append(( items+1)/so_far)
 print(items, so_far,(items)/so_far)
 print(batches, items, so_far,(batches)/so_far,(items)/so_far)
 gc.collect()
def show_graph(size):
plt.figure(figsize=(12,6))
plt.plot(times['batches'], times['batch_speed'], color='red')
 plt.plot(times['items'], times['item_speed'], color='blue', linestyle='--')
 plt.legend()
plt.title('Batch size' + str(size))
plt.show()
model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])
<train_model>
def test_dataset() :
print('Training images')
 show_images(DS(TRAINING_FILES, augment=True, batch_size=128 ).data())
 print('Validation image')
 show_images(DS(VALIDATION_FILES, batch_size=128 ).data())
 print('Testing images')
 show_images(DS(TEST_FILES, training=False, batch_size=128 ).data())
 print('Testing Augmented images')
 show_images(DS(TEST_FILES, training=False, augment=True, batch_size=128 ).data())
 def show_images(ds, rows=3, columns=6):
plt.figure(figsize=(12, 6))
count = 0
for i, examples in enumerate(ds.take(1)) :
images = examples[0]
labels = examples[1]
for j, image in enumerate(images):
if count == rows * columns:
break
plt.subplot(rows, columns, count+1, xticks=[], yticks=[])
 plt.imshow(image)
 if labels[0].dtype == tf.string:
plt.xlabel(labels[j].numpy().decode("utf-8"))
else:
plt.xlabel(CLASSES[labels[j]])
 count += 1
plt.tight_layout()
plt.show()
gc.collect()
datagen = ImageDataGenerator(
 rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
 datagen.fit(X_train )<train_model>
EFNS = [
efn.EfficientNetB0,
efn.EfficientNetB1,
efn.EfficientNetB2,
efn.EfficientNetB3,
efn.EfficientNetB4,
efn.EfficientNetB5,
efn.EfficientNetB6,
efn.EfficientNetB7,
]
def create_efficientnet(index, weight='imagenet'):
pretrained_model = EFNS[index](
 weights = weight,
include_top = False,
input_shape = [*IMAGE_SIZE, 3] 
)
 pretrained_model.trainable = True
model = tf.keras.Sequential([
pretrained_model,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(1024, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(len(CLASSES), activation='softmax'),
], name='efn' + str(index))
return model
def create_densenet() :
pretrained_model = tf.keras.applications.DenseNet201(
 weights = 'imagenet',
include_top = False,
input_shape = [*IMAGE_SIZE, 3] 
)
 pretrained_model.trainable = True
model = tf.keras.Sequential([
pretrained_model,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(1024, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(len(CLASSES), activation='softmax'),
], name='densenet')
 return model
def create_cnn() :
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(16, kernel_size=3, strides=2, padding='same', activation='relu', input_shape=[*IMAGE_SIZE, 3]),
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ], name='simple')
 return model
batch_size = 80
results = model.fit_generator(datagen.flow(X_train, y_train, batch_size = batch_size), epochs=50,
steps_per_epoch=X_train.shape[0] // batch_size,
validation_data=(X_test, y_test))<compute_test_metric>
def compile_models(models):
for x in models:
models[x].compile(
 optimizer = tf.keras.optimizers.Adam(learning_rate=1e-3),
loss = tf.keras.losses.SparseCategoricalCrossentropy() ,
metrics = ['sparse_categorical_accuracy'], 
)
 models[x].summary()
print('')
 models = {}
with strategy.scope() :
start = time.time()
models['efn7'] = create_efficientnet(7)
 print(f'Created efn7 in {(time.time() - start):.4f}s')
 start = time.time()
models['efn6'] = create_efficientnet(6)
 print(f'Created efn6 in {(time.time() - start):.4f}s')
 start = time.time()
models['efn4-noisy'] = create_efficientnet(4, 'noisy-student')
 print(f'Created efn4 in {(time.time() - start):.4f}s')
 start = time.time()
models['densenet'] = create_densenet()
print(f'Created densenet in {(time.time() - start):.4f}s')
 compile_models(models )
final_loss, final_acc = model.evaluate(X_test, y_test, verbose=0)
 print("Final loss: {0:.4f}, final accuracy: {1:.4f}".format(final_loss, final_acc))<save_to_csv>
train_ds = DS(TRAINING_FILES, augment=True, shuffle=True, batch_size=BATCH_SIZE, repeat=True)
 val_ds = DS(VALIDATION_FILES, batch_size=BATCH_SIZE)
 val_aug_ds = DS(VALIDATION_FILES, augment=True, batch_size=BATCH_SIZE)
 test_ds = DS(TEST_FILES, training=False, batch_size=BATCH_SIZE)
 print(f'Training: {train_ds.count() } images, Validation: {val_ds.count() }, Test: {test_ds.count() } images' )
prediction = model.predict(test)
 prediction = np.argmax(prediction,axis = 1)
 output=pd.DataFrame({"ImageId": list(range(1,len(prediction)+1)) ,
"Label": prediction})
 output.to_csv("output.csv", index=False, header=True )<import_modules>
lr_start = 0.00005
lr_max = 0.0000125 * strategy.num_replicas_in_sync
lr_min = 0.0000001
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
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)
 count = range(100)
 plt.figure(figsize=(12, 6))
plt.subplot(1, 3, 1)
 plt.plot(count, [lrfn(x)for x in count])
 cosine_decay_learning_rate = tf.keras.experimental.CosineDecayRestarts(
 0.001, 0.8, t_mul=2.0, m_mul=1.0, alpha=0.0,
name=None 
)
import os
from typing import Dict, List, Tuple
from joblib import Parallel, delayed
import pandas as pd
import numpy as np
from scipy.optimize.minpack import curve_fit
from scipy.optimize import least_squares
from xgboost import XGBRegressor<load_from_csv>
class Visibility(tf.keras.callbacks.Callback):
def __init__(self):
super(Visibility, self ).__init__()
self.metrics = ['sparse_categorical_accuracy', 'loss']
def on_train_begin(self, logs=None):
self.train_begin = time.time()
def on_train_end(self, logs=None):
print('
Training took ', time.time() - self.train_begin, 'seconds')
 def on_epoch_end(self, epoch, logs=None):
output = ""
for metric in self.metrics:
diff =(( logs['val_'+metric] / logs[metric])- 1)*100
output += f"val/{metric}: {diff:.2f}% "
print("
" + output)
 def on_train_batch_begin(self, batch, logs=None):
print("▪", end="")
 def on_test_batch_end(self, batch, logs=None):
print("▫", end="")
def load_kaggle_csv(dataset: str, datadir: str)-> pd.DataFrame:

 df = pd.read_csv(
 f"{os.path.join(datadir,dataset)}.csv", parse_dates=["Date"] 
)
 df['country'] = df["Country_Region"]
if "Province_State" in df:
df["Country_Region"] = np.where(
 df["Province_State"].isnull() ,
df["Country_Region"],
df["Country_Region"] + "_" + df["Province_State"], 
)
 df.drop(columns="Province_State", inplace=True)
 if "ConfirmedCases" in df:
df["ConfirmedCases"] = df.groupby("Country_Region")[
"ConfirmedCases"
].cummax()
if "Fatalities" in df:
df["Fatalities"] = df.groupby("Country_Region")["Fatalities"].cummax()
if not "DayOfYear" in df:
df["DayOfYear"] = df["Date"].dt.dayofyear
df["Date"] = df["Date"].dt.date
return df
def RMSLE(actual: np.ndarray, prediction: np.ndarray)-> float:

 return np.sqrt(
 np.mean(
 np.power(np.log1p(np.maximum(0, prediction)) - np.log1p(actual), 2)
)
 )<load_from_csv>
EPOCHS = 50
history = {}
def fit_models(models, history):
for x in models:
print(f'Starting training for {x}')
 hist = models[x].fit(
 train_ds.data() ,
validation_data = val_ds.data() ,
epochs = EPOCHS,
steps_per_epoch = train_ds.count() // BATCH_SIZE,
verbose=2,
callbacks=[
tf.keras.callbacks.ModelCheckpoint(
 monitor='val_sparse_categorical_accuracy', verbose=2, save_best_only=True, save_weights_only=True, mode='max', filepath=f'{x}.h5'),
lr_callback,
tf.keras.callbacks.EarlyStopping(
 monitor='val_sparse_categorical_accuracy', min_delta=0.001, patience=3, verbose=2, mode='max', baseline=None, restore_best_weights=True 
),
Visibility() ,
], 
)
 history[x] = hist
fit_models(models, history )
train = load_kaggle_csv(
 "train", "/kaggle/input/covid19-global-forecasting-week-3")
 country_health_indicators =(
 pd.read_csv("/kaggle/input/country-health-indicators/country_health_indicators_v3.csv")).rename(
 columns={'Country_Region': 'country'})
 train = pd.merge(train, country_health_indicators, on="country", how="left" )<merge>
highest_f1 = 0
highest_f1_model = ''
preds = list()
def get_y_true() :
def get_val_classes() :
all_classes = np.array([])
 for examples in val_ds.data() :
all_classes = np.concatenate([all_classes, examples[1].numpy() ])
 return all_classes
y_true = get_val_classes()
print('True Values:: Length:', len(y_true), ', List:', y_true)
 return y_true
def predict_models(models, y_true):
global highest_f1
global highest_f1_model
global preds
val_tpu_ds = val_aug_ds.data().map(lambda image, ids: image)
 for x in models:
val_aug_count = 2
y_val_pred = np.zeros(( 3712,104), dtype=np.float64)
 for i in range(val_aug_count):
y_val_loop = models[x].predict(val_tpu_ds, verbose=1)
 y_val_loop = tf.cast(y_val_loop, tf.float32)
 y_val_pred += y_val_loop.numpy()
y_val_pred /= val_aug_count
y_val = np.argmax(y_val_pred, axis=-1 ).astype(np.float32)
 print(f'
Scores for {x}
')
 print(f'{x}: Pred Values:: Length:{len(y_val)} List: {y_val}')
 model_f1_score = f1_score(y_true, y_val, average='macro', zero_division=0)*100
if model_f1_score > highest_f1:
highest_f1 = model_f1_score
highest_f1_model = x
scores = {
"metrics": [
"F1",
"Precision",
"Recall",
],
"scores": [
model_f1_score,
precision_score(y_true, y_val, average='macro', zero_division=0)*100,
recall_score(y_true, y_val, average='macro', zero_division=0)*100,
]
}
print(pd.DataFrame.from_dict(scores))
preds.append((
x,
y_val_pred,
model_f1_score, 
))
y_true = get_y_true()
predict_models(models, y_true )
test = load_kaggle_csv(
 "test", "/kaggle/input/covid19-global-forecasting-week-3")
 test = pd.merge(test, country_health_indicators, on="country", how="left" )<train_on_grid>
def get_best_alpha(pred1, pred2):
best_alpha = 0
best_f1 = 0
for x in np.linspace(0, 1, 101):
mixed =(pred1 * x)+(pred2 *(1-x))
y_mixed = np.argmax(mixed, axis=-1 ).astype(np.float32)
 mixed_f1 = f1_score(y_true, y_mixed, average='macro', zero_division=0)*100
if mixed_f1 > best_f1:
best_f1 = mixed_f1
best_alpha = x
return best_alpha, best_f1
def get_alpha(preds, y_true):
output = list()
preds = np.array(preds)
 preds = preds[preds[:,2].argsort() [::-1]]
model_name = preds[0][0]
predictions = preds[0][1]
model_f1 = preds[0][2]
output.append(( model_name, 1))
best_predictions = predictions
best_f1 = model_f1
print(best_f1, output)
 for i in range(1, len(preds)) :
i_name = preds[i][0]
i_preds = preds[i][1]
i_f1 = preds[i][1]
alpha, f1 = get_best_alpha(best_predictions, i_preds)
 if f1 > best_f1:
best_predictions =(alpha * i_preds)+(( 1-alpha)* best_predictions)
 best_f1 = f1
output.append(( i_name, alpha))
print(best_f1, output)
 return output
alpha_chain = get_alpha(preds, y_true )
def logistic(x: np.ndarray, x0: float, L: float, k: float)-> np.ndarray:

 return L /(1 + np.exp(-k *(x - x0)))
 def fit_single_logistic(x: np.ndarray, y: np.ndarray, maxfev: float)-> Tuple:

 p0 = [np.median(x), y[-1], 0.1]
pn0 = p0 *(np.random.random(len(p0)) + [0.5, 1.0, 0.5])
 try:
params, pcov = curve_fit(
 logistic,
x,
y,
p0=pn0,
maxfev=maxfev,
sigma=np.maximum(1, np.sqrt(y)) *(0.1 + 0.9 * np.random.random()),
bounds=([0, y[-1], 0.01], [200, 1e6, 1.5]), 
)
 pcov = pcov[np.triu_indices_from(pcov)]
except(RuntimeError, ValueError):
params = p0
pcov = np.zeros(len(p0)*(len(p0)- 1))
y_hat = logistic(x, *params)
 rmsle = RMSLE(y_hat, y)
 return(params, pcov, rmsle, y_hat)
 def fit_logistic(
 df: pd.DataFrame,
n_jobs: int = 8,
n_samples: int = 80,
maxfev: int = 8000,
x_col: str = "DayOfYear",
y_cols: List[str] = ["ConfirmedCases", "Fatalities"], 
)-> pd.DataFrame:
def fit_one(df: pd.DataFrame, y_col: str)-> Dict:
best_rmsle = None
best_params = None
x = df[x_col].to_numpy()
y = df[y_col].to_numpy()
for(params, cov, rmsle, y_hat)in Parallel(n_jobs=n_jobs )(
 delayed(fit_single_logistic )(x, y, maxfev=maxfev)
 for i in range(n_samples)
):
if rmsle >=(best_rmsle or rmsle):
best_rmsle = rmsle
best_params = params
result = {f"{y_col}_rmsle": best_rmsle}
result.update({f"{y_col}_p_{i}": p for i, p in enumerate(best_params)})
 return result
result = {}
for y_col in y_cols:
result.update(fit_one(df, y_col))
return pd.DataFrame([result])
 def predict_logistic(
 df: pd.DataFrame,
x_col: str = "DayOfYear",
y_cols: List[str] = ["ConfirmedCases", "Fatalities"], 
):
def predict_one(col):
df[f"yhat_logistic_{col}"] = logistic(
 df[x_col].to_numpy() ,
df[f"{col}_p_0"].to_numpy() ,
df[f"{col}_p_1"].to_numpy() ,
df[f"{col}_p_2"].to_numpy() , 
)
 for y_col in y_cols:
predict_one(y_col )<merge>
print(f'{highest_f1_model} has the highest f1 score - {highest_f1:0.5f}' )
<train_model><EOS> 
ds_submission = DS(TEST_FILES, training=False, augment=True, batch_size=BATCH_SIZE ).data()
ds_submission = ds_submission.map(lambda image, ids: image)
 def get_test_ids() :
all_ids = np.array([])
 for examples in test_ds.data() :
all_ids = np.concatenate([all_ids, examples[1].numpy().astype('U')])
 return all_ids
def get_prediction_with_tta(model, ds_submission):
tta = 5
y_pred = np.zeros(( 7382,104), dtype=np.float64)
 for x in range(tta):
y_pred_loop = model.predict(ds_submission, verbose=2)
 y_pred_loop = tf.cast(y_pred_loop, tf.float32)
 y_pred += y_pred_loop.numpy()
y_pred /= tta
return y_pred
def create_submission(alpha_chain, models, ds_submission):
y_pred = np.zeros(( 7382,104), dtype=np.float64)
 for x in alpha_chain:
model = x[0]
alpha = x[1]
print('Predicting with', model, 'with alpha', alpha)
 y_pred_chain = get_prediction_with_tta(models[model], ds_submission)
 y_pred =(alpha * y_pred_chain)+(( 1-alpha)* y_pred)
 y_pred = np.argmax(y_pred, axis=-1)
 filename = f'submission.csv'
np.savetxt(
 filename,
np.column_stack(( get_test_ids() , y_pred)) ,
fmt=('%s', '%s'),
delimiter=',',
header='id,label',
comments='' 
)
 create_submission(alpha_chain, models, ds_submission )