forked from Nerogar/OneTrainer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseImageMaskModel.py
More file actions
253 lines (213 loc) · 9.52 KB
/
Copy pathBaseImageMaskModel.py
File metadata and controls
253 lines (213 loc) · 9.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
import os
from abc import ABCMeta, abstractmethod
from typing import Callable
from modules.util import path_util
import torch
from torch import Tensor
from torchvision.transforms import transforms
from PIL import Image
from tqdm import tqdm
class MaskSample:
def __init__(self, filename: str, device: torch.device):
self.image_filename = filename
self.mask_filename = os.path.splitext(filename)[0] + "-masklabel.png"
self.device = device
self.image = None
self.mask_tensor = None
self.height = 0
self.width = 0
self.image2Tensor = transforms.Compose([
transforms.ToTensor(),
])
self.tensor2Image = transforms.Compose([
transforms.ToPILImage(),
])
def get_image(self) -> Image:
if self.image is None:
self.image = Image.open(self.image_filename).convert('RGB')
self.height = self.image.height
self.width = self.image.width
return self.image
def get_mask_tensor(self) -> Tensor:
if self.mask_tensor is None and os.path.exists(self.mask_filename):
mask = Image.open(self.mask_filename).convert('L')
mask = self.image2Tensor(mask)
mask = mask.to(self.device)
self.mask_tensor = mask.unsqueeze(0)
return self.mask_tensor
def set_mask_tensor(self, mask_tensor: Tensor, alpha: float):
self.mask_tensor = alpha * mask_tensor
def add_mask_tensor(self, mask_tensor: Tensor, alpha: float, inverted: bool):
mask = self.get_mask_tensor()
if inverted:
mask_tensor = 1.0 - mask_tensor
if mask is None:
mask = alpha * mask_tensor
else:
torch.add(mask, mask_tensor, alpha=alpha, out=mask)
torch.clamp(mask, 0, 1, out=mask)
self.mask_tensor = mask
def subtract_mask_tensor(self, mask_tensor: Tensor, alpha: float, inverted: bool):
mask = self.get_mask_tensor()
if inverted:
mask_tensor = 1.0 - mask_tensor
if mask is None:
mask = alpha * mask_tensor
else:
torch.subtract(mask, mask_tensor, alpha=alpha, out=mask)
torch.clamp(mask, 0, 1, out=mask)
self.mask_tensor = mask
def blend_mask_tensor(self, mask_tensor: Tensor, alpha: float):
mask = self.get_mask_tensor()
if mask is None:
mask = alpha * mask_tensor
else:
torch.add(mask, mask_tensor, alpha=alpha, out=mask)
if alpha < 0.0:
mask -= alpha
mask /= 1 + alpha
self.mask_tensor = mask
def apply_mask(self, mode: str, mask_tensor: Tensor, alpha: float, inverted: bool):
if mode in {'replace', 'fill'}:
self.set_mask_tensor(mask_tensor, alpha)
elif mode == 'add':
self.add_mask_tensor(mask_tensor, alpha, inverted)
elif mode == 'subtract':
self.subtract_mask_tensor(mask_tensor, alpha, inverted)
elif mode == 'blend':
self.blend_mask_tensor(mask_tensor, alpha)
else:
raise ValueError("invalid mode")
def save_mask(self):
if self.mask_tensor is not None:
mask = self.mask_tensor.cpu().squeeze()
mask = self.tensor2Image(mask).convert('RGB')
mask.save(self.mask_filename)
class BaseImageMaskModel(metaclass=ABCMeta):
@staticmethod
def __get_sample_filenames(sample_dir: str, include_subdirectories: bool = False) -> [str]:
def __is_supported_image_extension(filename: str) -> bool:
ext = os.path.splitext(filename)[1]
return path_util.is_supported_image_extension(ext) and '-masklabel.png' not in filename
filenames = []
if include_subdirectories:
for root, _, files in os.walk(sample_dir):
for filename in files:
if __is_supported_image_extension(filename):
filenames.append(os.path.join(root, filename))
else:
for filename in os.listdir(sample_dir):
if __is_supported_image_extension(filename):
filenames.append(os.path.join(sample_dir, filename))
return filenames
@abstractmethod
def mask_image(
self,
filename: str,
prompts: [str],
mode: str = 'fill',
alpha: float = 1.0,
threshold: float = 0.3,
smooth_pixels: int = 5,
expand_pixels: int = 10
):
"""
Masks a sample
Parameters:
filename (`str`): a sample filename
prompts (`[str]`): a list of prompts used to create a mask
mode (`str`): can be one of
- replace: creates new masks for all samples, even if a mask already exists
- fill: creates new masks for all samples without a mask
- add: adds the new region to existing masks
- subtract: subtracts the new region from existing masks
- blend: blends the new mask with the old one
alpha (`float`): the blending factor to use for modes add, subtract and blend
threshold (`float`): threshold for including pixels in the mask
smooth_pixels (`int`): radius of a smoothing operation applied to the generated mask
expand_pixels (`int`): amount of expansion of the generated mask in all directions
"""
def mask_images(
self,
filenames: list[str],
prompts: list[str],
mode: str = 'fill',
alpha: float = 1.0,
threshold: float = 0.3,
smooth_pixels: int = 5,
expand_pixels: int = 10,
progress_callback: Callable[[int, int], None] = None,
error_callback: Callable[[str], None] = None,
):
"""
Masks all samples in a list
Parameters:
filenames (`[str]`): a list of sample filenames
prompts (`[str]`): a list of prompts used to create a mask
mode (`str`): can be one of
- replace: creates new masks for all samples, even if a mask already exists
- fill: creates new masks for all samples without a mask
- add: adds the new region to existing masks
- subtract: subtracts the new region from existing masks
- blend: blends the new mask with the old one
alpha (`float`): the blending factor to use for modes add, subtract and blend
threshold (`float`): threshold for including pixels in the mask
smooth_pixels (`int`): radius of a smoothing operation applied to the generated mask
expand_pixels (`int`): amount of expansion of the generated mask in all directions
progress_callback (`Callable[[int, int], None]`): called after every processed image
error_callback (`Callable[[str], None]`): called for every exception
"""
if progress_callback is not None:
progress_callback(0, len(filenames))
for i, filename in enumerate(tqdm(filenames)):
try:
self.mask_image(filename, prompts, mode, alpha, threshold, smooth_pixels, expand_pixels)
except Exception:
if error_callback is not None:
error_callback(filename)
if progress_callback is not None:
progress_callback(i + 1, len(filenames))
def mask_folder(
self,
sample_dir: str,
prompts: list[str],
mode: str = 'fill',
threshold: float = 0.3,
smooth_pixels: int = 5,
expand_pixels: int = 10,
alpha: float = 1.0,
progress_callback: Callable[[int, int], None] = None,
error_callback: Callable[[str], None] = None,
include_subdirectories: bool = False,
):
"""
Masks all samples in a folder
Parameters:
sample_dir (`str`): directory where samples are located
prompts (`[str]`): a list of prompts used to create a mask
mode (`str`): can be one of
- replace: creates new masks for all samples, even if a mask already exists
- fill: creates new masks for all samples without a mask
- add: adds the new region to existing masks
- subtract: subtracts the new region from existing masks
- blend: blends the new mask with the old one
alpha (`float`): the blending factor to use for modes add, subtract and blend
threshold (`float`): threshold for including pixels in the mask
smooth_pixels (`int`): radius of a smoothing operation applied to the generated mask
expand_pixels (`int`): amount of expansion of the generated mask in all directions
progress_callback (`Callable[[int, int], None]`): called after every processed image
error_callback (`Callable[[str], None]`): called for every exception
include_subdirectories (`bool`): whether to include subdirectories when processing samples
"""
filenames = self.__get_sample_filenames(sample_dir, include_subdirectories)
self.mask_images(
filenames=filenames,
prompts=prompts,
mode=mode,
alpha=alpha,
threshold=threshold,
smooth_pixels=smooth_pixels,
expand_pixels=expand_pixels,
progress_callback=progress_callback,
error_callback=error_callback,
)