uhfband / keras2caffe

Keras to Caffe model converter tool
MIT License
67 stars 22 forks source link

AttributeError: 'Dense' object has no attribute 'inbound_nodes' #6

Closed trohit920 closed 6 years ago

trohit920 commented 6 years ago

Hello @uhfband , Thanks for your work, really appreciated it. I trained my own CNN in Keras for a classification application and have weights and model saved accordingly. However when i tried to convert .h5 model to file to caffe model i am facing an error. My conversion script is similar to given scripts. Error looks like this:

Using TensorFlow backend.
2018-08-06 14:42:47.822665: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-08-06 14:42:47.910827: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:897] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-08-06 14:42:47.911164: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1392] Found device 0 with properties: 
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.759
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 7.55GiB
2018-08-06 14:42:47.911176: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1471] Adding visible gpu devices: 0
2018-08-06 14:42:48.079997: I tensorflow/core/common_runtime/gpu/gpu_device.cc:952] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-08-06 14:42:48.080038: I tensorflow/core/common_runtime/gpu/gpu_device.cc:958]      0 
2018-08-06 14:42:48.080046: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 0:   N 
2018-08-06 14:42:48.080200: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1084] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 4052 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
<keras.models.Sequential object at 0x7fa3aedf8990>
None
Traceback (most recent call last):
  File "keras2caffe.py", line 31, in <module>
    keras2caffe.convert(keras_model, caffe_proto, caffe_weights)
  File "/home/rohit/keras2caffe/keras2caffe/convert.py", line 242, in convert
    if type(layer.inbound_nodes[0].inbound_layers[0]).__name__=='Flatten':
AttributeError: 'Dense' object has no attribute 'inbound_nodes'

I am using Caffe : 1, Tensorflow : 1.9 and Keras : 2.1.3. Please take a look. Do you think it is because of different version of Tensorflow and Keras?

uhfband commented 6 years ago

Hi! Keras has significant API difference within minor version changes, could you try Keras 2.1.2?

trohit920 commented 6 years ago

Keras 2.1.2 works for your given models but not for my own model as it requires keras 2.1.3. I am getting an error :


  'passed to optimizer: ' + str(k))
TypeError: Unexpected keyword argument passed to optimizer: amsgrad

Is there any work around here ? Or do i need to define my models again?

trohit920 commented 6 years ago

Thanks for your reply. After training model again with current Keras installation i can successfully convert to Caffe Model. Closing this now. Thanks.

FlYWMe commented 6 years ago

@trohit920 Hi ! Did you convert MobileNet-keras to caffemodel ? I met the same problem with you : AttributeError: 'Dense' object has no attribute 'inbound_nodes' with Keras2.1.3 and TypeError: Unexpected keyword argument passed to optimizer: amsgrad with keras2.1.2 . How did you solve the problem? Thanks!

trohit920 commented 6 years ago

@FlYWMe I downgraded to Keras 2.1.2 version and everything works fine.

FlYWMe commented 6 years ago

@trohit920 Thanks for reply! What did u do with "TypeError: Unexpected keyword argument passed to optimizer: amsgrad"? Did u mean retraining mobilenet with keras2.1.2 ? If so,how to import 'DepthwiseConv2D' ?

trohit920 commented 6 years ago

@FlYWMe No need for retraining again. If you run the script convert_mobilenet.py given here, your keras model will be changed to equivalent caffe model. DepthwiseConv2D is already defined in converter.py in keras2caffe.

FlYWMe commented 6 years ago

@trohit920 Ok, so how to solve the Unexpected keyword amsgrad ?

trohit920 commented 6 years ago

@FlYWMe If you have your own Keras model than this error is removed once you finished training it again with Keras2.1.2 version and you can define your own converter python script accordingly. Otherwise this script should work fine for default MobileNet. I had my own Keras model and i successfully converted it to Caffe using this converter. I am not sure what is causing problem at your end.

uhfband commented 6 years ago

@FlYWMe AMSgrad has been added to Keras 2.1.3, it's not available in Keras 2.1.2. You could use another optimizer, or create the stub implementation of amsgrad in Keras 2.1.2 (I don't know is it possible). I will try to migrate to newer version of Keras next week.

FlYWMe commented 6 years ago

@uhfband @trohit920 Thanks !