- 投稿日:2022-02-25T21:41:40+09:00
Fast-Neural-Style-TransferをiOS、MacOSで使えるようにする CoreML変換
Fast-Neural-Style-TransferをiOSで使う方法です 変換済みモデル 変換スクリプト 画風変換アプリを作ろう Fast-Neural-Style-Transferは画風変換ができるモデルです。 iOSで使えば画像やビデオの画風変換アプリができます。 CoreMLに変換するとiOS、MacOSデバイスで使える 方法 Pytorch版のリポジトリをクローン、リポジトリのリンクから事前トレーニング済みの重みをダウンロードします。 事前トレーニング済みの重みからモデルを復元します。 from models import TransformerNet import torch transform = style_transform() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") transformer = TransformerNet().to(device) transformer.load_state_dict(torch.load("style-transfer-models/mosaic_10000.pth")) transformer.eval() 変換します。 import coremltools as ct example_input = torch.randn(1,3,960,640).cuda() traced_model = torch.jit.trace(transformer, example_input) mlmodel = ct.convert(traced_model, inputs=[ct.ImageType(name="input", shape=example_input.shape,bias=[-0.485/0.229,-0.456/0.224,-0.406/0.225],scale=1.0/255.0/0.226)]) モデルの入力は正規化されているので、CoreMLの入力も同じように前処理されるように設定しています。 モデルからニューラールネットワークビルダーを作り、出力名を確認します。 spec = mlmodel.get_spec() builder = ct.models.neural_network.NeuralNetworkBuilder(spec=spec) builder.spec.description モデルの出力をRGB画像にするには、出力も前処理に合わせて非正規化する必要があります。 カラーチャネルごとにテンソルを前処理の逆にアクティベートし、再結合するレイヤーを追加します。 var_293は元のモデルの出力名です。 builder.add_split(name="split", input_name="var_293", output_names=["split_1","split_2","split_3"]) builder.add_activation(name="activation_1",non_linearity="LINEAR",input_name="split_1",output_name="activation_out_1",params=[255*0.226,0.485*255]) builder.add_activation(name="activation_2",non_linearity="LINEAR",input_name="split_2",output_name="activation_out_2",params=[255*0.226,0.456*255]) builder.add_activation(name="activation_3",non_linearity="LINEAR",input_name="split_3",output_name="activation_out_3",params=[255*0.226,0.406*255]) builder.add_stack(name="stack", input_names=["activation_out_1","activation_out_2","activation_out_3"], output_name="stack_out", axis=0) builder.add_squeeze(name="squeeze", input_name="stack_out", output_name="squeeze_out", axes = None, squeeze_all = True) 出力をRGB画像に設定します。 from coremltools.proto import FeatureTypes_pb2 as ft from coremltools.models.utils import save_spec builder.spec.description.output.pop() builder.spec.description.output.add() output = builder.spec.description.output[0] output.name = "squeeze_out" output.type.imageType.colorSpace = ft.ImageFeatureType.ColorSpace.Value('RGB') output.type.imageType.width = 640 output.type.imageType.height = 960 save_spec(builder.spec, 'fast-style-transfer-mosaic.mlmodel') Swiftで実行 let model = try! fast-style-transfer-mosaic(configuration: MLModelConfiguration()).model let vnModel = try! VNCoreMLModel(for: model) let request = VNCoreMLRequest(model: vnModel) let handler = VNImageRequestHandler(ciImage: image, options: [:]) try! handler.perform([request]) guard let result = request.result?.first as? VNPixelBufferObservation else { return } let pixelBuffer = result.pixelBuffer 動画に適用 ? フリーランスエンジニアです。 お仕事のご相談こちらまで rockyshikoku@gmail.com Core MLやARKitを使ったアプリを作っています。 機械学習/AR関連の情報を発信しています。 Twitter Medium
- 投稿日:2022-02-25T02:14:12+09:00
CustomCellを使う!!
#はじめに XIBファイルを使ってCustomCellを作成した際に、どうやったらTableViewに反映できるのか、 その方法を備忘録的にまとめていきたいと思います。 ###1.CustomCellの作成 画面左ナビゲーターエリアのいずれかのファイルを右クリック、New File...を選択 はい!画面のような操作をして頂くとXIBファイルが作成出来ます。 注意としましては[Also create XIB file] にチェックを入れる事を忘れないようにして下さい。 ###2.CustomCellをカスタマイズする!! 下記の図のように好きにカスタマイズしちゃって下さい!! ###3.CustomCellを表示しよう!! 画面右UTTエリアのIdantifierを(customCell)に設定する事を忘れないで下さい。 ViewController import UIKit class ViewController: UIViewController { let itemArray = ["cell 1","cell 2","cell 3"] override func viewDidLoad() { super.viewDidLoad() let tableView = UITableView(frame: self.view.bounds, style: .plain) //tableViewの設定を記述 tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "customCell") tableView.delegate = self tableView.dataSource = self view.addSubview(tableView) } } extension ViewController:UITableViewDelegate,UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { itemArray.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { //tableViewの内容を指定 let cell = tableView.dequeueReusableCell(withIdentifier: "customCell",for: indexPath) as! TableViewCell return cell } } customCellが無事に反映されましたね!!
- 投稿日:2022-02-25T00:28:57+09:00
WidgetKitで天気予報アプリ作ってみた〜記事まとめ〜
はじめに この記事はWidgetKitで天気予報アプリ作ってみたシリーズで書いた記事のまとめです。 記事一覧 1.天気情報取得編 2.位置情報取得 & 保存編 3.Widget Extension追加編 4.タイムライン作成編 5.View実装編 誰かの役に立てば幸いです! おしらせ 現在副業でiOSアプリ開発を募集しています。 ご依頼はTwitter DMでお待ちしております。 ↓活動リンクはこちら↓ https://linktr.ee/sasaki.ken