LoginSignup
3
3

More than 3 years have passed since last update.

セグメンテーションモデル(DeepLabV3)の結果をCore ML Helpersでラベルごとに振り分ける。

Last updated at Posted at 2020-10-10

アップルの公式配布DeepLabV3のCore ML モデルは、(512,512)のML Multi Arrayを出力します。
各ピクセルは0~14のラベル値です。

0,'background'背景
1,'aeroplane'飛行機
2,'bicycle'自転車
3,'bird'鳥
4,'boat'ボート
5,'bottle'ボトル
6,'bus'バス
7,'car'車
8,'cat'猫
9,'chair'椅子
10,'cow'牛
11,'diningtable'テーブル
12,'dog'犬
13,'horse'馬
14,'motorbike'バイク
15,'person'人
16,'pottedplant'鉢植え
17,'sheep'羊
18,'sofa'ソファ
19,'train'電車
20,'tv'テレビ

出力結果をマスク画像として使いたい場合、ピクセルのラベル値を判別する必要があります。
Core ML Helpersでラベル値によって判別してマスク画像にできます。

// Core ML Helpersの toRawBytes関数の中で、対象のラベル値を白くする

for c in 0..<channels {
      for y in 0..<height {
        for x in 0..<width {
          var value = ptr[c*cStride + y*yStride + x*xStride]
       //例えば車を判別したい場合、ラベル値が7なので、7は0(白)にして他は255(黒)にするとマスク画像ができる。
            if value != T(7) {
                value = T(0)
            } else {
                value = T(255)
            }
          let scaled = (value - min) * T(255) / (max - min)
          let pixel = clamp(scaled, min: T(0), max: T(255)).toUInt8
          pixels[(y*width + x)*bytesPerPixel + c] = pixel
        }
      }
    }

IMG_0530.PNG

マスク画像がとれると、背景だけぼかしたり、お馴染みのセグメンテーション画像が
できます。
IMG_0531.PNG

スクリーンショット 2020-09-09 11.13.10.png


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

Twitter
MLBoysチャンネル
Medium

相棒
note

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