Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/pt-br/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. # ==============================================================================

Modelo baseado em HRNet para segmentação semântica

Neste notebook, você vai:

  • Escolher e carregar um dos 17 modelos HRNet pré-treinados com diferentes datasets de segmentação semântica.

  • Executar a inferência para extrair características do backbone do modelo e previsões do head do modelo.

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

Carregando modelos do TensorFlow Hub

Aqui, você pode escolher o modelo HRNet pré-treinado a ser carregado. Modelos diferentes usam um dataset de treinamento diferente. Todos os modelos têm a mesma arquitetura, exceto pelo head do modelo, que tem uma dimensão diferente baseada no número de classes presentes no dataset de treinamento (dataset_output_classes). Confira mais informações sobre os diferentes datasets nos links acima e na coleção de datasets de fatores de influência.

#@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)

Carregando uma imagem e executando a inferência

Esta é uma demonstração de como executar a inferência para extrair características e previsões de uma imagem. A imagem foi obtida no dataset scene150.

Para fazer a inferência nos datasets que foram usados durante o treinamento, consultamos a coleção de datasets de fatores de influência.

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()