前言
最近做了一个图像去雨项目,使用的语言为Python,深度学习框架为Pytorch。去雨效果不错,具体请看效果视频!
图像去雨(GAN,Opencv,Pytorch)
网络模型
去雨的网络架构为Unet模型,模型的结构图如下所示:
代码如下:
import torch
import torch.nn as nn
from torchsummary import summary
from torchvision import models
from torchvision.models.feature_extraction import create_feature_extractor
import torch.nn.functional as F
from torchstat import stat
class Resnet18(nn.Module):
def __init__(self):
super(Resnet18, self).__init__()
self.resnet = models.resnet18(pretrained=False)
# self.resnet = create_feature_extractor(self.resnet, {'relu': 'feat320', 'layer1': 'feat160', 'layer2': 'feat80',
# 'layer3': 'feat40'})
def forward(self,x):
for name,m in self.resnet._modules.items():
x=m(x)
if name=='relu':
x1=x
elif name=='layer1':
x2=x
elif name=='layer2':
x3=x
elif name=='layer3':
x4=x
break
# x=self.resnet(x)
return x1,x2,x3,x4
class Linears(nn.Module):
def __init__(self,a,b):
super(Linears, self).__init__()
self.linear1=nn.Linear(a,b)
self.relu1=nn.LeakyReLU()
self.linear2 = nn.Linear(b, a)
self.sigmoid=nn.Sigmoid()
def forward(self,x):
x=self.linear1(x)
x=self.relu1(x)
x=self.linear2(x)
x=self.sigmoid(x)
return x
class DenseNetBlock(nn.Module):
def __init__(self,inplanes=1,planes=1,stride=1):
super(DenseNetBlock,self).__init__()
self.conv1=nn.Conv2d(inplanes,planes,3,stride,1)
self.bn1 = nn.BatchNorm2d(planes)
self.relu1=nn.LeakyReLU()
self.conv2 = nn.Conv2d(inplanes, planes, 3,stride,1)
self.bn2 = nn.BatchNorm2d(planes)
self.relu2 = n