mobilenetv2 310infer amend

This commit is contained in:
chenweitao_295 2021-05-27 14:35:50 +08:00
parent ba4c72d1e3
commit 393204fa94
5 changed files with 29 additions and 19 deletions

View File

@ -252,9 +252,10 @@ Current batch_size can only be set to 1.
```shell
# Ascend310 inference
bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [DVPP] [DEVICE_ID]
bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [LABEL_PATH] [DVPP] [DEVICE_ID]
```
- `LABEL_PATH` label.txt path. Write a py script to sort the category under the dataset, map the file names under the categories and category sort values,Such as[file name : sort value], and write the mapping results to the labe.txt file.
- `DVPP` is mandatory, and must choose from ["DVPP", "CPU"], it's case-insensitive.The size of the picture that MobilenetV2 performs inference is [224, 224], the DVPP hardware limits the width of divisible by 16, and the height is divisible by 2. The network conforms to the standard, and the network can pre-process the image through DVPP.
- `DEVICE_ID` is optional, default value is 0.

View File

@ -259,9 +259,10 @@ python export.py --platform [PLATFORM] --ckpt_file [CKPT_PATH] --file_format [EX
```shell
# Ascend310 inference
bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [DVPP] [DEVICE_ID]
bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [LABEL_PATH] [DVPP] [DEVICE_ID]
```
- `LABEL_PATH` label.txt存放的路径写一个py脚本对数据集下的类别名进行排序对类别下的文件名和类别排序值做映射例如[文件名:排序值]将映射结果写到labe.txt文件中。
- `DVPP` 为必填项,需要在["DVPP", "CPU"]选择大小写均可。Mobilenetv2执行推理的图片尺寸为[224, 224]DVPP硬件限制宽为16整除高为2整除网络符合标准网络可以通过DVPP对图像进行前处理。
- `DEVICE_ID` 可选默认值为0。

View File

@ -117,11 +117,11 @@ int main(int argc, char **argv) {
auto resizeShape = {FLAGS_image_height, FLAGS_image_width};
std::shared_ptr<TensorTransform> resize(new Resize(resizeShape));
auto crop_size = {224, 224};
std::shared_ptr<TensorTransform> center_crop(new CenterCrop(center_crop));
std::shared_ptr<TensorTransform> center_crop(new CenterCrop(crop_size));
Execute transform({decode, resize, center_crop, normalize, hwc2chw});
auto img = MSTensor();
auto image = ReadFileToTensor(all_files[i]);
composeDecode(image, &img);
transform(image, &img);
std::vector<MSTensor> model_inputs = model.GetInputs();
inputs.emplace_back(model_inputs[0].Name(), model_inputs[0].DataType(), model_inputs[0].Shape(),
img.Data().get(), img.DataSize());

View File

@ -28,24 +28,30 @@ def calcul_acc(labels, preds):
return sum(1 for x, y in zip(labels, preds) if x == y) / len(labels)
def read_label(label_path):
label_dict = {}
with open(label_path, 'r') as f:
lines = f.readlines()
for line in lines:
file_name = line.split(':')[0]
label = line.split(':')[1]
label_dict[file_name] = label
return label_dict
def get_result(result_path, label_path):
files = os.listdir(result_path)
preds = []
labels = []
label_dict = {}
with open(label_path, 'w') as f:
lines = f.readlines()
for line in lines:
label_dict[line.split(',')[0]] = line.split(',')[1]
label_dict = read_label(label_path)
for file in files:
file_name = file.split('.')[0]
label = int(label_dict[file_name + '.JEPG'])
label = int(label_dict[file_name])
labels.append(label)
resultPath = os.path.join(result_path, file)
output = np.fromfile(resultPath, dtype=np.float32)
output = np.fromfile(os.path.join(result_path, file), dtype=np.float32)
preds.append(np.argmax(output, axis=0))
acc = calcul_acc(labels, preds)
print("accuracy: {}".format(acc))
print("total{}, accuracy: {}".format(len(labels), acc))
if __name__ == '__main__':

View File

@ -14,8 +14,8 @@
# limitations under the License.
# ============================================================================
if [[ $# -lt 3 || $# -gt 4 ]]; then
echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [DVPP] [DEVICE_ID]
if [[ $# -lt 4 || $# -gt 5 ]]; then
echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [LABEL_PATH] [DVPP] [DEVICE_ID]
DVPP is mandatory, and must choose from [DVPP|CPU], it's case-insensitive
DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero"
exit 1
@ -30,15 +30,17 @@ get_real_path(){
}
model=$(get_real_path $1)
data_path=$(get_real_path $2)
DVPP=${3^^}
label_path=$(get_real_path $3)
DVPP=${4^^}
device_id=0
if [ $# == 4 ]; then
device_id=$4
if [ $# == 5 ]; then
device_id=$5
fi
echo "mindir name: "$model
echo "dataset path: "$data_path
echo "label path: "$label_path
echo "image process mode: "$DVPP
echo "device id: "$device_id
@ -85,7 +87,7 @@ function infer()
function cal_acc()
{
python3.7 ../postprocess.py --result_path=./result_Files --label_path=../label.txt &> acc.log &
python3.7 ../postprocess.py --result_path=./result_Files --label_path=$label_path &> acc.log &
}
compile_app