Discussion:
[theano-users] How does Theano handles a tensor of different shape than a function accepts ?
Rémi Bèges
2017-08-23 05:42:49 UTC
Permalink
Hello everyone,

I am using Theano as backend for Keras, for processing images (See previews
below), and I'm facing the following issue:

Calling K.function([...]) with an input shape* different* from the one
provided during function definition works with Theano backend but fails
with TensorFlow. I would like to understand how Theano handles that
difference and if possible write equivalent code for TensorFlow backend.
(TF fails because input tensor is simply different from input shape
accepted by the function)

Consider the following Keras code (It should look very similar to Theano,
it's just to get the idea):


import numpy as np
from keras import backend as K
from keras.layers.convolutional import (
AveragePooling2D, Convolution2D, MaxPooling2D, ZeroPadding2D)
from keras.models import Sequential


# Model accepts input 64*64
model_width = 64
model_height = 32
model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(3, model_height, model_width)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
model.add(AveragePooling2D((2, 2), strides=(2, 2)))

# Image to extract features from is 32*32 instead of 64*32
img_width = 32
img_height = 32
img = np.zeros((1, 3, img_height, img_width))

# Build the function
f = K.function([model.layers[0].input], [model.layers[0].output])

# Extract features of first layer
# This crashes with Tensor flow, unless img is resized to match model input_shape dimensions
# With Theano this works fine
features_conv1_1 = f([img])


This code runs fine with Theano. But with Tensorflow it crashes because
the input tensor is not of the same size of what the function accepts.

To make the code (and my entire program run) with TF I tried using
rescaling, padding with 0 and padding with reflection . None of those
methods work as well as what Theano does (see comparison below or image
attached)



Padding the input tensor along its spatial axes yields the closest results
but there are still some visible artifacts.

So to summarize:
How does Theano makes a function's input tensor match expected shape ?

Thanks a lot for your help,

Regards
Rémi
--
---
You received this message because you are subscribed to the Google Groups "theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to theano-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Frédéric Bastien
2017-08-28 13:05:45 UTC
Permalink
Theano don't hardcore the shapes in the graph most of the time. There is a
few places this can be done, not it is optional. As we don't use that, we
do don't care of the input shapes changes.

Fred
Post by Rémi Bèges
Hello everyone,
I am using Theano as backend for Keras, for processing images (See
Calling K.function([...]) with an input shape* different* from the one
provided during function definition works with Theano backend but fails
with TensorFlow. I would like to understand how Theano handles that
difference and if possible write equivalent code for TensorFlow backend.
(TF fails because input tensor is simply different from input shape
accepted by the function)
Consider the following Keras code (It should look very similar to Theano,
import numpy as np
from keras import backend as K
from keras.layers.convolutional import (
AveragePooling2D, Convolution2D, MaxPooling2D, ZeroPadding2D)
from keras.models import Sequential
# Model accepts input 64*64
model_width = 64
model_height = 32
model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(3, model_height, model_width)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
model.add(AveragePooling2D((2, 2), strides=(2, 2)))
# Image to extract features from is 32*32 instead of 64*32
img_width = 32
img_height = 32
img = np.zeros((1, 3, img_height, img_width))
# Build the function
f = K.function([model.layers[0].input], [model.layers[0].output])
# Extract features of first layer
# This crashes with Tensor flow, unless img is resized to match model input_shape dimensions
# With Theano this works fine
features_conv1_1 = f([img])
This code runs fine with Theano. But with Tensorflow it crashes because
the input tensor is not of the same size of what the function accepts.
To make the code (and my entire program run) with TF I tried using
rescaling, padding with 0 and padding with reflection . None of those
methods work as well as what Theano does (see comparison below or image
attached)
Padding the input tensor along its spatial axes yields the closest results
but there are still some visible artifacts.
How does Theano makes a function's input tensor match expected shape ?
Thanks a lot for your help,
Regards
Rémi
--
---
You received this message because you are subscribed to the Google Groups
"theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
---
You received this message because you are subscribed to the Google Groups "theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to theano-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...