python - 如何将图像文件数据集加载到 TensorFlow Jupyter Notebook

标签 python tensorflow jupyter-notebook dataset tensor

我正在尝试创建一个模型来对一些植物进行分类,这样我就可以学习如何使用 TensorFlow。问题是每个 good example我可以用作引用的是加载 .csv 数据集,并且我想加载 .jpeg 数据集(可以是 .png.jpg 以及)。

这些示例甚至使用内置数据集,例如:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

我的数据集组织在包含花朵标签的文件夹中,里面有按数字组织的图像。

enter image description here

enter image description here

最佳答案

假设你的文件夹结构如下:

├── testfiles
|   ├── BougainvilleaGlabra
|   |   ├── BougainvilleaGlabra_001.jpeg
|   |   ├── *.jpeg
|   ├── HandroanthusChrysotrichus
|   |   ├── HandroanthusChrysotrichus_001.jpeg
|   |   ├── *.jpeg
|   ├── SpathodeaVenusta
|   |   ├── SpathodeaVenusta_001.jpeg
|   |   ├── *.jpeg
|   ├──TibouchinaMutabilis
|   |   ├── TibouchinaMutabilis_001.jpeg
|   |   ├── *.jpeg
├── test.py

首先您需要获取所有图像路径。

import glob,os

path = 'testfiles/'
files = [f for f in glob.glob(path + "*/*.jpeg", recursive=True)]
print(files)

['testfiles/HandroanthusChrysotrichus/HandroanthusChrysotrichus_002.jpeg', 'testfiles/HandroanthusChrysotrichus/HandroanthusChrysotrichus_001.jpeg', ...]

然后你需要将每个类编码为数字。

label_map = {'BougainvilleaGlabra':0,
             'HandroanthusChrysotrichus':1,
             'SpathodeaVenusta':2,
             'TibouchinaMutabilis':3,}
label = [label_map[os.path.basename(file).split('_')[0]] for file in files]
print(label)

[1, 1, 1, 0, 0, 0, 2, 2, 2, 3, 3, 3]

然后您可以使用tf.data.Dataset。您需要一个函数来读取图像并将它们调整为相同的形状。

import tensorflow as tf
def read_image(filename,label):
    image_string = tf.read_file(filename)
    image_decoded = tf.image.decode_jpeg(image_string)
    image_resized = tf.image.resize_images(image_decoded, [28, 28])
    return image_resized,label

dataset = tf.data.Dataset.from_tensor_slices((files,label))
# you can use batch() to set batch_size
dataset = dataset.map(read_image).shuffle(1000).batch(2)
print(dataset.output_shapes)
print(dataset.output_types)

(TensorShape([Dimension(None), Dimension(28), Dimension(28), Dimension(None)]), TensorShape([Dimension(None)]))
(tf.float32, tf.int32)

最后定义迭代器来获取批量数据。

iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()

with tf.Session() as sess:
    for _ in range(2):
        sess.run(iterator.initializer)
        batch_image,batch_label = sess.run(next_element)
        print(batch_image.shape)
        print(batch_label.shape)

(2, 28, 28, 4)
(2,)
(2, 28, 28, 4)
(2,)

关于python - 如何将图像文件数据集加载到 TensorFlow Jupyter Notebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56130320/

相关文章:

powershell - 如何在 Windows 10 上使用 Powershell 将 Google Colab 与本地 TensorFlow Jupyter 服务器结合使用?

python - 如何使用 TensorFlow 的 sketch RNN 教程对 QuickDraw 涂鸦进行分类?

python - 带有 pandas 和 Jupyter notebook 的交互式箱线图

python - 写入 GML 文件时出现 NetworkX 键错误

python - 在python中部分覆盖父方法

python - 在python中声明空类成员

python - LSTM输入形状错误: Input 0 is incompatible with layer sequential_1

jupyter-notebook - 语句前的感叹号 (!) 有什么作用?

python - 为什么pytorch模型无法识别我定义的张量?

python - 以编程方式启动 python 虚拟机并与之交互