def __init__(self): super(AlexNet_flic_BN_PReLU, self).__init__(conv1=F.Convolution2D(3, 96, 11, stride=4, pad=1), bn1=F.BatchNormalization(96), prelu1=F.PReLU(), conv2=F.Convolution2D(96, 256, 5, stride=1, pad=2), bn2=F.BatchNormalization(256), prelu2=F.PReLU(), conv3=F.Convolution2D(256, 384, 3, stride=1, pad=1), prelu3=F.PReLU(), conv4=F.Convolution2D(384, 384, 3, stride=1, pad=1), prelu4=F.PReLU(), conv5=F.Convolution2D(384, 256, 3, stride=1, pad=1), prelu5=F.PReLU(), fc6=F.Linear(9216, 4096), prelu6=F.PReLU(), fc7=F.Linear(4096, 4096), prelu7=F.PReLU(), fc8=F.Linear(4096, 14))
def __init__(self, n_out): super(MLP, self).__init__(conv1=L.ConvolutionND(1, 1, 3, 20), bn1=F.BatchNormalization(3), conv2=L.ConvolutionND(1, 3, 5, 5, pad=1), bn2=F.BatchNormalization(5), conv3=L.ConvolutionND(1, 5, 5, 5, pad=1), fl4=F.Linear(149885, 256), fl5=F.Linear(256, n_out))
def __init__(self): super(myNet, self).__init__( conv1=F.Convolution2D(1, 20, 5), norm1=F.BatchNormalization(20), conv2=F.Convolution2D(20, 50, 5), norm2=F.BatchNormalization(50), ip1=F.Linear(4050, 1000), ip2=F.Linear(1000, 799), ) self.train = True
def __init__(self): super(Encoder, self).__init__(l1=F.Linear(dim, hidden_list[0], wscale=0.1), l2=F.Linear(hidden_list[0], hidden_list[1], wscale=0.1), l3=F.Linear(hidden_list[1], n_bit, wscale=0.0001), bn1=F.BatchNormalization(hidden_list[0]), bn2=F.BatchNormalization(hidden_list[1]))
def __init__(self): super(AlexBN, self).__init__( conv1=F.Convolution2D(3, 96, 11, stride=4), bn1=F.BatchNormalization(96), conv2=F.Convolution2D(96, 256, 5, pad=2), bn2=F.BatchNormalization(256), conv3=F.Convolution2D(256, 384, 3, pad=1), conv4=F.Convolution2D(384, 384, 3, pad=1), conv5=F.Convolution2D(384, 256, 3, pad=1), fc6=F.Linear(9216, 4096), fc7=F.Linear(4096, 4096), fc8=F.Linear(4096, 1000), )
def __init__(self): super(CaffeNet, self).__init__( conv1=F.Convolution2D(3, 96, 11, stride=4, pad=5), bn1=F.BatchNormalization(96), conv2=F.Convolution2D(96, 256, 5, stride=1, pad=2), bn2=F.BatchNormalization(256), conv3=F.Convolution2D(256, 384, 3, stride=1, pad=1), conv4=F.Convolution2D(384, 384, 3, stride=1, pad=1), conv5=F.Convolution2D(384, 256, 3, stride=1, pad=1), fc6=F.Linear(9216, 4096), fc7=F.Linear(4096, 4096), cls_score=F.Linear(4096, 21), bbox_pred=F.Linear(4096, 84) )
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))
def __init__(self, num_blocks=18, nc32=16, nc16=32, nc8=64): """ :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(ResNet, self).__init__() # The first layer is always a convolution. self.add_link( Convolution2D(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', 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, 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, 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(Convolution2D(in_channels=nc8, out_channels=10, ksize=1, stride=1, pad=0, wscale=ws))
def __init__(self, num_iunits, num_hunits, num_labels): super(cnn_lstm_fc1, self).__init__( conv11=F.Convolution2D(1, 16, 6, stride=2), bn11=F.BatchNormalization(16), conv12=F.Convolution2D(16, 32, 5, stride=1), bn12=F.BatchNormalization(32), conv13=F.Convolution2D(32, 48, 3, stride=1), fc14=F.Linear(1728, num_iunits / 2), conv21=F.Convolution2D(1, 16, 6, stride=2), bn21=F.BatchNormalization(16), conv22=F.Convolution2D(16, 32, 5, stride=1), bn22=F.BatchNormalization(32), conv23=F.Convolution2D(32, 48, 3, stride=1), fc24=F.Linear(1728, num_iunits / 2), i2h=F.Linear(num_iunits, num_hunits * 4), h2h=F.Linear(num_hunits, num_hunits * 4), h2y=F.Linear(num_hunits, num_labels), )
def __init__(self, num_labels): super(syuwa_cnn, self).__init__( conv11=F.Convolution2D(1, 16, 6, stride=2), bn11=F.BatchNormalization(16), conv12=F.Convolution2D(16, 32, 5, stride=1), bn12=F.BatchNormalization(32), conv13=F.Convolution2D(32, 48, 3, stride=1), fc14=F.Linear(1728, 512), fc15=F.Linear(512, 200), conv21=F.Convolution2D(1, 16, 6, stride=2), bn21=F.BatchNormalization(16), conv22=F.Convolution2D(16, 32, 5, stride=1), bn22=F.BatchNormalization(32), conv23=F.Convolution2D(32, 48, 3, stride=1), fc24=F.Linear(1728, 512), fc25=F.Linear(512, 200), fc_comb=F.Linear(400, num_labels), )
def __init__(self, conv, bn=True, act=F.relu): super(ConvBNAct, self).__init__(conv=conv) if bn: out_channels = self.conv.W.data.shape[0] self.add_link('bn', F.BatchNormalization(out_channels)) else: self.bn = None self.act = act
def __init__(self, in_channels, out1, proj3, out3, proj33, out33, pooltype, proj_pool=None, stride=1): if out1 > 0: assert stride == 1 assert proj_pool is not None self.f = FunctionSet( proj3=F.Convolution2D(in_channels, proj3, 1, nobias=True), conv3=F.Convolution2D(proj3, out3, 3, pad=1, stride=stride, nobias=True), proj33=F.Convolution2D(in_channels, proj33, 1, nobias=True), conv33a=F.Convolution2D(proj33, out33, 3, pad=1, nobias=True), conv33b=F.Convolution2D(out33, out33, 3, pad=1, stride=stride, nobias=True), proj3n=F.BatchNormalization(proj3), conv3n=F.BatchNormalization(out3), proj33n=F.BatchNormalization(proj33), conv33an=F.BatchNormalization(out33), conv33bn=F.BatchNormalization(out33), ) if out1 > 0: self.f.conv1 = F.Convolution2D(in_channels, out1, 1, stride=stride, nobias=True) self.f.conv1n = F.BatchNormalization(out1) if proj_pool is not None: self.f.poolp = F.Convolution2D(in_channels, proj_pool, 1, nobias=True) self.f.poolpn = F.BatchNormalization(proj_pool) if pooltype == 'max': self.f.pool = MaxPooling2D(3, stride=stride, pad=1) elif pooltype == 'avg': self.f.pool = AveragePooling2D(3, stride=stride, pad=1) else: raise NotImplementedError()
def __init__(self, num_iunits, num_hunits, num_labels): super(syuwa_conv_lstm, self).__init__( conv11=F.Convolution2D(1, 16, 6, stride=3), bn11=F.BatchNormalization(16), conv12=F.Convolution2D(16, 32, 5, stride=3, pad=1), bn12=F.BatchNormalization(32), conv13=F.Convolution2D(32, 48, 4, stride=2), fc14=F.Linear(1728, 512), fc15=F.Linear(512, num_iunits / 2), conv21=F.Convolution2D(1, 16, 6, stride=3), bn21=F.BatchNormalization(16), conv22=F.Convolution2D(16, 32, 5, stride=3, pad=1), bn22=F.BatchNormalization(32), conv23=F.Convolution2D(32, 48, 4, stride=2), fc24=F.Linear(1728, 512), fc25=F.Linear(512, num_iunits / 2), i2h = F.Linear(num_iunits, num_hunits * 4), h2h = F.Linear(num_hunits, num_hunits * 4), h2y = F.Linear(num_hunits, num_labels), )
def __init__(self): super(GoogLeNetBN, self).__init__( conv1=F.Convolution2D(3, 64, 7, stride=2, pad=3, nobias=True), norm1=F.BatchNormalization(64), conv2=F.Convolution2D(64, 192, 3, pad=1, nobias=True), norm2=F.BatchNormalization(192), inc3a=F.InceptionBN(192, 64, 64, 64, 64, 96, 'avg', 32), inc3b=F.InceptionBN(256, 64, 64, 96, 64, 96, 'avg', 64), inc3c=F.InceptionBN(320, 0, 128, 160, 64, 96, 'max', stride=2), inc4a=F.InceptionBN(576, 224, 64, 96, 96, 128, 'avg', 128), inc4b=F.InceptionBN(576, 192, 96, 128, 96, 128, 'avg', 128), inc4c=F.InceptionBN(576, 128, 128, 160, 128, 160, 'avg', 128), inc4d=F.InceptionBN(576, 64, 128, 192, 160, 192, 'avg', 128), inc4e=F.InceptionBN(576, 0, 128, 192, 192, 256, 'max', stride=2), inc5a=F.InceptionBN(1024, 352, 192, 320, 160, 224, 'avg', 128), inc5b=F.InceptionBN(1024, 352, 192, 320, 192, 224, 'max', 128), out=F.Linear(1024, 1000), conva=F.Convolution2D(576, 128, 1, nobias=True), norma=F.BatchNormalization(128), lina=F.Linear(2048, 1024, nobias=True), norma2=F.BatchNormalization(1024), outa=F.Linear(1024, 1000), convb=F.Convolution2D(576, 128, 1, nobias=True), normb=F.BatchNormalization(128), linb=F.Linear(2048, 1024, nobias=True), normb2=F.BatchNormalization(1024), outb=F.Linear(1024, 1000), )
def setUp(self): self.func = functions.BatchNormalization(3) self.func.gamma = numpy.random.uniform( .5, 1, self.func.gamma.shape).astype(numpy.float32) self.func.beta = numpy.random.uniform( -1, 1, self.func.beta.shape).astype(numpy.float32) self.func.ggamma.fill(0) self.func.gbeta.fill(0) self.gamma = self.func.gamma.copy().reshape(1, 3) # fixed on CPU self.beta = self.func.beta.copy().reshape(1, 3) # fixed on CPU self.x = numpy.random.uniform(-1, 1, (7, 3)).astype(numpy.float32) self.gy = numpy.random.uniform(-1, 1, (7, 3)).astype(numpy.float32)
def __init__(self, num_blocks=3, nc32=8, nc16=16, nc8=32, dropout=0.5): ksize = 3 pad = 1 wscale = np.sqrt(2) self.dropout = dropout super(ResNet, self, ).__init__() self.add_link( L.Convolution2D(in_channels=3, out_channels=nc32, ksize=ksize, stride=1, pad=pad, wscale=wscale) ) for i in range(num_blocks): self.add_link( ResBlock( in_channels=nc32, out_channels=nc32, ksize=ksize, fiber_map='id', stride=1, pad=pad, wscale=wscale ) ) for i in range(num_blocks): in_channels = nc16 if i > 0 else nc32 fiber_map = 'id' if i > 0 else 'linear' stride = 1 if i > 0 else 2 self.add_link( ResBlock( in_channels=in_channels, out_channels=nc16, ksize=ksize, fiber_map=fiber_map, stride=stride, pad=pad, wscale=wscale ) ) for i in range(num_blocks): in_channels = nc8 if i > 0 else nc16 fiber_map = 'id' if i > 0 else 'linear' stride = 1 if i > 0 else 2 self.add_link( ResBlock( in_channels=in_channels, out_channels=nc8, ksize=ksize, fiber_map=fiber_map, stride=stride, pad=pad, wscale=wscale ) ) self.add_link( F.BatchNormalization(nc8) ) self.add_link(L.Convolution2D(in_channels=nc8, out_channels=5, ksize=4, wscale=wscale))
def __init__(self): super(MyFcn, self).__init__( conv1_1=L.Convolution2D(30, 64, 3, stride=1, pad=1), bn1=F.BatchNormalization(64), conv1_2=L.Convolution2D(64, 64, 3, stride=1, pad=1), bn2=F.BatchNormalization(64), conv1_3=L.Convolution2D(64, 64, 3, stride=1, pad=1), bn3=F.BatchNormalization(64), conv1_4=L.Convolution2D(64, 32, 3, stride=1, pad=1), bn4=F.BatchNormalization(32), conv1_5=L.Convolution2D(32, 32, 3, stride=1, pad=1), bn5=F.BatchNormalization(32), conv1_6=L.Convolution2D(32, 32, 3, stride=1, pad=1), bn6=F.BatchNormalization(32), conv1_7=L.Convolution2D(32, MyFcn.CLASSES, 3, stride=1, pad=1) # conv2_1=L.Convolution2D( 64, 128, 3, stride=1, pad=1), # conv2_2=L.Convolution2D(128, 128, 3, stride=1, pad=1), # conv3_1=L.Convolution2D(128, 256, 3, stride=1, pad=1), # conv3_2=L.Convolution2D(256, 256, 3, stride=1, pad=1), # conv3_3=L.Convolution2D(256, 256, 3, stride=1, pad=1), # conv4_1=L.Convolution2D(256, 512, 3, stride=1, pad=1), # conv4_2=L.Convolution2D(512, 512, 3, stride=1, pad=1), # conv4_3=L.Convolution2D(512, 512, 3, stride=1, pad=1), # conv5_1=L.Convolution2D(512, 512, 3, stride=1, pad=1), # conv5_2=L.Convolution2D(512, 512, 3, stride=1, pad=1), # conv5_3=L.Convolution2D(512, 512, 3, stride=1, pad=1), # score_pool3=L.Convolution2D(256, MyFcn.CLASSES, 1, stride=1, pad=0), # score_pool4=L.Convolution2D(512, MyFcn.CLASSES, 1, stride=1, pad=0), # score_pool5=L.Convolution2D(512, MyFcn.CLASSES, 1, stride=1, pad=0), # upsample_pool4=L.Deconvolution2D(MyFcn.CLASSES, MyFcn.CLASSES, ksize= 4, stride=2, pad=1), # upsample_pool5=L.Deconvolution2D(MyFcn.CLASSES, MyFcn.CLASSES, ksize= 8, stride=4, pad=2), # upsample_final=L.Deconvolution2D(MyFcn.CLASSES, MyFcn.CLASSES, ksize=16, stride=8, pad=4), ) self.train = True
if os.path.exists(join(prefix, file)) and '.pkl' in file: with open(join(prefix, file), 'rb') as f: data_dic = cPickle.load(f) data.append(data_dic['data']) label = np.append(label, data_dic['labels']) data = np.vstack(data) return data, label train_data, train_labels = load_data() train_labels = train_labels.astype(np.int32) """ network definition """ model = FunctionSet(conv1=F.Convolution2D(3, 32, 5, stride=1, pad=2), norm1=F.BatchNormalization(32), conv2=F.Convolution2D(32, 32, 5, stride=1, pad=2), norm2=F.BatchNormalization(32), conv3=F.Convolution2D(32, 16, 5, stride=1, pad=2), norm3=F.BatchNormalization(16), conv4=F.Convolution2D(16, 2, 5, stride=1, pad=0), ip1=F.Linear(2, 2)) def forward(x_data, y_data, train=True): x, t = Variable(cuda.to_gpu(x_data)), chainer.Variable(cuda.to_gpu(y_data)) h = model.conv1(x) h = model.norm1(h) h = F.relu(h) h = F.max_pooling_2d(h, 3, stride=2)
import chainer from chainer import Function, FunctionSet, Variable, optimizers, serializers, utils from chainer import Link, Chain, ChainList import chainer.functions as F import chainer.links as L from progressbar import ProgressBar import matplotlib.pyplot as plt prefix = '../data' model = FunctionSet( conv1=F.Convolution2D(1, 20, 5), norm1=F.BatchNormalization(20), conv2=F.Convolution2D(20, 50, 5), norm2=F.BatchNormalization(50), ip1=F.Linear(4050, 1000), ip2=F.Linear(1000, 799), ) def forward(x_data, y_data, train=False, normalized=False): x, t = Variable(x_data), chainer.Variable(y_data) h = model.conv1(x) h = model.norm1(h) h = F.relu(h) h = F.max_pooling_2d(h, 3, stride=2) h = model.conv2(h) h = model.norm2(h)
# example of Convolutional Neural Network from libdnn import Classifier import chainer import chainer.functions as F import numpy from sklearn.datasets import fetch_mldata model = chainer.FunctionSet(conv1=F.Convolution2D(1, 15, 5), bn1=F.BatchNormalization(15), conv2=F.Convolution2D(15, 30, 3, pad=1), bn2=F.BatchNormalization(30), conv3=F.Convolution2D(30, 64, 3, pad=1), fl4=F.Linear(2304, 576), fl5=F.Linear(576, 10)) def forward(self, x, train): h = F.max_pooling_2d(F.relu(model.bn1(model.conv1(x))), 2) h = F.relu(model.bn2(model.conv2(h))) h = F.max_pooling_2d(F.relu(model.conv3(h)), 2) h = F.dropout(F.relu(model.fl4(h)), train=False) y = model.fl5(h) return y cnn = Classifier(model, gpu=-1) cnn.set_forward(forward) mnist = fetch_mldata('MNIST original', data_home='.')
original_model = pickle.load(open(PICKLE_PATH)) img = np.array(Image.open(IMPUT_IMAGE_PATH)) plt.imshow(img) img = img.astype(np.float32) print img.shape img = img.transpose(2, 0, 1) img = img[np.newaxis, :] img /= 255 print img print img.shape model = FunctionSet(conv1=F.Convolution2D(3, 96, 11, stride=4), bn1=F.BatchNormalization(96), conv2=F.Convolution2D(96, 256, 5, pad=2), bn2=F.BatchNormalization(256), conv3=F.Convolution2D(256, 384, 3, pad=1), conv4=F.Convolution2D(384, 384, 3, pad=1), conv5=F.Convolution2D(384, 256, 3, pad=1), fc6=F.Linear(9216, 4096), fc7=F.Linear(4096, 4096), fc8=F.Linear(4096, 1000)) ## copy parameter model.conv1.W.data = original_model.conv1.W.data model.conv1.b.data = original_model.conv1.b.data model.conv2.W.data = original_model.conv2.W.data model.conv2.b.data = original_model.conv2.b.data model.conv3.W.data = original_model.conv3.W.data
attr_triple_id2image_id = pickle.load(f) with open('../work/index2attribute.pkl', 'r') as f: index2attribute = pickle.load(f) attribute2index = dict((v, k) for k, v in index2attribute.iteritems()) #Model Preparation print "preparing model" image_feature_dim = 1024 #image feature dimention per image n_units = 128 # number of units per layer vocab_size = len(attribute2index) model = chainer.FunctionSet() model.img_feature2vec = F.Linear(2 * image_feature_dim, n_units) #parameter W,b model.bn_feature = F.BatchNormalization(n_units) #parameter sigma,gamma model.h1 = F.Linear(n_units, n_units) #hidden unit,#parameter W,b model.bn1 = F.BatchNormalization(n_units) #parameter gamma,beta model.out = F.Linear(n_units, vocab_size) #parameter W,b #To GPU if gpu_id >= 0: model.to_gpu() #Define Newtowork (Forward) def forward(x_data, y_data, train=True): x = Variable(x_data, volatile=not train) t = Variable(y_data, volatile=not train) feature_input = F.relu(model.bn_feature(model.img_feature2vec(x))) l1 = F.relu(model.bn1(model.h1(feature_input)))
Conf_matrix_list = [] for train_index, test_index in skf: # Model definition model = FunctionSet(conv1=F.Convolution2D(1, 32, (160, 64), stride=(1, 2)), conv2=F.Convolution2D(32, 64, (1, 16), stride=(1, 2)), conv3=F.Convolution2D(1, 32, (8, 8)), conv4=F.Convolution2D(32, 32, (8, 8)), conv5=F.Convolution2D(32, 64, (1, 4)), conv6=F.Convolution2D(64, 64, (1, 4)), conv7=F.Convolution2D(64, 128, (1, 2)), conv8=F.Convolution2D(128, 128, (1, 2)), conv9=F.Convolution2D(128, 256, (1, 2)), conv10=F.Convolution2D(256, 256, (1, 2)), fc11=F.Linear(256 * 10 * 1, 1024), norm1=F.BatchNormalization(1024), fc12=F.Linear(1024, 1024), norm2=F.BatchNormalization(1024), fc13=F.Linear(1024, 3)) #optimizer = optimizers.MomentumSGD(lr=LR, momentum=0.9) #optimizer = optimizers.SMORMS3(lr=LR, eps=1e-16) #optimizer = optimizers.AdaGrad(lr=LR) optimizer = optimizers.NesterovAG(lr=LR, momentum=0.9) optimizer.setup(model) if GPU_ID >= 0: cuda.get_device(GPU_ID).use() model.to_gpu(GPU_ID) print 'show train_index,test_index'
print('load MNIST dataset') mnist = data.load_mnist_data() mnist['data'] = mnist['data'].astype(np.float32) mnist['data'] /= 255 mnist['data'] = mnist['data'].reshape(70000, 1, 28, 28) mnist['target'] = mnist['target'].astype(np.int32) N = 60000 x_train, x_test = np.split(mnist['data'], [N]) y_train, y_test = np.split(mnist['target'], [N]) N_test = y_test.size # Prepare multi-layer perceptron model model = chainer.FunctionSet(cv1=F.Convolution2D(1, 30, 3), bn2=F.BatchNormalization(30), ln3=F.Linear(5070, 1000), ln4=F.Linear(1000, 10)) if args.gpu >= 0: cuda.get_device(args.gpu).use() model.to_gpu() def forward(x_data, y_data, train=True): # Neural net architecture x, t = chainer.Variable(x_data), chainer.Variable(y_data) h = F.max_pooling_2d( F.dropout(F.relu(model.bn2(model.cv1(x))), train=train), 2) h = F.dropout(F.relu(model.ln3(h)), train=train) y = model.ln4(h)