GithubHelp home page GithubHelp logo

amir-abdi / keras_to_tensorflow Goto Github PK

View Code? Open in Web Editor NEW
1.7K 1.7K 543.0 149 KB

General code to convert a trained keras model into an inference tensorflow model

License: MIT License

Python 51.00% Jupyter Notebook 49.00%
deep-learning keras machine-learning python tensorflow

keras_to_tensorflow's Introduction

Keras to TensorFlow

The keras_to_tensorflow is a tool that converts a trained keras model into a ready-for-inference TensorFlow model. The tool is NOT tailored for TensorFlow 2.0 it SEEMS to be working fine.

Summary

  • In the default behaviour, this tool freezes the nodes (converts all TF variables to TF constants), and saves the inference graph and weights into a binary protobuf (.pb) file. During freezing, TensorFlow also applies node pruning which removes nodes with no contribution to the output tensor.

  • This tool supports multiple output networks and enables the user to rename the output tensors via the --output_nodes_prefix flag.

  • If the --output_meta_ckpt flag is set, the checkpoint and metagraph files for TensorFlow will also be exported which can later be used in the tf.train.Saver class to continue training.

How to use

Keras models can be saved as a single [.hdf5 or h5] file, which stores both the architecture and weights, using the model.save() function. This model can be then converted to a TensorFlow model by calling this tool as follows:

python keras_to_tensorflow.py 
    --input_model="path/to/keras/model.h5" 
    --output_model="path/to/save/model.pb"

Keras models can also be saved in two separate files where a [.hdf5 or h5] file stores the weights, using the model.save_weights() function, and another .json file stores the network architecture using the model.to_json() function. In this case, the model can be converted as follows:

python keras_to_tensorflow.py 
    --input_model="path/to/keras/model.h5" 
    --input_model_json="path/to/keras/model.json" 
    --output_model="path/to/save/model.pb"

Try

python keras_to_tensorflow.py --help

to learn about other supported flags (quantize, output_nodes_prefix, save_graph_def).

Dependencies

  • keras
  • tensorflow
  • absl
  • pathlib

Legacy code

The code on how to freeze and save keras models in previous versions of tensorflow is also available. Back then, the freeze_graph tool (/tensorflow/python/tools/freeze_graph.py) was used to convert the variables into constants. This functionality is now handled by graph_util.convert_variables_to_constants

keras_to_tensorflow's People

Contributors

amir-abdi avatar andreped avatar anujanegi avatar aswathkiruba avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

keras_to_tensorflow's Issues

How to use the converted model for prediction?

@amir-abdi
Firstly, thank you for sharing this great work. However, I don't see any good example to demonstrate the usage for prediction using the converted TensorFlow model (.pb file). With Keras, you have predict function from the model, while with a normally trained TF model, you specified the logits and placeholder for the input. With this converted model, I have no idea of how to perform the prediction. Could you please give me some guidance or a concrete example? Thank you very much!

Can not use keras_to_tensorflow.py

when i use the command:

python3 keras_to_tensorflow.py -input_model_file my_model.h5 -output_model_file model.pb

it return an error, error message as follows:

Traceback (most recent call last):
File "keras_to_tensorflow.py", line 95, in
net_model = load_model(weight_file_path)
File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 239, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
return layer_module.deserialize(config, custom_objects=custom_objects)
File "/usr/local/lib/python3.5/dist-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py", line 139, in deserialize_keras_object
list(custom_objects.items())))
File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 2490, in from_config
process_layer(layer_data)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 2476, in process_layer
custom_objects=custom_objects)
File "/usr/local/lib/python3.5/dist-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py", line 133, in deserialize_keras_object
': ' + class_name)
ValueError: Unknown layer: BatchNorm

what shall i do? thank you !

Modified InceptionV3 Keras model wont run in iOS

I added the pb file outputted by the keras_to_tensorflow notebook to the camera example of TensorFlow iOS however i get the following errors:

2017-07-03 15:30:19.127247+0200 tf_camera_example[13801:5065229] 0x170355660 Copy matching assets reply: XPC_TYPE_DICTIONARY  <dictionary: 0x170355660> { count = 3, transaction: 0, voucher = 0x0, contents =
	"Assets" => <data: 0x170474400>: { length = 1229 bytes, contents = 0x62706c6973743030d4010203040506646558247665727369... }
	"Result" => <int64: 0x17042d920>: 0
	"AssetXMLMetadata" => <data: 0x1704743c0>: { length = 307 bytes, contents = 0x62706c6973743030d40102030405061f2058247665727369... }
}
2017-07-03 15:30:19.132701+0200 tf_camera_example[13801:5065229] 0x170355870 Copy assets attributes reply: XPC_TYPE_DICTIONARY  <dictionary: 0x170355870> { count = 1, transaction: 0, voucher = 0x0, contents =
	"Result" => <int64: 0x17042d860>: 1
}
2017-07-03 15:30:19.132829+0200 tf_camera_example[13801:5065229] [MobileAssetError:1] Unable to copy asset attributes
2017-07-03 15:30:19.133159+0200 tf_camera_example[13801:5065229] Could not get attribute 'LocalURL': Error Domain=MobileAssetError Code=1 "Unable to copy asset attributes" UserInfo={NSDescription=Unable to copy asset attributes}
2017-07-03 15:30:19.140440+0200 tf_camera_example[13801:5065229] 0x170355450 Copy matching assets reply: XPC_TYPE_DICTIONARY  <dictionary: 0x170355450> { count = 3, transaction: 0, voucher = 0x0, contents =
	"Assets" => <data: 0x1704751c0>: { length = 1237 bytes, contents = 0x62706c6973743030d4010203040506636458247665727369... }
	"Result" => <int64: 0x17042dcc0>: 0
	"AssetXMLMetadata" => <data: 0x170475280>: { length = 307 bytes, contents = 0x62706c6973743030d40102030405061f2058247665727369... }
}
2017-07-03 15:30:19.144379+0200 tf_camera_example[13801:5065229] 0x170355870 Copy assets attributes reply: XPC_TYPE_DICTIONARY  <dictionary: 0x170355870> { count = 1, transaction: 0, voucher = 0x0, contents =
	"Result" => <int64: 0x17042d720>: 1
}
2017-07-03 15:30:19.144512+0200 tf_camera_example[13801:5065229] [MobileAssetError:1] Unable to copy asset attributes
2017-07-03 15:30:19.145302+0200 tf_camera_example[13801:5065229] Could not get attribute 'LocalURL': Error Domain=MobileAssetError Code=1 "Unable to copy asset attributes" UserInfo={NSDescription=Unable to copy asset attributes}
2017-07-03 15:30:20.389946: E /Users/madhavajay/Desktop/dev/tensorflow/tensorflow/examples/ios/camera/tensorflow_utils.mm:140] Could not create TensorFlow Graph: Invalid argument: No OpKernel was registered to support Op 'Switch' with these attrs.  Registered devices: [CPU], Registered kernels:
  device='GPU'; T in [DT_FLOAT]
  device='GPU'; T in [DT_INT32]
  device='GPU'; T in [DT_BOOL]
  device='GPU'; T in [DT_STRING]
  device='CPU'; T in [DT_INT32]
  device='CPU'; T in [DT_FLOAT]

	 [[Node: batch_normalization_188/cond/Switch = Switch[T=DT_BOOL](batch_normalization_95/keras_learning_phase, batch_normalization_95/keras_learning_phase)]]
2017-07-03 15:30:20.396129: F /Users/madhavajay/Desktop/dev/tensorflow/tensorflow/examples/ios/camera/CameraExampleViewController.mm:391] Couldn't load model: Invalid argument: No OpKernel was registered to support Op 'Switch' with these attrs.  Registered devices: [CPU], Registered kernels:
  device='GPU'; T in [DT_FLOAT]
  device='GPU'; T in [DT_INT32]
  device='GPU'; T in [DT_BOOL]
  device='GPU'; T in [DT_STRING]
  device='CPU'; T in [DT_INT32]
  device='CPU'; T in [DT_FLOAT]

	 [[Node: batch_normalization_188/cond/Switch = Switch[T=DT_BOOL](batch_normalization_95/keras_learning_phase, batch_normalization_95/keras_learning_phase)]]
(lldb) 

Error when using convert model on tensorflow for mobile

Hi friends,
After I converted keras model to tensorflow model, I run it on tensorflow for mobile. I got an error:

camera/CameraExampleViewController.mm:320] Running model failed:Invalid argument: Shape [-1,112,112,1] has negative dimensions
[[Node: input_1 = Placeholderdtype=DT_FLOAT, shape=[?,112,112,1], _device="/job:localhost/replica:0/task:0/cpu:0"]]

Code on tensorflow for mobile:
const int image_channels = 4;

assert(image_channels >= wanted_input_channels);
tensorflow::Tensor image_tensor(
tensorflow::DT_FLOAT,
tensorflow::TensorShape(
{1, wanted_input_height, wanted_input_width, wanted_input_channels}));
auto image_tensor_mapped = image_tensor.tensor<float, 4>();
tensorflow::uint8 *in = sourceStartAddr;
float *out = image_tensor_mapped.data();
for (int y = 0; y < wanted_input_height; ++y) {
float *out_row = out + (y * wanted_input_width * wanted_input_channels);
for (int x = 0; x < wanted_input_width; ++x) {
const int in_x = (y * image_width) / wanted_input_width;
const int in_y = (x * image_height) / wanted_input_height;
tensorflow::uint8 *in_pixel =
in + (in_y * image_width * image_channels) + (in_x * image_channels);
float *out_pixel = out_row + (x * wanted_input_channels);
for (int c = 0; c < wanted_input_channels; ++c) {
out_pixel[c] = (in_pixel[c] * (1.0/255.0));// - input_mean) / input_std;
}
}
}

CVPixelBufferUnlockBaseAddress(pixelBuffer, unlockFlags);

if (tf_session.get()) {
std::vectortensorflow::Tensor outputs;
tensorflow::Status run_status = tf_session->Run(
{{input_layer_name, image_tensor}}, {output_layer_name}, {}, &outputs);
if (!run_status.ok()) {
LOG(ERROR) << "Running model failed:" << run_status;
Thanks!

output_node_prefix is different from output_prefix with suffix from 0-n

132 for i in range(num_output):
133     pred_node_names[i] = args.output_node_prefix+str(i)  # Here 
134     pred[i] = tf.identity(net_model.outputs[i], name=pred_node_names[i])
135 print('output nodes names are: ', pred_node_names)

Say I set the output_node_prefix to 'value', then the actual output_node_name is 'value0', which is strange.
I know the code is written to support multi-output scenarios, maybe we can "when the num_output is 1, there is no suffix, otherwise, we will use suffix". So when the num_output is 1, the output_node_name will be 'value', when there's many outputs, it will be 'value0', 'value1' ... 'value12', ...

If the idea is acceptable, I'd like to submit a pull request.

Support TFLite

This is a handy tool - thanks. While there is a converter to go from the big TF model to TFLite, it would be useful to have it all in one.

I can not convert the keras - retinanet.h5 model to tensorflow Pb model ,thanks.

File "/home/zhangliyuan/RetinaNet/keras_to_tensorflow.py", line 54, in
net_model = load_model(weight_file_path)
File "/home/zhangliyuan/.local/lib/python3.6/site-packages/keras/models.py", line 240, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/home/zhangliyuan/.local/lib/python3.6/site-packages/keras/models.py", line 314, in model_from_config
return layer_module.deserialize(config, custom_objects=custom_objects)
File "/home/zhangliyuan/.local/lib/python3.6/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/home/zhangliyuan/.local/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 140, in deserialize_keras_object
list(custom_objects.items())))
File "/home/zhangliyuan/.local/lib/python3.6/site-packages/keras/engine/topology.py", line 2490, in from_config
process_layer(layer_data)
File "/home/zhangliyuan/.local/lib/python3.6/site-packages/keras/engine/topology.py", line 2476, in process_layer
custom_objects=custom_objects)
File "/home/zhangliyuan/.local/lib/python3.6/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/home/zhangliyuan/.local/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 142, in deserialize_keras_object
return cls.from_config(config['config'])
File "/home/zhangliyuan/.local/lib/python3.6/site-packages/keras/engine/topology.py", line 1253, in from_config
return cls(**config)
File "/home/zhangliyuan/.local/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
return func(*args, **kwargs)
File "/home/zhangliyuan/.local/lib/python3.6/site-packages/keras/layers/normalization.py", line 71, in init
super(BatchNormalization, self).init(**kwargs)
File "/home/zhangliyuan/.local/lib/python3.6/site-packages/keras/engine/topology.py", line 280, in init
raise TypeError('Keyword argument not understood:', kwarg)
TypeError: ('Keyword argument not understood:', 'freeze')

Can not convert the keras - moiblenet, H5 model to tensorflow Pb model

I use the tool to convert the keras- moiblenet model to pb model , but An error has occurred : ValueError: Unknown activation function:relu6.

The detail info is:

File "keras_to_tensorflow.py", line 95, in
net_model = load_model(weight_file_path)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/models.py", line 240, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/models.py", line 314, in model_from_config
return layer_module.deserialize(config, custom_objects=custom_objects)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 140, in deserialize_keras_object
list(custom_objects.items())))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/topology.py", line 2490, in from_config
process_layer(layer_data)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/topology.py", line 2476, in process_layer
custom_objects=custom_objects)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/layers/init.py", line 55, in deserialize
printable_module_name='layer')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 142, in deserialize_keras_object
return cls.from_config(config['config'])
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/topology.py", line 1253, in from_config
return cls(**config)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/layers/core.py", line 291, in init
self.activation = activations.get(activation)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/activations.py", line 95, in get
return deserialize(identifier)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/activations.py", line 87, in deserialize
printable_module_name='activation function')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 160, in deserialize_keras_object
':' + function_name)
ValueError: Unknown activation function:relu6

Error in type conversion

File "", line 5, in
pred[i] = tf.identity(net_model.outputs[i], name=pred_node_names[i])

File "C:\Users\PARVA SHAH\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 125, in identity
return gen_array_ops.identity(input, name=name)

File "C:\Users\PARVA SHAH\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 2070, in identity
"Identity", input=input, name=name)

File "C:\Users\PARVA SHAH\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 513, in _apply_op_helper
raise err

File "C:\Users\PARVA SHAH\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 510, in _apply_op_helper
preferred_dtype=default_dtype)

File "C:\Users\PARVA SHAH\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 926, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)

File "C:\Users\PARVA SHAH\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py", line 229, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)

File "C:\Users\PARVA SHAH\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py", line 208, in constant
value, dtype=dtype, shape=shape, verify_shape=verify_shape))

File "C:\Users\PARVA SHAH\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 472, in make_tensor_proto
"supported type." % (type(values), values))

TypeError: Failed to convert object of type <class 'theano.tensor.var.TensorVariable'> to Tensor. Contents: Softmax.0. Consider casting elements to a supported type.

Unknown loss function:<lambda>

@amir-abdi : Thank you for your great work,
I have a problem when converting mymodel.h5 to mymodel.pb

Traceback (most recent call last):
File "keras_to_tensorflow.py", line 123, in
raise err
File "keras_to_tensorflow.py", line 114, in
net_model = load_model(weight_file_path)
File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 274, in load_model
sample_weight_mode=sample_weight_mode)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 626, in compile
loss_functions.append(losses.get(loss.get(name)))
File "/usr/local/lib/python3.5/dist-packages/keras/losses.py", line 122, in get
return deserialize(identifier)
File "/usr/local/lib/python3.5/dist-packages/keras/losses.py", line 114, in deserialize
printable_module_name='loss function')
File "/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py", line 164, in deserialize_keras_object
':' + function_name)
ValueError: Unknown loss function:

Have you any suggestion to solve this?
Thanks,

About to use "Quantize weights" method

I notice that the size of model.pb can be smaller if use "Quantize weights" method proposed by tensorflow-team. But I don't know how to use it. Can you think about this way?

the new .pb isn't work well

I do follow the way that Readme showed, but my new .pb is not work as same as .h5,I do not know the reason and also do not know how to fix it...It seems like no matter what image is delievered into the .pb,there is only one same output.

Redundant output_node_names_of_input_network?

Hello Amir,

I wonder whether the output_node_names_of_input_network is not redundant: couldn't we consider just net_model.output.name from the model loaded as load_model(weight_file_path)?

Bug and bugfix for convertion to tensorflow

While freezing the MINT net i recognized a small bug which lead to a wrong results when running the network in forward mode. With the current version the output has been connected in a wrong manner. The current code I am adressing is:

K.set_learning_phase(0)
net_model = load_model(weight_file_path)

pred = [None]*num_output
pred_node_names = [None]*num_output
for i in range(num_output):
     pred_node_names[i] = prefix_output_node_names_of_final_network+str(i)
     pred[i] = tf.identity(net_model.output[i], name=pred_node_names[i]),
print('output nodes names are: ', pred_node_names)

It seams that there is a small typo in net_model.output[i]. If the outputs field is used instead (net_model.outputs[i]) everythink works fine for me. I. e. the fix will be:

K.set_learning_phase(0)
net_model = load_model(weight_file_path)

pred = [None]*num_output
pred_node_names = [None]*num_output
for i in range(num_output):
     pred_node_names[i] = prefix_output_node_names_of_final_network+str(i)
     pred[i] = tf.identity(net_model.outputs[i], name=pred_node_names[i]),
print('output nodes names are: ', pred_node_names)

Can not convert the H5 model to tensorflow Pb model

@amir-abdi
Hi, very thanks to your reply about my problems!
I use the code exactly the same with you and my problem is also that can not convert the H5 model to tensorflow model.
In the code "net_model = load_model(weight_file_path)", error happens, and the error info is
"raise ValueError('No model found in config file.')"

The following error occurred

``Traceback (most recent call last):
File "pb1.py", line 110, in
raise err
File "pb1.py", line 101, in
net_model = load_model(weight_file_path)
File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 241, in load_model
raise ValueError('No model found in config file.')
ValueError: No model found in config file.

Is .pb going to be faster than .h5?

Hi all, based on your experiences, is the converted pb model going to be faster than the h5 model during inference? I tested on my CPU and it shows pb model is about 20% slower than the h5 model.

Conversion error

ValueError: Improper config format: {'l1': 0.0, 'name': 'WeightRegularizer', 'l2': 0.0005000000237487257}
How to deal with this error i am using python3.x

Eliminate dropout?

Hi Amir!!

Great job with the library. One thing that might be an issue sometimes however is the dropout handling. For example, you might want to freeze the graph to deploy the .pb file in iOS/Android, which means you have to remove the dropout nodes (or get a op RandomUniform not supported error...), however, in Keras the variables at inference time (Trainable=False) get rescaled to account for the fact that now all the nodes are active.

So how do you handle the dropout when you freeze your keras model to tensor flow?

Difference in output dimensions

Hello,

First of all thank you very much for the valuable code!

I used your code to convert my Keras model but there is some inconsistency in the output dimensions between the models.

Checking for the original model

model = load_model('keras_model.h5', compile=False)
print(model.output)

I obtained:

[<tf.Tensor 'conv2d_10/BiasAdd:0' shape=(?, ?, ?, 33) dtype=float32>, <tf.Tensor 'conv2d_13/BiasAdd:0' shape=(?, ?, ?, 33) dtype=float32>]

So the output of the model is a list of 2 tensors.

Now for the converted model in Tensorflow:

graph = tf.Graph()
with graph.as_default():
    graph_def = tf.GraphDef()
    with tf.gfile.GFile('tf_model.pb', 'rb') as fid:
        graph_def.ParseFromString(fid.read())
        tf.import_graph_def(graph_def, name='')

        # Print the name of operations in the session
        for op in graph.get_operations():
            print("Operation Name:",op.name)
            print("Tensor Stats:",str(op.values()))

The last few output lines of the above code is:

Operation Name : leaky_re_lu_9/LeakyRelu/mul
Tensor Stats : (<tf.Tensor 'leaky_re_lu_9/LeakyRelu/mul:0' shape=(?, ?, ?, 512) dtype=float32>,)
Operation Name : leaky_re_lu_9/LeakyRelu/Maximum
Tensor Stats : (<tf.Tensor 'leaky_re_lu_9/LeakyRelu/Maximum:0' shape=(?, ?, ?, 512) dtype=float32>,)
Operation Name : conv2d_10/convolution
Tensor Stats : (<tf.Tensor 'conv2d_10/convolution:0' shape=(?, ?, ?, 33) dtype=float32>,)
Operation Name : conv2d_10/BiasAdd
Tensor Stats : (<tf.Tensor 'conv2d_10/BiasAdd:0' shape=(?, ?, ?, 33) dtype=float32>,)
Operation Name : output_node0
Tensor Stats : (<tf.Tensor 'output_node0:0' shape=(?, ?, ?, 33) dtype=float32>,)

The last line is the output, it is a single tensor.

In conclusion, the Keras output is a list of 2 tensors, while the Tensorflow output is a single tensor.

Could you please help?
Thank you very much in advance!

No module named 'tensorflow.tools'

You can follow the tensorflow issue here: tensorflow/tensorflow#9778

The module doesn't need to be imported though.
To fix this:

# convert variables to constants and save
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io
if args.quantize:
    from tensorflow.tools.graph_transforms import TransformGraph
    transforms = ["quantize_weights", "quantize_nodes"]
    transformed_graph_def = TransformGraph(sess.graph.as_graph_def(), [], pred_node_names, transforms)
    constant_graph = graph_util.convert_variables_to_constants(sess, transformed_graph_def, pred_node_names)
else:
    constant_graph = graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), pred_node_names)    
graph_io.write_graph(constant_graph, output_fld, args.output_model_file, as_text=False)
print('saved the freezed graph (ready for inference) at: ', osp.join(output_fld, args.output_model_file))

converting .h5 files to .meta

Hi,
Thanks for this code!

Is there a way to use the code and convert the Keras model (.h5 file) to a .meta and .index files, to be read by tensorflow?

OSError: Unable to open file

Using TensorFlow backend.
Traceback (most recent call last):
File "keras_to_tensorflow.py", line 114, in
net_model = load_model(weight_file_path)
File "D:\Program Files\Anaconda3\lib\site-packages\keras\models.py", line 237,
in load_model
with h5py.File(filepath, mode='r') as f:
File "D:\Program Files\Anaconda3\lib\site-packages\h5py_hl\files.py", line 27
2, in init
fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
File "D:\Program Files\Anaconda3\lib\site-packages\h5py_hl\files.py", line 92
, in make_fid
fid = h5f.open(name, flags, fapl=fapl)
File "h5py_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (C:\Mino
nda\conda-bld\h5py_1474482825505\work\h5py_objects.c:2705)
File "h5py_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (C:\Mino
nda\conda-bld\h5py_1474482825505\work\h5py_objects.c:2663)
File "h5py\h5f.pyx", line 76, in h5py.h5f.open (C:\Minonda\conda-bld\h5py_1474
482825505\work\h5py\h5f.c:1951)
OSError: Unable to open file (Unable to open file: name = ''model.h5'', errno =
2, error message = 'no such file or directory', flags = 0, o_flags = 0)

input node and output node

hi, im coverting hdf5 to pb, so that i can use the model in android.
but how do i know the input node name and output node name?

how to specified customized loss function?

I got such error:
ValueError: Unknown loss function:softmax_cross_entropy_with_logits

When I read my model, the code is:
return load_model( run_archive_folder + game + '/run' + str(run_number).zfill(4) + "/models/version" + "{0:0>4}".format(version) + '.h5', custom_objects={'softmax_cross_entropy_with_logits': softmax_cross_entropy_with_logits})

I offered the softmax_cross_entropy_with_logits function in the keras_to_tensorflow.py, but no luck.
Can you help this out?
Thank you very much!

Keras multi-class output and node saving problem

Hi!

I am working on a Keras CNN model able to detect 8 object categories. I would like to convert it into protobuf to use it for an Android app, so I used keras_to_tensorflow library. The problem is that I get a really small number of saved nodes (10 in total) compared to the total of nodes, and I am currently not able to implement the model into my app.
Moreover, it is impossible for me to load the weights.h5 so I am trying to use the model.h5.

Here is the modification I made to the notebook in order to adapt it to my problem:

input_fld = '/home/Documents/Environment/'
weight_file = 'detection_model.h5'
num_output = 8
write_graph_def_ascii_flag = True
prefix_output_node_names_of_final_network = 'object'
output_graph_name = 'androidtest_model.pb'

Add License

If you get a chance, could you add a license file to the repo? Thanks!

Generating a TF checkpoint file

I'm intending to use the generated *.pb file into Lucid. However, aside of this file, is also necessary to have a TF model checkponit file (*.ckpt). I would like to know how to obtain that from my Keras model (if possible).

Random Guess after converting to .pb file.

Hi
First of all, thanks for your work.
I have a small problem and I do not know from where it comes from.
I have used your code to convert a Keras model into a .pb file in order to use it for inference on and Android application. I have taken the model from a GitHub repository which provides a demo.py file to check their work. I ran the demo and the prediction was OKay to some extent.
On the other hand, The problem I faced is that the android application is giving me 0.5 accuracy when inferring on Android which is kind of guessing unlike the demo.py from where I have extracted the model.
I am really confused. I do not know where the problem is!
I notice that android gives me a warning saying that "Skipped 151 frames! The application may be doing too much work on its main thread." I know that this means the UI thread is doing heavy work due to the inference and I need to practice multi threading programming. However, does this contribute to the problem of random guess of my model?

Thanks in advance

Cannot convert a Resnet model created with Keras (Thaeno backend) to tensorflow

Hi Amir,
Thanks for sharing your code. I am trying to convert Resnet model(h5 file) created using Thaeno backend to a tensorflow (pb file). I tried your code, but it threw an error stating, "Dimension 1 in both shapes must be equal, but are 32 and 64 for 'merge_1_9/concat".

I was able to figure out that Theano and TensorFlow implement convolution in different ways, and thus, convolution filters trained with Theano need to be converted before being with TensorFlow.

Is there any way to implement the conversion of Keras(Thaeno) to tensorflow. Thanks in advance.

the graph is empty

hi,
I created the .pb file from .h5 (when I tried to convert hdf5 to .pb I receive below error) but when I can not show the graph on the tensorboard because it's empty. could you please guide me.

return cls(**config)
TypeError: init() got an unexpected keyword argument 'input_dtype'
Best

Unable to convert hdf5 graph

Im trying to convert this weight file to tensorflow

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-321a000e8778> in <module>()
      1 K.set_learning_phase(0)
----> 2 net_model = load_model(weight_file_path)
      3 
      4 pred = [None]*num_output
      5 pred_node_names = [None]*num_output

/Users/anaconda/lib/python3.6/site-packages/keras/models.py in load_model(filepath, custom_objects)
    126         raise ValueError('No model found in config file.')
    127     model_config = json.loads(model_config.decode('utf-8'))
--> 128     model = model_from_config(model_config, custom_objects=custom_objects)
    129 
    130     # set weights

/Users/anaconda/lib/python3.6/site-packages/keras/models.py in model_from_config(config, custom_objects)
    175         raise Exception('`model_fom_config` expects a dictionary, not a list. '
    176                         'Maybe you meant to use `Sequential.from_config(config)`?')
--> 177     return layer_from_config(config, custom_objects=custom_objects)
    178 
    179 

/Users/anaconda/lib/python3.6/site-packages/keras/utils/layer_utils.py in layer_from_config(config, custom_objects)
     34         layer_class = get_from_module(class_name, globals(), 'layer',
     35                                       instantiate=False)
---> 36     return layer_class.from_config(config['config'])
     37 
     38 

/Users/anaconda/lib/python3.6/site-packages/keras/engine/topology.py in from_config(cls, config, custom_objects)
   2334 
   2335         for layer_data in config['layers']:
-> 2336             process_layer(layer_data)
   2337 
   2338         name = config.get('name')

/Users/anaconda/lib/python3.6/site-packages/keras/engine/topology.py in process_layer(layer_data)
   2312             # instantiate layer
   2313             layer = layer_from_config(layer_data,
-> 2314                                       custom_objects=custom_objects)
   2315             created_layers[layer_name] = layer
   2316 

/Users/anaconda/lib/python3.6/site-packages/keras/utils/layer_utils.py in layer_from_config(config, custom_objects)
     34         layer_class = get_from_module(class_name, globals(), 'layer',
     35                                       instantiate=False)
---> 36     return layer_class.from_config(config['config'])
     37 
     38 

/Users/anaconda/lib/python3.6/site-packages/keras/engine/topology.py in from_config(cls, config)
    924                 output of get_config.
    925         '''
--> 926         return cls(**config)
    927 
    928     def count_params(self):

TypeError: __init__() got an unexpected keyword argument 'sparse'

Getting error when converting model

Hi,

I am trying to convert an h5 model created with keras 2.0.8 to pb and am getting the following error:

Traceback (most recent call last):
File "keras_to_tensorflow.py", line 114, in
net_model = load_model(weight_file_path)
File "C:\Users\USER\Anaconda3\envs\tracx\lib\site-packages\keras\models.py", line 239, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "C:\Users\USER\Anaconda3\envs\tracx\lib\site-packages\keras\models.py", line 313, in model_from_config
return layer_module.deserialize(config, custom_objects=custom_objects)
File "C:\Users\USER\Anaconda3\envs\tracx\lib\site-packages\keras\layers_init_.py", line 54, in deserialize
printable_module_name='layer')
File "C:\Users\USER\Anaconda3\envs\tracx\lib\site-packages\keras\utils\generic_utils.py", line 139, in deserialize_keras_object
list(custom_objects.items())))
File "C:\Users\USER\Anaconda3\envs\tracx\lib\site-packages\keras\engine\topology.py", line 2487, in from_config
process_layer(layer_data)
File "C:\Users\USER\Anaconda3\envs\tracx\lib\site-packages\keras\engine\topology.py", line 2473, in process_layer
custom_objects=custom_objects)
File "C:\Users\USER\Anaconda3\envs\tracx\lib\site-packages\keras\layers_init_.py", line 54, in deserialize
printable_module_name='layer')
File "C:\Users\USER\Anaconda3\envs\tracx\lib\site-packages\keras\utils\generic_utils.py", line 141, in deserialize_keras_object
return cls.from_config(config['config'])
File "C:\Users\USER\Anaconda3\envs\tracx\lib\site-packages\keras\engine\topology.py", line 1252, in from_config
return cls(**config)
File "C:\Users\USER\Anaconda3\envs\tracx\lib\site-packages\keras\layers\core.py", line 470, in init
super(Flatten, self).init(**kwargs)
File "C:\Users\USER\Anaconda3\envs\tracx\lib\site-packages\keras\engine\topology.py", line 279, in init
raise TypeError('Keyword argument not understood:', kwarg)
TypeError: ('Keyword argument not understood:', 'data_format')

The test.h5 file got converted well so I suppose it's not a problem with my settings.

Thanks in advance.

what is tf.identify for

Thanks for your work.
I wonder the meaning of tf.identify(model.output,pred_output_name).
Does it mean add the output node of the original keras model to the new Tf model, and give it a name?
Then the problem is the size of output num. If the shape of output of my keras model is (?,5)
, ? represents batchsize,5 represents the columns of the output. The output is a list, then how do I set the value of.num_output?
Is any value ok? Does it depends on the batchsize I want to set?

ValueError: No model found in config file.

I want to convert .h5 file to .pb file.but there is an error message:
Input file specified (c3d.h5) only holds the weights, and not the model defenition.
I don't know how to solve this problem,can anybody help me?

After using Quantize the frozen model doesn't work

Hi there, I used the keras_to_tensorflow.py to create a frozen .pb file which worked great, but when I create the file using Quantize the model doesn't work and throws OOM error Allocation of 1153433600 exceeds 10% of system memory.

Error regarding load_model and set_learning_phase order

When I execute your script I successfully obtain the .pb file however, when using it in C++, I get following error:

> 2017-07-12 11:45:15.158206: W D:\Tensorflow\tensorflow\tensorflow\core\framework\op_kernel.cc:1191] Invalid argument: You must feed a value for placeholder tensor 'keras_learning_phase' with dtype bool
>          [[Node: keras_learning_phase = Placeholder[dtype=DT_BOOL, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]

By changing the order of load_model and set_learning_phase, i.e,

K.set_learning_phase(0)
net_model = load_model(weight_file_path)

the inference works smoothly.

I have a further question:
I would like to execute the graph with batchsize greater than 1. I figured out that by increasing num_output (let's say, 2) it would make the trick. But it did not work.

Do you have any idea of what to do to increase the batchsize?

Thanks!

Converted model loss some output layers

I used the keras model from https://github.com/qqwweee/keras-yolo3
I can successfully convert form .h to .pb.

Output:

input args: Namespace(f=None, graph_def=False, input_fld='.', input_model_file='/home/bill/git/keras-yolo3/model_data/yolo.h5', num_outputs=1, output_fld='', output_graphdef_file='model.ascii', output_model_file='', output_node_prefix='output_node', quantize=False, theano_backend=False)
/home/bill/anaconda3/lib/python3.6/site-packages/h5py/init.py:36: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type.
from ._conv import register_converters as _register_converters
Using TensorFlow backend.
2018-06-07 18:38:41.629332: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
/home/bill/.local/lib/python3.6/site-packages/keras/models.py:255: UserWarning: No training configuration found in save file: the model was not compiled. Compile it manually.
warnings.warn('No training configuration found in save file: '
output nodes names are: ['output_node0']
Converted 292 variables to const ops.
saved the freezed graph (ready for inference) at: yolo.h5.pb

Process finished with exit code 0

and the keras summary show the latest 3 layers are
conv2d_59 (Conv2D) (None, None, None, 2 261375 leaky_re_lu_58[0][0]
conv2d_67 (Conv2D) (None, None, None, 2 130815 leaky_re_lu_65[0][0]
conv2d_75 (Conv2D) (None, None, None, 2 65535 leaky_re_lu_72[0][0]

However, the pb file I checked with tensorboard losses conv2d_67 and conv2d_75.
Do you know what happened?

I can not convert the keras - mask rcnn.h5 model to tensorflow Pb model ,thanks.

Traceback (most recent call last):
File "/home/maxl/DeepLearning/Mask_RCNN-master/export_keras_to_tensorflow.py", line 51, in
net_model = load_model(weight_file_path)
File "/home/maxl/anaconda3/lib/python3.5/site-packages/keras/models.py", line 237, in load_model
raise ValueError('No model found in config file.')
ValueError: No model found in config file.

Weights are wrong when converting to tensorflow

System Info

  • OS: Ubuntu 16.04
  • Tensorflow 1.2.0
  • Keras 1.2.2

Description

Hi there,

I have a model called model.h5 that works fine in tests using Keras. Note: the model was trained in Keras v1.2.2, not Keras v2.x.

When I apply this script and try to run the constant_graph_weights.pb in tensorflow, my outputs are completely different (see code below).

The original model was made by transfer learning on the VGG-19 network. The VGG-19 weights were frozen, and the head was cut off and trained to take images and output 4 mutually exclusive categories. In Keras, this model works fine - I get 4 probabilities that add to 1.

In tensorflow, all my probabilities are roughly 0.25 no matter what. I'm thinking that the graph definition is being loaded properly but the weights aren't being applied at all - the retrained "head" of the network is probably using something like the default weights initialization.

Anyway, given the constant_graph_weights.pb file, here is what I use to generate the tensorflow prediction from an image, vs what I use in Keras:

#### TENSORFLOW ####
import tensorflow as tf
from tensorflow.python.platform import gfile
import cv2

def analyze_tf(image):
  with gfile.FastGFile('./constant_graph_weights.pb', 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    g_in = tf.import_graph_def(graph_def)

  sess = tf.Session()
  output = sess.graph.get_tensor_by_name('Softmax:0')
  input_tensor = sess.graph.get_tensor_by_name('input_1:0')
  init_op = sess.graph.get_operation_by_name('init')
  sess.run(init_op)
  pred_results = sess.run(output, {input_tensor: frame })

  return pred_results

image = cv2.imread('path/to/image')
pred = analyze_tf(normalize_image(image))
print(pred)

And the keras code, which works fine:

#### KERAS ####
from keras.models import load_model
from keras import backend
import cv2

os.environ["KERAS_BACKEND"] = "tensorflow"
backend.set_image_dim_ordering('tf')

model = load_model('./original_keras_model.h5')
def analyze_keras(image):
  return model.predict(image)

image = cv2.imread('path/to/image')
pred = analyze_keras(normalize_image(image))
print(pred)

Thanks so much for all your help!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.