关于xecption模型训练的记录——自己搭的翻车了

自己按照图纸搭建的网络,十分感人,5个epoch后正确率25%左右,每个epoch训练时长15-20分钟左右,训练目的为5种花的分类

不如之前的inceptionv3 ,10个epoch后达到50%正确率,并且一共训练10分钟左右

 

 

model.py

 

import torch
import torch.nn as nn



class Basic_conv(nn.Module):
    def __init__(self,in_channel,out_channel):
        super().__init__()
        self.conv=nn.Conv2d(in_channels=in_channel,out_channels=out_channel,kernel_size=3)
        self.relu=nn.ReLU(inplace=False)
    def forward(self,x):
        x=self.conv(x)
        x=self.relu(x)
        return x
        
class Entry_flow(nn.Module):
    def __init__(self,in_channel,out_channel):          
        super().__init__()
        self.SeparableConv=nn.Sequential(nn.Conv2d(in_channels=in_channel,out_channels=in_channel,kernel_size=3,groups=in_channel,padding=1),
        nn.Conv2d(in_channels=in_channel,out_channels=out_channel,kernel_size=1))
        self.SeparableConv2=nn.Sequential(nn.Conv2d(in_channels=out_channel,out_channels=out_channel,kernel_size=3,groups=out_channel,padding=1),
        nn.Conv2d(in_channels=out_channel,out_channels=out_channel,kernel_size=1))
        self.relu=nn.ReLU(inplace=False)
        self.maxpool=nn.MaxPool2d(kernel_size=3,stride=2,padding=1)
        self.res=nn.Conv2d(in_channels=in_channel,out_channels=out_channel,kernel_size=1,stride=2)

    
    def forward(self,x):
        x1=x
        x=self.SeparableConv(x)
        x=self.relu(x)
        x=self.SeparableConv2(x)
        x=self.maxpool(x)
        x1=self.res(x1)
        result=x1+x
        return result


class middle_flow(nn.Module):
    def __init__(self,channel):
        super().__init__()
        self.relu=nn.ReLU(inplace=False)
        self.SeparableConv_middle=nn.Conv2d(in_channels=channel,out_channels=channel,kernel_size=3,padding=1)
        self.SeparableConv_middle_1x1=nn.Conv2d(in_channels=channel,out_channels=channel,kernel_size=1)
    
    def forward(self,x):
        x1=x
        x=self.relu(x)
        x=self.SeparableConv_middle(x)
        x=self.relu(x)
        x=self.SeparableConv_middle(x)
        x=self.relu(x)
        x=self.SeparableConv_middle(x)
        result=x1+x
        return result

class Separableconv(nn.Module):
    def __init__(self,in_channel,out_channel):
        super().__init__()
        self.conv1=nn.Conv2d(in_channels=in_channel,out_channels=in_channel,kernel_size=3,padding=1)
        self.conv2=nn.Conv2d(in_channels=in_channel,out_channels=out_channel,kernel_size=1)
    def forward(self,x):
        x=self.conv1(x)
        x=self.conv2(x)
        return x

class exit_flow(nn.Module):
    def __init__(self):
        super().__init__()
        self.relu=nn.ReLU(inplace=False)
        self.Separable_conv1=Separableconv(728,728)
        self.Separable_conv2=Separableconv(728,1024)
        self.maxpool=nn.MaxPool2d(kernel_size=3,padding=1,stride=2)
        self.conv=nn.Conv2d(728,1024,kernel_size=1,stride=2)
        self.Separable_conv3=Separableconv(1024,1536)
        self.Separable_conv4=Separableconv(1536,2048)
        self.avgpool=nn.AdaptiveAvgPool2d((1,1))
        self.dropout=nn.Dropout(0.4)
        


    def forward(self,x):
        x1=x
        x1=self.conv(x)
        x=self.relu(x)
        x=self.Separable_conv1(x)
        x=self.relu(x)
        x=self.Separable_conv2(x)
        x=self.maxpool(x)
        result=x1+x
        result=self.Separable_conv3(result)
        result=self.relu(result)
        result=self.Separable_conv4(result)
        result=self.relu(result)
        result=self.avgpool(result)
        result=torch.flatten(result,1)
        #result=self.dropout(result)

        return result




        

class Xception(nn.Module):
    def __init__(self,num_class):
        super().__init__()
        self.conv1=Basic_conv(3,32)
        self.conv2=Basic_conv(32,64)
        self.entry_flow1=Entry_flow(in_channel=64,out_channel=128,)
        self.entry_flow2=Entry_flow(in_channel=128,out_channel=256)
        self.entry_flow3=Entry_flow(in_channel=256,out_channel=728)
        self.middle_flow=middle_flow(728)
        self.exit_flow=exit_flow()   
        self.fc=nn.Linear(2048,num_class)
        

    def forward(self,x):
        x=self.conv1(x)
        x=self.conv2(x)
        x=self.entry_flow1(x)
        x=self.entry_flow2(x)
        x=self.entry_flow3(x)
        x=self.middle_flow(x)
        x=self.middle_flow(x)
        x=self.middle_flow(x)
        x=self.middle_flow(x)
        x=self.middle_flow(x)
        x=self.middle_flow(x)
        x=self.middle_flow(x)
        x=self.middle_flow(x)
        x=self.exit_flow(x)
        x=self.fc(x)
        return x


还不没明白什么原因,这篇博客只是记录而已

 

posted @ 2021-09-14 22:40  Haokunnnnnnnna  阅读(123)  评论(0编辑  收藏  举报