Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<load_from_csv>
import numpy as np
import pandas as pd
import os
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
import pandas as pd
import matplotlib.pyplot as plt
dfx = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 df_train, df_valid = model_selection.train_test_split(dfx, test_size=0.1, random_state=42, stratify=dfx.label.values)
 _train = df_train.reset_index(drop=True)
 df_valid = df_valid.reset_index(drop=True)
 image_path = ".. /input/cassava-leaf-disease-classification/train_images/"
train_image_paths = [os.path.join(image_path, x)for x in df_train.image_id.values]
valid_image_paths = [os.path.join(image_path, x)for x in df_valid.image_id.values]
train_targets = df_train.label.values
valid_targets = df_valid.label.values<create_dataframe>
train = np.loadtxt(open('/kaggle/input/digit-recognizer/train.csv', 'r'), delimiter=',', skiprows=1, dtype='float32')
 test = np.loadtxt(open('/kaggle/input/digit-recognizer/test.csv', 'r'), delimiter=',', skiprows=1, dtype='float32')
 train_images = train[:, 1:].reshape(( train.shape[0], 28, 28, 1)) / 255.0
train_labels = train[:, 0].astype(np.uint8)
 test_images = test.reshape(( test.shape[0], 28, 28, 1)) / 255.0
cassava_train = CassavaDataset(train_image_paths, train_targets, 'train')
 cassava_test = CassavaDataset(valid_image_paths, valid_targets, 'test')
 batch_size = 16
train_loader = DataLoader(cassava_train, batch_size=batch_size, shuffle=False, num_workers=2)
 test_loader = DataLoader(cassava_test, batch_size=batch_size, shuffle=False, num_workers=2 )<compute_train_metric>
augmentation_layer = tf.keras.Sequential([
tf.keras.layers.experimental.preprocessing.RandomRotation(0.1, input_shape=(28, 28, 1)) ,
tf.keras.layers.experimental.preprocessing.RandomZoom(( 0.2, 0.2)) ,
] )
class AverageMeter:

 def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def accuracy(output, target, topk=(1,)) :

 maxk = max(topk)
 batch_size = target.size(0)
 _, pred = output.topk(maxk, 1, True, True)
 pred = pred.t()
correct = pred.eq(target.reshape(1, -1 ).expand_as(pred))
return [correct[:k].reshape(-1 ).float().sum(0)* 100./ batch_size for k in topk]<train_model>
for i in range(5):
new_img = augmentation_layer(train_images[np.random.randint(train_images.shape[0])] ).numpy()
plt.imshow(new_img.reshape(( 28, 28)))
 plt.show()
def train_epoch(model, loader, device, loss_func, optimizer, scheduler):
model.train()
summary_loss = AverageMeter()
summary_acc = AverageMeter()
start = time.time()
n = len(loader)
 for batch in tqdm(loader):
images, labels = batch
images = images.to(device)
 labels = labels.to(device)
 out = model(images)
 loss = loss_func(out, labels)
 loss.backward()
optimizer.step()
optimizer.zero_grad()
with torch.no_grad() :
acc = accuracy(out, labels)[0]
summary_loss.update(loss.detach().item() , batch_size)
 summary_acc.update(acc.detach().item() , batch_size)
 train_time = str(datetime.timedelta(seconds=time.time() - start))
print('Train loss: {:.5f} - Train acc: {:.2f}% - time: {}'.format(summary_loss.avg,
summary_acc.avg,
train_time))
return summary_loss, summary_acc<set_options>
model = Sequential([
tf.keras.layers.Input(( 28, 28, 1)) ,
augmentation_layer,
Conv2D(32, 3, activation='relu', padding="same"),
MaxPooling2D(2),
Conv2D(64, 3, activation='relu', padding="same"),
MaxPooling2D(2),
Conv2D(64, 3, activation='relu', padding="same"),
MaxPooling2D(2),
Flatten() ,
Dropout(0.3),
Dense(128, activation='relu'),
Dropout(0.3),
Dense(10, activation='softmax')
 ] )
resnet = timm.create_model('resnext50_32x4d', pretrained=True)
 num_ftrs = resnet.fc.in_features
resnet.fc = nn.Linear(num_ftrs, 5)
 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 resnet.to(device )<choose_model_class>
model.compile(
 optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
num_epochs = 1
best_acc = 0
best_epoch = 0
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(resnet.parameters() , lr=0.01, momentum=0.9)
 scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=0.2, patience=2, verbose=True, eps=1e-6)
 for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch + 1, num_epochs))
train_loss, train_acc = train_epoch(resnet, train_loader, device, criterion, optimizer, scheduler)
 val_loss, val_acc = validate_epoch(resnet, test_loader, device, criterion)
 scheduler.step(val_loss.avg)
 if val_acc.avg > best_acc:
best_acc = val_acc.avg
best_epoch = epoch
if epoch == 9:
print('Saving model...')
 PATH = './timm_resnext_epoch{}_384.pth'.format(epoch + 1)
 torch.save(resnet.state_dict() ,PATH )<choose_model_class>
history = model.fit(train_images, train_labels, epochs=100 )
num_epochs = 10
best_acc = 0
best_epoch = 0
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(xception.parameters() , lr=0.01, momentum=0.9)
 scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=0.2, patience=2, verbose=True, eps=1e-6)
 for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch + 1, num_epochs))
train_loss, train_acc = train_epoch(xception, train_loader, device, criterion, optimizer, scheduler)
 val_loss, val_acc = validate_epoch(xception, test_loader, device, criterion)
 scheduler.step(val_loss.avg)
 if val_acc.avg > best_acc:
best_acc = val_acc.avg
best_epoch = epoch
if epoch == 9:
print('Saving model...')
 PATH = './timm_xception_epoch{}_384.pth'.format(epoch + 1)
 torch.save(xception.state_dict() ,PATH )<find_best_params>
predition_model = tf.keras.Sequential()
for layer in model.layers:
if layer != augmentation_layer:
predition_model.add(layer)
 predition_model.compile(loss="sparse_categorical_crossentropy", optimizer="adam" )
PATH = './timm_resnext_epoch10_384.pth'
resnet = timm.create_model('resnext50_32x4d', pretrained=False)
 num_ftrs = resnet.fc.in_features
resnet.fc = nn.Linear(num_ftrs, 5)
 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 resnet.to(device)
 resnet.load_state_dict(torch.load(PATH))
resnet.eval()<load_from_csv>
test_labels = np.argmax(predition_model.predict(test_images), axis=-1)
 print(test_labels.shape )
submission_df = pd.read_csv('.. /input/cassava-leaf-disease-classification/sample_submission.csv')
 submission_df.head()<normalization>
image_ids = np.arange(1, test_labels.shape[0]+1)
 result = np.concatenate(( image_ids.reshape(image_ids.shape[0], 1), test_labels.reshape(test_labels.shape[0], 1)) , axis=1)
 df = pd.DataFrame(result, columns=["ImageId", "Label"], dtype='int')
 df.to_csv("submission.csv", index=False )