roebius / deeplearning_keras2

Modification of fast.ai deep learning course notebooks for usage with Keras 2 and Python 3.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tensorflow support

tjliupeng opened this issue · comments

Hi, Roebius,

I try to use your fastai notebook in the environment with tensorflow. And it always fail. I know that we need to change the image_dim_order. And I have changed the keras.json to use tensorflow.

What do I miss any other changes?

Thanks

commented

Hi tjliupeng,
if you want to use the part 1 notebooks with Tensorflow there are a few things you should consider.
Let's use the notebook for lesson 1 as an example.

If you just want to see the notebook running with TF instead of Theano (without checking TF performance) then you should edit only the original keras.json and change from "backend": "theano" to "backend": "tensorflow".
You do not have to change image_dim_ordering and the notebook should already run using TF. As an additional note, I remember reading some comments reporting non-optimal performance of TF compared to Theano when doing the switch this way, but I haven't personally checked.

In order to also use the Tensorflow image dimension ordering then you should edit a couple of files:

a. Edit the original keras.json and change:
from "backend": "theano" to "backend": "tensorflow"
from "image_data_format": "channels_first" to "image_data_format": "channels_last".
I used image_data_format following the latest Keras 2 documentation. The legacy parameter was image_dim_ordering, but I have not checked what happens if you use it instead of the new one.

b. You will need to edit also vgg16.py to indicate the proper image dimension ordering in the following line:
vgg_mean = np.array([123.68, 116.779, 103.939], dtype=np.float32).reshape((1,1,3))

c. Again in vgg16.py you will need to edit the create function to indicate the proper image dimension ordering in the following line:
model.add(Lambda(vgg_preprocess, input_shape=(224,224,3), output_shape=(224,224,3)))
and you will also need to comment the line where the weights are loaded:
#model.load_weights(get_file(fname, self.FILE_PATH+fname, cache_subdir='models'))
because the weights originated from the Theano version and their dimensions are not compatible with the Tensorflow dimension ordering.
With these modifications you will be able to run the notebook with TF using the TF dimension ordering, but then your vgg will not be the pre-trained one anymore!

I did not experiment with the other notebooks with Tensorflow, but as you can see some debugging might be required. The fact that you in most cases will not be able to load the provided Theano-pretrained weights is also a drawback to consider.

commented

no additional comments received