Esempio n. 1
0
File: net.py Progetto: xiaoxTM/sigma
def build(input_shape, nclass):
    inputs = layers.base.input_spec(input_shape, dtype='float32', name='input-data')
    with sigma.defaults(act='relu'):
        x = layers.convs.dense(inputs, 8)
        x = layers.convs.dense(x, 8)
        x = layers.convs.dense(x, nclass, act=None)
    return inputs, x
Esempio n. 2
0
def vgg16(input_shape, nclass=1000, classification=False,
          reuse=False, scope='vgg16'):
    with sigma.defaults(layers.convs.conv2d, stride=2, padding='same', act='relu'):
        x = layers.base.input_spec(input_shape, reuse=reuse, scope=scope)
        
        x = layers.convs.conv2d(x, 64, name='block1_conv1')
        x = layers.convs.conv2d(x, 64, name='block1_conv2')
        x = layers.pools.max_pool2d(x, 2, name='block1_pool')
        
        x = layers.convs.conv2d(x, 128, name='block2_conv1')
        x = layers.convs.conv2d(x, 128, name='block2_conv2')
        x = layers.pools.max_pool2d(x, 2, name='block2_pool')
        
        x = layers.convs.conv2d(x, 256, name='block3_conv1')
        x = layers.convs.conv2d(x, 256, name='block3_conv2')
        x = layers.convs.conv2d(x, 256, name='block3_conv3')
        x = layers.pools.max_pool2d(x, 2, name='block3_pool')
        
        x = layers.convs.conv2d(x, 512, name='block4_conv1')
        x = layers.convs.conv2d(x, 512, name='block4_conv2')
        x = layers.convs.conv2d(x, 512, name='block4_conv3')
        x = layers.pools.max_pool2d(x, 2, name='block4_pool')

        x = layers.convs.conv2d(x, 512, name='block5_conv1')
        x = layers.convs.conv2d(x, 512, name='block5_conv2')
        x = layers.convs.conv2d(x, 512, name='block5_conv3')
        x = layers.pools.max_pool2d(x, 2, name='block5_pool')
        
        if classification:
            x = layers.base.flatten(x, name='flatten')
            x = layers.convs.dense(x, 4096, name='fc1')
            x = layers.convs.dense(x, 4096, name='fc2')
            x = layers.convs.dense(x, nclass, name='predictions')

    return x
Esempio n. 3
0
File: net.py Progetto: xiaoxTM/sigma
def _generator(inputs, embeddings, ids, embed_size, batch_size,
               reuse=False,
               scope=None):
    with sigma.scope('generator'):
        with sigma.defaults(reuse=reuse, scope=scope):
            enc_layers = _encoder(inputs)
            local_embeddings = layers.convs.embeddings(embeddings, ids=ids)
            local_embeddings = layers.base.reshape(local_embeddings, [batch_size, 1, 1, embed_size])
            embedded = layers.merge.concat([enc_layers['e8'], local_embeddings])
            dec = _decoder(embedded, 32, enc_layers, ids, reuse=reuse, scope=scope)
    return enc_layers, dec
Esempio n. 4
0
 def _generator(shape, reuse=False):
     with sigma.defaults(weight_initializer=winit, padding=padding,
                         act=act, reuse=reuse, scope=scope):
         inputs = tf.random_normal(shape, dtype=tf.float32, name='random-noise')
         x = layers.convs.dense(inputs, 7*7*128, act=None, name='dense')
         x = layers.norm.batch_norm(x, act=act, name='batchnorm-1')
         x = layers.base.reshape(x, (-1, 7, 7, 128))
         x = layers.convs.deconv2d(x, None, 64, 5, 2, act=None, name ='conv2d-1')
         x = layers.norm.batch_norm(x, name='batchnorm-2')
         x = layers.convs.deconv2d(x, None, 1, 3, 2, act='sigmoid', name='conv2d-2')
     return x
Esempio n. 5
0
 def _discriminator(x, reuse=False):
     with sigma.defaults(weight_initializer=winit, padding=padding,
                         act=act, reuse=reuse, scope=scope):
         x = layers.convs.conv2d(x, 64, 5, 2, act=None, name='conv2d-1')
         x = layers.norm.batch_norm(x, name='batchnorm-1')
         x = layers.convs.conv2d(x, 128, 5, 2, act=None, name='conv2d-2')
         x = layers.norm.batch_norm(x, name='batchnorm-2')
         x = layers.base.flatten(x, name='flatten')
         x = layers.convs.dense(x, 1024, act=None, name='dense')
         x = layers.norm.batch_norm(x, name='batchnorm-3')
         x = layers.convs.dense(x, 1, act=None, name='regression')
         x = layers.norm.batch_norm(x, name='batchnorm-4')
     return x
Esempio n. 6
0
    def _generator(shape=None, noise=None, reuse=False):
        with sigma.defaults(weight_initializer=winit,
                            padding=padding,
                            act=act,
                            reuse=reuse,
                            scope=scope):
            # labels:
            #     batch-size x 1
            # where 1 indicates the identification of class
            # random generate noise with shape of
            #     batch-size x ninput
            if noise is None:
                if shape is not None:
                    inputs = tf.random_uniform(shape,
                                               0,
                                               1,
                                               dtype=tf.float32,
                                               name='random-noise')
                else:
                    raise ValueError('cannot feed generator with'
                                     'none shape and none noise')
            else:
                inputs = noise

            # dense(inputs, output_channels)
            # batch-size x (ninput+1) => 1024
            x = layers.convs.dense(inputs, 1024, name='dense-1')
            # x = layers.norm.batch_norm(x, reuse=reuse, name='batchnorm-1', scope=scope)
            # batch-size x 1024 => batch-size x (7*7*128)
            x = layers.convs.dense(x, 7 * 7 * 128, name='dense-2')
            # batch-size x (7 * 7 * 128) => batch-szie x 7 x 7 x 128
            x = layers.base.reshape(x, (-1, 7, 7, 128), name='reshape')

            # deconv2d(inputs, output_shape, output_channels, kernel_size, stride)
            #    if output_shape = None, output shape will be determined by
            #       input shape and strides
            # batch-size x 7 x 7 x 128 => batch-size x 14 x 14 x 64
            x = layers.convs.deconv2d(x, None, 64, 5, 2, name='conv2d-1')
            # batch-size x 14 x 14 x 128 => batch-size x 28 x 28 x 1
            x = layers.convs.deconv2d(x,
                                      None,
                                      1,
                                      3,
                                      2,
                                      act='sigmoid',
                                      name='conv2d-2')
        return x
Esempio n. 7
0
def resnet50(input_shape,
             nclass=1000,
             classification=False,
             reuse=False,
             scope='resnet50'):
    with sigma.defaults(reuse=reuse, scope=scope):
        x = layers.base.input_spec(input_shape)

        x = layers.convs.conv2d(x,
                                64,
                                kshape=7,
                                stride=2,
                                padding='same',
                                name='conv1')
        x = layers.norms.batch_norm(x, act='relu', name='bn_conv1')
        x = layers.pools.max_pool2d(x, pshape=3, stride=2)

        x = _conv_block(x, 3, [64, 64, 256], stage=2, block='a', stride=1)
        x = _identity_block(x, 3, [64, 64, 256], stage=2, block='b')
        x = _identity_block(x, 3, [64, 64, 256], stage=2, block='c')

        x = _conv_block(x, 3, [128, 128, 512], stage=3, block='a')
        x = _identity_block(x, 3, [128, 128, 512], stage=3, block='b')
        x = _identity_block(x, 3, [128, 128, 512], stage=3, block='c')
        x = _identity_block(x, 3, [128, 128, 512], stage=3, block='d')

        x = _conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
        x = _identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
        x = _identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
        x = _identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
        x = _identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
        x = _identity_block(x, 3, [256, 256, 1024], stage=4, block='f')

        x = _conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
        x = _identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
        x = _identity_block(x, 3, [512, 512, 2048], stage=5, block='c')

        x = layers.pools.avg_pool2d(x, 7, name='avg_pool')

        if classification:
            x = layers.base.flatten(x)
            x = layers.convs.dense(x, nclass, act='softmax', name='fc1000')
    return x
Esempio n. 8
0
    def _generator(x, reuse=False):
        with sigma.defaults(weight_initializer=winit, reuse=reuse,
                            act=act, scope=scope, padding=padding):
            x = layers.convs.dense(x, 1024, act=None, name='dense-1')
            x = layers.norm.batch_norm(x, name='batchnorm-1')
            # batch-size x 1024 => batch-size x (7*7*128)
            x = layers.convs.dense(x, 7*7*128, act=None, name='dense-2')
            x = layers.norm.batch_norm(x, name='batchnorm-2')
            # batch-size x (7 * 7 * 128) => batch-szie x 7 x 7 x 128
            x = layers.base.reshape(x, (-1, 7, 7, 128), name='reshape')

            # deconv2d(inputs, output_shape, output_channels, kernel_size, stride)
            #    if output_shape = None, output shape will be determined by
            #       input shape and strides
            # batch-size x 7 x 7 x 128 => batch-size x 14 x 14 x 64
            x = layers.convs.deconv2d(x, None, 64, 5, 2, name ='conv2d-1')
            x = layers.norm.batch_norm(x, name='batchnorm-3')
            # batch-size x 14 x 14 x 128 => batch-size x 28 x 28 x 1
            x = layers.convs.deconv2d(x, None, 1, 3, 2, act='sigmoid', name='conv2d-2')
        return x
Esempio n. 9
0
File: net.py Progetto: xiaoxTM/sigma
def _discriminator(inputs, reuse=False, scope=None):
    with sigma.scope('discriminator'):
        with sigma.defaults(reuse=reuse, scope=scope):
            x = layers.convs.conv2d(inputs, act='leaky_relu')

            x = layers.convs.conv2d(x)
            x = layers.norms.batch_norm(x, act='leaky_relu')

            x = layers.convs.conv2d(x)
            x = layers.norms.batch_norm(x, act='leaky_relu')

            x = layers.convs.conv2d(x)
            x = layers.norms.batch_norm(x, act='leaky_relu')

            fc1 = layers.base.flatten(x)
            fc1 = layers.convs.dense(fc1)

            fc2 = layers.base.flatten(x)
            fc2 = layers.convs.dense(fc2)

            return layers.actives.sigmoid(fc1), fc1, fc2
Esempio n. 10
0
    def _discriminator(x, reuse=False):
        with sigma.defaults(weight_initializer=winit,
                            padding=padding,
                            act=act,
                            reuse=reuse,
                            scope=scope):
            # 28 x 28 x 1 => 28 x 28 x 6
            # conv2d(inputs, output_channels, kernel_size, strides)
            #    kernel_size : int | list / tuple
            #                  if int, will expend to list / tuple

            # batch-size x 28 x 28 x 1 => batch-size x 14 x 14  x 64
            x = layers.convs.conv2d(x, 64, 5, 2, act=None, name='conv2d-1')
            # batch-size x 14 x 14 x 64 => batch-size x 7 x 7 x 128
            x = layers.convs.conv2d(x, 128, 5, 2, name='conv2d-2')
            # batch-size x 7 x 7 x 128 => batch-size x 6272
            x = layers.base.flatten(x, name='flatten')
            # 6272 => 1024
            x = layers.convs.dense(x, 1024, name='dense')

            # batch-size x 1024 => batch-size x 1 for regression
            x = layers.convs.dense(x, 1, name='regression')
        return x
Esempio n. 11
0
 def _discriminator(x, reuse=False):
     with sigma.defaults(padding=padding, weight_initializer=winit, act=act
                         reuse=reuse, scope=scope):
Esempio n. 12
0
def build_model(inputs,
                build_fun,
                labels=None,
                collections=None,
                summary=None,
                reuse=False,
                scope=None,
                **kwargs):
    """ build network architecture
        Attributes
        ==========
        inputs : tensor
                 input for network entrance
        build_fun : callable
                    callable function receives only one
                    parameter. should have signature of:
                    `def build_fun(x) --> (tensor, tensor):`
                    where the first tensor is loss and the
                    second tensor is metric (can be None)
        labels : tensor
                 label shape for network entrance
        reuse : bool
        scope : string
        kwargs : None or dict
                 parameters passed to build_fun
                 e.g.,
                     loss='margin_loss',
                     metric='accuracy',
                     fastmode=True,
                     ...
                     etc.
        Returns
        ==========
            ([inputs, labels], [loss, metric])
            inputs : tensor
                     input tensor to be feed by samples
            labels : tensor
                     placeholder for ground truth
            loss   : tensor
                     loss to be optimized
            metric : tensor
                     metric to measure the performance
    """
    with sigma.defaults(collections=collections,
                        summary=summary,
                        reuse=reuse,
                        scope=scope):
        x = build_fun(inputs, labels, **kwargs)
    if ops.helper.is_tensor(x):
        loss, metric = x, None
    elif isinstance(x, (list, tuple)):
        if len(x) == 1:
            loss, metric = x, None
        elif len(x) == 2:
            loss, metric = x
        else:
            raise ValueError(
                'The return value of `build_fun` must have'
                ' length of 1 or 2 in list / tuple. given {}'.format(len(x)))
    elif isinstance(x, dict):
        loss = x['loss']
        metric = x.get('metric', None)
    else:
        raise TypeError('The return value type of `build_fun` must be'
                        ' tensor / list / tuple / dict. given {}'.format(
                            type(x)))
    return [loss, metric]