Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 3f494b4

Browse files
committed
Revert fix the bug of SSD NMS Noise raised by community into master_clean
1 parent f259210 commit 3f494b4

File tree

5 files changed

+54
-19
lines changed

5 files changed

+54
-19
lines changed

examples/faster-rcnn/tools/test_net.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,19 @@ def parse_args():
7777

7878
parser.add_argument('-wi', '--conv_algo', dest='conv_algo', action="store_true", default=False,
7979
help='to choose the convolution algorithm')
80+
8081
parser.add_argument('-1st', '--enable_1st_conv_layer', dest='enable_1st_conv_layer', action="store_true", default=False,
8182
help='enable 1st conv layer')
83+
84+
parser.add_argument('-fc', '--fc_int8', dest='fc_int8', action="store_true", default=False,
85+
help='enable int8 fc layer')
86+
87+
parser.add_argument('-uff', '--disable_force_fp32', dest='disable_force_fp32', action="store_true", default=False,
88+
help='to disable force fp32 output in conv/fc + fp32')
89+
90+
parser.add_argument('-ucac', '--disable_cac_unify', dest='disable_cac_unify', action="store_true", default=False,
91+
help='to disable scale unify in conv/fc + avg pooling + conv/fc')
92+
8293
if len(sys.argv) == 1:
8394
parser.print_help()
8495
sys.exit(1)
@@ -119,6 +130,8 @@ def parse_args():
119130
if args.quantized_prototxt == None:
120131
test_net(net, imdb, max_per_image=args.max_per_image, vis=args.vis)
121132
else:
133+
if args.fc_int8:
134+
calibrator.enable_fc_int8()
122135
(blobs, params, top_blobs_map, bottom_blobs_map, conv_top_blob_layer_map, conv_bottom_blob_layer_map, winograd_bottoms, winograd_convolutions) = sample_net(args.prototxt, net, imdb, args.sample_iters, args.quant_mode, args.enable_1st_conv_layer)
123136

124137
(inputs_max, outputs_max, inputs_min) = sampling.calibrate_activations(blobs, conv_top_blob_layer_map, conv_bottom_blob_layer_map, winograd_bottoms, args.calibration_algos, "SINGLE", args.conv_algo)
@@ -130,3 +143,7 @@ def parse_args():
130143
with open(compile_net_path, "w") as f:
131144
f.write(compiled_net_str)
132145
calibrator.transform_convolutions(args.quantized_prototxt, compile_net_path, top_blobs_map, bottom_blobs_map, args.unsigned_range, args.concat_use_fp32, args.unify_concat_scales, args.conv_algo, args.enable_1st_conv_layer)
146+
if not args.disable_force_fp32:
147+
calibrator.force_fp32_opt(args.quantized_prototxt)
148+
if not args.disable_cac_unify:
149+
calibrator.cac_opt(args.quantized_prototxt)

examples/rfcn/tools/test_net.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,19 @@ def parse_args():
8282

8383
parser.add_argument('-wi', '--conv_algo', dest='conv_algo', action="store_true", default=False,
8484
help='to choose the convolution algorithm')
85+
8586
parser.add_argument('-1st', '--enable_1st_conv_layer', dest='enable_1st_conv_layer', action="store_true", default=False,
8687
help='enable 1st conv layer')
88+
89+
parser.add_argument('-fc', '--fc_int8', dest='fc_int8', action="store_true", default=False,
90+
help='enable int8 fc layer')
91+
92+
parser.add_argument('-uff', '--disable_force_fp32', dest='disable_force_fp32', action="store_true", default=False,
93+
help='to disable force fp32 output in conv/fc + fp32')
94+
95+
parser.add_argument('-ucac', '--disable_cac_unify', dest='disable_cac_unify', action="store_true", default=False,
96+
help='to disable scale unify in conv/fc + avg pooling + conv/fc')
97+
8798
if len(sys.argv) == 1:
8899
parser.print_help()
89100
sys.exit(1)
@@ -126,6 +137,8 @@ def parse_args():
126137
if args.quantized_prototxt == None:
127138
test_net(net, imdb, max_per_image=args.max_per_image, vis=args.vis)
128139
else:
140+
if args.fc_int8:
141+
calibrator.enable_fc_int8()
129142
(blobs, params, top_blobs_map, bottom_blobs_map, conv_top_blob_layer_map, conv_bottom_blob_layer_map, winograd_bottoms, winograd_convolutions) = sample_net(args.prototxt, net, imdb, args.sample_iters, args.quant_mode, args.enable_1st_conv_layer)
130143

131144
(inputs_max, outputs_max, inputs_min) = sampling.calibrate_activations(blobs, conv_top_blob_layer_map, conv_bottom_blob_layer_map, winograd_bottoms, args.calibration_algos, "SINGLE", args.conv_algo)
@@ -137,3 +150,7 @@ def parse_args():
137150
with open(compile_net_path, "w") as f:
138151
f.write(compiled_net_str)
139152
calibrator.transform_convolutions(args.quantized_prototxt, compile_net_path, top_blobs_map, bottom_blobs_map, args.unsigned_range, args.concat_use_fp32, args.unify_concat_scales, args.conv_algo, args.enable_1st_conv_layer)
153+
if not args.disable_force_fp32:
154+
calibrator.force_fp32_opt(args.quantized_prototxt)
155+
if not args.disable_cac_unify:
156+
calibrator.cac_opt(args.quantized_prototxt)

scripts/calibrator.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,11 @@ def force_fp32_opt(quantized_prototxt):
471471
if base_net.layer[index].top[0] in layer_bottom_name_map.keys():
472472
bottom_layer_indexes=layer_bottom_name_map[base_net.layer[index].top[0]]
473473
for bottom_layer_index in bottom_layer_indexes:
474-
if base_net.layer[bottom_layer_index].type in int8_layers:
474+
next_layer = base_net.layer[bottom_layer_index]
475+
if next_layer.top == next_layer.bottom and next_layer.type not in int8_layers:
476+
force_fp32 = True
477+
break
478+
if next_layer.type in int8_layers:
475479
force_fp32 = False
476480
if force_fp32 or index == np.max(quantize_layers_indexes):
477481
new_net_index=find_index_by_name(base_net.layer[index].name, layer_infos)
@@ -565,6 +569,14 @@ def cac_opt(quantized_prototxt):
565569
f.write(str(new_net))
566570
print('cac opt done')
567571

572+
def enable_fc_int8():
573+
local_q = quantize_layers + ["InnerProduct"]
574+
local_i = int8_layers + ["InnerProduct"]
575+
global quantize_layers
576+
global int8_layers
577+
quantize_layers = local_q
578+
int8_layers = local_i
579+
568580

569581
if __name__ == '__main__':
570582
usage_string = 'Usage: 1.Build the caffe\n ' \
@@ -712,8 +724,7 @@ def cac_opt(quantized_prototxt):
712724
user_conv_algo = params.conv_algo
713725

714726
if params.fc_int8:
715-
quantize_layers.append("InnerProduct")
716-
int8_layers.append("InnerProduct")
727+
enable_fc_int8()
717728

718729
try:
719730
toleration = float(params.loss)

src/caffe/layers/mkldnn_inner_product_layer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ void MKLDNNInnerProductLayer<Dtype>::InitInnerProductFwd(const vector<Blob<Dtype
303303
std::vector<float> scale_top(1);
304304
scale_top[0] = 1.0f;
305305
if(this->need_quantize_) scale_top = this->scale_out_;
306-
fwd_top_data.reset(new MKLDNNData<Dtype>(usr_top_data_memory_pd, prv_fwd_top_data_memory_pd, top[0], this));
306+
fwd_top_data.reset(new MKLDNNData<Dtype>(usr_top_data_memory_pd, prv_fwd_top_data_memory_pd, top[0], this, scale_top));
307307
fwd_top_data ->name = "fwd_top_data @ " + this->layer_param_.name();
308308
fwd_top_data_memory = fwd_top_data->create_output_memory();
309309

src/caffe/util/bbox_util.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,22 +2267,12 @@ void GetMaxScoreIndex(const vector<float>& scores, const float threshold,
22672267
const int top_k, vector<pair<float, int> >* score_index_vec) {
22682268
// Generate index score pairs.
22692269
#ifdef _OPENMP
2270-
#pragma omp parallel
2271-
#endif
2272-
{
2273-
vector<pair<float, int> > prv;
2274-
#ifdef _OPENMP
2275-
#pragma omp for nowait
2270+
#pragma omp parallel for
22762271
#endif
2277-
for (int i = 0; i < scores.size(); ++i) {
2278-
if (scores[i] > threshold) {
2279-
prv.push_back(std::make_pair(scores[i], i));
2280-
}
2272+
for (int i = 0; i < scores.size(); ++i) {
2273+
if (scores[i] > threshold) {
2274+
score_index_vec->at(i) = std::make_pair(scores[i], i);
22812275
}
2282-
#ifdef _OPENMP
2283-
#pragma omp critical
2284-
#endif
2285-
score_index_vec->insert(score_index_vec->end(), prv.begin(), prv.end());
22862276
}
22872277

22882278
// Sort the score pair according to the scores in descending order
@@ -2442,7 +2432,7 @@ void ApplyNMSFast(const vector<NormalizedBBox>& bboxes,
24422432
CHECK_EQ(bboxes.size(), scores.size())
24432433
<< "bboxes and scores have different size.";
24442434
// Get top_k scores (with corresponding indices).
2445-
vector<pair<float, int> > score_index_vec;
2435+
vector<pair<float, int> > score_index_vec(scores.size());
24462436
GetMaxScoreIndex(scores, score_threshold, top_k, &score_index_vec);
24472437
// Do nms.
24482438
float adaptive_threshold = nms_threshold;

0 commit comments

Comments
 (0)