1。Mindspore的环境安装指南,按照提示进行https://www.mindspore.cn/install
2.下载Mindspore的yolov5,并下载模型和代码https://www.hiascend.com/zh/software/modelzoo/detail/C/9af06eedaa1b9577d6221a939a31747d
下载这两个文件
3。安装库,cd到模型脚本目录下,运行pip install -r requirements.txt
COCO数据集是一个可用于图像检测(image detection),语义分割(semantic segmentation)和图像标题生成(image captioning)的大规模数据集。它有超过330K张图像(其中220K张是有标注的图像),包含150万个目标,80个目标类别(object categories:行人、汽车、大象等),91种材料类别(stuff categoris:草、墙、天空等),每张图像包含五句图像的语句描述,且有250,000个带关键点标注的行人。
具体的COCO数据集介绍 https://blog.csdn.net/qq_44554428/article/details/122597358
下载网址:官网下载链接
下载最小的val2017和annotions,放到目录下的datasets文件下
cd scripts
python export.py --ckpt_file [CKPT_PATH] --file_format [EXPORT_FORMAT]
#其中ckpt为ckpt模型文件地址,`EXPORT_FORMAT` 必须在 ["AIR", "MINDIR"]中选择。`BATCH_SIZE` 目前仅支持batch_size为1的推理(不用设置)。
运行后会再主目录下得到yolov5.mindir的模型文件。
export.py
脚本导出。以下展示了使用mindir模型执行推理的示例。运行指令:bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANN_FILE] [DVPP] [DEVICE_ID]
阅读readme与sh文件提示,其中 mindir_path 为模型地址;DATApath为MS COCO val2017的images地址;ANN_FILE为标注地址;DVPP为推理设备,设置CPU,DEVICE_ID默认为0,无需设置。
如:
在cmd命令下运行export。py文件,后面加上参数
python export.py --ckpt_file D:\yanyi\project_process\ocr\code\Yolov5_for_MindSpore_1.1_code\weights\yolov5.ckpt --file_format MINDIR
在执行推理前,mindir文件必须通过export.py
脚本导出。以下展示了使用mindir模型执行推理的示例。
bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANN_FILE] [DVPP] [DEVICE_ID]
在cmd下运行。sh文件,后面加参数
cd scripts
run_infer_310.sh D:\yanyi\project_process\ocr\code\Yolov5_for_MindSpore_1.1_code\yolov5.mindir D:\yanyi\project_process\ocr\code\Yolov5_for_MindSpore_1.1_code\dataset\val2017 D:\yanyi\project_process\ocr\code\Yolov5_for_MindSpore_1.1_code\dataset\annotations\panoptic_val2017.json CPU
不熟悉shell语言的可以加echo语句输出,查看运行到哪。
#!/bin/bash
# Copyright 2021 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
if [[ $# -lt 4 || $# -gt 5 ]]; then
echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANN_FILE] [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
fi
get_real_path(){
if [ "${1:0:1}" == "/" ]; then
echo "$1"
else
echo "$(realpath -m $PWD/$1)"
fi
}
model=$(get_real_path $1)
data_path=$(get_real_path $2)
ann_file=$(get_real_path $3)
DVPP=${4^^}
device_id=0
if [ $# == 5 ]; then
device_id=$5
fi
echo "mindir name: "$model
echo "dataset path: "$data_path
echo "ann file: "$ann_file
echo "image process mode: "$DVPP
echo "device id: "$device_id
export ASCEND_HOME=/usr/local/Ascend/
if [ -d ${ASCEND_HOME}/ascend-toolkit ]; then
echo "device id: "$device_id
export PATH=$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/atc/bin:$PATH
echo "device id: "$device_id
export LD_LIBRARY_PATH=$ASCEND_HOME/fwkacllib/lib64:/usr/local/lib:$ASCEND_HOME/ascend-toolkit/latest/atc/lib64:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH
export TBE_IMPL_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe
export PYTHONPATH=$ASCEND_HOME/fwkacllib/python/site-packages:${TBE_IMPL_PATH}:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/python/site-packages:$PYTHONPATH
export ASCEND_OPP_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp
else
echo "device DDDDDDDDDDDDDDDDDDDid: "$device_id
export PATH=$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH
echo "deviceDDDDDDDDDDDDDDDDDDDDD id: "$device_id
export LD_LIBRARY_PATH=$ASCEND_HOME/fwkacllib/lib64:/usr/local/lib:$ASCEND_HOME/atc/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH
export PYTHONPATH=$ASCEND_HOME/fwkacllib/python/site-packages:$ASCEND_HOME/atc/python/site-packages:$PYTHONPATH
export ASCEND_OPP_PATH=$ASCEND_HOME/opp
fi
echo "device AAAAAAAAAAAAAAAid: "$device_id
function compile_app()
{
cd ../ascend310_infer || exit
bash build.sh &> build.log
}
echo "device BBBBBBBBBBBBBid: "$device_id
function infer()
{
cd - || exit
if [ -d result_Files ]; then
rm -rf ./result_Files
fi
if [ -d time_Result ]; then
rm -rf ./time_Result
fi
mkdir result_Files
mkdir time_Result
if [ "$DVPP" == "DVPP" ];then
echo "Only support CPU mode"
exit 1
elif [ "$DVPP" == "CPU" ]; then
../ascend310_infer/out/main --mindir_path=$model --dataset_path=$data_path --device_id=$device_id --image_height=640 --image_width=640 &> infer.log
echo "device CCCCCCCCCCCCCCid: "$device_id
else
echo "image process mode must be in [DVPP|CPU]"
exit 1
fi
}
echo "device jisuanjingduid: "$device_id
function cal_acc()
{
echo "device DDDDDDDDDDDDid: "$device_id
python3.7 ../postprocess.py --result_files=./result_Files --dataset_path=$data_path --ann_file=$ann_file &> acc.log &
echo "AAAAAAAAAAAA"
}
echo "device compile_app id: "$device_id
compile_app
if [ $? -ne 0 ]; then
echo "compile app code failed"
exit 1
fi
echo "device infer id: "$device_id
infer
if [ $? -ne 0 ]; then
echo " execute inference failed"
exit 1
fi
echo "device cal_acc id: "$device_id
cal_acc
if [ $? -ne 0 ]; then
echo "calculate accuracy failed"
exit 1
fi
运行后会进入编译阶段,这里还没有结果
----------------------------------------目前只有这些-----------------------------------------------------
下载MS coco数据集val2017和instances_val2017.json,按照如下排列:
instances_val2017.json放入annotations文件中,val2017放到yolov5目录下,其他不需要设置。
运行命令:
python eval.py --data_dir=./dataset/yolov5/ --pretrained=./weights/yolov5.ckpt --testing_shape=640
尝试在win系统下运行,太难了最终还是没起来,分享一下可借鉴的推理代码
https://github.com/mindspore-ai/mindspore-21-days-tutorials/tree/main/chapter4