Modify model tests

This commit is contained in:
jiangjiajun
2022-11-05 07:54:16 +00:00
parent f00212aa42
commit cf8f53e36d
7 changed files with 111 additions and 92 deletions

14
tests/models/README.md Normal file
View File

@@ -0,0 +1,14 @@
# 添加模型单测
所有模型统一使用`runtime_config.py`中的RuntimeOption进行配置
```
import runtime_config as rc
model = fd.vision.XXX(..., runtime_option=rc.test_option)
```
验证For循环跑2+次与Baseline结果符合预期

View File

@@ -22,19 +22,20 @@ def test_facealignment_pfld():
model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/pfld-106-lite.onnx" model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/pfld-106-lite.onnx"
input_url = "https://bj.bcebos.com/paddlehub/fastdeploy/facealign_input.png" input_url = "https://bj.bcebos.com/paddlehub/fastdeploy/facealign_input.png"
output_url = "https://bj.bcebos.com/paddlehub/fastdeploy/result_landmarks.npy" output_url = "https://bj.bcebos.com/paddlehub/fastdeploy/result_landmarks.npy"
fd.download(model_url, ".") fd.download(model_url, "resources")
fd.download(input_url, ".") fd.download(input_url, "resources")
fd.download(output_url, ".") fd.download(output_url, "resources")
model_path = "pfld-106-lite.onnx" model_path = "resources/pfld-106-lite.onnx"
# use ORT # use ORT
model = fd.vision.facealign.PFLD(model_path, runtime_option=rc.test_option) model = fd.vision.facealign.PFLD(model_path, runtime_option=rc.test_option)
# compare diff # compare diff
im = cv2.imread("./facealign_input.png") im = cv2.imread("resources/facealign_input.png")
result = model.predict(im.copy()) for i in range(2):
expect = np.load("./result_landmarks.npy") result = model.predict(im)
expect = np.load("resources/result_landmarks.npy")
diff = np.fabs(np.array(result.landmarks) - expect) diff = np.fabs(np.array(result.landmarks) - expect)
thres = 1e-04 thres = 1e-04
assert diff.max() < thres, "The diff is %f, which is bigger than %f" % ( assert diff.max() < thres, "The diff is %f, which is bigger than %f" % (
diff.max(), thres) diff.max(), thres)

View File

@@ -22,9 +22,9 @@ import runtime_config as rc
def test_matting_ppmatting(): def test_matting_ppmatting():
model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/PP-Matting-512.tgz" model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/PP-Matting-512.tgz"
input_url = "https://bj.bcebos.com/paddlehub/fastdeploy/matting_input.jpg" input_url = "https://bj.bcebos.com/paddlehub/fastdeploy/matting_input.jpg"
fd.download_and_decompress(model_url, ".") fd.download_and_decompress(model_url, "resources")
fd.download(input_url, ".") fd.download(input_url, "resources")
model_path = "./PP-Matting-512" model_path = "./resources/PP-Matting-512"
model_file = os.path.join(model_path, "model.pdmodel") model_file = os.path.join(model_path, "model.pdmodel")
params_file = os.path.join(model_path, "model.pdiparams") params_file = os.path.join(model_path, "model.pdiparams")
config_file = os.path.join(model_path, "deploy.yaml") config_file = os.path.join(model_path, "deploy.yaml")
@@ -32,26 +32,27 @@ def test_matting_ppmatting():
model_file, params_file, config_file, runtime_option=rc.test_option) model_file, params_file, config_file, runtime_option=rc.test_option)
# 预测图片抠图结果 # 预测图片抠图结果
im = cv2.imread("./matting_input.jpg") im = cv2.imread("./resources/matting_input.jpg")
result = model.predict(im.copy()) for i in range(2):
pkl_url = "https://bj.bcebos.com/fastdeploy/tests/ppmatting_result.pkl" result = model.predict(im)
if pkl_url: pkl_url = "https://bj.bcebos.com/fastdeploy/tests/ppmatting_result.pkl"
fd.download(pkl_url, ".") if pkl_url:
with open("./ppmatting_result.pkl", "rb") as f: fd.download(pkl_url, "resources")
baseline = pickle.load(f) with open("./resources/ppmatting_result.pkl", "rb") as f:
baseline = pickle.load(f)
diff = np.fabs(np.array(result.alpha) - np.array(baseline)) diff = np.fabs(np.array(result.alpha) - np.array(baseline))
thres = 1e-05 thres = 1e-05
assert diff.max() < thres, "The diff is %f, which is bigger than %f" % ( assert diff.max() < thres, "The diff is %f, which is bigger than %f" % (
diff.max(), thres) diff.max(), thres)
def test_matting_ppmodnet(): def test_matting_ppmodnet():
model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/PPModnet_MobileNetV2.tgz" model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/PPModnet_MobileNetV2.tgz"
input_url = "https://bj.bcebos.com/paddlehub/fastdeploy/matting_input.jpg" input_url = "https://bj.bcebos.com/paddlehub/fastdeploy/matting_input.jpg"
fd.download_and_decompress(model_url, ".") fd.download_and_decompress(model_url, "resources")
fd.download(input_url, ".") fd.download(input_url, "resources")
model_path = "./PPModnet_MobileNetV2" model_path = "./resources/PPModnet_MobileNetV2"
model_file = os.path.join(model_path, "model.pdmodel") model_file = os.path.join(model_path, "model.pdmodel")
params_file = os.path.join(model_path, "model.pdiparams") params_file = os.path.join(model_path, "model.pdiparams")
config_file = os.path.join(model_path, "deploy.yaml") config_file = os.path.join(model_path, "deploy.yaml")
@@ -59,27 +60,29 @@ def test_matting_ppmodnet():
model_file, params_file, config_file, runtime_option=rc.test_option) model_file, params_file, config_file, runtime_option=rc.test_option)
# 预测图片抠图结果 # 预测图片抠图结果
im = cv2.imread("./matting_input.jpg") im = cv2.imread("./resources/matting_input.jpg")
result = model.predict(im.copy())
pkl_url = "https://bj.bcebos.com/fastdeploy/tests/ppmodnet_result.pkl" for i in range(2):
if pkl_url: result = model.predict(im)
fd.download(pkl_url, ".")
with open("./ppmodnet_result.pkl", "rb") as f:
baseline = pickle.load(f)
diff = np.fabs(np.array(result.alpha) - np.array(baseline)) pkl_url = "https://bj.bcebos.com/fastdeploy/tests/ppmodnet_result.pkl"
thres = 1e-05 if pkl_url:
assert diff.max() < thres, "The diff is %f, which is bigger than %f" % ( fd.download(pkl_url, "resources")
diff.max(), thres) with open("./resources/ppmodnet_result.pkl", "rb") as f:
baseline = pickle.load(f)
diff = np.fabs(np.array(result.alpha) - np.array(baseline))
thres = 1e-05
assert diff.max() < thres, "The diff is %f, which is bigger than %f" % (
diff.max(), thres)
def test_matting_pphumanmatting(): def test_matting_pphumanmatting():
model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/PPHumanMatting.tgz" model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/PPHumanMatting.tgz"
input_url = "https://bj.bcebos.com/paddlehub/fastdeploy/matting_input.jpg" input_url = "https://bj.bcebos.com/paddlehub/fastdeploy/matting_input.jpg"
fd.download_and_decompress(model_url, ".") fd.download_and_decompress(model_url, "resources")
fd.download(input_url, ".") fd.download(input_url, "resources")
model_path = "./PPHumanMatting" model_path = "./resources/PPHumanMatting"
# 配置runtime加载模型 # 配置runtime加载模型
runtime_option = fd.RuntimeOption() runtime_option = fd.RuntimeOption()
model_file = os.path.join(model_path, "model.pdmodel") model_file = os.path.join(model_path, "model.pdmodel")
@@ -89,17 +92,18 @@ def test_matting_pphumanmatting():
model_file, params_file, config_file, runtime_option=rc.test_option) model_file, params_file, config_file, runtime_option=rc.test_option)
# 预测图片抠图结果 # 预测图片抠图结果
im = cv2.imread("./matting_input.jpg") im = cv2.imread("./resources/matting_input.jpg")
result = model.predict(im.copy()) for i in range(2):
result = model.predict(im)
pkl_url = "https://bj.bcebos.com/fastdeploy/tests/pphumanmatting_result.pkl" pkl_url = "https://bj.bcebos.com/fastdeploy/tests/pphumanmatting_result.pkl"
if pkl_url: if pkl_url:
fd.download(pkl_url, ".") fd.download(pkl_url, "resources")
with open("./pphumanmatting_result.pkl", "rb") as f: with open("./resources/pphumanmatting_result.pkl", "rb") as f:
baseline = pickle.load(f) baseline = pickle.load(f)
diff = np.fabs(np.array(result.alpha) - np.array(baseline)) diff = np.fabs(np.array(result.alpha) - np.array(baseline))
thres = 1e-05 thres = 1e-05
assert diff.max() < thres, "The diff is %f, which is bigger than %f" % ( assert diff.max() < thres, "The diff is %f, which is bigger than %f" % (
diff.max(), thres) diff.max(), thres)

View File

@@ -20,8 +20,8 @@ import runtime_config as rc
def test_keypointdetection_pptinypose(): def test_keypointdetection_pptinypose():
pp_tinypose_model_url = "https://bj.bcebos.com/fastdeploy/tests/PP_TinyPose_256x192_test.tgz" pp_tinypose_model_url = "https://bj.bcebos.com/fastdeploy/tests/PP_TinyPose_256x192_test.tgz"
fd.download_and_decompress(pp_tinypose_model_url, ".") fd.download_and_decompress(pp_tinypose_model_url, "resources")
model_path = "./PP_TinyPose_256x192_test" model_path = "./resources/PP_TinyPose_256x192_test"
# 配置runtime加载模型 # 配置runtime加载模型
runtime_option = fd.RuntimeOption() runtime_option = fd.RuntimeOption()
model_file = os.path.join(model_path, "model.pdmodel") model_file = os.path.join(model_path, "model.pdmodel")
@@ -48,8 +48,8 @@ def test_keypointdetection_pptinypose():
def test_keypointdetection_det_keypoint_unite(): def test_keypointdetection_det_keypoint_unite():
det_keypoint_unite_model_url = "https://bj.bcebos.com/fastdeploy/tests/PicoDet_320x320_TinyPose_256x192_test.tgz" det_keypoint_unite_model_url = "https://bj.bcebos.com/fastdeploy/tests/PicoDet_320x320_TinyPose_256x192_test.tgz"
fd.download_and_decompress(det_keypoint_unite_model_url, ".") fd.download_and_decompress(det_keypoint_unite_model_url, "resources")
model_path = "./PicoDet_320x320_TinyPose_256x192_test" model_path = "./resources/PicoDet_320x320_TinyPose_256x192_test"
# 配置runtime加载模型 # 配置runtime加载模型
runtime_option = fd.RuntimeOption() runtime_option = fd.RuntimeOption()
tinypose_model_file = os.path.join( tinypose_model_file = os.path.join(
@@ -91,7 +91,7 @@ def test_keypointdetection_det_keypoint_unite():
(np.array(result.keypoints), np.array(result.scores)[:, np.newaxis]), (np.array(result.keypoints), np.array(result.scores)[:, np.newaxis]),
axis=1) axis=1)
print(result) print(result)
np.save("baseline.npy", result) np.save("resources/baseline.npy", result)
baseline = np.load(baseline_file) baseline = np.load(baseline_file)
diff = np.fabs(result - np.array(baseline)) diff = np.fabs(result - np.array(baseline))
thres = 1e-05 thres = 1e-05

View File

@@ -23,16 +23,16 @@ import runtime_config as rc
def test_pptracking(): def test_pptracking():
model_url = "https://bj.bcebos.com/fastdeploy/tests/pptracking.tgz" model_url = "https://bj.bcebos.com/fastdeploy/tests/pptracking.tgz"
input_url = "https://bj.bcebos.com/paddlehub/fastdeploy/person.mp4" input_url = "https://bj.bcebos.com/paddlehub/fastdeploy/person.mp4"
fd.download_and_decompress(model_url, ".") fd.download_and_decompress(model_url, "resources")
fd.download(input_url, ".") fd.download(input_url, "resources")
model_path = "pptracking/fairmot_hrnetv2_w18_dlafpn_30e_576x320" model_path = "resources/pptracking/fairmot_hrnetv2_w18_dlafpn_30e_576x320"
# use default backend # use default backend
runtime_option = fd.RuntimeOption() runtime_option = fd.RuntimeOption()
model_file = os.path.join(model_path, "model.pdmodel") model_file = os.path.join(model_path, "model.pdmodel")
params_file = os.path.join(model_path, "model.pdiparams") params_file = os.path.join(model_path, "model.pdiparams")
config_file = os.path.join(model_path, "infer_cfg.yml") config_file = os.path.join(model_path, "infer_cfg.yml")
model = fd.vision.tracking.PPTracking(model_file, params_file, config_file, runtime_option=rc.test_option) model = fd.vision.tracking.PPTracking(model_file, params_file, config_file, runtime_option=rc.test_option)
cap = cv2.VideoCapture("./person.mp4") cap = cv2.VideoCapture("./resources/person.mp4")
frame_id = 0 frame_id = 0
while True: while True:
_, frame = cap.read() _, frame = cap.read()
@@ -40,7 +40,7 @@ def test_pptracking():
break break
result = model.predict(frame) result = model.predict(frame)
# compare diff # compare diff
expect = pickle.load(open("pptracking/frame" + str(frame_id) + ".pkl", "rb")) expect = pickle.load(open("resources/pptracking/frame" + str(frame_id) + ".pkl", "rb"))
diff_boxes = np.fabs(np.array(expect["boxes"]) - np.array(result.boxes)) diff_boxes = np.fabs(np.array(expect["boxes"]) - np.array(result.boxes))
diff_scores = np.fabs(np.array(expect["scores"]) - np.array(result.scores)) diff_scores = np.fabs(np.array(expect["scores"]) - np.array(result.scores))
diff = max(diff_boxes.max(), diff_scores.max()) diff = max(diff_boxes.max(), diff_scores.max())

View File

@@ -22,11 +22,10 @@ import runtime_config as rc
def test_matting_rvm_cpu(): def test_matting_rvm_cpu():
model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/rvm.tgz" model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/rvm.tgz"
input_url = "https://bj.bcebos.com/paddlehub/fastdeploy/video.mp4" input_url = "https://bj.bcebos.com/paddlehub/fastdeploy/video.mp4"
fd.download_and_decompress(model_url, ".") fd.download_and_decompress(model_url, "resources")
fd.download(input_url, ".") fd.download(input_url, "resources")
model_path = "rvm/rvm_mobilenetv3_fp32.onnx" model_path = "resources/rvm/rvm_mobilenetv3_fp32.onnx"
# use ORT # use ORT
runtime_option.use_ort_backend()
model = fd.vision.matting.RobustVideoMatting( model = fd.vision.matting.RobustVideoMatting(
model_path, runtime_option=rc.test_option) model_path, runtime_option=rc.test_option)
@@ -39,7 +38,7 @@ def test_matting_rvm_cpu():
break break
result = model.predict(frame) result = model.predict(frame)
# compare diff # compare diff
expect_alpha = np.load("rvm/result_alpha_" + str(frame_id) + ".npy") expect_alpha = np.load("resources/rvm/result_alpha_" + str(frame_id) + ".npy")
result_alpha = np.array(result.alpha).reshape(1920, 1080) result_alpha = np.array(result.alpha).reshape(1920, 1080)
diff = np.fabs(expect_alpha - result_alpha) diff = np.fabs(expect_alpha - result_alpha)
thres = 1e-05 thres = 1e-05

View File

@@ -22,9 +22,9 @@ import runtime_config as rc
def test_classification_yolov5cls(): def test_classification_yolov5cls():
model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/yolov5n-cls.tgz" model_url = "https://bj.bcebos.com/paddlehub/fastdeploy/yolov5n-cls.tgz"
input_url = "https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg" input_url = "https://gitee.com/paddlepaddle/PaddleClas/raw/release/2.4/deploy/images/ImageNet/ILSVRC2012_val_00000010.jpeg"
fd.download_and_decompress(model_url, ".") fd.download_and_decompress(model_url, "resources")
fd.download(input_url, ".") fd.download(input_url, "resources")
model_path = "yolov5n-cls/yolov5n-cls.onnx" model_path = "resources/yolov5n-cls/yolov5n-cls.onnx"
# use ORT # use ORT
runtime_option = fd.RuntimeOption() runtime_option = fd.RuntimeOption()
runtime_option.use_ort_backend() runtime_option.use_ort_backend()
@@ -32,18 +32,19 @@ def test_classification_yolov5cls():
model_path, runtime_option=rc.test_option) model_path, runtime_option=rc.test_option)
# compare diff # compare diff
im = cv2.imread("./ILSVRC2012_val_00000010.jpeg") im = cv2.imread("./resources/ILSVRC2012_val_00000010.jpeg")
result = model.predict(im.copy(), topk=5) for i in range(2):
with open("yolov5n-cls/result.pkl", "rb") as f: result = model.predict(im, topk=5)
expect = pickle.load(f) with open("resources/yolov5n-cls/result.pkl", "rb") as f:
expect = pickle.load(f)
diff_label = np.fabs( diff_label = np.fabs(
np.array(result.label_ids) - np.array(expect["labels"])) np.array(result.label_ids) - np.array(expect["labels"]))
diff_score = np.fabs(np.array(result.scores) - np.array(expect["scores"])) diff_score = np.fabs(np.array(result.scores) - np.array(expect["scores"]))
thres = 1e-05 thres = 1e-05
assert diff_label.max( assert diff_label.max(
) < thres, "The label diff is %f, which is bigger than %f" % ( ) < thres, "The label diff is %f, which is bigger than %f" % (
diff_label.max(), thres) diff_label.max(), thres)
assert diff_score.max( assert diff_score.max(
) < thres, "The score diff is %f, which is bigger than %f" % ( ) < thres, "The score diff is %f, which is bigger than %f" % (
diff_score.max(), thres) diff_score.max(), thres)