import glob
import tensorflow as tf
from datasets.DetectionDataset import MapillaryLikeDetectionFileListDataset
from datasets.KITTI.segtrack.KITTI_MOTS_info import SEQ_IDS_TRAIN, SEQ_IDS_VAL
from datasets.Loader import register_dataset
from datasets.util.Util import username
from datasets import DataKeys
from shutil import copytree
from random import randint
from core.Log import log
import os
NAME = "KITTI_segtrack"
NAME_DETECTION = "KITTI_segtrack_detection"
DEFAULT_PATH = "/globalwork/" + username() + "/data/KITTI_MOTS/train/"
ID_DIVISOR = 1000
CLASS_IDS_WITH_INSTANCES = [1, 2]
CROWD_ID = 10
NUM_CLASSES = 3 # background, car, pedestrian
N_MAX_DETECTIONS = 100
用于检测单个图像
# used for detection on individual images
@register_dataset(NAME_DETECTION)
class KittiSegtrackDetectionDataset(MapillaryLikeDetectionFileListDataset):
# 得到验证集seq_ids_train和训练集seq_ids_val的序号
def __init__(self, config, subset, name=NAME, default_path=DEFAULT_PATH):
self.seq_ids_train = SEQ_IDS_TRAIN
self.seq_ids_val = SEQ_IDS_VAL
self.imgs_are_pngs = config.bool("imgs_are_pngs", True)
t = config.string_list("seq_ids_train", []) #得到训练集的序号
if t:
self.seq_ids_train = t
v = config.string_list("seq_ids_val", []) #得到验证集的序号
if v:
self.seq_ids_val = v
super().__init__(config, name, subset, default_path, NUM_CLASSES, N_MAX_DETECTIONS, CLASS_IDS_WITH_INSTANCES,
ID_DIVISOR, crowd_id=CROWD_ID)
self.copy_dataset_to_tmp = config.bool("copy_dataset_to_tmp", False)
if self.copy_dataset_to_tmp:
print("Copying dataset to $TMP!", file=log.v1)
new_path = "$TMP/" + str(randint(1, 100000))
new_path = os.path.expandvars(new_path)
os.makedirs(new_path)
print("Copying images...", file=log.v1)
copytree(self.data_dir + "/images", new_path + "/images")
print("Copying instances...", file=log.v1)
copytree(self.data_dir + "/instances", new_path + "/instances")
self.data_dir = new_path
def read_inputfile_lists(self):
seq_ids = self.seq_ids_train if self.subset == "train" else self.seq_ids_val
anns = []
for seq_id in seq_ids:
anns += sorted(glob.glob(self.data_dir + "/instances/" + seq_id + "/*.png"))
# sorted:按升序返回包含迭代中所有项目的新列表。
# 可以提供自定义关键字函数来自定义排序顺序,并且可以设置反向标志以得到降序结果。
# glob:返回与路径名称模式匹配的路径列表。
# 该模式可能包含简单的shell样式通配符la fnmatch。但是,与fnmatch不同,以点开头的文件名是特殊情况,与“ * ”和“?”不匹配模式。
# 如果递归为真,则模式'**'将匹配任何文件和零个或多个目录和子目录。
imgs = [x.replace("/instances/", "/images/") for x in anns] #将ann中所有/instances/替换为/images/
#Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
if not self.imgs_are_pngs: #如果图片不是png格式,那么替换为jpg格式,mots图片都是png格式
imgs = [x.replace(".png", ".jpg") for x in imgs]
return imgs, anns
- 不明白为什么要先匹配实例/instances/再替换为图像/images/,直接为图像/images/不行吗?