def keras_graph_to_onnx_graph(cls, model, name): # some import attribute:keras_version, backend # this list record outputs of this graph graph_outputs = [] for o in model.outputs: graph_outputs.append( make_tensor_value_info(name=o.name, elem_type=STR_TO_ONNX_TYPE[K.dtype(o)], shape=convert_shape(K.int_shape(o)))) # this list record input of this graph graph_inputs = [] for i in model.inputs: shape = convert_shape(K.int_shape(i)) if shape[0] == -1: shape[0] = 1 # channels_last to channels_first if K.image_data_format() == "channels_last" and len(shape) == 4: shape = shape[:1] + shape[3:] + shape[1:3] print(shape) graph_inputs.append( make_tensor_value_info(name=i.name, elem_type=STR_TO_ONNX_TYPE[K.dtype(i)], shape=shape)) # save structure of the graph (all layers) into nodes nodes = [] # save all weights into initializer initializer = [] for i, layer in enumerate(model.layers): # the InputLayer only contain input data of model if isinstance(layer, InputLayer): continue else: handler = cls.switch_onnx_node_creater(layer) graph_input_list, weight_list, node_list = handler(layer) nodes.extend(node_list) graph_inputs.extend(graph_input_list) initializer.extend(weight_list) return make_graph(nodes=nodes, name=name, inputs=graph_inputs, outputs=graph_outputs, initializer=initializer)
im_l = im_l[..., 0:3] im_input = im_l / 255.0 im_input = np.transpose(im_input, (2, 0, 1)) im_input = im_input[np.newaxis, ...] im_input = torch.from_numpy(im_input).float() if opt.cuda: model = model.cuda() im_input = im_input.cuda() with torch.no_grad(): out_c, out_s, out_p = model(im_input) out_c, out_s, out_p = out_c.cpu(), out_s.cpu(), out_p.cpu() out_img_c = out_c.detach().numpy().squeeze() out_img_c = utils.convert_shape(out_img_c) out_img_s = out_s.detach().numpy().squeeze() out_img_s = utils.convert_shape(out_img_s) out_img_p = out_p.detach().numpy().squeeze() out_img_p = utils.convert_shape(out_img_p) if opt.isHR: if opt.only_y is True: im_label = utils.quantize(sc.rgb2ycbcr(im_gt)[:, :, 0]) im_pre = utils.quantize(sc.rgb2ycbcr(out_img_c)[:, :, 0]) else: im_label = im_gt im_pre = out_img_c
def make_symbolic_weights(cls, name, weights): return make_tensor_value_info( name=name, elem_type=NP_TYPE_TO_TENSOR_TYPE[weights.dtype], shape=convert_shape(weights.shape))
def make_weights(cls, name, weight): return make_tensor(name=name, data_type=NP_TYPE_TO_TENSOR_TYPE[weight.dtype], dims=convert_shape(weight.shape), vals=weight.flatten().tolist())