Skip to content

Commit

Permalink
revert keras v2 converter
Browse files Browse the repository at this point in the history
  • Loading branch information
calad0i committed Dec 17, 2024
1 parent 3f8acb5 commit d2ccfb4
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions hls4ml/converters/keras_to_hls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from warnings import warn

import h5py

Expand Down Expand Up @@ -231,8 +230,8 @@ def parse_keras_model(model_arch, reader):
layer_config = model_arch['config']
if 'layers' in layer_config: # Newer Keras versions have 'layers' in 'config' key
layer_config = layer_config['layers']
# Sequential doesn't have InputLayer in TF < 2.3 (Keras 2.4.0)
if layer_config[0]['class_name'] != 'InputLayer':
warn(DeprecationWarning('keras < 2.4.0 (tf 2.3) is deprecated. Please use a newer version.'))
input_layer = {}
input_layer['name'] = 'input1'
input_layer['class_name'] = 'InputLayer'
Expand All @@ -244,33 +243,25 @@ def parse_keras_model(model_arch, reader):
layer_config = model_arch['config']['layers']
input_layers = [inp[0] for inp in model_arch['config']['input_layers']]
output_layers = [out[0] for out in model_arch['config']['output_layers']]
else:
raise Exception(f'ERROR: Model class not supported: {model_arch["class_name"]}')

# Get input shape and check for unsupported layer type
for keras_layer in layer_config:
if keras_layer['class_name'] not in supported_layers:
raise Exception(f'ERROR: Unsupported layer type: {keras_layer["class_name"]}')
raise Exception('ERROR: Unsupported layer type: {}'.format(keras_layer['class_name']))

output_shapes = {}
output_shape = None

print('Topology:')
for keras_layer in layer_config:
if 'batch_input_shape' in keras_layer['config'] or 'batch_shape' in keras_layer['config']:
if 'batch_input_shape' in keras_layer['config']:
if 'inbound_nodes' in keras_layer and len(keras_layer['inbound_nodes']) > 0:
input_shapes = [output_shapes[inbound_node[0]] for inbound_node in keras_layer['inbound_nodes'][0]]
else:
_input_shapes = keras_layer['config'].get('batch_input_shape', None)
input_shapes = _input_shapes or keras_layer['config']['batch_shape']
input_shapes = [keras_layer['config']['batch_input_shape']]
else:
if 'inbound_nodes' in keras_layer:
if 'args' in keras_layer['inbound_nodes'][0]:
# keras v3
input_shapes = [arg['config']['shape'] for arg in keras_layer['inbound_nodes'][0]['args']]
else:
# keras v2
input_shapes = [output_shapes[inbound_node[0]] for inbound_node in keras_layer['inbound_nodes'][0]]
input_shapes = [output_shapes[inbound_node[0]] for inbound_node in keras_layer['inbound_nodes'][0]]
else:
# Sequential model, so output_shape from the previous layer is still valid
input_shapes = [output_shape]
Expand Down

0 comments on commit d2ccfb4

Please sign in to comment.