简体   繁体   English

如何使用tf.keras.layers通过Tensorflow conv2d批处理图像序列

[英]How to Feed Batched Sequences of Images through Tensorflow conv2d using tf.keras.layers

I have an input of: 我有以下输入:

[batch_size, number_of_images, img_size_x, img_size_y]

eg [24, 51, 28,28] 例如[24, 51, 28,28]

Now I want to process each image of an item of the batch through a Conv2d-Layer and collect the outputs. 现在,我想通过Conv2d-Layer处理该批处理项目的每个图像,并收集输出。

I would like to reshape the input using a layer 我想使用一层重塑输入

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

to get something like [1224, 1, 28, 28] 得到类似[1224, 1, 28, 28]

which I can process. 我可以处理。

This is a minimal example to reproduce the error 这是重现该错误的最小示例

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)

I assume from the following error that this reshape layer requests the input in the form of [24, 1, 28, 28] but I need to pass [24, 51, 1, 28, 28] 我从以下错误中假定,此重塑层以[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]

Do you have any recommendations or see another possibility to structure my model? 您有什么建议或发现构建我的模型的另一种可能性?

If I use tf.reshape this works fine, but I get trouble using Keras functional API, as the output of tf.reshape is no output of a proper Layer. 如果我使用tf.reshape可以正常工作,但是使用Keras功能API会遇到麻烦,因为tf.reshape的输出没有适当Layer的输出。

Thanks in advance 提前致谢

@Berriel Thank you very much for your answer. @Berriel非常感谢您的回答。 If I change the code to the following everything works great. 如果将代码更改为以下内容,则一切正常。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM