2
2

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 5 years have passed since last update.

ディープラーニングで似顔絵生成。WarpGANをiOSで使う。

Last updated at Posted at 2020-09-14

似顔絵を生成できるWarpGAN

スクリーンショット 2020-08-13 22.38.11.png
![スクリーンショット 2020-09-14 20.54.14.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/235259/1ada8c64-e3e8-a361-330d-142afd0110a3.png)

Core MLに変換すれば、Image Picker Controllerなどから画像を入力するだけで似顔絵が生成できます。
CoreMLTools4.0になって変換できるようになりました(tfcoremlでは変換できなかった)。
変換済みCore ML モデル

1、グラフをpbtxt形式にする。

warpgan.py
tf.io.write_graph(self.sess.graph_def, '', 'warpgan.pbtxt')

*allow_pickleを設定する必要がありました

detect_face.py
data_dict = np.load(data_path, encoding='latin1',allow_pickle=True).item()

2、チェックポイントから凍結グラフを作る。

from tensorflow.python.tools.freeze_graph import freeze_graph

graph_def_file = 'warpgan.pbtxt'
checkpoint_file = 'warpgan_pretrained/ckpt-100000'
frozen_model_file = './frozen_model.pb'
output_node_names = 'Decoder/Decoder/images_rendered'

freeze_graph(input_graph=graph_def_file,
             input_saver="",
             input_binary=False,
             input_checkpoint=checkpoint_file,
             output_node_names=output_node_names,
             restore_op_name="save/restore_all",
             filename_tensor_name="save/Const:0",
             output_graph=frozen_model_file,
             clear_devices=True,
             initializer_nodes="")

3、coremltoolsで変換する。

import coremltools as ct
image_input = ct.ImageType(shape=(1, 256, 256, 3,),
                             scale=2/255,bias=[-1,-1,-1])

coreml_model = ct.convert(
        "frozen_model.pb",
        inputs=[image_input],
        )
coreml_model.save("warpgan.mlmodel")

Visionで顔まわりでクロップしてからモデルにかけると綺麗に変換できます。

result_4.jpg

Core MLを使ったアプリを作っています。
機械学習関連の情報を発信しています。

Twitter
[MLBoysチャンネル]
(https://www.youtube.com/channel/UCbHff-wfjTnB3rtXIP6y0xg)
Medium

相棒
note

2
2
1

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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?