Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<train_model>
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import cv2
import tensorflow as tf
from tensorflow import keras
for example in range(num_examples_train):
Train_input_images[example] = cv2.resize(Train_input_images[example],(n_h, n_w))
for example in range(num_examples_test):
Test_input_images[example] = cv2.resize(Test_input_images[example],(n_h, n_w))<define_variables>
test_batch_size = 8
image_size = 512
Train_labels = np.array(train_data.iloc[:, 0] )<choose_model_class>
PATH = '.. /input/cassava-leaf-disease-classification/'
sub_df = pd.read_csv(PATH + 'sample_submission.csv' )
image_generator = ImageDataGenerator(
 rotation_range=27,
width_shift_range=0.3,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=False,
samplewise_center=True,
samplewise_std_normalization=True 
)
 validation_datagen = ImageDataGenerator()<choose_model_class>
sub_df["path"] = PATH + 'test_images/' + sub_df["image_id"]
pretrained_model = keras.applications.resnet50.ResNet50(input_shape=(n_h, n_w, n_c),
include_top=False, weights='imagenet')
 model = keras.Sequential([
pretrained_model,
keras.layers.Flatten() ,
keras.layers.Dense(units=60, activation='relu'),
keras.layers.Dense(units=10, activation='softmax')
 ] )<choose_model_class>
test_ds = tf.data.Dataset.from_tensor_slices(( sub_df.path.values, sub_df.label.values))
Optimizer = 'RMSprop'
model.compile(optimizer=Optimizer,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'] )<split>
AUTOTUNE = tf.data.experimental.AUTOTUNE
train_images, dev_images, train_labels, dev_labels = train_test_split(Train_input_images,
Train_labels,
test_size=0.1, train_size=0.9,
shuffle=True,
random_state=44)
 test_images = Test_input_images<define_search_model>
def test_aug(image):
data = {"image":image}
aug_data = test_transforms(**data)
 aug_img = aug_data["image"]
aug_img = tf.cast(aug_img, tf.float32)
 return aug_img
train_datagen = ImageDataGenerator(
 rotation_range=27,
width_shift_range=0.3,
height_shift_range=0.2,
shear_range=0.3,
zoom_range=0.2,
horizontal_flip=False)
 validation_datagen = ImageDataGenerator()<train_model>
def process_data(image_path, label, func_aug):
image = tf.io.read_file(image_path)
 image = tf.image.decode_jpeg(image)
 image = tf.image.convert_image_dtype(image, tf.float32)
 aug_img = tf.numpy_function(func=func_aug, inp=[image], Tout=tf.float32)
 return aug_img, label
class myCallback(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('accuracy')> 0.999999):
print("Stop training!")
 self.model.stop_training = True<train_model>
test_ds_alb = test_ds.map(partial(process_data, func_aug=test_aug), num_parallel_calls=AUTOTUNE ).prefetch(AUTOTUNE )
EPOCHS = 5
batch_size = 212
history = model.fit_generator(train_datagen.flow(train_images,train_labels, batch_size=batch_size),
steps_per_epoch=train_images.shape[0] / batch_size,
epochs=EPOCHS,
validation_data=validation_datagen.flow(dev_images,dev_labels,
batch_size=batch_size),
validation_steps=dev_images.shape[0] / batch_size,
callbacks=[callbacks] )<save_to_csv>
test_ds_alb = test_ds_alb.map(set_shapes, num_parallel_calls=AUTOTUNE ).batch(8 )
submission = pd.read_csv('.. /input/digit-recognizer-submission/submission.csv')
 submission.to_csv('digit_submission.csv', index=False )<load_from_csv>
os.system('pip install /kaggle/input/kerasapplications -q')
 os.system('pip install /kaggle/input/efficientnet-keras-source-code/ -q --no-deps' )
mnist_train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 mnist_test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" )<categorify>
efficientnet = efn.EfficientNetB4(weights=None, include_top=False,
drop_connect_rate=0.3, input_shape=(image_size, image_size, 3))
inputs = Input(shape=(image_size, image_size, 3))
efficientnet = efficientnet(inputs)
 pooling = GlobalAveragePooling2D()(efficientnet)
 dropout = Dropout(0.3 )(pooling)
 outputs = Dense(5, activation="softmax" )(dropout)
 model = Model(inputs=inputs, outputs=outputs )
mnist_train_data = mnist_train.loc[:, "pixel0":]
mnist_train_label = mnist_train.loc[:, "label"]
mnist_train_data = mnist_train_data/255.0
mnist_test = mnist_test/255.0<normalization>
preds = []
for fold in [2,3]:
model.load_weights('.. /input/effnetb45128weights/EffNetB4_512_8_weights_fold_{}.h5'.format(fold+1))
preds.append(model.predict(test_ds_alb, workers=4, verbose=1))
standardized_scalar = StandardScaler()
standardized_data = standardized_scalar.fit_transform(mnist_train_data)
 standardized_data.shape<train_model>
preds = []
for fold in [2,3]:
model.load_weights('.. /input/effnetb45128weights/EffNetB4_512_8_weights_fold_{}.h5'.format(fold+1))
fold_preds = []
for i in range(5):
fold_preds.append(model.predict(test_ds_alb, workers=4, verbose=1))
preds.append(np.mean(fold_preds, axis=0))
cov_matrix = np.matmul(standardized_data.T, standardized_data)
 cov_matrix.shape<compute_train_metric>
preds_avg = np.mean(preds, axis=0 )
lambdas, vectors = eigh(cov_matrix, eigvals=(782, 783))
vectors.shape<concatenate>
y_pred = np.argmax(preds_avg, axis=-1 )
new_coordinates = np.matmul(vectors, standardized_data.T)
 print(new_coordinates.shape)
 new_coordinates = np.vstack(( new_coordinates, mnist_train_label)).T<prepare_output>
sub_df['label'] = y_pred
df_new = pd.DataFrame(new_coordinates, columns=["f1", "f2", "labels"])
 df_new.head()<normalization>
sub_df.drop(['path'], axis=1, inplace=True )
<concatenate><EOS> 
sub_df.to_csv('submission.csv', index=False )