在Google Colab中用TensorFlow部署YOLOv8时遇文件找不到错误求助
YOLOv8自定义数据集训练(TensorFlow/Colab):文件路径不存在错误修复
问题背景
在Google Colab基于TensorFlow实现YOLOv8自定义数据集训练时,已完成数据集划分、图像与标注加载预处理,但执行可视化代码时,提示image_0329等文件不存在,而这些文件实际存在于Google Drive的images文件夹中。
已执行代码
数据集划分
num_val = int(len(xml_files) * SPLIT) # 原代码缺少闭合括号,需补充 # Split the dataset into train and validation sets val_data = data.take(num_val) train_data = data.skip(num_val)
数据加载与预处理
def load_image(image_paths): image = tf.io.read_file(image_paths) image = tf.image.decode_jpeg(image, channels=3) return image def load_dataset(image_paths, classes, bbox): # Read Image image = load_image(image_paths) bounding_boxes = { "classes": tf.cast(classes, dtype=tf.float32), "boxes": bbox, } return {"images": tf.cast(image, tf.float32), "bounding_boxes": bounding_boxes} augmenter = keras.Sequential( layers=[ keras_cv.layers.RandomSharpness(1, [0,255]), keras_cv.layers.AutoContrast([0,255]), keras_cv.layers.JitteredResize( target_size=(640, 640), scale_factor=(0.75, 1.3), bounding_box_format="xyxy" ), ] ) train_ds = train_data.map(load_dataset, num_parallel_calls=tf.data.AUTOTUNE) train_ds = train_ds.shuffle(BATCH_SIZE * 4) train_ds = train_ds.ragged_batch(BATCH_SIZE, drop_remainder=True) train_ds = train_ds.map(augmenter, num_parallel_calls=tf.data.AUTOTUNE) val_ds = val_data.map(load_dataset, num_parallel_calls=tf.data.AUTOTUNE) val_ds = val_ds.shuffle(BATCH_SIZE * 4) val_ds = val_ds.ragged_batch(BATCH_SIZE, drop_remainder=True) val_ds = val_ds.map(augmenter, num_parallel_calls=tf.data.AUTOTUNE)
可视化代码
def visualize_dataset(inputs, value_range, rows, cols, bounding_box_format): inputs = next(iter(inputs.take(1))) images, bounding_boxes = inputs["images"], inputs["bounding_boxes"] visualization.plot_bounding_box_gallery( images, value_range=value_range, rows=rows, cols=cols, y_true=bounding_boxes, scale=5, font_scale=0.7, bounding_box_format=bounding_box_format, class_mapping=class_mapping, ) visualize_dataset( train_ds, bounding_box_format="xyxy", value_range=(0, 255), rows=2, cols=2 ) visualize_dataset( val_ds, bounding_box_format="xyxy", value_range=(0, 255), rows=2, cols=2 )
报错信息
Error: {{function_node __wrapped__IteratorGetNext_output_types_3_device_/job:localhost/replica:0/task:0/device:CPU:0}} /content/drive/MyDrive/images/images/image_0329; No such file or directory [[{{node ReadFile}}]] [Op:IteratorGetNext] name: Error: {{function_node __wrapped__IteratorGetNext_output_types_3_device_/job:localhost/replica:0/task:0/device:CPU:0}} /content/drive/MyDrive/images/images/image_0001; No such file or directory [[{{node ReadFile}}]] [Op:IteratorGetNext] name:
问题分析
从报错路径/content/drive/MyDrive/images/images/image_0329可以看出,路径中重复出现了images目录,这是核心问题:代码生成图像路径时错误地拼接了两次images目录,导致实际访问的路径不存在,而真实文件路径应为/content/drive/MyDrive/images/image_0329(需确认文件后缀是否正确)。
解决方案
1. 修正图像路径生成逻辑
检查你构建image_paths的代码(未在提供的代码中展示,是问题根源):
- 确认图像实际存储路径:比如文件存放在
/content/drive/MyDrive/images/下 - 避免重复拼接
images目录:如果原本代码写了类似os.path.join(IMAGE_DIR, "images", filename),改成os.path.join(IMAGE_DIR, filename)(其中IMAGE_DIR已指向/content/drive/MyDrive/images/)
2. 调试验证路径正确性
在load_image函数中添加路径打印,确认传入的路径是否正确:
def load_image(image_paths): # 打印路径用于调试 tf.print("Current image path:", image_paths) image = tf.io.read_file(image_paths) image = tf.image.decode_jpeg(image, channels=3) return image
运行代码后查看打印的路径,确认是否存在重复的images目录,针对性修正。
3. 检查文件后缀
额外确认路径中是否包含正确的文件后缀(如.jpg/.png),如果xml标注中只记录了文件名而没有后缀,需要在生成image_paths时补充后缀,比如:
image_path = os.path.join(IMAGE_DIR, f"{filename}.jpg")
4. 确认Drive挂载正确性
确保Google Drive已正确挂载到/content/drive/MyDrive/,可以通过以下命令验证:
!ls /content/drive/MyDrive/images/
如果能看到目标图像文件,说明挂载正常;否则重新执行挂载代码:
from google.colab import drive drive.mount('/content/drive')
验证步骤
修正路径后,重新运行数据集加载和可视化代码,若不再出现文件找不到错误,且能正常显示带标注的图像,则问题解决。
内容的提问来源于stack exchange,提问作者KHALID IJAZ
相关产品推荐
相关产品推荐

