卷积神经网络-卷积与池化-笔记一

e6c86ead16f740fcbda386252a680703.png36c85e4dc6b4405d86936d3ffb34eaec.pngcd7c6bcbdc70438886925f80702a4ef4.png9a4e3eebbb7d4287a5b8862f88775930.png7c041d2fc84c4744986535430733810c.png

# 相关示例代码
!pip install tensorflow==2.3.0
!pip install scikit-learn==0.24.2
!pip install opencv-python
# 相关示例代码
import pandas as pd
import numpy as np
import tensorflow as tf
import cv2
from PIL import Image
import matplotlib.pyplot as plt


import warnings
warnings.filterwarnings("ignore")

d312f97a55a444b4a44a7aa2cf6197c3.pngab0ed00ddede4ba5a7b0e56d43c0cdab.png1b71d9e1f89e455cb9bd925c1b4af4d2.png830f8436d81c4ba3b5faa103b50bb149.png787bef25147a4a60923dd27e1d37446d.png

# 相关示例代码
input_shape = (4, 28, 28, 3)

# 根据上述向量形状,创建随机张量Tensor(输入)
x = tf.random.normal(input_shape)
print(x.shape)

428ab6339a6943b5b40ea7de4a7d5483.png

# 相关示例代码
y = tf.keras.layers.Conv2D(filters=2, kernel_size=(3,3), activation='relu', input_shape=input_shape[1:])(x)
print(y.shape)

e3102d10a51141e487289638d0e18155.png

# 相关示例代码
# 定义卷积层
conv_layer1 = tf.keras.layers.Conv2D(2, 3, activation='relu', input_shape=input_shape[1:])
# 放入卷积层
y1 = conv_layer1(x)

print(y.shape)

af451d880e4a4b3ea51f77eb71927d8c.png

# 相关示例代码
#获得权重
print( conv_layer1.get_weights()[0].shape )

7392643c063f454d998a3273950ab799.png

# 相关示例代码
y2 = tf.keras.layers.Conv2D(filters=2, kernel_size=(3,3), padding='same',activation='relu', input_shape=input_shape[1:])(x) # padding='same' 边界补零
print(y2.shape)

a8e2f0c69771405ab0d176f2d177e375.png

# 相关示例代码
# 假设输入图像大小为 (4, 28, 28, 1)
input_shape = (4, 28, 28, 1)

# 定义卷积层 输出特征图8,卷积核大小(3,3), 滑动步长(1,1),激活函数 relu,padding='same'
y3 = tf.keras.layers.Conv2D(8, 3,strides=(1, 1), activation='relu', padding='same', input_shape=input_shape)(x)
print(y3.shape)#(4, 28, 28, 8)

cacd8893c1ee4949960f26b0a6c281c1.png

# 相关示例代码
# 读取图像
img = Image.open("1.jpg")

# 显示图像
plt.imshow(img)
plt.axis('off')  # 关闭坐标轴
plt.show()

5076f98b37e749f59253480a82c756a4.png

# 相关示例代码
# 将图像转换为NumPy数组
img_array = np.asarray(img)
img = img_array[np.newaxis, :] # np.newaxis 添加一个新的维度,将其变成四维。
print(img.shape)
print(img.dtype)
img = img.astype("float32")/255.0  #通常要将图像的像素值转换为浮点数,并进行归一化
print(img.dtype)

#显示图像
plt.imshow(img[0])  # 注意:由于现在图像是四维的,所以需要使用 img[0] 来取出第一个图像
plt.axis('off')  # 关闭坐标轴
plt.show()

a8e45c7f05864e288b08362c1a379036.png

# 相关示例代码
init_kernel = np.array([[-1.0,-1.0,-1.0],
                      [0,0,0],
                      [1.0,1.0,1.0],

                      [-2.0,-2.0,-2.0],
                      [0,0,0],
                      [2.0,2.0,2.0],

                      [-1.0,-1.0,-1.0],
                      [0,0,0],
                      [1.0,1.0,1.0]])

print(init_kernel.shape)
init_kernel = init_kernel.reshape([3,3,3,1])    # height width inc outc

d19ad8afa0c540bc9ef00fef935c2725.png

# 相关示例代码
kernel_initializer = tf.keras.initializers.constant(init_kernel)
conv_layer = tf.keras.layers.Conv2D(1, (3, 3),
                                    activation='relu',
                                    padding="same",
                                    input_shape=img.shape[1:],
                                    kernel_initializer=kernel_initializer)

print(conv_layer.get_weights())  # 尚未进行计算

89d7cc7a4df74d538c3ecd3ccb51c29f.png

# 相关示例代码
out = conv_layer(img)

# 查看卷积结果
print(out.shape)
outimg = np.squeeze(out)
print(outimg.shape)

outimg = outimg.astype("uint8")#将输出图像数组的数据类型转换为 uint8

# 显示图像
plt.imshow(outimg)
plt.axis('off')  # 关闭坐标轴
plt.show()

5ee936ff4f124cf2ba626799f739e52d.png1421acd7798b4b7789c3056a5e88eca0.png77d595195cdd479eaa946e06a01b6de9.png

# 相关示例代码
# 最大池化
max_pooling_layer = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))
out_max_pool = max_pooling_layer(out)

print(out_max_pool.shape)

3479abccfd294b969cc803451a5aad63.png 

# 相关示例代码
outimg = np.squeeze(out_max_pool) #降低维度
print(outimg.shape)


# 显示图像
plt.imshow(outimg)
plt.axis('off')  # 关闭坐标轴
plt.show()

 

 

 

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值