Kaggle competition: Flower Classification with TPUs
<SOS> metric: MacroFScore Kaggle data source: flower-classification-with-tpus<save_to_csv>
print("Tensorflow version " + tf.__version__)
 AUTO = tf.data.experimental.AUTOTUNE
res_df = pd.DataFrame({'PassengerId': test_df['PassengerId'], 'Survived': labels})
 res_df.to_csv('titanic_submission.csv', index=False )<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 )
PATH_WEEK2 = '/kaggle/input/covid19-global-forecasting-week-2'
df_Train = pd.read_csv(f'{PATH_WEEK2}/train.csv')
 df_test = pd.read_csv(f'{PATH_WEEK2}/test.csv' )<load_from_csv>
GCS_DS_PATH = KaggleDatasets().get_gcs_path('flower-classification' )
PATH_POPULATION = '/kaggle/input/population-by-country-2020'
df_Population = pd.read_csv(f'{PATH_POPULATION}/population_by_country_2020.csv' )<split>
EXT_DS_PATH = KaggleDatasets().get_gcs_path('oxford-102-for-tpu-competition' )
df_Train.iloc[np.r_[0:5, -6:-1], :]<split>
USE_EXTERNAL = True
SKIP_VALIDATION = False
df_test.iloc[np.r_[0:5, -6:-1], :]<split>
if tpu:
sz = 512
else:
sz = 192
IMAGE_SIZE = [sz, sz]
EPOCHS = 20
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
AUG_BATCH = BATCH_SIZE
GCS_PATH_SELECT = {
192: GCS_DS_PATH + '/tfrecords-jpeg-192x192',
224: GCS_DS_PATH + '/tfrecords-jpeg-224x224',
331: GCS_DS_PATH + '/tfrecords-jpeg-331x331',
512: GCS_DS_PATH + '/tfrecords-jpeg-512x512'
}
GCS_PATH = GCS_PATH_SELECT[IMAGE_SIZE[0]]
TRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/train/*.tfrec')
 VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec')
df_Population.iloc[np.r_[0:5, -6:-1], :]<rename_columns>
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']
df_Train.rename(columns={'Country_Region':'Country'}, inplace=True)
 df_test.rename(columns={'Country_Region':'Country'}, inplace=True)
 df_Train.rename(columns={'Province_State':'State'}, inplace=True)
 df_test.rename(columns={'Province_State':'State'}, inplace=True )<import_modules>
np.set_printoptions(threshold=15, linewidth=80)
 def batch_to_numpy_images_and_labels(data):
images, labels = data
numpy_images = images.numpy()
numpy_labels = labels.numpy()
if numpy_labels.dtype == object:
numpy_labels = [None for _ in enumerate(numpy_images)]
return numpy_images, numpy_labels
def title_from_label_and_target(label, correct_label):
if correct_label is None:
return CLASSES[label], True
correct =(label == correct_label)
 return "{} [{}{}{}]".format(CLASSES[label], 'OK' if correct else 'NO', u"\u2192" if not correct else '',
CLASSES[correct_label] if not correct else ''), correct
def display_one_flower(image, title, subplot, red=False, titlesize=16):
plt.subplot(*subplot)
 plt.axis('off')
 plt.imshow(image)
 if len(title)> 0:
plt.title(title, fontsize=int(titlesize)if not red else int(titlesize/1.2), color='red' if red else 'black', fontdict={'verticalalignment':'center'}, pad=int(titlesize/1.5))
return(subplot[0], subplot[1], subplot[2]+1)
 def display_batch_of_images(databatch, predictions=None):

 images, labels = batch_to_numpy_images_and_labels(databatch)
 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])) :
if label is None:
title = ''
elif isinstance(label, int):
title = CLASSES[label]
else:
idx = np.argmax(label, axis=0)
 title = CLASSES[idx]
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_flower(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()
def display_confusion_matrix(cmat, score, precision, recall):
plt.figure(figsize=(15,15))
ax = plt.gca()
ax.matshow(cmat, cmap='Reds')
 ax.set_xticks(range(len(CLASSES)))
 ax.set_xticklabels(CLASSES, fontdict={'fontsize': 7})
 plt.setp(ax.get_xticklabels() , rotation=45, ha="left", rotation_mode="anchor")
 ax.set_yticks(range(len(CLASSES)))
 ax.set_yticklabels(CLASSES, fontdict={'fontsize': 7})
 plt.setp(ax.get_yticklabels() , rotation=45, ha="right", rotation_mode="anchor")
 titlestring = ""
if score is not None:
titlestring += 'f1 = {:.3f} '.format(score)
 if precision is not None:
titlestring += '
precision = {:.3f} '.format(precision)
 if recall is not None:
titlestring += '
recall = {:.3f} '.format(recall)
 if len(titlestring)> 0:
ax.text(101, 1, titlestring, fontdict={'fontsize': 18, 'horizontalalignment':'right', 'verticalalignment':'top', 'color':'
plt.show()
def display_training_curves(training, validation, title, subplot):
if subplot%10==1:
plt.subplots(figsize=(10,10), facecolor='
plt.tight_layout()
ax = plt.subplot(subplot)
 ax.set_facecolor('
ax.plot(training)
 ax.plot(validation)
 ax.set_title('model '+ title)
 ax.set_ylabel(title)
 ax.set_xlabel('epoch')
 ax.legend(['train', 'valid.'] )
import plotly.graph_objects as go
import plotly.express as px
import matplotlib.pyplot as plt<rename_columns>
def get_batch_transformatioin_matrix(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):

 batch_size = tf.cast(tf.reduce_sum(tf.ones_like(rotation)) , tf.int64)
 rotation = tf.constant(math.pi)* rotation / 180.0
shear = tf.constant(math.pi)* shear / 180.0
one = tf.ones_like(rotation, dtype=tf.float32)
 zero = tf.zeros_like(rotation, dtype=tf.float32)
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 rotation_matrix_temp = tf.stack([c1, s1, zero, -s1, c1, zero, zero, zero, one], axis=0)
 rotation_matrix_temp = tf.transpose(rotation_matrix_temp)
 rotation_matrix = tf.reshape(rotation_matrix_temp, shape=(batch_size, 3, 3))
c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix_temp = tf.stack([one, s2, zero, zero, c2, zero, zero, zero, one], axis=0)
 shear_matrix_temp = tf.transpose(shear_matrix_temp)
 shear_matrix = tf.reshape(shear_matrix_temp, shape=(batch_size, 3, 3))
zoom_matrix_temp = tf.stack([one / height_zoom, zero, zero, zero, one / width_zoom, zero, zero, zero, one], axis=0)
 zoom_matrix_temp = tf.transpose(zoom_matrix_temp)
 zoom_matrix = tf.reshape(zoom_matrix_temp, shape=(batch_size, 3, 3))
shift_matrix_temp = tf.stack([one, zero, height_shift, zero, one, width_shift, zero, zero, one], axis=0)
 shift_matrix_temp = tf.transpose(shift_matrix_temp)
 shift_matrix = tf.reshape(shift_matrix_temp, shape=(batch_size, 3, 3))
return tf.linalg.matmul(tf.linalg.matmul(rotation_matrix, shear_matrix), tf.linalg.matmul(zoom_matrix, shift_matrix))
df_Population.rename(columns={'Country(or dependency)':'Country'}, inplace=True )<define_variables>
def batch_transform(images, labels):

 DIM = images.shape[1]
XDIM = DIM % 2
batch_size = tf.cast(tf.reduce_sum(tf.ones_like(images)) /(images.shape[1] * images.shape[2] * images.shape[3]), tf.int64)
 rot = 15.0 * tf.random.normal([batch_size], dtype='float32')
 shr = 5.0 * tf.random.normal([batch_size], dtype='float32')
 h_zoom = 1.0 + tf.random.normal([batch_size], dtype='float32')/ 10.0
w_zoom = 1.0 + tf.random.normal([batch_size], dtype='float32')/ 10.0
h_shift = 16.0 * tf.random.normal([batch_size], dtype='float32')
 w_shift = 16.0 * tf.random.normal([batch_size], dtype='float32')
 m = get_batch_transformatioin_matrix(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 = tf.linalg.matmul(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, ]], axis=1)
 d = tf.gather_nd(images, tf.transpose(idx3, perm=[0, 2, 1]), batch_dims=1)
 new_images = tf.reshape(d,(batch_size, DIM, DIM, 3))
return new_images, labels
renameCountryNames = {
"Congo(Brazzaville)": "Congo",
"Congo(Kinshasa)": "Congo",
"Cote d'Ivoire": "Côte d'Ivoire",
"Czechia": "Czech Republic(Czechia)",
"Korea, South": "South Korea",
"Saint Kitts and Nevis": "Saint Kitts & Nevis",
"Saint Vincent and the Grenadines": "St.Vincent & Grenadines",
"Taiwan*": "Taiwan",
"US": "United States"
}<categorify>
def onehot(image,label):
return image,tf.one_hot(label,len(CLASSES))
df_Train.replace({'Country': renameCountryNames}, inplace=True)
 df_test.replace({'Country': renameCountryNames}, inplace=True )<feature_engineering>
def batch_cutmix(images, labels, PROBABILITY=1.0, batch_size=0):
DIM = IMAGE_SIZE[0]
CLASSES = 104
if batch_size == 0:
batch_size = AUG_BATCH
do_cutmix = tf.cast(tf.random.uniform([batch_size], 0, 1)<= PROBABILITY, tf.int32)
 new_image_indices = tf.cast(tf.random.uniform([batch_size], 0, batch_size), tf.int32)
 new_x = tf.cast(tf.random.uniform([batch_size], 0, DIM), tf.int32)
 new_y = tf.cast(tf.random.uniform([batch_size], 0, DIM), tf.int32)
 b = tf.random.uniform([batch_size], 0, 1)
 new_width = tf.cast(DIM * tf.math.sqrt(1-b), tf.int32)* do_cutmix
new_y0 = tf.math.maximum(0, new_y - new_width // 2)
 new_y1 = tf.math.minimum(DIM, new_y + new_width // 2)
 new_x0 = tf.math.maximum(0, new_x - new_width // 2)
 new_x1 = tf.math.minimum(DIM, new_x + new_width // 2)
 target = tf.broadcast_to(tf.range(DIM), shape=(batch_size, DIM))
mask_y = tf.math.logical_and(new_y0[:, tf.newaxis] <= target, target <= new_y1[:, tf.newaxis])
 mask_x = tf.math.logical_and(new_x0[:, tf.newaxis] <= target, target <= new_x1[:, tf.newaxis])
 mask = tf.cast(tf.math.logical_and(mask_y[:, :, tf.newaxis], mask_x[:, tf.newaxis, :]), tf.float32)
 new_images = images * tf.broadcast_to(1 - mask[:, :, :, tf.newaxis], [batch_size, DIM, DIM, 3])+ \
tf.gather(images, new_image_indices)* tf.broadcast_to(mask[:, :, :, tf.newaxis], [batch_size, DIM, DIM, 3])
 a = tf.cast(new_width ** 2 / DIM ** 2, tf.float32)
 if len(labels.shape)== 1:
labels = tf.one_hot(labels, CLASSES)
 new_labels =(1-a)[:, tf.newaxis] * labels + a[:, tf.newaxis] * tf.gather(labels, new_image_indices)
 return new_images, new_labels
df_Population.loc[df_Population['Med.Age']=='N.A.', 'Med.Age'] = df_Population.loc[df_Population['Med.Age']!='N.A.', 'Med.Age'].mode() [0]
df_Population.loc[df_Population['Urban Pop %']=='N.A.', 'Urban Pop %'] = df_Population.loc[df_Population['Urban Pop %']!='N.A.', 'Urban Pop %'].mode() [0]
df_Population.loc[df_Population['Fert.Rate']=='N.A.', 'Fert.Rate'] = df_Population.loc[df_Population['Fert.Rate']!='N.A.', 'Fert.Rate'].mode() [0]
df_Population.loc[:, 'Migrants(net)'] = df_Population.loc[:, 'Migrants(net)'].fillna(0)
 df_Population['Yearly Change'] = df_Population['Yearly Change'].str.rstrip('%')
 df_Population['World Share'] = df_Population['World Share'].str.rstrip('%')
 df_Population['Urban Pop %'] = df_Population['Urban Pop %'].str.rstrip('%')
 df_Population = df_Population.astype({"Net Change": int,"Density(P/Km²)": int,"Population(2020)": int,"Land Area(Km²)": int,"Yearly Change": float,"Urban Pop %": int,"Fert.Rate": float,"Med.Age": int,"World Share": float, "Migrants(net)": float,})
 df_Population = df_Population.append(pd.Series(['Diamond Princess', 3500, 0, 0, 0, 0, 0.0, 1, 30, 0, 0.0], index=df_Population.columns), ignore_index=True )<filter>
def batch_mixup(images, labels, PROBABILITY=1.0, batch_size=0):
DIM = IMAGE_SIZE[0]
CLASSES = 104
if batch_size == 0:
batch_size = AUG_BATCH
do_mixup = tf.cast(tf.random.uniform([batch_size], 0, 1)<= PROBABILITY, tf.int32)
 new_image_indices = tf.cast(tf.random.uniform([batch_size], 0, batch_size), tf.int32)
 a = tf.random.uniform([batch_size], 0, 1)* tf.cast(do_mixup, tf.float32)
 new_images =(1-a)[:, tf.newaxis, tf.newaxis, tf.newaxis] * images + a[:, tf.newaxis, tf.newaxis, tf.newaxis] * tf.gather(images, new_image_indices)
 if len(labels.shape)== 1:
labels = tf.one_hot(labels, CLASSES)
 new_labels =(1-a)[:, tf.newaxis] * labels + a[:, tf.newaxis] * tf.gather(labels, new_image_indices)
 return new_images, new_labels
df_Population[df_Population['Population(2020)'] <= 5000]<merge>
def transform_cut_mix(image,label):
DIM = IMAGE_SIZE[0]
CLASSES = 104
SWITCH = 0.5
CUTMIX_PROB = 0.666
MIXUP_PROB = 0.666
image2, label2 = batch_cutmix(image, label, CUTMIX_PROB)
 image3, label3 = batch_mixup(image, label, MIXUP_PROB)
 imgs = []; labs = []
for j in range(AUG_BATCH):
P = tf.cast(tf.random.uniform([],0,1)<=SWITCH, tf.float32)
 imgs.append(P*image2[j,]+(1-P)*image3[j,])
 labs.append(P*label2[j,]+(1-P)*label3[j,])
 image4 = tf.reshape(tf.stack(imgs),(AUG_BATCH,DIM,DIM,3))
label4 = tf.reshape(tf.stack(labs),(AUG_BATCH,CLASSES))
return image4,label4
df_Train = df_Train.merge(df_Population, how='left', left_on='Country', right_on='Country')
 df_test = df_test.merge(df_Population, how='left', left_on='Country', right_on='Country' )<data_type_conversions>
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
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['class'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"id": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['id']
return image, idnum
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
def data_augment(image, label):
image = tf.image.random_flip_left_right(image)
 return image, label
def get_training_dataset(dataset, simple_aug=False, advance_aug=False, cut_mix_aug=0, do_onehot=True):
if simple_aug:
dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.repeat()
dataset = dataset.batch(AUG_BATCH)
 if advance_aug:
dataset = dataset.map(batch_transform, num_parallel_calls=AUTO)
 if do_onehot:
dataset = dataset.map(onehot, num_parallel_calls=AUTO)
 if cut_mix_aug==1:
dataset = dataset.map(batch_cutmix, num_parallel_calls=AUTO)
 elif cut_mix_aug==2:
dataset = dataset.map(batch_mixup, num_parallel_calls=AUTO)
 elif cut_mix_aug==3:
dataset = dataset.map(transform_cut_mix, num_parallel_calls=AUTO)
 dataset = dataset.unbatch()
dataset = dataset.shuffle(2048)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_validation_dataset(dataset, ordered=False, repeated=False, do_onehot=True):
if repeated:
dataset = dataset.repeat()
dataset = dataset.shuffle(2048)
 if do_onehot:
dataset = dataset.map(onehot, num_parallel_calls=AUTO)
 dataset = dataset.batch(BATCH_SIZE, drop_remainder=repeated)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 def int_div_round_up(a, b):
return(a + b - 1)// b
df_Train['Date'] = pd.to_datetime(df_Train['Date'], infer_datetime_format=True)
 df_test['Date'] = pd.to_datetime(df_test['Date'], infer_datetime_format=True )<feature_engineering>
if USE_EXTERNAL:
TRAINING_FILENAMES = TRAINING_FILENAMES + EXT_TRAINING_FILENAMES
if SKIP_VALIDATION:
TRAINING_FILENAMES = TRAINING_FILENAMES + VALIDATION_FILENAMES
NUM_TRAINING_IMAGES = count_data_items(TRAINING_FILENAMES)
 NUM_VALIDATION_IMAGES = count_data_items(VALIDATION_FILENAMES)if not SKIP_VALIDATION else 0
NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
VALIDATION_STEPS = int_div_round_up(NUM_VALIDATION_IMAGES, BATCH_SIZE)
 print('Dataset: {} training images, {} validation images, {} unlabeled test images'.format(NUM_TRAINING_IMAGES, NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))
MIN_TEST_DATE = df_test.Date.min()<filter>
train_ds = load_dataset(TRAINING_FILENAMES, labeled=True)
 valid_ds = load_dataset(VALIDATION_FILENAMES, labeled=True)if not SKIP_VALIDATION else None
test_ds = load_dataset(TEST_FILENAMES, labeled=False )
df_train = df_Train.loc[df_Train.Date < MIN_TEST_DATE, :]<something_strange>
print("Training data shapes:")
 for image, label in get_training_dataset(train_ds ).take(3):
print(image.numpy().shape, label.numpy().shape)
 print("Training data label examples:", label.numpy())
 if not SKIP_VALIDATION:
print("Validation data shapes:")
 for image, label in get_validation_dataset(valid_ds ).take(3):
print(image.numpy().shape, label.numpy().shape)
 print("Validation data label examples:", label.numpy())
 print("Test data shapes:")
 for image, idnum in get_test_dataset(test_ds ).take(3):
print(image.numpy().shape, idnum.numpy().shape)
 print("Test data IDs:", idnum.numpy().astype('U'))
EMPTY_VAL = "EMPTY_VAL"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state<feature_engineering>
if 0:
training_dataset = get_training_dataset(train_ds, simple_aug=True, advance_aug=False, cut_mix_aug=1)
 training_dataset = training_dataset.unbatch().batch(20)
 train_batch = iter(training_dataset )
X_Train = df_train.copy()
X_Train['State'].fillna(EMPTY_VAL, inplace=True)
 X_Train['State'] = X_Train.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 X_Train['year'] = X_Train['Date'].dt.year
X_Train['month'] = X_Train['Date'].dt.month
X_Train['week'] = X_Train['Date'].dt.week
X_Train['day'] = X_Train['Date'].dt.day
X_Train['dayofweek'] = X_Train['Date'].dt.dayofweek
X_Train.loc[:, 'Date'] = X_Train.Date.dt.strftime("%m%d")
 X_Train["Date"] = X_Train["Date"].astype(int)
 X_Train.head()<feature_engineering>
if 0:
display_batch_of_images(next(train_batch))
X_Test = df_test.copy()
X_Test['State'].fillna(EMPTY_VAL, inplace=True)
 X_Test['State'] = X_Test.loc[:, ['State', 'Country']].apply(lambda x : fillState(x['State'], x['Country']), axis=1)
 X_Test['year'] = X_Test['Date'].dt.year
X_Test['month'] = X_Test['Date'].dt.month
X_Test['week'] = X_Test['Date'].dt.week
X_Test['day'] = X_Test['Date'].dt.day
X_Test['dayofweek'] = X_Test['Date'].dt.dayofweek
X_Test.loc[:, 'Date'] = X_Test.Date.dt.strftime("%m%d")
 X_Test["Date"] = X_Test["Date"].astype(int)
 X_Test.head()<categorify>
if 0:
test_dataset = get_test_dataset(test_ds)
 test_dataset = test_dataset.unbatch().batch(20)
 test_batch = iter(test_dataset )
le = preprocessing.LabelEncoder()<categorify>
if 0:
display_batch_of_images(next(test_batch))
X_Train.Country = le.fit_transform(X_Train.Country)
 X_Train['State'] = le.fit_transform(X_Train['State'])
 X_Train.head()<categorify>
if 0:
images, labels = next(iter(training_dataset.take(1)))
 new_images, new_labels = batch_cutmix(images, labels, PROBABILITY=1.0, batch_size=20)
 display_batch_of_images(( new_images, new_labels))
X_Test.Country = le.fit_transform(X_Test.Country)
 X_Test['State'] = le.fit_transform(X_Test['State'])
 X_Test.head()<filter>
if 0:
images, labels = next(iter(training_dataset.take(1)))
 new_images, new_labels = batch_mixup(images, labels, PROBABILITY=1.0, batch_size=20)
 display_batch_of_images(( new_images, new_labels))
df_train.loc[df_train.Country == 'Afghanistan', :]<filter>
LR_START = 0.00003
LR_MAX = 0.00005 * strategy.num_replicas_in_sync
LR_MIN = 0.000001
LR_RAMPUP_EPOCHS = 5
LR_SUSTAIN_EPOCHS = 3
LR_EXP_DECAY =.8
@tf.function
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
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]))
X_Train.iloc[3990:4020]<set_options>
from tensorflow.keras.applications import DenseNet201
filterwarnings('ignore' )<train_on_grid>
with strategy.scope() :
pretrained_model = 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(len(CLASSES), activation='softmax')
 ])
 class LRSchedule(tf.keras.optimizers.schedules.LearningRateSchedule):
def __call__(self, step):
return lrfn(epoch=step//STEPS_PER_EPOCH)
 optimizer = tf.keras.optimizers.Adam(learning_rate=LRSchedule())
 model.compile(
 optimizer = optimizer,
loss = 'categorical_crossentropy',
metrics=['categorical_accuracy'] 
)
 model.summary()
train_accuracy = tf.keras.metrics.CategoricalAccuracy()
valid_accuracy = tf.keras.metrics.CategoricalAccuracy()
train_loss = tf.keras.metrics.Sum()
valid_loss = tf.keras.metrics.Sum()
loss_fn = lambda a,b: tf.nn.compute_average_loss(tf.keras.losses.categorical_crossentropy(a,b), global_batch_size=BATCH_SIZE )
<import_modules>
if 0:
history = model.fit(
 get_training_dataset(train_ds, simple_aug=True, advance_aug=False, cut_mix_aug=3),
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
validation_data=get_validation_dataset(valid_ds)if not SKIP_VALIDATION else None,
verbose=2
)
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn import metrics<compute_train_metric>
if 1:
if tpu:
STEPS_PER_TPU_CALL = 99
VALIDATION_STEPS_PER_TPU_CALL = 29
@tf.function
def train_step(data_iter):
def train_step_fn(images, labels):
with tf.GradientTape() as tape:
probabilities = model(images, training=True)
 loss = loss_fn(labels, probabilities)
 grads = tape.gradient(loss, model.trainable_variables)
 optimizer.apply_gradients(zip(grads, model.trainable_variables))
train_accuracy.update_state(labels, probabilities)
 train_loss.update_state(loss)
 for _ in tf.range(STEPS_PER_TPU_CALL):
strategy.experimental_run_v2(train_step_fn, next(data_iter))
@tf.function
def valid_step(data_iter):
def valid_step_fn(images, labels):
probabilities = model(images, training=False)
 loss = loss_fn(labels, probabilities)
 valid_accuracy.update_state(labels, probabilities)
 valid_loss.update_state(loss)
 for _ in tf.range(VALIDATION_STEPS_PER_TPU_CALL):
strategy.experimental_run_v2(valid_step_fn, next(data_iter))
def applyMLA(model, X_train, X_test, y_train, y_test):
start_time = time.time()
model.fit(X_train, y_train)
 end_time = time.time()
time2 = end_time-start_time
predictions = model.predict(X_test)
 RMSE_test = np.sqrt(metrics.mean_squared_error(y_test, predictions))
return [RMSE_test, time2]<categorify>
if 1:
start_time = epoch_start_time = time.time()
if tpu:
train_dist_ds = strategy.experimental_distribute_dataset(get_training_dataset(train_ds, simple_aug=True, advance_aug=False, cut_mix_aug=3))
valid_dist_ds = strategy.experimental_distribute_dataset(get_validation_dataset(valid_ds, repeated=True)) if not SKIP_VALIDATION else None
print("Training steps per epoch:", STEPS_PER_EPOCH, "in increment of:", STEPS_PER_TPU_CALL)
 if not SKIP_VALIDATION:
print("Validation images:", NUM_VALIDATION_IMAGES,
"Batch size:", BATCH_SIZE,
"Validation steps:", NUM_VALIDATION_IMAGES//BATCH_SIZE, "in increments of", VALIDATION_STEPS_PER_TPU_CALL)
 print("Repeated validation images:", int_div_round_up(NUM_VALIDATION_IMAGES, BATCH_SIZE*VALIDATION_STEPS_PER_TPU_CALL)*VALIDATION_STEPS_PER_TPU_CALL*BATCH_SIZE-NUM_VALIDATION_IMAGES)
 History = namedtuple('History', 'history')
 history = History(history={'loss': [], 'val_loss': [], 'categorical_accuracy': [], 'val_categorical_accuracy': []})if not SKIP_VALIDATION else History(history={'loss': [], 'val_loss': [], 'categorical_accuracy': []})
 epoch = 0
train_data_iter = iter(train_dist_ds)
 valid_data_iter = iter(valid_dist_ds)if not SKIP_VALIDATION else None
step = 0
epoch_steps = 0
while True:
train_step(train_data_iter)
 epoch_steps += STEPS_PER_TPU_CALL
step += STEPS_PER_TPU_CALL
print('=', end='', flush=True)
 if(step//STEPS_PER_EPOCH)> epoch:
print('|', end='', flush=True)
 if not SKIP_VALIDATION:
valid_epoch_steps = 0
valid_step(valid_data_iter)
 valid_epoch_steps += VALIDATION_STEPS_PER_TPU_CALL
print('=', end='', flush=True)
 history.history['val_categorical_accuracy'].append(valid_accuracy.result().numpy())
 history.history['val_loss'].append(valid_loss.result().numpy() / VALIDATION_STEPS)
 history.history['categorical_accuracy'].append(train_accuracy.result().numpy())
 history.history['loss'].append(train_loss.result().numpy() / STEPS_PER_EPOCH)
 epoch_time = time.time() - epoch_start_time
print('
EPOCH {:d}/{:d}'.format(epoch+1, EPOCHS))
if not SKIP_VALIDATION:
print('time: {:0.1f}s'.format(epoch_time),
'loss: {:0.4f}'.format(history.history['loss'][-1]),
'accuracy: {:0.4f}'.format(history.history['categorical_accuracy'][-1]),
'val_loss: {:0.4f}'.format(history.history['val_loss'][-1]),
'val_acc: {:0.4f}'.format(history.history['val_categorical_accuracy'][-1]),
'lr: {:0.4g}'.format(lrfn(epoch)) , flush=True)
 else:
print('time: {:0.1f}s'.format(epoch_time),
'loss: {:0.4f}'.format(history.history['loss'][-1]),
'accuracy: {:0.4f}'.format(history.history['categorical_accuracy'][-1]),
'lr: {:0.4g}'.format(lrfn(epoch)) , flush=True)
 epoch =(step+1)// STEPS_PER_EPOCH
epoch_start_time = time.time()
train_accuracy.reset_states()
if not SKIP_VALIDATION:
valid_accuracy.reset_states()
valid_loss.reset_states()
train_loss.reset_states()
if epoch >= EPOCHS:
break
else:
EPOCHS = 15
history = model.fit(
 get_training_dataset(train_ds),
steps_per_epoch=STEPS_PER_EPOCH,
epochs=EPOCHS,
validation_data=get_validation_dataset(valid_ds)if not SKIP_VALIDATION else None 
)
 simple_ctl_training_time = time.time() - start_time
print("OPTIMIZED CTL TRAINING TIME: {:0.1f}s".format(simple_ctl_training_time))
le = preprocessing.LabelEncoder()<prepare_x_and_y>
model.save_weights('densenet-flower-tpu.hdf5' )
countries = X_Train.Country.unique().tolist()
df_out = pd.DataFrame({'ForecastId': [], 'ConfirmedCases': [], 'Fatalities': []})
 for country in countries:
states = X_Train.loc[X_Train.Country == country, :].State.unique().tolist()
for state in states:
X_Train_CS = X_Train.loc[(X_Train.Country == country)&(X_Train.State == state), :]
y1_Train_CS = X_Train_CS.loc[:, 'ConfirmedCases']
y2_Train_CS = X_Train_CS.loc[:, 'Fatalities']
X_Train_CS.drop(columns=['Id', 'ConfirmedCases', 'Fatalities'], axis=1, inplace=True)
 X_Train_CS_PCA = X_Train_CS
X_Test_CS = X_Test.loc[(X_Test.Country == country)&(X_Test.State == state), :]
X_Test_CS_Id = X_Test_CS.loc[:, 'ForecastId']
X_Test_CS.drop(columns=['ForecastId'], axis=1, inplace=True)
 X_Test_CS_PCA = X_Test_CS
model1 = XGBRegressor(n_estimators=1250)
 model1.fit(X_Train_CS_PCA, y1_Train_CS)
 y1_pred = model1.predict(X_Test_CS_PCA)
 model2 = XGBRegressor(n_estimators=1000)
 model2.fit(X_Train_CS_PCA, y2_Train_CS)
 y2_pred = model2.predict(X_Test_CS_PCA)
 df = pd.DataFrame({'ForecastId': X_Test_CS_Id, 'ConfirmedCases': y1_pred, 'Fatalities': y2_pred})
 df_out = pd.concat([df_out, df], axis=0)
<data_type_conversions>
if not SKIP_VALIDATION:
cmdataset = get_validation_dataset(load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=True), ordered=True, do_onehot=False)
 images_ds = cmdataset.map(lambda image, label: image)
 labels_ds = cmdataset.map(lambda image, label: label ).unbatch()
cm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
cm_probabilities = model.predict(images_ds)
 cm_predictions = np.argmax(cm_probabilities, axis=-1)
 print("Correct labels: ", cm_correct_labels.shape, cm_correct_labels)
 print("Predicted labels: ", cm_predictions.shape, cm_predictions )
df_out.ForecastId = df_out.ForecastId.astype('int' )<save_to_csv>
if not SKIP_VALIDATION:
cmat = confusion_matrix(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)))
 score = f1_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 precision = precision_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 recall = recall_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 cmat =(cmat.T / cmat.sum(axis=1)).T
display_confusion_matrix(cmat, score, precision, recall)
 print('f1 score: {:.3f}, precision: {:.3f}, recall: {:.3f}'.format(score, precision, recall))
df_out.to_csv('submission.csv', index=False )<set_options>
test_ds = get_test_dataset(ordered=True)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities = model.predict(test_images_ds)
 predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 print('Generating submission.csv file...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions]), fmt=['%s', '%d'], delimiter=',', header='id,label', comments='')
 !head submission.csv
<load_from_csv><EOS> 
if 1 and not SKIP_VALIDATION:
dataset = get_validation_dataset(valid_ds, do_onehot=False)
 dataset = dataset.unbatch().batch(20)
 batch = iter(dataset)
 images, labels = next(batch)
 probabilities = model.predict(images)
 predictions = np.argmax(probabilities, axis=-1)
 display_batch_of_images(( images, labels), predictions )