简体   繁体   English

imgaug:加载和保存图像

[英]imgaug: load and save images

I am using Python+Tensorflow for CNN training on a high-performance computing cluster.我在高性能计算集群上使用 Python+Tensorflow 进行 CNN 训练。 I am training a convolutional neural network, but have a relatively small dataset.我正在训练一个卷积神经网络,但数据集相对较小。 So I am implementing techniques to augment it.所以我正在实施技术来增强它。 Now this is the first time i am working on a core computer vision problem so am relatively new to it.现在这是我第一次研究核心计算机视觉问题,所以我对它比较陌生。

As raising the number of images for training, the imgaug package ( https://github.com/aleju/imgaug ) which requires cv2 seems to be perfect and a simple solution to multiply the number of images.随着训练图像数量的增加,需要 cv2 的 imgaug 包( https://github.com/aleju/imgaug )似乎是完美的,并且是增加图像数量的简单解决方案。 I installed opencv2 using python 2.7, all required python packages are running locally (no env/Anaconda/Docker are used here).我使用 python 2.7 安装了 opencv2,所有需要的 python 包都在本地运行(这里没有使用 env/Anaconda/Docker)。

Practically, I' am trying to run this code from 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)

After creating a .py file with this code, I have copied this file into a folder of 50 images (.jpg).使用此代码创建 .py 文件后,我已将此文件复制到包含 50 个图像 (.jpg) 的文件夹中。 The code itself runs perfectly (no error encounter), but it seems like that the .jpg are not loaded into the.py file, even if I set the folder path, it will not load the images.代码本身运行完美(没有遇到错误),但似乎.jpg没有加载到.py文件中,即使我设置了文件夹路径,它也不会加载图像。 Also, no new files were written.此外,没有写入新文件。

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

Question 2: What is the best interface to use in this context?问题 2:在这种情况下使用的最佳界面是什么? All the images are stored in folders or .xlsx files.所有图像都存储在文件夹或 .xlsx 文件中。

(Hint: May be this is an option unfortunately its based on Keras ( Link )? Or these 2 option I've found, but I am not able to use them scipy.ndimage.imread and scipy.misc.imsave ) (提示:不幸的是,这可能是一个基于 Keras( 链接)的选项?或者我找到了这两个选项,但我无法使用它们scipy.ndimage.imreadscipy.misc.imsave

images_aug = seq.augment_images(images) images_aug = seq.augment_images(images)

Your code does augmentation on images , not on your files.您的代码对images增强,而不是对文件进行增强。 So you have read your files content first.所以你首先阅读了你的文件内容。 The author also have these lines in their README.作者在他们的自述文件中也有这些行。

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

Simply create a 4d array with size (N, height, width, channels).只需创建一个大小为(N、高度、宽度、通道)的 4d 数组。 Then read each image and push it into that array will work.然后读取每个图像并将其推送到该数组中即可。 For example例如

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?问题 2:在这种情况下使用的最佳界面是什么? All the images are stored in folders or .xlsx files.所有图像都存储在文件夹或 .xlsx 文件中。

Data argumentation is used to increase the robustness of training data.数据论证用于增加训练数据的鲁棒性。 If your "interface" means deep learning framework then any framework which has a python interface should work well.如果您的“接口”是指深度学习框架,那么任何具有 Python 接口的框架都应该运行良好。

Hope that helps.希望有帮助。

I have read the source code of imgaug, the method ' ia.quokka ' return (H,W,3) ndarray(the image array of dtype uint8.),so you can change the example to read and save images.我已经阅读了imgaug的源代码,方法' ia.quokka '返回(H,W,3)ndarray(dtype uint8的图像数组。),因此您可以更改示例以读取和保存图像。

This is my use:这是我的用途:

import imgaug as ia
from imgaug import augmenters as iaa
import numpy as np
import imageio

ia.seed(1)

img = imageio.imread("test.jpg") #read you image
images = np.array(
    [img for _ in range(32)], dtype=np.uint8)  # 32 means creat 32 enhanced images using following methods.

seq = iaa.Sequential(
    [
        iaa.Fliplr(0.5),  
        iaa.Crop(percent=(0, 0.1)),            
        iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5))),        
        iaa.ContrastNormalization((0.75, 1.5)),         
        iaa.AdditiveGaussianNoise(
            loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),    
        iaa.Multiply((0.8, 1.2), per_channel=0.2),
        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)

for i in range(32):
    imageio.imwrite(str(i)+'new.jpg', images_aug[i])  #write all changed images
from imgaug import augmenters as iaa

import cv2

seq = iaa.Sequential([
    iaa.Crop(px=(0, 16)),
    iaa.Fliplr(0.5),
    iaa.GaussianBlur(sigma=(0, 3.0))
])

imglist = []

img = cv2.imread('test.jpg')

imglist.append(img)

images_aug = seq.augment_images(imglist)

cv2.imwrite('new.jpg', images_aug[0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM