Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<prepare_output>
%%capture
!pip install.. /input/timm-pip/timm-0.3.1-py3-none-any.whl
df_PCA = pd.DataFrame(new_coordinates, columns=["f1", "f2", "labels"])
 df_PCA.head()<create_dataframe>
tf.__version__
mnist_train_data = np.array(mnist_train_data)
 mnist_train_label = np.array(mnist_train_label )<import_modules>
train_df = pd.read_csv(dataset_path + "/" + 'train.csv')
 train_df.head()
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Lambda, Flatten, BatchNormalization
from tensorflow.keras.layers import Conv2D, MaxPool2D, AvgPool2D
from tensorflow.keras.optimizers import Adadelta
from keras.utils.np_utils import to_categorical
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.callbacks import LearningRateScheduler<categorify>
set_seed(999)
 dataset_path = Path('.. /input/cassava-leaf-disease-classification')
 os.listdir(dataset_path)
 BASE_DIR = ".. /input/cassava-leaf-disease-classification/"
train_df = pd.read_csv(".. /input/cassava-leaf-disease-classification/train.csv" )
nclasses = mnist_train_label.max() - mnist_train_label.min() + 1
mnist_train_label = to_categorical(mnist_train_label, num_classes = nclasses)
 print("Shape of ytrain after encoding: ", mnist_train_label.shape )<choose_model_class>
len_df = len(train_df)
 print(f"There are {len_df} images" )
def build_model(input_shape=(28, 28, 1)) :
model = Sequential()
model.add(Conv2D(32, kernel_size = 3, activation='relu', input_shape = input_shape))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128, kernel_size = 4, activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
return model
def compile_model(model, optimizer='adam', loss='categorical_crossentropy'):
model.compile(optimizer=optimizer, loss=loss, metrics=["accuracy"])
 def train_model(model, train, test, epochs, split):
history = model.fit(train, test, shuffle=True, epochs=epochs, validation_split=split)
 return history<train_model>
with open(os.path.join(BASE_DIR, "label_num_to_disease_map.json")) as file:
map_classes = json.loads(file.read())
 map_classes = {int(k): v for k, v in map_classes.items() }
print(json.dumps(map_classes, indent=4))
train_df = pd.read_csv(os.path.join(BASE_DIR, "train.csv"))
train_df["class_name"] = train_df["label"].map(map_classes)
 train_df['path'] = train_df['image_id'].map(lambda x:dataset_path/'train_images'/x)
 train_df = train_df.drop(columns=['image_id'])
 train_df = train_df.sample(frac=1 ).reset_index(drop=True )
cnn_model = build_model(( 28, 28, 1))
compile_model(cnn_model, 'adam', 'categorical_crossentropy')
 model_history = train_model(cnn_model, mnist_train_data, mnist_train_label, 80, 0.2 )<predict_on_test>
train_df = train_df[["label", "path"]]
predictions = cnn_model.predict(mnist_test_arr )<define_variables>
im = Image.open(train_df['path'][1])
 width, height = im.size
print(width,height )
predictions_test = []
for i in predictions:
predictions_test.append(np.argmax(i))<save_to_csv>
dls = ImageDataLoaders.from_df(train_df,
valid_pct=0.2,
seed=999,
label_col=0,
fn_col=1,
bs=bs,
item_tfms=item_tfms,
batch_tfms=batch_tfms )
submission = pd.DataFrame({
"ImageId": mnist_test.index+1,
"Label": predictions_test
})
 submission.to_csv('my_first_submission.csv', index=False )<import_modules>
def create_timm_body(arch:str, pretrained=True, cut=None, n_in=3):
"Creates a body from any model in the `timm` library."
model = create_model(arch, pretrained=pretrained, num_classes=0, global_pool='')
 _update_first_layer(model, n_in, pretrained)
 if cut is None:
ll = list(enumerate(model.children()))
cut = next(i for i,o in reversed(ll)if has_pool_type(o))
if isinstance(cut, int): return nn.Sequential(*list(model.children())[:cut])
 elif callable(cut): return cut(model)
 else: raise NamedError("cut must be either integer or function")
 def create_timm_model(arch:str, n_out, cut=None, pretrained=True, n_in=3, init=nn.init.kaiming_normal_, custom_head=None,
concat_pool=True, **kwargs):
"Create custom architecture using `arch`, `n_in` and `n_out` from the `timm` library"
body = create_timm_body(arch, pretrained, None, n_in)
 if custom_head is None:
nf = num_features_model(nn.Sequential(*body.children())) *(2 if concat_pool else 1)
 head = create_head(nf, n_out, concat_pool=concat_pool, **kwargs)
 else: head = custom_head
model = nn.Sequential(body, head)
 if init is not None: apply_init(model[1], init)
 return model
def timm_learner(dls, arch:str, loss_func=None, pretrained=True, cut=None, splitter=None,
y_range=None, config=None, n_out=None, normalize=True, **kwargs):
"Build a convnet style learner from `dls` and `arch` using the `timm` library"
if config is None: config = {}
if n_out is None: n_out = get_c(dls)
 assert n_out, "`n_out` is not defined, and could not be inferred from data, set `dls.c` or pass `n_out`"
if y_range is None and 'y_range' in config: y_range = config.pop('y_range')
 model = create_timm_model(arch, n_out, default_split, pretrained, y_range=y_range, **config)
 learn = Learner(dls, model, loss_func=loss_func, splitter=default_split, **kwargs)
 if pretrained: learn.freeze()
return learn
import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras import models, layers, utils
from tensorflow.keras import Sequential
from sklearn.model_selection import train_test_split
from tensorflow.keras.layers import Flatten, Dense, Conv2D, MaxPooling2D, MaxPool2D 
<split>
learn = timm_learner(dls,
'tf_efficientnet_b0_ns',
opt_func = ranger,
loss_func=LabelSmoothingCrossEntropy() ,
metrics = [accuracy] ).to_native_fp16()
x_train, x_val, y_train, y_val = train_test_split(mnist_train_data, mnist_train_label, test_size = 0.2, random_state = 2 )<choose_model_class>
learn.lr_find()
def define_model() :
model = Sequential()
model.add(Conv2D(64,(3, 3), activation='relu', input_shape=(28, 28, 1)))
 model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(MaxPooling2D(( 2, 2)))
 model.add(layers.BatchNormalization())
 model.add(Conv2D(filters=128, kernel_size =(3,3), activation="relu"))
model.add(Conv2D(filters=128, kernel_size =(3,3), activation="relu"))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
 model.add(layers.BatchNormalization())
 model.add(Conv2D(filters=256, kernel_size=(3,3), activation="relu"))
model.add(layers.MaxPooling2D(pool_size=(2,2)))
 model.add(Flatten())
 model.add(layers.BatchNormalization())
 model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
return model<choose_model_class>
learn.freeze()
learn.fit_flat_cos(1,1e-1, wd=0.5, cbs=[MixUp() ] )
model = define_model()
model.compile(optimizer="adam", loss='categorical_crossentropy', metrics=['accuracy'] )<train_model>
learn.save('stage-1' )
model.fit(x_train, y_train , epochs=30 )<predict_on_test>
learn = learn.load('stage-1' )
predictions = model.predict(mnist_test_arr )<define_variables>
learn.unfreeze()
learn.lr_find()
predictions_test = []
for i in predictions:
predictions_test.append(np.argmax(i))<save_to_csv>
learn.unfreeze()
learn.fit_flat_cos(20,2e-2,pct_start=0,cbs=[MixUp() ] )
submission = pd.DataFrame({
"ImageId": mnist_test.index+1,
"Label": predictions_test
})
 submission.to_csv('my_second_submission.csv', index=False )<normalization>
learn = learn.to_native_fp32()
def rotate_image(image, angle = 90, scale = 1.0):
h, w = image.shape
M = cv2.getRotationMatrix2D(( w/2, h/2), angle, scale)
 return cv2.warpAffine(image, M,(w, h))<load_from_csv>
learn.save('stage-2' )
data = np.loadtxt('/kaggle/input/digit-recognizer/train.csv', delimiter = ',', skiprows = 1 )<split>
interp = ClassificationInterpretation.from_learner(learn )
y = data[:, 0]
x = data[:, 1:].reshape(-1, 28, 28)
 x_train, x_cv, y_train, y_cv = train_test_split(x, y, test_size = 0.1)
 x_train_temp = x_train.copy()
for angle in np.arange(-10, 15, 5):
x_train = np.concatenate(( x_train, np.array([rotate_image(image, angle, scale = 1)for image in x_train_temp])))
 x_train = x_train.reshape(-1, 28, 28, 1)
 x_cv = x_cv.reshape(-1, 28, 28, 1)
 y_train_temp = y_train.copy()
for i in range(x_train.shape[0]//y_train_temp.shape[0] - 1):
y_train = np.concatenate(( y_train, y_train_temp))<set_options>
sample_df = pd.read_csv(dataset_path/'sample_submission.csv')
 sample_df.head()
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')) )<choose_model_class>
_sample_df = sample_df.copy()
_sample_df['path'] = _sample_df['image_id'].map(lambda x:dataset_path/'test_images'/x)
 _sample_df = _sample_df.drop(columns=['image_id'])
 test_dl = dls.test_dl(_sample_df )
YaNet = Sequential(name = 'YaNet')
 YaNet.add(Conv2D(filters = 32,
kernel_size =(5, 5),
kernel_initializer = 'he_uniform',
padding = 'same',
activation = 'relu',
input_shape =(28, 28, 1)))
 YaNet.add(BatchNormalization())
 YaNet.add(Conv2D(filters = 32,
kernel_size =(5, 5),
kernel_initializer = 'he_uniform',
padding = 'same',
activation = 'relu'))
YaNet.add(MaxPooling2D())
 YaNet.add(BatchNormalization())
 YaNet.add(Conv2D(filters = 64,
kernel_size =(3, 3),
kernel_initializer = 'he_uniform',
padding = 'same',
activation = 'relu'))
YaNet.add(MaxPooling2D())
 YaNet.add(BatchNormalization())
 YaNet.add(Conv2D(filters = 128,
kernel_size =(3, 3),
kernel_initializer = 'he_uniform',
padding = 'same',
activation = 'relu'))
YaNet.add(BatchNormalization())
 YaNet.add(Conv2D(filters = 128,
kernel_size =(3, 3),
kernel_initializer = 'he_uniform',
padding = 'same',
activation = 'relu'))
YaNet.add(MaxPooling2D())
 YaNet.add(BatchNormalization())
 YaNet.add(Flatten())
 YaNet.add(Dense(units = 512,
activation = 'relu'))
YaNet.add(Dropout(0.5))
YaNet.add(BatchNormalization())
 YaNet.add(Dense(units = 256,
activation = 'relu'))
YaNet.add(Dropout(0.5))
YaNet.add(BatchNormalization())
 YaNet.add(Dense(units = 128,
activation = 'relu'))
YaNet.add(Dropout(0.5))
YaNet.add(BatchNormalization())
 YaNet.add(Dense(units = 64,
activation = 'relu'))
YaNet.add(Dropout(0.5))
YaNet.add(BatchNormalization())
 YaNet.add(Dense(units = 10,
activation = 'softmax'))
YaNet.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
 YaNet.fit(x_train, y_train,
epochs = 100,
validation_data =(x_cv, y_cv),
use_multiprocessing = True )<load_from_csv>
test_dl.show_batch()
x_test = np.loadtxt('/kaggle/input/digit-recognizer/test.csv', skiprows = 1, delimiter = ',')
 x_test = x_test.reshape(-1, 28, 28, 1 )<save_to_csv>
preds, _ = learn.tta(dl=test_dl, n=8, beta=0 )
final_prediction = np.argmax(YaNet.predict(x_test), axis = 1)
 output = pd.DataFrame({'ImageId': np.arange(1, x_test.shape[0]+1), 'Label': final_prediction})
 output.to_csv('my_submission.csv', index = False )<import_modules>
sample_df['label'] = preds.argmax(dim=-1 ).numpy()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import os
import seaborn as sns
import matplotlib.image as mpimg<load_from_csv>
sample_df.to_csv('submission.csv',index=False )