2021-06-17 20:56:03 +08:00
|
|
|
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
|
2020-11-10 17:19:50 +08:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
__dir__ = os.path.dirname(os.path.abspath(__file__))
|
2021-06-17 20:56:03 +08:00
|
|
|
sys.path.append(os.path.abspath(os.path.join(__dir__, '../')))
|
2020-11-10 17:19:50 +08:00
|
|
|
|
|
|
|
import time
|
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
import base64
|
2021-06-17 20:56:03 +08:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import cv2
|
|
|
|
|
|
|
|
from utils import logger
|
|
|
|
from utils.get_image_list import get_image_list
|
|
|
|
from utils import config
|
|
|
|
from utils.encode_decode import np_to_b64
|
|
|
|
from python.preprocess import create_operators
|
|
|
|
|
|
|
|
preprocess_config = [{
|
|
|
|
'ResizeImage': {
|
|
|
|
'resize_short': 256
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
'CropImage': {
|
|
|
|
'size': 224
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
'NormalizeImage': {
|
|
|
|
'scale': 0.00392157,
|
|
|
|
'mean': [0.485, 0.456, 0.406],
|
|
|
|
'std': [0.229, 0.224, 0.225],
|
|
|
|
'order': ''
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
'ToCHWImage': None
|
|
|
|
}]
|
2020-11-10 17:19:50 +08:00
|
|
|
|
|
|
|
|
2021-03-26 18:52:50 +08:00
|
|
|
def main(args):
|
|
|
|
image_path_list = get_image_list(args.image_file)
|
2020-11-10 17:19:50 +08:00
|
|
|
headers = {"Content-type": "application/json"}
|
2021-06-17 20:56:03 +08:00
|
|
|
preprocess_ops = create_operators(preprocess_config)
|
2021-03-26 18:52:50 +08:00
|
|
|
|
2020-11-10 17:19:50 +08:00
|
|
|
cnt = 0
|
2021-03-26 18:52:50 +08:00
|
|
|
predict_time = 0
|
|
|
|
all_score = 0.0
|
|
|
|
start_time = time.time()
|
2020-11-10 17:19:50 +08:00
|
|
|
|
2021-06-17 20:56:03 +08:00
|
|
|
img_data_list = []
|
2021-03-26 18:52:50 +08:00
|
|
|
img_name_list = []
|
|
|
|
cnt = 0
|
|
|
|
for idx, img_path in enumerate(image_path_list):
|
|
|
|
img = cv2.imread(img_path)
|
2020-11-10 17:19:50 +08:00
|
|
|
if img is None:
|
2021-03-26 18:52:50 +08:00
|
|
|
logger.warning(
|
2021-06-22 11:08:55 +08:00
|
|
|
f"Image file failed to read and has been skipped. The path: {img_path}"
|
|
|
|
)
|
2020-11-10 17:19:50 +08:00
|
|
|
continue
|
2021-03-26 18:52:50 +08:00
|
|
|
else:
|
2021-06-17 20:56:03 +08:00
|
|
|
for ops in preprocess_ops:
|
|
|
|
img = ops(img)
|
|
|
|
img = np.array(img)
|
|
|
|
img_data_list.append(img)
|
|
|
|
|
2021-03-26 18:52:50 +08:00
|
|
|
img_name = img_path.split('/')[-1]
|
|
|
|
img_name_list.append(img_name)
|
|
|
|
cnt += 1
|
|
|
|
if cnt % args.batch_size == 0 or (idx + 1) == len(image_path_list):
|
2021-06-17 20:56:03 +08:00
|
|
|
inputs = np.array(img_data_list)
|
|
|
|
b64str, revert_shape = np_to_b64(inputs)
|
2021-03-26 18:52:50 +08:00
|
|
|
data = {
|
|
|
|
"images": b64str,
|
|
|
|
"revert_params": {
|
|
|
|
"shape": revert_shape,
|
2021-06-17 20:56:03 +08:00
|
|
|
"dtype": str(inputs.dtype)
|
|
|
|
}
|
2021-03-26 18:52:50 +08:00
|
|
|
}
|
|
|
|
try:
|
|
|
|
r = requests.post(
|
|
|
|
url=args.server_url,
|
|
|
|
headers=headers,
|
|
|
|
data=json.dumps(data))
|
|
|
|
r.raise_for_status
|
|
|
|
if r.json()["status"] != "000":
|
|
|
|
msg = r.json()["msg"]
|
|
|
|
raise Exception(msg)
|
|
|
|
except Exception as e:
|
2021-06-22 11:08:55 +08:00
|
|
|
logger.error(f"{e}, in file(s): {img_name_list[0]} etc.")
|
2021-03-26 18:52:50 +08:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
results = r.json()["results"]
|
2021-06-17 20:56:03 +08:00
|
|
|
preds = results["prediction"]
|
2021-03-26 18:52:50 +08:00
|
|
|
elapse = results["elapse"]
|
|
|
|
|
2021-06-17 20:56:03 +08:00
|
|
|
cnt += len(preds)
|
2021-03-26 18:52:50 +08:00
|
|
|
predict_time += elapse
|
|
|
|
|
2021-06-17 20:56:03 +08:00
|
|
|
for number, result_list in enumerate(preds):
|
2021-04-30 16:10:17 +08:00
|
|
|
all_score += result_list["scores"][0]
|
|
|
|
result_str = ""
|
2021-06-17 20:56:03 +08:00
|
|
|
for i in range(len(result_list["class_ids"])):
|
2021-04-30 16:10:17 +08:00
|
|
|
result_str += "{}: {:.2f}\t".format(
|
2021-06-17 20:56:03 +08:00
|
|
|
result_list["class_ids"][i],
|
2021-04-30 16:10:17 +08:00
|
|
|
result_list["scores"][i])
|
2021-06-17 20:56:03 +08:00
|
|
|
|
2021-06-22 11:08:55 +08:00
|
|
|
logger.info(
|
|
|
|
f"File:{img_name_list[number]}, The result(s): {result_str}"
|
|
|
|
)
|
2021-03-26 18:52:50 +08:00
|
|
|
|
|
|
|
finally:
|
2021-06-17 20:56:03 +08:00
|
|
|
img_data_list = []
|
2021-03-26 18:52:50 +08:00
|
|
|
img_name_list = []
|
|
|
|
|
|
|
|
total_time = time.time() - start_time
|
|
|
|
logger.info("The average time of prediction cost: {:.3f} s/image".format(
|
|
|
|
predict_time / cnt))
|
|
|
|
logger.info("The average time cost: {:.3f} s/image".format(total_time /
|
|
|
|
cnt))
|
|
|
|
logger.info("The average top-1 score: {:.3f}".format(all_score / cnt))
|
2020-11-10 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-06-17 20:56:03 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("--server_url", type=str)
|
|
|
|
parser.add_argument("--image_file", type=str)
|
|
|
|
parser.add_argument("--batch_size", type=int, default=1)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-03-26 18:52:50 +08:00
|
|
|
main(args)
|