物体边缘检测:使用多输出通道,当图像读入时,先将其转换为float32的numpy.ndarray,然后图像转换为【H,W,3】的形状,以【N,C,H,W】的数据维度输出图片
卷积核大小设置为3×3
输入图像通道数3
输出图像通道数1
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import paddle
from paddle.nn import Conv2D
from paddle.nn.initializer import Assign
img = Image.open('E:/pycharm/paddleCNN/picture/206.jpg')
w = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]], dtype='float32') / 8
w = w.reshape([1, 1, 3, 3])
w = np.repeat(w, 3, axis=1)
conv = Conv2D(in_channels=3, out_channels=1, kernel_size=[3, 3],
weight_attr=paddle.ParamAttr(
initializer=Assign(value=w)))
x = np.array(img).astype('float32')
x = np.transpose(x, (2, 0, 1))
x = x.reshape(1, 3, img.height, img.width)
x = paddle.to_tensor(x)
y = conv(x)
out = y.numpy()
plt.figure(