MarvinTeichmann / tensorflow-fcn

An Implementation of Fully Convolutional Networks in Tensorflow.
MIT License
1.1k stars 433 forks source link

logits.get_shape() cannot get shape when I train the network #5

Closed jiankang1991 closed 8 years ago

jiankang1991 commented 8 years ago

I build up the train graph and want to train the network, but shape = [logits.get_shape()[0], num_classes] cannot get shape of logits in loss.py. The error is :

  File "/home/kang/Documents/work_code_PC1/CamVid_tensorflow_FCN/fcn8_vgg_train.py", line 79, in train_test
    losses = loss.loss(logits,labels,fcn_inputs.NUM_CLASSES)

  File "loss.py", line 42, in loss
    epsilon = tf.constant(value=FLAGS.epsilon, shape=shape)

  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/constant_op.py", line 162, in constant
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))

  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 363, in make_tensor_proto
    shape = [int(dim) for dim in shape]

TypeError: __int__ returned non-int (type NoneType)

my code is as follows:

def train_test():
    with tf.Graph().as_default():
        global_step = tf.Variable(0, trainable=False)
        images, labels = fcn_inputs._input_pipeline(FLAGS.txtfile,batch_size = FLAGS.batch_size,
                                                    image_shape = [360,480,3],
                                                    label_tensor_shape = [360,480,fcn_inputs.NUM_CLASSES])
        vgg_fcn = fcn8_vgg.FCN8VGG()
        vgg_fcn.build(images, train=True, num_classes = fcn_inputs.NUM_CLASSES,
                      random_init_fc8 = True, debug=True)

        logits = vgg_fcn.upscore32
        print(logits.get_shape()) 

        losses = loss.loss(logits,labels,fcn_inputs.NUM_CLASSES)
def loss(logits, labels, num_classes):
    """Calculate the loss from the logits and the labels.

    Args:
      hypes: dict
          hyperparameters of the model
      logits: tensor, float - [batch_size, width, height, num_classes].
          Use vgg_fcn.up as logits.
      labels: Labels tensor, int32 - [batch_size, width, height, num_classes].
          The ground truth of your data.

    Returns:
      loss: Loss tensor of type float.
    """
    with tf.name_scope('loss'):
        logits = tf.reshape(logits, (-1, num_classes))

        shape = [logits.get_shape()[0], num_classes]
        epsilon = tf.constant(value=FLAGS.epsilon, shape=shape)

It confuses me, why cannot tensorflow get the shape of upscore32? Thank you very much.

MarvinTeichmann commented 8 years ago

The shapes of each layer are computed using the shapes of the predecessor layers. Your images have most likely an undefined shape, yielding in an undefined shape of logits.

Use images.set_shape([360,480,3]) to set shape for images. This will give fcn_vgg.upscore32 a defined shape.

However one does not need to set the shape in advance. If it is not set, it is computed on runtime for each image. Commit fda8f40 modifies the loss to deal with abstract logits.shape.

53X commented 6 years ago

@MarvinTeichmann I have a similar problem and I have already identified that it is occurring due to the variable sized images that I should use for my model. When you said that if we don't set any definite shape in advance then it will be automatically computed during runtime then did you mean that that although when my snippet compiles it will give the error as mentioned by @karlTUM , but it will run properly when I actually fit my model with some training data ?