Skip to content

Commit e231be8

Browse files
support flownet2 with arbitary input size
revise default proto to match the filename in documentations fix a bug beautify python codes fix bug beautify codes add test samples with larger/smaller size remove unless code using bytearray without creating tmp file remove useless codes
1 parent fe9ff64 commit e231be8

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

modules/dnn/src/layers/scale_layer.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Implementation of Scale layer.
1515
#include "../op_inf_engine.hpp"
1616
#include "../ie_ngraph.hpp"
1717

18+
#include <opencv2/imgproc.hpp>
1819
#include <opencv2/dnn/shape_utils.hpp>
1920

2021
namespace cv
@@ -324,7 +325,7 @@ class DataAugmentationLayerImpl CV_FINAL : public DataAugmentationLayer
324325
std::vector<MatShape> &internals) const CV_OVERRIDE
325326
{
326327
CV_Assert_N(inputs.size() == 1, blobs.size() == 3);
327-
CV_Assert_N(blobs[0].total() == 1, blobs[1].total() == total(inputs[0], 1),
328+
CV_Assert_N(blobs[0].total() == 1,
328329
blobs[2].total() == inputs[0][1]);
329330

330331
outputs.assign(1, inputs[0]);
@@ -347,15 +348,20 @@ class DataAugmentationLayerImpl CV_FINAL : public DataAugmentationLayer
347348
float* outData = outputs[0].ptr<float>();
348349

349350
Mat data_mean_cpu = blobs[1].clone();
351+
Mat mean_resize = Mat(inputs[0].size[3], inputs[0].size[2], CV_32FC3);
352+
Mat mean_3d = Mat(data_mean_cpu.size[3], data_mean_cpu.size[2], CV_32FC3, data_mean_cpu.ptr<float>(0));
353+
resize(mean_3d, mean_resize, Size(inputs[0].size[3], inputs[0].size[2]));
354+
int new_size[] = {1, mean_resize.channels(), mean_resize.cols, mean_resize.rows};
355+
Mat data_mean_cpu_resize = mean_resize.reshape(1, *new_size);
350356
Mat data_mean_per_channel_cpu = blobs[2].clone();
351357

352-
const int numWeights = data_mean_cpu.total();
358+
const int numWeights = data_mean_cpu_resize.total();
353359
CV_Assert(numWeights != 0);
354360

355361
++num_iter;
356362
if (num_iter <= recompute_mean)
357363
{
358-
data_mean_cpu *= (num_iter - 1);
364+
data_mean_cpu_resize *= (num_iter - 1);
359365
const int batch = inputs[0].size[0];
360366
float alpha = 1.0 / batch;
361367

@@ -364,15 +370,15 @@ class DataAugmentationLayerImpl CV_FINAL : public DataAugmentationLayer
364370
Mat inpSlice(1, numWeights, CV_32F, inpData);
365371
inpSlice = alpha * inpSlice;
366372

367-
add(data_mean_cpu.reshape(1, 1), inpSlice, data_mean_cpu.reshape(1, 1));
373+
add(data_mean_cpu_resize.reshape(1, 1), inpSlice, data_mean_cpu_resize.reshape(1, 1));
368374
inpData += numWeights;
369375
}
370-
data_mean_cpu *= (1.0 / num_iter);
376+
data_mean_cpu_resize *= (1.0 / num_iter);
371377

372-
int newsize[] = {blobs[1].size[1], (int)blobs[1].total(2)};
373-
reduce(data_mean_cpu.reshape(1, 2, &newsize[0]), data_mean_per_channel_cpu, 1, REDUCE_SUM, CV_32F);
378+
int newsize[] = {inputs[0].size[1], (int)inputs[0].total(2)};
379+
reduce(data_mean_cpu_resize.reshape(1, 2, &newsize[0]), data_mean_per_channel_cpu, 1, REDUCE_SUM, CV_32F);
374380

375-
int area = blobs[1].total(2);
381+
int area = inputs[0].total(2);
376382
data_mean_per_channel_cpu *= (1.0 / area);
377383
}
378384

@@ -387,7 +393,7 @@ class DataAugmentationLayerImpl CV_FINAL : public DataAugmentationLayer
387393
Mat inpSlice(1, numWeights, CV_32F, inpData);
388394
Mat outSlice(1, numWeights, CV_32F, outData);
389395

390-
add(inpSlice, (-1) * data_mean_cpu, outSlice);
396+
add(inpSlice, (-1) * data_mean_cpu_resize, outSlice);
391397
inpData += numWeights;
392398
outData += numWeights;
393399
}

modules/dnn/test/test_layers.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,8 @@ TEST_P(Test_Caffe_layers, DataAugmentation)
646646
if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
647647
applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
648648
testLayerUsingCaffeModels("data_augmentation", true, false);
649+
testLayerUsingCaffeModels("data_augmentation_2x1", true, false);
650+
testLayerUsingCaffeModels("data_augmentation_8x6", true, false);
649651
}
650652

651653
TEST_P(Test_Caffe_layers, Resample)

samples/dnn/optical_flow.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Original repo: https://github.com/lmb-freiburg/flownet2.
66
77
Download the converted .caffemodel model from https://drive.google.com/open?id=16qvE9VNmU39NttpZwZs81Ga8VYQJDaWZ
8-
and .prototxt from https://drive.google.com/open?id=19bo6SWU2p8ZKvjXqMKiCPdK8mghwDy9b.
8+
and .prototxt from https://drive.google.com/file/d/1RyNIUsan1ZOh2hpYIH36A-jofAvJlT6a/view?usp=sharing.
99
Otherwise download original model from https://lmb.informatik.uni-freiburg.de/resources/binaries/flownet2/flownet2-models.tar.gz,
1010
convert .h5 model to .caffemodel and modify original .prototxt using .prototxt from link above.
1111
'''
@@ -18,7 +18,7 @@
1818

1919
class OpticalFlow(object):
2020
def __init__(self, proto, model, height, width):
21-
self.net = cv.dnn.readNet(proto, model)
21+
self.net = cv.dnn.readNetFromCaffe(proto, model)
2222
self.net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
2323
self.height = height
2424
self.width = width
@@ -62,9 +62,9 @@ def motion_to_color(self, flow):
6262
parser = argparse.ArgumentParser(description='Use this script to calculate optical flow using FlowNetv2',
6363
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
6464
parser.add_argument('-input', '-i', required=True, help='Path to input video file. Skip this argument to capture frames from a camera.')
65-
parser.add_argument('--height', default=320, help='Input height')
66-
parser.add_argument('--width', default=448, help='Input width')
67-
parser.add_argument('--proto', '-p', default='FlowNet2_deploy.prototxt', help='Path to prototxt.')
65+
parser.add_argument('--height', default=320, type=int, help='Input height')
66+
parser.add_argument('--width', default=448, type=int, help='Input width')
67+
parser.add_argument('--proto', '-p', default='FlowNet2_deploy_anysize.prototxt', help='Path to prototxt.')
6868
parser.add_argument('--model', '-m', default='FlowNet2_weights.caffemodel', help='Path to caffemodel.')
6969
args, _ = parser.parse_known_args()
7070

@@ -75,7 +75,25 @@ def motion_to_color(self, flow):
7575
cv.namedWindow(winName, cv.WINDOW_NORMAL)
7676
cap = cv.VideoCapture(args.input if args.input else 0)
7777
hasFrame, first_frame = cap.read()
78-
opt_flow = OpticalFlow(args.proto, args.model, args.height, args.width)
78+
79+
divisor = 64.
80+
var = {}
81+
var['ADAPTED_WIDTH'] = int(np.ceil(args.width/divisor) * divisor)
82+
var['ADAPTED_HEIGHT'] = int(np.ceil(args.height/divisor) * divisor)
83+
var['SCALE_WIDTH'] = args.width / float(var['ADAPTED_WIDTH'])
84+
var['SCALE_HEIGHT'] = args.height / float(var['ADAPTED_HEIGHT'])
85+
86+
config = ''
87+
proto = open(args.proto).readlines()
88+
for line in proto:
89+
for key, value in var.items():
90+
tag = "$%s$" % key
91+
line = line.replace(tag, str(value))
92+
config += line
93+
94+
caffemodel = open(args.model, 'rb').read()
95+
96+
opt_flow = OpticalFlow(bytearray(config.encode()), caffemodel, var['ADAPTED_HEIGHT'], var['ADAPTED_WIDTH'])
7997
while cv.waitKey(1) < 0:
8098
hasFrame, second_frame = cap.read()
8199
if not hasFrame:

0 commit comments

Comments
 (0)