【目標檢測】FPN 移植到windows上

參考:https://blog.csdn.net/qq_39407300/article/details/92646342

第一步:下載所需文件

git clone https://github.com/DetectionTeamUCAS/FPN_Tensorflow.git

git clone https://github.com/morpheusthewhite/Faster-RCNN-TensorFlow-Python3.5.git
第二步:修改“tf_faster_rcnn”的配置方式

1:首先配置一些基本的依賴:

pip install Cython

pip install easydict

pip install tensorflow-plot

2:進入到 “Faster-RCNN-TensorFlow-Python3.5/data/coco/PythonAPI”文件下,打開終端執行以下命令:

python setup.py build_ext  --inplace

python setup.py build_ext install

3:進入到 “Faster-RCNN-TensorFlow-Python3.5/lib/utils”文件下,打開終端執行以下命令

python setup.py build_ext  --inplace
4:將整個“utils/”文件夾中的所有文件複製到“FPN_Tensorflow-master/libs/box_utils/cython_utils/”文件夾內,提醒是否替換的話,選擇替換即可。

 

Q:遇到的問題:cl : Command line error D8021 : invalid numeric argument '/Wno-cpp'

參考:https://github.com/cocodataset/cocoapi/issues/51
環境:win7,python3.6.2
安裝pycocotools時的問題

執行 python setup.py build_ext --inplace 報錯如下:

cl : Command line error D8021 : invalid numeric argument '/Wno-cpp'
error: command 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe' failed with exit status 2 
解決辦法:
先下載cocoapi 下載鏈接
將cocoapi-master\PythonAPI中的setup.py中的第12行

extra_compile_args=['-Wno-cpp', '-Wno-unused-function', '-std=c99'],
改爲

extra_compile_args={'gcc': ['/Qstd=c99']},
然後執行

python setup.py build_ext --inplace
編譯好的文件如下

python setup.py build_ext install

 

Tricks:

1.ValueError: No variables to save

方法2-不需要重新定義-直接讀取
如果不重新定義變量,可以使用tf.train.import_meta_graph

import tensorflow as tf
import numpy as np
 
with tf.Session() as sess:
    new_saver = tf.train.import_meta_graph('my_net/save_net.ckpt.meta')
    new_saver.restore(sess, 'my_net/save_net.ckpt')
 

2.更改搜索框的數量,提高運行速度:

lib/fast_rcnn/config.py文件中C.TEST.RPN_POST_NMS_TOP_N = 2000,可以根據實際情況在不影響準確率的情況下修改,在faster rcnn中該參數爲100,可以減少該參數的值測試準確率,如果沒有太大影響可以降低以提升速度。

實踐證明,減少搜索框不僅縮短時間,準確率也有大幅提升。現在的準確率已經來到了百分之九十幾。

3.inference初始化一次模型

# -*- coding:utf-8 -*-

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os, sys
import tensorflow as tf
import time
import cv2
import argparse
import numpy as np
#sys.path.append("../")
from os import path
sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )
print(sys.path)

from data.io.image_preprocess import short_side_resize_for_inference_data
from libs.configs import cfgs
from libs.networks import build_whole_network
from libs.box_utils import draw_box_in_img
from help_utils import tools

class Inference_stain:
    
    def __init__(self): 
        faster_rcnn = build_whole_network.DetectionNetwork(base_network_name=cfgs.NET_NAME,
                    is_training=False)
        self.det_net=faster_rcnn        
         # 1. preprocess img
        self.img_plac = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3])  # is RGB. not GBR
        self.img_batch = tf.cast(self.img_plac, tf.float32)
        self.img_batch = short_side_resize_for_inference_data(img_tensor=self.img_batch,
                                                         target_shortside_len=cfgs.IMG_SHORT_SIDE_LEN,
                                                         length_limitation=cfgs.IMG_MAX_LENGTH)
        self.img_batch = self.img_batch - tf.constant(cfgs.PIXEL_MEAN)
        self.img_batch = tf.expand_dims(self.img_batch, axis=0) # [1, None, None, 3]
    
        self.detection_boxes, self.detection_scores, self.detection_category = self.det_net.build_whole_detection_network(
            input_img_batch=self.img_batch,
            gtboxes_batch=None)      
        
        self.config = tf.ConfigProto()
        self.config.gpu_options.allow_growth = True         
        self.init_op = tf.group(
                tf.global_variables_initializer(),
                tf.local_variables_initializer()
        )

        self.checkpoint_path = tf.train.latest_checkpoint('models')
        print('checkpoint_path:',self.checkpoint_path)
        self.sess = tf.Session(config=self.config)
        self.sess.run(self.init_op)
        restorer = tf.train.Saver()
        if not restorer is None:
            restorer.restore(self.sess, self.checkpoint_path)
            print('restore model')
            

    def detect(self, raw_img):
    
#        with tf.Session(config=self.config) as sess:
#            restorer = tf.train.Saver()
#            print('restorer:',restorer)
#            if not restorer is None:
#                restorer.restore(sess, self.checkpoint_path)
#                print('restore model')
#    
        resized_img, detected_boxes, detected_scores, detected_categories = \
            self.sess.run(
                [self.img_batch, self.detection_boxes, self.detection_scores, self.detection_category],
                feed_dict={self.img_plac: raw_img[:, :, ::-1]}  # cv is BGR. But need RGB
            )
        # print("{} cost time : {} ".format(img_name, (end - start)))
        print('raw_img.shape,resized_img.shape:',raw_img.shape,resized_img.shape)
        ratio = raw_img.shape[0]/resized_img.shape[1]
        print('ratio:',ratio)
        show_indices = detected_scores >= cfgs.SHOW_SCORE_THRSHOLD
        show_scores = detected_scores[show_indices]
        show_boxes = detected_boxes[show_indices]
        print('show_boxes.shape:',show_boxes.shape)
#            print('show_boxes:',show_boxes)
        print('show_boxes*ratio:',show_boxes*ratio)
        show_boxes = show_boxes*ratio
        return show_boxes
    
    
#    def inference(self,img_name):
#    
#
#        detect(img_name=img_name)



if __name__ == '__main__':

    os.environ["CUDA_VISIBLE_DEVICES"] = '0' 
    img_name = './2008-MS-0169-B01-0006.jpg'
    raw_img = cv2.imread(img_name)
    Inference_stain_exe = Inference_stain()
    show_boxes = Inference_stain_exe.detect(raw_img=raw_img)

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章