Skip to content

Commit

Permalink
and support for therml norm diff
Browse files Browse the repository at this point in the history
  • Loading branch information
gferraro committed Oct 1, 2024
1 parent ae4a81c commit 23eed72
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 30 deletions.
6 changes: 6 additions & 0 deletions src/ml_tools/hyperparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def insert_defaults(self):
self["segment_type"] = self.segment_type
self["multi_label"] = True
self["diff_norm"] = self.diff_norm
self["thermal_diff_norm"] = self.thermal_diff_norm

self["smooth_predictions"] = self.smooth_predictions
self["channels"] = self.channels

Expand Down Expand Up @@ -58,6 +60,10 @@ def excluded_labels(self):
def remapped_labels(self):
return self.get("remapped_labels", None)

@property
def thermal_diff_norm(self):
return self.get("thermal_diff_norm", False)

@property
def diff_norm(self):
return self.get("diff_norm", True)
Expand Down
96 changes: 69 additions & 27 deletions src/ml_tools/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,12 @@ def preprocess_frames(
data = []
frames_used = []
filtered_norm_limits = None
if self.params.diff_norm:
thermal_norm_limits = None
if self.params.diff_norm or self.params.thermal_diff_norm:
min_diff = None
max_diff = 0
thermal_max_diff = None
thermal_min_diff = None
for i, region in enumerate(reversed(track.bounds_history)):
if region.blank:
continue
Expand All @@ -201,16 +204,30 @@ def preprocess_frames(
continue

f.float_arrays()
diff_frame = region.subimage(f.thermal) - region.subimage(
clip.background
)
new_max = np.amax(diff_frame)
new_min = np.amin(diff_frame)
if min_diff is None or new_min < min_diff:
min_diff = new_min
if new_max > max_diff:
max_diff = new_max
filtered_norm_limits = (min_diff, max_diff)

if self.params.thermal_diff_norm:
diff_frame = f.thermal - np.median(f.thermal)
new_max = np.amax(diff_frame)
new_min = np.amin(diff_frame)
if thermal_min_diff is None or new_min < thermal_min_diff:
thermal_min_diff = new_min
if thermal_max_diff is None or new_max > thermal_max_diff:
thermal_max_diff = new_max
if self.params.diff_norm:
diff_frame = region.subimage(f.thermal) - region.subimage(
clip.background
)
new_max = np.amax(diff_frame)
new_min = np.amin(diff_frame)
if min_diff is None or new_min < min_diff:
min_diff = new_min
if new_max > max_diff:
max_diff = new_max
if self.params.thermal_diff_norm:
thermal_norm_limits = (thermal_min_diff, thermal_max_diff)

if self.params.diff_norm:
filtered_norm_limits = (min_diff, max_diff)
for i, region in enumerate(reversed(track.bounds_history)):
if region.blank:
continue
Expand Down Expand Up @@ -249,6 +266,7 @@ def preprocess_frames(
clip.background,
clip.crop_rectangle,
filtered_norm_limits=filtered_norm_limits,
thermal_norm_limits=thermal_norm_limits,
)
preprocessed = preprocess_single_frame(
cropped_frame,
Expand Down Expand Up @@ -293,30 +311,52 @@ def preprocess_segments(

# should really be over whole track buts let just do the indices we predict of
# seems to make little different to just doing a min max normalization
thermal_norm_limits = None
filtered_norm_limits = None
if self.params.diff_norm:
if self.params.diff_norm or self.params.thermal_diff_norm:
min_diff = None
max_diff = 0
for frame_index in frame_indices:
region = track.bounds_history[frame_index - track.start_frame]
f = clip.get_frame(region.frame_number)
if f is None:
logging.warn("Could not get frame {}", region.frame_number)
thermal_max_diff = None
thermal_min_diff = None
for i, region in enumerate(reversed(track.bounds_history)):
if region.blank:
continue
if region.width == 0 or region.height == 0:
logging.warn(
"No width or height for frame %s regoin %s",
region.frame_number,
region,
)
continue
f = clip.get_frame(region.frame_number)
if region.blank or region.width <= 0 or region.height <= 0:
continue

f.float_arrays()
diff_frame = region.subimage(f.thermal) - region.subimage(
clip.background
)
new_max = np.amax(diff_frame)
new_min = np.amin(diff_frame)
if min_diff is None or new_min < min_diff:
min_diff = new_min
if new_max > max_diff:
max_diff = new_max
filtered_norm_limits = (min_diff, max_diff)

if self.params.thermal_diff_norm:
diff_frame = f.thermal - np.median(f.thermal)
new_max = np.amax(diff_frame)
new_min = np.amin(diff_frame)
if thermal_min_diff is None or new_min < thermal_min_diff:
thermal_min_diff = new_min
if thermal_max_diff is None or new_max > thermal_max_diff:
thermal_max_diff = new_max
if self.params.diff_norm:
diff_frame = region.subimage(f.thermal) - region.subimage(
clip.background
)
new_max = np.amax(diff_frame)
new_min = np.amin(diff_frame)
if min_diff is None or new_min < min_diff:
min_diff = new_min
if new_max > max_diff:
max_diff = new_max
if self.params.thermal_diff_norm:
thermal_norm_limits = (thermal_min_diff, thermal_max_diff)

if self.params.diff_norm:
filtered_norm_limits = (min_diff, max_diff)
for frame_index in frame_indices:
region = track.bounds_history[frame_index - track.start_frame]

Expand All @@ -341,6 +381,7 @@ def preprocess_segments(
clip.background,
clip.crop_rectangle,
filtered_norm_limits=filtered_norm_limits,
thermal_norm_limits=thermal_norm_limits,
)
track_data[frame.frame_number] = cropped_frame
features = None
Expand All @@ -365,6 +406,7 @@ def preprocess_segments(
self.params.frame_size,
self.params.channels,
self.preprocess_fn,
sample=f"{clip.get_id()}-{track.get_id()}",
)
if frames is None:
logging.warn("No frames to predict on")
Expand Down
13 changes: 10 additions & 3 deletions src/ml_tools/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def preprocess_frame(
crop_rectangle=None,
calculate_filtered=True,
filtered_norm_limits=None,
thermal_norm_limits=None,
):
median = np.median(frame.thermal)
cropped_frame = frame.crop_by_region(region, only_thermal=True)
Expand All @@ -79,7 +80,8 @@ def preprocess_frame(
True,
)
cropped_frame.thermal -= median
np.clip(cropped_frame.thermal, 0, None, out=cropped_frame.thermal)
if thermal_norm_limits is None:
np.clip(cropped_frame.thermal, 0, None, out=cropped_frame.thermal)
if calculate_filtered and filtered_norm_limits is not None:
cropped_frame.filtered, stats = imageprocessing.normalize(
cropped_frame.filtered,
Expand All @@ -88,8 +90,13 @@ def preprocess_frame(
new_max=255,
)
if frame.thermal is not None:
thermal_min = None
thermal_max = None
if thermal_norm_limits is not None:
thermal_min, thermal_max = thermal_norm_limits
logging.info("Using therml min max %s, %s", thermal_min, thermal_max)
cropped_frame.thermal, _ = imageprocessing.normalize(
cropped_frame.thermal, new_max=255
cropped_frame.thermal, min=thermal_min, max=thermal_max, new_max=255
)
else:
cropped_frame.normalize()
Expand Down Expand Up @@ -161,7 +168,7 @@ def preprocess_movement(
# index += 1
# tools.saveclassify_image(
# data,
# f"samples/{index}",
# f"samples/{sample}-{index}",
# )

if preprocess_fn:
Expand Down

0 comments on commit 23eed72

Please sign in to comment.