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

作为背景,我从Kaggle下载了Pizza/Not Pizza dataset,我只想构建一个朴素的二进制分类模型.

在KERAS文档中,我应该使用IMAGE_DATASET_FROM_DIRECTORY函数,但有一个问题.它要求我将图像的大小作为参数提供给函数,但这会扰乱数据集.我已经注意到DS中的图像要么是512x384,要么是384x512,所以我想要做的就是加载这1000个图像,对它们应用转置,最后将所有内容转换为张量.

因此,我的问题是:如何从目录加载图像,而不预先设置特定的大小/形状?

推荐答案

你可以事先旋转所有第一个形状为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.

类似的东西可以在here个地方找到.

Python相关问答推荐

inspect_asm不给出输出

用Python获取HTML Span类中的数据

如何使用stride_tricks.as_strided逆转NumPy数组

如何在Python中使用时区夏令时获取任何给定本地时间的纪元值?

使用pandas、matplotlib和Yearbox绘制时显示错误的年份

Pythind 11无法弄清楚如何访问tuple元素

更改matplotlib彩色条的字体并勾选标签?

Pandas 第二小值有条件

'discord.ext. commanders.cog没有属性监听器'

Pandas 都是(),但有一个门槛

如何在Python数据框架中加速序列的符号化

Godot:需要碰撞的对象的AdditionerBody2D或Area2D以及queue_free?

Python—从np.array中 Select 复杂的列子集

无论输入分辨率如何,稳定扩散管道始终输出512 * 512张图像

使用__json__的 pyramid 在客户端返回意外格式

如何在Python中使用Iscolc迭代器实现观察者模式?

仅使用预先计算的排序获取排序元素

我可以不带视频系统的pygame,只用于游戏手柄输入吗?''

Regex用于匹配Python中逗号分隔的AWS区域

在pandas中,如何在由两列加上一个值列组成的枢轴期间或之后可靠地设置多级列的索引顺序,