Skip to content

Commit 8f23155

Browse files
committed
Merge pull request #1249 from dkurt:update_halide_pipeline
2 parents 62b781b + 37cf497 commit 8f23155

File tree

4 files changed

+16
-39
lines changed

4 files changed

+16
-39
lines changed

modules/dnn/perf/perf_halide_net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static void loadNet(std::string weights, std::string proto, std::string schedule
3939
else
4040
CV_Error(Error::StsNotImplemented, "Unknown framework " + framework);
4141

42-
net->setInput(blobFromImage(input, 1.0, false));
42+
net->setInput(blobFromImage(input, 1.0, Size(), Scalar(), false));
4343
net->setPreferableBackend(DNN_BACKEND_HALIDE);
4444
net->setPreferableTarget(targetId);
4545
net->setHalideScheduler(scheduler);
@@ -52,7 +52,7 @@ static void loadNet(std::string weights, std::string proto, std::string schedule
5252
PERF_TEST(GoogLeNet, HalidePerfTest)
5353
{
5454
Net net;
55-
loadNet("dnn/bvlc_googlenet2.caffemodel", "dnn/bvlc_googlenet.prototxt",
55+
loadNet("dnn/bvlc_googlenet.caffemodel", "dnn/bvlc_googlenet.prototxt",
5656
"", 227, 227, "prob", "caffe", DNN_TARGET_CPU, &net);
5757
TEST_CYCLE() net.forward();
5858
SANITY_CHECK_NOTHING();

modules/dnn/src/layers/batch_norm_layer.cpp

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -167,32 +167,9 @@ class BatchNormLayerImpl : public BatchNormLayer
167167
Halide::Func top = (name.empty() ? Halide::Func() : Halide::Func(name));
168168
Halide::Var x("x"), y("y"), c("c"), n("n");
169169

170-
const int weightsBlobIndex = 2;
171-
const int biasBlobIndex = weightsBlobIndex + hasWeights;
172-
const int numChannels = blobs[0].total();
173-
float* meanData = (float*)blobs[0].data;
174-
float* stdData = (float*)blobs[1].data;
175-
float* weightsData = (hasWeights ? (float*)blobs[weightsBlobIndex].data : NULL);
176-
float* biasData = (hasBias ? (float*)blobs[biasBlobIndex].data : NULL);
177-
178-
float varMeanScale = 1.f;
179-
if (!hasWeights && !hasBias) {
180-
varMeanScale = *blobs[2].ptr<float>();
181-
if (varMeanScale != 0)
182-
varMeanScale = 1/varMeanScale;
183-
}
184-
185-
Halide::Buffer<float> weights(numChannels);
186-
Halide::Buffer<float> bias(numChannels);
187-
for (int i = 0; i < numChannels; ++i)
188-
{
189-
weights(i) = (hasWeights ? weightsData[i] : 1.0f) /
190-
sqrt(stdData[i] * varMeanScale + epsilon);
191-
bias(i) = (hasBias ? biasData[i] : 0.0f) -
192-
weights(i) * meanData[i] * varMeanScale;
193-
}
194-
weights.set_host_dirty();
195-
bias.set_host_dirty();
170+
const int numChannels = weights_.total();
171+
auto weights = wrapToHalideBuffer(weights_, {numChannels});
172+
auto bias = wrapToHalideBuffer(bias_, {numChannels});
196173
top(x, y, c, n) = input * weights(c) + bias(c);
197174
return top;
198175
}

modules/dnn/src/layers/convolution_layer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ class ConvolutionLayerImpl : public BaseConvolutionLayerImpl
625625
{
626626
// prepare weightsMat where each row is aligned and has enough zero padding on the right to
627627
// use vectorized (i.e. with intrinsics) loops without tail processing
628-
Mat wm = blobs[0].reshape(1, outCn);
628+
Mat wm = blobs[0].reshape(1, outCn).clone();
629629
if( wm.step1() % VEC_ALIGN != 0 )
630630
{
631631
int newcols = (int)alignSize(wm.step1(), VEC_ALIGN);

modules/dnn/test/test_halide_nets.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void loadNet(const std::string& weights, const std::string& proto,
3434
static void test(const std::string& weights, const std::string& proto,
3535
const std::string& scheduler, int inWidth, int inHeight,
3636
const std::string& outputLayer, const std::string& framework,
37-
int targetId)
37+
int targetId, double l1 = 1e-5, double lInf = 1e-4)
3838
{
3939
Mat input(inHeight, inWidth, CV_32FC3), outputDefault, outputHalide;
4040
randu(input, 0.0f, 1.0f);
@@ -43,23 +43,23 @@ static void test(const std::string& weights, const std::string& proto,
4343
loadNet(weights, proto, framework, &netDefault);
4444
loadNet(weights, proto, framework, &netHalide);
4545

46-
netDefault.setInput(blobFromImage(input.clone(), 1.0f, false));
46+
netDefault.setInput(blobFromImage(input.clone(), 1.0f, Size(), Scalar(), false));
4747
outputDefault = netDefault.forward(outputLayer).clone();
4848

49-
netHalide.setInput(blobFromImage(input.clone(), 1.0f, false));
49+
netHalide.setInput(blobFromImage(input.clone(), 1.0f, Size(), Scalar(), false));
5050
netHalide.setPreferableBackend(DNN_BACKEND_HALIDE);
5151
netHalide.setPreferableTarget(targetId);
5252
netHalide.setHalideScheduler(scheduler);
5353
outputHalide = netHalide.forward(outputLayer).clone();
5454

55-
normAssert(outputDefault, outputHalide);
55+
normAssert(outputDefault, outputHalide, "First run", l1, lInf);
5656

5757
// An extra test: change input.
5858
input *= 0.1f;
59-
netDefault.setInput(blobFromImage(input.clone(), 1.0, false));
60-
netHalide.setInput(blobFromImage(input.clone(), 1.0, false));
59+
netDefault.setInput(blobFromImage(input.clone(), 1.0, Size(), Scalar(), false));
60+
netHalide.setInput(blobFromImage(input.clone(), 1.0, Size(), Scalar(), false));
6161

62-
normAssert(outputDefault, outputHalide);
62+
normAssert(outputDefault, outputHalide, "Second run", l1, lInf);
6363

6464
// Swap backends.
6565
netHalide.setPreferableBackend(DNN_BACKEND_DEFAULT);
@@ -71,7 +71,7 @@ static void test(const std::string& weights, const std::string& proto,
7171
netDefault.setHalideScheduler(scheduler);
7272
outputHalide = netDefault.forward(outputLayer).clone();
7373

74-
normAssert(outputDefault, outputHalide);
74+
normAssert(outputDefault, outputHalide, "Swap backends", l1, lInf);
7575
}
7676

7777
////////////////////////////////////////////////////////////////////////////////
@@ -119,7 +119,7 @@ TEST(Reproducibility_ENet_Halide, Accuracy)
119119
{
120120
test(findDataFile("dnn/Enet-model-best.net", false), "",
121121
findDataFile("dnn/halide_scheduler_enet.yml", false),
122-
512, 512, "l367_Deconvolution", "torch", DNN_TARGET_CPU);
122+
512, 512, "l367_Deconvolution", "torch", DNN_TARGET_CPU, 2e-5, 0.15);
123123
};
124124
////////////////////////////////////////////////////////////////////////////////
125125
// OpenCL target
@@ -166,7 +166,7 @@ TEST(Reproducibility_ENet_Halide_opencl, Accuracy)
166166
{
167167
test(findDataFile("dnn/Enet-model-best.net", false), "",
168168
findDataFile("dnn/halide_scheduler_opencl_enet.yml", false),
169-
512, 512, "l367_Deconvolution", "torch", DNN_TARGET_OPENCL);
169+
512, 512, "l367_Deconvolution", "torch", DNN_TARGET_OPENCL, 2e-5, 0.14);
170170
};
171171
#endif // HAVE_HALIDE
172172

0 commit comments

Comments
 (0)