def fake_input(img): image = PaddleTensor() image.name = "image" image.shape = img.shape image.dtype = PaddleDType.FLOAT32 image.data = PaddleBuf(img.flatten().tolist()) return [image]
def copyToTensor(self, batch_size): tensor = PaddleTensor() tensor.name = self.name tensor.shape = [batch_size, self.shape_size] tensor.dtype = self.list[self.dtype] tensor.data = PaddleBuf(self.data) return tensor
def array2tensor(ndarray): """ convert numpy array to PaddleTensor""" assert isinstance(ndarray, np.ndarray), "input type must be np.ndarray" tensor = PaddleTensor() tensor.name = "data" tensor.shape = ndarray.shape if "float" in str(ndarray.dtype): tensor.dtype = PaddleDType.FLOAT32 elif "int" in str(ndarray.dtype): tensor.dtype = PaddleDType.INT64 else: raise ValueError("{} type ndarray is unsupported".format(tensor.dtype)) tensor.data = PaddleBuf(ndarray.flatten().tolist()) return tensor
def preprocess(img): img = cv2.resize(img, (input_size, input_size)) img = img.transpose((2, 0, 1)) if modelname == "mobilenet-ssd": img = (img - 127.5) * 0.007843 else: mean = np.array([103.94, 116.669, 123.68], np.float32).reshape([3, 1, 1]) img = img - mean image = PaddleTensor() image.name = "data" image.shape = [1, 3, input_size, input_size] image.dtype = PaddleDType.FLOAT32 image.data = PaddleBuf(img.flatten().astype("float32").tolist()) return [image]
def warp_input(image_data, input_size): """ deal input to paddle tensor :param image_data: 输入的图像 :param image_shape: 原始图像的大小 :param input_size: 输入图像的大小 :return: """ # image data image = PaddleTensor() image.name = 'image' image.shape = input_size image.dtype = PaddleDType.FLOAT32 image.data = PaddleBuf(image_data.flatten().astype(np.float32).tolist()) return image
def texts2tensor(self, texts): """ Tranform the texts(dict) to PaddleTensor Args: texts(dict): texts Returns: tensor(PaddleTensor): tensor with texts data """ lod = [0] data = [] for i, text in enumerate(texts): data += text['processed'] lod.append(len(text['processed']) + lod[i]) tensor = PaddleTensor(np.array(data).astype('int64')) tensor.name = "words" tensor.lod = [lod] tensor.shape = [lod[-1], 1] return tensor
def texts2tensor(self, texts): """ Tranform the texts(dict) to PaddleTensor Args: texts(list): each element is a dict that must have a named 'processed' key whose value is word_ids, such as texts = [{'processed': [23, 89, 43, 906]}] Returns: tensor(PaddleTensor): tensor with texts data """ lod = [0] data = [] for i, text in enumerate(texts): data += text['processed'] lod.append(len(text['processed']) + lod[i]) tensor = PaddleTensor(np.array(data).astype('int64')) tensor.name = "words" tensor.lod = [lod] tensor.shape = [lod[-1], 1] return tensor
def texts2tensor(self, texts): """ Tranform the texts(list) to PaddleTensor Args: texts(list): texts Returns: tensor(PaddleTensor): tensor with texts data """ lod = [0] data = [] for i, text in enumerate(texts): text_inds = word_to_ids(text, self.word2id_dict, self.word_replace_dict, oov_id=self.oov_id) data += text_inds lod.append(len(text_inds) + lod[i]) tensor = PaddleTensor(np.array(data).astype('int64')) tensor.name = "words" tensor.lod = [lod] tensor.shape = [lod[-1], 1] return tensor