Kaggle competition: Flower Classification with TPUs
<SOS> metric: MacroFScore Kaggle data source: flower-classification-with-tpus<import_modules>
from xgboost import XGBClassifier<choose_model_class>
!pip install efficientnet
new_model = XGBClassifier(n_estimators = 1000, learning_rate=0.05 )<train_model>
AUTO = tf.data.experimental.AUTOTUNE
AUTO, tf.__version__
new_model.fit(X_train,y_train,
early_stopping_rounds = 5,
eval_set = [(X_test,y_test)],
verbose = False )<predict_on_test>
IMAGE_SIZE = [512, 512]
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
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']
xgb_predictions = new_model.predict(X_test )<compute_test_metric>
GCS_DS_PATH = KaggleDatasets().get_gcs_path()
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')+ tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec' )
accuracy_score(xgb_predictions,y_test )<predict_on_test>
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n )
test_predictions = new_model.predict(test.values )<load_from_csv>
NUM_TRAINING_IMAGES = count_data_items(TRAINING_FILENAMES)
 NUM_VALIDATION_IMAGES = count_data_items(VALIDATION_FILENAMES)
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
BATCH_SIZE, STEPS_PER_EPOCH, NUM_TRAINING_IMAGES, NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES
xgb_submission= pd.read_csv('/kaggle/input/titanic/gender_submission.csv' )<prepare_output>
def data_augment(image, label):
image = tf.image.random_flip_left_right(image)
 return(image), label
xgb_submission['Survived'] = test_predictions<save_to_csv>
def get_mat(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):
rotation = math.pi * rotation / 180.
 shear = math.pi * shear / 180.
 c1 = tf.math.cos(rotation)
 s1 = tf.math.sin(rotation)
 one = tf.constant([1],dtype='float32')
 zero = tf.constant([0],dtype='float32')
 rotation_matrix = tf.reshape(tf.concat([c1,s1,zero, -s1,c1,zero, zero,zero,one],axis=0),[3,3])
 c2 = tf.math.cos(shear)
 s2 = tf.math.sin(shear)
 shear_matrix = tf.reshape(tf.concat([one,s2,zero, zero,c2,zero, zero,zero,one],axis=0),[3,3])
 zoom_matrix = tf.reshape(tf.concat([one/height_zoom,zero,zero, zero,one/width_zoom,zero, zero,zero,one],axis=0),[3,3])
 shift_matrix = tf.reshape(tf.concat([one,zero,height_shift, zero,one,width_shift, zero,zero,one],axis=0),[3,3])
 return K.dot(K.dot(rotation_matrix, shear_matrix), K.dot(zoom_matrix, shift_matrix))
xgb_submission.to_csv('gender_submission.csv',index=False )<set_options>
def transform(image, label):
DIM = IMAGE_SIZE[0]
XDIM = DIM%2
rot = 15.* tf.random.normal([1],dtype='float32')
 shr = 5.* tf.random.normal([1],dtype='float32')
 h_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 w_zoom = 1.0 + tf.random.normal([1],dtype='float32')/10.
 h_shift = 16.* tf.random.normal([1],dtype='float32')
 w_shift = 16.* tf.random.normal([1],dtype='float32')
 m = get_mat(rot,shr,h_zoom,w_zoom,h_shift,w_shift)
 x = tf.repeat(tf.range(DIM//2,-DIM//2,-1), DIM)
 y = tf.tile(tf.range(-DIM//2,DIM//2),[DIM])
 z = tf.ones([DIM*DIM],dtype='int32')
 idx = tf.stack([x,y,z])
 idx2 = K.dot(m,tf.cast(idx,dtype='float32'))
idx2 = K.cast(idx2,dtype='int32')
 idx2 = K.clip(idx2,-DIM//2+XDIM+1,DIM//2)
 idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])
 d = tf.gather_nd(image,tf.transpose(idx3))
return tf.reshape(d,[DIM,DIM,3]),label
warnings.filterwarnings(action='ignore', category=DataConversionWarning)
 warnings.filterwarnings(action='ignore', category=DeprecationWarning)
 warnings.filterwarnings(action='ignore', category=FutureWarning )<load_from_csv>
def read_labelled_tfrec(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 = tf.image.decode_jpeg(example['image'], channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 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 = tf.image.decode_jpeg(example['image'], channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 idnum = example['id']
return image, idnum
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 data = train.append(test, sort=False )<create_dataframe>
def read_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_labelled_tfrec if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset
def read_train_data() :
dataset = read_dataset(TRAINING_FILENAMES, labeled=True)
 dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 return dataset.repeat().shuffle(2048, reshuffle_each_iteration = True ).batch(BATCH_SIZE ).prefetch(AUTO)
 def read_validation_data(ordered=False):
return read_dataset(VALIDATION_FILENAMES, labeled=True, ordered=ordered ).batch(BATCH_SIZE ).cache().prefetch(AUTO)
 def read_test_data(ordered=False):
return read_dataset(TEST_FILENAMES, labeled=False, ordered=ordered ).batch(BATCH_SIZE ).prefetch(AUTO )
types = pd.DataFrame(data.dtypes ).rename(columns={0: 'type'} ).sort_values(by=['type'],ascending=False)
 types<count_missing_values>
def get_training_dataset(dataset,do_aug=True):
dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 if do_aug: dataset = dataset.map(transform, num_parallel_calls=AUTO)
 dataset = dataset.repeat()
dataset = dataset.shuffle(2048)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_validation_dataset(dataset):
dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset
def check_missing(df):
null_val = df.isnull().sum()
percent = 100 * df.isnull().sum() /len(df)
 missing_table = pd.concat([null_val, percent], axis=1)
 col = missing_table.rename(columns = {0 : 'Num', 1 : 'Rate'})
 return col
print("Data
cols = check_missing(data)
 types.join(cols ).sort_values(by="Rate", ascending=False )<categorify>
LR_START = 0.0001
LR_MAX = 0.00005 * strategy.num_replicas_in_sync
LR_MIN = 0.00001
LR_RAMPUP_EPOCHS = 4
LR_SUSTAIN_EPOCHS = 6
LR_EXP_DECAY =.8
def lr_scheduler_2(epoch):
if epoch < LR_RAMPUP_EPOCHS:
lr = np.random.random_sample() * 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
data.drop(['Cabin'], axis=1, inplace = True)
 data["Embarked"] = data["Embarked"].fillna(data["Embarked"].mode() [0])
 data["Embarked"] = data["Embarked"].map({"S": 0, "C" : 1, "Q" : 2})
 title_mapping = {
' (.+)Mr\.(.+)': 1, ' (.+)Master\.(.+)': 1,
' (.+)Dr\.(.+)': 2, ' (.+)Don\.(.+)': 2, ' (.+)Major\.(.+)': 2,
' (.+)Sir\.(.+)':2, ' (.+)Col\.(.+)': 2, ' (.+)Jonkheer\.(.+)': 2,
' (.+)Capt\.(.+)': 2,' (.+)Countess\.(.+)': 2, ' (.+)Dona\.(.+)': 2,
' (.+)Rev\.(.+)': 3,
' (.+)Ms\.(.+)': 4, ' (.*)Miss\.(.+)': 4, ' (.+)Mrs\.(.+)': 4,
' (.+)Mme\.(.+)': 4,' (.+)Lady\.(.+)': 4, ' (.+)Mlle\.(.+)': 4
}
data["Title"] = data["Name"].replace(title_mapping, regex=True ).astype('int')
 data["Sex"] = data["Sex"].map({"male": 0, "female": 1} )<feature_engineering>
lr_callback = tf.keras.callbacks.LearningRateScheduler(lr_scheduler_2, verbose = True )
data['FamilySize'] = data["Parch"] + data["SibSp"]
data['IsFamily'] = data["Parch"] + data["SibSp"]
data.loc[data['IsFamily'] > 1, 'IsFamily'] = 2
data.loc[data['IsFamily'] == 1, 'IsFamily'] = 1
data.loc[data['IsFamily'] == 0, 'IsFamily'] = 0
DEFAULT_SURVIVAL_VALUE = 0.5
data['FamilySurvival'] = DEFAULT_SURVIVAL_VALUE
data['LastName'] = data['Name'].apply(lambda x: str.split(x, ",")[0])
 for grp, grp_df in data.groupby(['LastName', 'Fare']):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
data.loc[data['PassengerId'] == passID, 'FamilySurvival'] = 1
elif(smin==0.0):
data.loc[data['PassengerId'] == passID, 'FamilySurvival'] = 0
for _, grp_df in data.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['FamilySurvival'] == 0)|(row['FamilySurvival']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
data.loc[data['PassengerId'] == passID, 'FamilySurvival'] = 1
elif(smin==0.0):
data.loc[data['PassengerId'] == passID, 'FamilySurvival'] = 0<categorify>
def tta(model, TTA_NUM = 10):
probabilities = []
for i in range(TTA_NUM):
print('TTA Number: ',i)
 test_ds = read_test_data(ordered=True)
 test_images_ds = test_ds.map(lambda image, idnum: image)
 gc.collect()
probabilities.append(model.predict(test_images_ds,verbose =1))
prob2 = np.mean(probabilities,axis =0)
 return prob2
train_target = data[:891]["Survived"].values
data.drop(['Name', 'PassengerId', 'Age', 'Fare', 'Ticket', 'LastName'], axis = 1, inplace = True)
 data = pd.get_dummies(data, columns=["Embarked", "Title", "Sex", "IsFamily", "FamilySurvival"], drop_first=True)
 train = data[:891]
test = data[891:]
data.dtypes<train_model>
def train_and_predict(models, TTA_NUM = 10):
test_ds = read_test_data(ordered=True)
 test_images_ds = test_ds.map(lambda image, idnum: image)
 gc.collect()
print('Computing predictions...')
 print("TTA")
 probability =([tta(models[i], TTA_NUM)for i in range(len(models)) ])
 probabilities = np.average(probability, axis = 0)
 predictions = np.argmax(probabilities, axis=-1)
 gc.collect()
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='')
 return models
possible_features = train.columns.copy().drop('Survived')
 selector = SelectKBest(f_classif, len(possible_features))
selector.fit(train[possible_features], train_target)
 scores = -np.log10(selector.pvalues_)
 indices = np.argsort(scores)[::-1]
print('Feature importances:')
 for i in range(len(scores)) :
print('%.2f %s' %(scores[indices[i]], possible_features[indices[i]]))<define_search_space>
def get_model1() :
with strategy.scope() :
enet = efn.EfficientNetB7(
 input_shape=(IMAGE_SIZE[0], IMAGE_SIZE[0], 3),
weights='noisy-student',
include_top=False 
)
 model = tf.keras.Sequential([
enet,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(classes), activation='softmax')
 ])
 model.compile(
 optimizer=tf.keras.optimizers.Adam() ,
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 return model
fparams = \
['Sex_1', 'Title_4', 'FamilySurvival_1.0', 'Pclass', 'FareBin',
'FamilySurvival_0.5', 'Embarked_1', 'IsFamily_1', 'IsFamily_2',
'Parch', 'Title_3', 'AgeBin', 'SibSp']
train_features = train[fparams].values
test_features = test[fparams].values
CV_SPLIT_NUM = 6
rfgs_parameters = {
'n_estimators': [300],
'max_depth' : [2,3,4],
'max_features': [2,3,4],
"min_samples_split": [2,3,4],
"min_samples_leaf": [2,3,4]
}
rfc_cv = GridSearchCV(RandomForestClassifier() , rfgs_parameters, cv=CV_SPLIT_NUM)
 rfc_cv.fit(train_features, train_target)
 print("RFC GridSearch score: "+str(rfc_cv.best_score_))
print("RFC GridSearch params: ")
 print(rfc_cv.best_params_ )<save_to_csv>
def get_model2() :
with strategy.scope() :
enet = tf.keras.applications.DenseNet201(weights='imagenet',
include_top=False,
input_shape=[*IMAGE_SIZE, 3])
 model = tf.keras.Sequential([
enet,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(classes), activation='softmax')
 ])
 model.compile(
 optimizer=tf.keras.optimizers.Adam() ,
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'] 
)
 return model
survived = rfc_cv.best_estimator_.predict(test_features)
 pred = pd.DataFrame(pd.read_csv(".. /input/titanic/test.csv")['PassengerId'])
 pred['Survived'] = survived.astype(int)
 pred.to_csv(".. /working/submission.csv", index = False )<set_options>
SEED = 25
TTA_NUM = 10
EPOCHS_1 = 32
EPOCHS_2 = 20
warnings.filterwarnings('ignore')
<load_from_csv>
models = []
train = pd.read_csv(".. /input/train.csv")
 taille_train = len(train)
 labels = train['Survived']
test = pd.read_csv(".. /input/test.csv")
 all_data = pd.concat([train, test],ignore_index=True, sort='True' )<count_missing_values>
gc.collect()
enet_model = get_model1()
history = enet_model.fit(read_train_data() , steps_per_epoch=STEPS_PER_EPOCH, epochs=EPOCHS_1,
callbacks = [lr_callback])
 models.append(enet_model )
all_data.isnull().sum()<sort_values>
gc.collect()
dnet_model = get_model2()
history = dnet_model.fit(read_train_data() , steps_per_epoch=STEPS_PER_EPOCH, epochs=EPOCHS_2,
callbacks = [lr_callback])
 models.append(dnet_model )
<sort_values><EOS> 
model_h = train_and_predict(models, TTA_NUM )