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

高速超解像機械学習モデルをiOSで使う方法です

256 → 1024

スクリーンショット 2022-03-02 11.04.45.png

超解像を高速に使いたい

モバイルアプリでのユーザーエクスペリエンスの向上や、動画の超解像には、超解像モデルの実行時間が長すぎる場合があります。

Fast-SRGAN

高速実行できる超解像モデルです。
これをiOS用に変換して実行します。

#変換

チェックポイントからモデルをロードし、CoreML形式に変換します。

from tensorflow import keras

model = keras.models.load_model('models/generator.h5')
inputs = keras.Input((None, None, 3))
output = model(inputs)
model = keras.models.Model(inputs, output)

import coremltools as ct
mlmodel = ct.convert(model,inputs=[ct.ImageType(shape=(1,256,256,3),scale=1/255)])

出力形状を画像用に変形し、非正規化します。

from coremltools.proto import FeatureTypes_pb2 as ft
from coremltools.models.utils import save_spec

spec = mlmodel.get_spec()
builder = ct.models.neural_network.NeuralNetworkBuilder(spec=spec)
builder.add_permute(name="permute", dim=[0,3,1,2], input_name="Identity", output_name="permute_out")
# builder.add_squeeze(name="squeeze", input_name="permute_out", output_name="squeeze_out", axes = None, squeeze_all = True)
builder.add_activation(name="activation",non_linearity="LINEAR",input_name="permute_out",output_name="activation_out",params=[127.5,127.5])

builder.spec.description.output.pop()
builder.spec.description.output.add()
output = builder.spec.description.output[0]
output.name = "activation_out"

output.type.imageType.colorSpace = ft.ImageFeatureType.ColorSpace.Value('RGB')
output.type.imageType.width = 1024 
output.type.imageType.height = 1024

save_spec(builder.spec, 'fast-SRGAN.mlmodel')

0.42秒で実行できました。

変換スクリプト:

変換済みモデル:

🐣


フリーランスエンジニアです。
お仕事のご相談こちらまで
rockyshikoku@gmail.com

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

Twitter
Medium
GitHub

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