从Keras中的.jpgs创建规范化数据集



我目前正在学习使用TensorFlow/Keras,并且在将图像加载为数据集时遇到了一些问题。

就上下文而言,我已经从Kaggle下载了Pizza/Not Pizza数据集,我只想构建一个简单的二元分类模型。

从Keras文档中,我应该使用image_dataset_From_directory函数,但存在问题。它要求我给图像一个大小作为函数的参数,但这会混淆数据集。我已经注意到DS中的图像要么是512 x 384,要么是384 x 512,所以我想做的就是加载一千个图像,对它们应用转置,最后将所有图像转换为张量。

所以,我的问题是:如何从目录中加载图像,而不事先强加特定的大小/形状?

您可以预先旋转所有具有第一个形状384的图像(或者反过来,这没有区别(。

这个脚本会旋转您的图像,并将它们全部保存在一个新文件夹中:

import imageio
import numpy as np
import os
import ndimage
outPath = "rotated_images/"
path = "images/"
# iterate through the names of contents of the folder
for image_path in os.listdir(path):
# create the full input path and read the file
input_path = os.path.join(path, image_path)
image_to_rotate = imageio.imread(input_path)

# rotating all images with first shape 384
if image_to_rotate.shape[0] == 384:
# rotate the image
rotated = ndimage.rotate(image_to_rotate, 90)
else:
rotated = image_to_rotate
fullpath = os.path.join(outPath, image_path)
imageio.imsave(fullpath, rotated) 

之后,您可以根据需要在outPath文件夹中调用image_dataset_from_directory

类似的东西可以在这里找到。

最新更新