Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/ja/hub/tutorials/hrnet_semantic_segmentation.ipynb
25118 views
Kernel: Python 3

Licensed under the Apache License, Version 2.0 (the "License");

#@title Copyright 2022 The TensorFlow Hub Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ==============================================================================

セマンティックセグメンテーションの HRNet ベースモデル

このノートブックでは、以下について説明します。

  • 異なるセマンティックセグメンテーション データセットで事前にトレーニングした 17 個の HTNet モデルから 1 つを選択して読み込む

  • 推論を実行して、モデルヘッドのモデルバックボーンと予測から特徴量を抽出する

import tensorflow as tf import tensorflow_hub as hub import matplotlib.pyplot as plt from PIL import Image import numpy as np

TensorFlow Hub からモデルを読み込む

ここでは、事前トレーニング済みの読み込み用の HRNet モデルを選択できます。モデルの違いは使用されたトレーニングデータセットの違いです。トレーニングデータセット(dataset_output_classes)に含まれるクラスの数に応じて異なる次元を持つモデルヘッドを除き、すべてのモデルに同じアーキテクチャが使用されています。データセットの多様性についての詳細は、上記のリンクと影響要因データセットコレクションをご覧ください。

#@title Choose a pre-trained HRNet model to load. hrnet_model_name = 'ade20k-hrnetv2-w48/1' #@param ["ade20k-hrnetv2-w48/1", "isprs-hrnetv2-w48/1", "vkitti2-hrnetv2-w48/1", "vgallery-hrnetv2-w48/1", "sunrgbd-hrnetv2-w48/1", "suim-hrnetv2-w48/1", "scannet-hrnetv2-w48/1", "pvoc-hrnetv2-w48/1", "msegpcontext-hrnetv2-w48/1", "mapillary-hrnetv2-w48/1", "kitti-hrnetv2-w48/1", "isaid-hrnetv2-w48/1", "idd-hrnetv2-w48/1", "coco-hrnetv2-w48/1", "city-hrnetv2-w48/1", "camvid-hrnetv2-w48/1", "bdd-hrnetv2-w48/1"] tfhub_model_name = 'https://tfhub.dev/google/HRNet/' + hrnet_model_name print('HRNet model selected :', tfhub_model_name)
hrnet_model = hub.load(tfhub_model_name) print('HRNet model loaded :', tfhub_model_name)

画像を読み込んで推論を実行する

ここでは、画像から特徴量と予測を抽出する推論の実行方法を紹介します。画像は、scene150 データセットから取得されたものです。

トレーニング中に使用されたデータセットで推論を実行するには、影響要因データセットコレクションを参照してください。

img_file = tf.keras.utils.get_file(origin="https://tensorflow.org/images/bedroom_hrnet_tutorial.jpg") img = np.array(Image.open(img_file))/255.0
plt.imshow(img) plt.show() # Predictions will have shape (batch_size, h, w, dataset_output_classes) predictions = hrnet_model.predict([img]) plt.imshow(predictions[0,:,:,1]) plt.title('Predictions for class #1') plt.show() # Features will have shape (batch_size, h/4, w/4, 720) features = hrnet_model.get_features([img]) plt.imshow(features[0,:,:,1]) plt.title('Feature #1 out of 720') plt.show()