python - Deeplab - 经过训练的 Deeplab 模型的推理与可视化性能不一致

标签 python tensorflow deeplab

描述问题

我已经使用 Deeplab 在具有 4 个大小为 480x640 的类的自定义数据集上成功训练了我的模型,并使用 xception65 编码器。每当我使用 vis.py 脚本时,我都会在验证集上获得不错的结果:EvalImageA_ckpt , EvalImageB_ckpt 。但是,当我卡住模型时,我在相同的图像上没有得到相同的结果。

我使用 export_model.py 卡住了模型,并成功输出了 freeze_model.pb 文件。然而,当我使用这个 pb 文件运行推理时,在我提供的上面链接的相同图像上,输出始终为 0(即所有内容都被分类为“背景”)。一切都是黑色的!

我认为这是我如何导出或加载模型的问题,而不一定是模型本身的问题,因为运行 vis.py 脚本和我的自定义推理代码。也许我没有正确加载图表或初始化变量。或者也许我一开始就没有正确保存权重。任何帮助将不胜感激!

源代码

下面我提供了我的推理代码:

from deeplab.utils import get_dataset_colormap
from PIL import Image
import tensorflow as tf
import time
import matplotlib.pyplot as plt
import numpy as np
import cv2
import os
import glob


# tensorflow arguments
flags = tf.app.flags  # flag object for setup
FLAGS = flags.FLAGS   # object to access initialized flags
flags.DEFINE_string('frozen', None,
                    'The path/to/frozen.pb file.')

def _load_graph(frozen):
    print('Loading model `deeplabv3_graph` into memory from',frozen)
    with tf.gfile.GFile(frozen, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(
            graph_def, 
            input_map=None, 
            return_elements=None, 
            name="", 
            op_dict=None, 
            producer_op_list=None
        )
    return graph

def _run_inferences(sess, image, title):
    batch_seg_map = sess.run('SemanticPredictions:0',
        feed_dict={'ImageTensor:0': [np.asarray(image)]})
    semantic_prediction = get_dataset_colormap.label_to_color_image(batch_seg_map[0],
        dataset=get_dataset_colormap.__PRDL3_V1).astype(np.uint8)
    plt.imshow(semantic_prediction)
    plt.axis('off')
    plt.title(title)
    plt.show()


def main(argv):
    # initialize model
    frozen = os.path.normpath(FLAGS.frozen)
    assert os.path.isfile(frozen)
    graph = _load_graph(frozen)

    # open graph resource and begin inference in-loop
    with tf.Session(graph=graph) as sess:
        for img_path in glob.glob('*.png'):
            img = Image.open(img_path).convert('RGB')
            _run_inferences(sess, img, img_path)

if __name__ == '__main__':
    flags.mark_flag_as_required('frozen')
    tf.app.run()  # call the main() function

下面是我使用提供的 export_model.py 脚本导出模型的代码。

python export_model.py \
--logtostderr \
--atrous_rates=6 \
--atrous_rates=12 \
--atrous_rates=18 \
--output_stride=16 \
--checkpoint_path="/path/to/.../model.ckpt-32245" \
--export_path="/path/to/.../frozen_4_11_19.pb" \
--model_variant="xception_65" \
--num_classes=4 \
--crop_size=481 \
--crop_size=641 \
--inference_scales=1.0

系统信息

  • 您正在使用的模型的顶级目录是什么:deeplab
  • 我是否编写了自定义代码(而不是使用 TensorFlow 中提供的常用示例脚本):是
  • 操作系统平台和发行版(例如 Linux Ubuntu 16.04):Windows 10 Enterprise
  • TensorFlow 安装自(源代码或二进制文件):二进制文件
  • TensorFlow 版本(使用下面的命令):1.12.0
  • Bazel 版本(如果从源代码编译):N/A
  • CUDA/cuDNN 版本:9
  • GPU 型号和内存:NVIDIA Quadro M4000,8GB
  • 重现的确切命令:不适用

最佳答案

使用以下标志运行导出模型脚本

python deeplab/export_model.py \
--logtostderr \
--model_variant="xception_65" \
--atrous_rates=6 \
--atrous_rates=12 \
--atrous_rates=18 \
--output_stride=16 \
--num_classes=2 \
--decoder_output_stride=4 \
--crop_size=<<crop width>> \
--crop_size=<<crop height>> \
--dataset="shoes" \
--checkpoint_path="<<Checkpoint path>>" \
--export_path="<<Output frozen graph path>>" \

关于python - Deeplab - 经过训练的 Deeplab 模型的推理与可视化性能不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55717884/

相关文章:

python - 在 python 3 中解码 base64 字符串(是否使用 lxml)

android - Tensorflow-lite - 从量化模型输出中获取位图

tensorflow - 为什么 DeepLabV3+ 生成的所有图像都变成黑色?

python - Django 查询集 : Check if parent model has a child/referring model

php - 通过套接字从 PHP 向 Python 发送消息

python - 如何在Python中从json api获取特定数据

python - Tensorflow LSTM 像素级分类

python-3.x - tf.gradients() 是如何工作的?

tensorflow - 在 GPU 支持下对高维数据进行更快的 Kmeans 聚类

tensorflow - Deeplab 我的自定义数据集的权重标准是什么?