-
Notifications
You must be signed in to change notification settings - Fork 0
/
cutom_vgg16.py
84 lines (67 loc) · 2.93 KB
/
cutom_vgg16.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import pandas as pd
import numpy as np
import torch
import os
import argparse
from torch import nn
from torch.nn import init, Parameter
import torch.nn.functional as F
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
#from torchinfo import summary
from torchsummary import summary
import torch.optim as optim
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms
class VGG16(nn.Module):
def __init__(self):
super(VGG16, self).__init__()
self.conv1_1 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1)
self.conv1_2 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1)
self.conv2_1 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1)
self.conv2_2 = nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1)
self.conv3_1 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1)
self.conv3_2 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1)
self.conv3_3 = nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1)
self.conv4_1 = nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, padding=1)
self.conv4_2 = nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1)
self.conv4_3 = nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1)
self.conv5_1 = nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1)
self.conv5_2 = nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1)
self.conv5_3 = nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, padding=1)
self.maxpool = nn.AvgPool2d((2,2))
self.maxpool_1 = nn.AdaptiveAvgPool2d((1,1))
self.classifier = nn.Linear(512, 10)
def forward(self, x):
x = F.relu(self.conv1_1(x))
x = F.relu(self.conv1_2(x))
x = self.maxpool(x)
x = F.relu(self.conv2_1(x))
x = F.relu(self.conv2_2(x))
x = self.maxpool(x)
x = F.relu(self.conv3_1(x))
x = F.relu(self.conv3_2(x))
x = F.relu(self.conv3_3(x))
x = self.maxpool(x)
x = F.relu(self.conv4_1(x))
x = F.relu(self.conv4_2(x))
x = F.relu(self.conv4_3(x))
x = self.maxpool(x)
x = F.relu(self.conv5_1(x))
x = F.relu(self.conv5_2(x))
x = F.relu(self.conv5_3(x))
#print('before implementing any pooling',x.shape)
x = self.maxpool(x)
#print('After implementing Avg pooling',x.shape)
x = self.maxpool_1(x)
#print('After implementing AdaptivAvg pooling',x.shape)
x=x.reshape(x.shape[0],-1)
#print('after reshaping',x.shape)
x = self.classifier(x)
return x
def model_VGG() -> VGG16:
model = VGG16()
return model
#for testing the model
#model =model_VGG()
#summary(model,(3, 224, 224))