训练keras模型并在K.eval()上对其进行评估时出错

2024-04-24 18:41:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个非常复杂的keras模型,其中包含子模型和一个不同于训练模型的推理模型(我仅使用训练模型的一部分来推断结果),该模型基于this github中的感知图像错误代码。当我用它来预测值时,a得到了正确的预测结果,一切正常

然而,每次尝试训练时,我都会得到以下信息:

> --------------------------------------------------------------------------- InvalidArgumentError                      Traceback (most recent call
> last) <ipython-input-24-c3792779381d> in <module>
>      10     validation_steps=len(test_compares),
>      11 
> ---> 12     epochs=50, nr_gpus=2
>      13 )
> 
> /workdir/IQA/PIE+keras/model/PieAPPv0pt1_KERAS.py in train(self,
> optimizer, loss, generator, steps_per_epoch, validation_data,
> validation_steps, epochs, callbacks, train_features, nr_gpus)
>     251             train_multi = multi_gpu_model(train_model, gpus=nr_gpus)
>     252             train_multi.compile(optimizer=optimizer, loss=loss)
> --> 253             train_multi.fit_generator(generator=generator, steps_per_epoch=steps_per_epoch, epochs=epochs, callbacks=callbacks,
> validation_data=validation_data, validation_steps=validation_steps,
> shuffle=True)
>     254 
>     255         else:
> 
> /usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in
> wrapper(*args, **kwargs)
>      89                 warnings.warn('Update your `' + object_name + '` call to the ' +
>      90                               'Keras 2 API: ' + signature, stacklevel=2)
> ---> 91             return func(*args, **kwargs)
>      92         wrapper._original_function = func
>      93         return wrapper
> 
> /usr/local/lib/python3.6/dist-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) 
> 1416             use_multiprocessing=use_multiprocessing,    1417     
> shuffle=shuffle,
> -> 1418             initial_epoch=initial_epoch)    1419     1420     @interfaces.legacy_generator_methods_support
> 
> /usr/local/lib/python3.6/dist-packages/keras/engine/training_generator.py
> in fit_generator(model, generator, steps_per_epoch, epochs, verbose,
> callbacks, validation_data, validation_steps, class_weight,
> max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
>     215                 outs = model.train_on_batch(x, y,
>     216                                             sample_weight=sample_weight,
> --> 217                                             class_weight=class_weight)
>     218 
>     219                 outs = to_list(outs)
> 
> /usr/local/lib/python3.6/dist-packages/keras/engine/training.py in
> train_on_batch(self, x, y, sample_weight, class_weight)    1215       
> ins = x + y + sample_weights    1216        
> self._make_train_function()
> -> 1217         outputs = self.train_function(ins)    1218         return unpack_singleton(outputs)    1219 
> 
> /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py
> in __call__(self, inputs)    2713                 return
> self._legacy_call(inputs)    2714 
> -> 2715             return self._call(inputs)    2716         else:    2717             if py_any(is_tensor(x) for x in inputs):
> 
> /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py
> in _call(self, inputs)    2673             fetched =
> self._callable_fn(*array_vals, run_metadata=self.run_metadata)    2674
> else:
> -> 2675             fetched = self._callable_fn(*array_vals)    2676         return fetched[:len(self.outputs)]    2677 
> 
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py
> in __call__(self, *args, **kwargs)    1437           ret =
> tf_session.TF_SessionRunCallable(    1438              
> self._session._session, self._handle, args, status,
> -> 1439               run_metadata_ptr)    1440         if run_metadata:    1441           proto_data =
> tf_session.TF_GetBuffer(run_metadata_ptr)
> 
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/errors_impl.py
> in __exit__(self, type_arg, value_arg, traceback_arg)
>     526             None, None,
>     527             compat.as_text(c_api.TF_Message(self.status.status)),
> --> 528             c_api.TF_GetCode(self.status.status))
>     529     # Delete the underlying status object from memory otherwise it stays alive
>     530     # as there is a reference to status from this from the traceback due to
> 
> InvalidArgumentError: You must feed a value for placeholder tensor
> 'input_ref' with dtype float and shape [?,?,?,3]   [[{{node
> input_ref}}]]      [[{{node
> training/Adam/gradients/loss/score_AB_loss/sub_grad/Reshape}}]]

我已经验证了我的发电机,它正在正常工作。尽管如此,我还是尝试将模型的输出作为张量,然后使用keras后端使用K.eval()tf.eval(session=K.get_session())对其进行评估。在这两种情况下,我都得到了以下信息:

> --------------------------------------------------------------------------- InvalidArgumentError                      Traceback (most recent call
> last)
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py
> in _do_call(self, fn, *args)    1333     try:
> -> 1334       return fn(*args)    1335     except errors.OpError as e:
> 
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py
> in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata) 
> 1318       return self._call_tf_sessionrun(
> -> 1319           options, feed_dict, fetch_list, target_list, run_metadata)    1320 
> 
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py
> in _call_tf_sessionrun(self, options, feed_dict, fetch_list,
> target_list, run_metadata)    1406         self._session, options,
> feed_dict, fetch_list, target_list,
> -> 1407         run_metadata)    1408 
> 
> InvalidArgumentError: You must feed a value for placeholder tensor
> 'input_ref' with dtype float and shape [?,?,?,3]   [[{{node
> input_ref}}]]      [[{{node compute_scores/lambda_7/truediv}}]]
> 
> During handling of the above exception, another exception occurred:
> 
> InvalidArgumentError                      Traceback (most recent call
> last) <ipython-input-18-c2f5a14e61a1> in <module>
>       1 import keras.backend as K
>       2 
> ----> 3 K.eval(pred[0])
> 
> /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py
> in eval(x)
>     671     ```
>     672     """
> --> 673     return to_dense(x).eval(session=get_session())
>     674 
>     675 
> 
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py
> in eval(self, feed_dict, session)
>     693 
>     694     """
> --> 695     return _eval_using_default_session(self, feed_dict, self.graph, session)
>     696 
>     697 
> 
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py
> in _eval_using_default_session(tensors, feed_dict, graph, session)   
> 5179                        "the tensor's graph is different from the
> session's "    5180                        "graph.")
> -> 5181   return session.run(tensors, feed_dict)    5182     5183 
> 
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py
> in run(self, fetches, feed_dict, options, run_metadata)
>     927     try:
>     928       result = self._run(None, fetches, feed_dict, options_ptr,
> --> 929                          run_metadata_ptr)
>     930       if run_metadata:
>     931         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
> 
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py
> in _run(self, handle, fetches, feed_dict, options, run_metadata)   
> 1150     if final_fetches or final_targets or (handle and
> feed_dict_tensor):    1151       results = self._do_run(handle,
> final_targets, final_fetches,
> -> 1152                              feed_dict_tensor, options, run_metadata)    1153     else:    1154       results = []
> 
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py
> in _do_run(self, handle, target_list, fetch_list, feed_dict, options,
> run_metadata)    1326     if handle is None:    1327       return
> self._do_call(_run_fn, feeds, fetches, targets, options,
> -> 1328                            run_metadata)    1329     else:    1330       return self._do_call(_prun_fn, handle, feeds, fetches)
> 
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py
> in _do_call(self, fn, *args)    1346           pass    1347      
> message = error_interpolation.interpolate(message, self._graph)
> -> 1348       raise type(e)(node_def, op, message)    1349     1350   def _extend_graph(self):
> 
> InvalidArgumentError: You must feed a value for placeholder tensor
> 'input_ref' with dtype float and shape [?,?,?,3]   [[node input_ref
> (defined at
> /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:517)
> ]]     [[node compute_scores/lambda_7/truediv (defined at
> /workdir/IQA/PIE+keras/model/PieAPPv0pt1_KERAS.py:133) ]]
> 
> Caused by op 'input_ref', defined at:   File
> "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
>     "__main__", mod_spec)   File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
>     exec(code, run_globals)   File "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py", line
> 16, in <module>
>     app.launch_new_instance()   File "/usr/local/lib/python3.6/dist-packages/traitlets/config/application.py",
> line 664, in launch_instance
>     app.start()   File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelapp.py", line
> 563, in start
>     self.io_loop.start()   File "/usr/local/lib/python3.6/dist-packages/tornado/platform/asyncio.py",
> line 148, in start
>     self.asyncio_loop.run_forever()   File "/usr/lib/python3.6/asyncio/base_events.py", line 438, in run_forever
>     self._run_once()   File "/usr/lib/python3.6/asyncio/base_events.py", line 1451, in _run_once
>     handle._run()   File "/usr/lib/python3.6/asyncio/events.py", line 145, in _run
>     self._callback(*self._args)   File "/usr/local/lib/python3.6/dist-packages/tornado/ioloop.py", line 690,
> in <lambda>
>     lambda f: self._run_callback(functools.partial(callback, future))   File "/usr/local/lib/python3.6/dist-packages/tornado/ioloop.py", line
> 743, in _run_callback
>     ret = callback()   File "/usr/local/lib/python3.6/dist-packages/tornado/gen.py", line 787, in
> inner
>     self.run()   File "/usr/local/lib/python3.6/dist-packages/tornado/gen.py", line 748, in
> run
>     yielded = self.gen.send(value)   File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line
> 361, in process_one
>     yield gen.maybe_future(dispatch(*args))   File "/usr/local/lib/python3.6/dist-packages/tornado/gen.py", line 209, in
> wrapper
>     yielded = next(result)   File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line
> 268, in dispatch_shell
>     yield gen.maybe_future(handler(stream, idents, msg))   File "/usr/local/lib/python3.6/dist-packages/tornado/gen.py", line 209, in
> wrapper
>     yielded = next(result)   File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line
> 541, in execute_request
>     user_expressions, allow_stdin,   File "/usr/local/lib/python3.6/dist-packages/tornado/gen.py", line 209, in
> wrapper
>     yielded = next(result)   File "/usr/local/lib/python3.6/dist-packages/ipykernel/ipkernel.py", line
> 300, in do_execute
>     res = shell.run_cell(code, store_history=store_history, silent=silent)   File
> "/usr/local/lib/python3.6/dist-packages/ipykernel/zmqshell.py", line
> 536, in run_cell
>     return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)   File
> "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py",
> line 2848, in run_cell
>     raw_cell, store_history, silent, shell_futures)   File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py",
> line 2874, in _run_cell
>     return runner(coro)   File "/usr/local/lib/python3.6/dist-packages/IPython/core/async_helpers.py",
> line 68, in _pseudo_sync_runner
>     coro.send(None)   File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py",
> line 3051, in run_cell_async
>     interactivity=interactivity, compiler=compiler, result=result)   File
> "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py",
> line 3242, in run_ast_nodes
>     if (await self.run_code(code, result,  async_=asy)):   File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py",
> line 3319, in run_code
>     exec(code_obj, self.user_global_ns, self.user_ns)   File "<ipython-input-10-2df6f0aeb9af>", line 1, in <module>
>     pieModel = PieAPP('pieModel.p') #   File "/workdir/IQA/PIE+keras/model/PieAPPv0pt1_KERAS.py", line 23, in
> __init__
>     self.compute_scores   = self._build_compute_scores()   File "/workdir/IQA/PIE+keras/model/PieAPPv0pt1_KERAS.py", line 82, in
> _build_compute_scores
>     img_ref = L.Input((None,None,3), name='input_ref')   File "/usr/local/lib/python3.6/dist-packages/keras/engine/input_layer.py",
> line 178, in Input
>     input_tensor=tensor)   File "/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py",
> line 91, in wrapper
>     return func(*args, **kwargs)   File "/usr/local/lib/python3.6/dist-packages/keras/engine/input_layer.py",
> line 87, in __init__
>     name=self.name)   File "/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py",
> line 517, in placeholder
>     x = tf.placeholder(dtype, shape=shape, name=name)   File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_ops.py",
> line 2077, in placeholder
>     return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)   File
> "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_array_ops.py",
> line 5791, in placeholder
>     "Placeholder", dtype=dtype, shape=shape, name=name)   File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py",
> line 788, in _apply_op_helper
>     op_def=op_def)   File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/deprecation.py",
> line 507, in new_func
>     return func(*args, **kwargs)   File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py",
> line 3300, in create_op
>     op_def=op_def)   File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py",
> line 1801, in __init__
>     self._traceback = tf_stack.extract_stack()
> 
> InvalidArgumentError (see above for traceback): You must feed a value
> for placeholder tensor 'input_ref' with dtype float and shape
> [?,?,?,3]      [[node input_ref (defined at
> /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:517)
> ]]     [[node compute_scores/lambda_7/truediv (defined at
> /workdir/IQA/PIE+keras/model/PieAPPv0pt1_KERAS.py:133) ]]

所以,我的问题是:我做错了什么?为什么我可以使用model.predict()并得到正确的结果,但是当从模型中捕获输出张量并在K.eval()上对其求值(或尝试训练模型)时,我会得到第一条错误消息


Tags: runinpyselfreturnsessionlibpackages