我对该模型有两个输入:
input_img = Input(shape=(self.img_rows, self.img_cols, self.channels,))
input_cond = Input(shape=(self.cond_dim,))
它们的形状不同,我想将input_cond
的每个元素扩展为与[img_rows,img_cols]
大小相同的图像。
我写道:
def conv_cond_concat(tensors):
x_shapes = tensors[0].get_shape()
y_shapes = tensors[1].get_shape()
print (x_shapes,y_shapes)
return tensors[1]*K.ones([x_shapes[0], x_shapes[1], x_shapes[2], y_shapes[1]])
然后:
lamb_layer = Lambda(conv_cond_concat)
cond_img = lamb_layer([input_img,input_cond])
我收到以下投诉:
File "/homeXXXXanaconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 708, in ones
return variable(tf.constant_initializer(1., dtype=tf_dtype)(shape),
File "/home/XXXXanaconda2/lib/python2.7/site-packages/tensorflow/python/ops/init_ops.py", line 200, in __call__
self.value, dtype=dtype, shape=shape, verify_shape=verify_shape)
File "/home/XXXanaconda2/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 208, in constant
value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "/home/XXXanaconda2/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 380, in make_tensor_proto
if shape is not None and np.prod(shape, dtype=np.int64) == 0:
File "/home/XXXanaconda2/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2566, in prod
out=out, **kwargs)
File "/home/XXXanaconda2/lib/python2.7/site-packages/numpy/core/_methods.py", line 35, in _prod
return umr_prod(a, axis, dtype, out, keepdims)
TypeError: __long__ returned non-long (type NoneType)
该如何正确完成?
谢谢
答案 0 :(得分:0)
该错误是由x_shapes [0]引起的。第一维对应于批量大小,并且为无。
我重新编写了conv_cond_concat函数,该函数将添加动态尺寸。
def conv_cond_concat(tensors):
x_shapes = tensors[0].get_shape()
y_shapes = tensors[1].get_shape()
print (x_shapes,y_shapes)
tmp = K.placeholder((None, x_shapes[1], x_shapes[2], y_shapes[1]))
return tensors[1]*K.ones_like(tmp)
我不确定是否有更好的方法来添加动态尺寸。如果是这样,我想知道。
答案 1 :(得分:0)
您可以执行以下操作:
def broadcast(input):
img = input[0]
cond = input[1]
return K.tf.broadcast_to(cond, K.concatenate([K.shape(img), K.shape(cond)[-1:]]))
## Test the implementation
input_img = Input(shape=(4,4,3,))
input_cond = Input(shape=(2,))
output = Lambda(broadcast)([input_img, input_cond])
model = Model([input_img, input_cond], output)
model.predict([np.zeros((1,4,4,3)), np.random.random(2)[None]])
### output shape is (4, 4, 3, 2)