You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

训练图像识别模型时遇NumPy数组转Tensor失败问题

问题:调用model.fit()时报错ValueError: Failed to convert a NumPy array to a Tensor

训练图像识别模型时,调用model.fit()始终返回如下错误:

ValueError                                Traceback (most recent call last)
Cell In[106], line 1
----> 1 history = model.fit(x_train, batch_size=batch_size, epochs=epochs)

File c:\Users\rochav3\Anaconda\envs\py_OBJ_DETECTION\lib\site-packages\keras\utils\traceback_utils.py:67, in filter_traceback..error_handler(*args, **kwargs)
65 except Exception as e:  # pylint: disable=broad-except
66   filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67   raise e.with_traceback(filtered_tb) from None
68 finally:
69   del filtered_tb

File c:\Users\rochav3\Anaconda\envs\py_OBJ_DETECTION\lib\site-packages\tensorflow\python\framework\constant_op.py:102, in convert_to_eager_tensor(value, ctx, dtype)
100     dtype = dtypes.as_dtype(dtype).as_datatype_enum
101 ctx.ensure_initialized()
--> 102 return ops.EagerTensor(value, ctx.device_name, dtype)

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

查阅相关话题未解决,转为float32格式也无效。代码流程为从pickle文件读取含PIL图像的数据集,转为灰度图后转成NumPy数组,完整代码如下:

import tensorflow as tf
import cv2
import pickle
import numpy as np
from sklearn.model_selection import train_test_split
import random
import pandas as pd
import PIL
from PIL import ImageOps

with open('C:\Visual Studio\Django\DetectTool\OpenClosed_dataset.pickle', 'rb') as f:
    data = pickle.load(f)

np_data =[]
tam_data = len(data[0])

for i in range(tam_data):
    np_data.append((np.asarray(ImageOps.grayscale(data[0][i])),
    np.asarray(data[1][i])))

ds_Valves = []
for i in range(100):
    ds_Valves.append((np_data[i][0], np_data[i][1])) 

random.shuffle(ds_Valves)

df_Valves = pd.DataFrame(ds_Valves)

df_Valves.rename(columns= {0 : 'Image', 1: 'State'}, inplace= True)

df_X = df_Valves['Image']
df_Y = df_Valves.drop(columns=['Image'])

df_X = np.asarray(df_X).astype('object')
df_Y = np.asarray(df_Y).astype('float32')

x_train, x_test, y_train, y_test = train_test_split(df_X, df_Y, test_size=0.20, random_state=0 )

# 定义模型超参数
batch_size = 32
epochs = 10
learning_rate = 0.001

# 选择模型架构并编译
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(510, 510,3)),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D((2, 2)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax')
])

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
              loss='categorical_crossentropy',
              metrics=['accuracy'])

history = model.fit(x_train, batch_size=batch_size, epochs=epochs)

解决方案

  • 将数据集格式从.PNG改为.JPG
  • 替换PIL为cv2处理图像
  • 在数据集创建阶段直接生成Tensor

问题原因推测

原问题是尝试用内存中的PIL图像创建Tensor,而非从文件读取导致。


内容的提问来源于stack exchange,提问作者Vitor Rocha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 10:38:07