【发布时间】:2019-07-17 19:34:05
【问题描述】:
我分别构建了两个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-loop索引。应该怎么做才能使用呢?
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'
【问题讨论】:
标签: python tensorflow keras tensor