如何使用Tensorflow将花卉数据集下载到环境中?
花卉数据集可以使用一个Google API下载,该API基本上链接到花卉数据集。“get_file”方法可以用来传递API作为参数。完成此操作后,数据将下载到环境中。
阅读更多: 什么是TensorFlow以及Keras如何与TensorFlow一起创建神经网络?
我们将使用花卉数据集,其中包含数千朵花的图像。它包含5个子目录,每个类别都有一个子目录。
我们使用Google Colaboratory来运行以下代码。Google Colab或Colaboratory帮助通过浏览器运行Python代码,无需任何配置,并且可以免费访问GPU(图形处理单元)。Colaboratory构建在Jupyter Notebook之上。
import numpy as np
import os
import PIL
import PIL.Image
import tensorflow as tf
import tensorflow_datasets as tfds
print("The Tensorflow version is :")
print(tf.__version__)
import pathlib
print("The dataset is being downloaded")
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file(origin=dataset_url,
fname='flower_photos',
untar=True)
data_dir = pathlib.Path(data_dir)
print("The number of images in the dataset")
image_count = len(list(data_dir.glob('*/*.jpg')))
print(image_count)
print("Sample data in the dataset")
roses = list(data_dir.glob('roses/*'))
PIL.Image.open(str(roses[2]))
print("Some more sample data from the dataset")
roses = list(data_dir.glob('roses/*'))
PIL.Image.open(str(roses[4]))代码来源: https://tensorflowcn.cn/tutorials/load_data/images
输出
The Tensorflow version is : 2.4.0 The dataset is being downloaded Downloading data from https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz 228818944/228813984 [==============================] - 4s 0us/step The number of images in the dataset 3670 Sample data in the dataset Some more sample data from the dataset

解释
- 下载所需软件包。
- 在控制台上显示Tensorflow的版本。
- 使用API下载数据。
- 在控制台上还显示图像示例。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP