Skip to content
Snippets Groups Projects
Commit 4e0ea155 authored by Falguni Ghosh's avatar Falguni Ghosh
Browse files

Upload New File

parent 9e870cba
No related branches found
No related tags found
No related merge requests found
import torch.nn as nn
import torch
import torch.nn.functional as F
class ResBlock(nn.Module):
def __init__(self,in_channels,out_channels,stride):
super(ResBlock,self).__init__()
self.in_ch = in_channels
self.out_ch = out_channels
self.s = stride
self.block = nn.Sequential(nn.Conv2d(self.in_ch,self.out_ch,stride=self.s,kernel_size=3,padding=1),
nn.BatchNorm2d(self.out_ch), nn.ReLU(inplace=True),
nn.Conv2d(self.out_ch, self.out_ch, kernel_size=3, padding=1),
nn.BatchNorm2d(self.out_ch))
self.skip_connection = nn.Sequential(nn.Conv2d(self.in_ch,self.out_ch,stride=self.s,kernel_size=1),
nn.BatchNorm2d(self.out_ch))
def forward(self,x):
skip = x.clone()
x1 = self.block(x)
x2 = self.skip_connection(skip)
op = F.relu(x1+x2)
return op
class ResNet(nn.Module):
def __init__(self):
super(ResNet, self).__init__()
self.conv0 = nn.Sequential(nn.Conv2d(in_channels=3,out_channels=64,kernel_size=7,stride=2),
nn.BatchNorm2d(64), nn.ReLU(inplace=True),nn.MaxPool2d(kernel_size=3,stride=2))
self.block1 = ResBlock(64,64,1)
self.block2 = ResBlock(64,128,2)
self.block3 = ResBlock(128,256,2)
self.block4 = ResBlock(256,512,2)
self.GlobalAvgPool = nn.AvgPool2d(kernel_size=10)
self.flatten = nn.Flatten()
self.lin = nn.Linear(512,2)
self.sig = nn.Sigmoid()
def forward(self,x):
x1 = self.conv0(x)
x2 = self.block1.forward(x1)
x3 = self.block2.forward(x2)
x4 = self.block3.forward(x3)
x5 = self.block4.forward(x4)
x5 = self.GlobalAvgPool(x5)
x5 = self.flatten(x5)
x5 = self.lin(x5)
op = self.sig(x5)
return op
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment