Commit a8d1cee4 authored by suilin0432's avatar suilin0432
Browse files

update

parent c3b671e2
Pipeline #2707 failed with stages
in 3 minutes and 34 seconds
_BASE_: "../Base-RCNN-C4.yaml"
MODEL:
WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl"
MASK_ON: False
RESNETS:
DEPTH: 50
ROI_HEADS:
NUM_CLASSES: 20
INPUT:
# detectron2 VOC 上的 标准配置
# MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800)
# WSOD 中很多方法的标准配置
# MIN_SIZE_TRAIN: (480, 576, 688, 864, 1200)
# WSOD 中 DRN 使用的配置
MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800, 832, 864, 896, 928, 960, 992, 1024, 1056, 1088, 1120, 1152, 1184, 1216)
MAX_SIZE_TRAIN: 2000
MIN_SIZE_TEST: 688
MAX_SIZE_TEST: 4000
DATASETS:
TRAIN: ('voc_2007_train_wsl', 'voc_2007_val_wsl')
TEST: ('voc_2007_test', )
SOLVER:
STEPS: (8000, 10500)
MAX_ITER: 12000
WARMUP_ITERS: 100
IMS_PER_BATCH: 4
BASE_LR: 0.005
OUTPUT_DIR: "output/faster_rcnn_R50_C4_wsl_b4_lr005/"
TEST:
EVAL_PERIOD: 2000
_BASE_: "../Base-RCNN-FPN.yaml"
MODEL:
WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl"
MASK_ON: False
RESNETS:
DEPTH: 50
ROI_HEADS:
NUM_CLASSES: 20
INPUT:
MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800, 832, 864, 896, 928, 960, 992, 1024, 1056, 1088, 1120, 1152, 1184, 1216)
MAX_SIZE_TRAIN: 2000
MIN_SIZE_TEST: 688
MAX_SIZE_TEST: 4000
DATASETS:
TRAIN: ('voc_2007_train_wsl', 'voc_2007_val_wsl')
TEST: ('voc_2007_test',)
SOLVER:
STEPS: (8000, 10500)
MAX_ITER: 12000
WARMUP_ITERS: 100
IMS_PER_BATCH: 4
BASE_LR: 0.005
OUTPUT_DIR: "output/faster_rcnn_FPN_C4_wsl_b4_lr005/"
TEST:
EVAL_PERIOD: 2000
_BASE_: "../Base-RCNN-FPN.yaml"
MODEL:
WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl"
MASK_ON: False
RESNETS:
DEPTH: 50
ROI_HEADS:
NUM_CLASSES: 20
INPUT:
MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800, 832, 864, 896, 928, 960, 992, 1024, 1056, 1088, 1120, 1152, 1184, 1216)
MAX_SIZE_TRAIN: 2000
MIN_SIZE_TEST: 688
MAX_SIZE_TEST: 4000
DATASETS:
TRAIN: ('voc_2007_train_wsl_top1', 'voc_2007_val_wsl_top1')
TEST: ('voc_2007_test',)
SOLVER:
STEPS: (8000, 10500)
MAX_ITER: 12000
WARMUP_ITERS: 100
IMS_PER_BATCH: 4
BASE_LR: 0.005
OUTPUT_DIR: "output/faster_rcnn_R50_FPN_wsl_top1_b4_lr005/"
TEST:
EVAL_PERIOD: 2000
......@@ -27,7 +27,7 @@ from .cityscapes_panoptic import register_all_cityscapes_panoptic
from .coco import load_sem_seg, register_coco_instances
from .coco_panoptic import register_coco_panoptic, register_coco_panoptic_separated
from .lvis import get_lvis_instances_meta, register_lvis_instances
from .pascal_voc import register_pascal_voc
from .pascal_voc import register_pascal_voc, register_pascal_voc_wsl, register_pascal_voc_wsl_top1
# ==== Predefined datasets and splits for COCO ==========
......@@ -231,7 +231,27 @@ def register_all_pascal_voc(root):
year = 2007 if "2007" in name else 2012
register_pascal_voc(name, os.path.join(root, dirname), split, year)
MetadataCatalog.get(name).evaluator_type = "pascal_voc"
def register_all_pascal_voc_wsl(root):
SPLITS = [
("voc_2007_train_wsl", "VOC2007", "train"),
("voc_2007_val_wsl", "VOC2007", "val")
]
for name, dirname, split in SPLITS:
year = 2007 if "2007" in name else 2012
register_pascal_voc_wsl(name, os.path.join(root, dirname), split, year)
MetadataCatalog.get(name).evaluator_type = "pascal_voc"
def register_all_pascal_voc_wsl_top1(root):
SPLITS = [
("voc_2007_train_wsl_top1", "VOC2007", "train"),
("voc_2007_val_wsl_top1", "VOC2007", "val")
]
for name, dirname, split in SPLITS:
year = 2007 if "2007" in name else 2012
register_pascal_voc_wsl_top1(name, os.path.join(root, dirname), split, year)
MetadataCatalog.get(name).evaluator_type = "pascal_voc"
def register_all_ade20k(root):
root = os.path.join(root, "ADEChallengeData2016")
......@@ -261,4 +281,6 @@ if __name__.endswith(".builtin"):
register_all_cityscapes(_root)
register_all_cityscapes_panoptic(_root)
register_all_pascal_voc(_root)
register_all_pascal_voc_wsl(_root)
register_all_pascal_voc_wsl_top1(_root)
register_all_ade20k(_root)
......@@ -2,6 +2,7 @@
# Copyright (c) Facebook, Inc. and its affiliates.
import numpy as np
import json
import os
import xml.etree.ElementTree as ET
from typing import List, Tuple, Union
......@@ -10,7 +11,7 @@ from detectron2.data import DatasetCatalog, MetadataCatalog
from detectron2.structures import BoxMode
from detectron2.utils.file_io import PathManager
__all__ = ["load_voc_instances", "register_pascal_voc"]
__all__ = ["load_voc_instances", "register_pascal_voc", "register_pascal_voc_wsl", "register_pascal_voc_wsl_top1"]
# fmt: off
......@@ -74,9 +75,138 @@ def load_voc_instances(dirname: str, split: str, class_names: Union[List[str], T
dicts.append(r)
return dicts
def load_voc_instances_wsl(dirname: str, split: str, class_names: Union[List[str], Tuple[str, ...]]):
# 获取 数据集对应划分(train, val, test) 图片 ids
with PathManager.open(os.path.join(dirname, "ImageSets", "Main", split + ".txt")) as f:
fileids = np.loadtxt(f, dtype=np.str)
# 获取 annotations, wsl 预测之后的结果会保存为 json 的格式
annotation_wsl = json.load(open(
"{}/voc07_wsl_{}_contain.json".format(dirname, split), "r"
))
annotation_dirname = PathManager.get_local_path(os.path.join(dirname, "Annotations/"))
dicts = []
for fileid in fileids:
anno = annotation_wsl[str(int(fileid))]
jpeg_file = os.path.join(dirname, "JPEGImages", fileid + ".jpg")
anno_file = os.path.join(annotation_dirname, fileid + ".xml")
if not os.path.isfile(anno_file):
with Image.open(jpeg_file) as img:
width, height = img.size
r = {"file_name": jpeg_file, "image_id": fileid, "height": height, "width": width}
instances = []
for obj in anno:
bbox = obj["bbox"]
bbox = [int(i) for i in bbox] # 因为 predict 出来的 bbox 是float, 要转化为 int list
category_id = obj["category_id"] - 1 # 因为保存统计时将 index + 1 了从而方便 TIDE 统计了, 因此这里需要 - 1
instances.append(
{
"category_id": category_id, "bbox": bbox, "bbox_mode": BoxMode.XYXY_ABS
}
)
r["annotations"] = instances
dicts.append(r)
continue
with PathManager.open(anno_file) as f:
tree = ET.parse(f)
r = {
"file_name": jpeg_file,
"image_id": fileid,
"height": int(tree.findall("./size/height")[0].text),
"width": int(tree.findall("./size/width")[0].text),
}
instances = []
# 这里是从 annotation_wsl 中进行 gt 信息的提取, 而不是 从 anno file 中提取真正的 gt 信息出来
for obj in anno:
bbox = obj["bbox"]
bbox = [int(i) for i in bbox]
category_id = obj["category_id"] - 1
instances.append(
{
"category_id": category_id, "bbox": bbox, "bbox_mode": BoxMode.XYXY_ABS
}
)
r["annotations"] = instances
dicts.append(r)
return dicts
def load_voc_instances_wsl_top1(dirname: str, split: str, class_names: Union[List[str], Tuple[str, ...]]):
# 获取 数据集对应划分(train, val, test) 图片 ids
with PathManager.open(os.path.join(dirname, "ImageSets", "Main", split + ".txt")) as f:
fileids = np.loadtxt(f, dtype=np.str)
# 获取 annotations, wsl 预测之后的结果会保存为 json 的格式
annotation_wsl = json.load(open(
"{}/voc07_wsl_{}_top1.json".format(dirname, split), "r"
))
annotation_dirname = PathManager.get_local_path(os.path.join(dirname, "Annotations/"))
dicts = []
for fileid in fileids:
anno = annotation_wsl[str(int(fileid))]
jpeg_file = os.path.join(dirname, "JPEGImages", fileid + ".jpg")
anno_file = os.path.join(annotation_dirname, fileid + ".xml")
if not os.path.isfile(anno_file):
with Image.open(jpeg_file) as img:
width, height = img.size
r = {"file_name": jpeg_file, "image_id": fileid, "height": height, "width": width}
instances = []
for obj in anno:
bbox = obj["bbox"]
bbox = [int(i) for i in bbox] # 因为 predict 出来的 bbox 是float, 要转化为 int list
category_id = obj["category_id"] - 1 # 因为保存统计时将 index + 1 了从而方便 TIDE 统计了, 因此这里需要 - 1
instances.append(
{
"category_id": category_id, "bbox": bbox, "bbox_mode": BoxMode.XYXY_ABS
}
)
r["annotations"] = instances
dicts.append(r)
continue
with PathManager.open(anno_file) as f:
tree = ET.parse(f)
r = {
"file_name": jpeg_file,
"image_id": fileid,
"height": int(tree.findall("./size/height")[0].text),
"width": int(tree.findall("./size/width")[0].text),
}
instances = []
# 这里是从 annotation_wsl 中进行 gt 信息的提取, 而不是 从 anno file 中提取真正的 gt 信息出来
for obj in anno:
bbox = obj["bbox"]
bbox = [int(i) for i in bbox]
category_id = obj["category_id"] - 1
instances.append(
{
"category_id": category_id, "bbox": bbox, "bbox_mode": BoxMode.XYXY_ABS
}
)
r["annotations"] = instances
dicts.append(r)
return dicts
def register_pascal_voc(name, dirname, split, year, class_names=CLASS_NAMES):
DatasetCatalog.register(name, lambda: load_voc_instances(dirname, split, class_names))
MetadataCatalog.get(name).set(
thing_classes=list(class_names), dirname=dirname, year=year, split=split
)
# 新加一个可以指定Annotations地址的 voc 数据集注册
def register_pascal_voc_wsl(name, dirname, split, year, class_names=CLASS_NAMES):
DatasetCatalog.register(name, lambda: load_voc_instances_wsl(dirname, split, class_names))
MetadataCatalog.get(name).set(
thing_classes=list(class_names), dirname=dirname, year=year, split=split
)
# top1 pgt 版本的注册
def register_pascal_voc_wsl_top1(name, dirname, split, year, class_names=CLASS_NAMES):
DatasetCatalog.register(name, lambda: load_voc_instances_wsl_top1(dirname, split, class_names))
MetadataCatalog.get(name).set(
thing_classes=list(class_names), dirname=dirname, year=year, split=split
)
\ No newline at end of file
......@@ -109,6 +109,17 @@ class PascalVOCDetectionEvaluator(DatasetEvaluator):
)
aps[thresh].append(ap * 100)
results_tex = ""
for ap in aps[50]:
results_tex += "{:.2f}&".format(ap)
results_tex += "{:.2f}&".format(np.mean(aps[50]))
self._logger.info("AP50 Results: " + results_tex)
results_tex_75 = ""
for ap in aps[75]:
results_tex_75 += "{:.2f}&".format(ap)
results_tex_75 += "{:.2f}&".format(np.mean(aps[75]))
self._logger.info("AP75 Results: " + results_tex_75)
ret = OrderedDict()
mAP = {iou: np.mean(x) for iou, x in aps.items()}
ret["bbox"] = {"AP": np.mean(list(mAP.values())), "AP50": mAP[50], "AP75": mAP[75]}
......
CUDA_VISIBLE_DEVICES=0,1 python tools/train_net.py \
--config-file configs/PascalVOC-Detection/faster_rcnn_R_50_C4_wsl.yaml \
--num-gpus 2 --dist-url tcp://0.0.0.0:13192 \
OUTPUT_DIR output/fsod/voc_faster_rcnn_DC5_baseline_wsl_lr_005_B_4/ \
SOLVER.BASE_LR 0.005 SOLVER.IMS_PER_BATCH 4
CUDA_VISIBLE_DEVICES=0,1 python tools/train_net.py \
--config-file configs/PascalVOC-Detection/faster_rcnn_R_50_FPN_wsl.yaml \
--num-gpus 2 --dist-url tcp://0.0.0.0:13192 \
OUTPUT_DIR output/fsod/voc_faster_rcnn_FPN_baseline_wsl_lr_002_B_4/ \
SOLVER.BASE_LR 0.002 SOLVER.IMS_PER_BATCH 4
CUDA_VISIBLE_DEVICES=0,1 python tools/train_net.py \
--config-file configs/PascalVOC-Detection/faster_rcnn_R_50_FPN_wsl_top1.yaml \
--num-gpus 2 --dist-url tcp://0.0.0.0:14192 \
OUTPUT_DIR output/fsod/voc_faster_rcnn_FPN_baseline_wsl_top1_lr_002_B_4/ \
SOLVER.BASE_LR 0.002 SOLVER.IMS_PER_BATCH 4
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment