python - Keras Flatten Conv3D ValueError Flatten 的输入形状未完全定义

标签 python keras

我正在尝试使用 Keras 和基于 Marcin 的 PS3 示例的 Tensorflow 后端构建一个可变长度序列分类模型:https://stackoverflow.com/a/42635571/1203882

我收到一个错误:

ValueError:“Flatten”输入的形状未完全定义(得到(无,1,1,32)。确保将完整的“input_shape”或“batch_input_shape”参数传递给第一个模型中的图层。

我尝试在 Inception 层上放置一个输入形状,但错误仍然存​​在。我该如何纠正这个问题?

重现:

import numpy as np
import keras
from keras.utils import to_categorical
from keras.layers import TimeDistributed, Conv3D, Input, Flatten, Dense
from keras.applications.inception_v3 import InceptionV3
from random import randint
from keras.models import Model

HEIGHT = 224
WIDTH = 224
NDIMS = 3
NUM_CLASSES = 4

def input_generator():
    while True:
        nframes = randint(1,5)
        label = randint(0,NUM_CLASSES-1)
        x = np.random.random((nframes, HEIGHT, WIDTH, NDIMS))
        x = np.expand_dims(x, axis=0)
        y = keras.utils.to_categorical(label, num_classes=NUM_CLASSES)
        yield (x, y)

def make_model():
    layers = 32
    inp = Input(shape=(None, HEIGHT, WIDTH, NDIMS))
    cnn = InceptionV3(include_top=False, weights='imagenet')
    # cnn = InceptionV3(include_top=False, weights='imagenet', input_shape=(HEIGHT, WIDTH, NDIMS)) # same result
    td = TimeDistributed(cnn)(inp)
    c3da = Conv3D(layers, 3,3,3)(td)
    c3db = Conv3D(layers, 3,3,3)(c3da)
    flat = Flatten()(c3db)
    out = Dense(NUM_CLASSES, activation="softmax")(flat)
    model = Model(input=(None, HEIGHT, WIDTH, NDIMS), output=out)
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
    return model

if __name__ == '__main__':
    model = make_model()
    model.fit_generator(input_generator(), samples_per_epoch=5, nb_epoch=2, verbose=1)

最佳答案

不可能展平可变长度的张量。如果可能的话,Keras 如何知道最后一个全连接层的输入单元数?模型的参数数量需要在图创建时定义。

您的问题有两种可能的解决方案:

a) 固定帧数:

inp = Input(shape=(NFRAMES, HEIGHT, WIDTH, NDIMS))

b) 在展平层之前聚合帧的维度。例如:

from keras.layers import Lambda
import keras.backend as K    

def make_model():
    layers = 32
    inp = Input(shape=(None, HEIGHT, WIDTH, NDIMS))
    cnn = InceptionV3(include_top=False, weights='imagenet')
    # cnn = InceptionV3(include_top=False, weights='imagenet', input_shape=(HEIGHT, WIDTH, NDIMS)) # same result
    td = TimeDistributed(cnn)(inp)
    c3da = Conv3D(layers, 3,3,3)(td)
    c3db = Conv3D(layers, 3,3,3)(c3da)
    aggregated = Lambda(lambda x: K.sum(x, axis=1))(c3db)
    flat = Flatten()(aggregated)
    out = Dense(NUM_CLASSES, activation="softmax")(flat)
    model = Model(input=inp, output=out)
    model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
    return model

注意 1:可能有更好的策略来聚合框架的维度。

注2: keras.utils.to_categorical的输入应该是标签列表:

y = keras.utils.to_categorical([label], num_classes=NUM_CLASSES)

关于python - Keras Flatten Conv3D ValueError Flatten 的输入形状未完全定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51461935/

相关文章:

python - Django + sqlite + Unicode

python - 使用 python 的 NLTK 计算动词、名词和其他词性

machine-learning - Tensorflow:确定药物剂量的用例

python - 对于 Keras LSTM,传递滞后特征与特征时间步长有什么区别?

python - 如何将 1D numpy 数组从 keras 层输出更改为图像(3D numpy 数组)

python - 从数组中提取直方图热图所需的值

python - Django 重定向 - 不是有效的 View 函数或模式名称错误

python - 尝试一次通过搜索发送一个列表项

machine-learning - Tensorflow 2 异或实现

python - 使用Keras进行图像分类,CNN训练很慢