らんだむな記憶

blogというものを体験してみようか!的なー

DeepAA (1)

GitHub - OsciiArt/DeepAA: make ASCII Art by Deep Learningディープラーニングアスキーアートを描くというプロジェクトがある。ちょうど Pix2pix とか CycleGAN とかが世の中に出てきた頃のプロジェクトだ。ただし GAN を使っているわけではない。

この頃だと、TensorFlow 1.x の頃で、PyTorch も出てきた直後だった。ということで TensorFlow 1.3.0 で実装されている。今更その環境を作るのは少し面倒くさいので TensorFlow 2 対応をした。といっても元々 Keras が使われているので毛の先程度書き換えたら動く。
アップデートをサボったモジュールもあるが、主要そうなものはある程度アップデートした。GCP 環境が CUDA 11.0 なので、TensorFlow 2.4.1 のプレビルトバイナリを使っているが、結局 CPU モードで試したので何でもよかった。

tensorflow               2.4.1
numpy                    1.22.3
Pillow                   8.1.0
pandas                   1.4.1
scikit-learn             0.24.1
h5py                     2.10.0

そして output.py を以下のように変更するだけで動作した:

@@ -1,7 +1,6 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
-
-from keras.models import model_from_json
+from tensorflow.keras.models import model_from_json
 import numpy as np
 import pandas as pd
 from PIL import Image
@@ -10,8 +9,10 @@ import os
 
 
 # parameters
-model_path = "model/model.json" # or "model/model_light.json"
-weight_path = "model/weight.hdf5" # or "model/weight_light.json"
+#model_path = "model/model.json" # or "model/model_light.json"
+model_path = "model/model_light.json"
+#weight_path = "model/weight.hdf5" # or "model/weight_light.hdf5"
+weight_path = "model/weight_light.hdf5"
 image_path = 'sample images/original images/21 original.png' # put the path of the image that you convert.
 new_width = 0 # adjust the width of the image. the original width is used if new_width = 0.
 input_shape = [64, 64, 1]
@@ -43,7 +44,7 @@ char_list = pd.read_csv(char_list_path, encoding="cp932")
 print("len(char_list)", len(char_list))
 # print(char_list.head())
 char_list = char_list[char_list['frequency']>=10]
-char_list = char_list['char'].as_matrix()
+char_list = char_list['char'].values
 
 for k, v in enumerate(char_list):
     if v==" ":

変換したいデータは何でもいいのだが、OpenCV で細線化とかを施した画像を使えば良いはずで、実際にそれで試してみた。CPU モードでやったので少し時間がかかったが、数分ぼんやりと眺めていたら無事アスキーアート化してくれた。