来自tf.nn.conv2d和keras.layers.Conv2D的不相等输出

问题描述 投票:1回答:1

我一直在阅读AurélienGéron(textbook publisher webpage here)的Hands-On Machine Learning教科书(第二版)。我已经研究了将CNN应用于图像的内容。在第14章标题为Tensorflow实现的部分中,它们手动创建了过滤器,该过滤器将传递给tf.nn.conv2d并应用于图像以生成一组特征图。在这些手动过滤器示例之后,该书说:

在真实的CNN中,您通常将过滤器定义为可训练的变量...,而不是手动创建变量,请使用keras.layers.Conv2D层。

[上面的引用对我而言意味着,给定相同的输入(和等效的初始化),我们应该能够从tf.nn.conv2dkeras.layers.Conv2D导出相同的输出。为了验证这个想法,我检查了两个函数是否等效。根据this previously answered SO post对于卷积,两个函数相同

我开始对它们的等效性进行简单测试。我使用7x7过滤器(也称为卷积内核全部零创建了一个由一个特征图组成的卷积层,该卷积层分别针对tf.nn.conv2dkeras.layers.Conv2D实现。不出所料,在将两个图像的差异中的所有像素值相加后,此滤波器确实使输出图像的每个像素值的值为零。零差意味着输出图像是相同的。

然后我决定创建相同的7x7过滤器,但是这次全是。理想情况下,两个函数应产生相同的输出,因此两个输出图像中的差应为零。不幸的是,当我检查输出图像中的差异(并对每个像素的差异求和)时,我得到一个非零的总和值。在绘制图像和它们的差异时,很明显它们不是同一幅图像(尽管乍一看它们确实很相似)。]阅读完这两个功能的文档后,我相信我会给他们同样的输入。

我该怎么做/错误地假设这会阻止两个函数产生相同的输出?

我在下面附上我的代码和版本信息,以供参考。该代码使用scikit-learn china.jpg样本图像作为输入,并使用matplotlib.pyplot.imshow来帮助可视化输出图像及其差异。

TF版本:2.2.0-dev20200229

Keras版本:2.3.1

Scikit-Learn版本:0.22.1

Matplotlib版本:3.1.3

Numpy版本:1.18.1

from sklearn.datasets import load_sample_image import matplotlib.pyplot as plt import tensorflow as tf from tensorflow import keras import numpy as np # Get the feature map as a result of tf.nn.conv2d def featureMap1(batch): # Extract the channels batch_size, height, width, channels = batch.shape # Make a (7,7,3,1) filter set (one set of a 7x7 filter per channel) # of just ones. filters = np.ones(shape=(7, 7, channels, 1), dtype=np.float32) # Run the conv2d with stride of 1 (i.e: in.shape = out.shape) # Generate one feature map for this conv layer fmaps = tf.nn.conv2d(batch, filters, strides=1, padding='SAME', data_format='NHWC') # Return the feature map return fmaps # Get the feature map as a result of keras.layers.Conv2D def featureMap2(batch): # Create the input layer with the shape of the images inputLayer = keras.layers.Input(shape=batch.shape[1:]) # Create the convLayer which should apply the filter of all ones convLayer = keras.layers.Conv2D(filters=1, kernel_size=7, strides=1, padding='SAME', kernel_initializer='ones', data_format='channels_last', activation='linear') # Create the ouput layer outputLayer = convLayer(inputLayer) # Set up the model model = keras.Model(inputs=inputLayer, outputs=outputLayer) # Perform a prediction, no model fitting or compiling fmaps = model.predict(batch) return fmaps def main(): # Get the image and scale the RGB values to [0, 1] china = load_sample_image('china.jpg') / 255 # Build a batch of just one image batch = np.array([china]) # Get the feature maps and extract # the images within them img1 = featureMap1(batch)[0, :, :, 0] img2 = featureMap2(batch)[0, :, :, 0] # Calculate the difference in the images # Ideally, this should be all zeros... diffImage = np.abs(img1 - img2) # Add up all the pixels in the diffImage, # we expect a value of 0 if the images are # identical print('Differences value: ', diffImage.sum()) # Plot the images as a set of 4 figsize = 10 f, axarr = plt.subplots(2, 2, figsize=(figsize,figsize)) axarr[0,0].set_title('Original Image') axarr[0,0].imshow(batch[0], cmap='gray') axarr[1,0].set_title('Conv2D through tf.nn.conv2d') axarr[1,0].imshow(img1, cmap='gray') axarr[1,1].set_title('Conv2D through keras.layers.Conv2D') axarr[1,1].imshow(img2, cmap='gray') axarr[0,1].set_title('Diff') axarr[0,1].imshow(diffImage, cmap='gray') plt.show() return main()

我一直在阅读AurélienGéron的动手机器学习教科书(第二版)(此处是教科书的出版商网页)。我已经研究了将CNN应用于图像的内容。在此部分...
python function tensorflow keras equivalent
1个回答
0
投票
两个卷积层的输出应该相同。

您正在将

Model

© www.soinside.com 2019 - 2024. All rights reserved.