python-wml 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of python-wml might be problematic. Click here for more details.
- python_wml-3.0.0.dist-info/LICENSE +23 -0
- python_wml-3.0.0.dist-info/METADATA +51 -0
- python_wml-3.0.0.dist-info/RECORD +164 -0
- python_wml-3.0.0.dist-info/WHEEL +5 -0
- python_wml-3.0.0.dist-info/top_level.txt +1 -0
- wml/__init__.py +0 -0
- wml/basic_data_def/__init__.py +2 -0
- wml/basic_data_def/detection_data_def.py +279 -0
- wml/basic_data_def/io_data_def.py +2 -0
- wml/basic_img_utils.py +816 -0
- wml/img_patch.py +92 -0
- wml/img_utils.py +571 -0
- wml/iotoolkit/__init__.py +17 -0
- wml/iotoolkit/aic_keypoint.py +115 -0
- wml/iotoolkit/baidu_mask_toolkit.py +244 -0
- wml/iotoolkit/base_dataset.py +210 -0
- wml/iotoolkit/bboxes_statistics.py +515 -0
- wml/iotoolkit/build.py +0 -0
- wml/iotoolkit/cityscapes_toolkit.py +183 -0
- wml/iotoolkit/classification_data_statistics.py +25 -0
- wml/iotoolkit/coco_data_fwd.py +225 -0
- wml/iotoolkit/coco_keypoints.py +118 -0
- wml/iotoolkit/coco_keypoints_fmt2.py +103 -0
- wml/iotoolkit/coco_toolkit.py +397 -0
- wml/iotoolkit/coco_wholebody.py +269 -0
- wml/iotoolkit/common.py +108 -0
- wml/iotoolkit/crowd_pose.py +146 -0
- wml/iotoolkit/fast_labelme.py +110 -0
- wml/iotoolkit/image_folder.py +95 -0
- wml/iotoolkit/imgs_cache.py +58 -0
- wml/iotoolkit/imgs_reader_mt.py +73 -0
- wml/iotoolkit/labelme_base.py +102 -0
- wml/iotoolkit/labelme_json_to_img.py +49 -0
- wml/iotoolkit/labelme_toolkit.py +117 -0
- wml/iotoolkit/labelme_toolkit_fwd.py +733 -0
- wml/iotoolkit/labelmemckeypoints_dataset.py +169 -0
- wml/iotoolkit/lspet.py +48 -0
- wml/iotoolkit/mapillary_vistas_toolkit.py +269 -0
- wml/iotoolkit/mat_data.py +90 -0
- wml/iotoolkit/mckeypoints_statistics.py +28 -0
- wml/iotoolkit/mot_datasets.py +62 -0
- wml/iotoolkit/mpii.py +108 -0
- wml/iotoolkit/npmckeypoints_dataset.py +164 -0
- wml/iotoolkit/o365_to_coco.py +136 -0
- wml/iotoolkit/object365_toolkit.py +156 -0
- wml/iotoolkit/object365v2_toolkit.py +71 -0
- wml/iotoolkit/pascal_voc_data.py +51 -0
- wml/iotoolkit/pascal_voc_toolkit.py +194 -0
- wml/iotoolkit/pascal_voc_toolkit_fwd.py +473 -0
- wml/iotoolkit/penn_action.py +57 -0
- wml/iotoolkit/rawframe_dataset.py +129 -0
- wml/iotoolkit/rewrite_pascal_voc.py +28 -0
- wml/iotoolkit/semantic_data.py +49 -0
- wml/iotoolkit/split_file_by_type.py +29 -0
- wml/iotoolkit/sports_mot_datasets.py +78 -0
- wml/iotoolkit/vis_objectdetection_dataset.py +70 -0
- wml/iotoolkit/vis_torch_data.py +39 -0
- wml/iotoolkit/yolo_toolkit.py +38 -0
- wml/object_detection2/__init__.py +4 -0
- wml/object_detection2/basic_visualization.py +37 -0
- wml/object_detection2/bboxes.py +812 -0
- wml/object_detection2/data_process_toolkit.py +146 -0
- wml/object_detection2/keypoints.py +292 -0
- wml/object_detection2/mask.py +120 -0
- wml/object_detection2/metrics/__init__.py +3 -0
- wml/object_detection2/metrics/build.py +15 -0
- wml/object_detection2/metrics/classifier_toolkit.py +440 -0
- wml/object_detection2/metrics/common.py +71 -0
- wml/object_detection2/metrics/mckps_toolkit.py +338 -0
- wml/object_detection2/metrics/toolkit.py +1953 -0
- wml/object_detection2/npod_toolkit.py +361 -0
- wml/object_detection2/odtools.py +243 -0
- wml/object_detection2/standard_names.py +75 -0
- wml/object_detection2/visualization.py +956 -0
- wml/object_detection2/wmath.py +34 -0
- wml/semantic/__init__.py +0 -0
- wml/semantic/basic_toolkit.py +65 -0
- wml/semantic/mask_utils.py +156 -0
- wml/semantic/semantic_test.py +21 -0
- wml/semantic/structures.py +1 -0
- wml/semantic/toolkit.py +105 -0
- wml/semantic/visualization_utils.py +658 -0
- wml/threadtoolkit.py +50 -0
- wml/walgorithm.py +228 -0
- wml/wcollections.py +212 -0
- wml/wfilesystem.py +487 -0
- wml/wml_utils.py +657 -0
- wml/wstructures/__init__.py +4 -0
- wml/wstructures/common.py +9 -0
- wml/wstructures/keypoints_train_toolkit.py +149 -0
- wml/wstructures/kps_structures.py +579 -0
- wml/wstructures/mask_structures.py +1161 -0
- wml/wtorch/__init__.py +8 -0
- wml/wtorch/bboxes.py +104 -0
- wml/wtorch/classes_suppression.py +24 -0
- wml/wtorch/conv_module.py +181 -0
- wml/wtorch/conv_ws.py +144 -0
- wml/wtorch/data/__init__.py +16 -0
- wml/wtorch/data/_utils/__init__.py +45 -0
- wml/wtorch/data/_utils/collate.py +183 -0
- wml/wtorch/data/_utils/fetch.py +47 -0
- wml/wtorch/data/_utils/pin_memory.py +121 -0
- wml/wtorch/data/_utils/signal_handling.py +72 -0
- wml/wtorch/data/_utils/worker.py +227 -0
- wml/wtorch/data/base_data_loader_iter.py +93 -0
- wml/wtorch/data/dataloader.py +501 -0
- wml/wtorch/data/datapipes/__init__.py +1 -0
- wml/wtorch/data/datapipes/iter/__init__.py +12 -0
- wml/wtorch/data/datapipes/iter/batch.py +126 -0
- wml/wtorch/data/datapipes/iter/callable.py +92 -0
- wml/wtorch/data/datapipes/iter/listdirfiles.py +37 -0
- wml/wtorch/data/datapipes/iter/loadfilesfromdisk.py +30 -0
- wml/wtorch/data/datapipes/iter/readfilesfromtar.py +60 -0
- wml/wtorch/data/datapipes/iter/readfilesfromzip.py +63 -0
- wml/wtorch/data/datapipes/iter/sampler.py +94 -0
- wml/wtorch/data/datapipes/utils/__init__.py +0 -0
- wml/wtorch/data/datapipes/utils/common.py +65 -0
- wml/wtorch/data/dataset.py +354 -0
- wml/wtorch/data/datasets/__init__.py +4 -0
- wml/wtorch/data/datasets/common.py +53 -0
- wml/wtorch/data/datasets/listdirfilesdataset.py +36 -0
- wml/wtorch/data/datasets/loadfilesfromdiskdataset.py +30 -0
- wml/wtorch/data/distributed.py +135 -0
- wml/wtorch/data/multi_processing_data_loader_iter.py +866 -0
- wml/wtorch/data/sampler.py +267 -0
- wml/wtorch/data/single_process_data_loader_iter.py +24 -0
- wml/wtorch/data/test_data_loader.py +26 -0
- wml/wtorch/dataset_toolkit.py +67 -0
- wml/wtorch/depthwise_separable_conv_module.py +98 -0
- wml/wtorch/dist.py +591 -0
- wml/wtorch/dropblock/__init__.py +6 -0
- wml/wtorch/dropblock/dropblock.py +228 -0
- wml/wtorch/dropblock/dropout.py +40 -0
- wml/wtorch/dropblock/scheduler.py +48 -0
- wml/wtorch/ema.py +61 -0
- wml/wtorch/fc_module.py +73 -0
- wml/wtorch/functional.py +34 -0
- wml/wtorch/iter_dataset.py +26 -0
- wml/wtorch/loss.py +69 -0
- wml/wtorch/nets/__init__.py +0 -0
- wml/wtorch/nets/ckpt_toolkit.py +219 -0
- wml/wtorch/nets/fpn.py +276 -0
- wml/wtorch/nets/hrnet/__init__.py +0 -0
- wml/wtorch/nets/hrnet/config.py +2 -0
- wml/wtorch/nets/hrnet/hrnet.py +494 -0
- wml/wtorch/nets/misc.py +249 -0
- wml/wtorch/nets/resnet/__init__.py +0 -0
- wml/wtorch/nets/resnet/layers/__init__.py +17 -0
- wml/wtorch/nets/resnet/layers/aspp.py +144 -0
- wml/wtorch/nets/resnet/layers/batch_norm.py +231 -0
- wml/wtorch/nets/resnet/layers/blocks.py +111 -0
- wml/wtorch/nets/resnet/layers/wrappers.py +110 -0
- wml/wtorch/nets/resnet/r50_config.py +38 -0
- wml/wtorch/nets/resnet/resnet.py +691 -0
- wml/wtorch/nets/shape_spec.py +20 -0
- wml/wtorch/nets/simple_fpn.py +101 -0
- wml/wtorch/nms.py +109 -0
- wml/wtorch/nn.py +896 -0
- wml/wtorch/ocr_block.py +193 -0
- wml/wtorch/summary.py +331 -0
- wml/wtorch/train_toolkit.py +603 -0
- wml/wtorch/transformer_blocks.py +266 -0
- wml/wtorch/utils.py +719 -0
- wml/wtorch/wlr_scheduler.py +100 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from wml.iotoolkit.mat_data import MatData
|
|
2
|
+
import wml.object_detection2.keypoints as odk
|
|
3
|
+
import numpy as np
|
|
4
|
+
import wml.object_detection2.bboxes as odb
|
|
5
|
+
import glob
|
|
6
|
+
import os.path as osp
|
|
7
|
+
'''
|
|
8
|
+
1. head
|
|
9
|
+
2. left_shoulder 3. right_shoulder
|
|
10
|
+
4. left_elbow 5. right_elbow
|
|
11
|
+
6. left_wrist 7. right_wrist
|
|
12
|
+
8. left_hip 9. right_hip
|
|
13
|
+
10. left_knee 11. right_knee
|
|
14
|
+
12. left_ankle 13. right_ankle
|
|
15
|
+
'''
|
|
16
|
+
def __read_one_file(file_path):
|
|
17
|
+
data = MatData(file_path).data
|
|
18
|
+
x = data['x'].astype(np.float32)
|
|
19
|
+
y = data['y'].astype(np.float32)
|
|
20
|
+
visibility = data['visibility'].astype(np.float32)
|
|
21
|
+
kps = np.stack([x,y,visibility],axis=-1)
|
|
22
|
+
bbox = data['bbox'].astype(np.float32)
|
|
23
|
+
|
|
24
|
+
return kps,bbox
|
|
25
|
+
|
|
26
|
+
def read_penn_action_data(labels_path):
|
|
27
|
+
all_files = glob.glob(osp.join(labels_path,"*.mat"))
|
|
28
|
+
|
|
29
|
+
res = []
|
|
30
|
+
for file in all_files:
|
|
31
|
+
kps,bbox = __read_one_file(file)
|
|
32
|
+
res.append([file,kps,bbox])
|
|
33
|
+
|
|
34
|
+
return res
|
|
35
|
+
|
|
36
|
+
class Trans2COCO:
|
|
37
|
+
def __init__(self) -> None:
|
|
38
|
+
self.dst_idxs = [5,6,7,8,9,10,11,12,13,14,15,16]
|
|
39
|
+
self.src_idxs = np.array([2,3,4,5,6,7,8,9,10,11,12,13],dtype=np.int32)-1
|
|
40
|
+
self.coco_idxs = [0,1,2,3,4]
|
|
41
|
+
|
|
42
|
+
def __call__(self,mpii_kps,coco_kps):
|
|
43
|
+
if len(mpii_kps.shape)==2:
|
|
44
|
+
return self.trans_one(mpii_kps,coco_kps)
|
|
45
|
+
res = []
|
|
46
|
+
for mp,coco in zip(mpii_kps,coco_kps):
|
|
47
|
+
res.append(self.trans_one(mp,coco))
|
|
48
|
+
return np.array(res)
|
|
49
|
+
|
|
50
|
+
def trans_one(self,mpii_kps,coco_kps):
|
|
51
|
+
'''
|
|
52
|
+
img: [RGB]
|
|
53
|
+
'''
|
|
54
|
+
res = np.zeros([17,3],dtype=np.float32)
|
|
55
|
+
res[self.dst_idxs] = mpii_kps[self.src_idxs]
|
|
56
|
+
res[self.coco_idxs] = coco_kps[self.coco_idxs]
|
|
57
|
+
return res
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import os.path as osp
|
|
2
|
+
import wml.wml_utils as wmlu
|
|
3
|
+
import copy
|
|
4
|
+
import random
|
|
5
|
+
from typing import Type
|
|
6
|
+
from itertools import chain
|
|
7
|
+
from collections import namedtuple
|
|
8
|
+
|
|
9
|
+
VideoInfo = namedtuple('VideoInfo',["dir_path","frames","label"])
|
|
10
|
+
|
|
11
|
+
class RawFrameDataset:
|
|
12
|
+
get_item_in_namedtuple = False
|
|
13
|
+
def __init__(self,file_path=None) -> None:
|
|
14
|
+
self.data = []
|
|
15
|
+
self.dirname = None
|
|
16
|
+
if file_path is not None:
|
|
17
|
+
self.read(file_path)
|
|
18
|
+
|
|
19
|
+
@classmethod
|
|
20
|
+
def create(cls,other, data=None):
|
|
21
|
+
ds = cls()
|
|
22
|
+
if data is not None:
|
|
23
|
+
ds.data = data
|
|
24
|
+
else:
|
|
25
|
+
ds.data = copy.deepcopy(other.data)
|
|
26
|
+
|
|
27
|
+
ds.dirname = other.dirname
|
|
28
|
+
|
|
29
|
+
return ds
|
|
30
|
+
|
|
31
|
+
def read(self,file_path):
|
|
32
|
+
self.data = []
|
|
33
|
+
self.dirname = osp.dirname(file_path)
|
|
34
|
+
with open(file_path,"r") as f:
|
|
35
|
+
lines = f.readlines()
|
|
36
|
+
|
|
37
|
+
for line in lines:
|
|
38
|
+
line = line.strip()
|
|
39
|
+
if "#" in line:
|
|
40
|
+
datas = line.split("#")
|
|
41
|
+
else:
|
|
42
|
+
datas = line.split(" ")
|
|
43
|
+
self.data.append([datas[0],int(datas[1]),int(datas[2])]) #sub dir name, total frames, type
|
|
44
|
+
|
|
45
|
+
def write(self,file_name,spliter="#"):
|
|
46
|
+
save_path = osp.join(self.dirname,file_name)
|
|
47
|
+
print(f"Save path {save_path}")
|
|
48
|
+
with open(save_path,"w") as f:
|
|
49
|
+
for dirname,frames_nr,f_type in self.data:
|
|
50
|
+
f.write(f"{dirname}{spliter}{frames_nr}{spliter}{f_type}\n")
|
|
51
|
+
|
|
52
|
+
def __len__(self):
|
|
53
|
+
return len(self.data)
|
|
54
|
+
|
|
55
|
+
def __getitem__(self,idx):
|
|
56
|
+
data = self.data[idx]
|
|
57
|
+
if self.get_item_in_namedtuple:
|
|
58
|
+
return VideoInfo(osp.join(self.dirname,data[0]),data[1],data[2])
|
|
59
|
+
else:
|
|
60
|
+
return osp.join(self.dirname,data[0]),data[1],data[2]
|
|
61
|
+
|
|
62
|
+
def show_statistics_info(self):
|
|
63
|
+
print(f"Total {len(self.data)} files.")
|
|
64
|
+
counter = wmlu.MDict(dvalue=0)
|
|
65
|
+
for dirname,frames_nr,f_type in self.data:
|
|
66
|
+
counter[f_type] += 1
|
|
67
|
+
|
|
68
|
+
datas = list(zip(counter.keys(),counter.values()))
|
|
69
|
+
datas.sort(key=lambda x:x[0])
|
|
70
|
+
for f_type,nr in datas:
|
|
71
|
+
print(f"Type {f_type:5d}: {nr}")
|
|
72
|
+
|
|
73
|
+
def split_data(self,percent=[0.5,-1], shuffle=True,per_classes=True):
|
|
74
|
+
'''
|
|
75
|
+
按percent分割数据,-1表示所有剩余的数据
|
|
76
|
+
'''
|
|
77
|
+
res = []
|
|
78
|
+
if not per_classes:
|
|
79
|
+
datas = RawFrameDataset.simple_split_data(self.data,percent,shuffle)
|
|
80
|
+
for data in datas:
|
|
81
|
+
res.append(RawFrameDataset.create(self,data))
|
|
82
|
+
return res
|
|
83
|
+
else:
|
|
84
|
+
type_dict = self.get_type_dict()
|
|
85
|
+
|
|
86
|
+
all_res = []
|
|
87
|
+
for k,v in type_dict.items():
|
|
88
|
+
datas = RawFrameDataset.simple_split_data(v,percent,shuffle)
|
|
89
|
+
all_res.append(datas)
|
|
90
|
+
|
|
91
|
+
datas = list(zip(*all_res))
|
|
92
|
+
for data in datas:
|
|
93
|
+
data = list(chain(*data))
|
|
94
|
+
if shuffle:
|
|
95
|
+
random.shuffle(data)
|
|
96
|
+
res.append(RawFrameDataset.create(self,data))
|
|
97
|
+
|
|
98
|
+
return res
|
|
99
|
+
|
|
100
|
+
def get_type_dict(self):
|
|
101
|
+
res = wmlu.MDict(dtype=list)
|
|
102
|
+
for data in self.data:
|
|
103
|
+
res[data[-1]].append(data)
|
|
104
|
+
|
|
105
|
+
return res
|
|
106
|
+
|
|
107
|
+
@staticmethod
|
|
108
|
+
def simple_split_data(data,percent=[0.5,-1], shuffle=True):
|
|
109
|
+
data = copy.deepcopy(data)
|
|
110
|
+
total_nr = len(data)
|
|
111
|
+
if shuffle:
|
|
112
|
+
random.shuffle(data)
|
|
113
|
+
res = []
|
|
114
|
+
for x in percent:
|
|
115
|
+
if x>=0:
|
|
116
|
+
nr = int(total_nr*x+0.5)
|
|
117
|
+
res.append(data[:nr])
|
|
118
|
+
data = data[nr:]
|
|
119
|
+
else:
|
|
120
|
+
res.append(data)
|
|
121
|
+
break
|
|
122
|
+
return res
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#coding=utf-8
|
|
2
|
+
import os
|
|
3
|
+
import wml.object_detection2.utils as odu
|
|
4
|
+
import wml.wml_utils as wmlu
|
|
5
|
+
import shutil
|
|
6
|
+
|
|
7
|
+
def rewrite(input_dir,output_dir,image_sub_dir="JPEGImages",xml_sub_dir="Annotations",img_suffix=".jpg",begin_index=1):
|
|
8
|
+
if output_dir is None:
|
|
9
|
+
output_dir = os.path.join(input_dir,"output")
|
|
10
|
+
if not os.path.isdir(output_dir):
|
|
11
|
+
os.makedirs(output_dir)
|
|
12
|
+
files = odu.getVOCFiles(input_dir,image_sub_dir=image_sub_dir,xml_sub_dir=xml_sub_dir,img_suffix=img_suffix)
|
|
13
|
+
print(f"Nr {len(files)}")
|
|
14
|
+
index = begin_index
|
|
15
|
+
for img_file,xml_file in files:
|
|
16
|
+
suffix = wmlu.suffix(img_file)
|
|
17
|
+
img_out_path = os.path.join(output_dir,f"IMG_{index:0>4}."+suffix)
|
|
18
|
+
xml_out_path = os.path.join(output_dir,f"IMG_{index:0>4}."+"xml")
|
|
19
|
+
if os.path.exists(img_out_path):
|
|
20
|
+
print(f"Error file {img_out_path} exists.")
|
|
21
|
+
break
|
|
22
|
+
print(img_file,"->",img_out_path)
|
|
23
|
+
print(xml_file,"->",xml_out_path)
|
|
24
|
+
index += 1
|
|
25
|
+
|
|
26
|
+
shutil.copy(img_file,img_out_path)
|
|
27
|
+
shape, bboxes, labels_text, difficult, truncated = odu.read_voc_xml(xml_file)
|
|
28
|
+
odu.write_voc_xml(xml_out_path, img_out_path, shape, bboxes, labels_text, difficult,truncated)
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from PIL import Image
|
|
2
|
+
import os
|
|
3
|
+
from wml.semantic.visualization_utils import RGB_STANDARD_COLORS
|
|
4
|
+
import wml.wml_utils as wmlu
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
class SemanticData(object):
|
|
8
|
+
def __init__(self,img_suffix=".jpg",label_suffix=".png",img_sub_dir=None,label_sub_dir=None,
|
|
9
|
+
color_map=None):
|
|
10
|
+
if color_map is None:
|
|
11
|
+
color_map = RGB_STANDARD_COLORS
|
|
12
|
+
self.color_map = []
|
|
13
|
+
for x in color_map:
|
|
14
|
+
self.color_map += list(x)
|
|
15
|
+
self.img_suffix = img_suffix
|
|
16
|
+
self.label_suffix = label_suffix
|
|
17
|
+
self.img_sub_dir = img_sub_dir
|
|
18
|
+
self.label_sub_dir = label_sub_dir
|
|
19
|
+
self.files = []
|
|
20
|
+
|
|
21
|
+
def read_data(self,dir_path):
|
|
22
|
+
label_dir = dir_path if self.label_sub_dir is None else os.path.join(dir_path,self.label_sub_dir)
|
|
23
|
+
img_dir = dir_path if self.img_sub_dir is None else os.path.join(dir_path,self.img_sub_dir)
|
|
24
|
+
image_files = wmlu.recurse_get_filepath_in_dir(img_dir,suffix=self.img_suffix)
|
|
25
|
+
self.files = []
|
|
26
|
+
for ifn in image_files:
|
|
27
|
+
base_name = wmlu.base_name(ifn)
|
|
28
|
+
label_path = os.path.join(label_dir,base_name+self.label_suffix)
|
|
29
|
+
if not os.path.exists(label_path):
|
|
30
|
+
print(f"ERROR: Find label file {label_path} for image {ifn} faild.")
|
|
31
|
+
continue
|
|
32
|
+
else:
|
|
33
|
+
self.files.append([ifn,label_path])
|
|
34
|
+
|
|
35
|
+
def __len__(self):
|
|
36
|
+
return len(self.files)
|
|
37
|
+
|
|
38
|
+
def get_items(self):
|
|
39
|
+
for i in range(len(self.files)):
|
|
40
|
+
yield self.__getitem__(i)
|
|
41
|
+
|
|
42
|
+
def __getitem__(self, item):
|
|
43
|
+
ifn,lfn = self.files[item]
|
|
44
|
+
img = Image.open(ifn).convert('RGB')
|
|
45
|
+
'''
|
|
46
|
+
mask:[H,W], value is label
|
|
47
|
+
'''
|
|
48
|
+
mask = Image.open(lfn)
|
|
49
|
+
return [ifn,lfn],np.array(img),np.array(mask)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import shutil
|
|
2
|
+
import wml.wml_utils as wmlu
|
|
3
|
+
import os
|
|
4
|
+
from wml.iotoolkit.pascal_voc_toolkit import PascalVOCData
|
|
5
|
+
|
|
6
|
+
def copy_to_dir(files,save_dir):
|
|
7
|
+
wmlu.create_empty_dir(save_dir,remove_if_exists=False)
|
|
8
|
+
for f in files:
|
|
9
|
+
wmlu.safe_copy(f,save_dir)
|
|
10
|
+
|
|
11
|
+
def split_file_by_type(datas,save_dir,get_buddy_files=None):
|
|
12
|
+
for data in datas:
|
|
13
|
+
img_file, shape, labels, labels_names, bboxes, _, _, difficult, _ = data
|
|
14
|
+
names = set(labels_names)
|
|
15
|
+
files = [img_file]
|
|
16
|
+
if get_buddy_files is not None:
|
|
17
|
+
files = files + get_buddy_files(img_file)
|
|
18
|
+
|
|
19
|
+
for name in names:
|
|
20
|
+
t_save_dir = os.path.join(save_dir,name)
|
|
21
|
+
copy_to_dir(files,t_save_dir)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
if __name__ == "__main__":
|
|
25
|
+
dataset = PascalVOCData()
|
|
26
|
+
dataset.read_data("/3_data/wj/mldata/cell/deep_data/")
|
|
27
|
+
def get_buddy_file(img_file):
|
|
28
|
+
return [wmlu.change_suffix(img_file,"xml")]
|
|
29
|
+
split_file_by_type(dataset.get_items(),"/3_data/wj/mldata/cell/deep_data_split2",get_buddy_files=get_buddy_file)
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import wml.wml_utils as wmlu
|
|
2
|
+
import os
|
|
3
|
+
import numpy as np
|
|
4
|
+
import os.path as osp
|
|
5
|
+
from .common import *
|
|
6
|
+
|
|
7
|
+
class SportsMOTDatasets(object):
|
|
8
|
+
def __init__(self,dirs,absolute_coord=False,use_det=False):
|
|
9
|
+
if not isinstance(dirs,list):
|
|
10
|
+
dirs = [dirs]
|
|
11
|
+
self.dirs = dirs
|
|
12
|
+
self.tid_curr = 0
|
|
13
|
+
self.dir_curr = 0
|
|
14
|
+
self.absolute_coord = absolute_coord
|
|
15
|
+
self.use_det = use_det
|
|
16
|
+
|
|
17
|
+
def get_data_items(self):
|
|
18
|
+
dir_idx = -1
|
|
19
|
+
for seq_root in self.dirs:
|
|
20
|
+
seqs = wmlu.get_subdir_in_dir(seq_root)
|
|
21
|
+
for seq in seqs:
|
|
22
|
+
dir_idx += 1
|
|
23
|
+
seq_info = open(os.path.join(seq_root, seq, 'seqinfo.ini')).read()
|
|
24
|
+
seq_width = int(seq_info[seq_info.find('imWidth=') + 8:seq_info.find('\nimHeight')])
|
|
25
|
+
seq_height = int(seq_info[seq_info.find('imHeight=') + 9:seq_info.find('\nimExt')])
|
|
26
|
+
|
|
27
|
+
if self.use_det:
|
|
28
|
+
gt_txt = osp.join(seq_root,seq,"det","det.txt")
|
|
29
|
+
else:
|
|
30
|
+
gt_txt = os.path.join(seq_root, seq, 'gt', 'gt.txt')
|
|
31
|
+
if not os.path.exists(gt_txt):
|
|
32
|
+
print(f"{gt_txt} not exists.")
|
|
33
|
+
continue
|
|
34
|
+
gt = np.loadtxt(gt_txt, dtype=np.float64, delimiter=',')
|
|
35
|
+
idx = np.lexsort(gt.T[:2, :])
|
|
36
|
+
gt = gt[idx, :]
|
|
37
|
+
|
|
38
|
+
fid_datas = {}
|
|
39
|
+
for v in gt:
|
|
40
|
+
fid, tid, x, y, w, h, score,*_ = v[:9]
|
|
41
|
+
if not self.use_det and score == 0:
|
|
42
|
+
continue
|
|
43
|
+
if fid in fid_datas:
|
|
44
|
+
fid_datas[fid].append(v[:9])
|
|
45
|
+
else:
|
|
46
|
+
fid_datas[fid] = [v[:9]]
|
|
47
|
+
tid_to_cls = {}
|
|
48
|
+
for fid,datas in fid_datas.items():
|
|
49
|
+
labels = []
|
|
50
|
+
bboxes = []
|
|
51
|
+
for _, tid, x, y, w, h, *_ in datas:
|
|
52
|
+
fid = int(fid)
|
|
53
|
+
tid = int(tid)
|
|
54
|
+
if not tid in tid_to_cls:
|
|
55
|
+
self.tid_curr += 1
|
|
56
|
+
tid_to_cls[tid] = self.tid_curr
|
|
57
|
+
cls = self.tid_curr
|
|
58
|
+
else:
|
|
59
|
+
cls = tid_to_cls[tid]
|
|
60
|
+
if not self.absolute_coord:
|
|
61
|
+
xmin = x/seq_width
|
|
62
|
+
ymin = y/seq_height
|
|
63
|
+
xmax = (x+w)/seq_width
|
|
64
|
+
ymax = (y+h)/seq_height
|
|
65
|
+
else:
|
|
66
|
+
xmin = x
|
|
67
|
+
ymin = y
|
|
68
|
+
xmax = x + w
|
|
69
|
+
ymax = y + h
|
|
70
|
+
labels.append(cls)
|
|
71
|
+
bboxes.append([ymin,xmin,ymax,xmax])
|
|
72
|
+
|
|
73
|
+
if len(labels)>0:
|
|
74
|
+
img_name = '{:06d}.jpg'.format(fid)
|
|
75
|
+
img_path = os.path.join(seq_root,seq,"img1",img_name)
|
|
76
|
+
yield DetData(img_path, [seq_height,seq_width], labels, None, bboxes, None, None, None,dir_idx)
|
|
77
|
+
|
|
78
|
+
print(f"Last tid value {self.tid_curr}")
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import wml.img_utils as wmli
|
|
2
|
+
import random
|
|
3
|
+
import matplotlib.pyplot as plt
|
|
4
|
+
import time
|
|
5
|
+
import wml.wml_utils as wmlu
|
|
6
|
+
import os.path as osp
|
|
7
|
+
import wml.object_detection2.visualization as odv
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
def trans_name(name:str):
|
|
11
|
+
return name.replace("/","_")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def vis_dataset(dataset,save_dir,max_nr_per_dir=20,max_view_imgs=2000,is_relative_coordinate=False):
|
|
15
|
+
wmlu.create_empty_dir_remove_if(save_dir)
|
|
16
|
+
idxs = list(range(len(dataset)))
|
|
17
|
+
random.seed(time.time())
|
|
18
|
+
idxs = idxs[:max_view_imgs]
|
|
19
|
+
random.shuffle(idxs)
|
|
20
|
+
|
|
21
|
+
def text_fn(classes, scores):
|
|
22
|
+
return dataset.id2name[classes]
|
|
23
|
+
|
|
24
|
+
counter = wmlu.MDict(dvalue=0)
|
|
25
|
+
for id,name in dataset.id2name.items():
|
|
26
|
+
counter[id] = 0
|
|
27
|
+
|
|
28
|
+
for idx in idxs:
|
|
29
|
+
x = dataset[idx]
|
|
30
|
+
full_path, shape,category_ids, category_names, boxes, binary_mask, area, is_crowd, num_annotations_skipped = x
|
|
31
|
+
category_ids = np.array(category_ids)
|
|
32
|
+
boxes = np.array(boxes)
|
|
33
|
+
need_view = False
|
|
34
|
+
for id in category_ids:
|
|
35
|
+
if counter[id]<max_nr_per_dir:
|
|
36
|
+
need_view = True
|
|
37
|
+
break
|
|
38
|
+
if not need_view:
|
|
39
|
+
continue
|
|
40
|
+
|
|
41
|
+
img = wmli.imread(full_path)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
t_img = odv.draw_bboxes(
|
|
45
|
+
img=np.copy(img), classes=category_ids, scores=None, bboxes=boxes, color_fn=None,
|
|
46
|
+
text_fn=text_fn, thickness=1,
|
|
47
|
+
show_text=True,
|
|
48
|
+
font_scale=0.8,
|
|
49
|
+
is_relative_coordinate=is_relative_coordinate)
|
|
50
|
+
file_basename = osp.basename(full_path)
|
|
51
|
+
save_path = osp.join(save_dir,"all",file_basename)
|
|
52
|
+
wmli.imwrite(save_path,t_img)
|
|
53
|
+
|
|
54
|
+
for id in set(category_ids):
|
|
55
|
+
if counter[id]<max_nr_per_dir:
|
|
56
|
+
mask = category_ids==id
|
|
57
|
+
t_labels = category_ids[mask]
|
|
58
|
+
t_boxes = boxes[mask]
|
|
59
|
+
t_img = odv.draw_bboxes(
|
|
60
|
+
img=np.copy(img), classes=t_labels, scores=None, bboxes=t_boxes, color_fn=None,
|
|
61
|
+
text_fn=text_fn, thickness=1,
|
|
62
|
+
show_text=True,
|
|
63
|
+
font_scale=0.8,
|
|
64
|
+
is_relative_coordinate=is_relative_coordinate)
|
|
65
|
+
counter[id] = counter[id]+1
|
|
66
|
+
t_save_path = osp.join(save_dir,str(id)+"_"+trans_name(dataset.id2name[id]),file_basename)
|
|
67
|
+
wmlu.make_dir_for_file(t_save_path)
|
|
68
|
+
wmli.imwrite(t_save_path,t_img)
|
|
69
|
+
if np.all(np.array(list(counter.values()))>max_nr_per_dir):
|
|
70
|
+
break
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from wml.iotoolkit.coco_toolkit import *
|
|
2
|
+
import wml.img_utils as wmli
|
|
3
|
+
import wml.object_detection2.visualization as odv
|
|
4
|
+
import os.path as osp
|
|
5
|
+
import random
|
|
6
|
+
import time
|
|
7
|
+
|
|
8
|
+
def trans_file_name(filename,image_dir):
|
|
9
|
+
names = filename.split("/")[-2:]
|
|
10
|
+
return osp.join(*names)
|
|
11
|
+
random.seed(time.time())
|
|
12
|
+
COCOData.trans_file_name = trans_file_name
|
|
13
|
+
data = TorchCOCOData("/home/wj/ai/mldata/o365/train",
|
|
14
|
+
"/home/wj/ai/mldata/o365/train/zhiyuan_objv2_train.json")
|
|
15
|
+
save_dir = "tmp/imgs"
|
|
16
|
+
|
|
17
|
+
wmlu.create_empty_dir_remove_if(save_dir)
|
|
18
|
+
|
|
19
|
+
max_show_nr = 1000
|
|
20
|
+
idxs = list(range(len(data)))
|
|
21
|
+
random.shuffle(idxs)
|
|
22
|
+
idxs = idxs[:max_show_nr]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
for idx in idxs:
|
|
26
|
+
img,gts = data[idx]
|
|
27
|
+
bboxes = [x['bbox'] for x in gts]
|
|
28
|
+
labels = np.array([x['category_id'] for x in gts])
|
|
29
|
+
bboxes = np.array(bboxes)
|
|
30
|
+
bboxes[:,2:] = bboxes[:,:2]+bboxes[:,2:]
|
|
31
|
+
img = np.array(img)
|
|
32
|
+
img = odv.draw_bboxes_xy(
|
|
33
|
+
img=img, classes=labels, scores=None, bboxes=bboxes, color_fn=None,
|
|
34
|
+
thickness=4,
|
|
35
|
+
show_text=True,
|
|
36
|
+
font_scale=0.8)
|
|
37
|
+
save_path = osp.join(save_dir,f"{idx}.jpg")
|
|
38
|
+
print(f"Save {save_path}")
|
|
39
|
+
wmli.imwrite(save_path,img)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import wml.object_detection2.bboxes as odb
|
|
2
|
+
import wml.img_utils as wmli
|
|
3
|
+
|
|
4
|
+
def write_yolo_txt(save_path,img_path,labels,bboxes):
|
|
5
|
+
'''
|
|
6
|
+
bboxes: [y0,x0,y1,x1]
|
|
7
|
+
'''
|
|
8
|
+
h,w = wmli.get_img_size(img_path)[:2]
|
|
9
|
+
bboxes = odb.absolutely_boxes_to_relative_boxes(bboxes,width=w,height=h)
|
|
10
|
+
bboxes = odb.npchangexyorder(bboxes) #(x0,y0,x1,y1)
|
|
11
|
+
bboxes = odb.npto_cyxhw(bboxes) #->(cx,cy,w,h)
|
|
12
|
+
with open(save_path,"w") as f:
|
|
13
|
+
for l,bbox in zip(labels,bboxes):
|
|
14
|
+
#每一行为label cx cy w h
|
|
15
|
+
f.write(f"{l} {bbox[0]} {bbox[1]} {bbox[2]} {bbox[3]}\n")
|
|
16
|
+
|
|
17
|
+
def write_yoloseg_txt(save_path,img_path,labels,masks):
|
|
18
|
+
'''
|
|
19
|
+
masks:WPolygonMasks
|
|
20
|
+
'''
|
|
21
|
+
h,w = wmli.get_img_size(img_path)[:2]
|
|
22
|
+
with open(save_path,"w") as f:
|
|
23
|
+
for l,mask_items in zip(labels,masks):
|
|
24
|
+
#每一行为label x0 y0 x1 y1 ...
|
|
25
|
+
if len(mask_items.points) ==0:
|
|
26
|
+
continue
|
|
27
|
+
points = mask_items.points[0]
|
|
28
|
+
|
|
29
|
+
if len(points)<3:
|
|
30
|
+
continue
|
|
31
|
+
info = f"{l} "
|
|
32
|
+
|
|
33
|
+
for p in points:
|
|
34
|
+
x = p[0]/w
|
|
35
|
+
y = p[1]/h
|
|
36
|
+
info += f"{x} {y} "
|
|
37
|
+
info += "\n"
|
|
38
|
+
f.write(info)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#coding=utf-8
|
|
2
|
+
import numpy as np
|
|
3
|
+
from PIL import Image
|
|
4
|
+
|
|
5
|
+
def convert_semantic_to_rgb(semantic,color_map,return_nparray=False):
|
|
6
|
+
'''
|
|
7
|
+
convert semantic label map to rgb PIL image or a np.ndarray
|
|
8
|
+
Args:
|
|
9
|
+
semantic: [H,W] label value
|
|
10
|
+
color_map: list[int], [r0,g0,b0,r1,g1,b1,....]
|
|
11
|
+
Returns:
|
|
12
|
+
image: [H,W,3]
|
|
13
|
+
'''
|
|
14
|
+
new_mask = Image.fromarray(semantic.astype(np.uint8)).convert('P')
|
|
15
|
+
new_mask.putpalette(color_map)
|
|
16
|
+
if return_nparray:
|
|
17
|
+
return np.array(new_mask.convert('RGB'))
|
|
18
|
+
return new_mask
|
|
19
|
+
|
|
20
|
+
def draw_semantic_on_image(image,semantic,color_map,alpha=0.4,ignored_label=0):
|
|
21
|
+
'''
|
|
22
|
+
draw semantic on image
|
|
23
|
+
Args:
|
|
24
|
+
image:
|
|
25
|
+
semantic: [H,W] label value
|
|
26
|
+
color_map: list[int], [r0,g0,b0,r1,g1,b1,....]
|
|
27
|
+
alpha: mask percent
|
|
28
|
+
ignored_label:
|
|
29
|
+
Returns:
|
|
30
|
+
return image*(1-alpha)+semantic+alpha
|
|
31
|
+
'''
|
|
32
|
+
mask = convert_semantic_to_rgb(semantic,color_map=color_map,return_nparray=True)
|
|
33
|
+
new_img = image.astype(np.float32)*(1-alpha)+mask.astype(np.float32)*alpha
|
|
34
|
+
new_img = np.clip(new_img,0,255).astype(np.uint8)
|
|
35
|
+
pred = np.expand_dims(semantic!=ignored_label,axis=-1)
|
|
36
|
+
new_img = np.where(pred,new_img,image)
|
|
37
|
+
return new_img
|