diff --git a/dnmetis/README.md b/dnmetis/README.md new file mode 100644 index 0000000..43851e7 --- /dev/null +++ b/dnmetis/README.md @@ -0,0 +1,35 @@ +## 1.Install dnmetis_backend + +As README.md: +backend_C++/dnmetis_backend/README.md + +## 2.Download dataset and model(.om) + +1.download Imagenet-val dataset URL: http://www.image-net.org/download-imageurls + +2.download efficientnet-b8 model(.om) URL: obs://hwwheel23/efficientnet-b8.om + +3.process the original Imagenet-val dataset as list: + +![输入图片说明](https://images.gitee.com/uploads/images/2020/0918/234302_a572d632_5418572.jpeg "无标题.jpg") + + + +## 3.Start execute the inference: + +sh run_efficientnet-b8.sh + +or + +python3.7 main.py --model=/data/hwwheel123/model/efficientnet-b8.om --image_size='672,672,3' --inputs='images:0' --outputs='Softmax:0' --precision=fp16 + + +## 4.ATC offline model generate (optional): + +1.download efficientnet-b8 model(.pb) URL: obs://hwwheel23/efficientnet-b8.pb + +2.atc --model=$MODEL_DIR/efficientnet-b8.pb --framework=3 --input_shape='images:1,672,672,3' --output=$MODEL_DIR/efficientnet-b8 --mode=0 --out_nodes='Softmax:0' --soc_version=Ascend310 --input_fp16_nodes=images --output_type=FP16 + +## 5.Imagenet2012-val Top1 Accuracy: + +![输入图片说明](https://images.gitee.com/uploads/images/2020/0919/010210_5cf496fc_5418572.png "屏幕截图.png") diff --git a/dnmetis/main.py b/dnmetis/main.py new file mode 100644 index 0000000..2c5880b --- /dev/null +++ b/dnmetis/main.py @@ -0,0 +1,150 @@ +import argparse +import array +import collections +import json +import os +import sys +import threading +import time +from queue import Queue +#import env +import cv2 +import numpy as np +import re +import pdb + +# import converter.converter as converter +#from backend.backend_acl import AclBackend + + + +last_timing = [] +last_device_timing = [] + + +def get_args(): + """Parse commandline.""" + parser = argparse.ArgumentParser() + parser.add_argument("--dataset_path", default="./datasets/imagenet_10", help="path to the dataset") + parser.add_argument("--backend", default="acl", help="runtime to use") + parser.add_argument("--model", required=True, help="model file path") + parser.add_argument("--image_size",default='224,224,3',help="model inputs imagesize") + parser.add_argument("--inputs", default="input:0", help="model inputs nodes eg: data1:0 ") + parser.add_argument("--outputs",default="softmax:0", help="model outputs nodes list eg:fc1:0,fc2:0,fc3:0 ") + + # below will override DNMetis rules compliant settings - don't use for official submission + parser.add_argument("--count", default=1000, type=int, help="dataset items to infer") + parser.add_argument("--precision", default="fp32", choices=["fp32", "fp16", "int8", "uint8"], + help="precision mode, one of " + str(["fp32", "fp16", "int8", "uint8"])) + parser.add_argument("--feed", default=[], help="feed") + parser.add_argument("--image_list", default=[], help="image_list") + parser.add_argument("--label_list", default=[], help="label_list") + parser.add_argument("--cfg_path",default="./backend_cfg/built-in_config.txt") + args = parser.parse_args() + + # don't use defaults in argparser. Instead we default to a dict, override that with a profile + # and take this as default unless command line give + + + #if args.image_size is None: + # args.image_size = SUPPORTED_DATASETS[args.dataset][3]['image_size'] + + if args.inputs: + args.inputs = args.inputs.split(",") + if args.outputs: + args.outputs = args.outputs.split(",") + if args.image_size: + args.image_size = list(map(int, args.image_size.split(","))) + + return args + +def get_backend(backend): + if backend == "acl": + from backend.backend_acl import AclBackend + backend = AclBackend() + return backend + +def resize_with_aspectratio(img, out_height, out_width, scale=87.5, inter_pol=cv2.INTER_LINEAR): + height, width = img.shape[:2] + new_height = int(100. * out_height / scale) + new_width = int(100. * out_width / scale) + if height > width: + w = new_width + h = int(new_height * height / width) + else: + h = new_height + w = int(new_width * width / height) + img = cv2.resize(img, (w, h), interpolation=inter_pol) + return img + +def center_crop(img, out_height, out_width): + height, width = img.shape[:2] + left = int((width - out_width) / 2) + right = int((width + out_width) / 2) + top = int((height - out_height) / 2) + bottom = int((height + out_height) / 2) + img = img[top:bottom, left:right] + return img +def pre_process_noisy(img, dims=None, precision="fp32"): + img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + output_height, output_width, _ = dims + cv2_interpol = cv2.INTER_CUBIC + img = resize_with_aspectratio(img, output_height, output_width, inter_pol=cv2_interpol) + img = center_crop(img, output_height, output_width) + MEAN_RGB = [0.485 * 255, 0.456 * 255, 0.406 * 255] + STDDEV_RGB = [0.229 * 255, 0.224 * 255, 0.225 * 255] + + if precision=="fp32": + img = np.asarray(img, dtype='float32') + if precision=="fp16": + img = np.asarray(img, dtype='float16') + + means = np.array([0.485 * 255, 0.456 * 255, 0.406 * 255], dtype=np.float32) + img -= means + stddev = np.array([0.229 * 255, 0.224 * 255, 0.225 * 255], dtype=np.float32) + img /= stddev + return img + + + +def preprocess_dataset(args,offset=0): + with open(args.dataset_path + '/val_map.txt', 'r') as f: + for s in f: + image_name, label = re.split(r"\s+", s.strip()) + src = os.path.join(args.dataset_path, image_name) + img_org = cv2.imread(src) + processed_img = pre_process_noisy(img_org, dims=args.image_size, precision = args.precision) + args.feed.append(processed_img) + args.image_list.append(image_name) + args.label_list.append(int(label)+offset) + + +def main(): + good = 0 + total = 0 + #args + args = get_args() + + # find backend + backend = get_backend(args.backend) + + # load model to backend + model = backend.load(args) + # + # preprocess_dataset + preprocess_dataset(args,offset=0) + + #start inference: + for i in range(len(args.feed)): + predictions = backend.predict(args.feed[i]) + #print(args.feed[i].shape) + print('img_orig:',args.image_list[i],'label:',args.label_list[i],'predictions:',np.argmax(predictions),'\n') + if args.label_list[i] == np.argmax(predictions): + good +=1 + total +=1 + print('Predict total jpeg:',len(args.image_list),' Accuracy: ',good / total) + + backend.unload() + +if __name__ == "__main__": + main() diff --git a/dnmetis/run_inference.sh b/dnmetis/run_inference.sh new file mode 100644 index 0000000..2cbd1f7 --- /dev/null +++ b/dnmetis/run_inference.sh @@ -0,0 +1,4 @@ +python3.7 main.py --model=./model/efficientnet-b8.om --image_size='672,672,3' --precision=fp16 + + + diff --git a/dnmetis/version.py b/dnmetis/version.py new file mode 100644 index 0000000..2432e3c --- /dev/null +++ b/dnmetis/version.py @@ -0,0 +1,2 @@ + +version = '1.0'