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

🐞 Add device flag #601

Merged
merged 9 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 22 additions & 2 deletions anomalib/deploy/inferencers/torch_inferencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ class TorchInferencer(Inferencer):
model_source (Union[str, Path, AnomalyModule]): Path to the model ckpt file or the Anomaly model.
meta_data_path (Union[str, Path], optional): Path to metadata file. If none, it tries to load the params
from the model state_dict. Defaults to None.
device (Optional[str], optional): Device to use for inference. Options are auto, cpu, cuda. Defaults to "auto".
"""

def __init__(
self,
config: Union[str, Path, DictConfig, ListConfig],
model_source: Union[str, Path, AnomalyModule],
meta_data_path: Union[str, Path] = None,
device: str = "auto",
):

self.device = self._get_device(device)

# Check and load the configuration
if isinstance(config, (str, Path)):
self.config = get_configurable_parameters(config_path=config)
Expand All @@ -55,6 +59,22 @@ def __init__(

self.meta_data = self._load_meta_data(meta_data_path)

def _get_device(self, device: str) -> torch.device:
"""Get the device to use for inference.

Args:
device (str): Device to use for inference. Options are auto, cpu, cuda.

Returns:
torch.device: Device to use for inference.
"""
if device not in ("auto", "cpu", "cuda"):
raise ValueError(f"Unknown device {device}")

if device == "auto":
device = "cuda" if torch.cuda.is_available() else "cpu"
return torch.device(device)

def _load_meta_data(self, path: Optional[Union[str, Path]] = None) -> Union[Dict, DictConfig]:
"""Load metadata from file or from model state dict.

Expand Down Expand Up @@ -84,7 +104,7 @@ def load_model(self, path: Union[str, Path]) -> AnomalyModule:
model = get_model(self.config)
model.load_state_dict(torch.load(path)["state_dict"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to pass the map_location here? It might fail otherwise if we load a gpu model on cpu.
https://pytorch.org/tutorials/recipes/recipes/save_load_across_devices.html

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point

model.eval()
return model
return model.to(self.device)

def pre_process(self, image: np.ndarray) -> Tensor:
"""Pre process the input image by applying transformations.
Expand All @@ -105,7 +125,7 @@ def pre_process(self, image: np.ndarray) -> Tensor:
if len(processed_image) == 3:
processed_image = processed_image.unsqueeze(0)

return processed_image
return processed_image.to(self.device)

def forward(self, image: Tensor) -> Tensor:
"""Forward-Pass input tensor to the model.
Expand Down
9 changes: 8 additions & 1 deletion tools/inference/torch_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ def get_args() -> Namespace:
parser.add_argument("--weights", type=Path, required=True, help="Path to model weights")
parser.add_argument("--input", type=Path, required=True, help="Path to an image to infer.")
parser.add_argument("--output", type=Path, required=False, help="Path to save the output image.")
parser.add_argument(
"--device",
type=str,
required=False,
default="auto",
help="Device to use for inference. Defaults to auto. Options: auto, cpu, cuda",
samet-akcay marked this conversation as resolved.
Show resolved Hide resolved
)
parser.add_argument(
"--task",
type=str,
Expand Down Expand Up @@ -70,7 +77,7 @@ def infer() -> None:
args = get_args()

# Create the inferencer and visualizer.
inferencer = TorchInferencer(config=args.config, model_source=args.weights)
inferencer = TorchInferencer(config=args.config, model_source=args.weights, device=args.device)
visualizer = Visualizer(mode=args.visualization_mode, task=args.task)

filenames = get_image_filenames(path=args.input)
Expand Down