Kaggle competition: Cassava Leaf Disease Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: cassava-leaf-disease-classification<data_type_conversions>
BATCH_SIZE = 1
image_size = 512
enet_type = ['tf_efficientnet_b4_ns'] * 5
model_path = ['.. /input/cassava-models-eff/baseline_cld_fold0_epoch8_tf_efficientnet_b4_ns_512.pth',
'.. /input/cassava-models-eff/baseline_cld_fold1_epoch9_tf_efficientnet_b4_ns_512.pth',
'.. /input/cassava-models-eff/baseline_cld_fold2_epoch9_tf_efficientnet_b4_ns_512.pth',
'.. /input/cassava-models-eff/baseline_cld_fold3_epoch5_tf_efficientnet_b4_ns_512.pth',
'.. /input/cassava-models-eff/baseline_cld_fold4_epoch11_tf_efficientnet_b4_ns_512.pth']
train_X = train_X.reshape(-1, 28, 28, 1 ).astype("float32")/255.0
val_X = val_X.reshape(-1, 28, 28, 1 ).astype("float32")/255.0
test = test.values.reshape(-1, 28, 28, 1 ).astype("float32")/255.0
train_y = tf.keras.utils.to_categorical(train_y)
 val_y = tf.keras.utils.to_categorical(val_y )<load_from_zip>
transforms_valid = albumentations.Compose([
albumentations.CenterCrop(image_size, image_size, p=1),
albumentations.Resize(image_size, image_size),
albumentations.Normalize()
] )
! git clone https://github.com/bckenstler/CLR.git<choose_model_class>
OUTPUT_DIR = './'
MODEL_DIR = '.. /input/cassava-models-res/'
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
 TRAIN_PATH = '.. /input/cassava-leaf-disease-classification/train_images'
TEST_PATH = '.. /input/cassava-leaf-disease-classification/test_images'
clr_triangular = CyclicLR(mode='triangular' )<choose_model_class>
class CFG:
debug=False
num_workers=8
model_name='resnext50_32x4d'
size=512
batch_size=32
seed=2020
target_size=5
target_col='label'
n_fold=5
trn_fold=[0, 1, 2, 3, 4]
inference=True
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('accuracy')>0.9999):
print("
Reached 99.99% accuracy so cancelling training!")
 self.model.stop_training = True
callbacks = myCallback()
learning_rate_reduction = tf.keras.callbacks.ReduceLROnPlateau(monitor = 'val_loss', patience = 3, verbose = 1, factor = 0.5, min_lr = 1e-6 )<normalization>
test = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 test['filepath'] = test.image_id.apply(lambda x: os.path.join('.. /input/cassava-leaf-disease-classification/test_images', f'{x}'))
IMG_SIZE = 28
BATCH_SIZE = 64
AUTOTUNE = tf.data.experimental.AUTOTUNE
num_classes = 10
def augment(image_label, seed):
image, label = image_label
image = tf.image.resize_with_crop_or_pad(
 image,
IMG_SIZE + 6,
IMG_SIZE + 6 
)
 new_seed = tf.random.experimental.stateless_split(seed, num=1)[0,:]
image = tf.image.stateless_random_crop(
 image,
size=(BATCH_SIZE, IMG_SIZE, IMG_SIZE, 1),
seed=seed 
)
 image = tf.image.stateless_random_brightness(
 image,
max_delta=0.5,
seed=new_seed 
)
 image = tf.clip_by_value(
 image,
0,
1)
 return image, label
def create_train_val(train_ds, val_ds):
train_ds =(
 train_ds 
.shuffle(1000)
.map(f, num_parallel_calls=AUTOTUNE)
.prefetch(AUTOTUNE)
)
 val_ds =(
 val_ds 
.prefetch(AUTOTUNE)
)
 return train_ds, val_ds
def create_model(train_ds, val_ds, epochs):
model = tf.keras.Sequential([
layers.Conv2D(32,(3, 3), activation = 'relu', padding = 'same', input_shape=(28,28,1)) ,
layers.Conv2D(32,(3, 3), activation = 'relu', padding = 'same'),
layers.BatchNormalization() ,
layers.MaxPooling2D(2, 2),
layers.Dropout(0.2),
layers.Conv2D(64,(3, 3), activation = 'relu', padding = 'same'),
layers.Conv2D(64,(3, 3), activation = 'relu', padding = 'same'),
layers.BatchNormalization() ,
layers.MaxPooling2D(2, 2),
layers.Dropout(0.2),
layers.Conv2D(128,(3, 3), activation = 'relu', padding = 'same'),
layers.Conv2D(128,(3, 3), activation = 'relu', padding = 'same'),
layers.BatchNormalization() ,
layers.MaxPooling2D(2, 2),
layers.Dropout(0.2),
layers.Flatten() ,
layers.Dense(512, activation = 'relu'),
layers.BatchNormalization() ,
layers.Dropout(0.2),
layers.Dense(256, activation = 'relu'),
layers.BatchNormalization() ,
layers.Dropout(0.35),
layers.Dense(10, activation = 'softmax')
 ])
 model.compile(optimizer=Adam(0.1),
loss=tf.keras.losses.CategoricalCrossentropy() ,
metrics=['accuracy'])
 print(model.summary())
 return model
rng = tf.random.Generator.from_seed(123, alg='philox')
 def f(x, y):
seed = rng.make_seeds(2)[0]
image, label = augment(( x, y), seed)
 return image, label
train_ds = tf.data.Dataset.from_tensor_slices(( train_X, train_y)).batch(BATCH_SIZE)
 val_ds = tf.data.Dataset.from_tensor_slices(( val_X, val_y)).batch(BATCH_SIZE)
 test_ds = tf.data.Dataset.from_tensor_slices(( test)).batch(BATCH_SIZE)
 train_ds, val_ds = create_train_val(train_ds, val_ds)
 cnn_model = create_model(train_ds, val_ds, 100 )<train_model>
test_dataset_efficient = CLDDataset(test, 'test', transform=transforms_valid)
 test_loader_efficient = torch.utils.data.DataLoader(test_dataset_efficient, batch_size=BATCH_SIZE, shuffle=False, num_workers=4 )
startTime = timeit.default_timer()
history = cnn_model.fit(
 train_ds,
steps_per_epoch = train_X.shape[0] // BATCH_SIZE,
epochs = 50,
validation_data = val_ds,
validation_steps = val_X.shape[0] // BATCH_SIZE,
callbacks = [callbacks, learning_rate_reduction] 
)
 elapsedTime = timeit.default_timer() - startTime
print("Time taken for the Network to train : ",elapsedTime )<train_model>
def get_transforms(*, data):
if data == 'valid':
return A.Compose([
A.Resize(CFG.size, CFG.size),
A.Normalize(
 mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225], 
),
ToTensorV2() ,
] )
startTime = timeit.default_timer()
history = cnn_model.fit(
 train_ds,
steps_per_epoch = train_X.shape[0] // BATCH_SIZE,
epochs = 50,
validation_data = val_ds,
validation_steps = val_X.shape[0] // BATCH_SIZE,
callbacks = [clr_triangular] 
)
 elapsedTime = timeit.default_timer() - startTime
print("Time taken for the Network to train : ",elapsedTime )<save_to_csv>
class CustomResNext(nn.Module):
def __init__(self, model_name='resnext50_32x4d', pretrained=False):
super().__init__()
self.model = timm.create_model(model_name, pretrained=pretrained)
 n_features = self.model.fc.in_features
self.model.fc = nn.Linear(n_features, CFG.target_size)
 def forward(self, x):
x = self.model(x)
 return x
prediction = cnn_model.predict(test ).argmax(axis=1)
 sub['Label'] = prediction
sub.to_csv("MNIST_sub_cnn1.csv", index=False)
 sub.head()<set_options>
class enet_v2(nn.Module):
def __init__(self, backbone, out_dim, pretrained=False):
super(enet_v2, self ).__init__()
self.enet = timm.create_model(backbone, pretrained=pretrained)
 in_ch = self.enet.classifier.in_features
self.myfc = nn.Linear(in_ch, out_dim)
 self.enet.classifier = nn.Identity()
def forward(self, x):
x = self.enet(x)
 x = self.myfc(x)
 return x
np.random.seed(42)
 torch.random.seed = 42<load_from_csv>
def load_state(model_path):
model = CustomResNext(CFG.model_name, pretrained=False)
 try:
model.load_state_dict(torch.load(model_path)['model'], strict=True)
 state_dict = torch.load(model_path)['model']
except:
state_dict = torch.load(model_path)['model']
state_dict = {k[7:] if k.startswith('module.')else k: state_dict[k] for k in state_dict.keys() }
return state_dict
def inference(model, states, test_loader, device):
model.to(device)
 tk0 = tqdm(enumerate(test_loader), total=len(test_loader))
probs = []
for i,(images)in tk0:
images = images.to(device)
 avg_preds = []
for state in states:
model.load_state_dict(state)
 model.eval()
with torch.no_grad() :
y_preds = model(images)
 avg_preds.append(y_preds.softmax(1 ).to('cpu' ).numpy())
 avg_preds = np.mean(avg_preds, axis=0)
 probs.append(avg_preds)
 probs = np.concatenate(probs)
 return probs
<data_type_conversions><EOS> 
model = CustomResNext(CFG.model_name, pretrained=False)
 states = [load_state(MODEL_DIR+f'{CFG.model_name}_fold{fold}.pth')for fold in CFG.trn_fold]
test_dataset = TestDataset(test, transform=get_transforms(data='valid'))
test_loader = DataLoader(test_dataset, batch_size=CFG.batch_size, shuffle=False,
num_workers=CFG.num_workers, pin_memory=True)
 predictions = inference(model, states, test_loader, device)
 test_preds = []
for i in range(len(enet_type)) :
model = enet_v2(enet_type[i], out_dim=5)
 model = model.to(device)
 model.load_state_dict(torch.load(model_path[i]))
test_preds += [tta_inference_func(test_loader_efficient)]
pred = 0.5*predictions + 0.5*np.mean(test_preds, axis=0)
 test['label'] = softmax(pred ).argmax(1)
 test[['image_id', 'label']].to_csv(OUTPUT_DIR+'submission.csv', index=False)
 test.head()