首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试执行model.fit() -时出现ValueError :无法将NumPy数组转换为张量(不支持的对象类型numpy.ndarray)

尝试执行model.fit() -时出现ValueError :无法将NumPy数组转换为张量(不支持的对象类型numpy.ndarray)
EN

Stack Overflow用户
提问于 2020-11-24 07:39:02
回答 1查看 464关注 0票数 1

我正在尝试训练用于边界框回归的网络。我创建了如下所示的pd.DataFrame:

以下是我的训练和验证图像生成器:

代码语言:javascript
运行
复制
image_generator = tf.keras.preprocessing.image.ImageDataGenerator(
                                  rescale = 1./255,
                                  rotation_range = 25,
                                  zoom_range=[0.8, 1.2],
                                  vertical_flip=True,
                                  horizontal_flip=True,
                                  )
train_generator = image_generator.flow_from_dataframe(
            dataframe=train_df,
            directory=cbis_ddsm_train_images_dir,
            x_col="image file path",
            y_col="coordinates",
            class_mode="raw",
            batch_size=BATCH_SIZE,
            shuffle=True,
            seed=1,
            color_mode="grayscale",
            target_size=(1024, 1024))

val_gen = tf.keras.preprocessing.image.ImageDataGenerator(
                                  rescale = 1./255,
                                  )

val_generator = val_gen.flow_from_dataframe(
            dataframe=val_df,
            directory=cbis_ddsm_train_images_dir,
            x_col="image file path",
            y_col=None,
            class_mode="raw",
            batch_size=BATCH_SIZE,
            shuffle=False,
            seed=1,
            color_mode="grayscale",
             target_size=(1024, 1024))

请注意,我已经将Y列(即bbox坐标)从列表转换为numpy维数组,如下所示:

代码语言:javascript
运行
复制
for idx, row in train_df.iterrows():
  height, width = row['size']
  row['coordinates'] = np.asarray([normalize_bbox(c, height, width) for c in row['coordinates']][0]).astype('float32')

当我尝试执行下面的代码时:

代码语言:javascript
运行
复制
opt = Adam(lr=INIT_LR)
final_model.compile(optimizer=opt, loss="mse")
    
final_model.fit(train_generator, steps_per_epoch=steps_per_epoch, epochs=3,
                    validation_data=val_generator, validation_steps=val_steps, verbose=1)

我收到以下错误消息:

代码语言:javascript
运行
复制
ValueError                                Traceback (most recent call last)
<ipython-input-46-90f3a1cd9c87> in <module>()
      8 
      9     final_model.fit(train_generator, steps_per_epoch=steps_per_epoch, epochs=3,
---> 10                     validation_data=val_generator, validation_steps=val_steps, verbose=1)
     11     final_model.save(os.path.join(experiment1_dir, "resnet_fine-tuned-head.h5"))
     12 

14 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
    106   def _method_wrapper(self, *args, **kwargs):
    107     if not self._in_multi_worker_mode():  # pylint: disable=protected-access
--> 108       return method(self, *args, **kwargs)
    109 
    110     # Running inside `run_distribute_coordinator` already.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
   1061           use_multiprocessing=use_multiprocessing,
   1062           model=self,
-> 1063           steps_per_execution=self._steps_per_execution)
   1064 
   1065       # Container that configures and calls `tf.keras.Callback`s.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution)
   1115         use_multiprocessing=use_multiprocessing,
   1116         distribution_strategy=ds_context.get_strategy(),
-> 1117         model=model)
   1118 
   1119     strategy = ds_context.get_strategy()

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, shuffle, workers, use_multiprocessing, max_queue_size, model, **kwargs)
    914         max_queue_size=max_queue_size,
    915         model=model,
--> 916         **kwargs)
    917 
    918   @staticmethod

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, workers, use_multiprocessing, max_queue_size, model, **kwargs)
    786     peek, x = self._peek_and_restore(x)
    787     peek = self._standardize_batch(peek)
--> 788     peek = _process_tensorlike(peek)
    789 
    790     # Need to build the Model on concrete input shapes.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in _process_tensorlike(inputs)
   1019     return x
   1020 
-> 1021   inputs = nest.map_structure(_convert_numpy_and_scipy, inputs)
   1022   return nest.list_to_tuple(inputs)
   1023 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in map_structure(func, *structure, **kwargs)
    633 
    634   return pack_sequence_as(
--> 635       structure[0], [func(*x) for x in entries],
    636       expand_composites=expand_composites)
    637 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in <listcomp>(.0)
    633 
    634   return pack_sequence_as(
--> 635       structure[0], [func(*x) for x in entries],
    636       expand_composites=expand_composites)
    637 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in _convert_numpy_and_scipy(x)
   1014       if issubclass(x.dtype.type, np.floating):
   1015         dtype = backend.floatx()
-> 1016       return ops.convert_to_tensor(x, dtype=dtype)
   1017     elif scipy_sparse and scipy_sparse.issparse(x):
   1018       return _scipy_sparse_to_sparse_tensor(x)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
   1497 
   1498     if ret is None:
-> 1499       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
   1500 
   1501     if ret is NotImplemented:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_conversion_registry.py in _default_conversion_function(***failed resolving arguments***)
     50 def _default_conversion_function(value, dtype, name, as_ref):
     51   del as_ref  # Unused.
---> 52   return constant_op.constant(value, dtype, name=name)
     53 
     54 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name)
    262   """
    263   return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 264                         allow_broadcast=True)
    265 
    266 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
    273       with trace.Trace("tf.constant"):
    274         return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
--> 275     return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
    276 
    277   g = ops.get_default_graph()

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
    298 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
    299   """Implementation of eager constant."""
--> 300   t = convert_to_eager_tensor(value, ctx, dtype)
    301   if shape is None:
    302     return t

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
     96       dtype = dtypes.as_dtype(dtype).as_datatype_enum
     97   ctx.ensure_initialized()
---> 98   return ops.EagerTensor(value, ctx.device_name, dtype)
     99 
    100 

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

任何形式的帮助都将不胜感激。谢谢!

EN

Stack Overflow用户

回答已采纳

发布于 2020-12-18 22:52:04

这是Keras中的一个错误,请在此处报告:https://github.com/keras-team/keras/issues/13839

基本上,当class_mode == "raw"和标签是numpy数组时,flow_from_dataframe会以numpy数组而不是2D数组的形式为标签生成批,这会导致fit方法失败。

作为一种解决方法,直到修复它,在创建生成器之后添加以下行

代码语言:javascript
运行
复制
train_generator._targets = np.stack(train_generator._targets)
val_generator._targets = np.stack(val_generator._targets)
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64978209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档