def __init__(self): ksize = 3 bn = True act = F.relu self.dr = 0.3 super(P4MCNN, self).__init__( l1=ConvBNAct( conv=P4MConvZ2(in_channels=1, out_channels=10, ksize=ksize, stride=1, pad=0), bn=bn, act=act ), l2=ConvBNAct( conv=P4MConvP4M(in_channels=10, out_channels=10, ksize=ksize, stride=1, pad=0), bn=bn, act=act ), l3=ConvBNAct( conv=P4MConvZ2(in_channels=10, out_channels=10, ksize=ksize, stride=1, pad=0), bn=bn, act=act ), l4=ConvBNAct( conv=P4MConvP4M(in_channels=10, out_channels=10, ksize=ksize, stride=1, pad=0), bn=bn, act=act ), l5=ConvBNAct( conv=P4MConvP4M(in_channels=10, out_channels=10, ksize=ksize, stride=1, pad=0), bn=bn, act=act ), l6=ConvBNAct( conv=P4MConvP4M(in_channels=10, out_channels=10, ksize=ksize, stride=1, pad=0), bn=bn, act=act ), top=P4MConvP4M(in_channels=10, out_channels=10, ksize=1, stride=1, pad=0), )
def test_p4m_net_equivariance(): from groupy.gfunc import Z2FuncArray, P4MFuncArray import groupy.garray.D4_array as d4a from groupy.gconv.chainer_gconv.p4m_conv import P4MConvZ2, P4MConvP4M im = np.random.randn(1, 1, 11, 11).astype('float32') check_equivariance( im=im, layers=[ P4MConvZ2(in_channels=1, out_channels=2, ksize=3), P4MConvP4M(in_channels=2, out_channels=3, ksize=3) ], input_array=Z2FuncArray, output_array=P4MFuncArray, point_group=d4a, )
def testplot_p4m(im=None, m=0, r=0): if im is None: im = np.zeros((5, 5), dtype='float32') im[0:5, 1] = 1. im[0, 1:4] = 1. im[2, 1:3] = 1. from groupy.gfunc.z2func_array import Z2FuncArray from groupy.garray.D4_array import D4Array def rotate_flip_z2_func(im, flip, theta_index): imf = Z2FuncArray(im) rot = D4Array([flip, theta_index], 'int') rot_imf = rot * imf return rot_imf.v im = rotate_flip_z2_func(im, m, r) filter_e = np.array([[-1., -4., 1.], [-2., 0., 2.], [-1., 0., 1.]]) from groupy.gconv.chainer_gconv.p4m_conv import P4MConvZ2 from chainer import Variable from chainer import cuda print im.shape imv = Variable(cuda.to_gpu(im.astype('float32').reshape(1, 1, 5, 5))) conv = P4MConvZ2(in_channels=1, out_channels=1, ksize=3, pad=2, flat_channels=True, initialW=filter_e.reshape(1, 1, 1, 3, 3)) conv.to_gpu() conv_imv = conv(imv) print im.shape, conv_imv.data.shape return im, cuda.to_cpu(conv_imv.data)
def test_g_z2_conv_equivariance(): from groupy.gfunc import Z2FuncArray, P4FuncArray, P4MFuncArray import groupy.garray.C4_array as c4a import groupy.garray.D4_array as d4a from groupy.gconv.chainer_gconv.p4_conv import P4ConvZ2 from groupy.gconv.chainer_gconv.p4m_conv import P4MConvZ2 im = np.random.randn(1, 1, 11, 11).astype('float32') check_equivariance( im=im, layers=[P4ConvZ2(1, 2, 3)], input_array=Z2FuncArray, output_array=P4FuncArray, point_group=c4a, ) check_equivariance( im=im, layers=[P4MConvZ2(1, 2, 3)], input_array=Z2FuncArray, output_array=P4MFuncArray, point_group=d4a, )
def __init__(self): bn = True ksize = 3 pad = 1 act = F.relu super(P4MAllCNNC, self).__init__( l1=ConvBNAct( conv=P4MConvZ2(in_channels=3, out_channels=32, ksize=ksize, stride=1, pad=pad), bn=bn, act=act ), l2=ConvBNAct( conv=P4MConvP4M(in_channels=32, out_channels=32, ksize=ksize, stride=1, pad=pad), bn=bn, act=act ), l3=ConvBNAct( conv=P4MConvP4M(in_channels=32, out_channels=32, ksize=ksize, stride=2, pad=pad), bn=bn, act=act ), l4=ConvBNAct( conv=P4MConvP4M(in_channels=32, out_channels=64, ksize=ksize, stride=1, pad=pad), bn=bn, act=act ), l5=ConvBNAct( conv=P4MConvP4M(in_channels=64, out_channels=64, ksize=ksize, stride=1, pad=pad), bn=bn, act=act ), l6=ConvBNAct( conv=P4MConvP4M(in_channels=64, out_channels=64, ksize=ksize, stride=2, pad=pad), bn=bn, act=act ), l7=ConvBNAct( conv=P4MConvP4M(in_channels=64, out_channels=64, ksize=ksize, stride=1, pad=pad), bn=bn, act=act ), l8=ConvBNAct( conv=P4MConvP4M(in_channels=64, out_channels=64, ksize=1, stride=1, pad=0), bn=bn, act=act ), # Note: it's unusual to have a bn + relu before softmax, but this is what's described by springenberg et al. l9=ConvBNAct( conv=P4MConvP4M(in_channels=64, out_channels=10, ksize=1, stride=1, pad=0), bn=bn, act=act ), ) wtscale = 0.035 self.l1.conv.W.data = (np.random.randn(*self.l1.conv.W.data.shape) * wtscale).astype(np.float32) self.l2.conv.W.data = (np.random.randn(*self.l2.conv.W.data.shape) * wtscale).astype(np.float32) self.l3.conv.W.data = (np.random.randn(*self.l3.conv.W.data.shape) * wtscale).astype(np.float32) self.l4.conv.W.data = (np.random.randn(*self.l4.conv.W.data.shape) * wtscale).astype(np.float32) self.l5.conv.W.data = (np.random.randn(*self.l5.conv.W.data.shape) * wtscale).astype(np.float32) self.l6.conv.W.data = (np.random.randn(*self.l6.conv.W.data.shape) * wtscale).astype(np.float32) self.l7.conv.W.data = (np.random.randn(*self.l7.conv.W.data.shape) * wtscale).astype(np.float32) self.l8.conv.W.data = (np.random.randn(*self.l8.conv.W.data.shape) * wtscale).astype(np.float32) self.l9.conv.W.data = (np.random.randn(*self.l9.conv.W.data.shape) * wtscale).astype(np.float32)
def __init__(self, num_blocks=18, nc32=6, nc16=11, nc8=23): """ :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) """ ksize = 3 pad = 1 ws = sqrt( 2.) # This makes the initialization equal to that of He et al. super(P4MResNet, self).__init__() # The first layer is always a convolution. self.add_link( P4MConvZ2(in_channels=3, out_channels=nc32, ksize=ksize, stride=1, pad=pad, wscale=ws)) # Add num_blocks ResBlocks (2 * num_blocks layers) for the size 32x32 feature maps for i in range(num_blocks): self.add_link( ResBlock2D(in_channels=nc32, out_channels=nc32, ksize=ksize, fiber_map='id', conv_link=P4MConvP4M, stride=1, pad=pad, wscale=ws)) # Add num_blocks ResBlocks (2 * num_blocks layers) for the size 16x16 feature maps # The first convolution uses stride 2 for i in range(num_blocks): stride = 1 if i > 0 else 2 fiber_map = 'id' if i > 0 else 'linear' nc_in = nc16 if i > 0 else nc32 self.add_link( ResBlock2D(in_channels=nc_in, out_channels=nc16, ksize=ksize, fiber_map=fiber_map, conv_link=P4MConvP4M, stride=stride, pad=pad, wscale=ws)) # Add num_blocks ResBlocks (2 * num_blocks layers) for the size 8x8 feature maps # The first convolution uses stride 2 for i in range(num_blocks): stride = 1 if i > 0 else 2 fiber_map = 'id' if i > 0 else 'linear' nc_in = nc8 if i > 0 else nc16 self.add_link( ResBlock2D(in_channels=nc_in, out_channels=nc8, ksize=ksize, fiber_map=fiber_map, conv_link=P4MConvP4M, stride=stride, pad=pad, wscale=ws)) # Add BN and final layer # We do ReLU and average pooling between BN and final layer, # but since these are stateless they don't require a Link. self.add_link(F.BatchNormalization(size=nc8)) self.add_link( F.Convolution2D(in_channels=nc8 * 8, out_channels=10, ksize=1, stride=1, pad=0, wscale=ws))