未验证 提交 f6139c05 编写于 作者: G Guanghua Yu 提交者: GitHub

fix static trt_int8 inference (#3001)

上级 1c582c26
......@@ -158,7 +158,9 @@ class Checkpointer(Callback):
if dist.get_world_size() < 2 or dist.get_rank() == 0:
if mode == 'train':
end_epoch = self.model.cfg.epoch
if epoch_id % self.model.cfg.snapshot_epoch == 0 or epoch_id == end_epoch - 1:
if (
epoch_id + 1
) % self.model.cfg.snapshot_epoch == 0 or epoch_id == end_epoch - 1:
save_name = str(
epoch_id) if epoch_id != end_epoch - 1 else "model_final"
weight = self.weight
......
......@@ -58,11 +58,12 @@ class ObjectDetector {
explicit ObjectDetector(const std::string& model_dir,
bool use_gpu=false,
const std::string& run_mode="fluid",
const int gpu_id=0) {
const int gpu_id=0,
bool trt_calib_mode=false) {
config_.load_config(model_dir);
threshold_ = config_.draw_threshold_;
preprocessor_.Init(config_.preprocess_info_, config_.arch_);
LoadModel(model_dir, use_gpu, config_.min_subgraph_size_, 1, run_mode, gpu_id);
LoadModel(model_dir, use_gpu, config_.min_subgraph_size_, 1, run_mode, gpu_id, trt_calib_mode);
}
// Load Paddle inference model
......@@ -72,7 +73,8 @@ class ObjectDetector {
const int min_subgraph_size,
const int batch_size = 1,
const std::string& run_mode = "fluid",
const int gpu_id=0);
const int gpu_id=0,
bool trt_calib_mode=false);
// Run predictor
void Predict(const cv::Mat& im,
......
......@@ -43,6 +43,7 @@ DEFINE_int32(camera_id, -1, "Device id of camera to predict");
DEFINE_bool(run_benchmark, false, "Whether to predict a image_file repeatedly for benchmark");
DEFINE_double(threshold, 0.5, "Threshold of score.");
DEFINE_string(output_dir, "output", "Directory of output visualization files.");
DEFINE_bool(trt_calib_mode, false, "If the model is produced by TRT offline quantitative calibration, trt_calib_mode need to set True");
static std::string DirName(const std::string &filepath) {
auto pos = filepath.rfind(OS_PATH_SEP);
......@@ -206,7 +207,7 @@ int main(int argc, char** argv) {
// Load model and create a object detector
PaddleDetection::ObjectDetector det(FLAGS_model_dir, FLAGS_use_gpu,
FLAGS_run_mode, FLAGS_gpu_id);
FLAGS_run_mode, FLAGS_gpu_id, FLAGS_trt_calib_mode);
// Do inference on input video or image
if (!FLAGS_video_path.empty() || FLAGS_use_camera) {
PredictVideo(FLAGS_video_path, &det);
......
......@@ -25,7 +25,8 @@ void ObjectDetector::LoadModel(const std::string& model_dir,
const int min_subgraph_size,
const int batch_size,
const std::string& run_mode,
const int gpu_id) {
const int gpu_id,
bool trt_calib_mode) {
paddle::AnalysisConfig config;
std::string prog_file = model_dir + OS_PATH_SEP + "__model__";
std::string params_file = model_dir + OS_PATH_SEP + "__params__";
......@@ -33,14 +34,12 @@ void ObjectDetector::LoadModel(const std::string& model_dir,
if (use_gpu) {
config.EnableUseGpu(100, gpu_id);
config.SwitchIrOptim(true);
bool use_calib_mode = false;
if (run_mode != "fluid") {
auto precision = paddle::AnalysisConfig::Precision::kFloat32;
if (run_mode == "trt_fp16") {
precision = paddle::AnalysisConfig::Precision::kHalf;
} else if (run_mode == "trt_int8") {
precision = paddle::AnalysisConfig::Precision::kInt8;
use_calib_mode = true;
} else {
printf("run_mode should be 'fluid', 'trt_fp32', 'trt_fp16' or 'trt_int8'");
}
......@@ -50,7 +49,7 @@ void ObjectDetector::LoadModel(const std::string& model_dir,
min_subgraph_size,
precision,
false,
use_calib_mode);
trt_calib_mode);
}
} else {
config.DisableGpu();
......
......@@ -65,7 +65,8 @@ class Detector(object):
model_dir,
use_gpu=False,
run_mode='fluid',
threshold=0.5):
threshold=0.5,
trt_calib_mode=False):
self.config = config
if self.config.use_python_inference:
self.executor, self.program, self.fecth_targets = load_executor(
......@@ -75,7 +76,8 @@ class Detector(object):
model_dir,
run_mode=run_mode,
min_subgraph_size=self.config.min_subgraph_size,
use_gpu=use_gpu)
use_gpu=use_gpu,
trt_calib_mode=trt_calib_mode)
def preprocess(self, im):
preprocess_ops = []
......@@ -221,13 +223,15 @@ class DetectorSOLOv2(Detector):
model_dir,
use_gpu=False,
run_mode='fluid',
threshold=0.5):
threshold=0.5,
trt_calib_mode=False):
super(DetectorSOLOv2, self).__init__(
config=config,
model_dir=model_dir,
use_gpu=use_gpu,
run_mode=run_mode,
threshold=threshold)
threshold=threshold,
trt_calib_mode=trt_calib_mode)
def predict(self,
image,
......@@ -379,11 +383,14 @@ def load_predictor(model_dir,
run_mode='fluid',
batch_size=1,
use_gpu=False,
min_subgraph_size=3):
min_subgraph_size=3,
trt_calib_mode=False):
"""set AnalysisConfig, generate AnalysisPredictor
Args:
model_dir (str): root path of __model__ and __params__
use_gpu (bool): whether use gpu
trt_calib_mode (bool): If the model is produced by TRT offline quantitative
calibration, trt_calib_mode need to set True
Returns:
predictor (PaddlePredictor): AnalysisPredictor
Raises:
......@@ -393,7 +400,6 @@ def load_predictor(model_dir,
raise ValueError(
"Predict by TensorRT mode: {}, expect use_gpu==True, but use_gpu == {}"
.format(run_mode, use_gpu))
use_calib_mode = True if run_mode == 'trt_int8' else False
precision_map = {
'trt_int8': fluid.core.AnalysisConfig.Precision.Int8,
'trt_fp32': fluid.core.AnalysisConfig.Precision.Float32,
......@@ -417,7 +423,7 @@ def load_predictor(model_dir,
min_subgraph_size=min_subgraph_size,
precision_mode=precision_map[run_mode],
use_static=False,
use_calib_mode=use_calib_mode)
use_calib_mode=trt_calib_mode)
# disable print log when predict
config.disable_glog_info()
......@@ -500,7 +506,7 @@ def predict_video(detector, camera_id):
fps = 30
width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fourcc = cv2.VideoWriter_fourcc(* 'mp4v')
if not os.path.exists(FLAGS.output_dir):
os.makedirs(FLAGS.output_dir)
out_path = os.path.join(FLAGS.output_dir, video_name)
......@@ -531,13 +537,18 @@ def predict_video(detector, camera_id):
def main():
config = Config(FLAGS.model_dir)
detector = Detector(
config, FLAGS.model_dir, use_gpu=FLAGS.use_gpu, run_mode=FLAGS.run_mode)
config,
FLAGS.model_dir,
use_gpu=FLAGS.use_gpu,
run_mode=FLAGS.run_mode,
trt_calib_mode=FLAGS.trt_calib_mode)
if config.arch == 'SOLOv2':
detector = DetectorSOLOv2(
config,
FLAGS.model_dir,
use_gpu=FLAGS.use_gpu,
run_mode=FLAGS.run_mode)
run_mode=FLAGS.run_mode,
trt_calib_mode=FLAGS.trt_calib_mode)
# predict from image
if FLAGS.image_file != '':
predict_image(detector)
......@@ -590,6 +601,12 @@ if __name__ == '__main__':
type=str,
default="output",
help="Directory of output visualization files.")
parser.add_argument(
"--trt_calib_mode",
type=bool,
default=False,
help="If the model is produced by TRT offline quantitative "
"calibration, trt_calib_mode need to set True.")
FLAGS = parser.parse_args()
print_arguments(FLAGS)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册