def _build_graph(self, inputs):
        image, label = inputs
        blocks = CFG[DEPTH]

        bottleneck = functools.partial(resnet_bottleneck, stride_first=True)

        # tensorflow with padding=SAME will by default pad [2,3] here.
        # but caffe conv with stride will pad [3,3]
        image = tf.pad(image, [[0, 0], [3, 3], [3, 3], [0, 0]])
        image = tf.transpose(image, [0, 3, 1, 2])
        with argscope([Conv2D, MaxPooling, GlobalAvgPooling, BatchNorm],
                      data_format='NCHW'), \
                argscope(Conv2D, nl=tf.identity, use_bias=False,
                         W_init=variance_scaling_initializer(mode='FAN_OUT')):
            logits = (LinearWrap(image).Conv2D(
                'conv0', 64, 7, stride=2, nl=BNReLU,
                padding='VALID').MaxPooling(
                    'pool0', shape=3, stride=2, padding='SAME').apply(
                        resnet_group, 'group0',
                        bottleneck, 64, blocks[0], 1).apply(
                            resnet_group, 'group1',
                            bottleneck, 128, blocks[1], 2).apply(
                                resnet_group, 'group2', bottleneck,
                                256, blocks[2], 2).apply(
                                    resnet_group, 'group3', bottleneck, 512,
                                    blocks[3],
                                    2).GlobalAvgPooling('gap').FullyConnected(
                                        'linear', 1000, nl=tf.identity)())
        prob = tf.nn.softmax(logits, name='prob')
        ImageNetModel.compute_loss_and_error(logits, label)
Example #2
0
    def build_graph(self, image, label):
        blocks = CFG[DEPTH]

        bottleneck = functools.partial(resnet_bottleneck, stride_first=True)

        # tensorflow with padding=SAME will by default pad [2,3] here.
        # but caffe conv with stride will pad [3,2]
        image = tf.pad(image, [[0, 0], [3, 2], [3, 2], [0, 0]])
        image = tf.transpose(image, [0, 3, 1, 2])
        with argscope([Conv2D, MaxPooling, GlobalAvgPooling, BatchNorm],
                      data_format='channels_first'), \
                argscope(Conv2D, use_bias=False):
            logits = (LinearWrap(image).Conv2D(
                'conv0', 64, 7, strides=2, activation=BNReLU,
                padding='VALID').MaxPooling(
                    'pool0', 3, strides=2, padding='SAME').apply(
                        resnet_group, 'group0',
                        bottleneck, 64, blocks[0], 1).apply(
                            resnet_group, 'group1',
                            bottleneck, 128, blocks[1], 2).apply(
                                resnet_group, 'group2', bottleneck,
                                256, blocks[2], 2).apply(
                                    resnet_group, 'group3', bottleneck, 512,
                                    blocks[3],
                                    2).GlobalAvgPooling('gap').FullyConnected(
                                        'linear', 1000)())
        tf.nn.softmax(logits, name='prob')
        ImageNetModel.compute_loss_and_error(logits, label)
Example #3
0
    def build_graph(self, image, label):
        blocks = CFG[DEPTH]

        bottleneck = functools.partial(resnet_bottleneck, stride_first=True)

        # tensorflow with padding=SAME will by default pad [2,3] here.
        # but caffe conv with stride will pad [3,2]
        image = tf.pad(image, [[0, 0], [3, 2], [3, 2], [0, 0]])
        image = tf.transpose(image, [0, 3, 1, 2])
        with argscope([Conv2D, MaxPooling, GlobalAvgPooling, BatchNorm],
                      data_format='channels_first'), \
                argscope(Conv2D, use_bias=False):
            logits = (LinearWrap(image)
                      .Conv2D('conv0', 64, 7, strides=2, activation=BNReLU, padding='VALID')
                      .MaxPooling('pool0', 3, strides=2, padding='SAME')
                      .apply(resnet_group, 'group0', bottleneck, 64, blocks[0], 1)
                      .apply(resnet_group, 'group1', bottleneck, 128, blocks[1], 2)
                      .apply(resnet_group, 'group2', bottleneck, 256, blocks[2], 2)
                      .apply(resnet_group, 'group3', bottleneck, 512, blocks[3], 2)
                      .GlobalAvgPooling('gap')
                      .FullyConnected('linear', 1000)())
        tf.nn.softmax(logits, name='prob')
        ImageNetModel.compute_loss_and_error(logits, label)
Example #4
0
 def image_preprocess(image):
     image = ImageNetModel.image_preprocess(image)
     image = tf.transpose(image, [0, 3, 1, 2])
     return image
Example #5
0
    def build_graph(self, image, label):
        image = image / 256.0

        fw, fa, fg = get_dorefa(BITW, BITA, BITG)

        def new_get_variable(v):
            name = v.op.name
            # don't binarize first and last layer
            if not name.endswith('W') or 'conv1' in name or 'fct' in name:
                return v
            else:
                logger.info("Binarizing weight {}".format(v.op.name))
                return fw(v)

        def nonlin(x):
            return tf.clip_by_value(x, 0.0, 1.0)

        def activate(x):
            return fa(nonlin(x))

        def resblock(x, channel, stride):
            def get_stem_full(x):
                return (LinearWrap(x).Conv2D(
                    'c3x3a', channel,
                    3).BatchNorm('stembn').apply(activate).Conv2D(
                        'c3x3b', channel, 3)())

            channel_mismatch = channel != x.get_shape().as_list()[3]
            if stride != 1 or channel_mismatch or 'pool1' in x.name:
                # handling pool1 is to work around an architecture bug in our model
                if stride != 1 or 'pool1' in x.name:
                    x = AvgPooling('pool', x, stride, stride)
                x = BatchNorm('bn', x)
                x = activate(x)
                shortcut = Conv2D('shortcut', x, channel, 1)
                stem = get_stem_full(x)
            else:
                shortcut = x
                x = BatchNorm('bn', x)
                x = activate(x)
                stem = get_stem_full(x)
            return shortcut + stem

        def group(x, name, channel, nr_block, stride):
            with tf.variable_scope(name + 'blk1'):
                x = resblock(x, channel, stride)
            for i in range(2, nr_block + 1):
                with tf.variable_scope(name + 'blk{}'.format(i)):
                    x = resblock(x, channel, 1)
            return x

        with remap_variables(new_get_variable), \
                argscope(BatchNorm, decay=0.9, epsilon=1e-4), \
                argscope(Conv2D, use_bias=False, nl=tf.identity):
            logits = (
                LinearWrap(image)
                # use explicit padding here, because our private training framework has
                # different padding mechanisms from TensorFlow
                .tf.pad([[0, 0], [3, 2], [3, 2], [0, 0]]).Conv2D(
                    'conv1', 64, 7, stride=2, padding='VALID',
                    use_bias=True).tf.pad(
                        [[0, 0], [1, 1], [1, 1], [0, 0]],
                        'SYMMETRIC').MaxPooling(
                            'pool1',
                            3, 2, padding='VALID').apply(
                                group, 'conv2', 64,
                                2, 1).apply(group, 'conv3', 128, 2, 2).apply(
                                    group, 'conv4', 256, 2,
                                    2).apply(group, 'conv5', 512, 2,
                                             2).BatchNorm('lastbn').
                apply(nonlin).GlobalAvgPooling('gap').tf.multiply(
                    49)  # this is due to a bug in our model design
                .FullyConnected('fct', 1000)())
        tf.nn.softmax(logits, name='output')
        ImageNetModel.compute_loss_and_error(logits, label)
Example #6
0
    def _build_graph(self, inputs):
        image, label = inputs
        image = image / 256.0

        fw, fa, fg = get_dorefa(BITW, BITA, BITG)

        def new_get_variable(v):
            name = v.op.name
            # don't binarize first and last layer
            if not name.endswith('W') or 'conv1' in name or 'fct' in name:
                return v
            else:
                logger.info("Binarizing weight {}".format(v.op.name))
                return fw(v)

        def nonlin(x):
            return tf.clip_by_value(x, 0.0, 1.0)

        def activate(x):
            return fa(nonlin(x))

        def resblock(x, channel, stride):
            def get_stem_full(x):
                return (LinearWrap(x)
                        .Conv2D('c3x3a', channel, 3)
                        .BatchNorm('stembn')
                        .apply(activate)
                        .Conv2D('c3x3b', channel, 3)())
            channel_mismatch = channel != x.get_shape().as_list()[3]
            if stride != 1 or channel_mismatch or 'pool1' in x.name:
                # handling pool1 is to work around an architecture bug in our model
                if stride != 1 or 'pool1' in x.name:
                    x = AvgPooling('pool', x, stride, stride)
                x = BatchNorm('bn', x)
                x = activate(x)
                shortcut = Conv2D('shortcut', x, channel, 1)
                stem = get_stem_full(x)
            else:
                shortcut = x
                x = BatchNorm('bn', x)
                x = activate(x)
                stem = get_stem_full(x)
            return shortcut + stem

        def group(x, name, channel, nr_block, stride):
            with tf.variable_scope(name + 'blk1'):
                x = resblock(x, channel, stride)
            for i in range(2, nr_block + 1):
                with tf.variable_scope(name + 'blk{}'.format(i)):
                    x = resblock(x, channel, 1)
            return x

        with remap_variables(new_get_variable), \
                argscope(BatchNorm, decay=0.9, epsilon=1e-4), \
                argscope(Conv2D, use_bias=False, nl=tf.identity):
            logits = (LinearWrap(image)
                      # use explicit padding here, because our training framework has
                      # different padding mechanisms from TensorFlow
                      .tf.pad([[0, 0], [3, 2], [3, 2], [0, 0]])
                      .Conv2D('conv1', 64, 7, stride=2, padding='VALID', use_bias=True)
                      .tf.pad([[0, 0], [1, 1], [1, 1], [0, 0]], 'SYMMETRIC')
                      .MaxPooling('pool1', 3, 2, padding='VALID')
                      .apply(group, 'conv2', 64, 2, 1)
                      .apply(group, 'conv3', 128, 2, 2)
                      .apply(group, 'conv4', 256, 2, 2)
                      .apply(group, 'conv5', 512, 2, 2)
                      .BatchNorm('lastbn')
                      .apply(nonlin)
                      .GlobalAvgPooling('gap')
                      .tf.multiply(49)  # this is due to a bug in our model design
                      .FullyConnected('fct', 1000)())
        tf.nn.softmax(logits, name='output')
        ImageNetModel.compute_loss_and_error(logits, label)
 def image_preprocess(image):
     image = ImageNetModel.image_preprocess(image)
     image = tf.transpose(image, [0, 3, 1, 2])
     return image