<jupyter_start><jupyter_text>Image Classifier using Convolutional Neural Networks<jupyter_code>import torch
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
import torch.nn.functional as F
import matplotlib.pyplot as plt
import numpy as np
# Hyper Parameters
num_epochs = 400
batch_size = 64
learning_rate = 0.001
#ImageNet Assumptions
normalize = transforms.Normalize(
	mean=[0.485, 0.456, 0.406],
	std=[0.229, 0.224, 0.225]
)

#Normalize -> Tensor -> Data augmentations (Random cropping and flipping)
preprocess = transforms.Compose([
	transforms.CenterCrop(100),
	transforms.RandomHorizontalFlip(),
	transforms.ToTensor(),
	normalize
])
# Data Set
train_dataset = dsets.ImageFolder(root="train", transform=preprocess)

# Data Loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)

# Data Set
test_dataset = dsets.ImageFolder(root="test", transform=transforms.ToTensor())

# Data Loader
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
# CNN Model (2 conv layer)
class CNN(nn.Module):
	def __init__(self):
		super(CNN, self).__init__()
		self.layer1 = nn.Sequential(
			nn.Conv2d(3, 16, kernel_size=3, padding=1),
			nn.BatchNorm2d(16),
			nn.ReLU(),
			nn.MaxPool2d(2))
		self.layer2 = nn.Sequential(
			nn.Conv2d(16, 32, kernel_size=5, padding=2),
			nn.BatchNorm2d(32),
			nn.ReLU(),
			nn.MaxPool2d(2))
		self.fc1 = nn.Sequential(
			nn.Linear(20000, 500),
			nn.Dropout(0.75),
			nn.ReLU())
		self.fc2 = nn.Sequential(
			nn.Linear(500, 50),
			nn.Dropout(0.75),
			nn.ReLU())
		self.fc3 = nn.Sequential(
			nn.Linear(50, 5),
			nn.Dropout(0.75),
			nn.LogSoftmax())

	def forward(self, x):
		out = self.layer1(x)
		out = self.layer2(out)
		out = out.view(out.size(0), -1) #Flatten out the conv layer to fc layer
		out = self.fc1(out)
		out = self.fc2(out)
		out = self.fc3(out)
		return out
cnn = CNN()
#cnn.load_state_dict(torch.load('cnn.pkl'))

# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate) #Use Adam to optimize
sum_loss = []
# Train the Model
for epoch in range(num_epochs):
	l = 0
	for i, (images, labels) in enumerate(train_loader):
		images = Variable(images)
		labels = Variable(labels)

		# Forward + Backward + Optimize
		optimizer.zero_grad()
		outputs = cnn(images)
		loss = criterion(outputs, labels)
		loss.backward()
		optimizer.step()
		l += loss.data[0]
		if (i+1) % 1 == 0:
			print ('Epoch [%d/%d], Iter [%d/%d] Loss: %.4f'
				   %(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data[0]))
	sum_loss.append(l)
cnn.eval()  # Change model to 'eval' mode (BN uses moving mean/var).
correct = 0
total = 0
for images, labels in train_loader:
	images = Variable(images)
	outputs = cnn(images)
	_, predicted = torch.max(outputs.data, 1)
	total += labels.size(0)
	correct += (predicted == labels).sum()

print('Training Accuracy of the model on the test images: %d %%' % (100 * correct / total))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1410338225909381, 'fraction_numerical': 0.036056158264199105, 'mean_word_length': 4.588235294117647, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '622664', 'license': 'MIT', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1154, 'n_words': 271, 'path': '6000B-flower-Image-Classifier/cnn.ipynb', 'repo_name': 'pranav-ust/hkust-materials', 'size': '3134'}