Numpy实现卷积神经网络的方法

发布时间:2020-10-28 18:01:00 作者:Leah
来源:亿速云 阅读:181

Numpy实现卷积神经网络的方法?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

import numpy as np
import sys


def conv_(img, conv_filter):
  filter_size = conv_filter.shape[1]
  result = np.zeros((img.shape))
  # 循环遍历图像以应用卷积运算
  for r in np.uint16(np.arange(filter_size/2.0, img.shape[0]-filter_size/2.0+1)):
    for c in np.uint16(np.arange(filter_size/2.0, img.shape[1]-filter_size/2.0+1)):
      # 卷积的区域
      curr_region = img[r-np.uint16(np.floor(filter_size/2.0)):r+np.uint16(np.ceil(filter_size/2.0)),
             c-np.uint16(np.floor(filter_size/2.0)):c+np.uint16(np.ceil(filter_size/2.0))]
      # 卷积操作
      curr_result = curr_region * conv_filter
      conv_sum = np.sum(curr_result)
      # 将求和保存到特征图中
      result[r, c] = conv_sum

    # 裁剪结果矩阵的异常值
  final_result = result[np.uint16(filter_size/2.0):result.shape[0]-np.uint16(filter_size/2.0),
          np.uint16(filter_size/2.0):result.shape[1]-np.uint16(filter_size/2.0)]
  return final_result


def conv(img, conv_filter):
  # 检查图像通道的数量是否与过滤器深度匹配
  if len(img.shape) > 2 or len(conv_filter.shape) > 3:
    if img.shape[-1] != conv_filter.shape[-1]:
      print("错误:图像和过滤器中的通道数必须匹配")
      sys.exit()

  # 检查过滤器是否是方阵
  if conv_filter.shape[1] != conv_filter.shape[2]:
    print('错误:过滤器必须是方阵')
    sys.exit()

  # 检查过滤器大小是否是奇数
  if conv_filter.shape[1] % 2 == 0:
    print('错误:过滤器大小必须是奇数')
    sys.exit()

  # 定义一个空的特征图,用于保存过滤器与图像的卷积输出
  feature_maps = np.zeros((img.shape[0] - conv_filter.shape[1] + 1,
               img.shape[1] - conv_filter.shape[1] + 1,
               conv_filter.shape[0]))

  # 卷积操作
  for filter_num in range(conv_filter.shape[0]):
    print("Filter ", filter_num + 1)
    curr_filter = conv_filter[filter_num, :]

    # 检查单个过滤器是否有多个通道。如果有,那么每个通道将对图像进行卷积。所有卷积的结果加起来得到一个特征图。
    if len(curr_filter.shape) > 2:
      conv_map = conv_(img[:, :, 0], curr_filter[:, :, 0])
      for ch_num in range(1, curr_filter.shape[-1]):
        conv_map = conv_map + conv_(img[:, :, ch_num], curr_filter[:, :, ch_num])
    else:
      conv_map = conv_(img, curr_filter)
    feature_maps[:, :, filter_num] = conv_map
  return feature_maps


def pooling(feature_map, size=2, stride=2):
  # 定义池化操作的输出
  pool_out = np.zeros((np.uint16((feature_map.shape[0] - size + 1) / stride + 1),
             np.uint16((feature_map.shape[1] - size + 1) / stride + 1),
             feature_map.shape[-1]))

  for map_num in range(feature_map.shape[-1]):
    r2 = 0
    for r in np.arange(0, feature_map.shape[0] - size + 1, stride):
      c2 = 0
      for c in np.arange(0, feature_map.shape[1] - size + 1, stride):
        pool_out[r2, c2, map_num] = np.max([feature_map[r: r+size, c: c+size, map_num]])
        c2 = c2 + 1
      r2 = r2 + 1
  return pool_out
import skimage.data
import numpy
import matplotlib
import matplotlib.pyplot as plt
import NumPyCNN as numpycnn

# 读取图像
img = skimage.data.chelsea()
# 转成灰度图像
img = skimage.color.rgb2gray(img)

# 初始化卷积核
l1_filter = numpy.zeros((2, 3, 3))
# 检测垂直边缘
l1_filter[0, :, :] = numpy.array([[[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]])
# 检测水平边缘
l1_filter[1, :, :] = numpy.array([[[1, 1, 1], [0, 0, 0], [-1, -1, -1]]])

"""
第一个卷积层
"""
# 卷积操作
l1_feature_map = numpycnn.conv(img, l1_filter)
# ReLU
l1_feature_map_relu = numpycnn.relu(l1_feature_map)
# Pooling
l1_feature_map_relu_pool = numpycnn.pooling(l1_feature_map_relu, 2, 2)

"""
第二个卷积层
"""
# 初始化卷积核
l2_filter = numpy.random.rand(3, 5, 5, l1_feature_map_relu_pool.shape[-1])
# 卷积操作
l2_feature_map = numpycnn.conv(l1_feature_map_relu_pool, l2_filter)
# ReLU
l2_feature_map_relu = numpycnn.relu(l2_feature_map)
# Pooling
l2_feature_map_relu_pool = numpycnn.pooling(l2_feature_map_relu, 2, 2)

"""
第三个卷积层
"""
# 初始化卷积核
l3_filter = numpy.random.rand(1, 7, 7, l2_feature_map_relu_pool.shape[-1])
# 卷积操作
l3_feature_map = numpycnn.conv(l2_feature_map_relu_pool, l3_filter)
# ReLU
l3_feature_map_relu = numpycnn.relu(l3_feature_map)
# Pooling
l3_feature_map_relu_pool = numpycnn.pooling(l3_feature_map_relu, 2, 2)

"""
结果可视化
"""
fig0, ax0 = plt.subplots(nrows=1, ncols=1)
ax0.imshow(img).set_cmap("gray")
ax0.set_title("Input Image")
ax0.get_xaxis().set_ticks([])
ax0.get_yaxis().set_ticks([])
plt.savefig("in_img1.png", bbox_inches="tight")
plt.close(fig0)

# 第一层
fig1, ax1 = plt.subplots(nrows=3, ncols=2)
ax1[0, 0].imshow(l1_feature_map[:, :, 0]).set_cmap("gray")
ax1[0, 0].get_xaxis().set_ticks([])
ax1[0, 0].get_yaxis().set_ticks([])
ax1[0, 0].set_title("L1-Map1")

ax1[0, 1].imshow(l1_feature_map[:, :, 1]).set_cmap("gray")
ax1[0, 1].get_xaxis().set_ticks([])
ax1[0, 1].get_yaxis().set_ticks([])
ax1[0, 1].set_title("L1-Map2")

ax1[1, 0].imshow(l1_feature_map_relu[:, :, 0]).set_cmap("gray")
ax1[1, 0].get_xaxis().set_ticks([])
ax1[1, 0].get_yaxis().set_ticks([])
ax1[1, 0].set_title("L1-Map1ReLU")

ax1[1, 1].imshow(l1_feature_map_relu[:, :, 1]).set_cmap("gray")
ax1[1, 1].get_xaxis().set_ticks([])
ax1[1, 1].get_yaxis().set_ticks([])
ax1[1, 1].set_title("L1-Map2ReLU")

ax1[2, 0].imshow(l1_feature_map_relu_pool[:, :, 0]).set_cmap("gray")
ax1[2, 0].get_xaxis().set_ticks([])
ax1[2, 0].get_yaxis().set_ticks([])
ax1[2, 0].set_title("L1-Map1ReLUPool")

ax1[2, 1].imshow(l1_feature_map_relu_pool[:, :, 1]).set_cmap("gray")
ax1[2, 0].get_xaxis().set_ticks([])
ax1[2, 0].get_yaxis().set_ticks([])
ax1[2, 1].set_title("L1-Map2ReLUPool")

plt.savefig("L1.png", bbox_inches="tight")
plt.close(fig1)

# 第二层
fig2, ax2 = plt.subplots(nrows=3, ncols=3)
ax2[0, 0].imshow(l2_feature_map[:, :, 0]).set_cmap("gray")
ax2[0, 0].get_xaxis().set_ticks([])
ax2[0, 0].get_yaxis().set_ticks([])
ax2[0, 0].set_title("L2-Map1")

ax2[0, 1].imshow(l2_feature_map[:, :, 1]).set_cmap("gray")
ax2[0, 1].get_xaxis().set_ticks([])
ax2[0, 1].get_yaxis().set_ticks([])
ax2[0, 1].set_title("L2-Map2")

ax2[0, 2].imshow(l2_feature_map[:, :, 2]).set_cmap("gray")
ax2[0, 2].get_xaxis().set_ticks([])
ax2[0, 2].get_yaxis().set_ticks([])
ax2[0, 2].set_title("L2-Map3")

ax2[1, 0].imshow(l2_feature_map_relu[:, :, 0]).set_cmap("gray")
ax2[1, 0].get_xaxis().set_ticks([])
ax2[1, 0].get_yaxis().set_ticks([])
ax2[1, 0].set_title("L2-Map1ReLU")

ax2[1, 1].imshow(l2_feature_map_relu[:, :, 1]).set_cmap("gray")
ax2[1, 1].get_xaxis().set_ticks([])
ax2[1, 1].get_yaxis().set_ticks([])
ax2[1, 1].set_title("L2-Map2ReLU")

ax2[1, 2].imshow(l2_feature_map_relu[:, :, 2]).set_cmap("gray")
ax2[1, 2].get_xaxis().set_ticks([])
ax2[1, 2].get_yaxis().set_ticks([])
ax2[1, 2].set_title("L2-Map3ReLU")

ax2[2, 0].imshow(l2_feature_map_relu_pool[:, :, 0]).set_cmap("gray")
ax2[2, 0].get_xaxis().set_ticks([])
ax2[2, 0].get_yaxis().set_ticks([])
ax2[2, 0].set_title("L2-Map1ReLUPool")

ax2[2, 1].imshow(l2_feature_map_relu_pool[:, :, 1]).set_cmap("gray")
ax2[2, 1].get_xaxis().set_ticks([])
ax2[2, 1].get_yaxis().set_ticks([])
ax2[2, 1].set_title("L2-Map2ReLUPool")

ax2[2, 2].imshow(l2_feature_map_relu_pool[:, :, 2]).set_cmap("gray")
ax2[2, 2].get_xaxis().set_ticks([])
ax2[2, 2].get_yaxis().set_ticks([])
ax2[2, 2].set_title("L2-Map3ReLUPool")

plt.savefig("L2.png", bbox_inches="tight")
plt.close(fig2)

# 第三层
fig3, ax3 = plt.subplots(nrows=1, ncols=3)
ax3[0].imshow(l3_feature_map[:, :, 0]).set_cmap("gray")
ax3[0].get_xaxis().set_ticks([])
ax3[0].get_yaxis().set_ticks([])
ax3[0].set_title("L3-Map1")

ax3[1].imshow(l3_feature_map_relu[:, :, 0]).set_cmap("gray")
ax3[1].get_xaxis().set_ticks([])
ax3[1].get_yaxis().set_ticks([])
ax3[1].set_title("L3-Map1ReLU")

ax3[2].imshow(l3_feature_map_relu_pool[:, :, 0]).set_cmap("gray")
ax3[2].get_xaxis().set_ticks([])
ax3[2].get_yaxis().set_ticks([])
ax3[2].set_title("L3-Map1ReLUPool")

plt.savefig("L3.png", bbox_inches="tight")
plt.close(fig3)

关于Numpy实现卷积神经网络的方法问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. Python基于numpy模块实现回归预测的方法
  2. NumPy排序的实现

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

numpy

上一篇:Java fastjson实现解析json字符串

下一篇:使用python实现逻辑回归

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》