Welcome to ADV-TRAIN’s documentation!¶
This is a framework built on top of pytorch to make machine learning training and inference tasks easier. Along with that it also enables easy dataset and network instantiations, visualize boundaries and more. This was created at the Nanoelectronics Research Laboratory at Purdue.
Modules¶
-
class
advtrain.utils.normalize.
denormalize
(mean, std, img_dimensions, device='cpu')¶ De-normalizes the input and provides a backpropable de-normalization function
- Parameters
mean (list) – list of the mean for each channel
std (list) – list of the std for each channel
img_dimensions (list) – list [channels,h,w]
device (str) – cpu/cuda
- Returns
Returns
an object of normalize
-
class
advtrain.utils.normalize.
normalize
(mean, std, img_dimensions, device='cpu')¶ Normalizes the input and provides a backpropable normalization function. It perfroms the channel wise normalization using mean and standard deviation
- Parameters
mean (list) – list of the mean for each channel
std (list) – list of the std for each channel
img_dimensions (list) – list [channels,h,w]
device (str) – cpu/cuda
- Returns
Returns
an object of denormalize
-
advtrain.instantiate_model.
instantiate_model
(dataset='cifar10', num_classes=10, input_quant='FP', arch='resnet', dorefa=False, abit=32, wbit=32, qin=False, qout=False, suffix='', load=False, torch_weights=False, device='cpu')¶ Initializes/load network with random weight/saved and return auto generated model name ‘dataset_arch_suffix.ckpt’
- Parameters
dataset – mnists/cifar10/cifar100/imagenet/tinyimagenet/simple dataset the netwoek is trained on. Used in model name
num_classes – number of classes in dataset.
arch – resnet/vgg/lenet5/basicnet/slpconv model architecture the network to be instantiated with
suffix – str appended to the model name
load – boolean variable to indicate load pretrained model from ./pretrained/dataset/
torch_weights – boolean variable to indicate load weight from torchvision for imagenet dataset
- Returns
models with desired weight (pretrained / random ) model_name : str ‘dataset_arch_suffix.ckpt’ used to save/load model in ./pretrained/dataset
- Return type
model
-
class
advtrain.boundary_visualization.
VisualizeBoundaries
(framework=None, net=None, num_classes=10, num_channels=1, img_dim=32, device='cpu')¶ -
__init__
(framework=None, net=None, num_classes=10, num_channels=1, img_dim=32, device='cpu')¶ This is a class to visualize the boundaries of a neural net
- Parameters
framework (utils.Framework) – Object of Framework, If this is passed none of the other parameters are needed
net (nn.Module) – network whose decision boundaries are desired
num_classes (int) – Number of classes in the dataset
num_channels (int) – Number of channels in each image of the dataset
img_dim (int) – Assumes a square image, the height of the image in the dataset
device (str) – cpu/cuda device to perform the evaluation on
- Returns
Returns an object of the VisualizeBoundaries
-
generate_decision_boundaries
(images, labels, explore_range=40, use_random_dir=False)¶ Uses FGSM adversarial/random direction to obtain the boundaries of the nerual net around the specified images
- Parameters
images (torch.Tensor) – Tensor of images, expected shape [batch_size, channels, image_dim, image_dim]
labels (torch.Tensor) – Ground truth/correct labels for the images, shape [batch_size,]
explore_range (int) – Specifes the range around the image explored by the code, and empirical value, also determines the size of the output images
values are, 2, 40, 80, 100. Larger the number more the computations hence it takes longer (recommended) –
use_random_dir (bool) – Use random direction rather than adversarial direction as the basis vectors for the image
- Returns
Returns a Tensor of shape [batch_size, 3, explore_range, explore_range] which contains an RGB image of the boundaries around the input images.
-
show
(image, decision_boundary, index)¶ Visualizes the image and the decision boundary around it
- Parameters
image (torch.Tensor) – Tensor of images, expected shape [some non zero size, channels, image_dim, image_dim]
decision_boundary (torch.Tensor) – decision boundary a tensor of shape [some non zero size, 3, explore_range, explore_range] which contains an RGB image of the boundaries
index (int) – A value less than ‘some non zero size’ and proves an index of the image and decision bounday to display
- Returns
Returns a Tensor of shape [batch_size, 3, explore_range, explore_range] which contains an RGB image of the boundaries around the input images.
-
-
class
advtrain.utils.preprocess.
preprocess
¶ This class consists of a forward pass and backward pass function for preprocessing layer.
-
back_approx
(x)¶ This function forward propogates through the preprocessing layer :param x: Input to be back propagated
- Returns
The backward propagated input
-
forward
(x)¶ This function forward propogates through the preprocessing layer Args:
Returns:
-
-
class
advtrain.attack_framework.multi_lib_attacks.
attack_wrapper
(model, device, **params)¶ This is a LAL (Library Abstraction Layer) Wrapper around foolbox, advertorch and custom attack implementation
Example of attack_params {
‘lib’: ‘foolbox’, ‘attack’:’pgd’, ‘iterations’: 40, ‘epsilon’: 0.01, ‘stepsize’: 0.01, ‘bpda’: True, ‘random’: True ‘preprocess’: <pointer to quantize or hafltone or FP>, ‘custom_norm_func’: <pointer to normalize>, ‘targeted’: True,
}
Example of dataset_params {
‘mean’: [0.5, 0.5, 0.5], ‘std’: [0.5, 0.5, 0.5], ‘num_classes’: 10
}