Kaggle competition: Petals to the Metal - Flower Classification on TPU
<SOS> metric: macrofscore Kaggle data source: petals-to-the-metal-flower-classification-on-tpu<load_from_csv>
! pip install -q -U efficientnet
train= pd.read_csv('.. /input/train.csv')
 train['Label'] = 'train'
test = pd.read_csv('.. /input/test.csv')
 test['Label'] = 'test'
train.dropna(axis=0, subset=['Survived'], inplace=True)
 X_full = pd.concat([train.drop('Survived', axis = 1), test], axis = 0)
 X_full.drop('PassengerId', axis = 1, inplace=True)
 print("Categorical features: ", [cname for cname in X_full.columns if X_full[cname].dtype == "object"])
 print("Numeric features: ", [cname for cname in X_full.columns if X_full[cname].dtype in ['int64', 'float64']])
 X_full.head() 
<count_missing_values>
GCS_PATH = KaggleDatasets().get_gcs_path('tpu-getting-started')+ '/tfrecords-jpeg-512x512'
print(GCS_PATH )
missing_val_count_by_column =(X_full.isnull().sum())
 print(missing_val_count_by_column[missing_val_count_by_column > 0] )<categorify>
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
IMG_SIZE = [512, 512]
TRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/train/*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec')
 VALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')
 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']
X_full.drop('Name', axis=1, inplace=True)
 X_full.drop('Ticket', axis=1, inplace=True)
 X_full.drop('Cabin', axis=1, inplace=True)
 num_simple_imputer = SimpleImputer(strategy='median')
 numeric_features = ['Age', 'Fare', 'Pclass', 'SibSp', 'Parch']
num_transformed = pd.DataFrame(num_simple_imputer.fit_transform(X_full[numeric_features]))
num_transformed.columns = numeric_features
cat_simple_imputer = SimpleImputer(strategy='constant', fill_value='missing')
 categorical_features = ['Embarked','Sex', 'Label']
cat_transformed = pd.DataFrame(cat_simple_imputer.fit_transform(X_full[categorical_features]))
cat_transformed.columns = categorical_features
X_dummies = pd.get_dummies(cat_transformed, columns = categorical_features)
 X_full = pd.concat([num_transformed, X_dummies], axis = 1)
 print(X_full.dtypes)
 print(X_full.head() )<prepare_x_and_y>
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 NUM_TRAINING_IMAGES = count_data_items(TRAINING_FILENAMES)
 NUM_VALIDATION_IMAGES = count_data_items(VALIDATION_FILENAMES)
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 print('Dataset: {} training images, {} validation images, {} unlabeled test images'.format(NUM_TRAINING_IMAGES, NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))
X = X_full[X_full['Label_train'] == 1].copy()
X_test = X_full[X_full['Label_test'] == 1].copy()
X.drop('Label_train', axis=1, inplace=True)
 X.drop('Label_test', axis=1, inplace=True)
 X_test.drop('Label_test', axis=1, inplace=True)
 X_test.drop('Label_train', axis=1, inplace=True)
 y = train.Survived<split>
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image =(tf.cast(image, tf.float32)/ 127.5)- 1
image = tf.reshape(image, [*IMG_SIZE, 3])
 return image
def read_labeled_tfrec(example):
FORMAT = {
'image': tf.io.FixedLenFeature([], tf.string),
'class': tf.io.FixedLenFeature([], tf.int64)
 }
example = tf.io.parse_single_example(example, FORMAT)
 return decode_image(example['image']), tf.cast(example['class'], tf.int32)
 def read_unlabeled_tfrec(example):
FORMAT = {
'image': tf.io.FixedLenFeature([], tf.string),
'id': tf.io.FixedLenFeature([], tf.string)
 }
example = tf.io.parse_single_example(example, FORMAT)
 return decode_image(example['image']), example['id']
X_train, X_valid, y_train, y_valid = train_test_split(X, y, train_size=0.7, test_size=0.3,
random_state=0 )<choose_model_class>
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=tf.data.experimental.AUTOTUNE ).with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrec if labeled else read_unlabeled_tfrec, num_parallel_calls=tf.data.experimental.AUTOTUNE)
 return dataset
model = XGBClassifier(random_state = 18)
 model.fit(X_train, y_train)
 y_pred = model.predict(X_valid)
 predictions = [round(value)for value in y_pred]
accuracy = accuracy_score(y_valid, predictions)
 print("Accuracy: %.2f%%" %(accuracy * 100.0))
thresholds = sort(model.feature_importances_)
 models = []
for thresh in thresholds:
selection = SelectFromModel(model, threshold=thresh, prefit=True)
 select_X_train = selection.transform(X_train)
 selection_model = XGBClassifier()
selection_model.fit(select_X_train, y_train)
 models.append([selection_model, selection])
 select_X_valid = selection.transform(X_valid)
 select_y_pred = selection_model.predict(select_X_valid)
 predictions = [round(value)for value in select_y_pred]
accuracy = accuracy_score(y_valid, predictions)
 print("Thresh=%.3f, n=%d, Accuracy: %.2f%%" %(thresh, select_X_train.shape[1], accuracy*100.0))
print("MAE: ", mean_absolute_error(y_valid, predictions)) 
<compute_test_metric>
def data_augment(image, label):
image = tf.image.random_flip_left_right(image)
 image = tf.image.random_flip_up_down(image)
 return image, label
final_model = models[3][0]
final_selection = models[3][1]
final_X_train = final_selection.transform(X_train)
 final_X_test = final_selection.transform(X_test)
 final_X_valid = final_selection.transform(X_valid)
 final_y_pred = final_model.predict(final_X_valid)
 final_predictions = [round(value)for value in final_y_pred]
accuracy = accuracy_score(y_valid, final_predictions)
 print("n=%d, Accuracy: %.2f%%" %(final_X_train.shape[1], accuracy*100.0))
print("MAE: ", mean_absolute_error(y_valid, final_predictions))<train_on_grid>
def get_training_dataset() :
dataset = load_dataset(TRAINING_FILENAMES ).map(data_augment, num_parallel_calls=tf.data.experimental.AUTOTUNE)
 dataset = dataset.shuffle(2048 ).batch(BATCH_SIZE ).prefetch(tf.data.experimental.AUTOTUNE)
 return dataset
def get_validation_dataset(ordered=False):
dataset = load_dataset(VALIDATION_FILENAMES, ordered=ordered ).batch(BATCH_SIZE ).cache().prefetch(tf.data.experimental.AUTOTUNE)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered ).batch(BATCH_SIZE ).cache().prefetch(tf.data.experimental.AUTOTUNE)
 return dataset
grid_param = {
'n_estimators': [12, 25, 50, 75],
'max_depth': [3, 4, 5],
'learning_rate': [0.01, 0.05, 0.1],
'early_stopping_rounds': [3, 4, 5, 6]
}
gd_sr = GridSearchCV(estimator = final_model, param_grid = grid_param,
cv = 3, n_jobs = -1, verbose = 2)
 gd_sr.fit(X_train, y_train)
 best_parameters = gd_sr.best_params_
print(best_parameters )<compute_train_metric>
ds_train = get_training_dataset()
ds_valid = get_validation_dataset()
ds_test = get_test_dataset()
print("Training:", ds_train)
 print("Validation:", ds_valid)
 print("Test:", ds_test )
selection = SelectFromModel(model, threshold=0.027, prefit=True)
 select_X_train = selection.transform(X_train)
 select_X_test = selection.transform(X_test)
 select_X_valid = selection.transform(X_valid)
 another_model = XGBClassifier(early_stopping_rounds=3, learning_rate=0.01, max_depth=5, n_estimators=75)
 another_model.fit(select_X_train, y_train)
 select_y_pred = another_model.predict(select_X_valid)
 select_predictions = [round(value)for value in select_y_pred]
accuracy = accuracy_score(y_valid, select_predictions)
 print("n=%d, Accuracy: %.2f%%" %(select_X_train.shape[1], accuracy*100.0))
print("MAE: ", mean_absolute_error(y_valid, select_predictions))<train_model>
with strategy.scope() :
pretrained_model = DenseNet121(
 include_top=False,
weights='imagenet',
input_shape=[*IMG_SIZE, 3] 
)
 pretrained_model.trainable = True
model = tf.keras.Sequential([
pretrained_model,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ] )
random_forest = RandomForestClassifier(n_estimators=100)
 random_forest.fit(X_train, y_train)
 Y_pred_rf = random_forest.predict(X_test)
 random_forest.score(X_train, y_train )<compute_test_metric>
with strategy.scope() :
pretrained_model = efn.EfficientNetB7(include_top=False,
weights='imagenet',
input_shape=[*IMG_SIZE, 3])
 pretrained_model.trainable = True
model = tf.keras.Sequential([
pretrained_model,
tf.keras.layers.GlobalAveragePooling2D() ,
tf.keras.layers.Dense(len(CLASSES), activation='softmax')
 ] )
confusion_matrix(y_valid, final_y_pred )<save_to_csv>
model.compile(
 optimizer='adam',
loss = 'sparse_categorical_crossentropy',
metrics=['sparse_categorical_accuracy'],
)
final_preds = random_forest.predict(X_test)
 output = pd.DataFrame({'PassengerId':test.PassengerId,'Survived': final_preds})
 print(output.head(15))
output.to_csv('submission.csv', index=False )<set_options>
def exponential_lr(epoch,
start_lr = 0.00001, min_lr = 0.00001, max_lr = 0.00005,
rampup_epochs = 10, sustain_epochs = 0,
exp_decay = 0.8):
if epoch < rampup_epochs:
lr =(( max_lr - start_lr)/ rampup_epochs * epoch + start_lr)
 elif epoch < rampup_epochs + sustain_epochs:
lr = max_lr
else:
lr =(( max_lr - min_lr)*
exp_decay**(epoch - rampup_epochs - sustain_epochs)+ min_lr)
 return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(exponential_lr, verbose=True )
warnings.filterwarnings('ignore' )<load_from_csv>
EPOCHS = 20
history = model.fit(
 ds_train,
validation_data=ds_valid,
epochs=EPOCHS,
callbacks=[lr_callback]
)
train_data = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test_data = pd.read_csv('.. /input/digit-recognizer/test.csv')
 train_data.shape, test_data.shape<prepare_x_and_y>
cmdataset = get_validation_dataset(ordered=True)
 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)
 labels = range(len(CLASSES))
cmat = confusion_matrix(
 cm_correct_labels,
cm_predictions,
labels=labels, 
)
 cmat =(cmat.T / cmat.sum(axis=1)).T
train_data = train_data / 255.0
test_data = test_data / 255.0
x_train = train_data.values.reshape(-1, 28, 28, 1)
 x_test = test_data.values.reshape(-1, 28, 28,1 )<prepare_x_and_y>
score = f1_score(
 cm_correct_labels,
cm_predictions,
labels=labels,
average='macro', 
)
 precision = precision_score(
 cm_correct_labels,
cm_predictions,
labels=labels,
average='macro', 
)
 recall = recall_score(
 cm_correct_labels,
cm_predictions,
labels=labels,
average='macro', 
)
 display_confusion_matrix(cmat, score, precision, recall )
y_train = to_categorical(train_labels, 10 )<split>
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 )
<choose_model_class><EOS> 
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