model¶
Submodules¶
model¶
-
class
segmentation_rt.dl.model.model.Model(expr_dir, structures, seed=None, batch_size=None, epoch_count=1, niter=150, niter_decay=50, beta1=0.5, lr=0.0002, ngf=64, n_blocks=9, input_nc=1, use_dropout=False, norm='batch', max_grad_norm=500.0, monitor_grad_norm=True, save_epoch_freq=2, print_freq=60, display_epoch_freq=1, testing=False, resume=False)¶ Bases:
objectInitialize a chosen model with parameters.
This class allows the training of the networks and afterward for testing.Only resnet available, more coming soon.
- Parameters
expr_dir (str) – output folder.
seed (int) – manual seed.
batch_size (int) – input batch size.
epoch_count (int) – the starting epoch count.
niter (int) – # of iter at starting learning rate.
niter_decay (int) – # of iter to linearly decay learning rate to zero.
beta1 (float) – momentum term of adam.
lr (float) – initial learning rate for adam.
ngf (int) – # of gen filters in first conv layer.
n_blocks (int) – # of residual blocks in the global generator network.
input_nc (int) – the number of channels in input images.
ngf – the number of filters in the last conv layer.
n_blocks – the number of ResNet blocks.
use_dropout (bool) – if use dropout layers.
norm (str) – normalization.
max_grad_norm (float) – max grad norm to which it will be clipped (if exceeded).
monitor_grad_norm (bool) – flag set to monitor grad norms.
save_epoch_freq (int) – frequency of saving checkpoints at the end of epochs.
print_freq (int) – frequency of showing training results on console.
testing (bool) – if test phase.
-
eval()¶ Sets the module in evaluation mode.
-
load(checkpoint_path, optimizer=False)¶ Loads an object saved with torch.save from a file.
- Parameters
checkpoint_path (str) – path to the checkpoint.
optimizer (bool) – with optimizer.
-
save(checkpoint_name)¶ Save the model and optimizer.
- Parameters
checkpoint_name (str) – name of the checkpoint.
-
save_options()¶ Save model options.
-
test(dataset, export_path=None, checkpoint=None, save=False)¶ Model prediction for a SingleDataset.
- Parameters
dataset (
DataLoader) – training dataset.export_path (str) – export path.
- Returns
MAE of the dataset.
- Return type
float
-
train(train_dataset, test_dataset=None)¶ Train the model with a dataset.
- Parameters
train_dataset (
DataLoader) – training dataset.test_dataset (
DataLoader) – test dataset. If given output statistic during training.
-
train_instance(ct, segmentation)¶ Training instance (batch).
- Parameters
ct (Tensor) – input tensor.
segmentation (Tensor) – ground truth tensor.
- Returns
losses and visualization data.
-
update_learning_rate()¶ Update learning rate.
-
visualize_training(visuals, affine, epoch, index)¶ Save training image for visualization.
- Parameters
affine –
visuals (dict) – images.
epoch (int) – epoch
index (float) – index
networks¶
-
class
segmentation_rt.dl.model.networks.ResnetBlock(dim, norm_layer, use_dropout, use_bias)¶ Bases:
torch.nn.modules.module.ModuleInitialize the Resnet block.
A resnet block is a conv block with skip connections We construct a conv block with build_conv_block function, and implement skip connections in <forward> function. Original Resnet paper: https://arxiv.org/pdf/1512.03385.pdf.
- param int dim
the number of channels in the conv layer.
- Parameters
norm_layer – normalization layer.
use_dropout (bool) – if use dropout layers.
use_bias (bool) – if use bias.
-
forward(x)¶ Standard forward.
-
training: bool¶
-
class
segmentation_rt.dl.model.networks.ResnetGenerator(input_nc, output_nc, ngf=64, n_blocks=9, norm_layer=functools.partial(<class 'torch.nn.modules.batchnorm.BatchNorm3d'>, affine=True), use_dropout=False)¶ Bases:
torch.nn.modules.module.ModuleResnet-based generator that consists of Resnet blocks between a few downscaling/up-sampling operations.
- Parameters
input_nc (int) – the number of channels in input images.
output_nc (int) – the number of channels in output images.
ngf (int) – the number of filters in the last conv layer.
n_blocks (int) – the number of ResNet blocks.
norm_layer – normalization layer.
use_dropout (bool) – if use dropout layers.
-
forward(x)¶ Standard forward.
-
training: bool¶
-
segmentation_rt.dl.model.networks.build_conv_block(dim, norm_layer, use_dropout, use_bias)¶ Construct a convolutional block.
- Parameters
dim (int) – the number of channels in the conv layer.
norm_layer (
nn.Module) – normalization layer.use_dropout (bool) – if use dropout layers.
use_bias (bool) – if the conv layer uses bias or not
- Returns
Returns a conv block (with a conv layer, a normalization layer, and a non-linearity layer (ReLU))
- Return type
nn.Sequential
-
segmentation_rt.dl.model.networks.define_generator(input_nc, output_nc, ngf, n_blocks, device, use_dropout=False)¶ Create a generator.
- Parameters
input_nc (int) – the number of channels in input images.
output_nc (i) – the number of channels in output images.
ngf (int) – the number of filters in the last conv layer.
n_blocks (int) – the number of ResNet blocks.
use_dropout (bool) – if use dropout layers.
device – device.
- Returns
a generator.
- Return type
torch.nn.Module
-
segmentation_rt.dl.model.networks.dice_loss(output, target)¶ Dice Loss between two tensors.
- Parameters
output (
torch.Tensor) – input tensor.target (
torch.Tensor) – ground truth tensor.
- Returns
dice loss.
-
segmentation_rt.dl.model.networks.dice_score(output, target)¶ Dice Score between two tensors.
- param output
input tensor.
- type output
torch.Tensor- param target
ground truth tensor.
- type target
torch.Tensor- return
dice score.
-
segmentation_rt.dl.model.networks.print_network(net, out_f=None)¶ Prints the number of learnable parameters.
- Parameters
net – the network.
out_f – file
- Returns
number of learnable parameters.
- Return type
int
-
segmentation_rt.dl.model.networks.weights_init(m)¶ Initialize network weights.
- Parameters
m (
torch.nn.Module) – Module