美文网首页影视处理
物体检测1:安装和验证对象检测API(TF1)

物体检测1:安装和验证对象检测API(TF1)

作者: 古风子 | 来源:发表于2020-02-21 21:38 被阅读0次
    tf

    阅读和验证该文章前:请先搭建tensorflow环境:
    tensorflow环境搭建

    TensorFlow对象检测API是一个建立在TensorFlow之上的开源框架,可以轻松构建,训练和部署对象检测模型。 github地址

    下载TensorFlow Model模型库

    TensorFlow 模型库包含了很多开源的模型,包括图像分类、检测、自然语言处理NLP、视频预测、图像理解等等,我们要学习的对象检测API也包括在这里面;

    首先,在Home目录创建tensorflow, 将代码clone到本地

    git clone https://github.com/tensorflow/models.git
    

    使用以上命令,直接拉去的是tensorflow的master分支,也就是最新的分支代码

    注意:你要使用与你的tensorflow版本对应的版本,例如你的tensorflow是1,那么你应该modle-r1.xx版本;如果你是使用的是tensorflow2,你应该使用modle-2.xx版本;所以,你应该根据你的tensorflow的版本,选择合适的物体检测API分支

    (base) jiadongfeng:~/tensorflow/models/research$ git branch -a
    * master
     
     ...
      remotes/origin/guptapriya-patch-6
     ...
    
     ...
      remotes/origin/r1.10.0
      remotes/origin/r1.11
      remotes/origin/r1.12.0
      remotes/origin/r1.13.0
      remotes/origin/r1.4.0
      remotes/origin/r1.5
      remotes/origin/r1.6.0
      remotes/origin/r1.7.0
      remotes/origin/r1.8.0
      remotes/origin/r1.8.1
      remotes/origin/r1.9.0
      remotes/origin/r2.1.0
      remotes/origin/r2.1_model_reference
      ...
    
    

    因为我们使用的tesorflow版本是1.14.0,因此model branch我们使用remotes/origin/r1.13.0

    //你的tensorflow必须是v1.9以上版本,以下是物体检测api中做的限制
    if StrictVersion(tf.__version__) < StrictVersion('1.9.0'):
      raise ImportError('Please upgrade your TensorFlow installation to v1.9.* or later!')
    

    Protobuf 编译

    Tensorflow Object Detection API 用 Protobufs 来配置模型和训练参数. 在用这个框架之前,必须先编译Protobuf 库,切换到这个目录下: tensorflow/models/research/

    cd ~/tensorflow/models/research/
    

    执行:

    protoc object_detection/protos/*.proto --python_out=.
    

    添加环境变量 PYTHONPATH

    • 方法1:
      tensorflow/models/research/ 和 slim 目录 需要添加到PYTHONPATH环境变量中. 从终端中,切换到tensorflow/models/research/目录,执行:
    # From tensorflow/models/research/
    export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
    

    注意: 这条命令在新打开的终端中需要重新执行一次才会在新终端中生效;

    • 方法2

    如果不想那么麻烦,就用下面的命令编辑 ~/.bashrc 文件,把上面的语句添加到末尾.

    gedit ~/.bashrc 
    
    • 方法3
      也可以在anaconda安装目录下的site-packages文件夹下建立一个tensorflow_model.pth文件,文件内容写models的路径,自己slim的路径,博主的路径是:
    /home/jiadongfeng/tensorflow/models/research
    /home/jiadongfeng/tensorflow/models/research/slim
    

    测试model

    执行,得到OK则说明安装成功:

    (base) jiadongfeng:~/tensorflow/models/research$ export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
    (base) jiadongfeng:~/tensorflow/models/research$ python object_detection/builders/model_builder_test.py
    WARNING:tensorflow:
    The TensorFlow contrib module will not be included in TensorFlow 2.0.
    For more information, please see:
      * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
      * https://github.com/tensorflow/addons
      * https://github.com/tensorflow/io (for I/O related ops)
    If you depend on functionality not listed there, please file an issue.
    
    Running tests under Python 2.7.16: /home/jiadongfeng/anaconda2/bin/python
    [ RUN      ] ModelBuilderTest.test_create_experimental_model
    [       OK ] ModelBuilderTest.test_create_experimental_model
    [ RUN      ] ModelBuilderTest.test_create_faster_rcnn_model_from_config_with_example_miner
    [       OK ] ModelBuilderTest.test_create_faster_rcnn_model_from_config_with_example_miner
    [ RUN      ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_faster_rcnn_with_matmul
    [       OK ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_faster_rcnn_with_matmul
    [ RUN      ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_faster_rcnn_without_matmul
    [       OK ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_faster_rcnn_without_matmul
    [ RUN      ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_mask_rcnn_with_matmul
    [       OK ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_mask_rcnn_with_matmul
    [ RUN      ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_mask_rcnn_without_matmul
    [       OK ] ModelBuilderTest.test_create_faster_rcnn_models_from_config_mask_rcnn_without_matmul
    [ RUN      ] ModelBuilderTest.test_create_rfcn_model_from_config
    [       OK ] ModelBuilderTest.test_create_rfcn_model_from_config
    [ RUN      ] ModelBuilderTest.test_create_ssd_fpn_model_from_config
    [       OK ] ModelBuilderTest.test_create_ssd_fpn_model_from_config
    [ RUN      ] ModelBuilderTest.test_create_ssd_models_from_config
    [       OK ] ModelBuilderTest.test_create_ssd_models_from_config
    [ RUN      ] ModelBuilderTest.test_invalid_faster_rcnn_batchnorm_update
    [       OK ] ModelBuilderTest.test_invalid_faster_rcnn_batchnorm_update
    [ RUN      ] ModelBuilderTest.test_invalid_first_stage_nms_iou_threshold
    [       OK ] ModelBuilderTest.test_invalid_first_stage_nms_iou_threshold
    [ RUN      ] ModelBuilderTest.test_invalid_model_config_proto
    [       OK ] ModelBuilderTest.test_invalid_model_config_proto
    [ RUN      ] ModelBuilderTest.test_invalid_second_stage_batch_size
    [       OK ] ModelBuilderTest.test_invalid_second_stage_batch_size
    [ RUN      ] ModelBuilderTest.test_session
    [  SKIPPED ] ModelBuilderTest.test_session
    [ RUN      ] ModelBuilderTest.test_unknown_faster_rcnn_feature_extractor
    [       OK ] ModelBuilderTest.test_unknown_faster_rcnn_feature_extractor
    [ RUN      ] ModelBuilderTest.test_unknown_meta_architecture
    [       OK ] ModelBuilderTest.test_unknown_meta_architecture
    [ RUN      ] ModelBuilderTest.test_unknown_ssd_feature_extractor
    [       OK ] ModelBuilderTest.test_unknown_ssd_feature_extractor
    ----------------------------------------------------------------------
    Ran 17 tests in 0.383s
    
    OK (skipped=1)
    
    

    运行demo

    进入/tensorflow/models/research/object_detection目录,运行:

    jupyter notebook
    

    一定要在object_detection目录,因为代码的数据读取和保存都是在这个目录的。model中的测试代码是从当前目录读取数据的。当然,你也可以修改代码,重新指定目录读取数据

    在jupyter中打开以下文件

    ~/tensorflow/models/research/object_detection/object_detection_tutorial.ipynb
    

    在window系统中,可以直接打开以下网址查看:

    https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md

    按照步骤,获取测试demo代码
    以上官方提供的demo,如果你完全按照代码拷贝,是运行不了的。需要稍微修改,以下为本地处理了各种报错的代码(代码较多,放到了单独的页面)

    tensorflow物体检测API完整demo

    运行结果

    image1_ret.png image2_ret.png

    报错处理

    1. TypeError: load() takes at least 3 arguments (1 given)
    error locaton:
    tf.saved_model.load(str(model_dir))
    TypeError: load() takes at least 3 arguments (1
    

    这个是tensorflow 版本和物体检测版本不对应引起的。

    如果你使用tensorflow1,使用model的r1版本,使用git checkout直接拉取即可

    
    (base) jiadongfeng:~/tensorflow/models/research$ git branch -a
    * master
    
      emotes/origin/r1.10.0
      remotes/origin/r1.11
      remotes/origin/r1.12.0
      remotes/origin/r1.13.0
      remotes/origin/r1.4.0
      remotes/origin/r1.5
      remotes/origin/r1.6.0
      remotes/origin/r1.7.0
      remotes/origin/r1.8.0
      remotes/origin/r1.8.1
      remotes/origin/r1.9.0
      remotes/origin/r2.1.0
      remotes/origin/r2.1_model_reference
    
    (base) jiadongfeng:~/tensorflow/models/research$ git checkout   remotes/origin/r1.13.0
    
    
    

    不要使用model的最新版本或者2.0版本,因为model的2.0版本有使用tensorflow1的接口,无论如果都编译不过

    当我们将tensorflow更新到v2时,提示tf.contrib 被 deprecated 。也就是说在最新的支持tensorflow2的物体检测api中,又使用了tensorflow1的接口。

    /home/jiadongfeng/anaconda2/lib/python2.7/site-packages/ipykernel_launcher.py:10: UserWarning:
    This call to matplotlib.use() has no effect because the backend has already
    been chosen; matplotlib.use() must be called before pylab, matplotlib.pyplot,
    or matplotlib.backends is imported for the first time.

    解决方案:在头部新增import matplotlib和matplotlib.use('Agg')

    #####import
    import numpy as np
    import os
    import six.moves.urllib as urllib
    import sys
    import tarfile
    import tensorflow as tf
    import zipfile
    
    #顺序不能反
    import matplotlib
    matplotlib.use('Agg')
    
    1. 运行物体检测Demo,无运行报错,但是不显示图片
      参考:
      https://blog.csdn.net/weixin_43345331/article/details/87924551
      vis_util.visualize_boxes_and_labels_on_image_array(
          image_np,
          output_dict['detection_boxes'],
          output_dict['detection_classes'],
          output_dict['detection_scores'],
          category_index,
          instance_masks=output_dict.get('detection_masks'),
          use_normalized_coordinates=True,
          line_thickness=8)
      plt.figure(figsize=IMAGE_SIZE)
      plt.imshow(image_np)
    

    plt.imshow不显示图片,参照晚上调用plt.show()也不行,也没有报错,只能通过以下方式将处理结果保存下来查看

     plt.figure(figsize=IMAGE_SIZE)
      plt.imshow(image_np)
      pylab.show()
      //在当前目录保存处理后的图片
      plt.savefig(image_path.split('.')[0]+'_ret')
    

    相关文章

      网友评论

        本文标题:物体检测1:安装和验证对象检测API(TF1)

        本文链接:https://www.haomeiwen.com/subject/afqfqhtx.html