-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewritten Composite. Now should run more smoothly. also updated callb…
…acks and tested that example runs
- Loading branch information
Showing
5 changed files
with
358 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,49 @@ | ||
from dataclasses import dataclass | ||
from enum import auto, Enum | ||
from multiprocessing import Queue | ||
from typing import Any, Dict | ||
|
||
|
||
class TMCompositeCallback: | ||
class CallbackMethod(Enum): | ||
ON_TRAIN_COMPOSITE_BEGIN = auto() | ||
ON_TRAIN_COMPOSITE_END = auto() | ||
ON_EPOCH_COMPONENT_BEGIN = auto() | ||
ON_EPOCH_COMPONENT_END = auto() | ||
UPDATE_PROGRESS = auto() | ||
|
||
@dataclass | ||
class CallbackMessage: | ||
method: CallbackMethod | ||
kwargs: Dict[str, Any] | ||
|
||
class TMCompositeCallback: | ||
def __init__(self): | ||
pass | ||
|
||
def on_epoch_component_begin(self, component, epoch, logs=None): | ||
def on_epoch_component_begin(self, component, epoch, logs=None, **kwargs): | ||
pass | ||
|
||
def on_epoch_component_end(self, component, epoch, logs=None): | ||
def on_epoch_component_end(self, component, epoch, logs=None, **kwargs): | ||
pass | ||
|
||
def on_train_composite_end(self, composite, logs=None): | ||
def on_train_composite_end(self, composite, logs=None, **kwargs): | ||
pass | ||
|
||
def on_train_composite_begin(self, composite, logs=None): | ||
def on_train_composite_begin(self, composite, logs=None, **kwargs): | ||
pass | ||
|
||
|
||
class TMCompositeCallbackProxy: | ||
|
||
def __init__(self, queue: Queue): | ||
self.queue = queue | ||
|
||
def on_epoch_component_begin(self, component, epoch, logs=None): | ||
self.queue.put(('on_epoch_component_begin', component, epoch, logs)) | ||
self.queue.put(CallbackMessage(CallbackMethod.ON_EPOCH_COMPONENT_BEGIN, {'component': component, 'epoch': epoch, 'logs': logs})) | ||
|
||
def on_epoch_component_end(self, component, epoch, logs=None): | ||
self.queue.put(('on_epoch_component_end', component, epoch, logs)) | ||
self.queue.put(CallbackMessage(CallbackMethod.ON_EPOCH_COMPONENT_END, {'component': component, 'epoch': epoch, 'logs': logs})) | ||
|
||
def on_train_composite_end(self, composite, logs=None): | ||
self.queue.put(('on_train_composite_end', composite, logs)) | ||
self.queue.put(CallbackMessage(CallbackMethod.ON_TRAIN_COMPOSITE_END, {'composite': composite, 'logs': logs})) | ||
|
||
def on_train_composite_begin(self, composite, logs=None): | ||
self.queue.put(('on_train_composite_begin', composite, logs)) | ||
self.queue.put(CallbackMessage(CallbackMethod.ON_TRAIN_COMPOSITE_BEGIN, {'composite': composite, 'logs': logs})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,49 @@ | ||
import numpy as np | ||
from typing import Dict, Any | ||
from tmu.composite.components.base import TMComponent | ||
|
||
|
||
class ColorThermometerComponent(TMComponent): | ||
|
||
def __init__(self, model_cls, model_config, resolution=8, **kwargs) -> None: | ||
super().__init__(model_cls=model_cls, model_config=model_config, **kwargs) | ||
if resolution < 2 or resolution > 255: | ||
raise ValueError("Resolution must be between 2 and 255") | ||
self.resolution = resolution | ||
self._thresholds = None | ||
|
||
def _create_thresholds(self) -> None: | ||
self._thresholds = np.linspace(0, 255, self.resolution + 1)[1:-1] | ||
|
||
def preprocess(self, data: dict): | ||
def preprocess(self, data: dict) -> Dict[str, Any]: | ||
super().preprocess(data=data) | ||
X_org = data.get("X") | ||
Y = data.get("Y") | ||
|
||
if X_org is None: | ||
raise ValueError("Input data 'X' is missing") | ||
|
||
if X_org.ndim != 4: | ||
raise ValueError(f"Expected 4D input, got {X_org.ndim}D") | ||
|
||
if X_org.shape[-1] != 3: | ||
raise ValueError(f"Expected 3 color channels, got {X_org.shape[-1]}") | ||
|
||
if self._thresholds is None: | ||
self._create_thresholds() | ||
|
||
X_org = data["X"] | ||
Y = data["Y"] | ||
# Use broadcasting for efficient computation | ||
X = (X_org[:, :, :, :, np.newaxis] >= self._thresholds).astype(np.uint8) | ||
|
||
X = np.empty((X_org.shape[0], X_org.shape[1], X_org.shape[2], X_org.shape[3], self.resolution), dtype=np.uint8) | ||
for z in range(self.resolution): | ||
X[:, :, :, :, z] = X_org[:, :, :, :] >= (z + 1) * 255 / (self.resolution + 1) | ||
# Reshape correctly | ||
batch_size, height, width, channels, _ = X.shape | ||
X = X.transpose(0, 1, 2, 4, 3).reshape(batch_size, height, width, channels * (self.resolution - 1)) | ||
|
||
X = X.reshape((X_org.shape[0], X_org.shape[1], X_org.shape[2], 3 * self.resolution)) | ||
return { | ||
"X": X, | ||
"Y": Y | ||
} | ||
|
||
return dict( | ||
X=X, | ||
Y=Y, | ||
) | ||
def get_output_shape(self, input_shape: tuple) -> tuple: | ||
if len(input_shape) != 4: | ||
raise ValueError(f"Expected 4D input shape, got {len(input_shape)}D") | ||
return (*input_shape[:-1], input_shape[-1] * (self.resolution - 1)) |
Oops, something went wrong.