コード例 #1
0
ファイル: model.py プロジェクト: ishida-titech/GCMQA
def _get_model(layers, comm, predict=False):
    model = Sequential()
    W = chainer.initializers.HeNormal(1 / np.sqrt(1 / 2), dtype=np.float32)
    bias = chainer.initializers.Zero(dtype=np.float32)

    for layer in layers:
        name = layer['name']
        parameter = copy.deepcopy(layer['parameter'])
        if name.split('.')[0] == 'GC':
            parameter.update({'predict': predict})
            if 'batch_norm' in parameter.keys() and parameter['batch_norm']:
                parameter.update({'comm': comm})
            if 'activation' in parameter.keys() and parameter['activation']:
                parameter['activation'] = eval(parameter['activation'])
            add_layer = eval(name)(**parameter)
        elif name.split('.')[0] == 'L':
            if 'Linear' in name.split('.')[1]:
                parameter.update({'initialW': W, 'initial_bias': bias})
            add_layer = eval(name)(**parameter)
        elif name.split('.')[0] == 'F':
            if len(parameter) == 0:
                add_layer = partial(eval(name))
            else:
                add_layer = partial(eval(name), **parameter)
        elif name.split('.')[0] == 'MNL':
            if predict:
                add_layer = L.BatchNormalization(size=parameter['size'])
            else:
                add_layer = MNL.MultiNodeBatchNormalization(
                    size=parameter['size'], comm=comm)
        elif name == 'Flat':
            add_layer = partial(lambda *x: x[0])
        model.append(add_layer)
    return model
コード例 #2
0
ファイル: network.py プロジェクト: curegit/anime-halftone-inv
class Upsampler(Chain):
    def __init__(self, channels, ksize, repeat):
        super().__init__()
        self.channels = channels
        self.ksize = ksize
        self.repeat = repeat
        with self.init_scope():
            self.convs = Sequential(
                Convolution2D(channels * 2,
                              channels * 2,
                              ksize=ksize,
                              stride=1,
                              pad=0), relu).repeat(repeat)
            self.convs.append(
                Convolution2D(channels * 2, channels, ksize=1, stride=1,
                              pad=0))

    def __call__(self, x, y):
        x_height, x_width = x.shape[2:4]
        z_height, z_width = x_height * 2, x_width * 2
        z = resize_images(x, (z_height, z_width), mode="nearest")
        y_height, y_width = y.shape[2:4]
        height, width = min(x_height, y_height), min(x_width, y_height)
        z_width_sub = (z_width - width) // 2
        z_height_sub = (z_height - height) // 2
        y_width_sub = (y_width - width) // 2
        y_height_sub = (y_height - height) // 2
        zs = z[:, :, z_height_sub:-z_height_sub, z_width_sub:-z_width_sub]
        ys = y[:, :, y_height_sub:-y_height_sub, y_width_sub:-y_width_sub]
        return self.convs(concat((zs, ys), axis=1))
コード例 #3
0
def define_upsampling(opt, input_ch, output_ch=None):
    if opt.upsampling_mode == 'bilinear':
        seq = Sequential(lambda x: F.resize_images(
            x, (x.shape[2] * 2, x.shape[3] * 2), mode='bilinear'))

        if output_ch is not None:
            seq.append(
                define_conv(opt)(input_ch,
                                 output_ch,
                                 ksize=3,
                                 stride=1,
                                 pad=1,
                                 initialW=HeNormal()))

        return seq

    if opt.upsampling_mode == 'nearest':
        seq = Sequential(lambda x: F.resize_images(
            x, (x.shape[2] * 2, x.shape[3] * 2), mode='nearest'))

        if output_ch is not None:
            seq.append(
                define_conv(opt)(input_ch,
                                 output_ch,
                                 ksize=3,
                                 stride=1,
                                 pad=1,
                                 initialW=HeNormal()))

        return seq

    if opt.upsampling_mode == 'deconv':
        return define_deconv(opt)(input_ch,
                                  input_ch if output_ch is None else output_ch,
                                  ksize=3,
                                  stride=1,
                                  pad=1,
                                  initialW=HeNormal())

    if opt.upsampling_mode == 'subpx_conv':
        return PixelShuffler(opt, input_ch,
                             input_ch if output_ch is None else output_ch)
コード例 #4
0
    def _make_layer(self, block, planes, blocks, stride, pad, dilation):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = Sequential(
                L.Convolution2D(self.inplanes, planes * block.expansion,
                                ksize=1, stride=stride, nobias=True,
                                initialW=ini.Normal(math.sqrt(2. / (planes * block.expansion)))),
                L.BatchNormalization(planes * block.expansion,
                                     eps=1e-5, decay=0.95,
                                     initial_gamma=ini.One(), initial_beta=ini.Zero()),
            )

        layers = Sequential()
        layers.append(block(self.inplanes, planes,
                            stride, downsample, pad, dilation))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes, 1, None, pad, dilation))

        return layers
コード例 #5
0
ファイル: modules.py プロジェクト: iszhaoxin/KGI
class ShallowConcateS(chainer.Chain):
    def __init__(self, hidden_units):
        print(hidden_units)
        super(ShallowConcateS, self).__init__()
        with self.init_scope():
            self.layers = Sequential()
            for layer_units in hidden_units:
                self.layers.append(L.Linear(None, layer_units))
                self.layers.append(F.relu)
                self.layers.append(L.BatchNormalization(layer_units))
                self.layers.append(F.dropout)
            self.last = L.Linear(None, 2)
        print(self.layers)

    def __call__(self, x):
        y = self.layers(x)
        return self.last(y)