1
3

More than 3 years have passed since last update.

Pix2PixをiOSモバイル用モデルに変換(CoreML)スマホで画像生成。

Last updated at Posted at 2020-06-22

pix2pix_2.png

*論文:条件付き敵対的ネットワークによる画像から画像への変換

CoreMLに変換することでpix2pix画像変換をiPhoneアプリで使用できます。

このストーリーでは、TensorFlow CoreのPix2Pixチュートリアルモデルを使用します。

最初に、Colaboratoryでチュートリアルモデルをトレーニングします。

colabの以下のセルまでの全てのセルを実行します。

fit(train_dataset、EPOCHS、test_dataset)

モデルのトレーニングが完了したら、新しいセルを挿入して以下の手順で変換を実行します。

1、TFCoreMLをインストールします。

!pip install --upgrade tfcoreml

2、チェックポイントを復元します。

checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))

3、「saved_model」フォーマットでジェネレータを保存します。

generator.save( './ savedmodel')

4、変換を実行します。

import tfcoreml
input_name = generator.inputs[0].name.split(':')[0]
print(input_name) #Check input_name.
keras_output_node_name = generator_g.outputs[0].name.split(':')[0]
graph_output_node_name = keras_output_node_name.split('/')[-1]
mlmodel = tfcoreml.convert('./savedmodel',
                       input_name_shape_dict={input_name: (1, 256, 256, 3)},
                       output_feature_names=[graph_output_node_name],
                       minimum_ios_deployment_target='13',
                       image_input_names=input_name,
                       image_scale=2/ 255.0,
                       red_bias=-1,
                       green_bias=-1,
                       blue_bias=-1,
                       )
mlmodel.save('./pix2pix.mlmodel')

これで、iOSプロジェクトでPix2Pixを使用できます。

import Vision
lazy var coreMLRequest:VNCoreMLRequest = {
   let model = try! VNCoreMLModel(for: pix2pix().model)
   let request = VNCoreMLRequest(model: mode, completionHandler: self.coreMLCompletionHandler0)   
   return request
   }()

let handler = VNImageRequestHandler(ciImage: ciimage,options: [:])   

DispatchQueue.global(qos: .userInitiated).async {   
  try? handler.perform([coreMLRequest])
}

multiArrayを画像として視覚化するには、Hollance氏のCoreML Helpersが非常に便利です。

MultiArrayからImageへの変換 CoreMLHelper

チャオ!

Twitterフォローしてくださいお願いします!
https://twitter.com/JackdeS11

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