def __init__(self, num_blocks=4, nc32=8, nc16=14, nc8=28, k=1, drop_ratio=0.0): """ :param num_blocks: the number of resnet blocks per stage. There are 3 stages, for feature map width 32, 16, 8. Total number of layers is 6 * num_blocks + 2 :param nc32: the number of feature maps in the first stage (where feature maps are 32x32) :param nc16: the number of feature maps in the second stage (where feature maps are 16x16) :param nc8: the number of feature maps in the third stage (where feature maps are 8x8) """ ws = sqrt(2.) # This makes the initialization equal to He et al. super(P6WideResNet, self).__init__() # The first layer is always a convolution. self.add_link( P6ConvZ2(in_channels=3, out_channels=nc32, ksize=3, stride=1, pad=1, wscale=ws) ) # Add num_blocks ResBlocks (2n layers) for the size 32x32 feature maps for i in range(num_blocks): nc_in = nc32 * k if i > 0 else nc32 self.add_link( WideResBlock2D( in_channels=nc_in, out_channels=nc32 * k, wscale=ws, downsample=False, ConvLink=P6ConvP6, drop_ratio=drop_ratio)) # Add num_blocks ResBlocks (2n layers) for the size 16x16 feature maps # The first convolution uses stride 2 for i in range(num_blocks): nc_in = nc16 * k if i > 0 else nc32 * k downsample = i == 0 self.add_link( WideResBlock2D( in_channels=nc_in, out_channels=nc16 * k, wscale=ws, downsample=downsample, ConvLink=P6ConvP6, drop_ratio=drop_ratio)) # Add num_blocks ResBlocks (2n layers) for the size 8x8 feature maps for i in range(num_blocks): nc_in = nc8 * k if i > 0 else nc16 * k downsample = i == 0 self.add_link( WideResBlock2D( in_channels=nc_in, out_channels=nc8 * k, wscale=ws, downsample=downsample, ConvLink=P6ConvP6, drop_ratio=drop_ratio)) # Add BN and final layer # We do ReLU and average pooling between BN and final layer, but these # don't require a link. self.add_link(F.BatchNormalization(size=nc8 * k)) self.add_link( L.Convolution2D(in_channels=nc8 * k * 6, out_channels=10, ksize=1, wscale=ws))
def __init__(self, num_blocks=4, nc32=17, nc16=35, nc8=69, k=1, drop_ratio=0.0): """ :param num_blocks: the number of resnet blocks per stage. There are 3 stages, for feature map width 32, 16, 8. Total number of layers is 6 * num_blocks + 2 :param nc32: the number of feature maps in the first stage (where feature maps are 32x32) :param nc16: the number of feature maps in the second stage (where feature maps are 16x16) :param nc8: the number of feature maps in the third stage (where feature maps are 8x8) :param k: the widening factor """ ws = np.sqrt(2) super(WideResNetHex, self).__init__() # The first layer is always a convolution. self.add_link( HexConv2D(in_channels=3, ksize=3, pad=1, stride=1, out_channels=nc32, wscale=ws)) # Add num_blocks ResBlocks (2n layers) for the size 32x32 feature maps for i in range(num_blocks): nc_in = nc32 * k if i > 0 else nc32 self.add_link( WideResBlock2D( in_channels=nc_in, out_channels=nc32 * k, wscale=ws, downsample=False, ConvLink=HexConv2D, drop_ratio=drop_ratio)) # Add num_blocks ResBlocks (2n layers) for the size 16x16 feature maps # The first convolution uses stride 2 for i in range(num_blocks): nc_in = nc16 * k if i > 0 else nc32 * k downsample = i == 0 self.add_link( WideResBlock2D( in_channels=nc_in, out_channels=nc16 * k, wscale=ws, downsample=downsample, ConvLink=HexConv2D, drop_ratio=drop_ratio)) # Add num_blocks ResBlocks (2n layers) for the size 8x8 feature maps for i in range(num_blocks): nc_in = nc8 * k if i > 0 else nc16 * k downsample = i == 0 self.add_link( WideResBlock2D( in_channels=nc_in, out_channels=nc8 * k, wscale=ws, downsample=downsample, ConvLink=HexConv2D, drop_ratio=drop_ratio)) # Add BN and final layer # We do ReLU and average pooling between BN and final layer, but # these don't require a link. self.add_link(F.BatchNormalization(size=nc8 * k)) self.add_link( L.Convolution2D( in_channels=nc8 * k, ksize=1, out_channels=10, wscale=ws))