I have a convolutional network which consists of multiple CNN layers (which could work with any size) and a fully connected layer at the end which can only work with fixed input size.

I have trained the network for certain image size and the fully connected layer can only work for those input sizes.

I don't want to retrain the entire network when operating on different input sizes. Is there any way to achieve training of only the newly added fully connected layer.

My code for inference looks like this

def inference(images, reuse=False, trainable=True):
    coarse1_conv = conv2d('coarse1', images, [11, 11, 3, 96], [96], [1, 4, 4, 1], padding='VALID', reuse=reuse, trainable=trainable)
    coarse1 = tf.nn.max_pool(coarse1_conv, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID', name='pool1')
    coarse2_conv = conv2d('coarse2', coarse1, [5, 5, 96, 256], [256], [1, 1, 1, 1], padding='VALID', reuse=reuse, trainable=trainable)
    coarse2 = tf.nn.max_pool(coarse2_conv, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool1')
    coarse3 = conv2d('coarse3', coarse2, [3, 3, 256, 384], [384], [1, 1, 1, 1], padding='VALID', reuse=reuse, trainable=trainable)
    coarse4 = conv2d('coarse4', coarse3, [3, 3, 384, 384], [384], [1, 1, 1, 1], padding='VALID', reuse=reuse, trainable=trainable)
    coarse5 = conv2d('coarse5', coarse4, [3, 3, 384, 256], [256], [1, 1, 1, 1], padding='VALID', reuse=reuse, trainable=trainable)
    coarse6 = fc('coarse6', coarse5, [6*10*256, 4096], [4096], reuse=reuse, trainable=trainable)
    coarse7 = fc('coarse7', coarse6, [4096, 4070], [4070], reuse=reuse, trainable=trainable)
    coarse7_output = tf.reshape(coarse7, [-1, 55, 74, 1])
    return coarse7_output
  • 1
    $\begingroup$ Checkout the architecture of resnets etc.. $\endgroup$
    – Aditya
    Apr 17, 2018 at 18:03
  • $\begingroup$ Will do . ANything other than resnets which is directly adapted to the above code? $\endgroup$
    – Roarer
    Apr 18, 2018 at 4:45

1 Answer 1


Actually, if you change the input the input size, nothing goes wrong with the convolutional layers but the outputs of these layers increases and that will cause to the increased number of inputs to the dense layers 1. Consequently, you will have to have extra weights and you have to train them. That is why it is better to have a fixed size input. But there are other solutions that can be expanded to your task. Take a look at the contents of the third week of this course. As you will see, you have to input patches of the image to the network but what is done there is for detection tasks, you have to expand it to your task, but maybe resizing all inputs to a predefined size is simpler.

  • $\begingroup$ Do you have any suggestions on how to retrain the network if size increases without training all other parts $\endgroup$
    – Roarer
    Apr 18, 2018 at 4:45
  • $\begingroup$ @Roarer if you have fixed size inputs for different images, I mean you have e.g. 255, 512, 1024 that are predefined, you can use transfer learning for fully connected layers and make different models but if the inputs are of the arbitrary size I guess it is better to resize them to a fixed predefined size. the reason is that if you don't know the size, the number of weights would vary, and TF keeps the weights, you can not have arbitrary input size because of FC layers. $\endgroup$ Apr 18, 2018 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.