如何使用TensorFlow加载花卉数据集并进行处理?
我们将使用花卉数据集,其中包含数千张花卉图像。它包含5个子目录,每个类别都有一个子目录。
阅读更多: 什么是TensorFlow以及Keras如何与TensorFlow一起创建神经网络?
使用‘get_file’方法下载花卉数据集后,它将被加载到环境中以进行处理。加载参数已明确说明,加载的数据被分成训练集和验证集。
我们使用Google Colaboratory运行以下代码。Google Colab或Colaboratory帮助在浏览器上运行Python代码,无需任何配置,并且可以免费访问GPU(图形处理单元)。Colaboratory构建在Jupyter Notebook之上。
print("Loading parameters for the loader") batch_size = 32 img_height = 180 img_width = 180 print("Preprocessing the image dataset using Keras") print("Splitting dataset into training and validation set ") train_ds = tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split=0.2, subset="training", seed=123, image_size=(img_height, img_width), batch_size=batch_size) print("Splitting dataset into training and validation set ") val_ds = tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split=0.2, subset="validation", seed=123, image_size=(img_height, img_width), batch_size=batch_size) print("Printing the class names present in sub-directories") class_names = train_ds.class_names print(class_names)
代码来源:https://tensorflowcn.cn/tutorials/load_data/images
输出
Loading parameters for the loader Preprocessing the image dataset using Keras Splitting dataset into training and validation set Found 3670 files belonging to 5 classes. Using 2936 files for training. Splitting dataset into training and validation set Found 3670 files belonging to 5 classes. Using 734 files for validation. Printing the class names present in sub-directories ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
解释
- 参数已定义。
- 数据集被分成训练集和验证集。
- 每张图像分类的类别名称将显示在控制台上。
广告