Epoch 1/10 2023-07-22 21:56:00.836220: W tensorflow/core/framework/op_kernel.cc:1807] OP_REQUIRES failed at cast_op.cc:121 : UNIMPLEMENTED: Cast string to int64 is not supported Traceback (most recent call last): File "d:\AI\1.py", line 37, in <module> model.fit(images, labels, epochs=10, validation_split=0.2) File "D:\AI\env\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler raise e.with_traceback(filtered_tb) from None File "D:\AI\env\lib\site-packages\tensorflow\python\eager\execute.py", line 52, in quick_execute tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, tensorflow.python.framework.errors_impl.UnimplementedError: Graph execution error: Detected at node 'sparse_categorical_crossentropy/Cast' defined at (most recent call last): File "d:\AI\1.py", line 37, in <module> model.fit(images, labels, epochs=10, validation_split=0.2) File "D:\AI\env\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler return fn(*args, **kwargs) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1685, in fit tmp_logs = self.train_function(iterator) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1284, in train_function return step_function(self, iterator) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1268, in step_function outputs = model.distribute_strategy.run(run_step, args=(data,)) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1249, in run_step outputs = model.train_step(data) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1051, in train_step loss = self.compute_loss(x, y, y_pred, sample_weight) File "D:\AI\env\lib\site-packages\keras\engine\training.py", line 1109, in compute_loss return self.compiled_loss( File "D:\AI\env\lib\site-packages\keras\engine\compile_utils.py", line 265, in __call__ loss_value = loss_obj(y_t, y_p, sample_weight=sw) File "D:\AI\env\lib\site-packages\keras\losses.py", line 142, in __call__ losses = call_fn(y_true, y_pred) File "D:\AI\env\lib\site-packages\keras\losses.py", line 268, in call return ag_fn(y_true, y_pred, **self._fn_kwargs) File "D:\AI\env\lib\site-packages\keras\losses.py", line 2078, in sparse_categorical_crossentropy return backend.sparse_categorical_crossentropy( File "D:\AI\env\lib\site-packages\keras\backend.py", line 5610, in sparse_categorical_crossentropy target = cast(target, "int64") File "D:\AI\env\lib\site-packages\keras\backend.py", line 2304, in cast return tf.cast(x, dtype) Node: 'sparse_categorical_crossentropy/Cast' Cast string to int64 is not supported [[{{node sparse_categorical_crossentropy/Cast}}]] [Op:__inference_train_function_1010]
时间: 2023-08-05 20:07:14 浏览: 209
从零开始构建VGG项目所需要的训练,测试,预测图片 https://mp.csdn.net/mp-blog/creation/
这个错误是由于标签 `labels` 中的数据类型不正确导致的。`labels` 中的数据应该是整数类型,但是在您的代码中,它们被解析为字符串类型。为了解决这个问题,您需要将标签转换为整数类型。
请尝试在加载数据集时将标签转换为整数类型。修改以下部分的代码:
```python
for file in os.listdir(image_dir):
img_path = os.path.join(image_dir, file)
img = Image.open(img_path).convert('L') # 转换为灰度图像
img = img.resize((100, 100))
img = np.array(img, dtype=np.float32)
images.append(img)
labels.append(file.split('.')[0])
```
修改为:
```python
for file in os.listdir(image_dir):
img_path = os.path.join(image_dir, file)
img = Image.open(img_path).convert('L') # 转换为灰度图像
img = img.resize((100, 100))
img = np.array(img, dtype=np.float32)
images.append(img)
labels.append(int(file.split('.')[0]))
```
这样修改后,将标签转换为整数类型后,应该能够解决 `Cast string to int64 is not supported` 的问题。
阅读全文