Spaces:
Sleeping
Sleeping
Update custom_resnet.py
Browse files- custom_resnet.py +130 -59
custom_resnet.py
CHANGED
@@ -1,77 +1,72 @@
|
|
|
|
|
|
|
|
|
|
1 |
import torch.nn as nn
|
2 |
import torch.nn.functional as F
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
def __init__(self, in_planes, planes, stride=1):
|
8 |
-
super(BasicBlock, self).__init__()
|
9 |
-
self.conv1 = nn.Conv2d(
|
10 |
-
in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False
|
11 |
-
)
|
12 |
-
self.bn1 = nn.BatchNorm2d(planes)
|
13 |
-
self.conv2 = nn.Conv2d(
|
14 |
-
planes, planes, kernel_size=3, stride=1, padding=1, bias=False
|
15 |
-
)
|
16 |
-
self.bn2 = nn.BatchNorm2d(planes)
|
17 |
-
|
18 |
-
self.shortcut = nn.Sequential()
|
19 |
-
|
20 |
-
def forward(self, x):
|
21 |
-
out = F.relu(self.bn1(self.conv1(x)))
|
22 |
-
out = self.bn2(self.conv2(out))
|
23 |
-
out += self.shortcut(x)
|
24 |
-
out = F.relu(out)
|
25 |
-
return out
|
26 |
-
|
27 |
-
|
28 |
-
class CustomBlock(nn.Module):
|
29 |
-
def __init__(self, in_channels, out_channels):
|
30 |
-
super(CustomBlock, self).__init__()
|
31 |
-
|
32 |
-
self.inner_layer = nn.Sequential(
|
33 |
nn.Conv2d(
|
34 |
-
in_channels=
|
35 |
-
out_channels=
|
36 |
kernel_size=3,
|
37 |
stride=1,
|
38 |
padding=1,
|
39 |
bias=False,
|
40 |
),
|
41 |
-
nn.
|
42 |
-
nn.BatchNorm2d(out_channels),
|
43 |
nn.ReLU(),
|
44 |
)
|
45 |
|
46 |
-
self.
|
47 |
-
|
48 |
-
def forward(self, x):
|
49 |
-
x = self.inner_layer(x)
|
50 |
-
r = self.res_block(x)
|
51 |
-
|
52 |
-
out = x + r
|
53 |
-
|
54 |
-
return out
|
55 |
-
|
56 |
-
|
57 |
-
class CustomResNet(nn.Module):
|
58 |
-
def __init__(self, num_classes=10):
|
59 |
-
super(CustomResNet, self).__init__()
|
60 |
-
|
61 |
-
self.prep_layer = nn.Sequential(
|
62 |
nn.Conv2d(
|
63 |
-
in_channels=
|
64 |
-
out_channels=
|
65 |
kernel_size=3,
|
66 |
stride=1,
|
67 |
padding=1,
|
68 |
bias=False,
|
69 |
),
|
70 |
-
nn.
|
|
|
71 |
nn.ReLU(),
|
72 |
)
|
73 |
|
74 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
self.layer_2 = nn.Sequential(
|
77 |
nn.Conv2d(
|
@@ -87,18 +82,94 @@ class CustomResNet(nn.Module):
|
|
87 |
nn.ReLU(),
|
88 |
)
|
89 |
|
90 |
-
self.layer_3 =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
-
self.
|
|
|
|
|
|
|
|
|
93 |
|
94 |
-
self.fc = nn.Linear(512, num_classes)
|
95 |
|
96 |
def forward(self, x):
|
97 |
x = self.prep_layer(x)
|
98 |
x = self.layer_1(x)
|
|
|
|
|
99 |
x = self.layer_2(x)
|
100 |
x = self.layer_3(x)
|
101 |
-
|
102 |
-
x = x
|
103 |
-
x = self.
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from torchvision.datasets.cifar import CIFAR10
|
2 |
+
import torch
|
3 |
+
from torch.optim.lr_scheduler import OneCycleLR
|
4 |
+
from torch_lr_finder import LRFinder
|
5 |
import torch.nn as nn
|
6 |
import torch.nn.functional as F
|
7 |
+
import os
|
8 |
+
import pytorch_lightning as pl
|
9 |
+
import albumentations as A
|
10 |
+
from torchvision.datasets import CIFAR10
|
11 |
+
from torchvision import transforms
|
12 |
+
from albumentations.pytorch.transforms import ToTensorV2
|
13 |
+
import numpy as np
|
14 |
+
from pytorch_lightning import LightningModule, Trainer
|
15 |
+
from torch.utils.data import DataLoader, random_split
|
16 |
+
from torchmetrics import Accuracy
|
17 |
+
from torchvision import transforms
|
18 |
+
|
19 |
+
PATH_DATASETS = os.environ.get("PATH_DATASETS", ".")
|
20 |
+
class CustomResNet(LightningModule):
|
21 |
+
def __init__(self, num_classes=10, data_dir=PATH_DATASETS, hidden_size=16, learning_rate=0.05):
|
22 |
+
super(CustomResNet, self).__init__()
|
23 |
+
self.save_hyperparameters()
|
24 |
+
#self.custom_block = CustomBlock(in_channels=64, out_channels=128)
|
25 |
+
# Set our init args as class attributes
|
26 |
|
27 |
+
# loading the dataset
|
28 |
+
self.EPOCHS = 24
|
29 |
+
self.num_classes=num_classes
|
30 |
+
self.data_dir = data_dir
|
31 |
+
self.hidden_size = hidden_size
|
32 |
+
self.learning_rate = learning_rate
|
33 |
|
34 |
+
self.prep_layer = nn.Sequential(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
nn.Conv2d(
|
36 |
+
in_channels=3,
|
37 |
+
out_channels=64,
|
38 |
kernel_size=3,
|
39 |
stride=1,
|
40 |
padding=1,
|
41 |
bias=False,
|
42 |
),
|
43 |
+
nn.BatchNorm2d(64),
|
|
|
44 |
nn.ReLU(),
|
45 |
)
|
46 |
|
47 |
+
self.layer_1 = nn.Sequential(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
nn.Conv2d(
|
49 |
+
in_channels=64,
|
50 |
+
out_channels=128,
|
51 |
kernel_size=3,
|
52 |
stride=1,
|
53 |
padding=1,
|
54 |
bias=False,
|
55 |
),
|
56 |
+
nn.MaxPool2d(kernel_size=2),
|
57 |
+
nn.BatchNorm2d(128),
|
58 |
nn.ReLU(),
|
59 |
)
|
60 |
|
61 |
+
self.resblock1 = nn.Sequential(
|
62 |
+
nn.Conv2d(128, 128, kernel_size=3, stride=1, bias=False, padding=1),
|
63 |
+
nn.BatchNorm2d(128),
|
64 |
+
nn.ReLU(),
|
65 |
+
nn.Conv2d(128, 128, kernel_size=3, stride=1, bias=False, padding=1),
|
66 |
+
nn.BatchNorm2d(128),
|
67 |
+
nn.ReLU()
|
68 |
+
)
|
69 |
+
|
70 |
|
71 |
self.layer_2 = nn.Sequential(
|
72 |
nn.Conv2d(
|
|
|
82 |
nn.ReLU(),
|
83 |
)
|
84 |
|
85 |
+
self.layer_3 = nn.Sequential(
|
86 |
+
nn.Conv2d(256, 512, kernel_size=3, stride=1, bias=False, padding=1),
|
87 |
+
nn.MaxPool2d(kernel_size = 2, stride = 2),
|
88 |
+
nn.BatchNorm2d(512),
|
89 |
+
nn.ReLU()
|
90 |
+
)
|
91 |
+
self.resblock2 = nn.Sequential(
|
92 |
+
nn.Conv2d(512, 512, kernel_size=3, stride=1, bias=False, padding=1),
|
93 |
+
nn.BatchNorm2d(512),
|
94 |
+
nn.ReLU(),
|
95 |
+
nn.Conv2d(512, 512, kernel_size=3, stride=1, bias=False, padding=1),
|
96 |
+
nn.BatchNorm2d(512),
|
97 |
+
nn.ReLU()
|
98 |
+
)
|
99 |
+
|
100 |
+
self.maxpoollayer = nn.Sequential(nn.MaxPool2d(kernel_size=4,stride = 4))
|
101 |
|
102 |
+
self.fclayer = nn.Linear(512, self.num_classes)
|
103 |
+
# def loss_function(self, pred, target):
|
104 |
+
# criterion = torch.nn.CrossEntropyLoss()
|
105 |
+
|
106 |
+
# return criterion(pred, target)
|
107 |
|
|
|
108 |
|
109 |
def forward(self, x):
|
110 |
x = self.prep_layer(x)
|
111 |
x = self.layer_1(x)
|
112 |
+
r1 = self.resblock1(x)
|
113 |
+
x = x + r1
|
114 |
x = self.layer_2(x)
|
115 |
x = self.layer_3(x)
|
116 |
+
r2 = self.resblock2(x)
|
117 |
+
x = x + r2
|
118 |
+
x = self.maxpoollayer(x)
|
119 |
+
x = x.view((x.shape[0],-1))
|
120 |
+
x = self.fclayer(x)
|
121 |
+
|
122 |
+
return F.log_softmax(x,dim=-1)
|
123 |
+
# def get_loss_accuracy(self, batch):
|
124 |
+
# images, labels = batch
|
125 |
+
# predictions = self(images)
|
126 |
+
# predicted_classes = torch.argmax(predictions, dim=1)
|
127 |
+
# accuracy = (predicted_classes == labels).float().mean()
|
128 |
+
# loss = self.loss_function(predictions, labels)
|
129 |
+
|
130 |
+
# return loss, accuracy * 100
|
131 |
+
|
132 |
+
|
133 |
+
|
134 |
+
# def training_step(self, batch, batch_idx):
|
135 |
+
# loss, accuracy = self.get_loss_accuracy(batch)
|
136 |
+
# self.log("loss/train", loss, on_epoch=True, prog_bar=True, logger=True)
|
137 |
+
# self.log("acc/train", accuracy, on_epoch=True, prog_bar=True, logger=True)
|
138 |
+
|
139 |
+
# return loss
|
140 |
+
|
141 |
+
# def validation_step(self, batch, batch_idx):
|
142 |
+
# loss, accuracy = self.get_loss_accuracy(batch)
|
143 |
+
# self.log("loss/val", loss, on_epoch=True, prog_bar=True, logger=True)
|
144 |
+
# self.log("acc/val", accuracy, on_epoch=True, prog_bar=True, logger=True)
|
145 |
+
|
146 |
+
# return loss
|
147 |
+
|
148 |
+
# def test_step(self, batch, batch_idx):
|
149 |
+
# loss = self.validation_step(batch, batch_idx)
|
150 |
+
|
151 |
+
# return loss
|
152 |
+
|
153 |
+
# def configure_optimizers(self):
|
154 |
+
# LEARNING_RATE=0.03
|
155 |
+
# WEIGHT_DECAY=0
|
156 |
+
# optimizer = torch.optim.Adam(self.parameters(), lr=LEARNING_RATE, weight_decay=WEIGHT_DECAY)
|
157 |
+
|
158 |
+
# self.trainer.fit_loop.setup_data()
|
159 |
+
# dataloader = self.trainer.train_dataloader
|
160 |
+
|
161 |
+
# lr_scheduler = OneCycleLR(
|
162 |
+
# optimizer,
|
163 |
+
# max_lr=4.79E-02,
|
164 |
+
# steps_per_epoch=len(dataloader),
|
165 |
+
# epochs=24,
|
166 |
+
# pct_start=5/24,
|
167 |
+
# div_factor=100,
|
168 |
+
# three_phase=False,
|
169 |
+
# final_div_factor=100,
|
170 |
+
# anneal_strategy='linear'
|
171 |
+
# )
|
172 |
+
|
173 |
+
# scheduler = {"scheduler": lr_scheduler, "interval" : "step"}
|
174 |
+
|
175 |
+
# return [optimizer], [scheduler]
|