import pathlib
import warnings
import matplotlib.pyplot as plt
import tensorflow as tf
# import keras
# 设置GPU
gpus = tf.config.list_physical_devices("GPU")
if gpus:
gpu0 = gpus[0] # 如果有多个GPU,仅使用第0个GPU
tf.config.experimental.set_memory_growth(gpu0, True) # 设置GPU显存用量按需使用
tf.config.set_visible_devices([gpu0], "GPU")
warnings.filterwarnings("ignore") # 忽略警告信息
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
# gpus
# "./dataset/"
# 加载处理数据
data_dir = "C:/modelProject/dataset"
data_dir = pathlib.Path(data_dir)
image_count = len(list(data_dir.glob('*/*')))
print("图片总数为:", image_count)
batch_size = 16
img_height = 224
img_width = 224
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
labels='inferred',
validation_split=0.2,
subset="training",
seed=12,
image_size=(img_height, img_width),
batch_size=batch_size)
val_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=12,
image_size=(img_height, img_width),
batch_size=batch_size)
class_names = train_ds.class_names
print("数据类别有:", class_names)
出现了以下错误:
Traceback (most recent call last):
File “C:\modelProject\model.py”, line 33, in
train_ds = tf.keras.utils.image_dataset_from_directory(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\Python311\Lib\site-packages\keras\utils\image_dataset.py”, line 210, in image_dataset_from_directory
image_paths, labels, class_names = dataset_utils.index_directory(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\Python311\Lib\site-packages\keras\utils\dataset_utils.py”, line 543, in index_directory
if tf.io.gfile.isdir(tf.io.gfile.join(directory, subdir)):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “D:\Python311\Lib\site-packages\tensorflow\python\lib\io\file_io.py”, line 703, in is_directory_v2
return _pywrap_file_io.IsDirectory(compat.path_to_bytes(path))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0x9c in position 30: invalid start byte
请问有人了解怎么解决吗