*論文:条件付き敵対的ネットワークによる画像から画像への変換
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