tensorFlow:加载自定义本地图片作为数据集训练GAN

废话不多说 直接上代码

data_path = pathlib.Path("./tupian")  #这里是你要加载的图片文件夹路径、每个文件夹下分各个标签的文件
all_image_paths = list(data_path.glob('*/*'))
all_image_paths = [str(path) for path in all_image_paths]  # 所有图片路径的列表
random.shuffle(all_image_paths)  # 打散
image_count = len(all_image_paths)
ds = tf.data.Dataset.from_tensor_slices((all_image_paths))
##print(ds)
def load_and_preprocess_from_path_label(path):#你也可以加标签
    image = tf.io.read_file(path)  # 读取图片
    image = tf.image.decode_jpeg(image, channels=3)
    image = tf.image.resize(image, [192, 192])  # 原始图片大小为(266, 320, 3),重设为(192, 192)
    image /= 255.0  # 归一化到[0,1]范围

    return image
image_label_ds  = ds.map(load_and_preprocess_from_path_label)
ds = image_label_ds.apply(tf.data.experimental.shuffle_and_repeat(buffer_size=image_count))
BATCH_SIZE = 32
ds = ds.batch(BATCH_SIZE)
def change_range(image):
    return 2*image-1

keras_ds = ds.map(change_range)#这儿就是已经成数据集了的
#print(keras_ds)


#这儿是加载到模型中
# Load the dataset
#将这一句
(X_train, _), (_, _) = mnist.load_data()
#用下面这句替换
X_train = next(iter(keras_ds))

然后就完事了

你可能感兴趣的:(python,深度学习,tensorFlow,tensorflow,深度学习)