图像相关层之卷积和池化锐化图片示例
前情提要:黑白图只有1个通道数,彩色图却含有三个通道数。
一、彩色图像输入,灰度输出(代码和结果如下)
1、卷积未池化
1.1 代码一
from PIL import Image
import torch as t
from torch import nn
from torch.autograd import Variable as V
from torchvision.transforms import ToTensor,ToPILImage
to_tensor = ToTensor()
to_pil = ToPILImage()
image = Image.open('E:/pythonProjecttest/mg.png')
input = to_tensor(image).unsqueeze(0)
kernel = t.ones(3,3,3)/9
kernel[1][1][1] = 1
conv = nn.Conv2d(3,1,(3,3,3),1,bias=False)
conv.weight.data = kernel.view(1,3,3,3)
out = conv(V(input))
to_pil(out.data.squeeze(0)).show()
1.2 代码二
from PIL import Image
import torch as t
from torch import nn
from torch.autograd import Variable as V
from torchvision.transforms import ToTensor,ToPILImage
to_tensor = ToTensor()
to_pil = ToPILImage()
image = Image.open('E:/pythonProjecttest/mg.png')
if image.mode != 'L':
image = image.convert('L')
input = to_tensor(image).unsqueeze(0)
kernel = t.ones(3,3)/9
kernel[1][1] = 1
conv = nn.Conv2d(1,1,(3,3),1,bias=False)
conv.weight.data = kernel.view(1,1,3,3)
out = conv(V(input))
to_pil(out.data.squeeze(0)).show()
2、池化未卷积
from PIL import Image
import torch as t
from torch import nn
from torch.autograd import Variable as V
from torchvision.transforms import ToTensor,ToPILImage
to_tensor = ToTensor()
to_pil = ToPILImage()
image = Image.open('E:/pythonProjecttest/mg.png')
if image.mode != 'L':
image = image.convert('L')
input = to_tensor(image).unsqueeze(0)
pool = nn.AvgPool2d(2,2)
out = pool(V(input))
to_pil(out.data.squeeze(0)).show()
3、卷积及池化
from PIL import Image
import torch as t
from torch import nn
from torch.autograd import Variable as V
from torchvision.transforms import ToTensor,ToPILImage
to_tensor = ToTensor()
to_pil = ToPILImage()
image = Image.open('E:/pythonProjecttest/mg.png')
if image.mode != 'L':
image = image.convert('L')
input = to_tensor(image).unsqueeze(0)
kernel = t.ones(3,3)/9
kernel[1][1] = 1
conv = nn.Conv2d(1,1,(3,3),1,bias=False)
conv.weight.data = kernel.view(1,1,3,3)
out = conv(V(input))
pool = nn.AvgPool2d(2,2)
out = pool(out)
to_pil(out.data.squeeze(0)).show()
二、彩色图像输入,彩色输出(代码和结果如下)
1、卷积未池化
from PIL import Image
import torch as t
from torch import nn
from torch.autograd import Variable as V
from torchvision.transforms import ToTensor,ToPILImage
to_tensor = ToTensor()
to_pil = ToPILImage()
image = Image.open('E:/pythonProjecttest/mg.png')
input = to_tensor(image).unsqueeze(0)
kernel = t.ones(3,3,3,3)/9
kernel[1][1][1][1] = 1
conv = nn.Conv2d(3,3,(3,3,3,3),1,bias=False)
conv.weight.data = kernel.view(3,3,3,3)
out = conv(V(input))
to_pil(out.data.squeeze(0)).show()
2、池化未卷积
from PIL import Image
import torch as t
from torch import nn
from torch.autograd import Variable as V
from torchvision.transforms import ToTensor,ToPILImage
to_tensor = ToTensor()
to_pil = ToPILImage()
image = Image.open('E:/pythonProjecttest/mg.png')
input = to_tensor(image).unsqueeze(0)
pool = nn.AvgPool2d(2,2)
out = pool(V(input))
to_pil(out.data.squeeze(0)).show()
3、卷积及池化
from PIL import Image
import torch as t
from torch import nn
from torch.autograd import Variable as V
from torchvision.transforms import ToTensor,ToPILImage
to_tensor = ToTensor()
to_pil = ToPILImage()
image = Image.open('E:/pythonProjecttest/mg.png')
input = to_tensor(image).unsqueeze(0)
kernel = t.ones(3,3,3,3)/9
kernel[1][1][1][1] = 1
conv = nn.Conv2d(3,3,(3,3,3,3),1,bias=False)
conv.weight.data = kernel.view(3,3,3,3)
out = conv(V(input))
pool = nn.AvgPool2d(2,2)
out = pool(out)
to_pil(out.data.squeeze(0)).show()
|