Browse Source

added a new mixing to profile intermediate images

Dimitri Korsch 4 years ago
parent
commit
36c1cf2516
2 changed files with 31 additions and 0 deletions
  1. 4 0
      cvdatasets/dataset/__init__.py
  2. 27 0
      cvdatasets/dataset/mixins/image_profiler.py

+ 4 - 0
cvdatasets/dataset/__init__.py

@@ -4,6 +4,7 @@ from cvdatasets.dataset.mixins.bounding_box import BBoxMixin
 from cvdatasets.dataset.mixins.bounding_box import MultiBoxMixin
 from cvdatasets.dataset.mixins.chainer_mixins import IteratorMixin
 from cvdatasets.dataset.mixins.features import PreExtractedFeaturesMixin
+from cvdatasets.dataset.mixins.image_profiler import ImageProfilerMixin
 from cvdatasets.dataset.mixins.parts import BasePartMixin
 from cvdatasets.dataset.mixins.parts import CroppedPartMixin
 from cvdatasets.dataset.mixins.parts import PartCropMixin
@@ -40,6 +41,9 @@ __all__ = [
 	# features
 	"PreExtractedFeaturesMixin",
 
+	# image profiling
+	"ImageProfilerMixin",
+
 	# bounding boxes
 	"BBCropMixin",
 	"BBoxMixin",

+ 27 - 0
cvdatasets/dataset/mixins/image_profiler.py

@@ -0,0 +1,27 @@
+import numpy as np
+from contextlib import contextmanager
+
+from cvdatasets.dataset.mixins.base import BaseMixin
+
+
+class ImageProfilerMixin(BaseMixin):
+	def __init__(self, *args, **kwargs):
+		super(ImageProfilerMixin, self).__init__(*args, **kwargs)
+		self._profile_img_enabled = False
+
+	@contextmanager
+	def enable_img_profiler(self):
+		_dmp = self._profile_img_enabled
+		self._profile_img_enabled = True
+		yield
+		self._profile_img_enabled = _dmp
+
+	def _profile_img(self, img, tag):
+		if len(img) == 0: return
+		if self._profile_img_enabled:
+			print(f"[{tag:^30s}]",
+				" | ".join([
+					f"size: {str(img.shape):>20s}",
+					f"pixel values: ({img.min():+8.2f}, {img.max():+8.2f})"
+					])
+				)