Pytorch Custom
The pytorch*custom module contains functions that process models created with the pytorch package.
Table of Contents:
Feature Processing (pytorchcustom.fts)
Contains functions to extract features from a pytorch neural network
- fns.pytorchcustom.fts.feature_extractor(model, lay, model_in, norm, device)
Function to extract the features from a given layer of a model
- Parameters
model (loaded pytorch model) –
lay (str) – name of a layer in model
model_in (varies) – correctly formatted model input
norm (str) – {‘ft01’, ‘all01’, ‘none’} a string to indicate which normalization methodology to use;
device (torch.device) – device index to select
- Returns
ft_mat (np.ndarray[(any, any, any), float]) – an array of all features extracted from a given layer; the first two dimensions are the size of the feature; the last dimension is the number of features in a layer
See also
fns.mat.normalize_mat()
for information about choices for norm
- fns.pytorchcustom.fts.parse_features(model, features)
Function to make a list of the features to plot
Prints error message and exits program for features = [‘Grid’]
- Parameters
model (loaded pytorch model) – features (list[str]): list of features to plot, starting at feature 1
features (list[str]) –
- Returns
n_features (int) – how may features to plot
features (list[int]) – list of features to plot, starting at feature 0
Model Prints (pytorchcustom.prints)
Contains functions to print out lists of options for model-related input arguments
- fns.pytorchcustom.prints.print_layers(model)
Function that prints a list of layer names in a model
- Parameters
model (loaded pytorch model) –
- Returns
No Return Objects
- fns.pytorchcustom.prints.print_features(model)
Function that prints how many features are extracted from a layer of a model
- Parameters
model (loaded pytorch model) –
- Returns
No Return Objects
Model Checks (pytorchcustom.checks)
Contains functions to check that the modle-related input arguments passed to are valid
- fns.pytorchcustom.checks.check_layer(model, lay)
Function that checks if a layer name is in the model
- Parameters
model (loaded pytorch model) –
lay (str) – name of layer to test
- Returns
No Return Objects
- fns.pytorchcustom.checks.check_features(model, features)
Function that checks if number of features requested are available from a layer
- Parameters
model (loaded pytorch model) –
features (list[str]) – should be [‘Grid’], [‘All’], or a list of integers; features the script plans on extracting
- Returns
No Return Objects
Calico Functions (pytorchcustom.calico)
Contains functions to create a calico network and do prints/checks on the calcio network inputs
- fns.pytorchcustom.calico.check_calico_layer(model, lay, branch='None', catlay='None')
Function that checks if the layer is a valid selection for the split layer
- Parameters
model (loaded pytorch model) –
lay (str) – name of layer to test
branch (str) – key used to identify which layers are on the secondary branch; use ‘None’ if the model only has one branch
catlay (str) – name of layer where the branches of the model are concatenated use ‘None’ if the model only has one branch
- Returns
No Return Objects
- fns.pytorchcustom.calico.check_calico_features(model, features)
Function that checks if number of features requested are available from a layer
- Parameters
model (loaded pytorch model) –
features (str) – an integer; features the calico model scales
- Returns
No Return Objects
- fns.pytorchcustom.calico.eval_layer(model, lay, x)
Function to evaluate a pytorch layer given it’s name
- Parameters
model (loaded pytorch model) –
lay (str) – name of layer to evaluate
x (Union[torch.FloatTensor, torch.cuda.FloatTensor]) – input to layer
- Returns
x (torch.Tensor) – output of layer
Nested Cylinder Model Definition (pytorchcustom.field2PTW_model_definition)
- fns.pytorchcustom.field2PTW_model_definition.conv2d_shape(w, h, k, s_w, s_h, p_w, p_h)
Function to calculate the new dimension of an image after a
nn.Conv2d
Assumes 2D input and dilation=1- Parameters
w (int) – starting width
h (int) – starting height
k (int) – kernel size
s_w (int) – stride size along the width
s_h (int) – stride size along the height
p_w (int) – padding size along the width
p_h (int) – padding size along the height
- Returns
new_w (int) – number of pixels along the width
new_h (int) – number of pixels along the height
total (int) – total number of pixels in new image
- class fns.pytorchcustom.field2PTW_model_definition.CNN_Interpretability_Module(img_size=(1, 1700, 500), kernel=5, features=12, depth=12, conv_onlyweights=True, batchnorm_onlybias=True, act_layer=<class 'torch.nn.modules.activation.GELU'>)
Convolutional Neural Network Module that creates the “interpretability layers”
Sequence of Conv2D, Batch Normalization, and Activation
- Parameters
img_size (tuple[int, int, int]) – size of input (channels, height, width)
kernel (int) – size of square convolutional kernel
features (int) – number of features in the convolutional layers
depth (int) – number of interpretability blocks
conv_onlyweights (bool) – determines if convolutional layers learn only weights or weights and bias
batchnorm_onlybias (bool) – determiens if the batch normalization layers learn only bias or weights and bias
act_layer (nn.modules.activation) – torch neural network layer class to use as activation
- forward(x)
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class fns.pytorchcustom.field2PTW_model_definition.CNN_Reduction_Module(img_size=(1, 1700, 500), size_threshold=(8, 8), kernel=5, stride=2, features=12, conv_onlyweights=True, batchnorm_onlybias=True, act_layer=<class 'torch.nn.modules.activation.GELU'>)
Convolutional Neural Network Module that creates the “reduction layers”
Sequence of Conv2D, Batch Normalization, and Activation
- Parameters
img_size (tuple[int, int, int]) – size of input (channels, height, width)
size_threshold (tuple[int, int]) – (approximate) size of final, reduced image (height, width)
kernel (int) – size of square convolutional kernel
stride (int) – size of base stride for convolutional kernel
features (int) – number of features in the convolutional layers
conv_onlyweights (bool) – determines if convolutional layers learn only weights or weights and bias
batchnorm_onlybias (bool) – determiens if the batch normalization layers learn only bias or weights and bias
act_layer (nn.modules.activation) – torch neural network layer class to use as activation
- forward(x)
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class fns.pytorchcustom.field2PTW_model_definition.field2PTW(img_size=(1, 1700, 500), size_threshold=(8, 8), kernel=5, features=12, interp_depth=12, conv_onlyweights=True, batchnorm_onlybias=True, act_layer=<class 'torch.nn.modules.activation.GELU'>, hidden_features=20)
Convolutional Neural Network Model that uses a single PVI field to predict one scalar value
- Parameters
img_size (tuple[int, int, int]) – size of input (channels, height, width)
size_threshold (tuple[int, int]) – (approximate) size of reduced image (height, width)
kernel (int) – size of square convolutional kernel
features (int) – number of features in the convolutional layers
interp_depth (int) – number of interpretability blocks
conv_onlyweights (bool) – determines if convolutional layers learn only weights or weights and bias
batchnorm_onlybias (bool) – determiens if the batch normalization layers learn only bias or weights and bias
act_layer (nn.modules.activation) – torch neural network layer class to use as activation
hidden_features (int) – number of hidden features in the fully connected dense layer
- forward(x)
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.