Initialize network weights. Parameters: net (network) -- network to be initialized init_type (str) -- the name of an initialization method: normal | xavier | kaiming | orthogonal init_gain (float) -- scaling factor for normal, xavier and orthogonal. We use 'nor
(net, init_type='normal', init_gain=0.02)
| 65 | |
| 66 | |
| 67 | def init_weights(net, init_type='normal', init_gain=0.02): |
| 68 | """Initialize network weights. |
| 69 | |
| 70 | Parameters: |
| 71 | net (network) -- network to be initialized |
| 72 | init_type (str) -- the name of an initialization method: normal | xavier | kaiming | orthogonal |
| 73 | init_gain (float) -- scaling factor for normal, xavier and orthogonal. |
| 74 | |
| 75 | We use 'normal' in the original pix2pix and CycleGAN paper. But xavier and kaiming might |
| 76 | work better for some applications. Feel free to try yourself. |
| 77 | """ |
| 78 | def init_func(m): # define the initialization function |
| 79 | classname = m.__class__.__name__ |
| 80 | if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1): |
| 81 | if init_type == 'normal': |
| 82 | init.normal_(m.weight.data, 0.0, init_gain) |
| 83 | elif init_type == 'xavier': |
| 84 | init.xavier_normal_(m.weight.data, gain=init_gain) |
| 85 | elif init_type == 'kaiming': |
| 86 | init.kaiming_normal_(m.weight.data, a=0, mode='fan_in') |
| 87 | elif init_type == 'orthogonal': |
| 88 | init.orthogonal_(m.weight.data, gain=init_gain) |
| 89 | else: |
| 90 | raise NotImplementedError('initialization method [%s] is not implemented' % init_type) |
| 91 | if hasattr(m, 'bias') and m.bias is not None: |
| 92 | init.constant_(m.bias.data, 0.0) |
| 93 | elif classname.find('BatchNorm2d') != -1: # BatchNorm Layer's weight is not a matrix; only normal distribution applies. |
| 94 | init.normal_(m.weight.data, 1.0, init_gain) |
| 95 | init.constant_(m.bias.data, 0.0) |
| 96 | |
| 97 | print('initialize network with %s' % init_type) |
| 98 | net.apply(init_func) # apply the initialization function <init_func> |
| 99 | |
| 100 | |
| 101 | def init_net(net, init_type='normal', init_gain=0.02, gpu_ids=[]): |