Keras / Tensorflow InvalidArgumentError:输入操作loss_8 / classification_loss / Select_1类型

时间:2017-11-13 20:42:03

标签: python-3.x tensorflow keras

对于此图像识别代码,我遇到了一个相当难以理解的错误。我正在使用具有张量流后端的Keras-Retinanet创建更快的r-cnn模型。图像注释采用PASCAL VOC格式。我最好的猜测是,数据的形成方式不正确,但是这个错误信息并没有让我更清楚地知道什么是破坏。具体来说,是:“InvalidArgumentError:输入到操作loss_8 / classification_loss / Select_1类型的Select必须具有相同的大小和形状。输入0:[67503,10]!=输入1:[67503,5]”指的是什么?

batch_size = 1 # batch_size > 1 is not yet supported
model.fit_generator(
    generator=train_generator,
    steps_per_epoch=len(train_generator.image_names) // batch_size,
    epochs=50,
    verbose=1,
    validation_data=test_generator,
    validation_steps=3000,  # len(test_generator.image_names) // batch_size,
    callbacks=[


keras.callbacks.ModelCheckpoint('snapshots/resnet50_model_1.h5', monitor='val_loss', verbose=1, save_best_only=True),
keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0),
        ],

)



---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
~/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1322     try:
-> 1323       return fn(*args)
   1324     except errors.OpError as e:

~/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
   1301                                    feed_dict, fetch_list, target_list,
-> 1302                                    status, run_metadata)
   1303 

~/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    472             compat.as_text(c_api.TF_Message(self.status.status)),
--> 473             c_api.TF_GetCode(self.status.status))
    474     # Delete the underlying status object from memory otherwise it stays alive

InvalidArgumentError: Inputs to operation loss_8/classification_loss/Select_1 of type Select must have the same size and shape.  Input 0: [67503,10] != input 1: [67503,5]
     [[Node: loss_8/classification_loss/Select_1 = Select[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](loss_8/classification_loss/Equal, loss_8/classification_loss/sub_1, loss_8/classification_loss/GatherNd)]]
     [[Node: loss_8/classification_loss/Mean_2/_7785 = _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_12605_loss_8/classification_loss/Mean_2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-55-006fdf1f857f> in <module>()
      9     callbacks=[
     10         keras.callbacks.ModelCheckpoint('snapshots/resnet50_model_1.h5', monitor='val_loss', verbose=1, save_best_only=True),
---> 11         keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1, mode='auto', epsilon=0.0001, cooldown=0, min_lr=0),
     12     ],
     13 )

~/anaconda3/lib/python3.5/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
     85                 warnings.warn('Update your `' + object_name +
     86                               '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 87             return func(*args, **kwargs)
     88         wrapper._original_function = func
     89         return wrapper

~/anaconda3/lib/python3.5/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
   2075                     outs = self.train_on_batch(x, y,
   2076                                                sample_weight=sample_weight,
-> 2077                                                class_weight=class_weight)
   2078 
   2079                     if not isinstance(outs, list):

~/anaconda3/lib/python3.5/site-packages/keras/engine/training.py in train_on_batch(self, x, y, sample_weight, class_weight)
   1795             ins = x + y + sample_weights
   1796         self._make_train_function()
-> 1797         outputs = self.train_function(ins)
   1798         if len(outputs) == 1:
   1799             return outputs[0]

~/anaconda3/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
   2330         updated = session.run(self.outputs + [self.updates_op],
   2331                               feed_dict=feed_dict,
-> 2332                               **self.session_kwargs)
   2333         return updated[:len(self.outputs)]
   2334 

~/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    887     try:
    888       result = self._run(None, fetches, feed_dict, options_ptr,
--> 889                          run_metadata_ptr)
    890       if run_metadata:
    891         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1118     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1119       results = self._do_run(handle, final_targets, final_fetches,
-> 1120                              feed_dict_tensor, options, run_metadata)
   1121     else:
   1122       results = []

~/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1315     if handle is None:
   1316       return self._do_call(_run_fn, self._session, feeds, fetches, targets,
-> 1317                            options, run_metadata)
   1318     else:
   1319       return self._do_call(_prun_fn, self._session, handle, feeds, fetches)

~/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1334         except KeyError:
   1335           pass
-> 1336       raise type(e)(node_def, op, message)
   1337 
   1338   def _extend_graph(self):

InvalidArgumentError: Inputs to operation loss_8/classification_loss/Select_1 of type Select must have the same size and shape.  Input 0: [67503,10] != input 1: [67503,5]
     [[Node: loss_8/classification_loss/Select_1 = Select[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](loss_8/classification_loss/Equal, loss_8/classification_loss/sub_1, loss_8/classification_loss/GatherNd)]]
     [[Node: loss_8/classification_loss/Mean_2/_7785 = _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_12605_loss_8/classification_loss/Mean_2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

Caused by op 'loss_8/classification_loss/Select_1', defined at:
  File "/home/james/anaconda3/lib/python3.5/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/james/anaconda3/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/james/anaconda3/lib/python3.5/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/home/james/anaconda3/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/home/james/anaconda3/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/home/james/anaconda3/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/home/james/anaconda3/lib/python3.5/site-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/home/james/anaconda3/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/james/anaconda3/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/home/james/anaconda3/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/home/james/anaconda3/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/home/james/anaconda3/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/james/anaconda3/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/home/james/anaconda3/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/home/james/anaconda3/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/home/james/anaconda3/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/james/anaconda3/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/james/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2698, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/james/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2802, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/james/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-50-1a9dc197138b>", line 11, in <module>
    optimizer=keras.optimizers.adam(lr=1e-5, clipnorm=0.001)
  File "/home/james/anaconda3/lib/python3.5/site-packages/keras/engine/training.py", line 860, in compile
    sample_weight, mask)
  File "/home/james/anaconda3/lib/python3.5/site-packages/keras/engine/training.py", line 460, in weighted
    score_array = fn(y_true, y_pred)
  File "/home/james/anaconda3/lib/python3.5/site-packages/keras_retinanet/losses.py", line 37, in _focal
    focal_weight = keras_retinanet.backend.where(keras.backend.equal(labels, 1), 1 - classification, classification)
  File "/home/james/anaconda3/lib/python3.5/site-packages/keras_retinanet/backend/tensorflow_backend.py", line 47, in where
    return tensorflow.where(*args, **kwargs)
  File "/home/james/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py", line 2441, in where
    return gen_math_ops._select(condition=condition, t=x, e=y, name=name)
  File "/home/james/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/gen_math_ops.py", line 3988, in _select
    "Select", condition=condition, t=t, e=e, name=name)
  File "/home/james/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/home/james/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2956, in create_op
    op_def=op_def)
  File "/home/james/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1470, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): Inputs to operation loss_8/classification_loss/Select_1 of type Select must have the same size and shape.  Input 0: [67503,10] != input 1: [67503,5]
     [[Node: loss_8/classification_loss/Select_1 = Select[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](loss_8/classification_loss/Equal, loss_8/classification_loss/sub_1, loss_8/classification_loss/GatherNd)]]
     [[Node: loss_8/classification_loss/Mean_2/_7785 = _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_12605_loss_8/classification_loss/Mean_2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

0 个答案:

没有答案