Kaggle competition: Kannada MNIST
train_df.loc[train_df['text_pure'] == "look state action year ferguson upheav", 'target_new'] = 1
df_mislabeled = train_df[train_df['text_pure'].isin(df_mislabeled_all)]
filter_mislabel =(df_mislabeled['target'] != df_mislabeled['target_new'])
 print(f'Number of relabeled: {len(df_mislabeled[filter_mislabel])}')
 df_mislabeled<drop_column>
print("Reading data")
 train_set = pd.read_csv("/kaggle/input/Kannada-MNIST/train.csv")
 val_set = pd.read_csv("/kaggle/input/Kannada-MNIST/Dig-MNIST.csv")
 test_set = pd.read_csv("/kaggle/input/Kannada-MNIST/test.csv")
 train_images = train_set.iloc[:, 1:]
train_labels = train_set.iloc[:, 0]
val_images = val_set.iloc[:, 1:]
val_labels = val_set.iloc[:, 0]
test_images = test_set.iloc[:, 1:]
train_df = train_df.drop('text_pure', axis=1 )<define_variables>
train_transform = transforms.Compose(( [
transforms.ToPILImage() ,
transforms.RandomCrop(28),
transforms.RandomAffine(degrees=5, translate=(0.1, 0.1)) ,
transforms.ToTensor() ,
]))
test_transform = transforms.Compose(( [
transforms.ToPILImage() ,
transforms.ToTensor() ,
]))
ids_target1_error = [
328,443,513,791,794,882,883,886,890,893,894,896,923,926,928,1688,1709,2033,
2063,2619,2885,3097,3640,3802,3837,3842,3900,4026,4342,4530,4533,4575,4773,
4778,4790,5223,5781,6552,6554,6570,6701,6702,6729,6731,6745,6861,6945,6965,
7201,7226,7231,7264,7494,7797,8309,8317,8329,8330,8905,8908,8913,8916,8926,
8934,8939,8972,9337,9446,9775,9791,9808,10127,10543,10552 ]
print(f'Number of ids with target1 error: {len(ids_target1_error)}' )<feature_engineering>
class KannadaDataset(torch.utils.data.Dataset):
def __init__(self, images, labels=None, transforms=None):
self.X = images
self.y = labels
self.transforms = transforms
def __len__(self):
return len(self.X)
 def __getitem__(self, i):
data = self.X.iloc[i, :]
data = np.array(data ).astype(np.uint8 ).reshape(28, 28, 1)
 if self.transforms:
data = self.transforms(data)
 if self.y is not None:
return(data, self.y[i])
 else:
return data
print("Dataset class created" )
if ids_error_corrected:
train_df.at[train_df['id'].isin(ids_target1_error),'target_new'] = 0
train_df[train_df['id'].isin(ids_target1_error)][:20]<feature_engineering>
print("Defining network architecture")
 class network(nn.Module):
def __init__(self):
super(network, self ).__init__()
self.conv1_5 = nn.Conv2d(in_channels=1, out_channels=64, kernel_size=5, padding=2)
 self.conv1_5_bn = nn.BatchNorm2d(num_features=64)
 self.conv1_3 = nn.Conv2d(in_channels=1, out_channels=64, kernel_size=3, padding=1)
 self.conv1_3_bn = nn.BatchNorm2d(num_features=64)
 self.conv2_5 = nn.Conv2d(in_channels=128, out_channels=128, kernel_size=5, padding=2)
 self.conv2_5_bn = nn.BatchNorm2d(num_features=128)
 self.conv2_3 = nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1)
 self.conv2_3_bn = nn.BatchNorm2d(num_features=128)
 self.conv3_5 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=5, padding=2)
 self.conv3_5_bn = nn.BatchNorm2d(num_features=256)
 self.conv3_3 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1)
 self.conv3_3_bn = nn.BatchNorm2d(num_features=256)
 self.fc1 = nn.Linear(in_features=512*6*6, out_features=512)
 self.fc1_bn = nn.BatchNorm1d(num_features=512)
 self.fc2 = nn.Linear(in_features=512, out_features=256)
 self.fc2_bn = nn.BatchNorm1d(num_features=256)
 self.fc3 = nn.Linear(in_features=256, out_features=128)
 self.fc3_bn = nn.BatchNorm1d(num_features=128)
 self.fc4 = nn.Linear(in_features=128, out_features=64)
 self.fc4_bn = nn.BatchNorm1d(num_features=64)
 self.out = nn.Linear(in_features=64, out_features=10)
 def forward(self, x):
x_5 = F.relu(self.conv1_5_bn(self.conv1_5(x)))
 x_3 = F.relu(self.conv1_3_bn(self.conv1_3(x)))
 x = F.max_pool2d(torch.cat(( x_5, x_3), dim=1), kernel_size=2, stride=2)
 x_5 = F.relu(self.conv2_5_bn(self.conv2_5(x)))
 x_3 = F.relu(self.conv2_3_bn(self.conv2_3(x)))
 x = F.max_pool2d(torch.cat(( x_5, x_3), dim=1), kernel_size=2, stride=2)
 x_5 = F.relu(self.conv3_5_bn(self.conv3_5(x)))
 x_3 = F.relu(self.conv3_3_bn(self.conv3_3(x)))
 x = F.max_pool2d(torch.cat(( x_5, x_3), dim=1), kernel_size=2, stride=1)
 x = F.relu(self.fc1_bn(self.fc1(x.view(-1, 6*6*512))))
x = F.relu(self.fc2_bn(self.fc2(x)))
 x = F.relu(self.fc3_bn(self.fc3(x)))
 x = F.relu(self.fc4_bn(self.fc4(x)))
 x = self.out(x)
 return x
if ids_error_corrected:
train_df.at[train_df['id'].isin(ids_target0_error),'target_new'] = 1
train_df[train_df['id'].isin(ids_target0_error)]<feature_engineering>
net = network().to(device)
 lr = 0.001
batch_size = 100
max_epochs = 10
fig, axes = plt.subplots(1, 2, figsize=(18, 8))
axes = axes.flatten()
train_df['text_lemma'] = train_df['text'].apply(lambda x: remove_stopwords(x))
train_df['text_lemma'] = train_df['text_lemma'].apply(lambda x: lemmatizing(x))
lis = [train_df[train_df['target_new'] == 0]['text_lemma'],
train_df[train_df['target_new'] == 1]['text_lemma']]
for i, j in zip(lis, axes):
new = i.str.split()
new = new.values.tolist()
corpus = [word for i in new for word in i]
counter = Counter(corpus)
 most = counter.most_common()
x, y = [], []
for word, count in most[:30]:
if(word not in stop_words):
x.append(word)
 y.append(count)
 sns.barplot(x=y, y=x, palette='plasma', ax=j)
 axes[0].set_title('Non Disaster Tweets')
 axes[1].set_title('Disaster Tweets')
 axes[0].set_xlabel('Count')
 axes[0].set_ylabel('Word')
 axes[1].set_xlabel('Count')
 axes[1].set_ylabel('Word')
 fig.suptitle('Most Common Unigrams', fontsize=24, va='baseline')
 plt.tight_layout()<train_on_grid>
train_set = KannadaDataset(train_images, train_labels, train_transform)
 val_set = KannadaDataset(val_images, val_labels, test_transform)
 train_data = DataLoader(train_set, batch_size=batch_size, shuffle=True)
 val_data = DataLoader(val_set, batch_size=batch_size, shuffle=False)
 print("Begin training" )
def ngrams(n, title):
fig, axes = plt.subplots(1, 2, figsize=(18, 8))
axes = axes.flatten()
for i, j in zip(lis, axes):
new = i.str.split()
new = new.values.tolist()
corpus = [word for i in new for word in i]
def _get_top_ngram(corpus, n=None):
vec = CountVectorizer(ngram_range=(n, n), max_df=0.9,
stop_words='english' ).fit(corpus)
 bag_of_words = vec.transform(corpus)
 sum_words = bag_of_words.sum(axis=0)
 words_freq = [(word, sum_words[0, idx])
 for word, idx in vec.vocabulary_.items() ]
words_freq = sorted(words_freq, key=lambda x: x[1], reverse=True)
 return words_freq[:15]
top_n_bigrams = _get_top_ngram(i, n)[:15]
x, y = map(list, zip(*top_n_bigrams))
sns.barplot(x=y, y=x, palette='plasma', ax=j)
 axes[0].set_title('Non Disaster Tweets')
 axes[1].set_title('Disaster Tweets')
 axes[0].set_xlabel('Count')
 axes[0].set_ylabel('Words')
 axes[1].set_xlabel('Count')
 axes[1].set_ylabel('Words')
 fig.suptitle(title, fontsize=24, va='baseline')
 plt.tight_layout()<set_options>
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters() , lr=lr, momentum=0.9)
 scheduler = optim.lr_scheduler.StepLR(optimizer, 4, gamma=0.1)
 for epoch in range(max_epochs):
epoch_loss = 0.
 epoch_accuracy = 0.
 net.train()
for i,(images, labels)in enumerate(train_data):
images = images.to(device)
 labels = labels.to(device)
 optimizer.zero_grad()
outputs = net(images)
 loss = criterion(outputs, labels)
 loss.backward()
optimizer.step()
_, predicted = torch.max(outputs, 1)
 if(i+1)% 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, max_epochs, i+1, len(train_set)//batch_size, loss.item()))
net.eval()
with torch.no_grad() :
val_accuracy = 0.
 for images, labels in val_data:
images = images.to(device)
 labels = labels.to(device)
 outputs = net(images)
 _, predicted = torch.max(outputs, 1)
 val_accuracy +=(predicted==labels ).sum().item()
print('Validation accuracy is: {} %'.format(100 * val_accuracy / len(val_set)))
 scheduler.step()
ngrams(2, 'Most Common Bigrams' )<load_from_url>
test_set = KannadaDataset(images=test_images, transforms=test_transform)
 test_data = DataLoader(test_set, batch_size=1, shuffle=False)
 net.eval()
predictions = torch.LongTensor().to(device)
 with torch.no_grad() :
for images in test_data:
images = images.to(device)
 outputs = net(images)
 predictions = torch.cat(( predictions, outputs.argmax(dim=1)) , dim=0)
 submission = pd.read_csv("/kaggle/input/Kannada-MNIST/sample_submission.csv")
 submission['label'] = predictions.cpu().numpy()
submission.to_csv("/kaggle/working/submission.csv", index=False)
 print("Output written to csv file!" )