import cv2
import matplotlib.pyplot as plt
%matplotlib inline
img_path = 'images/udacity_sdc.png'
bgr_img = cv2.imread(img_path)
gray_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2GRAY)
gray_img = gray_img.astype("float32")/255
plt.imshow(gray_img, cmap='gray')
plt.show()
显示图像
import numpy as np
filter_vals = np.array([[-1, -1, 1, 1], [-1, -1, 1, 1], [-1, -1, 1, 1], [-1, -1, 1, 1]])
print('Filter shape: ', filter_vals.shape)
Filter shape: (4, 4)
# Defining four different filters,
filter_1 = filter_vals
filter_2 = -filter_1
filter_3 = filter_1.T
filter_4 = -filter_3
filters = np.array([filter_1, filter_2, filter_3, filter_4])
print('Filter 1: \n', filter_1)
Filter 1: [[-1 -1 1 1] [-1 -1 1 1] [-1 -1 1 1] [-1 -1 1 1]]
将卷积层初始化,使其包含你所创建的所有滤波器。然后添加一个最大池化层(相关文档请通过点击这里查阅),内核大小为(4x4),这样你就可以看到,在这一步之后,图像分辨率已经降低了!
import torch
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def \_\_init\_\_(self, weight):
super(Net, self).\_\_init\_\_()
# initializes the weights of the convolutional layer to be the weights of the 4 defined filters
k\_height, k\_width = weight.shape\[2:\]
# assumes there are 4 grayscale filters
self.conv = nn.Conv2d(1, 4, kernel\_size=(k\_height, k\_width), bias=False)
self.conv.weight = torch.nn.Parameter(weight)
# define a pooling layer
self.pool = nn.MaxPool2d(4, 4)
def forward(self, x):
# calculates the output of a convolutional layer
# pre- and post-activation
conv\_x = self.conv(x)
activated\_x = F.relu(conv\_x)
# applies pooling layer
pooled\_x = self.pool(activated\_x)
# returns all layers
return conv\_x, activated\_x, pooled\_x
weight = torch.from_numpy(filters).unsqueeze(1).type(torch.FloatTensor)
model = Net(weight)
print(model)
Net(
(conv): Conv2d(1, 4, kernel_size=(4, 4), stride=(1, 1), bias=False)
(pool): MaxPool2d(kernel_size=4, stride=4, padding=0, dilation=1, ceil_mode=False)
)
首先,我们将定义一个辅助函数viz_layer
,它会接收一个特定的层和多个滤波器(可选参数)作为输入,并在图像通过后显示该层的输出。
# helper function for visualizing the output of a given layer
def viz_layer(layer, n_filters= 4):
fig = plt.figure(figsize=(20, 20))
for i in range(n\_filters):
ax = fig.add\_subplot(1, n\_filters, i+1, xticks=\[\], yticks=\[\])
# grab layer outputs
ax.imshow(np.squeeze(layer\[0,i\].data.numpy()), cmap='gray')
ax.set\_title('Output %s' % str(i+1))
让我们看一下应用ReLu激活函数后,该卷积层的输出是什么。
# plot original image
plt.imshow(gray_img, cmap='gray')
fig = plt.figure(figsize=(12, 6))
fig.subplots_adjust(left=0, right=1.5, bottom=0.8, top=1, hspace=0.05, wspace=0.05)
for i in range(4):
ax = fig.add_subplot(1, 4, i+1, xticks=[], yticks=[])
ax.imshow(filters[i], cmap='gray')
ax.set_title('Filter %s' % str(i+1))
gray_img_tensor = torch.from_numpy(gray_img).unsqueeze(0).unsqueeze(1)
conv_layer, activated_layer, pooled_layer = model(gray_img_tensor)
viz_layer(activated_layer)
然后,看一下池化层的输出。池化层将上面描绘的特征映射图作为输入,并通过一些池化因子,通过在一个给定内核区域中构造一个仅拥有最大(即最亮)值的新的较小图像来减少那些映射图的维度。
# visualize the output of the pooling layer
viz_layer(pooled_layer)
手机扫一扫
移动阅读更方便
你可能感兴趣的文章