keras 使用CPU时图形执行错误,在GPU中使用相同的代码一切正常

siv3szwd  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(91)

我在Tensorflow上运行我的模型。输入数据的格式为:(#samples,1,19,512),这意味着我的输入数据格式是通道优先样式。我使用下面的代码将默认通道位置从第一个更改为最后一个:

from tensorflow.keras import backend as K
K.set_image_data_format('channels_first')

字符串
此外,基于通道优先类型来形成模型架构。您可以在下面找到我的模型的架构代码:

def TactileNet(nb_classes=2, Chans = 19, Samples = 512, kernLength = 16, F2 = 64,
               F1 = 64, D=4, dropoutRate = 0.5,dropoutType = 'Dropout', norm_rate = 0.25,
               Dense_nodes = 16, optimizer_type = 'Adam', lr=0.001, **kwargs):

  if dropoutType == 'SpatialDropout2D':
      dropoutType = SpatialDropout2D
  elif dropoutType == 'Dropout':
      dropoutType = Dropout
  else:
      raise ValueError('dropoutType must be one of SpatialDropout2D '
                        'or Dropout, passed as a string.')
  #EEGNet alike part
  input1       = Input(shape = (1, Chans, Samples))
  block1       = Conv2D(F1, (1, kernLength), padding = 'same',
                                  input_shape = (1, Chans, Samples),
                                  use_bias = False)(input1)
  block1       = BatchNormalization(axis = 1 ,trainable = True)(block1)
  block1       = DepthwiseConv2D((Chans, 1), use_bias = False, 
                                  depth_multiplier = D,
                                  depthwise_constraint = max_norm(norm_rate))(block1)
  block1       = BatchNormalization( axis = 1, trainable = True)(block1)
  block1       = Activation('elu')(block1)
  block1       = AveragePooling2D((1, 2))(block1)
  block1       = dropoutType(dropoutRate)(block1)

  ###############################################
  #first tower
  sub_block1   = Conv2D(64, (1, 1), padding = 'same',use_bias = False)(block1)
  sub_block1   = SeparableConv2D(128, (1, 128), padding = 'same',use_bias = False)(sub_block1)
  sub_block1   = AveragePooling2D((1, 2), padding = 'same')(sub_block1)
  #second tower
  sub_block2   = Conv2D(16, (1, 1), padding = 'same',use_bias = False)(block1)
  sub_block2   = SeparableConv2D(32, (1, 256), padding = 'same',use_bias = False)(sub_block2)
  sub_block2  = AveragePooling2D((1, 2), padding = 'same')(sub_block2)
  #third tower
  sub_block3   = Conv2D(64, (1, 1), padding = 'same', strides=(1,2), use_bias = False)(block1)
  #forth tower
  sub_block4   = AveragePooling2D((1, 2), padding = 'same')(block1)
  sub_block4   = Conv2D(32, (1, 1), padding = 'same',use_bias = False)(sub_block4)
  #concatenation
  concat       = concatenate([sub_block1, sub_block2, sub_block4, sub_block3],axis=1)
  
  #last tower
  block2       = BatchNormalization( axis = 1, trainable = True)(concat)
  block2       = Activation('elu')(block2)
  #SENEt block
  squeeze1     = GlobalAveragePooling2D()(block2)
  excitation1  = Dense(Dense_nodes, activation='relu')(squeeze1)
  excitation1  = Dense(256, activation='sigmoid')(excitation1)
  block2       = Permute(dims=(2,3,1))(block2)
  excitation1  = multiply([block2, excitation1])
  excitation1  = Permute(dims=(3,1,2))(excitation1)

  block2       = SeparableConv2D(256, (1, 64), padding = 'same',use_bias = False)(excitation1)
  block2       = BatchNormalization( axis = 1, trainable = True)(block2)
  block2       = Activation('elu')(block2)
  #SENEt block
  squeeze2     = GlobalAveragePooling2D()(block2)
  excitation2  = Dense(Dense_nodes, activation='relu')(squeeze2)
  excitation2  = Dense(256, activation='sigmoid')(excitation2)
  block2       = Permute(dims=(2,3,1))(block2)
  excitation2  = multiply([block2, excitation2])
  excitation2  = Permute(dims=(3,1,2))(excitation2)

  block2       = dropoutType(dropoutRate)(excitation2)

  GB           = GlobalAveragePooling2D()(block2)
  dense        = Dense(nb_classes, name = 'dense',kernel_constraint = max_norm(norm_rate))(GB)
  softmax      = Activation('softmax', name = 'softmax')(dense)
  if optimizer_type == 'Adam':
    optimizer = Adam(learning_rate = lr)
  if optimizer_type == 'Adamax':
    optimizer = Adamax(learning_rate = lr)
  if optimizer_type == 'AdamW':
    optimizer = AdamW(learning_rate = lr)
  model       = Model(inputs=input1, outputs=softmax)
  model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics = ['accuracy'])
  return model


当我与Google Colab一起使用GPU后端时,训练过程运行得很好。然而,在使用CPU时,该进程失败,我收到了以下错误:

InvalidArgumentError: Graph execution error:

Detected at node 'model_4/average_pooling2d_16/AvgPool' 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 215, 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 687, in <lambda>
      lambda f: self._run_callback(functools.partial(callback, future))
    File "/usr/local/lib/python3.8/dist-packages/tornado/ioloop.py", line 740, in _run_callback
      ret = callback()
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 821, in inner
      self.ctx_run(self.run)
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 782, in run
      yielded = self.gen.send(value)
    File "/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py", line 381, in dispatch_queue
      yield self.process_one()
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 250, in wrapper
      runner = Runner(ctx_run, result, future, yielded)
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 748, in __init__
      self.ctx_run(self.run)
    File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 782, 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 234, in wrapper
      yielded = ctx_run(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 234, in wrapper
      yielded = ctx_run(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 234, in wrapper
      yielded = ctx_run(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-13-85dbe5ff1517>", line 5, in <module>
      _, test_acc = members[i].evaluate(X_test, Y_test, verbose=0)
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 2040, in evaluate
      tmp_logs = self.test_function(iterator)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1820, in test_function
      return step_function(self, iterator)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1804, 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 1792, in run_step
      outputs = model.test_step(data)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1756, in test_step
      y_pred = self(x, training=False)
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 561, in __call__
      return super().__call__(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/base_layer.py", line 1132, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 96, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/functional.py", line 511, in call
      return self._run_internal_graph(inputs, training=training, mask=mask)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/functional.py", line 668, in _run_internal_graph
      outputs = node.layer(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/engine/base_layer.py", line 1132, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 96, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.8/dist-packages/keras/layers/pooling/base_pooling2d.py", line 84, in call
      outputs = self.pool_function(
Node: 'model_4/average_pooling2d_16/AvgPool'
Default AvgPoolingOp only supports NHWC on device type CPU
 [[{{node model_4/average_pooling2d_16/AvgPool}}]] [Op:__inference_test_function_19677]


我需要部署该模型,因此,如果您能在解决此问题方面提供更多帮助,我们将不胜感激。

anauzrmj

anauzrmj1#

此错误是因为CPU上的TensorFlow当前不支持Conv2D中的data_format channels_first。虽然Conv2D层接受channels_last(默认)或channels_first data_format,但channels_first data_format仅与tensorflow GPU兼容。
请参考Conv2D参数中的data_format定义。

相关问题