未实现错误:尽管未传递字符串,但keras中不支持将字符串转换为浮点

noj0wjuj  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(191)

我在让我的模型使用Keras Tuner训练时遇到了麻烦。我相信问题出在MyGenerator(Sequence)类上,但由于我在不使用它时内存不足,我无法轻松验证这是问题所在。
由于代码相当长,可以通过以下链接在Google Colab中访问:https://colab.research.google.com/drive/1xDaX3Eu9Yuax6KgNrNCfgrTYvMONdm4d?usp=sharing
我知道这不是最佳的,但我不知道我是否可以使它容易地访问,否则。如果你有任何提示,我可以添加到线程,我绝对会尝试这样做!
从本节中可以看出,我试图强制生成器返回一个仅包含整数的向量,但由于类的性质以及__getitem__(self, idx):方法只能从tuner.search()中调用,我自己无法完全理解它的工作原理,尽管我试图从文档中了解它。
如果错误消息是没有代码的任何帮助,它可以看到下面,但是我还没有能够理解它从哪里出错。

Search: Running Trial #1

Value             |Best Value So Far |Hyperparameter
256               |?                 |conv_1_filter
3                 |?                 |conv_1_kernel
0.4               |?                 |dropout_1_rate
224               |?                 |dense_1_units
0.01              |?                 |learning_rate

Model: "ALL_Classification_Model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_1 (InputLayer)        [(None, 450, 450, 3)]     0         
                                                                 
 efficientnetv2-b3 (Function  (None, 15, 15, 1536)     12930622  
 al)                                                             
                                                                 
 conv2d (Conv2D)             (None, 15, 15, 256)       3539200   
                                                                 
 batch_normalization (BatchN  (None, 15, 15, 256)      1024      
 ormalization)                                                   
                                                                 
 conv2d_1 (Conv2D)           (None, 15, 15, 256)       590080    
                                                                 
 dropout (Dropout)           (None, 15, 15, 256)       0         
                                                                 
 dense (Dense)               (None, 15, 15, 224)       57568     
                                                                 
 dense_1 (Dense)             (None, 15, 15, 2)         450       
                                                                 
=================================================================
Total params: 17,118,944
Trainable params: 4,187,810
Non-trainable params: 12,931,134
_________________________________________________________________
Epoch 1/6
WARNING:tensorflow:Model was constructed with shape (None, 450, 450, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 450, 450, 3), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'"), but it was called on an input with incompatible shape (None,).
WARNING:tensorflow:Model was constructed with shape (None, 450, 450, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 450, 450, 3), dtype=tf.float32, name='input_2'), name='input_2', description="created by layer 'input_2'"), but it was called on an input with incompatible shape (None,).
WARNING:tensorflow:Model was constructed with shape (None, 450, 450, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 450, 450, 3), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'"), but it was called on an input with incompatible shape (None,).
WARNING:tensorflow:Model was constructed with shape (None, 450, 450, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 450, 450, 3), dtype=tf.float32, name='input_2'), name='input_2', description="created by layer 'input_2'"), but it was called on an input with incompatible shape (None,).
---------------------------------------------------------------------------
UnimplementedError                        Traceback (most recent call last)
<ipython-input-89-340efb4dcfaf> in <module>
      5 
      6 # Note we do not need to_catagorical as we use loss='sparse_categorical_crossentropy'
----> 7 tuner.search(train_gen, 
      8              epochs=noepochauto,
      9              validation_data=val_gen,

6 frames
/usr/local/lib/python3.8/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     52   try:
     53     ctx.ensure_initialized()
---> 54     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     55                                         inputs, attrs, num_outputs)
     56   except core._NotOkStatusException as e:

UnimplementedError: Graph execution error:

Detected at node 'ALL_Classification_Model/Cast' defined at (most recent call last):
    File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
      exec(code, run_globals)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel_launcher.py", line 16, in <module>
      app.launch_new_instance()
    File "/usr/local/lib/python3.8/dist-packages/traitlets/config/application.py", line 992, in launch_instance
      app.start()
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelapp.py", line 612, in start
      self.io_loop.start()
    File "/usr/local/lib/python3.8/dist-packages/tornado/platform/asyncio.py", line 149, in start
      self.asyncio_loop.run_forever()
    File "/usr/lib/python3.8/asyncio/base_events.py", line 570, in run_forever
      self._run_once()
    File "/usr/lib/python3.8/asyncio/base_events.py", line 1859, in _run_once
      handle._run()
    File "/usr/lib/python3.8/asyncio/events.py", line 81, in _run
      self._context.run(self._callback, *self._args)
    File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 690, in <lambda>
      lambda f: self._run_callback(functools.partial(callback, future))
    File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 743, in _run_callback
      ret = callback()
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 787, in inner
      self.run()
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 748, in run
      yielded = self.gen.send(value)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 365, in process_one
      yield gen.maybe_future(dispatch(*args))
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
      yielded = next(result)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell
      yield gen.maybe_future(handler(stream, idents, msg))
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
      yielded = next(result)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 543, in execute_request
      self.do_execute(
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 209, in wrapper
      yielded = next(result)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/ipkernel.py", line 306, in do_execute
      res = shell.run_cell(code, store_history=store_history, silent=silent)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/zmqshell.py", line 536, in run_cell
      return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2854, in run_cell
      result = self._run_cell(
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 2881, in _run_cell
      return runner(coro)
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
      coro.send(None)
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3057, in run_cell_async
      has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3249, in run_ast_nodes
      if (await self.run_code(code, result,  async_=asy)):
    File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3326, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "<ipython-input-89-340efb4dcfaf>", line 7, in <module>
      tuner.search(train_gen,
    File "/usr/local/lib/python3.8/dist-packages/keras_tuner/engine/base_tuner.py", line 183, in search
      results = self.run_trial(trial, *fit_args, **fit_kwargs)
    File "<ipython-input-84-c1c2544d1484>", line 9, in run_trial
      return super(MyTuner, self).run_trial(trial, *args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras_tuner/engine/tuner.py", line 295, in run_trial
      obj_value = self._build_and_fit_model(trial, *args, **copied_kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras_tuner/engine/tuner.py", line 221, in _build_and_fit_model
      model = self._try_build(hp)
    File "/usr/local/lib/python3.8/dist-packages/keras_tuner/engine/hypermodel.py", line 140, in fit
      return model.fit(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1409, in fit
      tmp_logs = self.train_function(iterator)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1051, in train_function
      return step_function(self, iterator)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1040, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1030, in run_step
      outputs = model.train_step(data)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 889, in train_step
      y_pred = self(x, training=True)
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 490, in __call__
      return super().__call__(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/base_layer.py", line 1014, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 92, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/functional.py", line 458, in call
      return self._run_internal_graph(
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/functional.py", line 578, in _run_internal_graph
      y = self._conform_to_reference_input(y, ref_input=x)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/functional.py", line 678, in _conform_to_reference_input
      tensor = tf.cast(tensor, dtype=ref_input.dtype)
Node: 'ALL_Classification_Model/Cast'
Cast string to float is not supported
     [[{{node ALL_Classification_Model/Cast}}]] [Op:__inference_train_function_77977]

在我的分类器中使用Flatten()-layers时也遇到了很多问题,但这可能需要它自己的线程...

lymgl2op

lymgl2op1#

事实证明,在我的例子中,问题是序列生成器有一个小的错别字,它返回图像的字符串位置,而不是加载图像的数组。

相关问题