2

I am trying to get ResNet101 or ResNeXt, which are only available in Keras' repository for some reason, from Keras applications in TensorFlow 1.10:

import tensorflow as tf
from keras import applications

tf.enable_eager_execution()

resnext = applications.resnext.ResNeXt101(include_top=False, weights='imagenet', input_shape=(SCALED_HEIGHT, SCALED_WIDTH, 3), pooling=None)

However, this results in:

Traceback (most recent call last):
  File "myscript.py", line 519, in get_fpn
    resnet = applications.resnet50.ResNet50(include_top=False, weights='imagenet', input_shape=(SCALED_HEIGHT, SCALED_WIDTH, 3), pooling=None)
  File "Keras-2.2.4-py3.5.egg/keras/applications/__init__.py", line 28, in wrapper
    return base_fun(*args, **kwargs)
  File "Keras-2.2.4-py3.5.egg/keras/applications/resnet50.py", line 11, in ResNet50
    return resnet50.ResNet50(*args, **kwargs)
  File "Keras_Applications-1.0.8-py3.5.egg/keras_applications/resnet50.py", line 214, in ResNet50
    img_input = layers.Input(shape=input_shape)
  File "Keras-2.2.4-py3.5.egg/keras/engine/input_layer.py", line 178, in Input
    input_tensor=tensor)
  File "Keras-2.2.4-py3.5.egg/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "Keras-2.2.4-py3.5.egg/keras/engine/input_layer.py", line 87, in __init__
    name=self.name)
  File "Keras-2.2.4-py3.5.egg/keras/backend/tensorflow_backend.py", line 529, in placeholder
    x = tf.placeholder(dtype, shape=shape, name=name)
  File "tensorflow/python/ops/array_ops.py", line 1732, in placeholder
    raise RuntimeError("tf.placeholder() is not compatible with "
RuntimeError: tf.placeholder() is not compatible with eager execution.

I installed Keras from its GitHub master branch, since the pip installs of Keras and TensorFlow's Keras API for some strange reason do not include ResNet101, ResNetv2, ResNeXt, etc. Does anyone know how I can run such models (preferably ResNeXt) in TensorFlow's eager execution?

1 Answers1

2

As the error indicates, tf.placeholder() which is used as placeholder for feeding data to a tf Session using feed_dict, is incompatible with eager mode.

This link nicely explains it with an example: https://github.com/tensorflow/tensorflow/issues/18165#issuecomment-377841925

You can use models from tf.keras.applications for this purpose. I've tried with TF2.0 Beta release.

https://www.tensorflow.org/beta/tutorials/images/transfer_learning#create_the_base_model_from_the_pre-trained_convnets

import tensorflow as tf
resnext = tf.keras.applications.ResNeXt50(weights=None)
print(tf.executing_eagerly())

True

ResNeXt models are not available(I had to make some changes like copying resnext.py from keras/applications to tensorflow/python/keras/applications and changes to __init__.py etc.) but you can try with the existing models like ResNet50, if they work then you can try porting ResNeXt.

Manoj Mohan
  • 5,654
  • 1
  • 17
  • 21
  • I managed to get ResNet50 to work, and tried porting ResNeXt models from Keras installation folder to `tensorflow/python/keras/applications` as well, but then I got error in the `tensorflow/keras/applications` folder (no idea why there have to be two folder). If it's not too much to ask, could you reproduce the steps you took in porting ResNeXt to TensorFlow's Keras API? Or will I have better luck redesigning from scratch and downloading the pre-trained weights from Keras separately? –  Jun 23 '19 at 09:08
  • In my tf2.0 tensorflow directory, I see only tensorflow/python/keras/applications. – Manoj Mohan Jun 23 '19 at 13:36
  • 1
    For porting ResNeXt, 1. Create resnext.py(https://gist.github.com/usernameandme/5221e3afc6221c749277d2b6b7c96897) under tensorflow/python/keras/applications. 2. Update \_\_init__.py in the same folder. 3. Update \_\_init__.py in tensorflow/python/keras/api/_v2/keras/applications. Mimic the import conventions used by ResNet50 for ResNeXt50 in both the \_\_init__.py files. – Manoj Mohan Jun 23 '19 at 13:37