4
votes

I am a Tensorflow beginner and I am trying to learn how to use Tensorflow. I understand that we can build a network model and create a pair of inputs and labels for the network to train, i.e.

inputs = tf.placeholder(tf.float32, [None, patch_size, patch_size, num_channels], name="inputs")
labels = tf.placeholder(tf.float32, [None, patch_size, patch_size, num_channels], name="labels")
network = build_network(inputs, ...)
optimizer = get_optimizer(labels, ...)
session.run(optimizer, {inputs: input_batch, labels: label_batch})
save.save(session, path)

Let's say patch_size is 50 for training in the above case.

Now when we try to perform testing, we can re-use some of the above code to build the network model and run the test. Let's say I set the patch size to 180. How did Tensorflow treat it if I restore the checkpoints but specify a different patch_size to build the network model?

patch_size=180
inputs = tf.placeholder(tf.float32, [None, patch_size, patch_size, num_channels], name="inputs")
labels = tf.placeholder(tf.float32, [None, patch_size, patch_size, num_channels], name="labels")
network = build_network(inputs, ...)
saver.restore(session, path)
prediction = session.run(network, {inputs:, input_batch})

As far as I can tell Tensorflow could accept different train input size (batch size 50) and test input size (batch size 180).

  1. So how does Tensorflow work this out internally to predict the output when the train and test input sizes are different?

  2. Is it okay to use different input sizes?

  3. Are there any advantages or disadvantages of using different input sizes?

1

1 Answers

4
votes
  1. TensorFlow does not need to know all the sizes statically. If sizes are known statically, it can be helpful for various internal optimizations and checks but generally it is not a requirement. Tensorflow does not "predict" the sizes. Given a particular input (in session.run()) it just runs the operations. Operations' kernels can require some sizes to be statically known, but can also work with some sizes being available only at run-time.

  2. Generally, yes. if you don't see any errors, you should be good. There are some exceptions to this rule where TensorFlow does not give you an explicit error, but it might do not what you expect. For example, if you use static_rnn but feed inputs of different length, they will be padded with zeros and run through all RNN steps. This is documented and there is a dynamic_rnn variant where you can pass sequence_length.

  3. In general, the more things are knows statically, the better performance you can potentially get, but this varies greatly. I would suggest that you default to whatever is most convenient to you first. If you feel like it should be much faster, you can invest it trying to homogenize your graph and specify as many sizes are possible. Generally, TensorFlow does a pretty good job with shape inference. As long as you specify placeholder sizes, the rest can generally be inferred. You can check this if you want by querying the shape of some intermediate tensor.