繁体   English   中英

在一个功能模型中合并两个keras顺序模型时出错:AttributeError:'NoneType'对象没有属性'_inbound_nodes'

[英]Error when combine two keras sequential models inside one functional model: AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

我分别构建了两个keras顺序模型,并使用keras功能api组合了这两个模型。 在它们之间,我应用了数据重新打包,调整了通过两个顺序模型传递的数据的大小。

当我进行组合时,存在错误。 但我不太了解“ NoneType”中的哪个对象。

另一个问题是 ,在进行数据张量重新打包时,我将所有零都放置在占位符内。 当我运行model.fit时,将其替换为实际数据,还是将它们恒定为零? 将两个顺序模型之间的张量调整大小结合起来根本不适用吗?

# I use a keras sequential model to define the 2d model "base_model_SRCNN"
# I define the 3d model still using keras sequential model as "SRnet_3d_model"

ip = Input(shape = (TARGET_HEIGHT, TARGET_WIDTH, 1))
SRCNN_network = base_model_SRCNN(FILENAME, TARGET_HEIGHT, TARGET_WIDTH) (ip)
#----------------------------pack frame one by one---------------------------------------
...
# In this section, I re-pack the output tensor of SRCNN_network,
# the resulted new tensor is called "package_set_tensor"
# I've checked and ensure the type of "package_set_tensor" is a tensor, and the shape is (294, 5, 352, 288, 1)
#---------------------------build 3dSRnet model--------------------------------------------
SRnet_layer = SRnet_3d_model(AMOUNT, DEPTH, TARGET_HEIGHT, TARGET_WIDTH)(package_set_tensor)

#--------------------------test the result of combination----------------------------------
combined_model = Model(inputs = ip, outputs = SRnet_layer)

#I 've checked the type of SRnet_layer and ip are <class 'tensorflow.python.framework.ops.Tensor'>

编辑

#here's the lambda layer I defined:
def repacking(x):
    #----------------------------get shape from input tensor---------------------------------
    (AMOUNT, TARGET_HEIGHT, TARGET_WIDTH, tmp) = x.shape
    AMOUNT = keras.backend.shape(x)[0]#will return an empty tensor
    # or using 'list(x.shape)[0]' to return a Nontype object
    DEPTH = 5
    #AMOUNT = 26
    #----------------------------pack frame one by one---------------------------------------
    FIRST = True
    HALF_RANGE = math.floor(DEPTH/2)

    for i in range(AMOUNT):
        if (i - HALF_RANGE) < 0 or (i + HALF_RANGE) >= AMOUNT:
            AMOUNT = AMOUNT - 1
        else:
            if DEPTH%2 == 0:
                RANGE = range(i - HALF_RANGE, i + HALF_RANGE)
            else:
                RANGE = range(i - HALF_RANGE, i + HALF_RANGE + 1)

            for j in RANGE:
                frame = x[j, :, :, :]  #(352, 288, 1), type = tensor
                frame = tf.reshape(frame,(1, TARGET_HEIGHT, TARGET_WIDTH))

                if j == i - HALF_RANGE:
                    package = frame
                else:   
                    package = tf.concat([package, frame], 0)

            if FIRST == True:
                package_set = package
                FIRST = False
            else:
                package_set = tf.concat([package_set, package], 0)

    package_set = tf.reshape(package_set, (AMOUNT, DEPTH, TARGET_HEIGHT, TARGET_WIDTH, 1))   #(294, 5, 352, 288, 1)
    return package_set

但是batch_size(我命名为AMOUNT )信息不能用作for循环索引。 我应该怎么做才能使用它?

Traceback (most recent call last):
  File "main.py", line 71, in <module>
    model = combined(FILENAME, AMOUNT, DEPTH, TARGET_HEIGHT, TARGET_WIDTH)
  File "/home/user1/REUS/image-reconstruction/code/functional/model_build_up.py", line 132, in combined
    combined_model = Model(inputs = ip, outputs = SRnet_layer)
  File "/home/user1/.conda/envs/tf-cpu/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/home/user1/.conda/envs/tf-cpu/lib/python3.7/site-packages/keras/engine/network.py", line 93, in __init__
    self._init_graph_network(*args, **kwargs)
  File "/home/user1/.conda/envs/tf-cpu/lib/python3.7/site-packages/keras/engine/network.py", line 231, in _init_graph_network
    self.inputs, self.outputs)
  File "/home/user1/.conda/envs/tf-cpu/lib/python3.7/site-packages/keras/engine/network.py", line 1366, in _map_graph_network
    tensor_index=tensor_index)
  File "/home/user1/.conda/envs/tf-cpu/lib/python3.7/site-packages/keras/engine/network.py", line 1353, in build_map
    node_index, tensor_index)
  File "/home/user1/.conda/envs/tf-cpu/lib/python3.7/site-packages/keras/engine/network.py", line 1325, in build_map
    node = layer._inbound_nodes[node_index]
AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

Keras图层(模型表现为图层)将keras图层作为输入,而不是张量。

您需要将Layer参数传递给SRnet_3d_model而不是张量。

例如,举一个简单的例子:

inp = Input(shape=(2,))
out = Dense(1, activation='sigmoid')(inp)
model = Model(inp, out)

上面的变量inp是一个输入层,它不仅包含占位符张量,而且还包含用于构建图形的其他信息。 例如,当输出层out而建,keras咨询祖先层(这恰好是inp这是参数Dense.call ),以便确定该层中的输入尺寸。

因此,在构建keras模型时,您需要始终根据层进行操作(模型是Layer的子类,可以被认为是复杂的层)。

在某些情况下,现有层无法提供足够的功能。 然后,您有两个选择:Lambda图层或用户定义的图层。 Lambda层允许您以最小的开销在层内使用后端操作(或原始tf ops)。 当您希望图层拥有自己的变量/权重时,需要自定义图层。

Lambda层的一个简单示例是张量切片操作,例如:

Lambda(lambda x: x[:, 0])

请注意,在张量级别上,您需要考虑批次尺寸。 上面的lambda示例假定输入尺寸为(batch_size,n_features),并返回带有暗淡的张量(batch_size,1)。

暂无
暂无

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

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