Skip to content

Commit f35188c

Browse files
committed
add data pattern
1 parent 44acddd commit f35188c

6 files changed

+168
-0
lines changed

dwf/common/exception.py

+5
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ def __init__(self, status, msg):
4747
Exceptions for Development
4848
'''
4949
NOT_IMPLEMENTED = DWFException(5001, '方法未实现')
50+
51+
'''
52+
Exceptions for Data Pattern
53+
'''
54+
DATA_PATTERN_MISMATCH = DWFException(6001, '数据模式不匹配')

dwf/datapattern/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- coding:utf-8 -*-
2+
3+
from pkgutil import extend_path
4+
5+
__path__ = extend_path(__path__, __name__)
6+
7+
__all__ = ['metadata']

dwf/datapattern/metadata/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- coding:utf-8 -*-
2+
3+
from pkgutil import extend_path
4+
5+
__path__ = extend_path(__path__, __name__)
6+
7+
__all__ = ['base_pattern', '2imageFolder4Detection_txt']
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import json
2+
3+
class BasePattern(object):
4+
5+
def __init__(self):
6+
pass
7+
8+
def dumps(self):
9+
return json.dumps(self, default=lambda o: o.__dict__)
10+
11+
def load(self, json_str):
12+
self.__dict__ = json.loads(json_str)
13+
14+
def check(self, folder_path):
15+
raise NotImplementedError
16+
17+
def generate(self, folder_path):
18+
raise NotImplementedError
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from dwf.datapattern.metadata.base_pattern import BasePattern
2+
from dwf.common.exception import DATA_PATTERN_MISMATCH
3+
from torchvision.datasets.folder import ImageFolder
4+
import os
5+
import cv2
6+
from PIL import Image
7+
8+
class ImageFolder4Classfication(BasePattern):
9+
def __init__(self):
10+
super(ImageFolder4Classfication, self).__init__()
11+
self.data_type = 'image'
12+
self.organization = 'imageFolder4Classfication'
13+
self.algos = 'resnet'
14+
self.organization_parameter_width = None
15+
self.organization_parameter_height = None
16+
self.organization_parameter_channel = None
17+
self.organization_parameter_preprocess_resize_need = True
18+
self.organization_parameter_preprocess_resize_size = 224
19+
self.organization_parameter_preprocess_crop_need = True
20+
self.organization_parameter_preprocess_crop_need = 256
21+
self.organization_parameter_preprocess_shuffle_need = True
22+
self.organization_parameter_preprocess_normalization_need = True
23+
self.organization_parameter_preprocess_normalization_mean = [0.5, 0.5, 0.5]
24+
self.organization_parameter_preprocess_normalization_std = [0.5, 0.5, 0.5]
25+
26+
self.semantic = '10'
27+
28+
def check(self, folder_path):
29+
try:
30+
dataset_pending = ImageFolder(root=folder_path)
31+
except:
32+
raise DATA_PATTERN_MISMATCH
33+
return True
34+
35+
def generate(self, folder_path):
36+
max_width = 0
37+
min_width = 10000
38+
max_height = 0
39+
min_height = 10000
40+
channel_num = None
41+
for sub_dir in os.listdir(folder_path):
42+
for file in os.listdir(os.path.join(folder_path, sub_dir)):
43+
im = cv2.imread(os.path.join(folder_path, sub_dir,file))
44+
width, height, channel = im.shape[0],im.shape[1],im.shape[2]
45+
if channel_num is None:
46+
channel_num = channel
47+
if channel != channel_num:
48+
raise DATA_PATTERN_MISMATCH
49+
max_width = max(max_width, width)
50+
min_width = min(min_width, width)
51+
max_height = max(max_height, height)
52+
min_height = min(min_height, height)
53+
54+
self.organization_parameter_width = [min_width, max_width]
55+
self.organization_parameter_height = [min_height, max_height]
56+
self.organization_parameter_channel = channel_num
57+
return self.dumps()
58+
59+
# pattern = ImageFolder4Classfication()
60+
# print(pattern.generate('/Users/sherry/Desktop/0920crop'))
61+
62+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from dwf.datapattern.metadata.base_pattern import BasePattern
2+
from dwf.common.exception import DATA_PATTERN_MISMATCH
3+
import os
4+
import cv2
5+
from PIL import Image
6+
7+
class TwoImageFolder4Detection_txt(BasePattern):
8+
def __init__(self):
9+
super(TwoImageFolder4Detection_txt, self).__init__()
10+
self.data_type = 'image'
11+
self.organization = 'twoImageFolder4Detection@txt'
12+
self.algos = 'ssd'
13+
self.organization_parameter_width = None
14+
self.organization_parameter_height = None
15+
self.organization_parameter_channel = None
16+
self.organization_parameter_preprocess_resize_need = True
17+
self.organization_parameter_preprocess_resize_size = 224
18+
self.organization_parameter_preprocess_crop_need = True
19+
self.organization_parameter_preprocess_crop_need = 256
20+
self.organization_parameter_preprocess_shuffle_need = True
21+
self.organization_parameter_preprocess_normalization_need = True
22+
self.organization_parameter_preprocess_normalization_mean = [0.5, 0.5, 0.5]
23+
self.organization_parameter_preprocess_normalization_std = [0.5, 0.5, 0.5]
24+
25+
self.semantic = '01'
26+
27+
def check(self, folder_path):
28+
image_path = os.path.join(folder_path, 'Images')
29+
label_path = os.path.join(folder_path, 'Labels')
30+
31+
if not os.path.exists(image_path):
32+
raise DATA_PATTERN_MISMATCH
33+
34+
if not os.path.exists(label_path):
35+
raise DATA_PATTERN_MISMATCH
36+
37+
image_list = os.listdir(image_path)
38+
label_list = os.listdir(label_path)
39+
40+
if len(image_list) != len(label_list):
41+
raise DATA_PATTERN_MISMATCH
42+
return True
43+
44+
def generate(self, folder_path):
45+
max_width = 0
46+
min_width = 10000
47+
max_height = 0
48+
min_height = 10000
49+
channel_num = None
50+
for file in os.listdir(os.path.join(folder_path, 'Images')):
51+
im = cv2.imread(os.path.join(folder_path, 'Images',file))
52+
width, height, channel = im.shape[0],im.shape[1],im.shape[2]
53+
if channel_num is None:
54+
channel_num = channel
55+
if channel != channel_num:
56+
raise DATA_PATTERN_MISMATCH
57+
max_width = max(max_width, width)
58+
min_width = min(min_width, width)
59+
max_height = max(max_height, height)
60+
min_height = min(min_height, height)
61+
self.organization_parameter_width = [min_width, max_width]
62+
self.organization_parameter_height = [min_height, max_height]
63+
self.organization_parameter_channel = channel_num
64+
return self.dumps()
65+
66+
# pattern = TwoImageFolder4Detection_txt()
67+
# print(pattern.generate('/Users/sherry/Desktop/xlearn_data/1228'))
68+
69+

0 commit comments

Comments
 (0)