【问题标题】:Changing dataset_url to local filepath in tensorflow在张量流中将 dataset_url 更改为本地文件路径
【发布时间】:2022-03-25 22:12:39
【问题描述】:

我正在学习 tensorflow 分类教程here

在“下载数据集”部分有导入 dataset_url 的代码:

import pathlib
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)

我的 Mac 硬盘上有自己的图像数据集。我该如何更改文件路径?我尝试使用文件路径更新 dataset_url,但它正在创建错误消息。

当我运行以下命令时,我得到一个 0:

image_count = len(list(data_dir.glob('*/*.jpg')))
print(image_count)

我将图像解压缩到单独的文件夹中,但我似乎无法在 python 中正确分配它们。提供的链接似乎不起作用。感谢您的任何建议?

非常感谢。

【问题讨论】:

标签: python tensorflow


【解决方案1】:

定义图像数据集所在的路径,直到主文件夹名称。

train_path='c:/../dataset/flowers' # I have already downloaded this dataset

然后使用“image_dataset_from_directory”从该目录获取图像:

from tensorflow.keras.utils import image_dataset_from_directory
    
data_dir= tf.keras.utils.image_dataset_from_directory(train_path) # insert images directory path here

这将自动检测子文件夹中的图像,每个子文件夹名称将是那些图像类名称(标签)。

flowers/
  daisy/
  dandelion/
  roses/
  sunflowers/
  tulips/

输出:

Found 4317 files belonging to 5 classes.

您可以在此“image_dataset_from_directory” api here 中找到更多详细信息,并可以根据您的要求设置其其他参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 2017-07-03
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    相关资源
    最近更新 更多