def pytorch_to_keras(<EOL>model, args, input_shapes,<EOL>change_ordering=False, training=False, verbose=False, names=False,<EOL>):
<EOL>if isinstance(args, torch.autograd.Variable):<EOL><INDENT>args = (args, )<EOL><DEDENT>if isinstance(input_shapes, tuple):<EOL><INDENT>input_shapes = [input_shapes]<EOL><DEDENT>orig_state_dict_keys = _unique_state_dict(model).keys()<EOL>with set_training(model, training):<EOL><INDENT>trace, torch_out = torch.jit.get_trace_graph(model, tuple(args))<EOL><DEDENT>if orig_state_dict_keys != _unique_state_dict(model).keys():<EOL><INDENT>raise RuntimeError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if version.parse('<STR_LIT>') < version.parse(torch.__version__):<EOL><INDENT>trace.set_graph(_optimize_graph(trace.graph(), OperatorExportTypes.ONNX))<EOL><DEDENT>else:<EOL><INDENT>trace.set_graph(_optimize_graph(trace.graph(), False))<EOL><DEDENT>trace.graph().lint()<EOL>if verbose:<EOL><INDENT>print(trace.graph())<EOL><DEDENT>nodes = list(trace.graph().nodes())<EOL>if six.PY3:<EOL><INDENT>from types import SimpleNamespace<EOL>seq_to_find =['<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>k = <NUM_LIT:0><EOL>s = <NUM_LIT:0><EOL>for i, node in enumerate(nodes):<EOL><INDENT>if node.kind() == seq_to_find[k]:<EOL><INDENT>if k == <NUM_LIT:0>:<EOL><INDENT>s = i<EOL><DEDENT>k += <NUM_LIT:1><EOL>if k == len(seq_to_find):<EOL><INDENT>reshape_op = nodes[s + k - <NUM_LIT:1>]<EOL>flatten_op = {<EOL>'<STR_LIT>': (lambda: '<STR_LIT>'),<EOL>'<STR_LIT>': (lambda: {}),<EOL>'<STR_LIT>':  (lambda: list(reshape_op.outputs())),<EOL>'<STR_LIT>': (lambda: reshape_op.scopeName()),<EOL>'<STR_LIT>': (lambda: list(reshape_op.inputs())[:<NUM_LIT:1>]),<EOL>'<STR_LIT>': (lambda: reshape_op.__str__()),<EOL>}<EOL>nodes = nodes[:s] + [SimpleNamespace(**flatten_op)] + nodes[s+k:]<EOL>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>k = <NUM_LIT:0><EOL>s = -<NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>graph_outputs = [get_leaf_id(n) for n in trace.graph().outputs()]<EOL>graph_inputs = [get_leaf_id(n) for n in trace.graph().inputs()]<EOL>state_dict = _unique_state_dict(model)<EOL>if verbose:<EOL><INDENT>print('<STR_LIT>', graph_inputs)<EOL>print('<STR_LIT>', graph_outputs)<EOL>print('<STR_LIT>', list(state_dict))<EOL><DEDENT>import re<EOL>import keras<EOL>from keras import backend as K<EOL>K.set_image_data_format('<STR_LIT>')<EOL>layers = dict()<EOL>keras_inputs = []<EOL>for i in range(len(args)):<EOL><INDENT>layers[graph_inputs[i]] = keras.layers.InputLayer(<EOL>input_shape=input_shapes[i], name='<STR_LIT>'.format(i)<EOL>).output<EOL>keras_inputs.append(layers[graph_inputs[i]])<EOL><DEDENT>outputs = []<EOL>group_indices = defaultdict(lambda: <NUM_LIT:0>, {})<EOL>for node in nodes:<EOL><INDENT>node_inputs = list(node.inputs())<EOL>node_input_names = []<EOL>for node_input in node_inputs:<EOL><INDENT>node_input_names.append(get_leaf_id(node_input))<EOL><DEDENT>node_type = node.kind()<EOL>node_scope_name = node.scopeName()<EOL>node_id = get_node_id(node)<EOL>node_name_regex = re.findall(r'<STR_LIT>', node_scope_name)<EOL>try: <EOL><INDENT>int(node_name_regex[-<NUM_LIT:1>])<EOL>node_weigth_group_name = '<STR_LIT:.>'.join(<EOL>node_name_regex[:-<NUM_LIT:1>]<EOL>)<EOL>node_weights_name = node_weigth_group_name + '<STR_LIT:.>' + str(group_indices[node_weigth_group_name])<EOL>group_indices[node_weigth_group_name] += <NUM_LIT:1><EOL><DEDENT>except ValueError:<EOL><INDENT>node_weights_name = '<STR_LIT:.>'.join(<EOL>node_name_regex<EOL>)<EOL><DEDENT>except IndexError:<EOL><INDENT>node_weights_name = '<STR_LIT:.>'.join(node_input_names)<EOL><DEDENT>node_attrs = {k: node[k] for k in node.attributeNames()}<EOL>node_outputs = list(node.outputs())<EOL>node_outputs_names = []<EOL>for node_output in node_outputs:<EOL><INDENT>if node_output.node().scopeName():<EOL><INDENT>node_outputs_names.append(node_output.node().scopeName())<EOL><DEDENT><DEDENT>if verbose:<EOL><INDENT>print('<STR_LIT>')<EOL>print('<STR_LIT>', node_scope_name)<EOL>print('<STR_LIT>', node_id)<EOL>print('<STR_LIT>', node_type)<EOL>print('<STR_LIT>', node_input_names)<EOL>print('<STR_LIT>', node_outputs_names)<EOL>print('<STR_LIT>', node_weights_name)<EOL>print('<STR_LIT>', node_attrs)<EOL>print('<STR_LIT>', node_id in graph_outputs)<EOL><DEDENT>AVAILABLE_CONVERTERS[node_type](<EOL>node_attrs,<EOL>node_weights_name, node_id,<EOL>node_input_names,<EOL>layers, state_dict,<EOL>names<EOL>)<EOL>if node_id in graph_outputs:<EOL><INDENT>outputs.append(layers[node_id])<EOL><DEDENT><DEDENT>model = keras.models.Model(inputs=keras_inputs, outputs=outputs)<EOL>if change_ordering:<EOL><INDENT>import numpy as np<EOL>conf = model.get_config()<EOL>for layer in conf['<STR_LIT>']:<EOL><INDENT>if layer['<STR_LIT>'] and '<STR_LIT>' in layer['<STR_LIT>']:<EOL><INDENT>layer['<STR_LIT>']['<STR_LIT>'] =tuple(np.reshape(np.array(<EOL>[<EOL>[None] +<EOL>list(layer['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:2>:][:]) +<EOL>[layer['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:1>]]<EOL>]), -<NUM_LIT:1><EOL>))<EOL><DEDENT>if layer['<STR_LIT>'] and '<STR_LIT>' in layer['<STR_LIT>']:<EOL><INDENT>if len(list(layer['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:1>:][:])) > <NUM_LIT:0>:<EOL><INDENT>layer['<STR_LIT>']['<STR_LIT>'] =tuple(np.reshape(np.array(<EOL>[<EOL>list(layer['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:1>:][:]),<EOL>layer['<STR_LIT>']['<STR_LIT>'][<NUM_LIT:0>]<EOL>]), -<NUM_LIT:1><EOL>),)<EOL><DEDENT><DEDENT>if layer['<STR_LIT>'] and '<STR_LIT>' in layer['<STR_LIT>']:<EOL><INDENT>layer['<STR_LIT>']['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>if layer['<STR_LIT>'] and '<STR_LIT>' in layer['<STR_LIT>']:<EOL><INDENT>layer['<STR_LIT>']['<STR_LIT>'] = <NUM_LIT:3><EOL><DEDENT><DEDENT>K.set_image_data_format('<STR_LIT>')<EOL>model_tf_ordering = keras.models.Model.from_config(conf)<EOL>for dst_layer, src_layer in zip(<EOL>model_tf_ordering.layers, model.layers<EOL>):<EOL><INDENT>dst_layer.set_weights(src_layer.get_weights())<EOL><DEDENT>model = model_tf_ordering<EOL><DEDENT>print('<STR_LIT>'<EOL>'<STR_LIT>')<EOL>return model<EOL>
By given pytorch model convert layers with specified convertors.

Args:
    model: pytorch model
    args: pytorch model arguments
    input_shapes: keras input shapes (using for each InputLayer)
    change_ordering: change CHW to HWC
    training: switch model to training mode
    verbose: verbose output
    names: use short names, use random-suffix or keep original names for keras layers

Returns:
    model: created keras model.