Spaces:
Sleeping
Sleeping
File size: 11,482 Bytes
b066d77 |
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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
import os
from pathlib import Path
import cv2
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
import imagehash
from typing import Callable
from datetime import datetime as dt
from abc import ABC, abstractmethod
_DATASET_AVG_MEAN = 129.38489987766278
_DATASET_AVG_STD = 54.084109207654805
def save_to_file(location: str = './extracted_paths.txt') -> Callable:
def outer_wrapper(fn: Callable) -> Callable:
def inner_wrapper(*args, **kwargs):
paths: list[str] = fn(*args, **kwargs)
if kwargs.get('to_file'):
with open(location, 'a') as file:
file.write('\nFiles to remove [TIMESTAMP {}]:\n'.format(dt.now().strftime('%Y%m%d%H%M%S')))
for p in paths:
file.write(f'{p}\n')
return paths
return inner_wrapper
return outer_wrapper
def visualize(show_limit: int = -1) -> Callable:
def outer_wrapper(fn: Callable) -> Callable:
def inner_wrapper(*args, **kwargs):
paths: list[str] = fn(*args, **kwargs)
if kwargs.get('visualize_'):
if show_limit != -1:
paths = paths[:show_limit]
num_cols = 8
num_rows = len(paths) // num_cols + 1
fig = plt.figure(figsize=(8, 8))
for i, path in enumerate(paths, start=1):
plt.subplot(num_rows, num_cols, i)
plt.imshow(Image.open(path), cmap='gray')
plt.title(f'{Path(path).parent.name}', fontsize=7)
plt.axis('off')
fig.tight_layout()
plt.tight_layout()
fig.subplots_adjust(hspace=0.6, top=0.97)
plt.show()
return paths
return inner_wrapper
return outer_wrapper
class DataFilter(ABC):
def __init__(self):
self.paths = []
@abstractmethod
def extract(self, data_dir: str | Path, visualize_: bool, to_file: bool) -> list[str]:
pass
@abstractmethod
def clear(self) -> None:
pass
@abstractmethod
def filter(self) -> bool:
pass
@staticmethod
def _load_data(dir_: str) -> tuple[list[np.ndarray], list[str], list[str]]:
images = []
class_names = []
paths = []
for path in Path(dir_).glob('**/*.jpg'):
label = path.parent.name
image = cv2.imread(str(path), cv2.IMREAD_GRAYSCALE)
if image is not None and label is not None:
images.append(np.array(image))
class_names.append(label)
paths.append(str(path))
return images, class_names, paths
class DataFilterCompose(DataFilter):
def __init__(self, components: list[DataFilter]):
super().__init__()
self.components = components
@staticmethod
def build(components: list[DataFilter]) -> DataFilter:
return DataFilterCompose(components)
def extract(self, data_dir: str | Path, visualize_: bool, to_file: bool) -> list[str]:
extracted_paths = []
for component in self.components:
cur_extracted_paths = component.extract(data_dir,
visualize_=visualize_,
to_file=to_file)
extracted_paths += cur_extracted_paths
self.paths += extracted_paths
return extracted_paths
def clear(self) -> None:
for component in self.components:
component.clear()
def filter(self):
for component in self.components:
component.filter()
def add_component(self, component: DataFilter, position: int) -> None:
self.components.insert(position, component)
def rm_component(self, position: int) -> None:
self.components.pop(position)
class StatsDataFilter(DataFilter):
_OPTIM_MEAN_THRESH = 107
_OPTIM_STD_THRESH = 51
def __init__(self, data_avg_mean: float = None, data_avg_std: float = None, console_output: bool = False):
super().__init__()
self.data_avg_mean = data_avg_mean
self.data_avg_std = data_avg_std
self.console_output = console_output
@visualize()
@save_to_file()
def extract(self, data_dir: str | Path, visualize_: bool, to_file: bool) -> list[str]:
if self.data_avg_mean is None or self.data_avg_std is None:
stats = self._compute_dataset_stats(data_dir)
self.data_avg_mean = stats['avg_mean']
self.data_avg_std = stats['avg_std']
extracted_paths = self._extract_outliers_by_stats(
data_dir,
self.data_avg_mean,
self.data_avg_std,
StatsDataFilter._OPTIM_MEAN_THRESH,
StatsDataFilter._OPTIM_STD_THRESH,
self.console_output)
self.paths += extracted_paths
return extracted_paths
def clear(self) -> None:
self.paths.clear()
if self.console_output:
print(f'[{self.__class__.__name__}]: Paths memory cleared.')
def filter(self) -> bool:
has_error = False
for path in self.paths:
if not Path(path).exists():
has_error = True
continue
os.remove(path)
if self.console_output:
print(f'[{self.__class__.__name__}]: Removed {path}')
return has_error
@classmethod
def _extract_outliers_by_stats(cls,
data_root: str | Path,
dataset_avg_mean: float,
dataset_avg_std: float,
mean_thresh: float,
std_thresh: float,
console_output: bool = False) -> list[str]:
outlier_paths = []
count = 0
_, _, paths = StatsDataFilter._load_data(data_root)
total_len = len(paths)
for path in iter(paths):
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
if abs(dataset_avg_mean - np.mean(img)) > mean_thresh or abs(
dataset_avg_std - np.std(img)) > std_thresh:
outlier_paths.append(path)
if console_output:
count += 1
print(f'[{cls.__name__}]: Computed {count}/{total_len} images ({count / total_len * 100:.2f}%)')
return outlier_paths
@staticmethod
def _compute_dataset_stats(data_dir: str) -> dict[str, float]:
img_paths = list(Path(data_dir).glob('**/*.jpg'))
num_images = len(img_paths)
mean_sum = 0
std_sum = 0
for img_path in img_paths:
img = cv2.imread(str(img_path), cv2.IMREAD_GRAYSCALE)
img_mean = np.mean(img)
img_std = np.std(img)
mean_sum += img_mean
std_sum += img_std
avg_mean = mean_sum / num_images
avg_std = std_sum / num_images
stats_dict = {
'avg_mean': avg_mean,
'avg_std': avg_std,
}
return stats_dict
class PcaDataFilter(DataFilter):
_OPTIM_NUM_COMPONENTS = 4
_OPTIM_ERROR_THRESH = 87
def __init__(self, console_output: bool = False):
super().__init__()
self.console_output = console_output
@visualize()
@save_to_file()
def extract(self, data_dir: str | Path, visualize_: bool, to_file: bool) -> list[str]:
extracted_paths = self._extract_outliers_with_pca(data_dir)
self.paths += extracted_paths
return extracted_paths
def clear(self) -> None:
self.paths.clear()
if self.console_output:
print(f'[{self.__class__.__name__}]: Paths memory cleared.')
def filter(self) -> bool:
has_error = False
for path in self.paths:
if not Path(path).exists():
has_error = True
continue
os.remove(path)
if self.console_output:
print(f'[{self.__class__.__name__}]: Removed {path}')
return has_error
@staticmethod
def _extract_outliers_with_pca(dir_: str | Path) -> list[str]:
x, _, img_paths = PcaDataFilter._load_data(dir_)
x = np.array(x)
num_samples, height, width = x.shape
X_flattened = x.reshape(num_samples, height * width)
outlier_indices = PcaDataFilter._detect_outliers_with_pca(X_flattened,
PcaDataFilter._OPTIM_NUM_COMPONENTS,
PcaDataFilter._OPTIM_ERROR_THRESH)
img_paths_to_remove = [img_paths[i] for i in outlier_indices.tolist()]
return img_paths_to_remove
@staticmethod
def _detect_outliers_with_pca(orig_data: np.ndarray,
num_components: int,
error_thresh: float) -> np.ndarray:
pca = PCA(n_components=num_components)
X_reduced = pca.fit_transform(orig_data)
X_reconstructed = pca.inverse_transform(X_reduced)
reconstruction_errors = np.sqrt(np.mean((orig_data - X_reconstructed) ** 2, axis=1))
outlier_indices = np.where(reconstruction_errors > error_thresh)[0]
return outlier_indices
class DHashDuplicateFilter(DataFilter):
def __init__(self, hash_size: int = 8, console_output: bool = False):
super().__init__()
self.hash_size = hash_size
self.console_output = console_output
@visualize(60)
@save_to_file()
def extract(self, data_dir: str | Path, visualize_: bool, to_file: bool) -> list[str]:
_, _, paths = self._load_data(data_dir)
hashes = set()
duplicates = []
for path in paths:
hash_ = imagehash.dhash(Image.open(path), self.hash_size)
if hash_ in hashes:
duplicates.append(path)
if self.console_output:
print(f'[{self.__class__.__name__}]: Duplicate found at {path}')
else:
hashes.add(hash_)
self.paths += duplicates
return duplicates
def clear(self) -> None:
self.paths.clear()
if self.console_output:
print(f'[{self.__class__.__name__}]: Paths memory cleared.')
def filter(self) -> bool:
has_error = False
for path in self.paths:
if not Path(path).exists():
has_error = True
continue
os.remove(path)
if self.console_output:
print(f'[{self.__class__.__name__}]: Removed {path}')
return has_error
if __name__ == '__main__':
dataset_dir = Path('./dataset')
stats_filter = StatsDataFilter(_DATASET_AVG_MEAN, _DATASET_AVG_STD, True)
pca_filter = PcaDataFilter(console_output=True)
duplicate_filter = DHashDuplicateFilter(console_output=True)
compose = DataFilterCompose.build([
stats_filter,
pca_filter,
duplicate_filter
])
# You may set the value of visualize_ or to_file parameters to True
# to plot extracted images or save paths to a file.
stats_filter.extract(dataset_dir, visualize_=False, to_file=False)
# WARNING: uncommenting the line below will irreversibly remove dataset files
# compose.filter()
|