srproj_reader
data.dataset.srproj_reader
ImageLoader
여러 형태의 데이터를 입력으로 받아, 전처리 과정을 거쳐 PIL Image 혹은 np.ndarray 로 return 합니다. 현재 지원하는 데이터는 다음과 같습니다. - 데이터 타입: [image path, np.ndarray, PIL Image] - color: ["RGB", "Gray"] - bit: [8, 16]
입력으로 받은 데이터에 따른 출력값은 다음과 같습니다. | image path | np.ndarray | PIL | RGB; 8 | PIL(RGB;8) | PIL(RGB;8) | PIL(RGB;8) | RGB;16 | PIL(RGB;8) | PIL(RGB;8) | - | Gray; 8 | PIL(L;8) | PIL(L;8) | PIL(L;8) | Gray;16 | PIL(L;8) | PIL(L;8) | PIL(L;8) |
Note1
PIL은 RGB;16을 지원하지 않습니다. 따라서 PIL(RGB;16)은 입력으로 들어올 수 없습니다.
Note2
PIL은 "I;16" 모드로 Gray;16을 지원하지만, PIL 내부의 convert 함수를 써서 Gray;8로 변환시 값이 overflow 나는 issue가 있습니다.
Note3
이미지는 기본적으로 np.ndarray로 로드되며, PIL.Image.Image로 로드하려면 to_numpy=False를 세팅하세요.
Note4
image_mode는 "RGB", "L" 중 하나를 지원하며, multipage (이미지 리스트) 인 경우 각 페이지의 모드를 리스트로 입력하세요.
예시: 1, 3번째 페이지는 RGB 이고 2번째 페이지는 L 인 경우 image_mode = ["RGB", "L", "RGB"]
Source code in SaigeToolkit/data/transform/image_load.py
__call__
call_method
make [PIL Image or np.ndarray] from PIL.Image, np.ndarray, or path string
Parameters:
-
image(Union[Image, ndarray, str, List]) –PIL.Image, array, path string or list of it.
Returns:
-
Dict(Dict) –{ "image": Union[PIL.Image.Image, np.ndarray, List[PIL.Image.Image], List[np.ndarray]], **input_dict, }
Source code in SaigeToolkit/data/transform/image_load.py
load
Source code in SaigeToolkit/data/transform/image_load.py
load_from_pil
Source code in SaigeToolkit/data/transform/image_load.py
load_from_array
Source code in SaigeToolkit/data/transform/image_load.py
load_from_path
Loading function using PIL.Image library. This function is introduced because {exif_transpose} must be processed. {exif_transpose} fix rotated image binary data using {EXIF} information. Without {exif_transpose}, network would accidently learn randomly rotated image data.
Parameters:
-
path(str) –path to image file
Returns:
-
Union[Image, ndarray]–Union[Image.Image, np.ndarray]: image
Source code in SaigeToolkit/data/transform/image_load.py
_load_from_plain_file_path
staticmethod
Loading function using PIL.Image library. This function is introduced because {exif_transpose} must be processed. {exif_transpose} fix rotated image binary data using {EXIF} information. Without {exif_transpose}, network would accidently learn randomly rotated image data.
Parameters:
-
path(str) –path to image file
Returns:
-
Image–Image.Image: image
Source code in SaigeToolkit/data/transform/image_load.py
_load_from_encrypted_file_path
staticmethod
Loading function using PIL.Image library and decrypting encrypted image file.
Parameters:
-
path(str) –encrypted image file path
Returns:
-
Image–Image.Image: decrypted image
Source code in SaigeToolkit/data/transform/image_load.py
_convert_16_to_8
staticmethod
이미지 픽셀당 비트수를 16에서 8로 변화합니다. 입출력 데이터 타입이 같습니다. (pil로 받으면 pil을 ndarray로 받을 시 ndarray를 출력합니다.)
Source code in SaigeToolkit/data/transform/image_load.py
_is_16bit
staticmethod
Source code in SaigeToolkit/data/transform/image_load.py
read_image_size
read_image_size(image: Union[Image, Tensor, ndarray]) -> ImageSizeType
image size: (W, H)
Source code in SaigeToolkit/data/transform/image_function.py
path_interpreter
data path interpreter
Parameters:
-
code(str) –dataset code (rule: {domain}-{source}-{category}) (ex) sample dataset for cls: "test_directory-sample-cls")
Raises:
-
RuntimeError–invalid classifier keys (code) for srproj code.
-
Exception–wrong type of input code
Returns:
-
Tuple[str, str, str, str]–Tuple[str, str, str, str]: absolute path for dataset, domain, source, category
Source code in SaigeToolkit/data/dataset/srproj_reader.py
read_srproj
read_srproj(code: str, split: str, two_class: bool = False, mode: Optional[str] = None, use_absolute_path: bool = False, srproj_image_directory: Optional[str] = None, system_image_directory: Optional[str] = None, get_class_colors: bool = False, skip_problematic_files: bool = False) -> Tuple[List[Dict], int, Union[List[str], List[Dict]]]
read srproj dataset from file-system.
Parameters:
-
code(str) –dataset code.
-
split(str) –split dataset, "Training" or "Validation".
-
two_class(bool, default:False) –load data as two class mode (ex) normal <-> abnormal)
-
mode(Optional[str], default:None) –select data label type.
-
use_absolute_path(bool, default:False) –use the image path in srproj as is.
-
srproj_image_directory(Optional[str], default:None) –srproj 파일에 작성된 이미지 경로를 현재 시스템 상의 이미지 경로로 변환하기 위해 치환해야 하는 이미지 폴더 경로. None이면 SaigeDatabase 규칙 사용.
-
system_image_directory(Optional[str], default:None) –코드가 구동되는 시스템 상의 이미지 폴더 경로. None이면 SaigeDatabase 규칙 사용.
-
get_class_colors(bool)–srproj에 정의된 각 클래스의 color를 받을지 여부. True이면
-
skip_problematic_files(bool)–srproj 파일에 작성된 이미지 경로로부터 이미지들을 읽을 때, 해당 파라미터가 True 라면, 파일이 실제 존재하지 않거나, 읽다가 문제가 발생할 경우 해당 파일을 제외하고 나머지 파일을 계속 읽음. 만약, False라면 Error를 raise 함.
Returns:
-
Tuple[List[Dict], int, Union[List[str], List[Dict]]]–Tuple[List[Dict], int, Union[List[str], List[Dict]]]: list of meta data dicts, number of classes, and list of class names or dict
Note
.sproj 파일에는 각 이미지들의 경로와 라벨 정보 등이 포함되어 있습니다.
이때 이미지 경로는 해당 파일을 생성한 PC 기준의 절대 경로로 작성되어 있기 때문에, 현재 이 코드가 실행되는 시스템에서의 경로로 변환해 주어야 합니다.
현재 이미지 경로 변환 옵션은 3가지가 존재합니다.
1. sproj를 생성한 PC에서 코드를 실행하는 경우,
use_absolute_path= True로 세팅하면 srproj에 작성된 이미지 경로를 그대로 사용합니다.
2. 이미지와 srproj가 SaigeDatabase 룰에 맞게 구성된 경우,
이미지는 images 폴더에 하위에 있고 (root/images/.../xxx.png)
srproj는 images 폴더보다 한 단계 아래 경로에 있어야 합니다 (root/projects/xxx.srproj)
이때는srproj_image_directory= None,system_image_directory= None 으로 설정합니다.
3. 이미지와 srproj가 SaigeDatabase 룰을 따르지 않는 경우,srproj_image_directory와system_image_directory`를 설정하면
srproj에 작성된 이미지 경로를 다음 규칙으로 변환합니다:
{srproj_image_directory}/.../xxx.png -> {system_image_directory}/.../xxx.png
Source code in SaigeToolkit/data/dataset/srproj_reader.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
check_label
check all data label is valid. If any data is corrupted, pop out from meta data list.
Parameters:
-
files(List[Dict]) –meta data list to be checked
-
mode(str) –label data type
Returns:
-
List[Dict]–List[Dict]: cleaned meta data list
Source code in SaigeToolkit/data/dataset/srproj_reader.py
load_srproj_label
load single data dict from srproj meta data.
Parameters:
-
file(Dict) –single meta data dict
-
use_crop_fn(bool, default:False) –Whether to use the crop function. Defaults to False.
Raises:
-
NotImplementedError–only four modes are available. ["Classification", "Detection", "Segmentation", "OpticalCharacterRecognition"]
Returns:
-
Dict(Dict) –label data as dict
Source code in SaigeToolkit/data/dataset/srproj_reader.py
load_label_cls
load_label_det
load detection label
Parameters:
-
file(Dict) –meta data for detection label
Returns:
-
Dict(Dict) –
Source code in SaigeToolkit/data/dataset/srproj_reader.py
load_label_seg
load segmentation label
Parameters:
-
file(Dict) –meta data for segmentation label
-
return_polygon(bool, default:True) –whether return polygon data. Defaults to True.
-
mask_to_pil(bool, default:False) –whether return mask as PIL.Image. Defaults to False.
Returns:
-
Dict(Dict) –segmentation label data dict
Source code in SaigeToolkit/data/dataset/srproj_reader.py
load_label_ocr
load ocr label
Parameters:
-
file(Dict) –meta data for ocr label
Returns:
-
Dict(Dict) –ocr label data dict
Source code in SaigeToolkit/data/dataset/srproj_reader.py
_refine_kie_labels
Source code in SaigeToolkit/data/dataset/srproj_reader.py
merge_srproj_classes
Source code in SaigeToolkit/data/dataset/srproj_reader.py
convert_srproj_class_color_to_rgb
srproj의 각 클래스 색 코드를 rgb 값으로 변환합니다.