Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
]
inputs_to_op_nodes = list(
flatten([list(flatten(n.input)) for n in op_nodes])
)
inputs_to_op_nodes = replace_strings_in_list(
inputs_to_op_nodes, map_ignored_layer_to_its_input
)
inputs_to_op_nodes = [i.split(":")[0] for i in inputs_to_op_nodes]
const_nodes_by_name = {n.name: n for n in const_nodes}
tensors = []
for i in inputs_to_op_nodes:
if i in model_tensors:
src = model_tensors[i]
tensors += [
Struct(
name=i,
obj=src,
shape=get_tensor_dims(src),
data=get_tensor_data(src),
)
]
elif i in const_nodes_by_name:
src = const_nodes_by_name[i].attr["value"].tensor
tensors += [
Struct(
name=i,
obj=src,
shape=get_tensor_dims(src),
data=get_tensor_data(src),
)
]
]
inputs_to_op_nodes = list(
flatten([list(flatten(n.input)) for n in op_nodes])
)
inputs_to_op_nodes = replace_strings_in_list(
inputs_to_op_nodes, map_ignored_layer_to_its_input
)
inputs_to_op_nodes = [i.split(":")[0] for i in inputs_to_op_nodes]
const_nodes_by_name = {n.name: n for n in const_nodes}
tensors = []
for i in inputs_to_op_nodes:
if i in model_tensors:
src = model_tensors[i]
tensors += [
Struct(
name=i,
obj=src,
shape=get_tensor_dims(src),
data=get_tensor_data(src),
)
]
elif i in const_nodes_by_name:
src = const_nodes_by_name[i].attr["value"].tensor
tensors += [
Struct(
name=i,
obj=src,
shape=get_tensor_dims(src),
data=get_tensor_data(src),
)
]
"ResizeNearestNeighbor": lambda nodes, inputs, tensors, _: Struct(
op="ResizeNearestNeighbor",
input=[i for i in inputs],
ksize=[int(tensors[0].data[0]), int(tensors[0].data[1])]
if len(tensors) == 1 and len(tensors[0].data) == 2
else [int(tensors[-1].data[0]), int(tensors[-1].data[1])]
if len(tensors) >= 4 and len(tensors[-1].data) == 2
else [1, 1],
),
"Dense": lambda nodes, inputs, tensors, _: Struct(
op="Dense",
input=[i for i in inputs] + [t.name for t in tensors],
data_frmt=get_attr(
by_op(nodes, "Dense") or by_op(nodes, "MatMul"), "data_format"
),
def pool(self, op, x, out=""):
self.layers += [Struct(name=out, op=op + "Pool", input=[x])]
return self._patch_last_layer_name_and_return()
else:
activation = "Linear"
if class_name not in known_classes:
if class_name in requires_runtime_flag:
print("SKIP:", class_name, "layer is used only for training")
else:
print("IGNORED:", class_name, "unknown layer")
map_ignored_layer_to_its_input[name] = inputs
return
klass = known_classes[class_name]
if type(klass) == int:
klass = Struct(id=klass)
o_l = Struct()
o_l.type = klass.id
o_l.class_name = class_name
o_l.name = name
auto_pad = get_attr(layer, "padding") # layer.attr['padding'].s.decode("utf-8")
pads = get_attr(layer, "pads")
strides = get_attr(layer, "strides") # layer.attr['strides'].list.i
pool_size = get_attr(layer, "ksize") # layer.attr['ksize'].list.i
shape = get_attr(layer, "shape")
starts = get_attr(layer, "starts")
ends = get_attr(layer, "ends")
slice_strides = get_attr(layer, "slice_strides")
rank = get_attr(layer, "rank") or get_layer_rank(layer)
data_frmt = get_attr(
layer, "data_format"
) # layer.attr['data_format'].s.decode("utf-8")
"Multinomial": lambda nodes, inputs, tensors, _: Struct(
op="Multinomial",
input=inputs,
shape=[int(by_name(tensors, "/num_samples").data[0])],
# seed = get_attr(nodes[0], 'seed'),
known_paddings.get(auto_pad) if auto_pad else pads or starts or [0, 0, 0, 0]
)
o_l.strides = strides_to_HW(strides, data_frmt) if strides else slice_strides or []
o_l.pool_size = (
pool_to_HW(pool_size, data_frmt) if pool_size else ends or shape or []
)
o_l.axis = embody(axis, default=-1)
o_l.alpha = embody(alpha, default=1)
o_l.beta = beta or 0
o_l.rank = (
-1
) # default initialization, actual value will be set later on in this function
tensor_names = [i for i in inputs if i in model_tensors]
o_l.tensors = [
Struct(
name=x,
shape=get_tensor_dims(model_tensors[x]),
data=get_tensor_data(model_tensors[x]),
)
for x in tensor_names
]
# Patch shapes & data
layer_has_model_tensors = len(o_l.tensors) > 0
if hasattr(klass, "out_shapes") and layer_has_model_tensors:
shapes = klass.out_shapes([x.shape for x in o_l.tensors])
# if we have more shapes than actual tensors,
# then create & fill missing tensors with zeros
in_tensor_num = len(o_l.tensors)
for index, new_shape in enumerate(shapes):
if index >= in_tensor_num:
"Dense": lambda nodes, inputs, tensors, _: Struct(
op="Dense",
input=[i for i in inputs] + [t.name for t in tensors],
data_frmt=get_attr(
by_op(nodes, "Dense") or by_op(nodes, "MatMul"), "data_format"
),
def tanh(self, x, out=""):
self.layers += [Struct(name=out, op="Tanh", input=[x])]
return self._patch_last_layer_name_and_return()