diff --git a/official/cv/faster_rcnn/README.md b/official/cv/faster_rcnn/README.md index 7543e8218d3d6c70a6a48e7117bc91a465caa7f2..9086d9017b771c7b2b56c3a6153ca17a7b257c5f 100644 --- a/official/cv/faster_rcnn/README.md +++ b/official/cv/faster_rcnn/README.md @@ -89,10 +89,10 @@ Dataset used: [COCO2017](<https://cocodataset.org/>) Organize the dataset information into a TXT file, each row in the file is as follows: ```log - train2017/0000001.jpg 0,259,401,459,7 35,28,324,201,2 0,30,59,80,2 + train2017/0000001.jpg 0,259,401,459,7,0 35,28,324,201,2,0 0,30,59,80,2,0 ``` - Each row is an image annotation which split by space, the first column is a relative path of image, the others are box and class information of the format [xmin,ymin,xmax,ymax,class]. We read image from an image path joined by the `IMAGE_DIR`(dataset directory) and the relative path in `ANNO_PATH`(the TXT file path), `IMAGE_DIR` and `ANNO_PATH` are setting in `default_config_50.yaml銆乨efault_config_101.yaml or default_config_152.yaml`. + Each row is an image annotation which split by space, the first column is a relative path of image, the others are box, class and if it is crowd information of the format [xmin,ymin,xmax,ymax,class,is_crowd]. We read image from an image path joined by the `image_dir`(dataset directory) and the relative path in `anno_path`(the TXT file path), `image_dir` and `anno_path` are setting in `default_config_50.yaml銆乨efault_config_101.yaml or default_config_152.yaml`. # Quick Start @@ -178,7 +178,7 @@ bash run_eval_ascend.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH] [BACKBONE] [COC ```shell # inference -bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANN_FILE] [DEVICE_ID] +bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANNO_PATH] [DEVICE_ID] ``` - Running on [ModelArts](https://support.huaweicloud.com/modelarts/) @@ -477,7 +477,7 @@ The following example only supports mindir inference with batch_size=1. ```shell # Ascend310 inference -bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANN_FILE] [DEVICE_ID] +bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANNO_PATH] [DEVICE_ID] ``` ### result diff --git a/official/cv/faster_rcnn/README_CN.md b/official/cv/faster_rcnn/README_CN.md index e54c9dec1212315f94648e837ea682036f72a6cb..405fbdd17ac900353bbe163a50b04df761d73dd8 100644 --- a/official/cv/faster_rcnn/README_CN.md +++ b/official/cv/faster_rcnn/README_CN.md @@ -90,10 +90,10 @@ Faster R-CNN鏄竴涓袱闃舵鐩爣妫€娴嬬綉缁滐紝璇ョ綉缁滈噰鐢≧PN锛屽彲浠� 灏嗘暟鎹泦淇℃伅鏁寸悊鎴怲XT鏂囦欢锛屾瘡琛屽唴瀹瑰涓嬶細 ```txt - train2017/0000001.jpg 0,259,401,459,7 35,28,324,201,2 0,30,59,80,2 + train2017/0000001.jpg 0,259,401,459,7,0 35,28,324,201,2,0 0,30,59,80,2,0 ``` - 姣忚鏄寜绌洪棿鍒嗗壊鐨勫浘鍍忔爣娉紝绗竴鍒楁槸鍥惧儚鐨勭浉瀵硅矾寰勶紝鍏朵綑涓篬xmin,ymin,xmax,ymax,class]鏍煎紡鐨勬鍜岀被淇℃伅銆備粠`IMAGE_DIR`锛堟暟鎹泦鐩綍锛夊浘鍍忚矾寰勪互鍙奰ANNO_PATH`锛圱XT鏂囦欢璺緞锛夌殑鐩稿璺緞涓鍙栧浘鍍忋€俙IMAGE_DIR`鍜宍ANNO_PATH`鍙湪`config_50.yaml銆乧onfig_101.yaml鎴朿onfig_152.yaml`涓缃€� + 姣忚鏄寜绌洪棿鍒嗗壊鐨勫浘鍍忔爣娉紝绗竴鍒楁槸鍥惧儚鐨勭浉瀵硅矾寰勶紝鍏朵綑涓篬xmin,ymin,xmax,ymax,class,is_crowd]鏍煎紡鐨勬,绫诲拰鏄惁鏄竴缇ょ墿浣撶殑淇℃伅銆備粠`image_dir`锛堟暟鎹泦鐩綍锛夊浘鍍忚矾寰勪互鍙奰anno_path`锛圱XT鏂囦欢璺緞锛夌殑鐩稿璺緞涓鍙栧浘鍍忋€俙image_dir`鍜宍anno_path`鍙湪`config_50.yaml銆乧onfig_101.yaml鎴朿onfig_152.yaml`涓缃€� # 蹇€熷叆闂� @@ -122,7 +122,7 @@ bash run_distribute_train_ascend.sh [RANK_TABLE_FILE] [PRETRAINED_MODEL] [BACKBO bash run_eval_ascend.sh [VALIDATION_JSON_FILE] [CHECKPOINT_PATH] [BACKBONE] [COCO_ROOT] [MINDRECORD_DIR](option) #鎺ㄧ悊 -bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANN_FILE] [DEVICE_ID] +bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANNO_PATH] [DEVICE_ID] ``` ## 鍦℅PU涓婅繍琛� @@ -477,7 +477,7 @@ python export.py --config_path [CONFIG_PATH] --ckpt_file [CKPT_PATH] --device_ta ```shell # Ascend310 inference -bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANN_FILE] [DEVICE_ID] +bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANNO_PATH] [DEVICE_ID] ``` ### 缁撴灉 diff --git a/official/cv/faster_rcnn/default_config.yaml b/official/cv/faster_rcnn/default_config.yaml index f2a5b3a4bc436d98f26b3dcc6cd010ad9b69651c..35314cdf52793976e8fc192877a4c8857c2fdf26 100644 --- a/official/cv/faster_rcnn/default_config.yaml +++ b/official/cv/faster_rcnn/default_config.yaml @@ -153,6 +153,10 @@ coco_classes: ['background', 'person', 'bicycle', 'car', 'motorcycle', 'airplane 'teddy bear', 'hair drier', 'toothbrush'] num_classes: 81 +# annotations file(json format or user defined text format) +anno_path: '' +image_dir: '' + # train.py FasterRcnn training run_distribute: False dataset: "coco" @@ -160,12 +164,9 @@ pre_trained: "/cache/train/fasterrcnn/faster_rcnn-12_7393.ckpt" device_id: 0 device_num: 1 rank_id: 0 -image_dir: '' -anno_path: '' backbone: 'resnet_v1.5_50' # eval.py FasterRcnn evaluation -ann_file: '/cache/data/annotations/instances_val2017.json' checkpoint_path: "/cache/train/fasterrcnn/faster_rcnn-12_7393.ckpt" # export.py fasterrcnn_export diff --git a/official/cv/faster_rcnn/default_config_101.yaml b/official/cv/faster_rcnn/default_config_101.yaml index c06337dada2b97af79dc674f49e107e863a1b2df..48ef8396a88f14cf7e39df650e43eecc74ccc47a 100644 --- a/official/cv/faster_rcnn/default_config_101.yaml +++ b/official/cv/faster_rcnn/default_config_101.yaml @@ -154,6 +154,10 @@ coco_classes: ['background', 'person', 'bicycle', 'car', 'motorcycle', 'airplane 'teddy bear', 'hair drier', 'toothbrush'] num_classes: 81 +# annotations file(json format or user defined text format) +anno_path: '' +image_dir: '' + # train.py FasterRcnn training run_distribute: False dataset: "coco" @@ -161,12 +165,9 @@ pre_trained: "/cache/train/fasterrcnn/faster_rcnn-12_7393.ckpt" device_id: 0 device_num: 1 rank_id: 0 -image_dir: '' -anno_path: '' backbone: 'resnet_v1_101' # eval.py FasterRcnn evaluation -ann_file: '/cache/data/annotations/instances_val2017.json' checkpoint_path: "/cache/train/fasterrcnn/faster_rcnn-12_7393.ckpt" # export.py fasterrcnn_export diff --git a/official/cv/faster_rcnn/default_config_152.yaml b/official/cv/faster_rcnn/default_config_152.yaml index 896c0b02fc523a6d19141e703416c9534f743b28..65ea34a74857f2e73d6ac92618bc577b68888730 100644 --- a/official/cv/faster_rcnn/default_config_152.yaml +++ b/official/cv/faster_rcnn/default_config_152.yaml @@ -154,6 +154,10 @@ coco_classes: ['background', 'person', 'bicycle', 'car', 'motorcycle', 'airplane 'teddy bear', 'hair drier', 'toothbrush'] num_classes: 81 +# annotations file(json format or user defined text format) +anno_path: '' +image_dir: '' + # train.py FasterRcnn training run_distribute: False dataset: "coco" @@ -161,12 +165,9 @@ pre_trained: "/cache/train/fasterrcnn/faster_rcnn-12_7393.ckpt" device_id: 0 device_num: 1 rank_id: 0 -image_dir: '' -anno_path: '' backbone: 'resnet_v1_152' # eval.py FasterRcnn evaluation -ann_file: '/cache/data/annotations/instances_val2017.json' checkpoint_path: "/cache/train/fasterrcnn/faster_rcnn-12_7393.ckpt" # export.py fasterrcnn_export diff --git a/official/cv/faster_rcnn/eval.py b/official/cv/faster_rcnn/eval.py index cfef5b55a47f9915e34c57de63bc27984e15490c..5f867621e9e21c19eb38d8487a29bd059dc3fb70 100644 --- a/official/cv/faster_rcnn/eval.py +++ b/official/cv/faster_rcnn/eval.py @@ -16,6 +16,8 @@ """Evaluation for FasterRcnn""" import os import time +from collections import defaultdict + import numpy as np from pycocotools.coco import COCO import mindspore.common.dtype as mstype @@ -23,7 +25,7 @@ from mindspore import context from mindspore.train.serialization import load_checkpoint, load_param_into_net from mindspore.common import set_seed, Parameter -from src.dataset import data_to_mindrecord_byte_image, create_fasterrcnn_dataset +from src.dataset import data_to_mindrecord_byte_image, create_fasterrcnn_dataset, parse_json_annos_from_txt from src.util import coco_eval, bbox2result_1image, results2json from src.model_utils.config import config from src.model_utils.moxing_adapter import moxing_wrapper @@ -38,8 +40,11 @@ if config.backbone in ("resnet_v1.5_50", "resnet_v1_101", "resnet_v1_152"): elif config.backbone == "resnet_v1_50": from src.FasterRcnn.faster_rcnn_resnet50v1 import Faster_Rcnn_Resnet -def fasterrcnn_eval(dataset_path, ckpt_path, ann_file): + +def fasterrcnn_eval(dataset_path, ckpt_path, anno_path): """FasterRcnn evaluation.""" + if not os.path.isfile(ckpt_path): + raise RuntimeError("CheckPoint file {} is not valid.".format(ckpt_path)) ds = create_fasterrcnn_dataset(config, dataset_path, batch_size=config.test_batch_size, is_training=False) net = Faster_Rcnn_Resnet(config) param_dict = load_checkpoint(ckpt_path) @@ -57,7 +62,15 @@ def fasterrcnn_eval(dataset_path, ckpt_path, ann_file): eval_iter = 0 total = ds.get_dataset_size() outputs = [] - dataset_coco = COCO(ann_file) + + if config.dataset != "coco": + dataset_coco = COCO() + dataset_coco.dataset, dataset_coco.anns, dataset_coco.cats, dataset_coco.imgs = dict(), dict(), dict(), dict() + dataset_coco.imgToAnns, dataset_coco.catToImgs = defaultdict(list), defaultdict(list) + dataset_coco.dataset = parse_json_annos_from_txt(anno_path, config) + dataset_coco.createIndex() + else: + dataset_coco = COCO(anno_path) print("\n========================================\n") print("total images num: ", total) @@ -130,7 +143,7 @@ def eval_fasterrcnn(): else: print("coco_root not exits.") else: - if os.path.isdir(config.IMAGE_DIR) and os.path.exists(config.ANNO_PATH): + if os.path.isdir(config.image_dir) and os.path.exists(config.anno_path): print("Create Mindrecord. It may take some time.") data_to_mindrecord_byte_image(config, "other", False, prefix, file_num=1) print("Create Mindrecord Done, at {}".format(mindrecord_dir)) @@ -139,7 +152,7 @@ def eval_fasterrcnn(): print("CHECKING MINDRECORD FILES DONE!") print("Start Eval!") - fasterrcnn_eval(mindrecord_file, config.checkpoint_path, config.ann_file) + fasterrcnn_eval(mindrecord_file, config.checkpoint_path, config.anno_path) if __name__ == '__main__': eval_fasterrcnn() diff --git a/official/cv/faster_rcnn/postprocess.py b/official/cv/faster_rcnn/postprocess.py index cc8ef6e45c2eb2e886213ca3f72015f06944e08b..83244df5cf3a1fcf1b495f89ba8c9923f65858c0 100644 --- a/official/cv/faster_rcnn/postprocess.py +++ b/official/cv/faster_rcnn/postprocess.py @@ -29,14 +29,14 @@ def modelarts_pre_process(): pass @moxing_wrapper(pre_process=modelarts_pre_process) -def get_eval_result(ann_file, result_path): +def get_eval_result(anno_path, result_path): """ get evaluation result of faster rcnn""" max_num = 128 result_path = result_path outputs = [] - dataset_coco = COCO(ann_file) + dataset_coco = COCO(anno_path) img_ids = dataset_coco.getImgIds() for img_id in img_ids: @@ -71,4 +71,4 @@ def get_eval_result(ann_file, result_path): coco_eval(result_files, eval_types, dataset_coco, single_result=False) if __name__ == '__main__': - get_eval_result(config.ann_file, config.result_path) + get_eval_result(config.anno_path, config.result_path) diff --git a/official/cv/faster_rcnn/scripts/run_distribute_train_ascend.sh b/official/cv/faster_rcnn/scripts/run_distribute_train_ascend.sh index 9a2d1fd71988fb2fe9a5694cdc105ccc7284cd73..8abf2e3302560e57aa5b53d1e6be4967e929068a 100644 --- a/official/cv/faster_rcnn/scripts/run_distribute_train_ascend.sh +++ b/official/cv/faster_rcnn/scripts/run_distribute_train_ascend.sh @@ -59,7 +59,7 @@ then exit 1 fi -mindrecord_dir=$PATH3/MindRecord_COCO_TRAIN/ +mindrecord_dir=$PATH3/FASTERRCNN_MINDRECORD/ if [ $# -eq 5 ] then mindrecord_dir=$(get_real_path $5) diff --git a/official/cv/faster_rcnn/scripts/run_distribute_train_gpu.sh b/official/cv/faster_rcnn/scripts/run_distribute_train_gpu.sh index f6273887b18811432ab32af8c5d23d7a2f12ac1a..403ee41db03db51f0c56bbea0feb5ae4ae904768 100644 --- a/official/cv/faster_rcnn/scripts/run_distribute_train_gpu.sh +++ b/official/cv/faster_rcnn/scripts/run_distribute_train_gpu.sh @@ -50,7 +50,7 @@ export RANK_SIZE=$1 PRETRAINED_PATH=$2 PATH3=$4 -mindrecord_dir=$PATH3/MindRecord_COCO_TRAIN/ +mindrecord_dir=$PATH3/FASTERRCNN_MINDRECORD/ if [ $# -eq 5 ] then mindrecord_dir=$(get_real_path $5) diff --git a/official/cv/faster_rcnn/scripts/run_eval_ascend.sh b/official/cv/faster_rcnn/scripts/run_eval_ascend.sh index d14700da685916f7f37b6bd6ca069e075095f10c..667dcbb8bb466a6f2951ab797915de6503c51b15 100644 --- a/official/cv/faster_rcnn/scripts/run_eval_ascend.sh +++ b/official/cv/faster_rcnn/scripts/run_eval_ascend.sh @@ -42,7 +42,7 @@ echo $PATH2 if [ ! -f $PATH1 ] then - echo "error: ANN_FILE=$PATH1 is not a file" + echo "error: ANNO_PATH=$PATH1 is not a file" exit 1 fi @@ -58,7 +58,7 @@ then exit 1 fi -mindrecord_dir=$PATH3/MindRecord_COCO_TRAIN/ +mindrecord_dir=$PATH3/FASTERRCNN_MINDRECORD/ if [ $# -eq 5 ] then mindrecord_dir=$(get_real_path $5) @@ -106,6 +106,6 @@ cp -r ../src ./eval cd ./eval || exit env > env.log echo "start eval for device $DEVICE_ID" -python eval.py --config_path=$CONFIG_FILE --device_id=$DEVICE_ID --ann_file=$PATH1 --checkpoint_path=$PATH2 \ +python eval.py --config_path=$CONFIG_FILE --device_id=$DEVICE_ID --anno_path=$PATH1 --checkpoint_path=$PATH2 \ --backbone=$3 --coco_root=$PATH3 --mindrecord_dir=$mindrecord_dir &> log & cd .. \ No newline at end of file diff --git a/official/cv/faster_rcnn/scripts/run_eval_gpu.sh b/official/cv/faster_rcnn/scripts/run_eval_gpu.sh index e1be83b4bb505d59d57f3dd2965acd57970bed01..d3126290e683d9e31db8fd9fbdcc5a89afb03982 100644 --- a/official/cv/faster_rcnn/scripts/run_eval_gpu.sh +++ b/official/cv/faster_rcnn/scripts/run_eval_gpu.sh @@ -42,7 +42,7 @@ echo $PATH3 if [ ! -f $PATH1 ] then - echo "error: ANN_FILE=$PATH1 is not a file" + echo "error: ANNO_PATH=$PATH1 is not a file" exit 1 fi @@ -58,7 +58,7 @@ then exit 1 fi -mindrecord_dir=$PATH3/MindRecord_COCO_TRAIN/ +mindrecord_dir=$PATH3/FASTERRCNN_MINDRECORD/ if [ $# -eq 5 ] then mindrecord_dir=$(get_real_path $5) @@ -106,5 +106,5 @@ cd ./eval || exit env > env.log echo "start eval for device $DEVICE_ID" python eval.py --config_path=$CONFIG_FILE --coco_root=$PATH3 --mindrecord_dir=$mindrecord_dir \ ---device_target="GPU" --device_id=$DEVICE_ID --ann_file=$PATH1 --checkpoint_path=$PATH2 --backbone=$3 &> log & +--device_target="GPU" --device_id=$DEVICE_ID --anno_path=$PATH1 --checkpoint_path=$PATH2 --backbone=$3 &> log & cd .. diff --git a/official/cv/faster_rcnn/scripts/run_infer_310.sh b/official/cv/faster_rcnn/scripts/run_infer_310.sh index dd825094b23e9535cf9e3b3f213153c2ef75ebd2..10bd3e8c991299c59d850905d3ac29ac5a56da54 100644 --- a/official/cv/faster_rcnn/scripts/run_infer_310.sh +++ b/official/cv/faster_rcnn/scripts/run_infer_310.sh @@ -15,7 +15,7 @@ # ============================================================================ if [[ $# -lt 3 || $# -gt 4 ]]; then - echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANN_FILE] [DEVICE_ID] + echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANNO_PATH] [DEVICE_ID] DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero" exit 1 fi @@ -29,7 +29,7 @@ get_real_path(){ } model=$(get_real_path $1) data_path=$(get_real_path $2) -ann_file=$(get_real_path $3) +anno_path=$(get_real_path $3) device_id=0 if [ $# == 4 ]; then @@ -38,7 +38,7 @@ fi echo "mindir name: "$model echo "dataset path: "$data_path -echo "ann_file: " $ann_file +echo "anno_path: " $anno_path echo "device id: "$device_id export ASCEND_HOME=/usr/local/Ascend/ @@ -77,7 +77,7 @@ function infer() function cal_acc() { - python3.7 ../postprocess.py --ann_file=$ann_file --result_path=./result_Files &> acc.log & + python3.7 ../postprocess.py --anno_path=$anno_path --result_path=./result_Files &> acc.log & } compile_app diff --git a/official/cv/faster_rcnn/scripts/run_standalone_train_ascend.sh b/official/cv/faster_rcnn/scripts/run_standalone_train_ascend.sh index a587cefa89ac18c9d220ebf258c312511636b62f..11b95cbd9420c313a10924265f5c51275f08386b 100644 --- a/official/cv/faster_rcnn/scripts/run_standalone_train_ascend.sh +++ b/official/cv/faster_rcnn/scripts/run_standalone_train_ascend.sh @@ -51,7 +51,7 @@ then exit 1 fi -mindrecord_dir=$PATH2/MindRecord_COCO_TRAIN/ +mindrecord_dir=$PATH2/FASTERRCNN_MINDRECORD/ if [ $# -eq 4 ] then mindrecord_dir=$(get_real_path $4) diff --git a/official/cv/faster_rcnn/scripts/run_standalone_train_gpu.sh b/official/cv/faster_rcnn/scripts/run_standalone_train_gpu.sh index 71f5a4146efecbb4cbe43d85b4d354df21652696..64c5e90a5d94b8893a1ea5dd6717789a7d04ac42 100644 --- a/official/cv/faster_rcnn/scripts/run_standalone_train_gpu.sh +++ b/official/cv/faster_rcnn/scripts/run_standalone_train_gpu.sh @@ -51,7 +51,7 @@ then exit 1 fi -mindrecord_dir=$PATH2/MindRecord_COCO_TRAIN/ +mindrecord_dir=$PATH2/FASTERRCNN_MINDRECORD/ if [ $# -eq 4 ] then mindrecord_dir=$(get_real_path $4) diff --git a/official/cv/faster_rcnn/src/dataset.py b/official/cv/faster_rcnn/src/dataset.py index e2f4c903f2a9223060565c5353fba5e42d066aaa..655a051c5a40da49117b836188fcd1f5c9cc2df8 100644 --- a/official/cv/faster_rcnn/src/dataset.py +++ b/official/cv/faster_rcnn/src/dataset.py @@ -25,6 +25,7 @@ import mindspore.dataset as de import mindspore.dataset.vision.c_transforms as C from mindspore.mindrecord import FileWriter + def bbox_overlaps(bboxes1, bboxes2, mode='iou'): """Calculate the ious between each bbox of bboxes1 and bboxes2. @@ -156,6 +157,7 @@ class Expand: boxes += np.tile((left, top), 2) return img, boxes, labels + def rescale_with_tuple(img, scale): h, w = img.shape[:2] scale_factor = min(max(scale) / max(h, w), min(scale) / min(h, w)) @@ -164,11 +166,13 @@ def rescale_with_tuple(img, scale): return rescaled_img, scale_factor + def rescale_with_factor(img, scale_factor): h, w = img.shape[:2] new_size = int(w * float(scale_factor) + 0.5), int(h * float(scale_factor) + 0.5) return cv2.resize(img, new_size, interpolation=cv2.INTER_NEAREST) + def rescale_column(img, img_shape, gt_bboxes, gt_label, gt_num, config): """rescale operation for image""" img_data, scale_factor = rescale_with_tuple(img, (config.img_width, config.img_height)) @@ -426,17 +430,65 @@ def create_coco_label(is_training, config): return image_files, image_anno_dict -def anno_parser(annos_str): - """Parse annotation from string to list.""" - annos = [] - for anno_str in annos_str: - anno = list(map(int, anno_str.strip().split(','))) - annos.append(anno) +def parse_json_annos_from_txt(anno_file, config): + """for user defined annotations text file, parse it to json format data""" + if not os.path.isfile(anno_file): + raise RuntimeError("Evaluation annotation file {} is not valid.".format(anno_file)) + + annos = { + "images": [], + "annotations": [], + "categories": [] + } + + # set categories field + for i, cls_name in enumerate(config.coco_classes): + annos["categories"].append({"id": i, "name": cls_name}) + + with open(anno_file, "rb") as f: + lines = f.readlines() + + img_id = 1 + anno_id = 1 + for line in lines: + line_str = line.decode("utf-8").strip() + line_split = str(line_str).split(' ') + # set image field + file_name = line_split[0] + annos["images"].append({"file_name": file_name, "id": img_id}) + # set annotations field + for anno_info in line_split[1:]: + anno = anno_info.split(",") + x = float(anno[0]) + y = float(anno[1]) + w = float(anno[2]) - float(anno[0]) + h = float(anno[3]) - float(anno[1]) + category_id = int(anno[4]) + iscrowd = int(anno[5]) + annos["annotations"].append({"bbox": [x, y, w, h], + "area": w * h, + "category_id": category_id, + "iscrowd": iscrowd, + "image_id": img_id, + "id": anno_id}) + anno_id += 1 + img_id += 1 + return annos -def filter_valid_data(image_dir, anno_path): +def create_train_data_from_txt(image_dir, anno_path): """Filter valid image file, which both in image_dir and anno_path.""" + def anno_parser(annos_str): + """Parse annotation from string to list.""" + annos = [] + for anno_str in annos_str: + anno = anno_str.strip().split(",") + xmin, ymin, xmax, ymax = list(map(float, anno[:4])) + cls_id = int(anno[4]) + iscrowd = int(anno[5]) + annos.append([xmin, ymin, xmax, ymax, cls_id, iscrowd]) + return annos image_files = [] image_anno_dict = {} if not os.path.isdir(image_dir): @@ -465,7 +517,7 @@ def data_to_mindrecord_byte_image(config, dataset="coco", is_training=True, pref if dataset == "coco": image_files, image_anno_dict = create_coco_label(is_training, config=config) else: - image_files, image_anno_dict = filter_valid_data(config.IMAGE_DIR, config.ANNO_PATH) + image_files, image_anno_dict = create_train_data_from_txt(config.image_dir, config.anno_path) fasterrcnn_json = { "image": {"type": "bytes"},