-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
79 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import unittest | ||
from hw_asr.logger.tensorboard import TensorboardWriter | ||
from hw_asr.logger.utils import plot_spectrogram_to_buf | ||
from hw_asr.logger.wandb import WanDBWriter | ||
from hw_asr.utils.parse_config import ConfigParser | ||
import shutil | ||
import torchaudio | ||
from torchvision.transforms import ToTensor | ||
from pathlib import Path | ||
import numpy as np | ||
import torch | ||
import PIL | ||
|
||
|
||
class TestVisualization(unittest.TestCase): | ||
def test_visualiaers(self): | ||
log_dir = str(Path(__file__).parent / "logs_dir") | ||
|
||
try: | ||
config = ConfigParser.get_default_configs() | ||
logger = config.get_logger("test") | ||
|
||
tensorboard = TensorboardWriter(log_dir, logger, True) | ||
wandb = WanDBWriter(config, logger) | ||
|
||
test_methods = [ | ||
"add_scalar", | ||
"add_scalars", | ||
"add_image", | ||
"add_audio", | ||
"add_text", | ||
"add_histogram" | ||
] | ||
|
||
audio_path = Path(__file__).parent.parent.parent / "test_data" / "audio" / "84-121550-0000.flac" | ||
audio, sr = torchaudio.load(audio_path) | ||
print(audio.shape) | ||
wave2spec = config.init_obj( | ||
config["preprocessing"]["spectrogram"], | ||
torchaudio.transforms, | ||
) | ||
|
||
wave = wave2spec(audio) | ||
image = ToTensor()(PIL.Image.open(plot_spectrogram_to_buf(wave.squeeze(0).log()))) | ||
print(image.shape) | ||
|
||
hist = torch.from_numpy(np.asarray([1, 2, 3, 4])) | ||
|
||
test_data = [ | ||
1, | ||
{"test1": 1, "test2": 2}, | ||
image, | ||
audio, | ||
"test", | ||
hist | ||
] | ||
|
||
for method, value in zip(test_methods, test_data): | ||
kwargs = {} | ||
if method == 'add_audio': | ||
kwargs = {'sample_rate': sr} | ||
elif method == 'add_histogram': | ||
kwargs = {'bins': 'auto'} | ||
|
||
logger.info(f"test {method}") | ||
getattr(tensorboard, method)(method, value, **kwargs) | ||
getattr(wandb, method)(method, value, **kwargs) | ||
|
||
finally: | ||
shutil.rmtree(log_dir) | ||
|
||
|
||
|