From 4fd289f38a4158c919ab5e196e20c1884083ff9b Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Tue, 11 Jun 2024 07:19:53 +1000 Subject: [PATCH 1/3] Create Image_Classification_Example_MNIST_CNN.py Image Classification Example --- .../Image_Classification_Example_MNIST_CNN.py | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 computer_vision/Image_Classification_Example_MNIST_CNN.py diff --git a/computer_vision/Image_Classification_Example_MNIST_CNN.py b/computer_vision/Image_Classification_Example_MNIST_CNN.py new file mode 100644 index 000000000000..b80707a11978 --- /dev/null +++ b/computer_vision/Image_Classification_Example_MNIST_CNN.py @@ -0,0 +1,141 @@ +""" +This Script contains the default EMNIST code for comparison. + +Execution Details are available here: +https://www.kaggle.com/code/dipuk0506/mnist-cnn + +The code is modified from: + nextjournal.com/gkoehler/pytorch-mnist + +@author: Dipu Kabir +""" + +import torch, torchvision +import torch.nn as nn +import torch.nn.functional as F +import torch.optim as optim + +n_epochs = 8 +batch_size_train = 64 +batch_size_test = 1000 +learning_rate = 0.01 +momentum = 0.5 +log_interval = 100 + + +torch.backends.cudnn.enabled = False + + +train_loader = torch.utils.data.DataLoader( + torchvision.datasets.MNIST('/files/', train=True, download=True, + transform=torchvision.transforms.Compose([ + torchvision.transforms.ToTensor(), + torchvision.transforms.Normalize( + (0.1307,), (0.3081,)) + ])), + batch_size=batch_size_train, shuffle=True) + +test_loader = torch.utils.data.DataLoader( + torchvision.datasets.MNIST('/files/', train=False, download=True, + transform=torchvision.transforms.Compose([ + torchvision.transforms.ToTensor(), + torchvision.transforms.Normalize( + (0.1307,), (0.3081,)) + ])), + batch_size=batch_size_test, shuffle=True) + +examples = enumerate(test_loader) +batch_idx, (example_data, example_targets) = next(examples) + +print(example_data.shape) + +import matplotlib.pyplot as plt + +fig = plt.figure() +for i in range(6): + plt.subplot(2,3,i+1) + plt.tight_layout() + plt.imshow(example_data[i][0], cmap='gray', interpolation='none') + plt.title("Ground Truth: {}".format(example_targets[i])) + plt.xticks([]) + plt.yticks([]) +fig + + +class Net(nn.Module): + def __init__(self): + super(Net, self).__init__() + self.conv1 = nn.Conv2d(1, 10, kernel_size=5) + self.conv2 = nn.Conv2d(10, 20, kernel_size=5) + self.conv2_drop = nn.Dropout2d() + self.fc1 = nn.Linear(320, 50) + self.fc2 = nn.Linear(50, 10) + + def forward(self, x): + x = F.relu(F.max_pool2d(self.conv1(x), 2)) + x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) + x = x.view(-1, 320) + x = F.relu(self.fc1(x)) + x = F.dropout(x, training=self.training) + x = self.fc2(x) + return F.log_softmax(x) + + + +network = Net() +optimizer = optim.SGD(network.parameters(), lr=learning_rate, + momentum=momentum) + + +train_losses = [] +train_counter = [] +test_losses = [] +test_counter = [i*len(train_loader.dataset) for i in range(n_epochs + 1)] + + +def train(epoch): + network.train() + for batch_idx, (data, target) in enumerate(train_loader): + optimizer.zero_grad() + output = network(data) + loss = F.nll_loss(output, target) + loss.backward() + optimizer.step() + if batch_idx % log_interval == 0: + print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( + epoch, batch_idx * len(data), len(train_loader.dataset), + 100. * batch_idx / len(train_loader), loss.item())) + train_losses.append(loss.item()) + train_counter.append( + (batch_idx*64) + ((epoch-1)*len(train_loader.dataset))) + +def test(): + network.eval() + test_loss = 0 + correct = 0 + with torch.no_grad(): + for data, target in test_loader: + output = network(data) + test_loss += F.nll_loss(output, target, size_average=False).item() + pred = output.data.max(1, keepdim=True)[1] + correct += pred.eq(target.data.view_as(pred)).sum() + test_loss /= len(test_loader.dataset) + test_losses.append(test_loss) + print('\nTest set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( + test_loss, correct, len(test_loader.dataset), + 100. * correct / len(test_loader.dataset))) + + +test() +for epoch in range(1, n_epochs + 1): + train(epoch) + test() +#%% + +fig = plt.figure() +plt.plot(train_counter, train_losses, color='blue') +plt.scatter(test_counter, test_losses, color='red') +plt.legend(['Train Loss', 'Test Loss'], loc='upper right') +plt.xlabel('number of training examples seen') +plt.ylabel('negative log likelihood loss') +fig From 29671f5efd92fd35abc684565415566125ef291e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:28:16 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../Image_Classification_Example_MNIST_CNN.py | 157 ++++++++++-------- 1 file changed, 91 insertions(+), 66 deletions(-) diff --git a/computer_vision/Image_Classification_Example_MNIST_CNN.py b/computer_vision/Image_Classification_Example_MNIST_CNN.py index b80707a11978..d9e34f7cf28e 100644 --- a/computer_vision/Image_Classification_Example_MNIST_CNN.py +++ b/computer_vision/Image_Classification_Example_MNIST_CNN.py @@ -1,7 +1,7 @@ """ This Script contains the default EMNIST code for comparison. -Execution Details are available here: +Execution Details are available here: https://www.kaggle.com/code/dipuk0506/mnist-cnn The code is modified from: @@ -27,22 +27,36 @@ train_loader = torch.utils.data.DataLoader( - torchvision.datasets.MNIST('/files/', train=True, download=True, - transform=torchvision.transforms.Compose([ - torchvision.transforms.ToTensor(), - torchvision.transforms.Normalize( - (0.1307,), (0.3081,)) - ])), - batch_size=batch_size_train, shuffle=True) + torchvision.datasets.MNIST( + "/files/", + train=True, + download=True, + transform=torchvision.transforms.Compose( + [ + torchvision.transforms.ToTensor(), + torchvision.transforms.Normalize((0.1307,), (0.3081,)), + ] + ), + ), + batch_size=batch_size_train, + shuffle=True, +) test_loader = torch.utils.data.DataLoader( - torchvision.datasets.MNIST('/files/', train=False, download=True, - transform=torchvision.transforms.Compose([ - torchvision.transforms.ToTensor(), - torchvision.transforms.Normalize( - (0.1307,), (0.3081,)) - ])), - batch_size=batch_size_test, shuffle=True) + torchvision.datasets.MNIST( + "/files/", + train=False, + download=True, + transform=torchvision.transforms.Compose( + [ + torchvision.transforms.ToTensor(), + torchvision.transforms.Normalize((0.1307,), (0.3081,)), + ] + ), + ), + batch_size=batch_size_test, + shuffle=True, +) examples = enumerate(test_loader) batch_idx, (example_data, example_targets) = next(examples) @@ -53,12 +67,12 @@ fig = plt.figure() for i in range(6): - plt.subplot(2,3,i+1) - plt.tight_layout() - plt.imshow(example_data[i][0], cmap='gray', interpolation='none') - plt.title("Ground Truth: {}".format(example_targets[i])) - plt.xticks([]) - plt.yticks([]) + plt.subplot(2, 3, i + 1) + plt.tight_layout() + plt.imshow(example_data[i][0], cmap="gray", interpolation="none") + plt.title("Ground Truth: {}".format(example_targets[i])) + plt.xticks([]) + plt.yticks([]) fig @@ -79,63 +93,74 @@ def forward(self, x): x = F.dropout(x, training=self.training) x = self.fc2(x) return F.log_softmax(x) - - - + + network = Net() -optimizer = optim.SGD(network.parameters(), lr=learning_rate, - momentum=momentum) +optimizer = optim.SGD(network.parameters(), lr=learning_rate, momentum=momentum) train_losses = [] train_counter = [] test_losses = [] -test_counter = [i*len(train_loader.dataset) for i in range(n_epochs + 1)] +test_counter = [i * len(train_loader.dataset) for i in range(n_epochs + 1)] def train(epoch): - network.train() - for batch_idx, (data, target) in enumerate(train_loader): - optimizer.zero_grad() - output = network(data) - loss = F.nll_loss(output, target) - loss.backward() - optimizer.step() - if batch_idx % log_interval == 0: - print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( - epoch, batch_idx * len(data), len(train_loader.dataset), - 100. * batch_idx / len(train_loader), loss.item())) - train_losses.append(loss.item()) - train_counter.append( - (batch_idx*64) + ((epoch-1)*len(train_loader.dataset))) - + network.train() + for batch_idx, (data, target) in enumerate(train_loader): + optimizer.zero_grad() + output = network(data) + loss = F.nll_loss(output, target) + loss.backward() + optimizer.step() + if batch_idx % log_interval == 0: + print( + "Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}".format( + epoch, + batch_idx * len(data), + len(train_loader.dataset), + 100.0 * batch_idx / len(train_loader), + loss.item(), + ) + ) + train_losses.append(loss.item()) + train_counter.append( + (batch_idx * 64) + ((epoch - 1) * len(train_loader.dataset)) + ) + + def test(): - network.eval() - test_loss = 0 - correct = 0 - with torch.no_grad(): - for data, target in test_loader: - output = network(data) - test_loss += F.nll_loss(output, target, size_average=False).item() - pred = output.data.max(1, keepdim=True)[1] - correct += pred.eq(target.data.view_as(pred)).sum() - test_loss /= len(test_loader.dataset) - test_losses.append(test_loss) - print('\nTest set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( - test_loss, correct, len(test_loader.dataset), - 100. * correct / len(test_loader.dataset))) - - + network.eval() + test_loss = 0 + correct = 0 + with torch.no_grad(): + for data, target in test_loader: + output = network(data) + test_loss += F.nll_loss(output, target, size_average=False).item() + pred = output.data.max(1, keepdim=True)[1] + correct += pred.eq(target.data.view_as(pred)).sum() + test_loss /= len(test_loader.dataset) + test_losses.append(test_loss) + print( + "\nTest set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n".format( + test_loss, + correct, + len(test_loader.dataset), + 100.0 * correct / len(test_loader.dataset), + ) + ) + + test() for epoch in range(1, n_epochs + 1): - train(epoch) - test() -#%% + train(epoch) + test() +# %% fig = plt.figure() -plt.plot(train_counter, train_losses, color='blue') -plt.scatter(test_counter, test_losses, color='red') -plt.legend(['Train Loss', 'Test Loss'], loc='upper right') -plt.xlabel('number of training examples seen') -plt.ylabel('negative log likelihood loss') +plt.plot(train_counter, train_losses, color="blue") +plt.scatter(test_counter, test_losses, color="red") +plt.legend(["Train Loss", "Test Loss"], loc="upper right") +plt.xlabel("number of training examples seen") +plt.ylabel("negative log likelihood loss") fig From 4e3484a3cf8b34a4166f96d4f3448785dff6e50d Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Tue, 11 Jun 2024 14:18:40 +1000 Subject: [PATCH 3/3] Update and rename Image_Classification_Example_MNIST_CNN.py to image_classification_example_mnist_cnn.py --- ...image_classification_example_mnist_cnn.py} | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) rename computer_vision/{Image_Classification_Example_MNIST_CNN.py => image_classification_example_mnist_cnn.py} (88%) diff --git a/computer_vision/Image_Classification_Example_MNIST_CNN.py b/computer_vision/image_classification_example_mnist_cnn.py similarity index 88% rename from computer_vision/Image_Classification_Example_MNIST_CNN.py rename to computer_vision/image_classification_example_mnist_cnn.py index d9e34f7cf28e..5aa8761ca588 100644 --- a/computer_vision/Image_Classification_Example_MNIST_CNN.py +++ b/computer_vision/image_classification_example_mnist_cnn.py @@ -4,15 +4,16 @@ Execution Details are available here: https://www.kaggle.com/code/dipuk0506/mnist-cnn -The code is modified from: +The code is improved from: nextjournal.com/gkoehler/pytorch-mnist @author: Dipu Kabir """ -import torch, torchvision +import torch +import torchvision import torch.nn as nn -import torch.nn.functional as F +import torch.nn.functional as functional import torch.optim as optim n_epochs = 8 @@ -86,13 +87,13 @@ def __init__(self): self.fc2 = nn.Linear(50, 10) def forward(self, x): - x = F.relu(F.max_pool2d(self.conv1(x), 2)) - x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) + x = functional.relu(functional.max_pool2d(self.conv1(x), 2)) + x = functional.relu(functional.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) x = x.view(-1, 320) - x = F.relu(self.fc1(x)) - x = F.dropout(x, training=self.training) + x = functional.relu(self.fc1(x)) + x = functional.dropout(x, training=self.training) x = self.fc2(x) - return F.log_softmax(x) + return functional.log_softmax(x) network = Net() @@ -110,7 +111,7 @@ def train(epoch): for batch_idx, (data, target) in enumerate(train_loader): optimizer.zero_grad() output = network(data) - loss = F.nll_loss(output, target) + loss = functional.nll_loss(output, target) loss.backward() optimizer.step() if batch_idx % log_interval == 0: @@ -136,7 +137,7 @@ def test(): with torch.no_grad(): for data, target in test_loader: output = network(data) - test_loss += F.nll_loss(output, target, size_average=False).item() + test_loss += functional.nll_loss(output, target, size_average=False).item() pred = output.data.max(1, keepdim=True)[1] correct += pred.eq(target.data.view_as(pred)).sum() test_loss /= len(test_loader.dataset)