在TensorFlow / Keras中加载自定义数据集

时间:2019-05-12 23:25:29

标签: python tensorflow

所以在这段代码中,作者使用的是MNIST数据集,我想使用我自己的包含图像的数据集。我不知道如何在这里给自己的数据集提供路径?

(train_images, _), (test_images, _) = tf.keras.datasets.mnist.load_data()



# Load the dataset
(train_images, _), (test_images, _) = tf.keras.datasets.mnist.load_data()
train_images = train_images.reshape(train_images.shape[0], 
                                            config.raw_size,
                                            config.raw_size,
                                            config.channels)
# Add noise for condition input
train_inputs = artefacts.add_gaussian_noise(train_images, stdev=0.2, data_range=(0, 255)).astype('float32')
train_inputs = data_processing.normalise(train_inputs, (-1, 1), (0, 255))
train_images = data_processing.normalise(train_images, (-1, 1), (0, 255))
train_labels = train_images.astype('float32')

train_dataset = tf.data.Dataset.from_tensor_slices((train_inputs, train_labels))\
            .shuffle(config.buffer_size).batch(config.batch_size)

# Test set
test_images = test_images.reshape(test_images.shape[0], 
                                        config.raw_size,
                                        config.raw_size,
                                        config.channels)
test_inputs = artefacts.add_gaussian_noise(test_images, stdev=0.2, data_range=(0, 255)).astype('float32')
test_inputs = data_processing.normalise(test_inputs, (-1, 1), (0, 255))
test_images = data_processing.normalise(test_images, (-1, 1), (0, 255))
test_labels = test_imag

es.astype('float32')

1 个答案:

答案 0 :(得分:0)

如果在类型为foldername的名为.jpeg的文件夹中有图像数据集,以下解决方案是否可以满足您的需求?

import glob

import numpy as np
from matplotlib.pyplot import imread

foldername = "YOUR FOLDER NAME"

# load in the images as a numpy array of shape (number images x width x height x channels)
image_array = np.array([imread(im) for im in glob.glob(f"{foldername}/*.jpeg")])