参考: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)