Main Classes¶
AstroWrapper¶
‘AstroWrapper’ handles things like training, testing, saving/loading of the weights after supplying it with a network structure and some parameters.
-
class
astronet.wrappers.AstroWrapper(net, regression=False, batch_size=128, balanced=False, augments=[], transforms=[], seed=0, verbose=1)¶ Wrapper that extends the functionality of the ‘nolearn.lasagne.NeuralNet’ class. Added functionality includes on-the-fly data augmentation, loading data in batches and balancing classes.
‘astronet’ is designed for use with astrophysical data. It can be used on before-after-diff images for transient detection, timeseries such as lightcurve data or any other type of data in image or timeseries form. The augmentations implemented in ‘astronet’ include both standard augmentations, like normalization, flipping and rotation, and augmentations designed for astrophysical data, like adding foreground stars, or modelling CCD errors.
Parameters: net : nolearn.lasagne.NeuralNet
The network that will be used in training and testing.
regression : boolean, default False
Specifies if training labels are classes or continuous.
batch_size : int, default 128
Number of input samples per batch.
balanced : boolean, default False
If true, ensures that each batch contains an equal amount of samples from all classes. Only implemented for binary classification.
augments : list, default []
Augmentation steps that shall be applied to the data before training the model. The ordering does matter. Each element is a tuple with two elements, where the first element is a string specifying the method and the second is a dictionary with the parameters for the augmentation method.
transforms : list, default []
Transformations that will be applied to the images/patterns before training or applying the model. This list has the same form as the list with augmentations.
seed : int, default 0
The seed to be used
verbose : int, default 1
Verbosity level
Examples
With the following list of augments, all training data will be flipped with the default probability (0.5), and then a random value between -10 and 10 is added to each input.
>>> from astronet import AstroNet >>> >>> augments = [ >>> ('flipud', {}), >>> ('add_const', {'range':[-10,10]}) >>> ] >>> >>> net1 = AstroNet('ShallowNet') >>> model1 = AstroWrapper(net, augments=augments)
After the AstroWrapper has been created, one can still add augmentations using the ‘add_augment’ method. The following results in an equivalent model.
>>> net2 = AstroNet('ShallowNet') >>> model2 = AstroWrapper(net) >>> model2.add_method('flipud', {}) >>> model2.add_method('add_const', {'range':[-10,10]})
Methods
-
add_augment(method, args)¶ Method to add augmentation steps after initializationof the AugmentationBatchIterator. Calls the method in the AugmentationBatchIterator.
Parameters: method : string
Name of the augmentation that should be added. Should be key in self._methods
augments : dictionary, default {}
Parameters for augmentation that is to be added. Should be given in the form {‘k1’:v1, ‘k2’:v2}.
-
fit(XI_train, y_train, XF_train=None)¶ Fits the deep network.
Parameters: XI_train : array-like
Array of training images
y_train : array-like
Training labels
XF_train : array-like or None, default None
Optional: Array of training patterns (features)
-
get_params()¶ Returns the current model parameters.
Returns: dictionary
Parameters of the model.
-
load_weights(fname)¶ Loads previously saved weights. Weights should be loaded from a network with the same architecture as the current network only.
Parameters: fname : string
Relative path to the pickle file containing the weights.
-
predict(X, XF=None)¶ Computes predictions for new instances
Parameters: X : array-like
Array of input images
XF : array-like or None
Optional: Array of patterns (features)
Returns: preds : array-like
The predictions computed by the model
-
predict_proba(X, XF=None)¶ Computes predictions (probs) for new instances
Parameters: X : array-like
Array of input images
XF : array-like or None
Optional: Array of patterns (features)
Returns: preds : array-like
The predictions computed by the model
-
save_weights(odir, ofname)¶ Saves the weights of the current model to a pickle file, to be loaded at a later stage.
Parameters: odir : string
Relative path to the output directory.
ofname : string
Filename of the output pickle file.
-
set_params(**parameters)¶ Sets model parameters and returns self. Also recreates AugmentationBatchIterators with new model parameters.
- **parameters : dictionary
- Parameters of the model.
Returns: self : AstroWrapper
AstroWrapper with the changed values and new AugmentationBatchIterators for these values.
-
AstroNet¶
‘AstroNet’ is a simple extension of the ‘nolearn.lasagne.NeuralNetwork’ class, with some built-in networks available. One of the pre-made network is returned by calling AstroNet with a network name, input size and number of outputs.
-
class
astronet.models.AstroNet(net_type, input_shape, output_size, regression=False, epochs=100, learning_rate=0.0002, verbose=1)¶ Class that extends the nolearn NeuralNet class. Provides a simple way of loading a pre-defined network architecture for use with AstroWrapper.
Parameters: net_type : string
Name of the pre-defined network to be created.
input_shape : tuple or list
Shape of the input of the form (depth, width, height).
output_size : integer
Number of output nodes. Should be equal to the number of classes to predict.
regression : boolean, default False
Should be set to True when the labels are real values (regression) instead of distinct classes (classification).
epochs : integer, default 100
Number of times all input patterns are fed to the network during training.
learning_rate : float, default 0.0002
Parameter governing the speed of learning of the network. Careful tuning is often necessary to reach a good performance. Value >0.
verbose : integer, default 0
Verbosity level.
Methods
-
get_layers(net_type, shape, output_size)¶ Method to select a pre-defined architecture.
Parameters: net_type : string
Name of the pre-defined network to be created.
shape : tuple or list
Shape of the input of the form (depth, width, height).
output_size : integer
Number of output nodes. Should be equal to the number of classes to predict.
Returns: layers : list
The architecture of the selected pre-defined network.
-
AugmentationBatchIterator¶
The ‘AugmentationBatchIterator’ (ABI) splits the input up in batches of a predefined size. Every time a batch is used during training, it will first be passed through the ‘transform’ method in the ABI, which applies the chosen augmentations to the batch, returning the transformed input.
-
class
astronet.augmentations.AugmentationBatchIterator(batch_size, augments=[], balanced=False, regression=False, seed=0, verbose=1)¶ Class that handles data augmentation and batch processing.
Parameters: batch_size : integer
Number of input patterns per batch.
augments : list, default []
Augmentations that will be applied to the data in each batch. Each item in this list should be a class that extends the ‘Identity’ augmentation. The ‘apply’ method is called on each of the augmentations in this list in order.
balanced : boolean, default False
If set to True, will force each batch to contain an equal amount of samples from each class. Only works for binary classification problems.
regression : boolean, default False
Should be set to True when the labels are real values (regression) instead of distinct classes (classification).
seed : integer, default 0
Integer to seed the random number generator.
verbose : integer, default 0
Specifies verbosity of the model.
Attributes
Methods
-
add_augment(method)¶ Method to add augmentation steps after initialization of the AugmentationBatchIterator.
Parameters: method : object extending ‘Identity’, or list of those.
Augmentation(s) of the same form as those in the augments list during initiation.
-
transform(Xb, yb)¶ Load the data if input of filenames, then applies balancing and augmentations to the data if applicable.
Parameters: Xb : array-like
Can be either an array of of input patterns, or a list of filenames for the input patterns. In case this is a list of filenames, files will be opened before applying augmentation steps.
yb : array-like
Labels corresponding to input patterns. Should be set to None when input patterns are unknown.
Returns: X : array-like, same as Xb or numpy.ndarray
Input patterns after all augmentations are applied.
Y : array-like, same as yb
Labels after all augmentations are applied.
-