Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/es-419/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 basado en HRNet para segmentación semántica

En estas notas, haremos lo siguiente:

  • Elegir y cargar uno de los 17 modelos HRNet previamente entrenados con diferentes conjuntos de datos de segmentación semántica.

  • Ejecutar la inferencia para extraer características de la columna vertebral del modelo y para extraer predicciones de la cabeza del modelo.

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

Carga de modelos de TensorFlow Hub

Aquí se puede elegir qué modelo HRNet previamente entrenado cargar; modelos diferentes implican el uso de conjuntos de datos de entrenamiento diferentes. Todos los modelos tienen la misma arquitectura, excepto por la cabeza del modelo, que tiene una dimensión diferente que se basa en la cantidad de clases que contiene el conjunto de datos de entrenamiento (dataset_output_classes). Para más información acerca de los diferentes conjuntos de datos, consulte los enlaces que figuran arriba y la colección de conjuntos de datos de factores de influencia.

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

Carga de una imagen y ejecución de la inferencia

En esta demostración se comparte cómo ejecutar la inferencia para extraer características y predicciones de una imagen. La imagen fue tomada del conjunto de datos scene150.

Para realizar la inferencia en los conjuntos de datos que se usaron durante el entrenamiento, consultamos la colección de conjuntos de datos de factores de influencia.

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