dengdan / seglink

An Implementation of the seglink alogrithm in paper Detecting Oriented Text in Natural Images by Linking Segments
GNU General Public License v3.0
495 stars 178 forks source link

Errors while changing the basenet #19

Closed abc8350712 closed 7 years ago

abc8350712 commented 7 years ago

When I try to change the VGG net to Resnet,it doesn't work.

I mainly change the vgg.py file like

def basenet(inputs):
    logit, endpoints =resnet_50(inputs)
    endpoints['conv4_3'] = endpoints['vgg/resnet_50/block2/unit_2']
    endpoints['fc7'] = endpoints['vgg/resnet_50/block3/unit_4']
    return endpoints['fc7'], endpoints

#try to keep the output as original net

However it dosen't work but come out:

Traceback (most recent call last): File "/home/moon/seglink-master/train_seglink.py", line 276, in tf.app.run() File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 48, in run _sys.exit(main(_sys.argv[:1] + flags_passthrough)) File "/home/moon/seglink-master/train_seglink.py", line 271, in main train_op = create_clones(batch_queue) File "/home/moon/seglink-master/train_seglink.py", line 220, in create_clones averaged_gradients = sum_gradients(gradients) File "/home/moon/seglink-master/train_seglink.py", line 164, in sum_gradients grad = tf.add_n(grads, name = v.op.name + '_summed_gradients') File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 1918, in add_n raise ValueError("inputs must be a list of at least one Tensor with the " ValueError: inputs must be a list of at least one Tensor with the same dtype and shape

My coarse renset implemention is as follow:

import tensorflow as tf
import collections

slim = tf.contrib.slim

Block = collections.namedtuple('Block', ['scope', 'unit_fn', 'args'])

def subsample(inputs, factor, scope=None):
    if factor == 1:
        return inputs
    else:
        return slim.max_pool2d(inputs, [1, 1], stride= factor, scope=scope)

def bottleneck(inputs,
               depth,
               depth_bottleneck,
               stride,
               outputs_collections='collections',
               scope=None):

    with tf.variable_scope(scope) as sc:
        depth_in = slim.utils.last_dimension(inputs.get_shape(), min_rank=4)
        preact = slim.batch_norm(inputs, activation_fn=tf.nn.relu, scope='preact')
        if depth==depth_in:
            shortcut = subsample(inputs, stride, 'shortcut')
        else:

            shortcut = slim.conv2d(preact, depth, [1, 1],
                                   stride=stride, normalizer_fn=None,
                                   activation_fn=None, scope='shortcut')
        residual = slim.conv2d(preact, depth_bottleneck, [1, 1], stride=1, scope='conv1')

        residual = slim.conv2d(residual, depth_bottleneck, [3, 3], stride=stride, padding='SAME', scope='conv2')

        residual = slim.conv2d(residual, depth, [1, 1], stride=1, scope='conv3')

        output = shortcut+residual

        return slim.utils.collect_named_outputs(outputs_collections, sc.name, output)

def resnet_50(input):
    blocks = [
        Block('block1', bottleneck, [(256, 64, 1)] * 2 + [(256, 64, 2)]),
        Block(
            'block2', bottleneck, [(512, 128, 1)] * 3 + [(512, 128, 2)]),
        Block(
            'block3', bottleneck, [(1024, 256, 1)] * 5 + [(1024, 256, 2)]),
        Block(
            'block4', bottleneck, [(2048, 512, 1)] * 3)]
    net = input
    net = slim.conv2d(net, 64, 7, stride=2, scope='conv1', padding='SAME')
    net = slim.max_pool2d(net, [3, 3], stride=2, scope='pool1')
    with tf.variable_scope('resnet_50'):
        for i, block in enumerate(blocks):
            with tf.variable_scope(block.scope):
                args = block.args
                for j, arg in enumerate(args):
                    depth, depth_bottleneck, stride = arg
                    net = bottleneck(net, depth, depth_bottleneck, stride, scope='unit_'+str(j))
    endpoints = slim.utils.convert_collection_to_dict('collections')
    return net, endpoints

Can you help me figure it out? Is there any example for changing basenet? Thank you! @dengdan

dengdan commented 7 years ago

The error implies that you have a variable defined in your model but without being used when running the train_op. Try:

def basenet(inputs):
    logit, endpoints =resnet_50(inputs)
    endpoints['conv4_3'] = endpoints['vgg/resnet_50/block2/unit_2']
    endpoints['fc7'] = logit # Maybe relu should be added on the logit: relu(logit)
    return endpoints['fc7'], endpoints
abc8350712 commented 7 years ago

@dengdan Amazing!It can work!Thank you very much!!