1. 程式人生 > >不同loss function之間的對比(基於FSRCNN)

不同loss function之間的對比(基於FSRCNN)

對於L2、huber和Cross三種不同的損失函式形式進行測試。(之前都是用L1)

將SR_model.py程式碼修改如下:

# loss
            loss_type = train_opt['pixel_criterion']
            if loss_type == 'l1':
                self.cri_pix = nn.L1Loss().to(self.device)
            elif loss_type == 'l2':
                self.cri_pix = nn.MSELoss().to(self.device)
            #######################################################################
            elif loss_type=='huber':
                self.cri_pix = nn.SmoothL1Loss().to(self.device)

            elif loss_type=='Cross':
                self.cri_pix = nn.CrossEntropyLoss().to(self.device)

            ####################################################################3###
            else:
                raise NotImplementedError('Loss type [{:s}] is not recognized.'.format(loss_type))

放大兩倍

#######################################################################################################3
#FSRCNN
class FSRCNN(nn.Module):
    def __init__(self, in_nc, out_nc, nf, nb, upscale=2, norm_type='batch', act_type='relu', \
            mode='NAC', res_scale=1, upsample_mode='upconv'):##play attention the upscales
        super(FSRCNN,self).__init__()
        #Feature extractionn
        self.conv1=nn.Conv2d(in_channels=in_nc,out_channels=nf,kernel_size=5,stride=1,padding=2)#nf=56.add padding ,make the data alignment
        self.prelu1=nn.PReLU()
 
        #Shrinking
        self.conv2=nn.Conv2d(in_channels=nf,out_channels=12,kernel_size=1,stride=1,padding=0)
        self.prelu2 = nn.PReLU()
 
        # Non-linear Mapping
        self.conv3=nn.Conv2d(in_channels=12,out_channels=12,kernel_size=3,stride=1,padding=1)
        self.prelu3 = nn.PReLU()
        self.conv4=nn.Conv2d(in_channels=12,out_channels=12,kernel_size=3,stride=1,padding=1)
        self.prelu4 = nn.PReLU()
        self.conv5=nn.Conv2d(in_channels=12,out_channels=12,kernel_size=3,stride=1,padding=1)
        self.prelu5 = nn.PReLU()
        self.conv6=nn.Conv2d(in_channels=12,out_channels=12,kernel_size=3,stride=1,padding=1)
        self.prelu6 = nn.PReLU()
 
        # Expanding
        self.conv7=nn.Conv2d(in_channels=12,out_channels=nf,kernel_size=1,stride=1,padding=0)
        self.prelu7 = nn.PReLU()
 
        # Deconvolution
        self.last_part= nn.ConvTranspose2d(in_channels=nf,out_channels=in_nc,kernel_size=9,stride=upscale, padding=4, output_padding=1)
 
 
    def forward(self, x):#
         out = self.prelu1(self.conv1(x))
         out = self.prelu2(self.conv2(out))
         out = self.prelu3(self.conv3(out))
         out = self.prelu4(self.conv4(out))
         out = self.prelu5(self.conv5(out))
         out = self.prelu6(self.conv6(out))
         out = self.prelu7(self.conv7(out))
         out = self.last_part(out)
 
         return out
 
 
##########################################################################################################

結果對比如下:

在初始的時候,huber function的效果比L2的要好,或者說比L2的PSNR上升得更快。baseline中採用得為L1,那麼上升得效果也是較差得~

結果如下圖所示

 

cross function會報錯。。。。

由此看來,如果採用cross需要改比較多的資料結構。。。為此這裡就不展開了。。。。

一個不錯得pytorch手冊

http://www.ituring.com.cn/book/2456