如何将 flatten dim 张量传递给张量流中的 Conv2D?
How to pass flatten dim tensor to Conv2D in tensorflow?
我很好奇如何将 flatten dim 输入传递到 Conv2D
,我从 RGB 图像输入开始,但我做了一些转换并得到了 1 个暗张量,我想将其传递给卷积滤波器以进行特征提取。当我将 flatten tensor 传递给 Conv2D
时,出现以下值错误:
> ValueError: Input 0 of layer conv2d_12 is incompatible with the layer:
> expected ndim=4, found ndim=2. Full shape received: [None, 784]
我的尝试:
x.get_shape()
TensorShape([None, 784, 3, 1])
x_new = Lambda(lambda x: tf.reduce_sum(tf.squeeze(x, axis=-1), axis=-1))(x)
h = Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='relu')(x_new)
h = Conv2D(filters=64, kernel_size=(3, 3), activation='relu')(h)
h = MaxPooling2D(pool_size=(2, 2))(h)
我想知道是否可以将 flatten dim 张量 x_new
传递给 Conv2D
。有什么办法可以在张量流中做到这一点?有什么想法让这发生吗?谢谢
你试过重塑吗?
tf.reshape(tf.shape(x), shape=(28, 28, 1))
你的 x
的形状是 TensorShape([None, 784, 3, 1])
。但是之后在您的 Lambda 层中使用 squeeze
将维度减小到 TensorShape([None, 784, 3])
,这很好。之后,您对最后一个通道求和并将维度更改为 TensorShape([None, 3])
。问题是 keras 和 tensorflow 始终使用 [batch_number,heigth, width, channels]
形状,但现在您只是选择了 [batch,channels]
。您现在可以使用 tf.expand_dims(x_new,axis=-1)
两次将形状扩展为 TensorShape([None, 3,1,1])
以构建一个 3 行 1 列的向量。我认为如果 Zero-Padding
被激活,那么使用 3x3 Kernel
应该不是问题 :)。所以简而言之,这就是您可能需要的代码:
x_new = Lambda(lambda x: tf.expand_dims(tf.reduce_sum(x, axis=2),axis=-1))(x)
编辑:
私聊后,此代码解决了问题:
def test(image_shape):
in_image = Input(shape=image_shape)
x_new = Lambda(lambda x: tf.expand_dims(tf.reduce_sum(x, axis=2),axis=-1))(in_image) # Sum up over the third dimension and afterwards expand the dims
b = tf.shape(x_new) # get the shape of the Lambda layer
x_new = tf.keras.layers.Reshape([28,28,1], input_shape=b) (x_new) # reshape to [None 28 28 1] Image
h = Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='relu')(x_new) # Conv
h = Conv2D(filters=64, kernel_size=(3, 3), activation='relu')(h)# Conv
model = Model(in_image, h)
return model
mod=test([None,784, 3, 1]) # Declare image-shape for the model input
aa = tf.constant(1,shape=[1,784, 3, 1]) # input a test image with only ones in it
bb = tf.constant(1,shape=[3,784, 3, 1]) # Changing batch sizes work aswell
aa_result = mod.predict(aa)
bb_result = mod.predict(bb)
我很好奇如何将 flatten dim 输入传递到 Conv2D
,我从 RGB 图像输入开始,但我做了一些转换并得到了 1 个暗张量,我想将其传递给卷积滤波器以进行特征提取。当我将 flatten tensor 传递给 Conv2D
时,出现以下值错误:
> ValueError: Input 0 of layer conv2d_12 is incompatible with the layer:
> expected ndim=4, found ndim=2. Full shape received: [None, 784]
我的尝试:
x.get_shape() TensorShape([None, 784, 3, 1]) x_new = Lambda(lambda x: tf.reduce_sum(tf.squeeze(x, axis=-1), axis=-1))(x) h = Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='relu')(x_new) h = Conv2D(filters=64, kernel_size=(3, 3), activation='relu')(h) h = MaxPooling2D(pool_size=(2, 2))(h)
我想知道是否可以将 flatten dim 张量 x_new
传递给 Conv2D
。有什么办法可以在张量流中做到这一点?有什么想法让这发生吗?谢谢
你试过重塑吗?
tf.reshape(tf.shape(x), shape=(28, 28, 1))
你的 x
的形状是 TensorShape([None, 784, 3, 1])
。但是之后在您的 Lambda 层中使用 squeeze
将维度减小到 TensorShape([None, 784, 3])
,这很好。之后,您对最后一个通道求和并将维度更改为 TensorShape([None, 3])
。问题是 keras 和 tensorflow 始终使用 [batch_number,heigth, width, channels]
形状,但现在您只是选择了 [batch,channels]
。您现在可以使用 tf.expand_dims(x_new,axis=-1)
两次将形状扩展为 TensorShape([None, 3,1,1])
以构建一个 3 行 1 列的向量。我认为如果 Zero-Padding
被激活,那么使用 3x3 Kernel
应该不是问题 :)。所以简而言之,这就是您可能需要的代码:
x_new = Lambda(lambda x: tf.expand_dims(tf.reduce_sum(x, axis=2),axis=-1))(x)
编辑:
私聊后,此代码解决了问题:
def test(image_shape):
in_image = Input(shape=image_shape)
x_new = Lambda(lambda x: tf.expand_dims(tf.reduce_sum(x, axis=2),axis=-1))(in_image) # Sum up over the third dimension and afterwards expand the dims
b = tf.shape(x_new) # get the shape of the Lambda layer
x_new = tf.keras.layers.Reshape([28,28,1], input_shape=b) (x_new) # reshape to [None 28 28 1] Image
h = Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='relu')(x_new) # Conv
h = Conv2D(filters=64, kernel_size=(3, 3), activation='relu')(h)# Conv
model = Model(in_image, h)
return model
mod=test([None,784, 3, 1]) # Declare image-shape for the model input
aa = tf.constant(1,shape=[1,784, 3, 1]) # input a test image with only ones in it
bb = tf.constant(1,shape=[3,784, 3, 1]) # Changing batch sizes work aswell
aa_result = mod.predict(aa)
bb_result = mod.predict(bb)