Exemplo n.º 1
0
def inceptionv4(options,params,rng, input):
    results = OrderedDict()
    # inception v4 lower part
    params['W1_1'], results['layerR0'] = convolu_theano(rng, input,filter_shape=(32,3,3,3),image_shape=options['image_shape'],stride=(1,1),poolsize=(2,2), weights_path=None)
    params['W1_2'], results['layerR1'] = convolu_theano(rng, ReLU(results['layerR1']),filter_shape=(32,3,3,3),image_shape=layerR0.shape,stride=(0,0),poolsize=(2,2), weights_path=None)
    params['W1_3'], results['layerR2'] = convolu_theano(rng, ReLU(results['layerR2']),filter_shape=(64,3,3,3),image_shape=layerR1.shape,stride=(0,0),poolsize=(2,2), weights_path=None)
    params['W1_4'], results['layerR3'] = convolu_theano(rng, ReLU(results['layerR3']),filter_shape=(96,3,3,3),image_shape=layerR2.shape,stride=(1,1),poolsize=(2,2), weights_path=None)
    results['layerL1'] = fancy_max_pool(ReLU(results['layerR2']), pool_shape=(3,3), pool_stride=(1,1), ignore_border=False)
    # inception v4 lower part concatenation
    results['filter_concate_1'] = theano.concatenate([results['layerL1'], results['layerR3']], axis=3)

    # inception v4 middle part
    params['W2_1'], results['layerR4'] = convolu_theano(rng, results['filter_concate_1'], filter_shape=(64,3,1,1),image_shape=filter_concate_1.shape,stride=(0,0),poolsize=(2,2), weights_path=None)
    params['W2_2'], results['layerL2'] = convolu_theano(rng, results['filter_concate_1'], filter_shape=(64,3,1,1),image_shape=filter_concate_1.shape,stride=(0,0),poolsize=(2,2), weights_path=None)
    params['W2_3'], results['layerL3'] = convolu_theano(rng, ReLU(results['layerL2']), filter_shape=(96,3,1,1),image_shape=layerL2.shape,stride=(0,0),poolsize=(2,2), weights_path=None)
    params['W2_4'], results['layerR5'] = convolu_theano(rng, ReLU(results['layerR4']), filter_shape=(64,3,7,1),image_shape=layerR4.shape,stride=(0,0),poolsize=(2,2), weights_path=None)
    params['W2_5'], results['layerR6'] = convolu_theano(rng, ReLU(results['layerR5']), filter_shape=(64,3,1,7),image_shape=layerR5.shape,stride=(0,0),poolsize=(2,2), weights_path=None)
    params['W2_6'], results['layerR7'] = convolu_theano(rng, ReLU(results['layerR7']), filter_shape=(96,3,3,3),image_shape=layerR6.shape,stride=(0,0),poolsize=(2,2), weights_path=None)
    #inception v4 middle part concatenation
    results['filter_concate_2'] = theano.concatenate([results['layerL3'],results['layerR7'] ], axis=3)

    #inception v4 upper part
    results['layerR8'] = fancy_max_pool(results['filter_concate_2'], pool_shape=(2,2), pool_stride=(1,1), ignore_border=False)
    params['W3_1'], results['layerL4'] = convolu_theano(rng, results['filter_concate_2'] , filter_shape=(192,3,3,3),image_shape=filter_concate_2.shape, stride=(0,0), poolsize=(2,2), weights_path=None)
    #inception v4 upper part concatenation
    results['filter_concate_3'] = theano.concatenate([ReLU(results['layerL4']),results['layerR8']], axis=3)

    return params, results
Exemplo n.º 2
0
    def __init__(self, dim, use_bias):
        super(ResnetBlock, self).__init__()
        conv_block = []

        conv_block += [
            ReflectionPad2D(1),
            Conv2D(dim,
                   num_filters=dim,
                   filter_size=3,
                   stride=1,
                   bias_attr=use_bias),
            Instancenorm(),
            ReLU()
        ]

        conv_block += [
            ReflectionPad2D(1),
            Conv2D(dim,
                   num_filters=dim,
                   filter_size=3,
                   stride=1,
                   bias_attr=use_bias),
            Instancenorm()
        ]

        self.conv_block = Sequential(*conv_block)
Exemplo n.º 3
0
def cnn_layer(tparams, proj, options):
    #proj = proj.dimshuffle(1,'x',0,2)  #(batchsize,1,max_len,dim_proj)
    proj = proj.dimshuffle(
        1, 3, 0, 2
    )  # (maxlen,n_sample(batchsize), dim_proj, num_chn) -> (batchsize,num_chn,max_len,dim_proj)

    #image_shape = proj.shape
    filter_shapes = options['filter_shapes']
    image_shape = options['image_shape']
    pool_sizes = options['pool_sizes']

    image_shape = (None, image_shape[1], image_shape[2], image_shape[3])

    conv_outs = []
    for i in range(len(filter_shapes)):
        filter_shape = filter_shapes[i]
        pool_size = pool_sizes[i]

        #img_h = image_shape[2]
        filter_h = filter_shape[2]
        #img_w = image_shape[3]
        #filter_w = filter_shape[3]
        #poolsize = (img_h-filter_h+1,img_w-filter_w+1)

        conv_out = conv.conv2d(input=proj,
                               filters=tparams['cnn_f' + str(filter_h)],
                               filter_shape=filter_shape,
                               image_shape=image_shape)
        conv_out_relu = ReLU(
            conv_out +
            tparams['cnn_b' + str(filter_h)].dimshuffle('x', 0, 'x', 'x'))
        if options['pool_type'] == 'max':
            conv_out_pool = pool.pool_2d(input=conv_out_relu,
                                         ds=pool_size,
                                         ignore_border=True,
                                         mode='max')
            # conv_out_pool = pool.pool_2d(input=conv_out_relu,ds=pool_size,ignore_border=True,mode='max')
        elif options['pool_type'] == 'avg':
            conv_out_pool = conv_out_relu.flatten(3)
            conv_out_pool = tensor.mean(conv_out_pool, axis=2)
        else:
            sys.exit()
        conv_outs.append(conv_out_pool.flatten(2))
    proj = tensor.concatenate(conv_outs, 1)

    return proj
Exemplo n.º 4
0
    def __init__(self, input_size, hidden_size, output_size, weight_init=0.01):
        self.params = {}
        # self.params["W1"] = weight_init*np.random.rand(input_size,hidden_size)
        self.params["W1"] = np.random.randn(
            input_size, hidden_size) / np.sqrt(input_size) * np.sqrt(2.0)
        self.params["b1"] = np.zeros(hidden_size)
        # self.params["W2"] = weight_init*np.random.rand(hidden_size,output_size)
        self.params["W2"] = np.random.randn(
            hidden_size, output_size) / np.sqrt(hidden_size) * np.sqrt(2.0)
        self.params["b2"] = np.zeros(output_size)

        self.layers = OrderedDict()
        self.layers["Affine1"] = Affine(self.params["W1"], self.params["b1"])
        self.layers["ReLU"] = ReLU()
        self.layers["Affine2"] = Affine(self.params["W2"], self.params["b2"])

        self.last_layer = IdentityWithLoss()
Exemplo n.º 5
0
    def __init__(self, dim, use_bias):
        super(ResnetAdaILNBlock, self).__init__()
        self.pad1 = ReflectionPad2D(1)
        self.conv1 = Conv2D(dim,
                            num_filters=dim,
                            filter_size=3,
                            stride=1,
                            bias_attr=use_bias)
        self.norm1 = adaILN(dim)
        self.relu1 = ReLU()

        self.pad2 = ReflectionPad2D(1)
        self.conv2 = Conv2D(dim,
                            num_filters=dim,
                            filter_size=3,
                            stride=1,
                            bias_attr=use_bias)
        self.norm2 = adaILN(dim)
Exemplo n.º 6
0
    def __init__(self, dim, use_bias):
        super(ResnetBlock, self).__init__()

        self.conv_block1_1 = ReflectionPad2D(1)
        self.conv_block1_2 = Conv2D(dim,
                                    dim,
                                    filter_size=3,
                                    stride=1,
                                    padding=0,
                                    bias_attr=use_bias)
        self.conv_block1_4 = ReLU(False)

        self.conv_block2_1 = ReflectionPad2D(1)
        self.conv_block2_2 = Conv2D(dim,
                                    dim,
                                    filter_size=3,
                                    stride=1,
                                    padding=0,
                                    bias_attr=use_bias)
Exemplo n.º 7
0
    def __init__(self,
                 input_nc,
                 output_nc,
                 ngf=64,
                 n_blocks=6,
                 img_size=256,
                 light=False):
        assert (n_blocks >= 0)
        super(ResnetGenerator, self).__init__()
        self.input_nc = input_nc
        self.output_nc = output_nc
        self.ngf = ngf
        self.n_blocks = n_blocks
        self.img_size = img_size
        self.light = light

        self.DownBlock1_1 = ReflectionPad2D(3)
        self.DownBlock1_2 = Conv2D(3,
                                   64,
                                   filter_size=7,
                                   stride=1,
                                   padding=0,
                                   bias_attr=False)
        self.DownBlock1_4 = ReLU(False)

        self.DownBlock2_1 = ReflectionPad2D(1)
        self.DownBlock2_2 = Conv2D(64,
                                   128,
                                   filter_size=3,
                                   stride=2,
                                   padding=0,
                                   bias_attr=False)
        self.DownBlock2_4 = ReLU(False)

        self.DownBlock3_1 = ReflectionPad2D(1)
        self.DownBlock3_2 = Conv2D(128,
                                   256,
                                   filter_size=3,
                                   stride=2,
                                   padding=0,
                                   bias_attr=False)
        self.DownBlock3_4 = ReLU(False)
        n_downsampling = 2
        # Down-Sampling
        self.DownBlock1 = ResnetBlock(256, use_bias=False)
        self.DownBlock2 = ResnetBlock(256, use_bias=False)
        self.DownBlock3 = ResnetBlock(256, use_bias=False)
        self.DownBlock4 = ResnetBlock(256, use_bias=False)
        # Down-Sampling Bottleneck
        mult = 4
        # Class Activation Map
        self.gap_fc = Linear(ngf * mult, 1, bias_attr=False)
        self.gmp_fc = Linear(ngf * mult, 1, bias_attr=False)

        self.conv1x1 = Conv2D(
            ngf * mult * 2,
            ngf * mult,
            filter_size=1,
            stride=1,
            bias_attr=fluid.ParamAttr(initializer=fluid.initializer.Uniform(
                low=-1 / math.sqrt(ngf * mult * 2),
                high=1 / math.sqrt(ngf * mult * 2))))
        self.relu = ReLU(False)

        # Gamma, Beta block
        if self.light:
            FC = [
                Linear(ngf * mult, ngf * mult, bias_attr=False),
                ReLU(False),
                Linear(ngf * mult, ngf * mult, bias_attr=False),
                ReLU(False)
            ]
        else:
            FC = [
                Linear(img_size // mult * img_size // mult * ngf * mult,
                       ngf * mult,
                       bias_attr=False),
                ReLU(False),
                Linear(ngf * mult, ngf * mult, bias_attr=False),
                ReLU(False)
            ]
        self.gamma = Linear(ngf * mult, ngf * mult, bias_attr=False)
        self.beta = Linear(ngf * mult, ngf * mult, bias_attr=False)

        # Up-Sampling Bottleneck
        for i in range(n_blocks):
            setattr(self, 'UpBlock1_' + str(i + 1),
                    ResnetAdaILNBlock(ngf * mult, use_bias=False))

        # Up-Sampling
        UpBlock2 = []
        for i in range(n_downsampling):
            mult = 2**(n_downsampling - i)
            UpBlock2 += [
                Upsample(scales=2, resamples='NEAREST'),
                ReflectionPad2D(1),
                Conv2D(ngf * mult,
                       int(ngf * mult / 2),
                       filter_size=3,
                       stride=1,
                       padding=0,
                       bias_attr=False),
                ILN(int(ngf * mult / 2)),
                ReLU(False)
            ]

        UpBlock2 += [
            ReflectionPad2D(3),
            Conv2D(ngf,
                   output_nc,
                   filter_size=7,
                   stride=1,
                   padding=0,
                   bias_attr=False),
            Tanh()
        ]

        self.FC = Sequential(*FC)
        self.UpBlock2 = Sequential(*UpBlock2)
Exemplo n.º 8
0
    def __init__(self,
                 input_nc,
                 output_nc,
                 ngf=64,
                 n_blocks=6,
                 img_size=256,
                 light=False):
        assert (n_blocks >= 0)
        super(ResnetGenerator, self).__init__()
        self.input_nc = input_nc
        self.output_nc = output_nc
        self.ngf = ngf
        self.n_blocks = n_blocks
        self.img_size = img_size
        self.light = light

        DownBlock = []
        DownBlock += [
            ReflectionPad2D(3),
            Conv2D(input_nc,
                   num_filters=ngf,
                   filter_size=7,
                   stride=1,
                   padding=0),
            Instancenorm(),
            ReLU()
        ]

        # Down-Sampling
        n_downsampling = 2
        for i in range(n_downsampling):
            mult = 2**i
            DownBlock += [
                ReflectionPad2D(1),
                Conv2D(ngf * mult,
                       num_filters=ngf * mult * 2,
                       filter_size=3,
                       stride=2),
                Instancenorm(),
                ReLU()
            ]

        # Down-Sampling Bottleneck
        mult = 2**n_downsampling
        for i in range(n_blocks):
            DownBlock += [ResnetBlock(ngf * mult, use_bias=False)]

        # Class Activation Map

        self.gap_fc = Linear(ngf * mult, 1)
        self.gmp_fc = Linear(ngf * mult, 1)
        self.conv1x1 = Conv2D(ngf * mult * 2,
                              num_filters=ngf * mult,
                              filter_size=1,
                              stride=1,
                              bias_attr=True)
        self.relu = ReLU()

        # Gamma, Beta block
        if self.light:
            FC = [
                Linear(ngf * mult, ngf * mult),
                ReLU(),
                Linear(ngf * mult, ngf * mult),
                ReLU()
            ]
        else:
            FC = [
                Linear(img_size // mult * img_size // mult * ngf * mult,
                       ngf * mult),
                ReLU(),
                Linear(ngf * mult, ngf * mult),
                ReLU()
            ]

        self.gamma = Linear(ngf * mult, ngf * mult)
        self.beta = Linear(ngf * mult, ngf * mult)

        # Up-Sampling Bottleneck
        for i in range(n_blocks):
            setattr(self, 'UpBlock1_' + str(i + 1),
                    ResnetAdaILNBlock(ngf * mult, use_bias=False))

        # Up-Sampling
        UpBlock2 = []
        for i in range(n_downsampling):
            mult = 2**(n_downsampling - i)
            UpBlock2 += [
                Resize_nearest(scale=2),
                ReflectionPad2D(1),
                Conv2D(ngf * mult,
                       num_filters=int(ngf * mult / 2),
                       filter_size=3,
                       stride=1),
                ILN(int(ngf * mult / 2)),
                ReLU()
            ]

        UpBlock2 += [
            ReflectionPad2D(3),
            Conv2D(ngf, num_filters=output_nc, filter_size=7, stride=1),
            Tanh()
        ]

        self.DownBlock = Sequential(*DownBlock)
        self.FC = Sequential(*FC)
        self.UpBlock2 = Sequential(*UpBlock2)