Upload 4 files
Browse files
cat.jpg
ADDED
datasets.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
"""
|
3 |
+
Module containing wrapper classes for PyTorch Datasets
|
4 |
+
Author: Shilpaj Bhalerao
|
5 |
+
Date: Jun 25, 2023
|
6 |
+
"""
|
7 |
+
# Standard Library Imports
|
8 |
+
from typing import Tuple
|
9 |
+
|
10 |
+
# Third-Party Imports
|
11 |
+
from torchvision import datasets, transforms
|
12 |
+
|
13 |
+
|
14 |
+
class AlbumDataset(datasets.CIFAR10):
|
15 |
+
"""
|
16 |
+
Wrapper class to use albumentations library with PyTorch Dataset
|
17 |
+
"""
|
18 |
+
def __init__(self, root: str = "./data", train: bool = True, download: bool = True, transform: list = None):
|
19 |
+
"""
|
20 |
+
Constructor
|
21 |
+
:param root: Directory at which data is stored
|
22 |
+
:param train: Param to distinguish if data is training or test
|
23 |
+
:param download: Param to download the dataset from source
|
24 |
+
:param transform: List of transformation to be performed on the dataset
|
25 |
+
"""
|
26 |
+
super().__init__(root=root, train=train, download=download, transform=transform)
|
27 |
+
|
28 |
+
def __getitem__(self, index: int) -> Tuple:
|
29 |
+
"""
|
30 |
+
Method to return image and its label
|
31 |
+
:param index: Index of image and label in the dataset
|
32 |
+
"""
|
33 |
+
image, label = self.data[index], self.targets[index]
|
34 |
+
|
35 |
+
if self.transform:
|
36 |
+
transformed = self.transform(image=image)
|
37 |
+
image = transformed["image"]
|
38 |
+
return image, label
|
dog.jpg
ADDED
resnet.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
ResNet in PyTorch.
|
3 |
+
For Pre-activation ResNet, see 'preact_resnet.py'.
|
4 |
+
|
5 |
+
Reference:
|
6 |
+
[1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
|
7 |
+
Deep Residual Learning for Image Recognition. arXiv:1512.03385
|
8 |
+
"""
|
9 |
+
import torch.nn as nn
|
10 |
+
import torch.nn.functional as F
|
11 |
+
|
12 |
+
|
13 |
+
class BasicBlock(nn.Module):
|
14 |
+
expansion = 1
|
15 |
+
|
16 |
+
def __init__(self, in_planes, planes, stride=1):
|
17 |
+
super(BasicBlock, self).__init__()
|
18 |
+
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
|
19 |
+
self.bn1 = nn.BatchNorm2d(planes)
|
20 |
+
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
|
21 |
+
self.bn2 = nn.BatchNorm2d(planes)
|
22 |
+
|
23 |
+
self.shortcut = nn.Sequential()
|
24 |
+
if stride != 1 or in_planes != self.expansion*planes:
|
25 |
+
self.shortcut = nn.Sequential(
|
26 |
+
nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
|
27 |
+
nn.BatchNorm2d(self.expansion*planes)
|
28 |
+
)
|
29 |
+
|
30 |
+
def forward(self, x):
|
31 |
+
out = F.relu(self.bn1(self.conv1(x)))
|
32 |
+
out = self.bn2(self.conv2(out))
|
33 |
+
out += self.shortcut(x)
|
34 |
+
out = F.relu(out)
|
35 |
+
return out
|
36 |
+
|
37 |
+
|
38 |
+
class ResNet(nn.Module):
|
39 |
+
def __init__(self, block, num_blocks, num_classes=10):
|
40 |
+
super(ResNet, self).__init__()
|
41 |
+
self.in_planes = 64
|
42 |
+
|
43 |
+
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
|
44 |
+
self.bn1 = nn.BatchNorm2d(64)
|
45 |
+
self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)
|
46 |
+
self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)
|
47 |
+
self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)
|
48 |
+
self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)
|
49 |
+
self.linear = nn.Linear(512*block.expansion, num_classes)
|
50 |
+
|
51 |
+
def _make_layer(self, block, planes, num_blocks, stride):
|
52 |
+
strides = [stride] + [1]*(num_blocks-1)
|
53 |
+
layers = []
|
54 |
+
for stride in strides:
|
55 |
+
layers.append(block(self.in_planes, planes, stride))
|
56 |
+
self.in_planes = planes * block.expansion
|
57 |
+
return nn.Sequential(*layers)
|
58 |
+
|
59 |
+
def forward(self, x):
|
60 |
+
out = F.relu(self.bn1(self.conv1(x)))
|
61 |
+
out = self.layer1(out)
|
62 |
+
out = self.layer2(out)
|
63 |
+
out = self.layer3(out)
|
64 |
+
out = self.layer4(out)
|
65 |
+
out = F.avg_pool2d(out, 4)
|
66 |
+
out = out.view(out.size(0), -1)
|
67 |
+
out = self.linear(out)
|
68 |
+
return out
|
69 |
+
|
70 |
+
|
71 |
+
def ResNet18():
|
72 |
+
return ResNet(BasicBlock, [2, 2, 2, 2])
|
73 |
+
|
74 |
+
|
75 |
+
def ResNet34():
|
76 |
+
return ResNet(BasicBlock, [3, 4, 6, 3])
|