uhfband / keras2caffe

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

How to transfer tf.keras built .h5 model to caffemodel #23

Closed DanielXu123 closed 4 years ago

DanielXu123 commented 4 years ago

Hi, uhfband. May I ask how to run this script to transfer exits .h5 model, which was build by tf.keras.

DanielXu123 commented 4 years ago

I loaded the model using tensorflow.keras.models.load_model and then runs keras2caffe.convert(model, 'xx.prototxt', 'xx.caffemodel')

import sys
sys.path.append('./')

import keras2caffe
from tensorflow.keras.models import load_model

model = load_model('./xx.h5')

keras2caffe.convert(model, 'xx.prototxt', 'xx.caffemodel')

It turns an TypeError as follow

  File "xhn_demo.py", line 9, in <module>
    keras2caffe.convert(model, 'fod_model.prototxt', 'fod_model.caffemodel')
  File "/data1/xuhaonan/KERAS2CAFFE/keras2caffe/keras2caffe/convert.py", line 259, in convert
    if type(layer._inbound_nodes[0].inbound_layers[0]).__name__=='Flatten':
TypeError: 'Flatten' object does not support indexing
uhfband commented 4 years ago

Hi! I will try upgrade to TF 2.0 within the next days

DanielXu123 commented 4 years ago

Hi , uhfband. I'm using PYTHON 3.6 Tensorflowversion 1.15.0 tensorflow.keras.version '2.2.4-tf' CAFFEversion 1.0.0

DanielXu123 commented 4 years ago

I'm not very sure about whether it's related to TF version or not. I transfered another model, similiar error shows as below.

    keras2caffe.convert(model, 'mobv3_model.prototxt', 'mobv3_model.caffemodel')
  File "/data1/xuhaonan/KERAS2CAFFE/keras2caffe/keras2caffe/convert.py", line 259, in convert
    if type(layer._inbound_nodes[0].inbound_layers[0]).__name__=='Flatten':
TypeError: 'GlobalAveragePooling2D' object does not support indexing
uhfband commented 4 years ago

Keras internal API is differs from version to version. Possible it will not work with Keras version other than 2.1.5, and with tf.keras.