def __init__(self): super().__init__() with self.init_scope(): # bottom up self.resnet = ResNet50Layers('auto') del self.resnet.fc6 # top layer (reduce channel) self.toplayer = L.Convolution2D( in_channels=None, out_channels=256, ksize=1, stride=1, pad=0) # conv layer for top-down pathway self.conv_p4 = L.Convolution2D(None, 256, ksize=3, stride=1, pad=1) self.conv_p3 = L.Convolution2D(None, 256, ksize=3, stride=1, pad=1) self.conv_p2 = L.Convolution2D(None, 256, ksize=3, stride=1, pad=1) self.conv_p6 = L.Convolution2D(None, 256, ksize=1, stride=2, pad=0) # lateral connection self.lat_p4 = L.Convolution2D( in_channels=None, out_channels=256, ksize=1, stride=1, pad=0) self.lat_p3 = L.Convolution2D( in_channels=None, out_channels=256, ksize=1, stride=1, pad=0) self.lat_p2 = L.Convolution2D( in_channels=None, out_channels=256, ksize=1, stride=1, pad=0) # anchor_sizes / anchor_base anchor_base is invisible from lamba function?? self.anchor_scales = list( map(lambda x: x/float(self.anchor_base), self.anchor_sizes))
def create_model(weight='auto', activate=F.sigmoid): resnet = ResNet50Layers(pretrained_model=weight) model = chainer.Sequential(lambda x: resnet(x, layers=['res5'])['res5'], L.Linear(None, 128)) if activate: model.append(activate) return model
def _copy_imagenet_pretrained_resnet(self): if self._n_layers == 50: pretrained_model = ResNet50Layers(pretrained_model='auto') elif self._n_layers == 101: pretrained_model = ResNet101Layers(pretrained_model='auto') else: raise ValueError self.extractor.conv1.copyparams(pretrained_model.conv1) # The pretrained weights are trained to accept BGR images. # Convert weights so that they accept RGB images. self.extractor.conv1.W.data[:] = \ self.extractor.conv1.W.data[:, ::-1] self.extractor.bn1.copyparams(pretrained_model.bn1) _copy_persistent_chain(self.extractor.bn1, pretrained_model.bn1) self.extractor.res2.copyparams(pretrained_model.res2) _copy_persistent_chain(self.extractor.res2, pretrained_model.res2) self.extractor.res3.copyparams(pretrained_model.res3) _copy_persistent_chain(self.extractor.res3, pretrained_model.res3) self.extractor.res4.copyparams(pretrained_model.res4) _copy_persistent_chain(self.extractor.res4, pretrained_model.res4) self.head.res5.copyparams(pretrained_model.res5) _copy_persistent_chain(self.head.res5, pretrained_model.res5)
def __init__(self, initialW=None): super().__init__() with self.init_scope(): # bottom up self.resnet = ResNet50Layers('auto') del self.resnet.fc6 # top layer (reduce channel) self.toplayer = L.Convolution2D(in_channels=None, out_channels=256, ksize=1, stride=1, pad=0, initialW=initialW) # conv layer for top-down pathway self.conv_p4 = L.Convolution2D(None, 256, ksize=3, stride=1, pad=1, initialW=initialW) self.conv_p3 = L.Convolution2D(None, 256, ksize=3, stride=1, pad=1, initialW=initialW) self.conv_p2 = L.Convolution2D(None, 256, ksize=3, stride=1, pad=1, initialW=initialW) self.conv_p7 = L.Convolution2D(None, 256, ksize=3, stride=1, pad=1, initialW=initialW) # lateral connection self.lat_p4 = L.Convolution2D(in_channels=None, out_channels=256, ksize=1, stride=1, pad=0, initialW=initialW) self.lat_p3 = L.Convolution2D(in_channels=None, out_channels=256, ksize=1, stride=1, pad=0, initialW=initialW) self.lat_p2 = L.Convolution2D(in_channels=None, out_channels=256, ksize=1, stride=1, pad=0, initialW=initialW)
def __init__(self, activate, init_scale, dropout_ratio=0.5, weight='auto'): super().__init__() with self.init_scope(): self.resnet = ResNet50Layers(weight) self.fc = L.Linear( None, 128, initialW=chainer.initializers.LeCunNormal(scale=init_scale)) self.activate = activate self.dropout_ratio = dropout_ratio
def main(): parser = argparse.ArgumentParser(description='evaluate imagenet') parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--count-by', choices=(None, 'layers', 'functions'), default=None) parser.add_argument('model', choices=('vgg16', 'googlenet', 'resnet50', 'resnet101', 'resnet152')) args = parser.parse_args() if args.model == 'vgg16': model = VGG16Layers(pretrained_model=None) elif args.model == 'googlenet': model = GoogLeNet(pretrained_model=None) elif args.model == 'resnet50': model = ResNet50Layers(pretrained_model=None) elif args.model == 'resnet101': model = ResNet101Layers(pretrained_model=None) elif args.model == 'resnet152': model = ResNet152Layers(pretrained_model=None) # override batch_normalization, don't override in prodution if 'resnet' in args.model: monkey.override_bn() if args.gpu >= 0: chainer.cuda.get_device(args.gpu).use() model.to_gpu(args.gpu) else: if chainer.config.use_ideep != "never": model.to_intel64() if args.count_by is not None and args.gpu > 0: warnings.warn("count_by with GPU is not supported", ValueError) if args.count_by == 'layers': from perf_counter import Counter monkey.decorate_link(model, Counter) if args.count_by == 'functions': from perf_counter import CounterHook else: from monkey import CounterHook image = np.zeros((1, 3, 224, 224), dtype=np.float32) # dummy image with chainer.using_config('train', False): with chainer.using_config('enable_backprop', False): with CounterHook() as counter: model.predict(image, oversample=False) for fn, float_ops in counter.call_history: print('"{}","{}"'.format(fn, float_ops))
def __init__(self, n_class, roi_size, spatial_scale, loc_initialW=None, score_initialW=None, mask_initialW=None): # n_class includes the background super().__init__() with self.init_scope(): # res5ブロックがほしいだけなのに全部読み込むのは無駄ではある resnet50 = ResNet50Layers() self.res5 = copy.deepcopy(resnet50.res5) # strideは1にする self.res5.a.conv1.stride = (1, 1) self.res5.a.conv4.stride = (1, 1) # 論文 図3の左から2つめ self.conv1 = L.Convolution2D( in_channels=None, out_channels=2048, ksize=3, stride=1, pad=1) # マスク推定ブランチへ self.deconv1 = L.Deconvolution2D( in_channels=None, out_channels=256, ksize=2, stride=2, pad=0, initialW=mask_initialW) self.conv2 = L.Convolution2D( in_channels=None, out_channels=n_class - 1, ksize=3, stride=1, pad=1, initialW=mask_initialW) self.cls_loc = L.Linear(2048, n_class * 4, initialW=loc_initialW) self.score = L.Linear(2048, n_class, initialW=score_initialW) self.n_class = n_class self.roi_size = roi_size self.spatial_scale = spatial_scale
def __init__(self, classlabels=66): super(Encoder, self).__init__( #model = L.ResNet50Layers(), model=ResNet50Layers(), fc=L.Linear(None, classlabels))