From 3af2ebd1f2de2a4c1ff4e6185e4711dfaafe5c7b Mon Sep 17 00:00:00 2001 From: ishandutta0098 Date: Thu, 13 Oct 2022 01:42:29 +0530 Subject: [PATCH 01/12] add torch to requirements --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 0fbc1cc4b45c..a9fdd7a4a813 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,3 +18,4 @@ texttable tweepy xgboost yulewalker +torch \ No newline at end of file From d41a827daee81219229bcc3d892592af9d7625bc Mon Sep 17 00:00:00 2001 From: ishandutta0098 Date: Thu, 13 Oct 2022 01:58:17 +0530 Subject: [PATCH 02/12] add lenet architecture in pytorch --- computer_vision/lenet_pytorch.py | 72 ++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 computer_vision/lenet_pytorch.py diff --git a/computer_vision/lenet_pytorch.py b/computer_vision/lenet_pytorch.py new file mode 100644 index 000000000000..1ebedb2a8833 --- /dev/null +++ b/computer_vision/lenet_pytorch.py @@ -0,0 +1,72 @@ +""" +LeNet Network + +Paper: http://vision.stanford.edu/cs598_spring07/papers/Lecun98.pdf +""" + +import torch +import torch.nn as nn + + +class LeNet(nn.Module): + def __init__(self): + super(LeNet, self).__init__() + + self.tanh = nn.Tanh() + self.avgpool = nn.AvgPool2d(kernel_size=2, stride=2) + + self.conv1 = nn.Conv2d( + in_channels=1, + out_channels=6, + kernel_size=(5, 5), + stride=(1, 1), + padding=(0, 0), + ) + self.conv2 = nn.Conv2d( + in_channels=6, + out_channels=16, + kernel_size=(5, 5), + stride=(1, 1), + padding=(0, 0), + ) + self.conv3 = nn.Conv2d( + in_channels=16, + out_channels=120, + kernel_size=(5, 5), + stride=(1, 1), + padding=(0, 0), + ) + + self.linear1 = nn.Linear(120, 84) + self.linear2 = nn.Linear(84, 10) + + def forward(self, x): + x = self.tanh(self.conv1(x)) + x = self.avgpool(x) + x = self.tanh(self.conv2(x)) + x = self.avgpool(x) + x = self.tanh(self.conv3(x)) + + x = x.reshape(x.shape[0], -1) + x = self.tanh(self.linear1(x)) + x = self.linear2(x) + return x + + +def test_model() -> bool: + """ + Test the model on a random input of size [64, 1, 32, 32] + + >>> test_model() + True + + """ + random_image = torch.randn(64, 1, 32, 32) + model = LeNet() + output = model(random_image) + + return output.shape == torch.zeros([64, 10]).shape + + +if __name__ == "__main__": + print(f"Model Passed: {test_model()}") From dbfa015dadb4541435dbe2c2a004566a16b338cd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Oct 2022 20:36:50 +0000 Subject: [PATCH 03/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- computer_vision/lenet_pytorch.py | 2 +- requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/computer_vision/lenet_pytorch.py b/computer_vision/lenet_pytorch.py index 1ebedb2a8833..59a730aa9df3 100644 --- a/computer_vision/lenet_pytorch.py +++ b/computer_vision/lenet_pytorch.py @@ -10,7 +10,7 @@ class LeNet(nn.Module): def __init__(self): - super(LeNet, self).__init__() + super().__init__() self.tanh = nn.Tanh() self.avgpool = nn.AvgPool2d(kernel_size=2, stride=2) diff --git a/requirements.txt b/requirements.txt index a9fdd7a4a813..3e4f02a5745b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,7 +15,7 @@ statsmodels sympy tensorflow texttable +torch tweepy xgboost yulewalker -torch \ No newline at end of file From 224818070cb35af6dc06c9d6d00121c7232d63ae Mon Sep 17 00:00:00 2001 From: ishandutta0098 Date: Thu, 13 Oct 2022 02:11:00 +0530 Subject: [PATCH 04/12] add type hints --- computer_vision/lenet_pytorch.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/computer_vision/lenet_pytorch.py b/computer_vision/lenet_pytorch.py index 1ebedb2a8833..63e949be8ed3 100644 --- a/computer_vision/lenet_pytorch.py +++ b/computer_vision/lenet_pytorch.py @@ -4,12 +4,13 @@ Paper: http://vision.stanford.edu/cs598_spring07/papers/Lecun98.pdf """ +import numpy import torch import torch.nn as nn class LeNet(nn.Module): - def __init__(self): + def __init__(self) -> None: super(LeNet, self).__init__() self.tanh = nn.Tanh() @@ -40,7 +41,7 @@ def __init__(self): self.linear1 = nn.Linear(120, 84) self.linear2 = nn.Linear(84, 10) - def forward(self, x): + def forward(self, x: numpy.ndarray) -> numpy.ndarray: x = self.tanh(self.conv1(x)) x = self.avgpool(x) x = self.tanh(self.conv2(x)) From 0d14ebbbb7a79302cebe2f7846a2feb3972b56cc Mon Sep 17 00:00:00 2001 From: ishandutta0098 Date: Thu, 13 Oct 2022 02:21:08 +0530 Subject: [PATCH 05/12] remove file --- computer_vision/lenet_pytorch.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/computer_vision/lenet_pytorch.py b/computer_vision/lenet_pytorch.py index 63e949be8ed3..1def77e8f065 100644 --- a/computer_vision/lenet_pytorch.py +++ b/computer_vision/lenet_pytorch.py @@ -10,8 +10,8 @@ class LeNet(nn.Module): - def __init__(self) -> None: - super(LeNet, self).__init__() + def __init__(self): + super().__init__() self.tanh = nn.Tanh() self.avgpool = nn.AvgPool2d(kernel_size=2, stride=2) From a6121d65474a375f3c4b3a4adb09886f2952e689 Mon Sep 17 00:00:00 2001 From: ishandutta0098 Date: Thu, 13 Oct 2022 02:24:21 +0530 Subject: [PATCH 06/12] add type hints --- computer_vision/lenet_pytorch.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/computer_vision/lenet_pytorch.py b/computer_vision/lenet_pytorch.py index 1def77e8f065..63e949be8ed3 100644 --- a/computer_vision/lenet_pytorch.py +++ b/computer_vision/lenet_pytorch.py @@ -10,8 +10,8 @@ class LeNet(nn.Module): - def __init__(self): - super().__init__() + def __init__(self) -> None: + super(LeNet, self).__init__() self.tanh = nn.Tanh() self.avgpool = nn.AvgPool2d(kernel_size=2, stride=2) From b08a3c8f3b8759378ad27db1eea0468cbc796515 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Oct 2022 20:56:07 +0000 Subject: [PATCH 07/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- computer_vision/lenet_pytorch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/computer_vision/lenet_pytorch.py b/computer_vision/lenet_pytorch.py index 63e949be8ed3..8380ddf25221 100644 --- a/computer_vision/lenet_pytorch.py +++ b/computer_vision/lenet_pytorch.py @@ -11,7 +11,7 @@ class LeNet(nn.Module): def __init__(self) -> None: - super(LeNet, self).__init__() + super().__init__() self.tanh = nn.Tanh() self.avgpool = nn.AvgPool2d(kernel_size=2, stride=2) From ad84dc96be30ee29334a25bcc32cf2eccb49270d Mon Sep 17 00:00:00 2001 From: ishandutta0098 Date: Thu, 13 Oct 2022 02:28:17 +0530 Subject: [PATCH 08/12] update variable name --- computer_vision/lenet_pytorch.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/computer_vision/lenet_pytorch.py b/computer_vision/lenet_pytorch.py index 8380ddf25221..1cd48325c7f3 100644 --- a/computer_vision/lenet_pytorch.py +++ b/computer_vision/lenet_pytorch.py @@ -41,17 +41,17 @@ def __init__(self) -> None: self.linear1 = nn.Linear(120, 84) self.linear2 = nn.Linear(84, 10) - def forward(self, x: numpy.ndarray) -> numpy.ndarray: - x = self.tanh(self.conv1(x)) - x = self.avgpool(x) - x = self.tanh(self.conv2(x)) - x = self.avgpool(x) - x = self.tanh(self.conv3(x)) - - x = x.reshape(x.shape[0], -1) - x = self.tanh(self.linear1(x)) - x = self.linear2(x) - return x + def forward(self, image_array: numpy.ndarray) -> numpy.ndarray: + image_array = self.tanh(self.conv1(image_array)) + image_array = self.avgpool(image_array) + image_array = self.tanh(self.conv2(image_array)) + image_array = self.avgpool(image_array) + image_array = self.tanh(self.conv3(image_array)) + + image_array = image_array.reshape(image_array.shape[0], -1) + image_array = self.tanh(self.linear1(image_array)) + image_array = self.linear2(image_array) + return image_array def test_model() -> bool: From 43d2128ae44f88d9299c19c8b2a55406e9826e81 Mon Sep 17 00:00:00 2001 From: ishandutta0098 Date: Tue, 20 Dec 2022 11:49:23 +0530 Subject: [PATCH 09/12] add fail test --- computer_vision/lenet_pytorch.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/computer_vision/lenet_pytorch.py b/computer_vision/lenet_pytorch.py index 1cd48325c7f3..89393b9dd393 100644 --- a/computer_vision/lenet_pytorch.py +++ b/computer_vision/lenet_pytorch.py @@ -54,20 +54,28 @@ def forward(self, image_array: numpy.ndarray) -> numpy.ndarray: return image_array -def test_model() -> bool: +def test_model(image_tensor: torch.tensor) -> bool: """ - Test the model on a random input of size [64, 1, 32, 32] + Test the model on an input batch of 64 images - >>> test_model() + Args: + image_tensor (torch.tensor): Batch of Images for the model + + >>> test_model(torch.randn(64, 1, 32, 32)) True """ - random_image = torch.randn(64, 1, 32, 32) - model = LeNet() - output = model(random_image) + try: + model = LeNet() + output = model(image_tensor) + except: + return False return output.shape == torch.zeros([64, 10]).shape - if __name__ == "__main__": - print(f"Model Passed: {test_model()}") + random_image_1 = torch.randn(64, 1, 32, 32) + random_image_2 = torch.randn(1, 32, 32) + + print(f"random_image_1 Model Passed: {test_model(random_image_1)}") + print(f"random_image_2 Model Passed: {test_model(random_image_2)}") \ No newline at end of file From 6bf6e91fdb2348f2d4ce57b31a0ecaf1bf155992 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 20 Dec 2022 06:20:44 +0000 Subject: [PATCH 10/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- computer_vision/lenet_pytorch.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/computer_vision/lenet_pytorch.py b/computer_vision/lenet_pytorch.py index 89393b9dd393..2227010b2d09 100644 --- a/computer_vision/lenet_pytorch.py +++ b/computer_vision/lenet_pytorch.py @@ -65,7 +65,7 @@ def test_model(image_tensor: torch.tensor) -> bool: True """ - try: + try: model = LeNet() output = model(image_tensor) except: @@ -73,9 +73,10 @@ def test_model(image_tensor: torch.tensor) -> bool: return output.shape == torch.zeros([64, 10]).shape + if __name__ == "__main__": random_image_1 = torch.randn(64, 1, 32, 32) random_image_2 = torch.randn(1, 32, 32) print(f"random_image_1 Model Passed: {test_model(random_image_1)}") - print(f"random_image_2 Model Passed: {test_model(random_image_2)}") \ No newline at end of file + print(f"random_image_2 Model Passed: {test_model(random_image_2)}") From f2a2e781b92c6b946aaecf0c4fadc66c981d36c7 Mon Sep 17 00:00:00 2001 From: ishandutta0098 Date: Tue, 20 Dec 2022 12:36:55 +0530 Subject: [PATCH 11/12] add newline --- computer_vision/lenet_pytorch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/computer_vision/lenet_pytorch.py b/computer_vision/lenet_pytorch.py index 2227010b2d09..367cfc1d734c 100644 --- a/computer_vision/lenet_pytorch.py +++ b/computer_vision/lenet_pytorch.py @@ -79,4 +79,4 @@ def test_model(image_tensor: torch.tensor) -> bool: random_image_2 = torch.randn(1, 32, 32) print(f"random_image_1 Model Passed: {test_model(random_image_1)}") - print(f"random_image_2 Model Passed: {test_model(random_image_2)}") + print(f"\nrandom_image_2 Model Passed: {test_model(random_image_2)}") From a5227d901417822ab90d90b4e74f19975fbd7de8 Mon Sep 17 00:00:00 2001 From: ishandutta0098 Date: Tue, 20 Dec 2022 12:37:54 +0530 Subject: [PATCH 12/12] reformatting --- computer_vision/lenet_pytorch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/computer_vision/lenet_pytorch.py b/computer_vision/lenet_pytorch.py index 367cfc1d734c..177a5ebfcdb4 100644 --- a/computer_vision/lenet_pytorch.py +++ b/computer_vision/lenet_pytorch.py @@ -68,7 +68,7 @@ def test_model(image_tensor: torch.tensor) -> bool: try: model = LeNet() output = model(image_tensor) - except: + except RuntimeError: return False return output.shape == torch.zeros([64, 10]).shape