LoginSignup
4
3

More than 5 years have passed since last update.

GANGAN(贋GAN)

Last updated at Posted at 2018-12-01

はじめに

ジョーク記事ですので悪しからず.

2018年はGANが大流行でしたね.
DeepMindのインターン生が作ったというBigGANは, もう本物と区別が付かないようなクオリティです.
pics-generated (2).png
Large Scale GAN Training for High Fidelity Natural Image Synthesis

また, フランスの芸術家集団ObviousがGANで生成した絵画をオークションに出したら43万ドルで落札されたというセンセーショナルなニュースもありました.
https_%2F%2Fjp.hypebeast.com%2Ffiles%2F2018%2F10%2Fai-art-portrait-christies-obvious-sold-432500-usd-1.jpg
AI絵画, 大手オークションで初の落札 予想額の40倍超 写真4枚 国際ニュース:AFPBB News
これを受けて「GAN(贋)作を掴まされた」なんてジョークも流行りましたね.

この1年でGANが生成する画像は本物の画像に大きく近づいたと言えます.
また, 個人的に最近GANの論文をいろいろと読んでいたせいか, 正方形の画像が縦横に並んでいると「GANだ!」と思うようになってしまいました.
というわけで, 今回の記事では, 本物の画像をグリッド状に整列することでGANで生成したと見紛うような画像を作ります.

こんな感じのものを目指します.
hakodate.png

名付けて「GANGAN(贋GAN)」です.

手法

データ

この前参加した函館開発合宿の写真を使います.
例えば, 函館名物ラッキーピエロ.
IMG_20181111_133858.jpg

リサイズ

短辺の長さに合わせて中心を正方形でクロップしてから, 適当なサイズに縮小します.

im = Image.open(file)
w, h = im.size
w_mid, h_mid = int(w/2), int(h/2)
unit = int(min(w, h)/2)
im_crop = im.crop((w_mid-unit, h_mid-unit, w_mid+unit, h_mid+unit))
im_resize =  im_crop.resize((S,S))

整列

画像のリストを, numpyreshapetransposeをがんばって組み合わせて整列させます.
ここが一番難しかったです.

ims = np.array(ims)
ims = ims.reshape((5,5,S,S,3))
ims = ims.transpose(0,2,1,3,4)
ims = ims.reshape((5*S,5*S,3))

 

コード

全体のコードです.

import glob
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

def main():
    S = 256 # size of one picture
    n_img = 5*5

    print('Enter relative path to the directry that has pictures:')
    dir_name = str(input())
    files =glob.glob(dir_name + '/*')
    ims = []
    for i,file in enumerate(files):
        if i>=n_img:
            break
        print(file)
        im = Image.open(file)
        w, h = im.size
        w_mid, h_mid = int(w/2), int(h/2)
        unit = int(min(w, h)/2)
        im_crop = im.crop((w_mid-unit, h_mid-unit, w_mid+unit, h_mid+unit))
        im_resize =  im_crop.resize((S,S))
        ims.append(np.array(im_resize))

    ims = np.array(ims)
    ims = ims.reshape((5,5,S,S,3))
    ims = ims.transpose(0,2,1,3,4)
    ims = ims.reshape((5*S,5*S,3))
    plt.imsave('result.png', ims)

if __name__=='__main__':
    main()

おわりに

調べてみたら, アルバムジャケットをDCGANで生成している人がいました.
albums_128px.png
https://github.com/Newmu/dcgan_code

これをGANGANで生成すると…?
result.png
どっちが本物かわからなくなってきましたね?
Twitterで見かける#私を構成する9枚にも使えます.

以上です. お付き合いいただきありがとうございました.
GANをチューニングする時間と体力がなかった.

「このくらいなら自分でも書ける」と思った応物/OBOGのあなた!
まだ枠がありますのでぜひアドベントカレンダーに参加してください!

4
3
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
4
3