caffe2:機械学習では画像データの下準備が重要(PARTⅠ)

その買うを、もっとハッピーに。|ハピタス

machine learning(機械学習)で扱う画像データは前処理が必要らしいので今日はimage-preprocessingを学習することにした。このtutorialは猫の画像が可愛いのが気に入っている。

スポンサーリンク

画像読み込みと前処理

まずは必要なモジュールをimportする。

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

%matplotlib inline
import skimage
import skimage.io as io
import skimage.transform 
import sys
import numpy as np
import math
from matplotlib import pyplot
import matplotlib.image as mpimg
print("Required modules imported.")
Required modules imported.

skimageはpip3 install scikit-imageでインストールできる。IMAGE_LOCATIONは画像フォルダーの中の画像でもいいし、ネット上の画像でもいい。tutorialには4ラウンド分の画像が用意されている。※ネット上の画像は将来的に消される可能性があるのでダウンロードしておくのがいいようだ。

# You can load either local IMAGE_FILE or remote URL
# For Round 1 of this tutorial, try a local image.
IMAGE_LOCATION = 'images/cat.jpg'

# For Round 2 of this tutorial, try a URL image with a flower: 
# IMAGE_LOCATION = "https://cdn.pixabay.com/photo/2015/02/10/21/28/flower-631765_1280.jpg"
# IMAGE_LOCATION = "images/flower.jpg"

# For Round 3 of this tutorial, try another URL image with lots of people:
# IMAGE_LOCATION = "https://upload.wikimedia.org/wikipedia/commons/1/18/NASA_Astronaut_Group_15.jpg"
# IMAGE_LOCATION = "images/astronauts.jpg"

# For Round 4 of this tutorial, try a URL image with a portrait!
# IMAGE_LOCATION = "https://upload.wikimedia.org/wikipedia/commons/9/9a/Ducreux1.jpg"
# IMAGE_LOCATION = "images/Ducreux.jpg"

img = skimage.img_as_float(skimage.io.imread(IMAGE_LOCATION)).astype(np.float32)

# test color reading
# show the original image
pyplot.figure()
pyplot.subplot(1,2,1)
pyplot.imshow(img)
pyplot.axis('on')
pyplot.title('Original image = RGB')

# show the image in BGR - just doing RGB->BGR temporarily for display
imgBGR = img[:, :, (2, 1, 0)]
#pyplot.figure()
pyplot.subplot(1,2,2)
pyplot.imshow(imgBGR)
pyplot.axis('on')
pyplot.title('OpenCV, Caffe2 = BGR')
Text(0.5,1,'OpenCV, Caffe2 = BGR')

元画像は一般的なRGB、caffe, caffe2, opencvはGBR形式を採用している(これはopencvのlegacy supportに起因しているらしい)。上の画像のように、caffe2で扱う画像は事前にBGR形式に変換しておく必要がある。

スポンサーリンク

CaffeはCHW Orderを好む

CPUは一般的にH(Height)W(Width)C(Collor Channel)の順序でメモリにallocateされるが、GPU(cuDNN)ではこの順序がCHWになる。つまり、caffe2-GPU用の画像の前処理にはHWC→CHW、RGB→BGRの変換が必須。CPUモードでcaffe2を使うならHWCの順序のままでも問題はないのだろうが、caffe2をGPUモードで使う場合はBGR変換だけではなく、このCHW変換も必要になってくる。

スポンサーリンク

ミラーイメージと画像回転

写真はlandscapeだったりportraitだったり解像度もカメラによって違うし、そういった違いはEXIFデータで簡単に識別できるが、このEXIFデータがない場合は目視で確認するしかない。例えば、下の2つの画像の鏡文字は通常の文字に変換される必要があるし、90度回転したセルタワーの画像は元の無回転(回転していない)画像に変換される必要がある。でないと、機械学習の学習効率は大きく落ち込むことになる。

# Image came in sideways - it should be a portait image!
# How you detect this depends on the platform
# Could be a flag from the camera object
# Could be in the EXIF data
# ROTATED_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/8/87/Cell_Phone_Tower_in_Ladakh_India_with_Buddhist_Prayer_Flags.jpg"
ROTATED_IMAGE = "images/cell-tower.jpg"
imgRotated = skimage.img_as_float(skimage.io.imread(ROTATED_IMAGE)).astype(np.float32)
pyplot.figure()
pyplot.imshow(imgRotated)
pyplot.axis('on')
pyplot.title('Rotated image')

# Image came in flipped or mirrored - text is backwards!
# Again detection depends on the platform
# This one is intended to be read by drivers in their rear-view mirror
# MIRROR_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/2/27/Mirror_image_sign_to_be_read_by_drivers_who_are_backing_up_-b.JPG"
MIRROR_IMAGE = "images/mirror-image.jpg"
imgMirror = skimage.img_as_float(skimage.io.imread(MIRROR_IMAGE)).astype(np.float32)
pyplot.figure()
pyplot.imshow(imgMirror)
pyplot.axis('on')
pyplot.title('Mirror image')
Text(0.5,1,'Mirror image')
# Run me to flip the image back and forth
imgMirror = np.fliplr(imgMirror)
pyplot.figure()
pyplot.imshow(imgMirror)
pyplot.axis('off')
pyplot.title('Mirror image')
Text(0.5,1,'Mirror image')
# Run me to rotate the image 90 degrees
imgRotated = np.rot90(imgRotated, 3)
pyplot.figure()
pyplot.imshow(imgRotated)
pyplot.axis('off')
pyplot.title('Rotated image')
Text(0.5,1,'Rotated image')
参考サイトhttps://github.com/

スポンサーリンク
スポンサーリンク