Kaggle competition: Cassava Leaf Disease Classification
train_datagen = ImageDataGenerator(
 rescale=1./255, validation_split=0.2)
<define_variables>
from fastai.vision.all import *
import albumentations
train_data = '.. /input/state-farm-distracted-driver-detection/train'
test_data = '.. /input/state-farm-distracted-driver-detection/test'
train_generator = train_datagen.flow_from_directory(
 train_data,
target_size=(224, 224),
batch_size=32,
class_mode='categorical',
subset='training')
 val_generator = train_datagen.flow_from_directory(
 train_data,
target_size=(224,224),
batch_size=32,
class_mode='categorical',
subset='validation')
<define_variables>
set_seed(42 )
ac_labels= ["c0: safe driving",
"c1: texting - right",
"c2: talking on the phone - right",
"c3: texting - left",
"c4: talking on the phone - left",
"c5: operating the radio",
"c6: drinking",
"c7: reaching behind",
"c8: hair and makeup",
"c9: talking to passenger"] 
<define_variables>
class AlbumentationsTransform(RandTransform):
"A transform handler for multiple `Albumentation` transforms"
split_idx,order=None,2
def __init__(self, train_aug, valid_aug): store_attr()
def before_call(self, b, split_idx):
self.idx = split_idx
def encodes(self, img: PILImage):
if self.idx == 0:
aug_img = self.train_aug(image=np.array(img)) ['image']
else:
aug_img = self.valid_aug(image=np.array(img)) ['image']
return PILImage.create(aug_img )
imgs, labels = next(train_generator )<count_values>
def get_x(row): return data_path/row['image_id']
def get_y(row): return row['label']
def list_counts(start_dir):
lst = sorted(os.listdir(start_dir))
out = [(fil, len(os.listdir(os.path.join(start_dir, fil)))) for fil in lst if os.path.isdir(os.path.join(start_dir,fil)) ]
return out
out = list_counts(train_data)
 labels, counts = zip(*out)
 print("Total number of images : ",functools.reduce(lambda a,b : a+b, counts))
out<import_modules>
class CassavaModel(Module):
def __init__(self, num_classes):
self.effnet = EfficientNet.from_pretrained("efficientnet-b3")
 self.dropout = nn.Dropout(0.1)
 self.out = nn.Linear(1536, num_classes)
 def forward(self, image):
batch_size, _, _, _ = image.shape
x = self.effnet.extract_features(image)
 x = F.adaptive_avg_pool2d(x, 1 ).reshape(batch_size, -1)
 outputs = self.out(self.dropout(x))
return outputs
from keras.layers import ZeroPadding2D, Conv2D, MaxPooling2D, Flatten, Dense, Dropout, Input
from keras.layers import GlobalAveragePooling2D, MaxPooling2D
from keras.models import Model, Sequential
from keras.callbacks import ModelCheckpoint
from keras import regularizers<choose_model_class>
Path('/kaggle/input' ).ls()
input_layer = Input(shape=(224,224, 3))
conv = Conv2D(filters=8, kernel_size=2 )(input_layer)
 conv = Conv2D(filters=16, kernel_size=2, activation='relu' )(conv)
 conv = Conv2D(filters=32, kernel_size=2, activation='relu' )(conv)
 conv = MaxPooling2D()(conv)
 conv = Conv2D(filters=64, kernel_size=2, activation='relu' )(conv)
 conv = Conv2D(filters=128, kernel_size=2, activation='relu' )(conv)
 conv = Conv2D(filters=512, kernel_size=2, activation='relu' )(conv)
 conv = GlobalAveragePooling2D()(conv)
 dense = Dense(units=500, activation='relu' )(conv)
 dense = Dropout(0.1 )(dense)
 dense = Dense(units=100, activation='relu' )(dense)
 dense = Dropout(0.1 )(dense)
 output = Dense(units=10, activation='softmax' )(dense)
 model = Model(inputs=input_layer, outputs = output )<choose_model_class>
learn = load_learner(Path('/kaggle/input/effnet-inference/inference(1)'), cpu=False )
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] )<train_model>
path = Path(".. /input")
 data_path = path/'cassava-leaf-disease-classification'
checkpoint = ModelCheckpoint('best_model_1.hdf5', save_best_only=True, verbose=1)
 history = model.fit_generator(train_generator, steps_per_epoch=len(train_generator),
epochs=10,
validation_data = val_generator,
validation_steps=len(val_generator),
callbacks=[checkpoint] )<load_pretrained>
test_df = pd.read_csv(data_path/'sample_submission.csv')
 test_df.head()
!ls.. /input/state-farm-distracted-driver-detection/
batch_index = 0
files_list = os.listdir(".. /input/state-farm-distracted-driver-detection/test/")
 def load_test_images(batch_size=32, src='.. /input/state-farm-distracted-driver-detection/test/'):
global batch_index, files_list
imgs_list = files_list[batch_index: batch_index+batch_size]
batch_index += len(imgs_list)
 batch_imgs = []
for img_name in imgs_list:
img = Image.open(src+img_name)
 im = img.resize(( 224,224))
batch_imgs.append(np.array(im)/255.)
return np.array(batch_imgs)
<predict_on_test>
test_copy = test_df.copy()
test_copy['image_id'] = test_copy['image_id'].apply(lambda x: f'test_images/{x}' )
preds_list = np.array([])
 batch_index=0
batch_size = 32
while True:
tst_imgs = load_test_images(batch_size=batch_size)
 if(tst_imgs.shape[0] <= 0):
print("Batchsize is less : ",batch_index)
 break
preds = model.predict(tst_imgs)
 print("\r {}, batch_size : {}, nth_batch/all_batch : {}/{}".format(preds_list.shape,batch_size, batch_index, len(files_list)) ,end="")
 sys.stdout.flush()
if len(preds_list)== 0:
preds_list = np.array(preds)
 else:
preds_list = np.append(preds_list, preds, axis=0)
<save_to_csv>
test_dl = learn.dls.test_dl(test_copy )
titles = "img,c0,c1,c2,c3,c4,c5,c6,c7,c8,c9".split(",")
 names = pd.DataFrame(files_list[:len(preds_list)])
 names.columns=["img"]
df = pd.DataFrame(preds_list)
 df.columns=titles[1:]
df['img']=names['img']
df = df[titles]
df.tail()
df.to_csv('sub.csv',index=False)
<choose_model_class>
preds, _ = learn.get_preds(dl=test_dl )
def load_VGG16(weights_path=None, no_top=True):
input_shape =(224, 224, 3)
 img_input = Input(shape=input_shape)
 x = Conv2D(64,(3, 3), activation='relu', padding='same', name='block1_conv1' )(img_input)
 x = Conv2D(64,(3, 3), activation='relu', padding='same', name='block1_conv2' )(x)
 x = MaxPooling2D(( 2, 2), strides=(2, 2), name='block1_pool' )(x)
 x = Conv2D(128,(3, 3), activation='relu', padding='same', name='block2_conv1' )(x)
 x = Conv2D(128,(3, 3), activation='relu', padding='same', name='block2_conv2' )(x)
 x = MaxPooling2D(( 2, 2), strides=(2, 2), name='block2_pool' )(x)
 x = Conv2D(256,(3, 3), activation='relu', padding='same', name='block3_conv1' )(x)
 x = Conv2D(256,(3, 3), activation='relu', padding='same', name='block3_conv2' )(x)
 x = Conv2D(256,(3, 3), activation='relu', padding='same', name='block3_conv3' )(x)
 x = MaxPooling2D(( 2, 2), strides=(2, 2), name='block3_pool' )(x)
 x = Conv2D(512,(3, 3), activation='relu', padding='same', name='block4_conv1' )(x)
 x = Conv2D(512,(3, 3), activation='relu', padding='same', name='block4_conv2' )(x)
 x = Conv2D(512,(3, 3), activation='relu', padding='same', name='block4_conv3' )(x)
 x = MaxPooling2D(( 2, 2), strides=(2, 2), name='block4_pool' )(x)
 x = Conv2D(512,(3, 3), activation='relu', padding='same', name='block5_conv1' )(x)
 x = Conv2D(512,(3, 3), activation='relu', padding='same', name='block5_conv2' )(x)
 x = Conv2D(512,(3, 3), activation='relu', padding='same', name='block5_conv3' )(x)
 x = MaxPooling2D(( 2, 2), strides=(2, 2), name='block5_pool' )(x)
 x = GlobalAveragePooling2D()(x)
 vmodel = Model(img_input, x, name='vgg16')
 if weights_path is not None:
print("Weights have been loaded.")
 vmodel.load_weights(weights_path)
 return vmodel 
<choose_model_class>
test_df['label'] = preds.argmax(dim=-1 ).numpy()
<categorify><EOS> 
test_df.to_csv('submission.csv', index=False )