Convolutional neural network implement frame (with code implementation) (ii)

Learning Source: depth learning theory and actual combat
in the previous article, we convolution neural network to do a few basic concepts to understand, to know what it meant convolution, what is pooled, what is the convolution neural network. The main contents of this article is the realization convolution and pooled with the tensorflow.
Use the version introduced:
tensorflow: 1.15
Python: 3

convolution

Convolution tensorflow the API is, tf.nn.conv2d ().
Parameter as shown below:
tf_converlution
Parameter explained as follows:
The first parameter: INPUT, i.e. the input data, typically a (batch, h, w, channels ) of 4-dimensional vectors:

  • Enter a number of pictures of: batch
  • h: represents the height of the image
  • w: represents the width of the image
  • channels: the picture represents the number of channels, such as a picture of channels 3 rpg

The second parameter: filter, i.e. convolution kernel, but also by one (k_h, k_w, in, out) of 4-dimensional vectors:

  • k_h: convolution kernel height
  • w_h: convolution kernel width
  • in: the number of channels, the same number of channels the general picture
  • out: i.e., the number of convolution

The third parameter: Strides, i.e. moving step, but also by one (1, s_h, s_w, 1) a 4-dimensional vector:

  • s_h: moving on the step height
  • s_w: moving in the widthwise step
  • About s_h, location s_w parameters related to the fact, and data_format, data_format in two forms: ' "NHWC" and "NCHW", if the former, strdes expressed as (1, s_h, s_w, 1), if the latter, the strides expressed as (1,1, s_h, s_w)

The fourth parameter: padding, that is, whether the output will be padded with zeros Policy

  • If the padding = "SAME", tensorflow will output zero-padded to ensure that the same input and output dimensions;
  • If the padding = "VALID", tensorflow does not automatically zeros.
    Other parameters of the default, students can explore their interest.

Convolution code implementation

# 导入相关模块
import tensorflow as tf 
import urllib.request
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
%matplotlib inline 
#导入图片
cat_url = 'https://image.ibb.co/iFGs0x/kitty.png'
cat = Image.open(urllib.request.urlopen(cat_url)).convert('L')
cat = np.array(im,dtype='float32')

# 可视化图片
plt.imshow(cat.astype('uint8'),cmap='gray')

source_cat

查看图片维度
print (cat.shape[0])
print (cat.shape[1])

h_w

# 将图片矩阵转化为tensor
cat = tf.constant(cat.reshape((1,cat.shape[0],cat.shape[1],1)),name='input')

# 定义一个卷积核
sobel_kernel = np.array([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],dtype=np.float32)
sobel_kernel = tf.constant(sobel_kernel,shape=(3,3,1,1))
#调用tensorflow 卷积API
# padding='SAME' 的卷积
edge1 = tf.nn.conv2d(cat,sobel_kernel,[1,1,1,1],'SAME',name='same_conv')
# padding='VALID'的卷积
edge2 = tf.nn.conv2d(cat,sobel_kernel,[1,1,1,1],'VALID',name='valid_conv')
#激活tensorflow
sess = tf.InteractiveSession()

edge1_np = sess.run(edge1)
edge2_np = sess.run(edge2)
# padding = 'SAME'下的图片
plt.imshow(np.squeeze(edge1_np),cmap='gray')
plt.title('shape={}'.format(edge1_np.shape))

con_same

# padding="VALID"
plt.imshow(np.squeeze(edge2_np),cmap='gray')
plt.title('shape={}'.format(edge2_np.shape))

conv_valid
By comparing the two methods, we found out what? We padding for the input and output dimensions dimensions SAME picture is the same.

Pooling

tensorflow has pooled encapsulated API layer, in this discussion we maximize cell layer API:. tf.nn.max_pool ()
parameter as shown below:
max_pool
Specific explanation:
value: i.e. input, is a four-dimensional Tensor ( BATCH, H, W, channels).
ksize: pool size of the core, is a four-dimensional tensor (1, k_h, k_w, 1), and the same meaning as convolution.
strides:. convolution same meanings strides
padding: convolution with padding meaning.

Pooling code implementation

# padding='SAME'
small_im1 = tf.nn.max_pool(cat,[1,2,2,1],[1,2,2,1],'SAME',name='same_max_pool')
# padding = "VALID"
small_im2 = tf.nn.max_pool(cat,[1,2,2,1],[1,2,2,1],'VALID',name = 'valid_max_pool')
# 激活tensor
small_im1_np,small_im2_np = sess.run([small_im1,small_im2])
#画图 padding='SAME'
plt.imshow(np.squeeze(small_im1_np),cmap='gray')
plt.title('shape={}'.format(small_im1_np.shape))

pool_max

# 画图 padding='VALID'
plt.imshow(np.squeeze(small_im2_np),cmap='gray')
plt.title('shape={}'.format(small_im2_np.shape))

valid_pool
Strategies found pooling zero padding layer, the dimensions of the output is not affected.

总结,通过卷积和池化实现,我们发现,池化在维度下降一半的情况下,并没有丢失多少信息。
Released eight original articles · won praise 6 · views 2526

Guess you like

Origin blog.csdn.net/weixin_42374329/article/details/105222107