python - imgaug : load and save images

标签 python image opencv tensorflow scipy

我正在使用 Python+Tensorflow 在高性能计算集群上进行 CNN 训练。我正在训练一个卷积神经网络,但数据集相对较小。所以我正在实现技术来增强它。这是我第一次处理核心计算机视觉问题,所以对它来说还比较陌生。

随着训练图像数量的增加,需要 cv2 的 imgaug 包 ( https://github.com/aleju/imgaug ) 似乎是完美的,并且是增加图像数量的简单解决方案。我使用 python 2.7 安装了 opencv2,所有必需的 python 包都在本地运行(这里没有使用 env/Anaconda/Docker)。

实际上,我正在尝试从 here 运行这段代码:

import os
import imgaug as ia
from imgaug import augmenters as iaa
import scipy
import cv2
import numpy as np
# optional check my hint import scipy.misc import imwrite
# optional check my hint import scipy.misc import imsave

ia.seed(1)

# Example batch of images.
# The array has shape (32, 64, 64, 3) and dtype uint8.
images = np.array(
    [ia.quokka(size=(64, 64)) for _ in range(32)],
    dtype=np.uint8
)

seq = iaa.Sequential([
    iaa.Fliplr(0.5), # horizontal flips
    iaa.Crop(percent=(0, 0.1)), # random crops
    # Small gaussian blur with random sigma between 0 and 0.5.
    # But we only blur about 50% of all images.
    iaa.Sometimes(0.5,
        iaa.GaussianBlur(sigma=(0, 0.5))
    ),
    # Strengthen or weaken the contrast in each image.
    iaa.ContrastNormalization((0.75, 1.5)),
    # Add gaussian noise.
    # For 50% of all images, we sample the noise once per pixel.
    # For the other 50% of all images, we sample the noise per pixel AND
    # channel. This can change the color (not only brightness) of the
    # pixels.
    iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
    # Make some images brighter and some darker.
    # In 20% of all cases, we sample the multiplier once per channel,
    # which can end up changing the color of the images.
    iaa.Multiply((0.8, 1.2), per_channel=0.2),
    # Apply affine transformations to each image.
    # Scale/zoom them, translate/move them, rotate them and shear them.
    iaa.Affine(
        scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
        translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
        rotate=(-25, 25),
        shear=(-8, 8)
    )
], random_order=True) # apply augmenters in random order

images_aug = seq.augment_images(images)

用这段代码创建一个 .py 文件后,我将这个文件复制到一个包含 50 张图片 (.jpg) 的文件夹中。 代码本身运行完美(没有遇到错误),但似乎 .jpg 没有加载到 .py 文件中,即使我设置了文件夹路径,它也不会加载图像。此外,没有写入新文件。

问题 1:我想我必须将图像作为数组加载到 .py 或 cv2 中,但我找不到任何示例如何加载和写入它们,因为我从未做过这样的事情。有什么帮助吗?

问题 2:在此上下文中使用的最佳界面是什么?所有图像都存储在文件夹或 .xlsx 文件中。

(提示:不幸的是,这可能是一个基于 Keras(Link)的选项?或者我找到了这两个选项,但我无法使用它们 scipy.ndimage.imreadscipy.misc.imsave)

最佳答案

images_aug = seq.augment_images(图像)

您的代码对图像 进行扩充,而不是对您的文件进行扩充。所以你首先阅读了你的文件内容。 作者在他们的自述文件中也有这些行。

for batch_idx in range(1000):
    # 'images' should be either a 4D numpy array of shape (N, height, width, channels)
    # or a list of 3D numpy arrays, each having shape (height, width, channels).
    # Grayscale images must have shape (height, width, 1) each.
    # All images must have numpy's dtype uint8. Values are expected to be in
    # range 0-255.

Question 1: I guess I must load the images as an array into the .py, or into the cv2 but I can’t find any example how to load and write them, as I have never done something like this. Any help?

只需创建一个具有大小(N、高度、宽度、 channel )的 4d 数组。然后读取每个图像并将其插入该数组即可。例如

import numpy as np
import cv2
images = np.zeros((N, height, width, channels))
for idx, img_path in enumerate(img_paths):
    img = cv2.imread(img_path, 1)
    images[idx, :, :, :] = img

Question 2: What is the best interface to use in this context? All the images are stored in folders or .xlsx files.

数据论证用于增加训练数据的稳健性。如果您的“接口(interface)”是指深度学习框架,那么任何具有 Python 接口(interface)的框架都应该运行良好。

希望对您有所帮助。

关于python - imgaug : load and save images,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50506782/

相关文章:

Python:迭代元组时是否保留顺序?

c++ - Visual Studio 中的 OpenCV 使用 C++ 如何从视频文件中剪切选定的时序?

c++ - OpenCv C++ 透视变换

c - OpenCV 中的肤色检测

Python + Selenium + PhantomJS 渲染为 PDF

javascript - [][] 如何在正则表达式中解析?

python - Telegram API 要求用户确认

python - 如何使用 PIL 将图像保存在内存中并上传?

javascript - 图片加载像 google image PHP

Python通过http发送数据和jpg