未验证 提交 e970eccb 编写于 作者: L Liubov Batanina 提交者: GitHub

Merge pull request #16472 from l-bat:cp_vton

Add CP-VTON sample

* Support resize from PyTorch

* Add CP-VTON sample

* Fix downsampling

* Fix test

* Add model links

* Add default args

* Speed up resize

* Fix TOM link

* Add default args

* Fix comments

* Set aspect ratio for input

* Update links

* Check files exist
上级 5ad9e5f9
......@@ -35,7 +35,7 @@ public:
CV_Assert(params.has("zoom_factor_x") && params.has("zoom_factor_y"));
}
interpolation = params.get<String>("interpolation");
CV_Assert(interpolation == "nearest" || interpolation == "bilinear");
CV_Assert(interpolation == "nearest" || interpolation == "opencv_linear" || interpolation == "bilinear");
alignCorners = params.get<bool>("align_corners", false);
}
......@@ -106,14 +106,15 @@ public:
Mat& inp = inputs[0];
Mat& out = outputs[0];
if (interpolation == "nearest")
if (interpolation == "nearest" || interpolation == "opencv_linear")
{
InterpolationFlags mode = interpolation == "nearest" ? INTER_NEAREST : INTER_LINEAR;
for (size_t n = 0; n < inputs[0].size[0]; ++n)
{
for (size_t ch = 0; ch < inputs[0].size[1]; ++ch)
{
resize(getPlane(inp, n, ch), getPlane(out, n, ch),
Size(outWidth, outHeight), 0, 0, INTER_NEAREST);
Size(outWidth, outHeight), 0, 0, mode);
}
}
}
......
......@@ -980,6 +980,15 @@ void ONNXImporter::populateNet(Net dstNet)
replaceLayerParam(layerParams, "width_scale", "zoom_factor_x");
}
replaceLayerParam(layerParams, "mode", "interpolation");
if (layerParams.get<String>("interpolation") == "linear" && framework_name == "pytorch") {
layerParams.type = "Resize";
Mat scales = getBlob(node_proto, constBlobs, 1);
CV_Assert(scales.total() == 4);
layerParams.set("interpolation", "opencv_linear");
layerParams.set("zoom_factor_y", scales.at<float>(2));
layerParams.set("zoom_factor_x", scales.at<float>(3));
}
}
else if (layer_type == "LogSoftmax")
{
......
......@@ -311,6 +311,9 @@ TEST_P(Test_ONNX_layers, Padding)
TEST_P(Test_ONNX_layers, Resize)
{
testONNXModels("resize_nearest");
if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER);
testONNXModels("resize_bilinear");
}
TEST_P(Test_ONNX_layers, MultyInputs)
......
......@@ -40,6 +40,7 @@ Follow these steps if you want to convert the original model yourself:
'''
import argparse
import os.path
import numpy as np
import cv2 as cv
......@@ -48,12 +49,11 @@ backends = (cv.dnn.DNN_BACKEND_DEFAULT, cv.dnn.DNN_BACKEND_INFERENCE_ENGINE, cv.
targets = (cv.dnn.DNN_TARGET_CPU, cv.dnn.DNN_TARGET_OPENCL, cv.dnn.DNN_TARGET_OPENCL_FP16, cv.dnn.DNN_TARGET_MYRIAD)
def preprocess(image_path):
def preprocess(image):
"""
Create 4-dimensional blob from image and flip image
:param image_path: path to input image
:param image: input image
"""
image = cv.imread(image_path)
image_rev = np.flip(image, axis=1)
input = cv.dnn.blobFromImages([image, image_rev], mean=(104.00698793, 116.66876762, 122.67891434))
return input
......@@ -137,15 +137,15 @@ def decode_labels(gray_image):
return segm
def parse_human(image_path, model_path, backend=cv.dnn.DNN_BACKEND_OPENCV, target=cv.dnn.DNN_TARGET_CPU):
def parse_human(image, model_path, backend=cv.dnn.DNN_BACKEND_OPENCV, target=cv.dnn.DNN_TARGET_CPU):
"""
Prepare input for execution, run net and postprocess output to parse human.
:param image_path: path to input image
:param image: input image
:param model_path: path to JPPNet model
:param backend: name of computation backend
:param target: name of computation target
"""
input = preprocess(image_path)
input = preprocess(image)
input_h, input_w = input.shape[2:]
output = run_net(input, model_path, backend, target)
grayscale_out = postprocess(output, (input_w, input_h))
......@@ -157,7 +157,7 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Use this script to run human parsing using JPPNet',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--input', '-i', required=True, help='Path to input image.')
parser.add_argument('--model', '-m', required=True, help='Path to pb model.')
parser.add_argument('--model', '-m', default='lip_jppnet_384.pb', help='Path to pb model.')
parser.add_argument('--backend', choices=backends, default=cv.dnn.DNN_BACKEND_DEFAULT, type=int,
help="Choose one of computation backends: "
"%d: automatically (by default), "
......@@ -171,7 +171,11 @@ if __name__ == '__main__':
'%d: VPU' % targets)
args, _ = parser.parse_known_args()
output = parse_human(args.input, args.model, args.backend, args.target)
if not os.path.isfile(args.model):
raise OSError("Model not exist")
image = cv.imread(args.input)
output = parse_human(image, args.model, args.backend, args.target)
winName = 'Deep learning human parsing in OpenCV'
cv.namedWindow(winName, cv.WINDOW_AUTOSIZE)
cv.imshow(winName, output)
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册