Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<train_model>
%matplotlib inline
BATCH_SIZE = 256
print(os.listdir('.. /input/Kannada-MNIST/'))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 print(device )
def run_k_fold(NFOLDS, seed):
oof = np.zeros(( len(train), len(target_cols)))
 predictions = np.zeros(( len(test), len(target_cols)))
 for fold in range(NFOLDS):
gc.collect()
oof_ = run_training(fold, seed)
 modelPred = Model(
 num_features=num_features,
num_targets=num_targets,
hidden_size=hidden_size,
Dropout_Model=Dropout_Model)
 pred_ = run_predicition(fold, modelPred, test, feature_cols, DEVICE, BATCH_SIZE)
 predictions += pred_ / NFOLDS
oof += oof_
return oof, predictions
SEED = [0,1,2,3,4,5,6]
oof = np.zeros(( len(train), len(target_cols)))
 predictions = np.zeros(( len(test), len(target_cols)))
 for seed in SEED:
oof_, predictions_ = run_k_fold(NFOLDS, seed)
 oof += oof_ / len(SEED)
 predictions += predictions_ / len(SEED)
 train[target_cols] = oof
test[target_cols] = predictions
valid_results = train_targets_scored.drop(columns=target_cols ).merge(train[['sig_id']+target_cols], on='sig_id', how='left' ).fillna(0)
 y_true = train_targets_scored[target_cols].values
y_pred = valid_results[target_cols].values
score = 0
for i in range(len(target_cols)) :
score_ = log_loss(y_true[:, i], y_pred[:, i])
 score += score_ / target.shape[1]
print("hidden_size: ", hidden_size)
 print("CV log_loss: ", score )<merge>
df_train = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 target = df_train['label']
df_train.drop('label', axis=1, inplace=True)
 X_test = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 X_test.drop('id', axis=1, inplace=True)
 X_test.head()
valid_results = train_targets_scored.drop(columns=target_cols ).merge(train[['sig_id']+target_cols], on='sig_id', how='left' ).fillna(0)
 y_true = train_targets_scored[target_cols].values
y_pred = valid_results[target_cols].values
score = 0
for i in range(len(target_cols)) :
score_ = log_loss(y_true[:, i], y_pred[:, i])
 score += score_ / target.shape[1]
print("CV log_loss: ", score )<save_to_csv>
X_train, X_dev, y_train, y_dev = train_test_split(df_train, target, stratify=target, random_state=42, test_size=0.01)
 print('X_train', len(X_train))
print('X_dev', len(X_dev))
print('X_test', len(X_test))
sub = sample_submission.drop(columns=target_cols ).merge(test[['sig_id']+target_cols], on='sig_id', how='left' ).fillna(0)
 sub.to_csv('submission.csv', index=False )<load_from_csv>
class CharData(Dataset):

 def __init__(self,
images,
labels=None,
transform=None, 
):
self.X = images
self.y = labels
self.transform = transform
def __len__(self):
return len(self.X)
 def __getitem__(self, idx):
img = np.array(self.X.iloc[idx, :], dtype='uint8' ).reshape([28, 28, 1])
 if self.transform is not None:
img = self.transform(img)
 if self.y is not None:
y = np.zeros(10, dtype='float32')
 y[self.y.iloc[idx]] = 1
return img, y
else:
return img
plt.rcParams.update({'font.size': 14})
 train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 sub_sample = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv")
 print(train.shape, test.shape, sub_sample.shape )<count_duplicates>
train_transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=5, translate=(0.1, 0.1), scale=(0.9, 1.1), shear=5),
transforms.ToTensor()
])
 test_transform = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor()
] )
train.duplicated().sum()<remove_duplicates>
train_dataset = CharData(X_train, y_train, train_transform)
 dev_dataset = CharData(X_dev, y_dev, test_transform)
 test_dataset = CharData(X_test, transform=test_transform )
train = train.drop_duplicates().reset_index(drop=True )<count_missing_values>
train_loader = DataLoader(dataset=train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)
 dev_loader = DataLoader(dataset=dev_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4)
 test_loader = DataLoader(dataset=test_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4 )
train.isnull().sum()<count_missing_values>
DEPTH_MULT = 2
class ConvLayer(nn.Module):
def __init__(self, input_size, output_size, kernel_size=3):
super(ConvLayer, self ).__init__()
self.ops = nn.Sequential(
 nn.Conv2d(input_size, output_size, kernel_size=kernel_size, stride=1, padding=kernel_size//2),
nn.BatchNorm2d(output_size),
nn.ReLU(inplace=True)
)
 def forward(self, x):
return self.ops(x)
 class FCLayer(nn.Module):
def __init__(self, input_size, output_size):
super(FCLayer, self ).__init__()
self.ops = nn.Sequential(
 nn.Linear(input_size, output_size),
nn.BatchNorm1d(output_size),
nn.ReLU(inplace=True)
)
 self.residual = input_size == output_size
def forward(self, x):
if self.residual:
return(self.ops(x)+ x)/ np.sqrt(2)
 return self.ops(x)
 def mixup(x, shuffle, lam, i, j):
if shuffle is not None and lam is not None and i == j:
x = lam * x +(1 - lam)* x[shuffle]
return x
class Net(nn.Module):
def __init__(self, num_classes):
super(Net, self ).__init__()
self.conv1 = ConvLayer(1, DEPTH_MULT * 32)
 self.conv2 = ConvLayer(DEPTH_MULT * 32, DEPTH_MULT * 32)
 self.conv3 = ConvLayer(DEPTH_MULT * 32, DEPTH_MULT * 32)
 self.conv4 = ConvLayer(DEPTH_MULT * 32, DEPTH_MULT * 32)
 self.conv5 = ConvLayer(DEPTH_MULT * 32, DEPTH_MULT * 64)
 self.conv6 = ConvLayer(DEPTH_MULT * 64, DEPTH_MULT * 64)
 self.conv7 = ConvLayer(DEPTH_MULT * 64, DEPTH_MULT * 64)
 self.conv8 = ConvLayer(DEPTH_MULT * 64, DEPTH_MULT * 64)
 self.conv9 = ConvLayer(DEPTH_MULT * 64, DEPTH_MULT * 64)
 self.conv10 = ConvLayer(DEPTH_MULT * 64, DEPTH_MULT * 64)
 self.mp = nn.MaxPool2d(kernel_size=2, stride=2)
 self.fc1 = FCLayer(DEPTH_MULT * 64 * 7 * 7, DEPTH_MULT * 512)
 self.fc2 = FCLayer(DEPTH_MULT * 512, DEPTH_MULT * 512)
 self.fc3 = FCLayer(DEPTH_MULT * 512, DEPTH_MULT * 512)
 self.fc4 = FCLayer(DEPTH_MULT * 512, DEPTH_MULT * 512)
 self.projection = nn.Linear(DEPTH_MULT * 512, 10)
 def forward(self, x):
if isinstance(x, list):
x, shuffle, lam = x
else:
shuffle = None
lam = None
j = np.random.randint(15)
 x = mixup(x, shuffle, lam, 0, j)
 x = self.conv1(x)
 x = mixup(x, shuffle, lam, 1, j)
 x = self.conv2(x)
 x = mixup(x, shuffle, lam, 2, j)
 x = self.conv3(x)
 x = mixup(x, shuffle, lam, 3, j)
 x = self.conv4(x)
 x = self.mp(x)
 x = mixup(x, shuffle, lam, 4, j)
 x = self.conv5(x)
 x = mixup(x, shuffle, lam, 5, j)
 x = self.conv6(x)
 x = mixup(x, shuffle, lam, 6, j)
 x = self.conv7(x)
 x = mixup(x, shuffle, lam, 7, j)
 x = self.conv8(x)
 x = mixup(x, shuffle, lam, 8, j)
 x = self.conv9(x)
 x = mixup(x, shuffle, lam, 9, j)
 x = self.conv10(x)
 x = self.mp(x)
 x = x.view(x.size(0), -1)
 x = mixup(x, shuffle, lam, 10, j)
 x = self.fc1(x)
 x = mixup(x, shuffle, lam, 11, j)
 x = self.fc2(x)
 x = mixup(x, shuffle, lam, 12, j)
 x = self.fc3(x)
 x = mixup(x, shuffle, lam, 13, j)
 x = self.fc4(x)
 x = mixup(x, shuffle, lam, 14, j)
 x = self.projection(x)
 return x
test.isnull().sum()<count_missing_values>
def criterion(input, target, size_average=True):

 l = -(target * torch.log(F.softmax(input, dim=1)+ 1e-10)).sum(1)
 if size_average:
l = l.mean()
else:
l = l.sum()
return l
test.isnull().sum()<count_unique_values>
model = Net(10)
 model = model.to(device)
 n_epochs = 160
optimizer = optim.Adam(model.parameters() , lr=0.001)
 exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=n_epochs // 4, gamma=0.1 )
print(train.keyword.nunique() , test.keyword.nunique())
 print(set(train.keyword.unique())- set(test.keyword.unique()))<count_unique_values>
def train(epoch, history=None):
model.train()
for batch_idx,(data, target)in enumerate(train_loader):
data = data.to(device)
 target = target.to(device)
 alpha = 2
lam = np.random.beta(alpha, alpha)
 shuffle = torch.randperm(data.shape[0])
 target = lam * target +(1 - lam)* target[shuffle]
optimizer.zero_grad()
output = model([data, shuffle, lam])
 loss = criterion(output, target)
 if history is not None:
history.loc[epoch + batch_idx / len(train_loader), 'train_loss'] = loss.data.cpu().numpy()
loss.backward()
optimizer.step()
if(batch_idx + 1)% 100 == 0:
print('Train Epoch: {} [{}/{}({:.0f}%)]\tLR: {:.6f}\tLoss: {:.6f}'.format(
 epoch,(batch_idx + 1)* len(data), len(train_loader.dataset),
100.*(batch_idx + 1)/ len(train_loader),
optimizer.state_dict() ['param_groups'][0]['lr'],
loss.data))
exp_lr_scheduler.step()
def evaluate(epoch, history=None):
model.eval()
loss = 0
correct = 0
with torch.no_grad() :
for data, target in dev_loader:
data = data.to(device)
 target = target.to(device)
 output = model(data)
 loss += criterion(output, target, size_average=False ).data
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(target.max(1, keepdim=True)[1].data.view_as(pred)).cpu().sum().numpy()
loss /= len(dev_loader.dataset)
 accuracy = correct / len(dev_loader.dataset)
 if history is not None:
history.loc[epoch, 'dev_loss'] = loss.cpu().numpy()
history.loc[epoch, 'dev_accuracy'] = accuracy
print('Dev loss: {:.4f}, Dev accuracy: {}/{}({:.3f}%)
 '.format(
 loss, correct, len(dev_loader.dataset),
100.* accuracy))
print(train.location.nunique() , test.location.nunique() )<categorify>
model.eval()
predictions = []
for data in tqdm(test_loader):
data = data.to(device)
 output = model(data ).max(dim=1)[1]
predictions += list(output.data.cpu().numpy() )
for col in ['keyword','location']:
train[col] = train[col].fillna('None')
 test[col] = test[col].fillna('None')
 def clean_loc(x):
if x == 'None':
return 'None'
elif x == 'Earth' or x =='Worldwide' or x == 'Everywhere':
return 'World'
elif 'New York' in x or 'NYC' in x:
return 'New York'
elif 'London' in x:
return 'London'
elif 'Mumbai' in x:
return 'Mumbai'
elif 'Washington' in x and 'D' in x and 'C' in x:
return 'Washington DC'
elif 'San Francisco' in x:
return 'San Francisco'
elif 'Los Angeles' in x:
return 'Los Angeles'
elif 'Seattle' in x:
return 'Seattle'
elif 'Chicago' in x:
return 'Chicago'
elif 'Toronto' in x:
return 'Toronto'
elif 'Sacramento' in x:
return 'Sacramento'
elif 'Atlanta' in x:
return 'Atlanta'
elif 'California' in x:
return 'California'
elif 'Florida' in x:
return 'Florida'
elif 'Texas' in x:
return 'Texas'
elif 'United States' in x or 'USA' in x:
return 'USA'
elif 'United Kingdom' in x or 'UK' in x or 'Britain' in x:
return 'UK'
elif 'Canada' in x:
return 'Canada'
elif 'India' in x:
return 'India'
elif 'Kenya' in x:
return 'Kenya'
elif 'Nigeria' in x:
return 'Nigeria'
elif 'Australia' in x:
return 'Australia'
elif 'Indonesia' in x:
return 'Indonesia'
elif x in top_loc:
return x
else: return 'Others'
train['location_clean'] = train['location'].apply(lambda x: clean_loc(str(x)))
 test['location_clean'] = test['location'].apply(lambda x: clean_loc(str(x)) )<save_to_csv>
submission = pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 submission['label'] = predictions
submission.to_csv('submission.csv', index=False)
 submission.head()