def collect_data_dict(data_d, shard_outs): for batch in shard_outs.batches: for named_tensor in batch.results: data_d.setdefault(named_tensor.edge_info.name, []).append( utils.tensor_pb_to_array(named_tensor.data, np.float32))
def _get_feed_dict(self, sess, unknown_dim_size): # Create feed dict with random data feed_dict = {} for inp in self._input_tensor_names: real_name, port, _ = self.get_node_name_and_output_index(inp) # This shape might have multiple unknown dimensions, so we # don't update batch_dim_indx here shape = self.tf_tensorshape_to_lgf_tensorshape( sess.graph.get_tensor_by_name(inp).shape, update_batch_dim_indx=False) # Correct for batch size if necessary corrected_edge_info = lgf_pb2.EdgeInfo() corrected_edge_info.CopyFrom(self._input_edges[(real_name, port)]) batch_dim_indx = corrected_edge_info.shape.batch_dim_indx if (batch_dim_indx >= 0 and len(shape.d) > 0 and shape.d[batch_dim_indx] > 0): corrected_edge_info.shape.d[batch_dim_indx] = shape.d[ batch_dim_indx] data = utils.generate_random_inference_inputs( [corrected_edge_info], unknown_dim_size=unknown_dim_size) named_tensor = data.inputs[0] feed_dict[sess.graph.get_tensor_by_name( inp)] = utils.tensor_pb_to_array( named_tensor.data, utils.dtype_pb_to_np_dtype(named_tensor.data.dtype)) return feed_dict
def update_quality_metrics(self, performance_data, test_outputs, labels): # Get the predictions predictions = [] for inf_out in test_outputs.batches: for named_tensor in inf_out.results: if named_tensor.edge_info.name.startswith( self.prediction_edge()): predictions.append( utils.tensor_pb_to_array(named_tensor.data, np.float32)) predictions = np.concatenate(predictions, axis=0) # Format the arrays num_samples = min(labels.shape[0], predictions.shape[0]) predictions = predictions[:num_samples] predictions = predictions.reshape(num_samples, -1) labels = labels[:num_samples] labels = labels.reshape(num_samples, 1) # Calculate top k accuracy for each value of k self._total += num_samples for k in self.k_list(): self._correct[k] += np.sum( labels == self.get_top_k_predictions(predictions, k)) performance_data.quality_metrics.metrics[TOP_K_FORMAT.format( k)] = self._correct[k] / self._total
def inference_to_obj_det_results(self, inf_out, test_images_sizes, start_img): results = [] for named_tensor in inf_out.results: if named_tensor.edge_info.name.startswith(self.prediction_edge()): raw = utils.tensor_pb_to_array(named_tensor.data, np.float32) logging.info(raw.shape) for j in range(raw.shape[0]): label_ind = start_img + j # Post-processing to convert output boxes to detection results boxes = object_detection_utils.non_max_suppression( raw[j:j + 1], 0.5) detection_result = ObjDetResult(label_ind) for det_cls, bboxs in boxes.items(): for box, score in bboxs: new_box = object_detection_utils.\ convert_to_original_size( box, np.array(self.original_image_size()), test_images_sizes[label_ind], True ) detection_result.add_box( BoundingBox(det_cls, score, new_box)) results.append(detection_result) continue return results
def get_tensor_data_from_const_node(node): # Fetches content to display on node hover if node.HasField(lgf_pb2.LNF.const.DESCRIPTOR.name): tensor_pb = node.const.value elif node.HasField(lgf_pb2.LNF.variable.DESCRIPTOR.name): tensor_pb = node.variable.const.value else: return None # Converting numpy_array to python array to make it JSON serializable array_with_zeroes = utils.tensor_pb_to_array(tensor_pb, np.float64).flatten() if np.count_nonzero(array_with_zeroes) == 0: return [[len(array_with_zeroes)], 0] array_without_zeroes = array_with_zeroes[np.nonzero(array_with_zeroes)] max_abs_val = np.max(np.abs(array_without_zeroes)) counts, _ = np.histogram( array_without_zeroes, bins=20 if len(array_without_zeroes) < 4096 else 4096, range=(-max_abs_val, max_abs_val)) # All histograms already present inside the graph have directory number as -1 return [ counts.tolist(), np.float64(max_abs_val), len(array_with_zeroes) - len(array_without_zeroes), -1 ]
def _get_phases_and_dequant_scales(self, new_opu_node, light_graph, phasify_node, phasify_subgraph_nodes, transform_result): # Create a subgraph subgraph = lgf_graph.LightGraph(phasify_subgraph_nodes, output_edges=phasify_node.outputs) # Run the graph subgraph_inputs = utils.create_inference_inputs([], []) runner = graph_runner.GraphRunner(subgraph, self._hw_specs, self._sw_config, self._sim_params) out_inf = runner.run_single_batch(subgraph_inputs) # Get numpy arrays phasify_output_arrays = [ utils.tensor_pb_to_array(named_tensor.data, np.float32) for named_tensor in out_inf.results ] # Get the adc scales node adc_scales_node = light_graph.get_node_by_name(phasify_node.inputs[ lgf_pb2.PhasifyNode.ADC_SCALES_INPUT_INDEX].name) # Create constant nodes phases_node = self.create_const_node( phasify_output_arrays[lgf_pb2.PhasifyNode.PHASES_OUTPUT_INDEX], new_opu_node.name + "_phases", new_opu_node.inputs[lgf_pb2.MatMulNode.PHASES_INDEX].dtype, lgf_pb2.ConstNode.WEIGHTS) phases_node.const.weight_rows = self._get_weight_rows( light_graph, phasify_node) if not self._sw_config.disable_block_sparsity: self._add_block_sparsity( phases_node, phasify_output_arrays[lgf_pb2.PhasifyNode.PHASES_OUTPUT_INDEX]) dequant_scales_node = self.create_const_node( phasify_output_arrays[ lgf_pb2.PhasifyNode.DEQUANT_SCALES_OUTPUT_INDEX], new_opu_node.name + "_dequant_scales", new_opu_node.inputs[lgf_pb2.MatMulNode.DEQUANT_SCALES_INDEX].dtype, lgf_pb2.ConstNode.DEQUANT_SCALE) new_adc_scales_node = self.create_const_node( phasify_output_arrays[lgf_pb2.PhasifyNode.ADC_SCALES_OUTPUT_INDEX], adc_scales_node.name, new_opu_node.inputs[lgf_pb2.MatMulNode.ADC_SCALES_INDEX].dtype, adc_scales_node.const.const_type) # Update the new opu node inputs new_opu_node.inputs[lgf_pb2.MatMulNode.PHASES_INDEX].CopyFrom( phases_node.outputs[0]) new_opu_node.inputs[lgf_pb2.MatMulNode.DEQUANT_SCALES_INDEX].CopyFrom( dequant_scales_node.outputs[0]) new_opu_node.inputs[lgf_pb2.MatMulNode.ADC_SCALES_INDEX].CopyFrom( new_adc_scales_node.outputs[0]) transform_result.CopyFrom( self.create_transform_result( to_add=[phases_node, dequant_scales_node], to_replace=[new_adc_scales_node]))
def inference_out_pb_to_array_list(self, out_pb, output_order=None): if output_order is None: assert len(out_pb.results) == 1 return [ utils.tensor_pb_to_array( out_pb.results[0].data, utils.dtype_pb_to_np_dtype(out_pb.results[0].data.dtype)) ] array_dict = {} for named_tensor in out_pb.results: array_dict[named_tensor.edge_info.name + ":" + str( named_tensor.edge_info.port)] = utils.tensor_pb_to_array( named_tensor.data, utils.dtype_pb_to_np_dtype(named_tensor.data.dtype)) return [array_dict[name] for name in output_order]
def inference_input_to_feed_dict(inf_input, graph): feed_dict = {} for named_tensor in inf_input.inputs: tf_tensor = TFSavedModelGraphRunner._tf_tensor_from_edge( named_tensor.edge_info, graph) array = utils.tensor_pb_to_array( named_tensor.data, utils.dtype_pb_to_np_dtype(named_tensor.data.dtype)) feed_dict[tf_tensor] = array return feed_dict
def transform(self, offset_node, light_graph): """ Apply mean correction. """ if offset_node.name not in self._corrections: raise ValueError("No correction for node {0}".format(offset_node.name)) orig_arr = utils.tensor_pb_to_array(offset_node.const.value, np.float32) new_arr = orig_arr - self._corrections[offset_node.name] offset_node.const.value.CopyFrom( utils.array_to_tensor_pb(new_arr, offset_node.const.value.dtype)) return self.create_transform_result(to_replace=[offset_node])
def inference_to_obj_det_results(self, inf_out, test_images_sizes, start_img): results = [] arrays = {} BOXES = "detection_boxes" CLASSES = "detection_classes" SCORES = "detection_scores" NUM = "num_detections" for named_tensor in inf_out.results: arrays[named_tensor.edge_info.name] = utils.tensor_pb_to_array( named_tensor.data, utils.dtype_pb_to_np_dtype(named_tensor.edge_info.dtype)) orig_size = self.original_image_size() for j in range(arrays[BOXES].shape[0]): label_ind = start_img + j detection_result = model_quality.ObjDetResult(label_ind) for i in range(int(arrays[NUM][j])): box = arrays[BOXES][j, i, :] # scale to size in pixels new_box = np.array([ box[0] * orig_size[0], box[1] * orig_size[1], box[2] * orig_size[0], box[3] * orig_size[1] ]) new_box = object_detection_utils.convert_to_original_size( new_box, np.array(self.original_image_size()), test_images_sizes[label_ind], True) detection_result.add_box( model_quality.BoundingBox(arrays[CLASSES][j, i], arrays[SCORES][j, i], new_box)) results.append(detection_result) return results
def _get_dequant_bias(self, new_opu_node, light_graph, transform_result): # Create a dequant bias with all 0's _, num_y, _, j = new_opu_node.inputs[ lgf_pb2.MatMulNode.DEQUANT_SCALES_INDEX].shape.d zero_dequant_bias_node = self.create_const_node( np.zeros([1, num_y, 1, j]), new_opu_node.name + "_dequant_bias", self._sw_config.float_type, lgf_pb2.ConstNode.DEQUANT_BIAS) # Add dequant bias to the new opu node new_opu_node.inputs.add().CopyFrom(zero_dequant_bias_node.outputs[0]) # Get constant nodes from the transform result const_nodes = [ t.node for t in list(transform_result.to_add) + list(transform_result.to_replace) if t.node.HasField(lgf_pb2.LNF.const.DESCRIPTOR.name) ] const_nodes.append( light_graph.get_node_by_name(new_opu_node.inputs[ lgf_pb2.MatMulNode.QUANT_PARAMS_INDEX].name)) # Create a subgraph to run the new opu node subgraph = lgf_graph.LightGraph( const_nodes + [new_opu_node, zero_dequant_bias_node], input_edges=[new_opu_node.inputs[lgf_pb2.MatMulNode.INPUT_INDEX]], output_edges=[new_opu_node.outputs[0]]) # Create zero inputs input_edge = subgraph.input_edges()[0] batch_dilation_factor = (input_edge.shape.batch_dilation_factor if input_edge.shape.batch_dilation_factor > 0 else 1) array = np.zeros([ d if d != -1 else self._sim_params.compiled_batch_size * batch_dilation_factor for d in input_edge.shape.d ]) zero_inputs = utils.create_inference_inputs([input_edge], [array]) # Run zeros through the subgraph runner = graph_runner.GraphRunner(subgraph, self._hw_specs, self._sw_config, self._sim_params) out_inf = runner.run_single_batch(zero_inputs) # New bias is chosen so the outputs of a zero input will be exactly zero dequant_bias = -1 * utils.tensor_pb_to_array(out_inf.results[0].data, np.float32) # Convert to a [1, last_dim] vector dequant_bias = np.reshape(dequant_bias, [-1, dequant_bias.shape[-1]]) dequant_bias = dequant_bias[0:1, :] # Pad and reshape so dequant_bias is [1, num_y, 1, j] pad = [[0, 0], [0, num_y * j - dequant_bias.shape[1]]] dequant_bias = np.pad(dequant_bias, pad, "constant", constant_values=0) dequant_bias = np.split(dequant_bias, num_y, axis=1) dequant_bias = np.stack(dequant_bias, axis=0) dequant_bias = np.reshape(dequant_bias, [1, num_y, 1, j]) # Create a dequant bias node and add to the transform result dequant_bias_node = self.create_const_node( dequant_bias, zero_dequant_bias_node.name, zero_dequant_bias_node.outputs[0].dtype, zero_dequant_bias_node.const.const_type) transform_result.to_add.add().node.CopyFrom(dequant_bias_node)