Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ndarrays in process_inputs #105

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/test_torchview.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,33 @@ def test_isolated_tensor(verify_result: Callable[..., Any]) -> None:
)

verify_result([model_graph])


def test_process_input_ndarray():
from torchview.torchview import process_input
import numpy as np
input_data = [{'array': np.random.rand(2, 3, 5)}]
x, kwargs_recorder_tensor, input_data_node = process_input(
input_data=input_data,
input_size=None,
kwargs={},
device='cpu'
)
assert x is not input_data, 'should not have the same id'
assert x[0]['array'] is input_data[0]['array'], 'ndarrays should be unmodified'


def test_process_input_ndarray_and_tensor():
from torchview.torchview import process_input
import numpy as np

input_data = [{'array': np.random.rand(2, 3, 5), 'tensor': torch.rand(2, 3, 5)}]
x, kwargs_recorder_tensor, input_data_node = process_input(
input_data=input_data,
input_size=None,
kwargs={},
device='cpu'
)
assert x is not input_data, 'should not have the same id'
assert x[0]['array'] is input_data[0]['array'], 'ndarrays should be unmodified'
assert x[0]['tensor'] is not input_data[0]['tensor'], 'tensor should be modified'
4 changes: 4 additions & 0 deletions torchview/torchview.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import graphviz
import torch
import numpy as np
from torch import nn
from torch.jit import ScriptModule

Expand Down Expand Up @@ -351,6 +352,9 @@ def traverse_data(
if isinstance(data, torch.Tensor):
return action_fn(data)

if isinstance(data, np.ndarray):
return data

# Recursively apply to collection items
aggregate = aggregate_fn(data)
if isinstance(data, Mapping):
Expand Down