0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Google Colabのpix2pixスクリプトを自前で作成した画像データを用いて実行させた

Posted at

概要

Google Colabのpix2pixのスクリプトに、自前で用意した学習用画像データを渡して実行した。
学習用画像は、プログラムを作成してpix2pixのスクリプトに合わせて準備した。

デフォルトで用意されている画像を確認

自分で学習データを用意する前に、Google Colab内でロードされているデータセット「facades.tar.gz」をダウンロードして中身の画像を確認した。

たてよこ256pxの画像が左が出力画像(真の画像)、右が入力画像の組み合わせで結合されていた。

image.png

フォルダ構成は、testフォルダに106枚のjpg、trainフォルダに400枚、valフォルダに10枚の画像が入っていた。
image.png

自分で用意した画像

デフォルトで用意されている画像と同じサイズ、同じ構成(左が出力画像、右が入力画像)になるように変換するプログラムを作成する。
変換もとの画像は、次のものを使用した。

以下の順で変換するプログラムを作成して、フォルダ内に配置した画像にたいして処理を実行した。

  • 画像を256x256にリサイズ。出力画像とする。
  • リサイズ画像を白黒画像
  • 白黒画像から輪郭をとる
  • リサイズ画像と輪郭画像を結合

元の画像
image.png

作成した学習用の画像
image.png

使用したプログラムは以下のものでgocvを使用した。

package main

import (
	"fmt"
	"image"
	"image/draw"
	"image/jpeg"
	"io/ioutil"
	"os"

	"gocv.io/x/gocv"
)

func main() {
	inputdir := "./images/"
	out := "./outdata/"

	imgSize := 256
	outImgWidth := 512
	outImgHeight := 256

	fmt.Println("image convert start")
	files, err := ioutil.ReadDir(inputdir)

	if err != nil {
		panic(err)
	}

	// エッジ抽出の閾値
	edge_thresh := 40
	for _, file := range files {

		filename := inputdir + file.Name()
		// 画像のリサイズ
		resizeMat := gocv.IMRead(filename, gocv.IMReadAnyColor)
		gocv.Resize(resizeMat, &resizeMat, image.Point{imgSize, imgSize}, 0, 0, gocv.InterpolationDefault)
		// グレースケールさせてからエッジ抽出
		gray := gocv.NewMat()
		gocv.CvtColor(resizeMat, &gray, gocv.ColorBGRToGray)
		edge := gocv.NewMat()
		gocv.Canny(gray, &edge, float32(edge_thresh), float32(edge_thresh*3))

		// 画像の結合 左:リサイズ 右:エッジ抽出
		outImg := image.NewRGBA(image.Rect(0, 0, outImgWidth, outImgHeight))
		resizeImg, _ := resizeMat.ToImage()
		rectLeft := image.Rect(0, 0, imgSize, imgSize)
		draw.Draw(outImg, rectLeft, resizeImg, image.Point{0, 0}, draw.Over)

		edgeImg, _ := edge.ToImage()
		rectRight := image.Rect(imgSize, 0, imgSize+imgSize, imgSize)
		draw.Draw(outImg, rectRight, edgeImg, image.Point{0, 0}, draw.Over)

		// 画像の保存
		outname, _ := os.Create(out + file.Name())
		qt := jpeg.Options{
			Quality: 100,
		}
		err6 := jpeg.Encode(outname, outImg, &qt)
		if err6 != nil {
			fmt.Fprintln(os.Stderr, err6)
			return
		}

		resizeMat.Close()
		gray.Close()
		edge.Close()
	}

	fmt.Println("image convert end")
}

testフォルダに100枚。trainフォルダに1000枚いれてfacades.tar.gzに圧縮した。valフォルダはColab内で使用されてなさそうなので削除した。

S3に配置

Goolgle Colabからダウンロードさせるため、作成したfacades.tar.gzをS3に配置し、access control listを編集してアクセス可能なように、Everyone (public access)にReadを設定した。Colab側に貼り付けるため、Object URLをメモっておく。

image.png

画像ダウンロード先の書き換え

Colab側の学習画像ダウンロード先のURLを、メモっておいたs3のObject URLに書き換えて、実行させた。

下は書き換え済み
image.png

学習結果

最終的な学習結果は、次のような画像が生成された。テスト画像のほうも似たような感じだった。
image.png

training_checkpointsフォルダ配下に、学習済モデルができているので、googleドライブにコピーしてからダウンロードした。
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?