def search(self, state: np.ndarray, time_limit: float=None, max_states: int=None) -> (np.ndarray, bool): time_limit, max_states = self.reset(time_limit, max_states) self.tt.tick() if cube.is_solved(state): return True # Each element contains the state from which it came and the action taken to get to it self.states = { state.tostring(): (None, None) } queue = deque([state]) while self.tt.tock() < time_limit and len(self) < max_states: state = queue.popleft() tstate = state.tostring() for i, action in enumerate(cube.action_space): new_state = cube.rotate(state, *action) new_tstate = new_state.tostring() if new_tstate in self.states: continue elif cube.is_solved(new_state): self.action_queue.appendleft(i) while self.states[tstate][0] is not None: self.action_queue.appendleft(self.states[tstate][1]) tstate = self.states[tstate][0] return True else: self.states[new_tstate] = (tstate, i) queue.append(new_state) return False
def make_node(self, state: np.ndarray, to_play: BoardPiece): """ Create a node (usually root node) from a given state if it doesn't exist in self.nodes. :param state: :param to_play: which player's turn it is :return: """ if (hash(state.tostring()) + hash(to_play)) not in self.nodes: self.nodes[hash(state.tostring()) + hash(to_play)] = MonteCarloNode(state, to_play)
def write_preprocessed_target_data(id: int, spec: np.ndarray, mel: np.ndarray, filename: str): raw_spec = spec.tostring() raw_mel = mel.tostring() example = tf.train.Example(features=tf.train.Features( feature={ 'id': int64_feature([id]), 'spec': bytes_feature([raw_spec]), 'spec_width': int64_feature([spec.shape[1]]), 'mel': bytes_feature([raw_mel]), 'mel_width': int64_feature([mel.shape[1]]), 'target_length': int64_feature([len(mel)]), })) write_tfrecord(example, filename)
def write_preprocessed_mgc_lf0_data(index: int, key: str, mgc: np.ndarray, lf0: np.ndarray, filename: str): raw_mgc = mgc.tostring() raw_lf0 = lf0.tostring() example = tf.train.Example(features=tf.train.Features( feature={ 'id': int64_feature([index]), 'key': bytes_feature([key.encode('utf-8')]), 'mgc': bytes_feature([raw_mgc]), 'mgc_width': int64_feature([mgc.shape[1]]), 'lf0': bytes_feature([raw_lf0]), 'target_length': int64_feature([len(mgc)]), })) write_tfrecord(example, filename)
def write_preprocessed_data(id: int, key: str, waveform: np.ndarray, mel: np.ndarray, text: str, filename: str): raw_waveform = waveform.tostring() raw_mel = mel.tostring() example = tf.train.Example(features=tf.train.Features(feature={ 'id': int64_feature([id]), 'key': bytes_feature([key.encode('utf-8')]), 'waveform': bytes_feature([raw_waveform]), 'waveform_length': int64_feature([len(waveform)]), 'mel': bytes_feature([raw_mel]), 'mel_length': int64_feature([len(mel)]), 'mel_width': int64_feature([mel.shape[1]]), 'text': bytes_feature([text.encode('utf-8')]), })) write_tfrecord(example, filename)
def search(self, state: np.ndarray, time_limit: float=None, max_states: int=None) -> bool: """Seaches according to the batched, weighted A* algorithm While there is time left, the algorithm finds the best `expansions` open states (using priority queue) with lowest cost according to the A* cost heuristic (see `self.cost`). From these, it expands to new open states according to `self.expand_batch`. """ self.tt.tick() time_limit, max_states = self.reset(time_limit, max_states) if cube.is_solved(state): return True #First node self.indices[state.tostring()], self.states[1], self.G[1] = 1, state, 0 heapq.heappush( self.open_queue, (0, 1) ) #Given cost 0: Should not matter; just to avoid np.empty weirdness while self.tt.tock() < time_limit and len(self) + self.expansions * cube.action_dim <= max_states: self.tt.profile("Remove nodes from open priority queue") n_remove = min( len(self.open_queue), self.expansions ) expand_idcs = np.array([ heapq.heappop(self.open_queue)[1] for _ in range(n_remove) ], dtype=int) self.tt.end_profile("Remove nodes from open priority queue") is_won = self.expand_batch(expand_idcs) if is_won: #🦀🦀🦀WE DID IT BOIS🦀🦀🦀 i = self.indices[ cube.get_solved().tostring() ] #Build action queue while i != 1: self.action_queue.appendleft( self.parent_actions[i] ) i = self.parents[i] return True return False
def add_state_mask_to_buffer(state_bytes: bytes, mask: np.ndarray, action_num: int): """ add state mask map to buffer """ state_mask_mapper[state_bytes] = mask.tostring() state_action_num_mapper[state_bytes] = action_num
def simple_convert_to_example(logmel_feats: np.ndarray): raw_mel = logmel_feats.tostring() example = tf.train.Example(features=tf.train.Features( feature={ 'LogMel_Features': bytes_feature([raw_mel]), })) return example
def write_preprocessed_source_data2(id: int, text1: str, source1: np.ndarray, text2: str, source2: np.ndarray, filename: str): raw_source1 = source1.tostring() raw_source2 = source2.tostring() example = tf.train.Example(features=tf.train.Features( feature={ 'id': int64_feature([id]), 'text': bytes_feature( [text1.encode('utf-8'), text2.encode('utf-8')]), 'source': bytes_feature([raw_source1, raw_source2]), 'source_length': int64_feature([len(source1), len(source2)]), })) write_tfrecord(example, filename)
def sendDetectImg(self, img: np.ndarray): dist = img.tostring() self.client.send(dist) response = self.client.recv(4096) detections = pickle.loads(response) return detections
def serialize_example(video: np.ndarray, label: int): """Creates a tf.Example message ready to be written to a file.""" seq_len = video.shape[0] height = video.shape[1] width = video.shape[2] channels = video.shape[3] assert channels == 3, 'Only RGB video format is supported' # Transforms a Tensor into a serialized TensorProto proto. # TODO: Replaces to.string method from numpy with tf.io.serialize_tensor # Note: tf.io.serialize_tensor causes issues when using Keras's model.fit # ValueError: Cannot take the length of shape with unknown rank. # video_bytes = tf.io.serialize_tensor(video) video_bytes = video.tostring() # Create a dictionary mapping the feature name to the tf.Example-compatible # data type feature = { 'video': bytes_feature(video_bytes), 'label': int64_feature(label), 'seq_len': int64_feature(seq_len), 'height': int64_feature(height), 'width': int64_feature(width), 'channels': int64_feature(channels) } # Creates a Features message using tf.train.Example example_proto = tf.train.Example( features=tf.train.Features(feature=feature)) return example_proto.SerializeToString()
def write_test_examples(inputs: np.ndarray, label: np.ndarray, path: str, filename: str): filename = filename.split('/')[-1] if not os.path.exists(path): pathlib.Path(path).mkdir(parents=True, exist_ok=True) filename = path + "Scene" + filename + ".tfrecords" print(f"[writing_training_examples]: writing {filename}") with tf.io.TFRecordWriter(filename) as writer: height, width, _ = inputs.shape print(f"[writing_training_examples]: {height} {width}") inputs_bytes = inputs.tostring() label_bytes = label.tostring() example = serialize_test_example(height, width, inputs_bytes, label_bytes) writer.write(example)
def _expand_from(self, index: np.ndarray, width: float): elements = [] densities = [] origo = np.zeros(2) queue = Queue() queue.put((origo, index)) directions = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]]) seen_set = set() seen_set.add(index.tostring()) while not queue.empty(): position, index = queue.get() density = self._gaussian_kernel(origo, position) elements.append(self.element_index_matrix[index[0]][index[1]]) densities.append(density) for d in directions: next_index = index + d if self.__within_bounds(next_index) \ and next_index.tostring() not in seen_set \ and self._euclidean_distance(position, origo) <= width: queue.put((position + d, next_index)) seen_set.add(next_index.tostring()) return elements, densities
def search(self, state: np.ndarray, time_limit: float=None, max_states: int=None) -> bool: time_limit, max_states = self.reset(time_limit, max_states) self.tt.tick() self.indices[state.tostring()] = 1 self.states[1] = state if cube.is_solved(state): return True oh = cube.as_oh(state) p, v = self.net(oh) self.P[1] = p.softmax(dim=1).cpu().numpy() self.V[1] = v.cpu().numpy() indices_visited = [1] actions_taken = [] while self.tt.tock() < time_limit and len(self) + cube.action_dim <= max_states: self.tt.profile("Expanding leaves") solve_leaf_index, solve_action = self.expand_leaf(indices_visited, actions_taken) self.tt.end_profile("Expanding leaves") # If a solution is found if solve_leaf_index != -1: self.action_queue = deque(actions_taken) + deque([solve_action]) if self.search_graph: self._complete_graph() self._shorten_action_queue(solve_leaf_index) return True # Find leaves indices_visited, actions_taken = self.find_leaf(time_limit) self.action_queue = deque(actions_taken) # Generates a best guess action queue in case of no solution return False
def board_to_hash(self, board: np.ndarray): """ Returns the corresponding hash value for a board Arguments: board (numpy array): Numpy array representing state of a board. """ return self._board_hashes[board.tostring()]
def write_prediction_result(id_: int, key: str, alignments: List[np.ndarray], mel: np.ndarray, ground_truth_mel: np.ndarray, text: str, source: np.ndarray, filename: str): example = tf.train.Example(features=tf.train.Features(feature={ 'id': int64_feature([id_]), 'key': bytes_feature([key.encode('utf-8')]), 'mel': bytes_feature([mel.tostring()]), 'mel_length': int64_feature([mel.shape[0]]), 'mel_width': int64_feature([mel.shape[1]]), 'ground_truth_mel': bytes_feature([ground_truth_mel.tostring()]), 'ground_truth_mel_length': int64_feature([ground_truth_mel.shape[0]]), 'alignment': bytes_feature([alignment.tostring() for alignment in alignments]), 'text': bytes_feature([text.encode('utf-8')]), 'source': bytes_feature([source.tostring()]), 'source_length': int64_feature([source.shape[0]]), })) write_tfrecord(example, filename)
def on_frame(self, frame: ndarray) -> None: """`on_frame` callback of a `Camera` instance. Will send the frame to all connected clients of the stream endpoint. :param numpy.ndarray frame: Current camera frame """ # put the frame into all stream request queues so they can be sent in the get_stream method for queue in self.stream_queues: queue.put_nowait(None if frame is None else frame.tostring())
def write_preprocessed_source_data(_id: int, key: str, source: np.ndarray, text, phones: np.ndarray, phone_txt, speaker_id, lang, filename: str): raw_source = source.tostring() example = tf.train.Example(features=tf.train.Features( feature={ 'id': int64_feature([_id]), 'key': bytes_feature([key.encode('utf-8')]), 'source': bytes_feature([raw_source]), 'source_length': int64_feature([len(source)]), 'text': bytes_feature([text.encode('utf-8')]), 'phone': bytes_feature([phones.tostring()]), 'phone_length': int64_feature([len(phones)]), 'phone_txt': bytes_feature([phone_txt.encode('utf-8')]), 'speaker_id': bytes_feature([speaker_id.encode('utf-8')]), 'lang': bytes_feature([lang.encode('utf-8')]), })) write_tfrecord(example, filename)
def write_samples(self, samples: np.ndarray) -> None: """Plays the samples immediately. :param samples: The samples should be between -1. and 1. :return: None """ samples = (samples * (2**15)).astype(np.int16) self.stream.write(samples.tostring())
def saveRecordedDataToFile(self, pcm: np.ndarray, fileName='Record'): fileName += strftime("_%Y-%m-%d_%H-%M-%S", gmtime()) waveFile = wave.open(fileName + ".wav", 'wb') waveFile.setnchannels(Cai.numberOfChannels) waveFile.setsampwidth(Cai.sampleWidthInBytes) waveFile.setframerate(Cai.frameRate) waveFile.writeframes(pcm.tostring()) Logger.info("Saving recorded data as: " + fileName) waveFile.close()
def make_tf_example(image: np.ndarray, label: np.ndarray, height: int, width: int, filename: str) -> tf.train.Example: return tf.train.Example(features=tf.train.Features( feature={ "image": tf.train.Feature(bytes_list=tf.train.BytesList( value=[image.tostring()])), "label": tf.train.Feature(bytes_list=tf.train.BytesList( value=[label.tostring()])), "height": tf.train.Feature(int64_list=tf.train.Int64List(value=[height])), "width": tf.train.Feature(int64_list=tf.train.Int64List(value=[width])), "filename": tf.train.Feature(bytes_list=tf.train.BytesList( value=[filename.encode()])) }))
def send_raw_data(self, data: np.ndarray, num_repeats: int): byte_data = data.tostring() rng = iter(int, 1) if num_repeats <= 0 else range(0, num_repeats) # <= 0 = forever for _ in rng: if self.__sending_interrupt_requested: break self.send_data(byte_data) self.current_sent_sample = len(data) self.current_sending_repeat += 1
def __init__(self, image: np.ndarray) -> None: if len(image.shape) == 3: height, width, n_channels = image.shape fmt = QtGui.QImage.Format_BGR30 else: height, width = image.shape n_channels = 1 fmt = QtGui.QImage.Format_Grayscale8 super().__init__(image.tostring(), width, height, n_channels * width, fmt)
def write_preprocessed_data(key: str, waveform: np.ndarray, speaker_id, age, gender, filename: str): raw_waveform = waveform.tostring() example = tf.train.Example(features=tf.train.Features(feature={ 'key': bytes_feature([key.encode('utf-8')]), 'waveform': bytes_feature([raw_waveform]), 'waveform_length': int64_feature([len(waveform)]), 'speaker_id': int64_feature([speaker_id]), 'age': int64_feature([age]), 'gender': int64_feature([gender]), })) write_tfrecord(example, filename)
def write_preprocessed_target_data(_id: int, key: str, mel: np.ndarray, filename: str): raw_mel = mel.tostring() example = tf.train.Example(features=tf.train.Features( feature={ 'id': int64_feature([_id]), 'key': bytes_feature([key.encode('utf-8')]), 'mel': bytes_feature([raw_mel]), 'target_length': int64_feature([len(mel)]), 'mel_width': int64_feature([mel.shape[1]]), })) write_tfrecord(example, filename)
def write_preprocessed_source_data(_id: int, key: str, source: np.ndarray, text, phones: np.ndarray, phone_txt, speaker_id, age, gender, filename: str): raw_source = source.tostring() phones = phones if phones is not None else np.empty([0], dtype=np.int64) phone_txt = phone_txt if phone_txt is not None else '' example = tf.train.Example(features=tf.train.Features( feature={ 'id': int64_feature([_id]), 'key': bytes_feature([key.encode('utf-8')]), 'source': bytes_feature([raw_source]), 'source_length': int64_feature([len(source)]), 'text': bytes_feature([text.encode('utf-8')]), 'phone': bytes_feature([phones.tostring()]), 'phone_length': int64_feature([len(phones)]), 'phone_txt': bytes_feature([phone_txt.encode('utf-8')]), 'speaker_id': int64_feature([speaker_id]), 'age': int64_feature([age]), 'gender': int64_feature([gender]), })) write_tfrecord(example, filename)
def write_mgc_lf0_prediction_result(id_: int, key: str, alignments: List[np.ndarray], mgc: np.ndarray, ground_truth_mgc: np.ndarray, lf0: np.ndarray, ground_truth_lf0: np.ndarray, text: str, source: np.ndarray, accent_type: Optional[np.ndarray], filename: str): example = tf.train.Example(features=tf.train.Features(feature={ 'id': int64_feature([id_]), 'key': bytes_feature([key.encode('utf-8')]), 'mgc': bytes_feature([mgc.tostring()]), 'target_length': int64_feature([mgc.shape[0]]), 'mgc_width': int64_feature([mgc.shape[1]]), 'ground_truth_mgc': bytes_feature([ground_truth_mgc.tostring()]), 'ground_truth_target_length': int64_feature([ground_truth_mgc.shape[0]]), 'lf0': bytes_feature([lf0.tostring()]), 'ground_truth_lf0': bytes_feature([ground_truth_lf0.tostring()]), 'alignment': bytes_feature([alignment.tostring() for alignment in alignments]), 'text': bytes_feature([text.encode('utf-8')]), 'source': bytes_feature([source.tostring()]), 'source_length': int64_feature([source.shape[0]]), 'accent_type': bytes_feature([accent_type.tostring()]) if accent_type is not None else bytes_feature([]), })) write_tfrecord(example, filename)
def write_preprocessed_target_data(_id: int, key: str, codes: np.ndarray, codes_length: int, lang, filename: str): raw_codes = codes.tostring() example = tf.train.Example(features=tf.train.Features( feature={ 'id': int64_feature([_id]), 'key': bytes_feature([key.encode('utf-8')]), 'lang': bytes_feature([lang.encode('utf-8')]), 'codes': bytes_feature([raw_codes]), 'codes_length': int64_feature([codes_length]), 'codes_width': int64_feature([codes.shape[1]]), })) write_tfrecord(example, filename)
def send_raw_data(self, data: np.ndarray, num_repeats: int): byte_data = data.tostring() if num_repeats == -1: # forever rng = iter(int, 1) else: rng = range(0, num_repeats) for _ in rng: self.send_data(byte_data) self.current_sent_sample = len(data) self.current_sending_repeat += 1
def db_value(self, value: np.ndarray): if value is None: return None if isinstance(value, (list, tuple)): value = np.array(value, dtype=np.int32) if value.dtype != np.int32: value = value.astype(np.int32) if value.ndim != 1: value = value.ravel() assert isinstance(value, np.ndarray) # value.size could be 0 (i.e. empty array) if the tensor is a scalar. assert value.ndim == 1 assert value.dtype == np.int32 return value.tostring()
def send_raw_data(self, data: np.ndarray, num_repeats: int): byte_data = data.tostring() rng = iter(int, 1) if num_repeats <= 0 else range(0, num_repeats) # <= 0 = forever sock = self.prepare_send_connection() if sock is None: return try: for _ in rng: if self.__sending_interrupt_requested: break self.send_data(byte_data, sock) self.current_sent_sample = len(data) self.current_sending_repeat += 1 finally: self.shutdown_socket(sock)