All Projects → kevinzakka → Pytorch Goodies

kevinzakka / Pytorch Goodies

PyTorch Boilerplate For Research

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Pytorch Goodies

Awesome Iccv
ICCV2019最新录用情况
Stars: ✭ 305 (-28.57%)
Mutual labels:  segmentation
Animal Matting
Github repository for the paper End-to-end Animal Image Matting
Stars: ✭ 363 (-14.99%)
Mutual labels:  segmentation
Cascadepsp
[CVPR2020] CascadePSP: Toward Class-Agnostic and Very High-Resolution Segmentation via Global and Local Refinement
Stars: ✭ 407 (-4.68%)
Mutual labels:  segmentation
Bcdu Net
BCDU-Net : Medical Image Segmentation
Stars: ✭ 314 (-26.46%)
Mutual labels:  segmentation
Inaspeechsegmenter
CNN-based audio segmentation toolkit. Allows to detect speech, music and speaker gender. Has been designed for large scale gender equality studies based on speech time per gender.
Stars: ✭ 352 (-17.56%)
Mutual labels:  segmentation
Semantic human matting
Semantic Human Matting
Stars: ✭ 388 (-9.13%)
Mutual labels:  segmentation
Erfnet pytorch
Pytorch code for semantic segmentation using ERFNet
Stars: ✭ 304 (-28.81%)
Mutual labels:  segmentation
Pose2seg
Code for the paper "Pose2Seg: Detection Free Human Instance Segmentation" @ CVPR2019.
Stars: ✭ 423 (-0.94%)
Mutual labels:  segmentation
Instaboost
Code for ICCV2019 paper "InstaBoost: Boosting Instance Segmentation Via Probability Map Guided Copy-Pasting"
Stars: ✭ 368 (-13.82%)
Mutual labels:  segmentation
Maskfusion
MaskFusion: Real-Time Recognition, Tracking and Reconstruction of Multiple Moving Objects
Stars: ✭ 404 (-5.39%)
Mutual labels:  segmentation
Deepcut
A Thai word tokenization library using Deep Neural Network
Stars: ✭ 330 (-22.72%)
Mutual labels:  segmentation
Rectlabel Support
RectLabel - An image annotation tool to label images for bounding box object detection and segmentation.
Stars: ✭ 338 (-20.84%)
Mutual labels:  segmentation
Pvt
Stars: ✭ 379 (-11.24%)
Mutual labels:  segmentation
Tianchi Medical Lungtumordetect
天池医疗AI大赛[第一季]:肺部结节智能诊断 UNet/VGG/Inception/ResNet/DenseNet
Stars: ✭ 314 (-26.46%)
Mutual labels:  segmentation
Dipy
DIPY is the paragon 3D/4D+ imaging library in Python. Contains generic methods for spatial normalization, signal processing, machine learning, statistical analysis and visualization of medical images. Additionally, it contains specialized methods for computational anatomy including diffusion, perfusion and structural imaging.
Stars: ✭ 417 (-2.34%)
Mutual labels:  segmentation
Pointnet
PointNet: Deep Learning on Point Sets for 3D Classification and Segmentation
Stars: ✭ 3,517 (+723.65%)
Mutual labels:  segmentation
Fbrs interactive segmentation
[CVPR2020] f-BRS: Rethinking Backpropagating Refinement for Interactive Segmentation https://arxiv.org/abs/2001.10331
Stars: ✭ 366 (-14.29%)
Mutual labels:  segmentation
Trackit
[ECCV'20] Ocean: Object-aware Anchor-Free Tracking
Stars: ✭ 424 (-0.7%)
Mutual labels:  segmentation
Fsgan
FSGAN - Official PyTorch Implementation
Stars: ✭ 420 (-1.64%)
Mutual labels:  segmentation
Co Fusion
Co-Fusion: Real-time Segmentation, Tracking and Fusion of Multiple Objects
Stars: ✭ 400 (-6.32%)
Mutual labels:  segmentation

Model Statistics

Number of Parameters

num_params = sum(p.numel() for p in model.parameters()) # Total parameters
num_trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)  # Trainable parameters

Number of FLOPS

[...]

Weight Initialization

PyTorch layers are initialized by default in their respective reset_parameters() method. For example:

  • nn.Linear
    • weight and bias: uniform distribution [-limit, +limit] where limit is 1. / sqrt(fan_in) and fan_in is the number of input units in the weight tensor.
  • nn.Conv2D
    • weight and bias: uniform distribution [-limit, +limit] where limit is 1. / sqrt(fan_in) and fan_in is the number of input units in the weight tensor.

With this implementation, the variance of the layer outputs is equal to Var(W) = 1 / 3 * sqrt(fan_in) which isn't the best initialization strategy out there.

Note that PyTorch provides convenience functions for some of the initializations. The input and output shapes are computed using the method _calculate_fan_in_and_fan_out() and a gain() method scales the standard deviation to suit a particular activation.

Xavier Initialization

This initialization is general-purpose and meant to "work" pretty well for any activation in practice.

# default xavier init
for m in model.modules():
    if isinstance(m, (nn.Conv2d, nn.Linear)):
        nn.init.xavier_uniform(m.weight)

You can tailor this initialization to your specific activation by using the nn.init.calculate_gain(act) argument.

# default xavier init
for m in model.modules():
    if isinstance(m, (nn.Conv2d, nn.Linear)):
        nn.init.xavier_uniform(m.weight, gain=nn.init.calculate_gain('relu'))

He et. al Initialization

This is a similarly derived initialization tailored specifically for ReLU activations since they do not exhibit zero mean.

# he initialization
for m in model.modules():
    if isinstance(m, (nn.Conv2d, nn.Linear)):
        nn.init.kaiming_normal(m.weight, mode='fan_in')

For mode=fan_in, the variance of the distribution is ensured in the forward pass, while for mode=fan_out, it is ensured in the backwards pass.

SELU Initialization

Again, this initialization is specifically derived for the SELU activation function. The authors use the fan_in strategy. They mention that there is no significant difference between sampling from a Gaussian, a truncated Gaussian or a Uniform distribution.

# selu init
for m in model.modules():
    if isinstance(m, nn.Conv2d):
        fan_in = m.kernel_size[0] * m.kernel_size[1] * m.in_channels
        nn.init.normal(m.weight, 0, sqrt(1. / fan_in))
    elif isinstance(m, nn.Linear):
        fan_in = m.in_features
        nn.init.normal(m.weight, 0, sqrt(1. / fan_in))

Orthogonal Initialization

Orthogonality is a desirable quality in NN weights in part because it is norm preserving, i.e. it rotates the input matrix, but cannot change its norm (scale/shear). This property is valuable in deep or recurrent networks, where repeated matrix multiplication can result in signals vanishing or exploding.

for m in model.modules():
    if isinstance(m, (nn.Conv2d, nn.Linear)):
        nn.init.orthogonal(m.weight)

Batch Norm Initialization

for m in model:
    if isinstance(m, nn.BatchNorm2d):
        nn.init.constant(m.weight, 1)
        nn.init.constant(m.bias, 0)

Weight Regularization

L2 Regularization

Heavily penalizes peaky weight vectors and encourages diffuse weight vectors. Has the appealing property of encouraging the network to use all of its inputs a little rather that some of its inputs a lot.

with torch.enable_grad():
    reg = 1e-6
    l2_loss = torch.zeros(1)
    for name, param in model.named_parameters():
        if 'bias' not in name:
            l2_loss = l2_loss + (0.5 * reg * torch.sum(torch.pow(W, 2)))

L1 Regularization

Encourages sparsity, meaning we encourage the network to select the most useful inputs/features rather than use all.

with torch.enable_grad():
    reg = 1e-6
    l1_loss = torch.zeros(1)
    for name, param in model.named_parameters():
        if 'bias' not in name:
            l1_loss = l1_loss + (reg * torch.sum(torch.abs(W)))

Orthogonal Regularization

Improves gradient flow by keeping the matrix norm close to unitary.

with torch.enable_grad():
    reg = 1e-6
    orth_loss = torch.zeros(1)
    for name, param in model.named_parameters():
        if 'bias' not in name:
            param_flat = param.view(param.shape[0], -1)
            sym = torch.mm(param_flat, torch.t(param_flat))
            sym -= torch.eye(param_flat.shape[0])
            orth_loss = orth_loss + (reg * sym.abs().sum())

Max Norm Constraint

If a hidden unit's weight vector's L2 norm L ever gets bigger than a certain max value c, multiply the weight vector by c/L. Enforce it immediately after each weight vector update or after every X gradient update.

This constraint is another form of regularization. While L2 penalizes high weights using the loss function, "max norm" acts directly on the weights. L2 exerts a constant pressure to move the weights near zero which could throw away useful information when the loss function doesn't provide incentive for the weights to remain far from zero. On the other hand, "max norm" never drives the weights to near zero. As long as the norm is less than the constraint value, the constraint has no effect.

def max_norm(model, max_val=3, eps=1e-8):
    for name, param in model.named_parameters():
        if 'bias' not in name:
            norm = param.norm(2, dim=0, keepdim=True)
            desired = torch.clamp(norm, 0, max_val)
            param = param * (desired / (eps + norm))

Batch Normalization

[...]

Dropout

[...]

Optimization Misc.

Correct Validation Strategies

[...]

References

  • Thanks to Zijun Deng for inspiring the code for the segmentation metrics.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].