def convolution(x): x = self.convolution[0](x) x = self.convolution[1](x) x = self.convolution[2](x) x = M.max_pooling_2d(x, 3, 2) x = self.convolution[3](x) x = self.convolution[4](x) x = M.max_pooling_2d(x, 3, 2) return x
def __init__(self, pretrained_model='auto'): super(GoogLeNet, self).__init__( conv1=Convolution2D(3, 64, 7, stride=2, pad=3), conv2_reduce=Convolution2D(64, 64, 1), conv2=Convolution2D(64, 192, 3, stride=1, pad=1), inc3a=Inception(192, 64, 96, 128, 16, 32, 32), inc3b=Inception(256, 128, 128, 192, 32, 96, 64), inc4a=Inception(480, 192, 96, 208, 16, 48, 64), inc4b=Inception(512, 160, 112, 224, 24, 64, 64), inc4c=Inception(512, 128, 128, 256, 24, 64, 64), inc4d=Inception(512, 112, 144, 288, 32, 64, 64), inc4e=Inception(528, 256, 160, 320, 32, 128, 128), inc5a=Inception(832, 256, 160, 320, 32, 128, 128), inc5b=Inception(832, 384, 192, 384, 48, 128, 128), loss3_fc=Linear(1024, 1000), loss1_conv=Convolution2D(512, 128, 1), loss1_fc1=Linear(4 * 4 * 128, 1024), loss1_fc2=Linear(1024, 1000), loss2_conv=Convolution2D(528, 128, 1), loss2_fc1=Linear(4 * 4 * 128, 1024), loss2_fc2=Linear(1024, 1000), ) if pretrained_model == 'auto': _retrieve( 'bvlc_googlenet.npz', 'http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel', self) elif pretrained_model: npz.load_npz(pretrained_model, self) self.functions = OrderedDict([ ('conv1', [self.conv1, relu]), ('pool1', [ lambda x: max_pooling_2d(x, ksize=3, stride=2), lambda x: local_response_normalization(x, n=5) ]), ('conv2_reduce', [self.conv2_reduce, relu]), ('conv2', [self.conv2, relu]), ('pool2', [ lambda x: local_response_normalization(x, n=5), lambda x: max_pooling_2d(x, ksize=3, stride=2) ]), ('inc3a', [self.inc3a]), ('inc3b', [self.inc3b]), ('pool3', [lambda x: max_pooling_2d(x, ksize=3, stride=2)]), ('inc4a', [self.inc4a]), ('inc4b', [self.inc4b]), ('inc4c', [self.inc4c]), ('inc4d', [self.inc4d]), ('inc4e', [self.inc4e]), ('pool4', [lambda x: max_pooling_2d(x, ksize=3, stride=2)]), ('inc5a', [self.inc5a]), ('inc5b', [self.inc5b]), ('pool6', [lambda x: average_pooling_2d(x, ksize=7, stride=1)]), ('prob', [lambda x: dropout(x, ratio=0.4), self.loss3_fc]) ])
def __call__(self, x): h = max_pooling_2d.max_pooling_2d(relu.relu(self.conv1(x)), 3, stride=2) h = max_pooling_2d.max_pooling_2d(relu.relu(self.conv2(h)), 3, stride=2) h = relu.relu(self.conv3(h)) h = relu.relu(self.conv4(h)) h = max_pooling_2d.max_pooling_2d(relu.relu(self.conv5(h)), 3, stride=2) h = self.fc6(h) h = self.fc7(h) return self.fc8(h)
def __call__(self, x): outs = [] if self.out1 > 0: h1 = self.conv1(x) h1 = self.conv1n(h1) h1 = relu.relu(h1) outs.append(h1) h3 = relu.relu(self.proj3n(self.proj3(x))) h3 = relu.relu(self.conv3n(self.conv3(h3))) outs.append(h3) h33 = relu.relu(self.proj33n(self.proj33(x))) h33 = relu.relu(self.conv33an(self.conv33a(h33))) h33 = relu.relu(self.conv33bn(self.conv33b(h33))) outs.append(h33) if self.pooltype == 'max': p = max_pooling_2d.max_pooling_2d(x, 3, stride=self.stride, pad=1, cover_all=False) else: p = average_pooling_2d.average_pooling_2d(x, 3, stride=self.stride, pad=1) if self.proj_pool is not None: p = relu.relu(self.poolpn(self.poolp(p))) outs.append(p) y = concat.concat(outs, axis=1) return y
def __call__(self, x): test = not self.train outs = [] if self.out1 > 0: h1 = self.conv1(x) h1 = self.conv1n(h1, test=test) h1 = relu.relu(h1) outs.append(h1) h3 = relu.relu(self.proj3n(self.proj3(x), test=test)) h3 = relu.relu(self.conv3n(self.conv3(h3), test=test)) outs.append(h3) h33 = relu.relu(self.proj33n(self.proj33(x), test=test)) h33 = relu.relu(self.conv33an(self.conv33a(h33), test=test)) h33 = relu.relu(self.conv33bn(self.conv33b(h33), test=test)) outs.append(h33) if self.pooltype == 'max': p = max_pooling_2d.max_pooling_2d(x, 3, stride=self.stride, pad=1) else: p = average_pooling_2d.average_pooling_2d(x, 3, stride=self.stride, pad=1) if self.proj_pool is not None: p = relu.relu(self.poolpn(self.poolp(p), test=test)) outs.append(p) y = concat.concat(outs, axis=1) return y
def __init__(self, pretrained_model='auto'): if pretrained_model: # As a sampling process is time-consuming, # we employ a zero initializer for faster computation. kwargs = {'initialW': constant.Zero()} else: # employ default initializers used in the original paper kwargs = {'initialW': normal.HeNormal(scale=1.0)} super(ResNet50Layers, self).__init__( conv1=Convolution2D(3, 64, 7, 2, 3, **kwargs), bn1=BatchNormalization(64), res2=BuildingBlock(3, 64, 64, 256, 1, **kwargs), res3=BuildingBlock(4, 256, 128, 512, 2, **kwargs), res4=BuildingBlock(6, 512, 256, 1024, 2, **kwargs), res5=BuildingBlock(3, 1024, 512, 2048, 2, **kwargs), fc6=Linear(2048, 1000), ) if pretrained_model == 'auto': _retrieve( 'ResNet-50-model.npz', 'ResNet-50-model.caffemodel', self) elif pretrained_model: npz.load_npz(pretrained_model, self) self.functions = OrderedDict([ ('conv1', [self.conv1, self.bn1, relu]), ('pool1', [lambda x: max_pooling_2d(x, ksize=3, stride=2)]), ('res2', [self.res2]), ('res3', [self.res3]), ('res4', [self.res4]), ('res5', [self.res5]), ('pool5', [_global_average_pooling_2d]), ('fc6', [self.fc6]), ('prob', [softmax]), ])
def functions(self): return collections.OrderedDict([ ('conv1', [self.conv1, self.bn1, relu]), ('pool1', [lambda x: max_pooling_2d(x, ksize=3, stride=2)]), ('res2', [self.res2]), ('res3', [self.res3]), ('res4', [self.res4]), ('res5', [self.res5]) ])
def forward(self, x): outs = [] if self.out1 > 0: h1 = self.conv1(x) h1 = self.conv1n(h1) h1 = relu.relu(h1) outs.append(h1) h3 = relu.relu(self.proj3n(self.proj3(x))) h3 = relu.relu(self.conv3n(self.conv3(h3))) outs.append(h3) h33 = relu.relu(self.proj33n(self.proj33(x))) h33 = relu.relu(self.conv33an(self.conv33a(h33))) h33 = relu.relu(self.conv33bn(self.conv33b(h33))) outs.append(h33) if self.pooltype == 'max': p = max_pooling_2d.max_pooling_2d(x, 3, stride=self.stride, pad=1, cover_all=False) else: p = average_pooling_2d.average_pooling_2d(x, 3, stride=self.stride, pad=1) if self.proj_pool is not None: p = relu.relu(self.poolpn(self.poolp(p))) outs.append(p) y = concat.concat(outs, axis=1) return y
def __call__(self, x): out1 = self.f.conv1(x) out3 = self.f.conv3(relu.relu(self.f.proj3(x))) out5 = self.f.conv5(relu.relu(self.f.proj5(x))) pool = self.f.projp( max_pooling_2d.max_pooling_2d(x, 3, stride=1, pad=1)) y = relu.relu(concat.concat((out1, out3, out5, pool), axis=1)) return y
def __call__(self, x): out1 = self.f.conv1(x) out3 = self.f.conv3(relu.relu(self.f.proj3(x))) out5 = self.f.conv5(relu.relu(self.f.proj5(x))) pool = self.f.projp(max_pooling_2d.max_pooling_2d( x, 3, stride=1, pad=1)) y = relu.relu(concat.concat((out1, out3, out5, pool), axis=1)) return y
def __call__(self, x): h = relu.relu(self.conv1(x), self.use_cudnn) h = max_pooling_2d.max_pooling_2d(h, 2, stride=2, use_cudnn=self.use_cudnn) h = relu.relu(self.conv2(h), self.use_cudnn) h = max_pooling_2d.max_pooling_2d(h, 2, stride=2, use_cudnn=self.use_cudnn) h = relu.relu(self.conv3_1(h), self.use_cudnn) h = relu.relu(self.conv3_2(h), self.use_cudnn) h = max_pooling_2d.max_pooling_2d(h, 2, stride=2, use_cudnn=self.use_cudnn) h = relu.relu(self.conv4_1(h), self.use_cudnn) h = relu.relu(self.conv4_2(h), self.use_cudnn) h = max_pooling_2d.max_pooling_2d(h, 2, stride=2, use_cudnn=self.use_cudnn) h = relu.relu(self.conv5_1(h), self.use_cudnn) h = relu.relu(self.conv5_2(h), self.use_cudnn) h = max_pooling_2d.max_pooling_2d(h, 2, stride=2, use_cudnn=self.use_cudnn) h = self.fc6(h) h = self.fc7(h) return self.fc8(h)
def functions(self): return collections.OrderedDict([ ('conv1', [self.conv1, relu]), ('pool1', [lambda x: max_pooling_2d(x, 3, stride=2)]), ('fire2', [self.fire2]), ('fire3', [self.fire3]), ('pool2', [lambda x: max_pooling_2d(x, 3, stride=2)]), ('fire4', [self.fire4]), ('fire5', [self.fire5]), ('pool3', [lambda x: max_pooling_2d(x, 3, stride=2)]), ('fire6', [self.fire6]), ('fire7', [self.fire7]), ('fire8', [self.fire8]), ('fire9', [self.fire9, dropout]), ('conv10', [self.conv10, relu]), ('pool4', [lambda x: average_pooling_2d(x, 13)]), ('prob', [lambda x: reshape(x, (-1, 1000))]), ])
def functions(self): return collections.OrderedDict([ ('conv1', [self.conv1, self.bn1, relu]), ('pool1', [lambda x: max_pooling_2d(x, ksize=3, stride=2)]), ('res2', [self.res2]), ('res3', [self.res3]), ('res4', [self.res4]), ('res5', [self.res5]), ('pool5', [_global_average_pooling_2d]), ('fc6', [self.fc6]), ('prob', [softmax]), ])
def __call__(self, x): h0 = max_pooling_2d.max_pooling_2d(relu.relu(self.conv1a(x)), 3, stride=2) h1 = max_pooling_2d.max_pooling_2d(relu.relu(self.conv1b(x)), 3, stride=2) h0 = max_pooling_2d.max_pooling_2d(relu.relu(self.conv2a(h0)), 3, stride=2) h1 = max_pooling_2d.max_pooling_2d(relu.relu(self.conv2b(h1)), 3, stride=2) h2 = relu.relu(self.conv3a(h0) + self.conv3b(h1)) h3 = relu.relu(self.conv3c(h0) + self.conv3d(h1)) h2 = relu.relu(self.conv4a(h2)) h3 = relu.relu(self.conv4b(h3)) h2 = max_pooling_2d.max_pooling_2d(relu.relu(self.conv5a(h2)), 3, stride=2) h3 = max_pooling_2d.max_pooling_2d(relu.relu(self.conv5b(h3)), 3, stride=2) h3 = self.fc6a(h2) + self.fc6b(h3) h3 = self.fc7(h3) return self.fc8(h3)
def functions(self): return collections.OrderedDict([ ('conv1', [self.conv1, self.bn1, relu]), ('pool1', [lambda x: max_pooling_2d(x, ksize=3, stride=2)]), ('res2', [self.res2]), ('res3', [self.res3]), ('res4', [self.res4]), ('res5', [self.res5]), #('pool5', [_global_average_pooling_2d]), #not for us... #('fc6', [self.fc6]), ('score_fr', [self.score_fr]), ('upscore', [self.upscore]), #('prob', [softmax]), #the other fcn doesn't have this, as there its in the sofmax crossentropy ])
def forward(self, x): """Computes the output of the Inception module. Args: x (~chainer.Variable): Input variable. Returns: Variable: Output variable. Its array has the same spatial size and the same minibatch size as the input array. The channel dimension has size ``out1 + out3 + out5 + proj_pool``. """ out1 = self.conv1(x) out3 = self.conv3(relu.relu(self.proj3(x))) out5 = self.conv5(relu.relu(self.proj5(x))) pool = self.projp(max_pooling_2d.max_pooling_2d(x, 3, stride=1, pad=1)) y = relu.relu(concat.concat((out1, out3, out5, pool), axis=1)) return y
def __call__(self, x): h = self.bn1(self.conv1(x), test=not self.train) h = max_pooling_2d.max_pooling_2d(relu.relu(h), 3, stride=2, use_cudnn=self.use_cudnn) h = self.res2(h, self.train) h = self.res3(h, self.train) h = self.res4(h, self.train) h = self.res5(h, self.train) h = average_pooling_2d.average_pooling_2d(h, 7, stride=1, use_cudnn=self.use_cudnn) h = self.fc(h) return h
def __call__(self, x, test=None): """Computes the output of the InceptionBN module. Args: x (Variable): An input variable. test (bool): If ``True``, batch normalization layers run in testing mode; if ``test`` is omitted, ``not self.train`` is used as ``test``. """ if test is None: test = not self.train outs = [] if self.out1 > 0: h1 = self.conv1(x) h1 = self.conv1n(h1, test=test) h1 = relu.relu(h1) outs.append(h1) h3 = relu.relu(self.proj3n(self.proj3(x), test=test)) h3 = relu.relu(self.conv3n(self.conv3(h3), test=test)) outs.append(h3) h33 = relu.relu(self.proj33n(self.proj33(x), test=test)) h33 = relu.relu(self.conv33an(self.conv33a(h33), test=test)) h33 = relu.relu(self.conv33bn(self.conv33b(h33), test=test)) outs.append(h33) if self.pooltype == 'max': p = max_pooling_2d.max_pooling_2d(x, 3, stride=self.stride, pad=1, cover_all=False) else: p = average_pooling_2d.average_pooling_2d(x, 3, stride=self.stride, pad=1) if self.proj_pool is not None: p = relu.relu(self.poolpn(self.poolp(p), test=test)) outs.append(p) y = concat.concat(outs, axis=1) return y
def __init__(self, pretrained_model, n_layers): super(ResNetLayers, self).__init__() if pretrained_model: # As a sampling process is time-consuming, # we employ a zero initializer for faster computation. kwargs = {'initialW': constant.Zero()} else: # employ default initializers used in the original paper kwargs = {'initialW': normal.HeNormal(scale=1.0)} if n_layers == 50: block = [3, 4, 6, 3] elif n_layers == 101: block = [3, 4, 23, 3] elif n_layers == 152: block = [3, 8, 36, 3] else: raise ValueError('The n_layers argument should be either 50, 101,' ' or 152, but {} was given.'.format(n_layers)) with self.init_scope(): self.conv1 = Convolution2D(3, 64, 7, 2, 3, **kwargs) self.bn1 = BatchNormalization(64) self.res2 = BuildingBlock(block[0], 64, 64, 256, 1, **kwargs) self.res3 = BuildingBlock(block[1], 256, 128, 512, 2, **kwargs) self.res4 = BuildingBlock(block[2], 512, 256, 1024, 2, **kwargs) self.res5 = BuildingBlock(block[3], 1024, 512, 2048, 2, **kwargs) self.fc6 = Linear(2048, 1000) if pretrained_model and pretrained_model.endswith('.caffemodel'): _retrieve(n_layers, 'ResNet-{}-model.npz'.format(n_layers), pretrained_model, self) elif pretrained_model: npz.load_npz(pretrained_model, self) self.functions = collections.OrderedDict([ ('conv1', [self.conv1, self.bn1, relu]), ('pool1', [lambda x: max_pooling_2d(x, ksize=3, stride=2)]), ('res2', [self.res2]), ('res3', [self.res3]), ('res4', [self.res4]), ('res5', [self.res5]), ('pool5', [_global_average_pooling_2d]), ('fc6', [self.fc6]), ('prob', [softmax]), ])
def __init__(self, pretrained_model, n_layers): if pretrained_model: # As a sampling process is time-consuming, # we employ a zero initializer for faster computation. kwargs = {'initialW': constant.Zero()} else: # employ default initializers used in the original paper kwargs = {'initialW': normal.HeNormal(scale=1.0)} if n_layers == 50: block = [3, 4, 6, 3] elif n_layers == 101: block = [3, 4, 23, 3] elif n_layers == 152: block = [3, 8, 36, 3] else: raise ValueError('The n_layers argument should be either 50, 101,' ' or 152, but {} was given.'.format(n_layers)) super(ResNetLayers, self).__init__( conv1=Convolution2D(3, 64, 7, 2, 3, **kwargs), bn1=BatchNormalization(64), res2=BuildingBlock(block[0], 64, 64, 256, 1, **kwargs), res3=BuildingBlock(block[1], 256, 128, 512, 2, **kwargs), res4=BuildingBlock(block[2], 512, 256, 1024, 2, **kwargs), res5=BuildingBlock(block[3], 1024, 512, 2048, 2, **kwargs), fc6=Linear(2048, 1000), ) if pretrained_model and pretrained_model.endswith('.caffemodel'): _retrieve(n_layers, 'ResNet-{}-model.npz'.format(n_layers), pretrained_model, self) elif pretrained_model: npz.load_npz(pretrained_model, self) self.functions = collections.OrderedDict([ ('conv1', [self.conv1, self.bn1, relu]), ('pool1', [lambda x: max_pooling_2d(x, ksize=3, stride=2)]), ('res2', [self.res2]), ('res3', [self.res3]), ('res4', [self.res4]), ('res5', [self.res5]), ('pool5', [_global_average_pooling_2d]), ('fc6', [self.fc6]), ('prob', [softmax]), ])
def __call__(self, x): """Computes the output of the Inception module. Args: x (~chainer.Variable): Input variable. Returns: Variable: Output variable. Its array has the same spatial size and the same minibatch size as the input array. The channel dimension has size ``out1 + out3 + out5 + proj_pool``. """ out1 = self.conv1(x) out3 = self.conv3(relu.relu(self.proj3(x))) out5 = self.conv5(relu.relu(self.proj5(x))) pool = self.projp(max_pooling_2d.max_pooling_2d( x, 3, stride=1, pad=1)) y = relu.relu(concat.concat((out1, out3, out5, pool), axis=1)) return y
def _max_pooling_2d(x): return max_pooling_2d(x, ksize=2)