无法训练堆叠式Keras模型,错误:您必须输入占位符张量'input_2'的值

时间:2019-03-22 22:59:58

标签: tensorflow keras

我正在尝试使用2个经过预训练的模型来构建模型。这是堆叠模型的代码:

model_only_translation = model_t.get_net_cap_fast_translation(patch_z, patch_height, patch_width, n_ch, n_class_t)
model_only_rotation_nv1 = model_r.get_net_cap_fast_rotation_nv_1(patch_z, patch_height, patch_width, n_ch, n_class_r)
if model_translation_pretrained != None:
    model_only_translation.load_weights(model_translation_pretrained)
model_only_translation.trainable = translation_trainable
if model_rotation_pretrained != None:
    model_only_rotation_nv1.load_weights(model_rotation_pretrained)

centered_image = Lambda(lambda x: tf.expand_dims(apply_translation_back(x[0][:, :, :, :, 0], x[1]), -1))([model_only_translation.input, model_only_translation.output])  # model_only_translation.output: [bs, 3] for translation params
centered_image = Lambda(lambda x: tf.reshape(x, [-1, patch_z, patch_height, patch_width, n_ch]))(centered_image)
# centered_image = Lambda(lambda x: tf.reshape(tf.expand_dims(apply_translation_back(x[0][:, :, :, :, 0], x[1]), -1), [-1, patch_z, patch_height, patch_width, n_ch]))(
#     [model_only_translation.input,
#      model_only_translation.output])  # model_only_translation.output: [bs, 3] for translation params

out_caps = model_only_rotation_nv1(centered_image)
model = Model(model_only_translation.input, [out_caps, model_only_translation.output])
# model = Model(model_only_translation.input, out_caps)

if translation_trainable:
    weight_trans = 1
else:
    weight_trans = 0
model.compile(optimizer='Adam', loss='mse', metrics=['mse'], loss_weights=[1., weight_trans])
# model.compile(optimizer='Adam', loss='mse', metrics=['mse'])
return model

对于每个模型(model_only_translation和model_only_rotation),它们都是相似的模型:

    def get_net_cap_fast_rotation_nv_1(patch_z, patch_height, patch_width, n_ch, n_class_r):
    inputs = Input(shape=(patch_z, patch_height, patch_width, n_ch))
    # conv1 = inception_block(inputs)
    conv2 = Conv3D(8, (9, 9, 9), activation='relu', padding='valid')(inputs)
    conv2 = MaxPooling3D(pool_size=(2, 2, 2))(conv2)
    conv2 = BatchNormalization(axis=-1)(conv2)

    # conv2 = Conv3D(32, (3, 3, 3), activation='relu', padding='valid')(conv2)
    # conv2 = MaxPooling3D(pool_size=(2, 2, 2))(conv2)
    # conv2 = BatchNormalization(axis=-1)(conv2)
    # conv2 = MaxPooling3D(pool_size=(2,2,2))(conv2)
    # conv2 = Conv3D(256, (9, 9, 9), activation='relu', padding='valid')(conv2)
    # conv2 = MaxPooling3D(pool_size=(2, 2, 2))(conv2)
    # conv2 = Conv3D(32, (5, 5, 5), strides=(2,2,2), activation='relu', padding='valid')(conv2)

    primarycaps = PrimaryCap3D_fast(conv2, dim_capsule=16, kernel_size=9, strides=2, padding='valid')

    # digitcaps = CapsuleLayer(num_capsule=16, dim_vector=16, num_routing=routings,
    #                          name='digitcaps')(primarycaps)

    digitcaps = CapsuleLayer(num_capsule=1, dim_vector=32, num_routing=routings,
                             name='digitcaps_rotation')(primarycaps)

    # out_caps = Length(name='out_caps')(digitcaps)
    out_caps = Lambda(lambda x: tf.reshape(x, [-1, 32]))(digitcaps)
    out_caps = Dense(n_class_r, activation='tanh')(out_caps)
    out_caps = Lambda(lambda x: unit_scaling(x), name="params_out_rotation")(out_caps)


    # model
    model = Model(inputs=inputs, outputs=out_caps)
    # model.trainable = False

    # model.compile(optimizer='Adadelta', loss='mean_absolute_error',loss_weights=[1,0.5,0.3,0.1])
    adadelta = Adadelta(lr=0.0001, rho=0.95, epsilon=1e-08, decay=0.01)
    sgd = SGD(lr=0.01, momentum=0.9, decay=0.01, nesterov=False, clipvalue=0.5)
    model.compile(optimizer='Adam', loss='mse', metrics=['mse'])

    return model


    def get_net_cap_fast_translation(patch_z, patch_height, patch_width, n_ch, n_class):
    inputs = Input((patch_z, patch_height, patch_width, n_ch))

    # conv1 = inception_block(inputs)
    conv2 = Conv3D(8, (9, 9, 9), activation='relu', padding='valid')(inputs)
    conv2 = MaxPooling3D(pool_size=(2, 2, 2))(conv2)
    conv2 = BatchNormalization(axis=-1)(conv2)

    # conv2 = Conv3D(32, (3, 3, 3), activation='relu', padding='valid')(conv2)
    # conv2 = MaxPooling3D(pool_size=(2, 2, 2))(conv2)
    # conv2 = BatchNormalization(axis=-1)(conv2)
    # conv2 = MaxPooling3D(pool_size=(2,2,2))(conv2)
    # conv2 = Conv3D(256, (9, 9, 9), activation='relu', padding='valid')(conv2)
    # conv2 = MaxPooling3D(pool_size=(2, 2, 2))(conv2)
    # conv2 = Conv3D(32, (5, 5, 5), strides=(2,2,2), activation='relu', padding='valid')(conv2)

    primarycaps = PrimaryCap3D_fast(conv2, dim_capsule=16, kernel_size=9, strides=2, padding='valid')

    # digitcaps = CapsuleLayer(num_capsule=16, dim_vector=16, num_routing=routings,
    #                          name='digitcaps')(primarycaps)

    digitcaps = CapsuleLayer(num_capsule=1, dim_vector=32, num_routing=routings,
                             name='digitcaps1')(primarycaps)

    # out_caps = Length(name='out_caps')(digitcaps)
    out_caps = Lambda(lambda x: tf.reshape(x, [-1, 32]))(digitcaps)
    out_caps = Dense(n_class, name="params_out_translation")(out_caps)


    # model
    model = Model(inputs=inputs, outputs=out_caps)
    # model.trainable = False

    # model.compile(optimizer='Adadelta', loss='mean_absolute_error',loss_weights=[1,0.5,0.3,0.1])
    adadelta = Adadelta(lr=0.0001, rho=0.95, epsilon=1e-08, decay=0.01)
    sgd = SGD(lr=0.01, momentum=0.9, decay=0.01, nesterov=False, clipvalue=0.5)

    model.compile(optimizer='Adam', loss='mse', metrics=['mse'])

    return model

但是,模型编译没有错误,但是在训练过程中出现了此错误:

Epoch 1/100
Traceback (most recent call last):
  File "D:\packages\python3\lib\site-packages\tensorflow\python\client\session.py", line 1322, in _do_call
    return fn(*args)
  File "D:\packages\python3\lib\site-packages\tensorflow\python\client\session.py", line 1307, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "D:\packages\python3\lib\site-packages\tensorflow\python\client\session.py", line 1409, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'input_2' with dtype float and shape [?,40,125,125,1]
     [[Node: input_2 = Placeholder[dtype=DT_FLOAT, shape=[?,40,125,125,1], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
     [[Node: model_2/digitcaps_rotation/Reshape/_293 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_2230_model_2/digitcaps_rotation/Reshape", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last):
  File "D:/code/KneeLM/Multi-RL/rotation_detection/Cascade_detection_translation_2rotations_nv/solution_regression_separate/main_whole_in_cascade_nv1.py", line 163, in <module>
    hist=network.fit_generator(myGenerator(training_index),steps_per_epoch=len(training_index)/batch_size,callbacks=callbacks_list,epochs=nb_epoch)
  File "D:\packages\python3\lib\site-packages\keras\legacy\interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
  File "D:\packages\python3\lib\site-packages\keras\engine\training.py", line 2042, in fit_generator
    class_weight=class_weight)
  File "D:\packages\python3\lib\site-packages\keras\engine\training.py", line 1762, in train_on_batch
    outputs = self.train_function(ins)
  File "D:\packages\python3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2273, in __call__
    **self.session_kwargs)
  File "D:\packages\python3\lib\site-packages\tensorflow\python\client\session.py", line 900, in run
    run_metadata_ptr)
  File "D:\packages\python3\lib\site-packages\tensorflow\python\client\session.py", line 1135, in _run
    feed_dict_tensor, options, run_metadata)
  File "D:\packages\python3\lib\site-packages\tensorflow\python\client\session.py", line 1316, in _do_run
    run_metadata)
  File "D:\packages\python3\lib\site-packages\tensorflow\python\client\session.py", line 1335, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'input_2' with dtype float and shape [?,40,125,125,1]
     [[Node: input_2 = Placeholder[dtype=DT_FLOAT, shape=[?,40,125,125,1], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
     [[Node: model_2/digitcaps_rotation/Reshape/_293 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_2230_model_2/digitcaps_rotation/Reshape", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

由操作“ input_2”引起,定义于:

  File "D:/code/KneeLM/Multi-RL/rotation_detection/Cascade_detection_translation_2rotations_nv/solution_regression_separate/main_whole_in_cascade_nv1.py", line 143, in <module>
    network = model.get_net_cap_fast_nv1(patch_shape[0], patch_shape[1], patch_shape[2], 1, 3, 3, True, pretrained_weights_translation, pretrained_weights_rotation)
  File "D:\code\KneeLM\Multi-RL\rotation_detection\Cascade_detection_translation_2rotations_nv\solution_regression_separate\model_cascade.py", line 17, in get_net_cap_fast_nv1
    model_only_rotation_nv1 = model_r.get_net_cap_fast_rotation_nv_1(patch_z, patch_height, patch_width, n_ch, n_class_r)
  File "D:\code\KneeLM\Multi-RL\rotation_detection\Cascade_detection_translation_2rotations_nv\solution_regression_separate\model_rotation.py", line 48, in get_net_cap_fast_rotation_nv_1
    inputs = Input(shape=(patch_z, patch_height, patch_width, n_ch))
  File "D:\packages\python3\lib\site-packages\keras\engine\topology.py", line 1436, in Input
    input_tensor=tensor)
  File "D:\packages\python3\lib\site-packages\keras\legacy\interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
  File "D:\packages\python3\lib\site-packages\keras\engine\topology.py", line 1347, in __init__
    name=self.name)
  File "D:\packages\python3\lib\site-packages\keras\backend\tensorflow_backend.py", line 442, in placeholder
    x = tf.placeholder(dtype, shape=shape, name=name)
  File "D:\packages\python3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1808, in placeholder
    return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
  File "D:\packages\python3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 5835, in placeholder
    "Placeholder", dtype=dtype, shape=shape, name=name)
  File "D:\packages\python3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "D:\packages\python3\lib\site-packages\tensorflow\python\framework\ops.py", line 3392, in create_op
    op_def=op_def)
  File "D:\packages\python3\lib\site-packages\tensorflow\python\framework\ops.py", line 1718, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'input_2' with dtype float and shape [?,40,125,125,1]
     [[Node: input_2 = Placeholder[dtype=DT_FLOAT, shape=[?,40,125,125,1], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
     [[Node: model_2/digitcaps_rotation/Reshape/_293 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_2230_model_2/digitcaps_rotation/Reshape", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

我尝试了以下简单代码,没有问题:

def build_encoder(dim):
    input_img = Input(shape=(dim, ))
    # "encoded" is the encoded representation of the input
    encoded = Dense(64, activation='relu')(input_img)
    encoded = Dense(encoding_dim, activation='relu')(encoded)
    encoder = Model(input_img, encoded)
    encoder.compile(optimizer='adadelta', loss='binary_crossentropy')
    return encoder

# "decoded" is the lossy reconstruction of the input


def build_decoder(dim):
    encoded_input = Input(shape=(dim,))
    output = Dense(64, activation='relu')(encoded_input)
    output = Dense(784, activation='relu')(output)
    decoder = Model(encoded_input, output)
    decoder.compile(optimizer='adadelta', loss='binary_crossentropy')
    return decoder


# get models
encoder = build_encoder(784)
decoder = build_decoder(encoding_dim)

# encoder.trainable = False
import tensorflow as tf
centered_image = Lambda(lambda x: x[1])([encoder.input,encoder.output])
centered_image = Lambda(lambda x: tf.reshape(x, [-1, encoding_dim]))(centered_image)

decoded = decoder(centered_image)
autoencoder = Model(encoder.input, decoded)

autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

0 个答案:

没有答案