Utilities¶
This section lists out all the utility functions present througout TensorNet
.
Common Utilities¶
-
tensornet.utils.
set_seed
(seed: int, cuda: bool)[source]¶ Set seed to make the results reproducible.
- Parameters
seed (int) – Random seed value.
cuda (bool) – Whether CUDA is available.
-
tensornet.utils.
initialize_cuda
(seed: int) → Tuple[bool, torch.device][source]¶ Check if GPU is availabe and set seed.
- Parameters
seed (int) – Random seed value.
- Returns
2-element tuple containing
(bool): if cuda is available
(torch.device): device name
-
tensornet.utils.
get_predictions
(model: torch.nn.modules.module.Module, loader: torch.utils.data.dataloader.DataLoader, device: Union[str, torch.device], sample_count: int = 25)[source]¶ Get correct and incorrect model predictions.
- Parameters
model (torch.nn.Module) – Model Instance.
loader (torch.utils.data.DataLoader) – Data Loader.
device (
str
ortorch.device
) – Device where data will be loaded.(obj (sample_count) – int, optional): Total number of predictions to store from each correct and incorrect samples. (default: 25)
-
tensornet.utils.
class_level_accuracy
(model: torch.nn.modules.module.Module, loader: torch.utils.data.dataloader.DataLoader, device: Union[str, torch.device], classes: Union[List[str], Tuple[str]])[source]¶ Print test accuracy for each class in dataset.
- Parameters
model (torch.nn.Module) – Model Instance.
loader (torch.utils.data.DataLoader) – Data Loader.
device (
str
ortorch.device
) – Device where data will be loaded.classes (
list
ortuple
) – List of classes in the dataset.
-
tensornet.utils.
plot_metric
(data: Union[List[float], Dict[str, List[float]]], metric: str, title: str = None, size: Tuple[int] = 7, 5, legend_font: int = 15, legend_loc: str = 'lower right')[source]¶ Plot accuracy graph or loss graph.
- Parameters
data (
list
ordict
) – If only single plot then this is a list, else for multiple plots this is a dict with keys containing the plot name and values being a list of points to plot.metric (str) – Metric name which is to be plotted. Can be either loss or accuracy.
title (
str
, optional) – Title of the plot, if no title given then it is determined from the x and y label.size (
tuple
, optional) – Size of the plot. (default: ‘(7, 5)’)legend_loc (
str
, optional) – Location of the legend box in the plot. No legend will be plotted if there is only a single plot. (default: ‘lower right’)legend_font (
int
, optional) – Font size of the legend (default: ‘15’)
-
tensornet.utils.
plot_predictions
(data: List[dict], classes: Union[List[str], Tuple[str]], plot_title: str, plot_path: str)[source]¶ Display data.
- Parameters
data (list) – List of images, model predictions and ground truths. Images should be numpy arrays.
classes (
list
ortuple
) – List of classes in the dataset.plot_title (str) – Title for the plot.
plot_path (str) – Complete path for saving the plot.
-
tensornet.utils.
save_and_show_result
(classes: Union[List[str], Tuple[str]], correct_pred: Optional[List[dict]] = None, incorrect_pred: Optional[List[dict]] = None, path: Optional[str] = None)[source]¶ Display network predictions.
- Parameters
classes (
list
ortuple
) – List of classes in the dataset.correct_pred (
list
, optional) – Contains correct model predictions and labels.incorrect_pred (
list
, optional) – Contains incorrect model predictions and labels.path (
str
, optional) – Path where the results will be saved.
Model Utilities¶
Utility methods used by classes and methods present in the Models
section.
-
tensornet.models.utils.
summary
(model: torch.nn.modules.module.Module, input_size: Union[Tuple[int], List[int], Dict[str, Union[tuple, list]]], batch_size: int = - 1, dtypes: Optional = None)[source]¶ Display model summary.
- Parameters
model (torch.nn.Module) – Model instance.
input_size (
tuple
,list
ordict
) – Input size for the model.batch_size (
int
, optional) – Batch size. (default: -1)dtypes (optional) – Model input data types.
Data Utilities¶
Utility methods used by classes and methods present in the Data
section.
-
tensornet.data.utils.
unnormalize
(image, mean, std, transpose=False)[source]¶ Un-normalize a given image.
- Parameters
image (
numpy.ndarray
ortorch.Tensor
) – A ndarray or tensor. If tensor, it should be in CPU.mean (
float
ortuple
) – Mean. It can be a single value or a tuple with 3 values (one for each channel).std (
float
ortuple
) – Standard deviation. It can be a single value or a tuple with 3 values (one for each channel).transpose (
bool
, optional) – If True, transposed output will be returned. This param is effective only when image is a tensor. If tensor, the output will have channel number as the last dim. (default: False)
- Returns
Unnormalized image
- Return type
(numpy.ndarray or torch.Tensor)
-
tensornet.data.utils.
normalize
(image, mean, std, transpose=False)[source]¶ Normalize a given image.
- Parameters
image (
numpy.ndarray
ortorch.Tensor
) – A ndarray or tensor. If tensor, it should be in CPU.mean (
float
ortuple
) – Mean. It can be a single value or a tuple with 3 values (one for each channel).std (
float
ortuple
) – Standard deviation. It can be a single value or a tuple with 3 values (one for each channel).transpose (
bool
, optional) – If True, transposed output will be returned. This param is effective only when image is a tensor. If tensor, the output will have channel number as the last dim. (default: False)
- Returns
Normalized image
- Return type
(numpy.ndarray or torch.Tensor)