dfcnn.py 2.2 KB
Newer Older
H
hypox64 已提交
1 2 3 4 5 6
import torch
from torch import nn
import torch.nn.functional as F


class dfcnn(nn.Module):
H
hypox64 已提交
7
    def __init__(self, num_classes, input_nc):
H
hypox64 已提交
8 9
        super(dfcnn, self).__init__()
        self.layer1 = nn.Sequential(       
H
hypox64 已提交
10
            nn.Conv2d(input_nc, 32, 3, 1, 1, bias=False),
H
hypox64 已提交
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
            nn.BatchNorm2d(32),
            nn.ReLU(inplace = True),
            nn.Conv2d(32, 32, 3, 1, 1, bias=False),
            nn.BatchNorm2d(32),
            nn.ReLU(inplace = True),
            nn.MaxPool2d(2),   
        )
        self.layer2 = nn.Sequential(         
            nn.Conv2d(32, 64, 3, 1, 1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace = True),
            nn.Conv2d(64, 64, 3, 1, 1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace = True),             
            nn.MaxPool2d(2),                
        )
        self.layer3 = nn.Sequential(         
            nn.Conv2d(64, 128, 3, 1, 1, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU(inplace = True),
            nn.Conv2d(128, 128, 3, 1, 1, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU(inplace = True),             
            nn.MaxPool2d(2),                
        )
        self.layer4 = nn.Sequential(         
            nn.Conv2d(128, 256, 3, 1, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace = True),
            nn.Conv2d(256, 256, 3, 1, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace = True),
            nn.MaxPool2d(2),                        
        )
        self.layer5 = nn.Sequential(         
            nn.Conv2d(256, 512, 3, 1, 1, bias=False),
            nn.BatchNorm2d(512),
            nn.ReLU(inplace = True),
            nn.Conv2d(512, 512, 3, 1, 1, bias=False),
            nn.BatchNorm2d(512),
            nn.ReLU(inplace = True),                       
        )
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.out = nn.Linear(512, num_classes)   

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.layer5(x)
        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        x = self.out(x)
        return x