def read_nnef_tensor(filename): import nnef with open(filename, "rb") as file: if NNEF_READ_PROVISIONAL: # noinspection PyProtectedMember return nnef._read_tensor_provisional(file) else: return nnef.read_tensor(file)[0]
def read_input(file, name, shape, dtype, transpose): data = nnef.read_tensor(file) any_batch = shape[0] == 0 offset = int(any_batch) if tuple(data.shape[offset:]) != tuple(shape[offset:]): raise ValueError( "Mismatch between declared and read shape for input '{}'; {} vs {}" .format(name, data.shape, shape)) if data.dtype != dtype: raise ValueError( "Mismatch between declared and read dtype for input '{}'; {} vs {}" .format(name, data.dtype, dtype)) return transpose_channels_first_to_last(data) if transpose else data
def main(): np.set_printoptions(precision=3, suppress=True, floatmode='fixed', threshold=sys.maxsize) args = get_args() if args.tensor_path is None: utils.set_stdin_to_binary() if sys.stdin.isatty(): print("No input provided!", file=sys.stderr) exit(1) tensor = nnef.read_tensor(sys.stdin) else: tensor = read_nnef_tensor(args.tensor_path) print('Shape:', list(tensor.shape)) if int(args.k) <= 0: print(tensor) else: axis = int(args.axis) k = min(int(args.k), tensor.shape[axis]) if axis >= len(tensor.shape) or axis < -len(tensor.shape): print("axis={} is outside the supported range for this tensor.". format(axis)) exit(1) values, indices = topk(tensor, axis=axis, k=k) values = np.squeeze(values) indices = np.squeeze(indices) print("TopK({}, k={}, axis={}):\nValues:\n{}\nIndices:\n{}".format( tensor_name(args.tensor_path) if args.tensor_path is not None else "stdin", k, axis, values, indices))
def read_nnef_tensor(filename): with open(filename, "rb") as file: return nnef.read_tensor(file)[0]
def create_input(input_source, np_dtype, shape, allow_bigger_batch=False): assert isinstance(input_source, (DefaultInput, RandomInput, ImageInput, NNEFTensorInput)) np_dtype = np.dtype(np_dtype) if isinstance(input_source, DefaultInput): if 'float' in np_dtype.name: input_source = RandomInput('normal', 0.0, 1.0) elif 'int' in np_dtype.name: input_source = RandomInput('binomial', 255, 0.5) elif 'bool' == np_dtype.name: input_source = RandomInput('bernoulli', 0.5) else: raise utils.NNEFToolsException( "Random does not support this dtype: {}".format(np_dtype.name)) if isinstance(input_source, RandomInput): if input_source.algo == 'uniform': if 'float' in np_dtype.name: return np.random.uniform(input_source.args[0], input_source.args[1], shape).astype(np_dtype) elif 'int' in np_dtype.name: return np.random.randint( int(math.ceil(input_source.args[0])), int(math.floor(input_source.args[1])) + 1, shape, np_dtype) else: raise Exception( "Random 'uniform' can not be applied to: {}".format( np_dtype.name)) elif input_source.algo == 'normal': if 'float' in np_dtype.name: return np.random.normal(input_source.args[0], input_source.args[1], shape).astype(np_dtype) else: raise Exception( "Random 'normal' can not be applied to: {}".format( np_dtype.name)) elif input_source.algo == 'binomial': if 'int' in np_dtype.name: return np.random.binomial(input_source.args[0], input_source.args[1], shape).astype(np_dtype) else: raise Exception( "Random 'normal' can not be applied to: {}".format( np_dtype.name)) elif input_source.algo == 'bernoulli': if 'bool' == np_dtype.name: return np.random.uniform(0.0, 1.0, shape) <= input_source.args[0] else: raise Exception( "Random 'bernoulli' can not be applied to: {}".format( np_dtype.name)) else: assert False elif isinstance(input_source, ImageInput): import skimage import skimage.io import skimage.color import skimage.transform assert shape is None or len( shape) == 4, "ImageInput can only produce tensors with rank=4" assert input_source.data_format.upper() in [ ImageInput.DATA_FORMAT_NCHW, ImageInput.DATA_FORMAT_NHWC ] assert input_source.color_format.upper() in [ ImageInput.COLOR_FORMAT_RGB, ImageInput.COLOR_FORMAT_BGR ] imgs = [] for pattern in input_source.filenames: filenames = sorted(glob.glob(os.path.expanduser(pattern))) assert filenames, "No files found for path: {}".format(pattern) for filename in filenames: target_size = None if shape is not None: if input_source.data_format.upper( ) == ImageInput.DATA_FORMAT_NCHW: if shape[1] != 3: raise utils.NNEFToolsException( 'NCHW image is specified as input, but channel dimension of input tensor is not 3.' ) target_size = [shape[2], shape[3]] else: if shape[3] != 3: raise utils.NNEFToolsException( 'NHWC image is specified as input, but channel dimension of input tensor is not 3.' ) target_size = [shape[1], shape[2]] img = skimage.img_as_ubyte(skimage.io.imread(filename)) if len(img.shape) == 2: img = skimage.color.gray2rgb(img) img = img.astype(np.float32) if input_source.color_format.upper( ) == ImageInput.COLOR_FORMAT_RGB: img = img[..., (0, 1, 2)] # remove alpha channel if present else: img = img[..., (2, 1, 0)] if input_source.range: min_ = np.array(input_source.range[0], dtype=np.float32) max_ = np.array(input_source.range[1], dtype=np.float32) scale = (max_ - min_) / 255.0 bias = min_ img = img * scale + bias if input_source.norm: mean = np.array(input_source.norm[0], dtype=np.float32) std = np.array(input_source.norm[1], dtype=np.float32) img = (img - mean) / std if target_size is not None: img = skimage.transform.resize(img, target_size, preserve_range=True, anti_aliasing=True, mode='reflect') img = img.astype(np_dtype) if input_source.data_format.upper( ) == ImageInput.DATA_FORMAT_NCHW: img = img.transpose((2, 0, 1)) img = np.expand_dims(img, 0) imgs.append(img) if shape is not None and len(imgs) < shape[0]: print( "Info: Network batch size bigger than supplied data, repeating it", file=sys.stderr) imgs = imgs * ((shape[0] + len(imgs) - 1) // len(imgs)) imgs = imgs[:shape[0]] assert len(imgs) == shape[0] assert shape is None or len(imgs) == shape[0] or allow_bigger_batch if not all(img.shape == imgs[0].shape for img in imgs): raise utils.NNEFToolsException( "The size of all images must be the same, or --size must be specified" ) return np.concatenate(tuple(imgs), 0) elif isinstance(input_source, NNEFTensorInput): with open(input_source.filename) as f: return nnef.read_tensor(f)[0] else: assert False
def run_using_argv(argv): try: args = get_args(argv) write_outputs = args.output_names is None or args.output_names if args.input is None: if sys.stdin.isatty(): raise utils.NNEFToolsException("No input provided!") utils.set_stdin_to_binary() if write_outputs: if args.output is None: if sys.stdout.isatty(): raise utils.NNEFToolsException("No output provided!") utils.set_stdout_to_binary() parent_dir_of_input_model = os.path.dirname( utils.path_without_trailing_separator(args.network)) tmp_dir = None if args.network.endswith('.tgz'): nnef_path = tmp_dir = tempfile.mkdtemp( prefix="nnef_", dir=parent_dir_of_input_model) utils.tgz_extract(args.network, nnef_path) else: nnef_path = args.network try: parser_configs = NNEFParserConfig.load_configs( args.custom_operations, load_standard=True) # read without weights reader = nnef_io.Reader(parser_configs=parser_configs, infer_shapes=False) graph = reader( os.path.join(nnef_path, 'graph.nnef') if os.path. isdir(nnef_path) else nnef_path) if args.input is None: inputs = tuple( nnef.read_tensor(sys.stdin) for _ in range(len(graph.inputs))) elif len(args.input) == 1 and os.path.isdir(args.input[0]): inputs = tuple( nnef_io.read_nnef_tensor( os.path.join(args.input[0], tensor.name + '.dat')) for tensor in graph.inputs) else: inputs = tuple( nnef_io.read_nnef_tensor(path) for path in args.input) reader = nnef_io.Reader(parser_configs=parser_configs, input_shape=tuple( list(input.shape) for input in inputs)) graph = reader(nnef_path) tensor_hooks = [] stats_hook = None if args.stats: stats_hook = backend.StatisticsHook() tensor_hooks.append(stats_hook) if write_outputs and args.output_names is not None: if '*' in args.output_names: tensor_hooks.append( backend.ActivationExportHook( tensor_names=[ t.name for t in graph.tensors if not t.is_constant and not t.is_variable ], output_directory=args.output)) else: tensor_hooks.append( backend.ActivationExportHook( tensor_names=args.output_names, output_directory=args.output)) if args.permissive: backend.try_to_fix_unsupported_attributes(graph) outputs = backend.run(nnef_graph=graph, inputs=inputs, device=args.device, custom_operations=get_custom_runners( args.custom_operations), tensor_hooks=tensor_hooks) if write_outputs and args.output_names is None: if args.output is None: for array in outputs: nnef.write_tensor(sys.stdout, array) else: for tensor, array in zip(graph.outputs, outputs): nnef_io.write_nnef_tensor( os.path.join(args.output, tensor.name + '.dat'), array) if stats_hook: if args.stats.endswith('/') or args.stats.endswith('\\'): stats_path = os.path.join(nnef_path, args.stats, 'graph.stats') else: stats_path = os.path.join(nnef_path, args.stats) stats_hook.save_statistics(stats_path) if tmp_dir and (args.stats and _is_inside(nnef_path, args.stats)): if args.network.endswith('.tgz'): print("Info: Changing input archive", file=sys.stderr) shutil.move(args.network, args.network + '.nnef-tools-backup') utils.tgz_compress(dir_path=nnef_path, file_path=args.network) os.remove(args.network + '.nnef-tools-backup') else: output_path = args.network.rsplit('.', 1)[0] + '.nnef.tgz' backup_path = output_path + '.nnef-tools-backup' if os.path.exists(output_path): shutil.move(output_path, backup_path) utils.tgz_compress(dir_path=nnef_path, file_path=output_path) if os.path.exists(backup_path): os.remove(backup_path) finally: if tmp_dir: shutil.rmtree(tmp_dir) except utils.NNEFToolsException as e: print("Error: " + str(e), file=sys.stderr) exit(1) except nnef.Error as e: print("Error: " + str(e), file=sys.stderr) exit(1)
def create_input(input_source, np_dtype, shape): assert isinstance(input_source, (RandomInput, ImageInput, NNEFTensorInput)) np_dtype = np.dtype(np_dtype) if isinstance(input_source, RandomInput): if 'float' in np_dtype.name: assert input_source.float_min is not None and input_source.float_max is not None, \ "float_min or float_max is not set on the input source" return ((input_source.float_max - input_source.float_min) * np.random.random(shape) + input_source.float_min).astype(np_dtype) elif 'int' in np_dtype.name: assert input_source.int_min is not None and input_source.int_max is not None, \ "int_min or int_max is not set on the input source" return np.random.randint(low=input_source.int_min, high=input_source.int_max, size=shape, dtype=np_dtype) elif np_dtype.name == 'bool': assert input_source.true_prob is not None, "true_prob is not set on the input source" return np.random.random(shape) <= input_source.true_prob else: assert False, "Unsupported dtype: {}".format(np_dtype.name) elif isinstance(input_source, ImageInput): from matplotlib.image import imread from scipy.misc import imresize assert len( shape) == 4, "ImageInput can only produce tensors with rank=4" assert input_source.data_format.upper() in [ ImageInput.DATA_FORMAT_NCHW, ImageInput.DATA_FORMAT_NHWC ] assert input_source.color_format.upper() in [ ImageInput.COLOR_FORMAT_RGB, ImageInput.COLOR_FORMAT_BGR ] imgs = [] for filename in input_source.filenames: if input_source.data_format.upper() == ImageInput.DATA_FORMAT_NCHW: target_size = [shape[2], shape[3], shape[1]] else: target_size = [shape[1], shape[2], shape[3]] img = imread(filename) if input_source.color_format.upper( ) == ImageInput.COLOR_FORMAT_RGB: img = img[..., (0, 1, 2)] # remove alpha channel if present else: img = img[..., (2, 1, 0)] img = ((img.astype(np.float32) - np.array(input_source.sub)) / np.array(input_source.div)) img = imresize(img, target_size).astype(np_dtype) if input_source.data_format.upper() == ImageInput.DATA_FORMAT_NCHW: img = img.transpose((2, 0, 1)) img = np.expand_dims(img, 0) imgs.append(img) if len(imgs) < shape[0]: imgs = imgs * ((shape[0] + len(imgs) - 1) / len(imgs)) imgs = imgs[:shape[0]] assert len(imgs) == shape[0] return np.concatenate(a_tuple=tuple(imgs), axis=0) elif isinstance(input_source, NNEFTensorInput): with open(input_source.filename) as f: return nnef.read_tensor(f)[0] else: assert False