Kaggle competition: Digit Recognizer
import albumentations as A<normalization>
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow.keras import layers, Sequential, optimizers
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.utils import to_categorical
import matplotlib.pyplot as plt
def gem(x, p=3, eps=1e-5):
return F.avg_pool2d(x.clamp(min=eps ).pow(p),(x.size(-2), x.size(-1)) ).pow(1./p)
 class GeM(nn.Module):
def __init__(self, p=3, eps=1e-5):
super(GeM, self ).__init__()
self.p = Parameter(torch.ones(1)* p)
 self.eps = eps
def forward(self, x):
return gem(x, p=self.p, eps=self.eps)
 def __repr__(self):
return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist() [0])+ ', ' + 'eps=' + str(
 self.eps)+ ')'<choose_model_class>
print("GPUs Available: ", tf.config.experimental.list_physical_devices('GPU'))
class Net(nn.Module):
def __init__(self, num_classes=5):
super().__init__()
self.model = timm.create_model('seresnext50_32x4d', pretrained=False)
 self._avg_pooling = nn.AdaptiveAvgPool2d(1)
 self.dropout=nn.Dropout(0.5)
 self._fc = nn.Linear(2048 , num_classes, bias=True)
 def forward(self, inputs):
input_iid = inputs
input_iid=input_iid/255.
 bs = input_iid.size(0)
 x = self.model.forward_features(input_iid)
 fm = self._avg_pooling(x)
 fm = fm.view(bs, -1)
 feature=self.dropout(fm)
 x = self._fc(feature)
 return x<create_dataframe>
raw_csv = "/kaggle/input/digit-recognizer/train.csv"
test_csv = "/kaggle/input/digit-recognizer/test.csv"
class DatasetTest() :
def __init__(self, test_data_dir):
self.ds = self.get_list(test_data_dir)
 self.root_dir = test_data_dir
self.val_trans=A.Compose([A.HorizontalFlip(p=0.5),
A.VerticalFlip(p=0.5),
A.ColorJitter(brightness=0.1, contrast=0.2, saturation=0.2, hue=0.00, always_apply=False, p=1.0),
A.RandomCrop(height= 600, width = 600,always_apply=True, p=1.0)] 
)
 def get_list(self, dir):
pic_list = os.listdir(dir)
 return pic_list
def __len__(self):
return len(self.ds)
 def preprocess_func(self, image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 image1=self.val_trans(image=image)['image']
image2=self.val_trans(image=image)['image']
image3=self.val_trans(image=image)['image']
image4=self.val_trans(image=image)['image']
image5=self.val_trans(image=image)['image']
image6=self.val_trans(image=image)['image']
image7=self.val_trans(image=image)['image']
image8=self.val_trans(image=image)['image']
image_batch = np.stack([image1,
image2,
image3,
image4,
image5,
image6,
image7,
image8
])
 image_batch = np.transpose(image_batch, axes=[0, 3, 1, 2])
 return image, image_batch
def __getitem__(self, item):
fname = self.ds[item]
image_path = os.path.join(self.root_dir, fname)
 image = cv2.imread(image_path, -1)
 image,float_image = self.preprocess_func(image)
 return fname, image,float_image<predict_on_test>
raw_df = pd.read_csv(raw_csv)
 test_df = pd.read_csv(test_csv )
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 kaggle_root = '/kaggle/input'
model_dir = os.path.join(kaggle_root, 'cassva-models-se50-640')
 weights = [os.path.join(model_dir, f)for f in os.listdir(model_dir)]
test_datadir= os.path.join(kaggle_root, 'cassava-leaf-disease-classification/test_images')
 dataiter=DatasetTest(test_datadir)
 def predict_with_model(model,weights):
merge_res_dict = {}
for j, weight in enumerate(weights):
model.load_state_dict(torch.load(weight, map_location=device), strict=False)
 model.eval()
cur_result=pd.DataFrame(columns=['image_id','label'])
 len_data = len(dataiter)
 image_ids=[]
precictions=[]
for i in range(len(dataiter)) :
print('weight {}: data {}/{}'.format(j, i+1, len_data))
fname,_,float_image=dataiter.__getitem__(i)
 input=torch.from_numpy(float_image ).to(device ).float()
with torch.no_grad() :
output=model(input)
 output=torch.nn.functional.softmax(output,dim=-1)
 output=output.cpu().numpy()
output=np.mean(output,axis=0)
 image_ids.append(fname)
 label=np.argmax(output)
 precictions.append(label)
 if fname not in merge_res_dict:
merge_res_dict[fname] = output
else:
merge_res_dict[fname] += output
merge_result = pd.DataFrame(columns=['image_id','label'])
 precictions = []
for fname in merge_res_dict:
label = np.argmax(merge_res_dict[fname])
 precictions.append(label)
 merge_result['image_id'] = image_ids
merge_result['label'] = precictions
merge_result.to_csv('submission.csv',index=False)
 model=Net().to(device)
 predict_with_model(model,weights )<define_variables>
def get_image_and_label(data_frame):
IMGs = data_frame.drop(["label"], axis=1 ).values if 'label' in data_frame.columns else data_frame.values
IMGs = np.array([image.reshape(( 28, 28)) for image in IMGs])
 IMGs = np.expand_dims(IMGs, axis=3)
 labels = data_frame['label'].values if 'label' in data_frame.columns else None
return IMGs, labels
package_path = '.. /input/pytorch-image-models/pytorch-image-models-master'
sys.path.append(package_path )<set_options>
raw_IMGs, raw_labels = get_image_and_label(raw_df)
 test_IMGs, _ = get_image_and_label(test_df )
warnings.filterwarnings("ignore")
<init_hyperparams>
classes = len(set(raw_labels))
classes
CFG = {
'fold_num': 5,
'seed': 719,
'model_arch': 'tf_efficientnet_b4_ns',
'img_size': 512,
'epochs': 10,
'train_bs': 32,
'valid_bs': 32,
'lr': 1e-4,
'num_workers': 4,
'accum_iter': 1,
'verbose_step': 1,
'device': 'cuda:0',
'tta': 3,
'used_epochs': [6,7,8,9],
'weights': [1,1,1,1]
}<load_from_csv>
raw_labels = to_categorical(raw_labels, num_classes=classes)
 raw_labels
train = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 train.head(10 )<count_values>
train_IMGs, validation_IMGs, trian_labels, validation_labels = train_test_split(raw_IMGs, raw_labels, test_size=0.1, random_state=42 )
train.label.value_counts()<load_from_csv>
model = Sequential([
layers.Conv2D(32,(3,3), activation="relu", input_shape=(28,28,1)) ,
layers.BatchNormalization() ,
layers.MaxPooling2D(( 2,2)) ,
layers.Conv2D(64,(3,3), activation="relu"),
layers.BatchNormalization() ,
layers.MaxPooling2D(( 2,2)) ,
layers.Conv2D(128,(3,3), activation="relu"),
layers.BatchNormalization() ,
layers.Flatten() ,
layers.Dense(1024, activation="relu"),
layers.Dropout(0.2),
layers.Dense(256, activation="relu"),
layers.Dropout(0.2),
layers.Dense(64, activation="relu"),
layers.Dropout(0.2),
layers.Dense(32, activation="relu"),
layers.Dropout(0.2),
layers.Dense(16, activation="relu"),
layers.Dropout(0.2),
layers.Dense(int(classes), activation="softmax")
 ] )
submission = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission.head()<set_options>
model.compile(loss="categorical_crossentropy",
optimizer=optimizers.Adam(learning_rate=1e-4),
metrics=['accuracy'] )
def seeder(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
def get_img(path):
im_bgr = cv2.imread(path)
 im_rgb = im_bgr[:, :, ::-1]
return im_rgb 
<load_pretrained>
train_datagen = ImageDataGenerator(
 rescale=1/255,
rotation_range=20,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1,
shear_range=0.1 
)
 validation_datagen = ImageDataGenerator(rescale=1/255)
 test_datagen = ImageDataGenerator(rescale=1/255 )
img = get_img('.. /input/cassava-leaf-disease-classification/train_images/1000015157.jpg')
 plt.figure(figsize=(15,15))
plt.imshow(img)
 plt.show()<categorify>
train_generator = train_datagen.flow(train_IMGs, trian_labels, batch_size=32)
 validation_generator = train_datagen.flow(validation_IMGs, validation_labels, batch_size=32)
 test_generator = test_datagen.flow(test_IMGs, batch_size=32, shuffle=False )
class CassavaDataset(Dataset):
def __init__(self,df,data_root,transforms=None,output_label=True):
super(CassavaDataset ).__init__()
self.df=df.reset_index().copy()
self.data_root=data_root
self.transforms=transforms
self.output_label=output_label
def __len__(self):
return self.df.shape[0]
def __getitem__(self,index:int):
if self.output_label == True:
label=self.iloc[index]["label"]
path = "{}/{}".format(self.data_root, self.df.iloc[index]['image_id'])
 img=get_img(path)
 if self.transforms:
img=self.transforms(image=img)["image"]
if self.output_label == True:
return img,torch.tensor(label,dtype=torch.double)
 else:
return img 
<categorify>
history = model.fit_generator(train_generator, epochs=100, validation_data=validation_generator, verbose=1 )
HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize 
)
 def get_train_transforms() :
return Compose([
RandomResizedCrop(CFG['img_size'], CFG['img_size']),
Transpose(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
ShiftScaleRotate(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
CoarseDropout(p=0.5),
Cutout(p=0.5),
ToTensorV2(p=1.0),
], p=1.)
def get_valid_transforms() :
return Compose([
CenterCrop(CFG['img_size'], CFG['img_size'], p=1.) ,
Resize(CFG['img_size'], CFG['img_size']),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.)
def get_inference_transforms() :
return Compose([
RandomResizedCrop(CFG['img_size'], CFG['img_size']),
Transpose(p=0.5),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),
RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),
ToTensorV2(p=1.0),
], p=1.)<choose_model_class>
model.evaluate(validation_IMGs, validation_labels )
class CassvaImgClassifier(nn.Module):
def __init__(self, model_arch, n_class, pretrained=False):
super().__init__()
self.model = timm.create_model(model_arch, pretrained=pretrained)
 n_features = self.model.classifier.in_features
self.model.classifier = nn.Linear(n_features, n_class)
 def forward(self, x):
x = self.model(x)
 return x<feature_engineering>
accuracy = history.history["accuracy"]
val_accuracy = history.history["val_accuracy"]
loss = history.history["loss"]
val_loss = history.history["val_loss"]
epochs = range(len(accuracy))
if __name__ == "__main__":
seeder(CFG["seed"])
 folds = StratifiedKFold(n_splits=CFG['fold_num'] ).split(np.arange(train.shape[0]), train.label.values)
 for fold,(trn_idx, val_idx)in enumerate(folds):
if fold > 0:
break
print('Inference fold {} started'.format(fold))
test = pd.DataFrame()
test['image_id'] = list(os.listdir('.. /input/cassava-leaf-disease-classification/test_images/'))
test_ds = CassavaDataset(test, '.. /input/cassava-leaf-disease-classification/test_images/', transforms=get_inference_transforms() , output_label=False)
 valid_ = train.loc[val_idx,:].reset_index(drop=True)
 valid_ds = CassavaDataset(valid_, '.. /input/cassava-leaf-disease-classification/train_images/', transforms=get_inference_transforms() , output_label=False)
 val_loader = torch.utils.data.DataLoader(
 valid_ds,
batch_size=CFG['valid_bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False, 
)
 tst_loader = torch.utils.data.DataLoader(
 test_ds,
batch_size=CFG['valid_bs'],
num_workers=CFG['num_workers'],
shuffle=False,
pin_memory=False, 
)
 device = torch.device(CFG['device'])
 model = CassvaImgClassifier(CFG['model_arch'], train.label.nunique() ).to(device)
 val_preds = []
tst_preds = []
for i, epoch in enumerate([1]):
model.load_state_dict(torch.load(".. /input/aashika-cassava/tf_efficientnet_b4_ns_fold_0_9"))
with torch.no_grad() :
for _ in range(CFG["tta"]):
val_preds += [CFG['weights'][i]/sum(CFG['weights'])/CFG['tta']*inference_one_epoch(model, val_loader, device)]
tst_preds += [CFG['weights'][i]/sum(CFG['weights'])/CFG['tta']*inference_one_epoch(model, tst_loader, device)]
val_preds = np.mean(val_preds, axis=0)
 tst_preds = np.mean(tst_preds, axis=0)
 print('fold {} validation loss = {:.5f}'.format(fold, log_loss(valid_.label.values, val_preds)))
 print('fold {} validation accuracy = {:.5f}'.format(fold,(valid_.label.values==np.argmax(val_preds, axis=1)).mean()))
del model
torch.cuda.empty_cache() 
<feature_engineering>
pred_labels = model.predict_generator(test_generator )
test['label'] = np.argmax(tst_preds, axis=1)
 test.head()<save_to_csv>
pred_labels = np.argmax(pred_labels, axis=-1)
 pred_labels
test.to_csv('submission.csv', index=False )<import_modules>
my_submission = pd.DataFrame({'ImageId': test_df.index + 1, 'Label': pred_labels})
 my_submission.head()
<install_modules><EOS> 
my_submission.to_csv('submission.csv', index=False )