Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for test_file in os.listdir(data_dir):
case_dir = os.path.join(data_dir, test_file)
# skip the non-dir files
if not os.path.isdir(case_dir):
continue
input_file = os.path.join(case_dir, 'input_0.pb')
input_tensor = TensorProto()
with open(input_file, 'rb') as proto_file:
input_tensor.ParseFromString(proto_file.read())
inputs.append(numpy_helper.to_array(input_tensor))
output_tensor = TensorProto()
output_file = os.path.join(case_dir, 'output_0.pb')
with open(output_file, 'rb') as proto_file:
output_tensor.ParseFromString(proto_file.read())
outputs.append(numpy_helper.to_array(output_tensor))
return model_path, inputs, outputs
def numpy_to_pb(name, np_data, out_filename):
tensor = numpy_helper.from_array(np_data, name)
onnx.save_tensor(tensor, out_filename)
model_onnx = onnx.load('../test/tiny_yolov2/Model.onnx')
test_data_dir = '../test/tiny_yolov2/test_data_set_0'
inputs = []
inputs_num = len(glob.glob(os.path.join(test_data_dir, 'input_*.pb')))
for i in range(inputs_num):
input_file = os.path.join(test_data_dir, 'input_{}.pb'.format(i))
tensor = onnx.TensorProto()
with open(input_file, 'rb') as f:
tensor.ParseFromString(f.read())
inputs.append(numpy_helper.to_array(tensor))
"""
for i in range(inputs_num):
input_file = os.path.join(test_data_dir, 'input_{}.pb'.format(i))
inputs.append(onnx.load_tensor(input_file))
"""
# For some models this string has to match the input to the model
inputDict = {
"image": inputs[0]
}
outputs_list = []
for out in enumerate_model_node_outputs(model_onnx):
def test_convert_bipolar_matmul_to_xnorpopcountmatmul():
lfc = LFC(weight_bit_width=1, act_bit_width=1, in_bit_width=1)
checkpoint = torch.load(trained_lfc_checkpoint, map_location="cpu")
lfc.load_state_dict(checkpoint["state_dict"])
bo.export_finn_onnx(lfc, (1, 1, 28, 28), export_onnx_path)
model = ModelWrapper(export_onnx_path)
model = model.transform(InferShapes())
model = model.transform(FoldConstants())
model = model.transform(GiveUniqueNodeNames())
model = model.transform(GiveReadableTensorNames())
model = model.transform(ConvertSignToThres())
# load one of the test vectors
raw_i = get_data("finn", "data/onnx/mnist-conv/test_data_set_0/input_0.pb")
input_tensor = onnx.load_tensor_from_string(raw_i)
# run using FINN-based execution
input_dict = {"global_in": nph.to_array(input_tensor)}
expected_ctx = oxe.execute_onnx(model, input_dict, True)
expected = expected_ctx[model.graph.output[0].name]
model = model.transform(ConvertBipolarMatMulToXnorPopcount())
produced_ctx = oxe.execute_onnx(model, input_dict, True)
produced = produced_ctx[model.graph.output[0].name]
assert np.isclose(expected, produced, atol=1e-3).all()
os.remove(export_onnx_path)
def onnx_attribute_to_dict(onnx_attr):
"""
Parse ONNX attribute
:param onnx_attr: ONNX attribute
:return: Python data type
"""
if onnx_attr.HasField('t'):
return numpy_helper.to_array(getattr(onnx_attr, 't'))
for attr_type in ['f', 'i', 's']:
if onnx_attr.HasField(attr_type):
return getattr(onnx_attr, attr_type)
for attr_type in ['floats', 'ints', 'strings']:
if getattr(onnx_attr, attr_type):
return list(getattr(onnx_attr, attr_type))
return {arg.name: onnx_attribute_to_dict(arg) for arg in args}
def quantitize_graph(g, verbose=False):
"""Quantitize graph."""
new_weights = []
quantitized_weights = []
nodes = []
remap = {}
remove = []
for i, w in enumerate(g.initializer):
# only quantitize float32
if w.data_type != onnx_pb.TensorProto.FLOAT:
continue
w_np = numpy_helper.to_array(w)
# only look at sizes >= 32 elements
if w_np.size < 32:
continue
# weights we want to quantitize
remove.append(i)
name = w.name
if verbose:
logger.info("quantitizing %s", name)
w_quant, zp, scale = eight_bit_quantitize(w_np)
nw = numpy_helper.from_array(w_quant, name=name)
if verbose:
w_dequant = eight_bit_dequantitize(w_quant, zp, scale)
rtol = np.abs(w_dequant - w_np)
s = {}
for j in [1.0, 5.0, 10.0, 20.0]:
def to_array(self): # type: () -> numpy.ndarray
"""
Get the value of this tensor as a Numpy array.
"""
return onnx.numpy_helper.to_array(self._proto)
def equalAndThen(self, x, y, msg, k):
"""
Helper for implementing 'requireEqual' and 'checkEqual'. Upon failure,
invokes continuation 'k' with the error message.
"""
if isinstance(x, onnx.TensorProto) and isinstance(y, onnx.TensorProto):
self.equalAndThen(x.name, y.name, msg, k)
# Use numpy for the comparison
t1 = onnx.numpy_helper.to_array(x)
t2 = onnx.numpy_helper.to_array(y)
new_msg = "{}In embedded parameter '{}'".format(colonize(msg), x.name)
self.equalAndThen(t1, t2, new_msg, k)
elif isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
try:
np.testing.assert_equal(x, y)
except AssertionError as e:
k("{}{}".format(colonize(msg, ": "), str(e).lstrip()))
else:
if x != y:
# TODO: Better algorithm for lists
sx = str(x)
sy = str(y)
if len(sx) > 40 or len(sy) > 40 or '\n' in sx or '\n' in sy:
# long form
l = "=" * 50
def _constant(op, graph, tensors, initializer):
inputs = _get_inputs(op, graph, tensors, initializer)
attrs = _parse_attribute(op.attribute)
# TODO: Currently do not support sparse value
assert "value" in attrs, "Do not support sparse value for Constant"
tensor = attrs["value"]
dims = list()
for dim in tensor.dims:
dims.append(dim)
weight_data = numpy_helper.to_array(tensor)
outputs = graph.new_weight(dims=tuple(dims), data=weight_data)
return outputs
def _get_pad_from_Pad(node):
if len(node.origin.input) == 1:
pads = node.get_attribute('pads')
else:
pad_tensor = node.get_precedence_by_idx(1)
if pad_tensor is None:
pads = numpy_helper.to_array(node.initializers[0]).tolist()
else:
pads = numpy_helper.to_array(node.get_precedence_by_idx(1).tensors[0]).tolist()
return pads
def tensor2list(onnx_tensor):
# Use the onnx.numpy_helper because the data may be raw
return numpy_helper.to_array(onnx_tensor).flatten().tolist()
tensors = [(init.name, {'name': init.name,