python - 如何使用 tf.keras.layers 通过 Tensorflow conv2d 馈送批量图像序列

标签 python tensorflow machine-learning keras deep-learning

我有一个输入:

[batch_size, number_of_images, img_size_x, img_size_y]

例如[24, 51, 28,28]

现在我想通过 Conv2d-Layer 处理批处理中某个项目的每个图像并收集输出。

我想使用图层 reshape 输入

tf.keras.layer.Reshape(1,28,28)

得到类似[1224, 1, 28, 28]

我可以处理。

这是重现错误的最小示例

import numpy as np
import tensorflow as tf
tf.enable_eager_execution()

input_data = np.ones((24, 51, 28, 28))
input_label = np.ones((24, 51, 10))

output_data = np.ones((24, 10))

inp_layer = tf.keras.layers.Input(shape=(51, 28, 28))
input_batch_label = tf.keras.layers.Input(shape=(51, 10))

res1 = tf.keras.layers.Reshape((1, 28, 28), name="reshape1")(inp_layer)
perm1 = tf.keras.layers.Permute((2, 3, 1))(res1)
cnn1 = tf.keras.layers.Conv2D(64, 3, padding="same", activation='relu')(perm1)
max1 = tf.keras.layers.MaxPooling2D(16, 16, padding="valid")(cnn1)
res2 = tf.keras.layers.Reshape((51, 64))(max1)

combined_input = tf.keras.layers.concatenate([res2, input_batch_label], axis=-1, )

flat = tf.keras.layers.Flatten()(combined_input)
fc1 = tf.keras.layers.Dense(10)(flat)


model = tf.keras.Model(inputs=[inp_layer, input_batch_label], outputs=fc1)
model.compile(optimizer=tf.train.AdamOptimizer(0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
model.fit([input_data, input_label], output_data, batch_size=24, verbose=1)

我从以下错误中假设此 reshape 层请求以 [24, 1, 28, 28] 形式的输入,但我需要传递 [24, 51, 1 , 28, 28]

tensorflow.python.framework.errors_impl.InvalidArgumentError: 
Input to reshape is a tensor with 959616 values, but the requested shape has 18816
[[{{node Reshape}}]] [Op:StatefulPartitionedCall]

您有任何建议或看到构建我的模型的另一种可能性吗?

如果我使用 tf.reshape ,效果很好,但使用 Keras 功能 API 时会遇到麻烦,因为 tf.reshape 的输出不是正确层的输出。

提前致谢

最佳答案

@Berriel 非常感谢您的回答。 如果我将代码更改为以下内容,一切都会很好。

def reshape1():
    def func(x):
        ret = tf.reshape(x, [-1, 1, 28, 28])
        return ret
    return tf.keras.layers.Lambda(func)

def reshape2():
    def func(x):
        ret = tf.reshape(x, [-1, 51, 64])
        return ret
    return tf.keras.layers.Lambda(func)

res1 = reshape1()(inp_layer)
perm1 = tf.keras.layers.Permute((2, 3, 1))(res1)
cnn1 = tf.keras.layers.Conv2D(64, 3, padding="same", activation='relu')(perm1)
max1 = tf.keras.layers.MaxPooling2D(16, 16, padding="valid")(cnn1)
#res2 = tf.keras.layers.Reshape((51, 64))(max1)
res2 = reshape2()(max1)
combined_input = tf.keras.layers.concatenate([res2, input_batch_label], axis=-1, )

flat = tf.keras.layers.Flatten()(combined_input)
fc1 = tf.keras.layers.Dense(10)(flat)

关于python - 如何使用 tf.keras.layers 通过 Tensorflow conv2d 馈送批量图像序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55667521/

相关文章:

python - OpenCV如何平滑轮廓,降低噪声

python - 如何在Tensorflow中使用变量的旧值和新值?

python - 如何从tensorflow fully_connected获取权重

machine-learning - 迭代应用 keras 模型 fit() 时的学习率状态如何?

python - Else子句在Python异常处理中的应用

python - "Insufficient Permission: Request had insufficient authentication scopes"即使是最通用的范围

python - Git 预提交 Hook 在 Windows 上不起作用

python - 如何在 tensorflow 中正确地将一个数组附加到另​​一个数组?

scala - Apache Spark ALS - 它如何解决最小二乘问题?

apache-spark - Spark MLlib 中的 HashingTF 中的 numFeatures 与文档中的实际术语数之间有什么关系?