示例#1
0
    def predict(self,
                data: List[List[str]],
                max_seq_len: int = 128,
                split_char: str = '\002',
                batch_size: int = 1,
                use_gpu: bool = False):
        """
        Predicts the data labels.

        Args:
            data (obj:`List(List(str))`): The processed data whose each element is the list of a single text or a pair of texts.
            max_seq_len (:obj:`int`, `optional`, defaults to :int:`None`):
                If set to a number, will limit the total sequence returned so that it has a maximum length.
            split_char(obj:`str`, defaults to '\002'): The char used to split input tokens in token-cls task.
            batch_size(obj:`int`, defaults to 1): The number of batch.
            use_gpu(obj:`bool`, defaults to `False`): Whether to use gpu to run or not.

        Returns:
            results(obj:`list`): All the predictions labels.
        """
        if self.task not in self._tasks_supported \
                and self.task is not None:      # None for getting embedding
            raise RuntimeError(
                f'Unknown task {self.task}, current tasks supported:\n'
                '1. seq-cls: sequence classification;\n'
                '2. token-cls: sequence labeling;\n'
                '3. None: embedding')

        paddle.set_device('gpu') if use_gpu else paddle.set_device('cpu')

        batches = self._batchify(data, max_seq_len, batch_size, split_char)
        results = []
        self.eval()
        for batch in batches:
            input_ids, segment_ids = batch
            input_ids = paddle.to_tensor(input_ids)
            segment_ids = paddle.to_tensor(segment_ids)

            if self.task == 'seq-cls':
                probs = self(input_ids, segment_ids)
                idx = paddle.argmax(probs, axis=1).numpy()
                idx = idx.tolist()
                labels = [self.label_map[i] for i in idx]
                results.extend(labels)
            elif self.task == 'token-cls':
                probs = self(input_ids, segment_ids)
                batch_ids = paddle.argmax(
                    probs, axis=2).numpy()  # (batch_size, max_seq_len)
                batch_ids = batch_ids.tolist()
                token_labels = [[self.label_map[i] for i in token_ids]
                                for token_ids in batch_ids]
                results.extend(token_labels)
            elif self.task == None:
                sequence_output, pooled_output = self(input_ids, segment_ids)
                results.append([
                    pooled_output.squeeze(0).numpy().tolist(),
                    sequence_output.squeeze(0).numpy().tolist()
                ])

        return results
示例#2
0
def train():
    global e_greed, update_num
    total_reward = 0
    # 重置游戏状态
    obs = env.reset()
    obs = preprocess(obs)

    while True:
        # 使用贪心策略获取游戏动作的来源
        e_greed = max(0.01, e_greed - e_greed_decrement)
        if np.random.rand() < e_greed:
            # 随机生成动作
            action = env.action_space()
        else:
            # 策略模型预测游戏动作
            obs1 = np.expand_dims(obs, axis=0)
            action = policyQ(paddle.to_tensor(obs1, dtype='float32'))
            action = paddle.argmax(action).numpy()[0]
        # 执行游戏
        next_obs, reward, done, info = env.step(action)
        next_obs = preprocess(next_obs)
        total_reward += reward
        # 记录游戏数据
        rpm.append((obs, action, reward, next_obs, done))
        obs = next_obs
        # 游戏结束
        if done:
            break
        # 记录的数据打印batch_size就开始训练
        if len(rpm) > batch_size:
            # 获取训练数据
            batch_obs, batch_action, batch_reword, batch_next_obs, batch_done = rpm.sample(batch_size)
            # 计算损失函数
            action_value = policyQ(batch_obs)
            action_onehot = paddle.nn.functional.one_hot(batch_action, action_dim)
            pred_action_value = paddle.sum(action_value * action_onehot, axis=1)

            batch_argmax_action = paddle.argmax(policyQ(batch_next_obs), axis=1)
            v = targetQ(batch_next_obs)
            select_v = []
            for i in range(v.shape[0]):
                select_v.append(v[i][int(batch_argmax_action[i].numpy()[0])])
            select_v = paddle.stack(select_v).squeeze()

            select_v.stop_gradient = True
            target = batch_reword + gamma * select_v * (1.0 - batch_done)

            cost = paddle.nn.functional.mse_loss(pred_action_value, target)
            # 梯度更新
            cost.backward()
            optimizer.step()
            optimizer.clear_grad()
            # 指定的训练次数更新一次目标模型的参数
            if update_num % 200 == 0:
                targetQ.load_dict(policyQ.state_dict())
            update_num += 1
    return total_reward
示例#3
0
def decode(s_arc, s_rel, mask, tree=True):
    """Decode function"""
    mask = mask.numpy()
    lens = np.sum(mask, -1)
    # Prevent self-loops
    arc_preds = paddle.argmax(s_arc, axis=-1).numpy()
    bad = [not istree(seq[:i + 1]) for i, seq in zip(lens, arc_preds)]
    if tree and any(bad):
        arc_preds[bad] = eisner(s_arc.numpy()[bad], mask[bad])
    arc_preds = paddle.to_tensor(arc_preds)
    rel_preds = paddle.argmax(s_rel, axis=-1)
    rel_preds = index_sample(rel_preds, paddle.unsqueeze(arc_preds, axis=-1))
    rel_preds = paddle.squeeze(rel_preds, axis=-1)
    return arc_preds, rel_preds
示例#4
0
def Slide_Infer(model,
                infer_data,
                params_path=None,
                save_img_path=None,
                threshold=0.5,
                name='result'):
    # 信息修改与读取
    infer_data.out_mode = 'slide'  # 滑框模式
    raw_size = infer_data.raw_size  # 原图大小
    is_tif = infer_data.is_tif
    if infer_data.is_tif == True:
        geoinfo = infer_data.geoinfo
    # 数据读取器
    # infer_loader = paddle.io.DataLoader(infer_data, batch_size=1)
    infer_loader = DataLoader(infer_data, batch_size=1)
    # 开始预测
    if save_img_path is not None:
        if os.path.exists(save_img_path) == False:
            os.mkdir(save_img_path)
    model.eval()
    para_state_dict = paddle.load(params_path)
    model.set_dict(para_state_dict)
    # lens = len(infer_data)
    inf_imgs = []  # 保存块
    # for idx, infer_load_data in qenumerate(infer_loader):
    for infer_load_data in tqdm(infer_loader):
        if infer_load_data is None:
            break
        img = infer_load_data
        pred_list = model(img)
        # img = paddle.concat([A_img, B_img], axis=1)
        # pred_list = model(img)
        num_class, H, W = pred_list[0].shape[1:]
        if num_class == 2:
            inf_imgs.append((paddle.argmax(pred_list[0], axis=1). \
                            squeeze().numpy() * 255).astype('uint8'))
        elif num_class == 1:
            inf_imgs.append(((pred_list[0] > threshold).numpy(). \
                             astype('uint8') * 255).reshape([H, W]))
        else:
            inf_imgs.append((paddle.argmax(pred_list[0], axis=1). \
                            squeeze().numpy()).astype('uint8'))
        # print('[Infer] ' + str(idx + 1) + '/' + str(lens))
    fix_img = splicing_list(inf_imgs, raw_size)  # 拼接
    if is_tif == True:
        save_path = os.path.join(save_img_path, (name + '.tif'))
        save_tif(fix_img, geoinfo, save_path)
    else:
        save_path = os.path.join(save_img_path, (name + '.png'))
        cv2.imwrite(save_path, fix_img)
示例#5
0
    def test_basic(self):
        with fluid.program_guard(fluid.Program()):
            data = fluid.data(name="X", shape=[3, 4], dtype="float32")
            out = paddle.argmax(input=data)

            place = fluid.CPUPlace()
            exe = fluid.Executor(place)
            np_input = np.random.rand(3, 4).astype("float32")
            expected_result = np.argmax(np_input, axis=1)

            result, = exe.run(feed={"X": np_input}, fetch_list=[out])
        self.assertEqual((result == expected_result).all(), True)

        with fluid.program_guard(fluid.Program()):
            data = fluid.data(name="X", shape=[3, 4], dtype="float32")
            out = paddle.argmax(input=data, axis=0)

            place = fluid.CPUPlace()
            exe = fluid.Executor(place)
            np_input = np.random.rand(3, 4).astype("float32")
            expected_result = np.argmax(np_input, axis=0)

            result = exe.run(feed={"X": np_input}, fetch_list=[out])
        self.assertEqual((result == expected_result).all(), True)

        with fluid.program_guard(fluid.Program()):
            data = fluid.data(name="X", shape=[3, 4], dtype="float32")
            out = paddle.argmax(input=data, dtype="int32")

            place = fluid.CPUPlace()
            exe = fluid.Executor(place)
            np_input = np.random.rand(3, 4).astype("float32")
            expected_result = np.argmax(np_input, axis=1).astype(np.int32)

            result = exe.run(feed={"X": np_input}, fetch_list=[out])
        self.assertEqual((result == expected_result).all(), True)

        with fluid.program_guard(fluid.Program()):
            data1 = fluid.data(name="X", shape=[3, 4], dtype="float32")
            data2 = fluid.data(name="Y", shape=[3], dtype="int64")
            out = paddle.argmax(input=data, out=data2)

            place = fluid.CPUPlace()
            exe = fluid.Executor(place)
            result = exe.run(
                feed={"X": np.random.rand(3, 4).astype("float32")},
                fetch_list=[data2, out])
        self.assertEqual((result[0] == result[1]).all(), True)
示例#6
0
def main():
    # 初始化游戏
    env = flappyBird.GameState()
    # 图像输入形状和动作维度
    obs_dim = resize_shape[0]
    action_dim = env.action_dim

    # 创建模型
    model = Model(obs_dim, action_dim)
    model.load_dict(paddle.load(save_model_path))
    model.eval()

    # 开始游戏
    obs = env.reset()
    episode_reward = 0
    done = False
    # 游戏未结束执行一直执行游戏
    while not done:
        obs = preprocess(obs)
        obs = np.expand_dims(obs, axis=0)
        obs = paddle.to_tensor(obs, dtype='float32')
        action = model(obs)
        action = paddle.argmax(action).numpy()[0]
        obs, reward, done, info = env.step(action, is_train=False)
        episode_reward += reward
    print("最终得分为:{:.2f}".format(episode_reward))
def inference(model_file, word2id_dict, text_list: list):
    """前向推理"""
    vocab_size = len(word2id_dict)
    embedding_size = 256
    max_seq_len = 128
    sentiment_classifier = SentimentClassifier(embedding_size,
                                               vocab_size,
                                               num_steps=max_seq_len,
                                               num_layers=1)
    params_dict = paddle.load(model_file)
    # sentiment_classifier.set_state_dict(params_dict)
    sentiment_classifier.load_dict(params_dict)
    sentiment_classifier.eval()

    inputs = []
    for char in text_list:
        if char:
            inputs.append(word2id_dict[char] if char in
                          word2id_dict else word2id_dict['[oov]'])
    if len(inputs) > max_seq_len:
        inputs = inputs[:max_seq_len]
    if len(inputs) < max_seq_len:
        for _ in range(max_seq_len - len(inputs)):
            inputs.append(word2id_dict['[pad]'])  # 数据补齐
    inputs = paddle.to_tensor([inputs])

    pred = sentiment_classifier(inputs)
    labels = paddle.argmax(pred, axis=-1)

    return labels.numpy()
示例#8
0
    def _forward(self):
        feats = self.backbone(self.inputs)
        hrnet_outputs = self.final_conv(feats[0])

        if self.training:
            return self.loss(hrnet_outputs, self.inputs)
        elif self.deploy:
            outshape = hrnet_outputs.shape
            max_idx = paddle.argmax(hrnet_outputs.reshape(
                (outshape[0], outshape[1], outshape[2] * outshape[3])),
                                    axis=-1)
            return hrnet_outputs, max_idx
        else:
            if self.flip:
                self.inputs['image'] = self.inputs['image'].flip([3])
                feats = self.backbone(self.inputs)
                output_flipped = self.final_conv(feats[0])
                output_flipped = self.flip_back(output_flipped.numpy(),
                                                self.flip_perm)
                output_flipped = paddle.to_tensor(output_flipped.copy())
                if self.shift_heatmap:
                    output_flipped[:, :, :,
                                   1:] = output_flipped.clone()[:, :, :, 0:-1]
                hrnet_outputs = (hrnet_outputs + output_flipped) * 0.5
            imshape = (self.inputs['im_shape'].numpy()
                       )[:, ::-1] if 'im_shape' in self.inputs else None
            center = self.inputs['center'].numpy(
            ) if 'center' in self.inputs else np.round(imshape / 2.)
            scale = self.inputs['scale'].numpy(
            ) if 'scale' in self.inputs else imshape / 200.
            outputs = self.post_process(hrnet_outputs, center, scale)
            return outputs
示例#9
0
 def compute(self, pred, label, ignore_index):
     pred = paddle.argmax(pred, 1)
     active_acc = label.reshape([-1]) != ignore_index
     active_pred = pred.masked_select(active_acc)
     active_labels = label.masked_select(active_acc)
     correct = active_pred.equal(active_labels)
     return correct
    def forward(self, ipt):
        # 卷积 + ReLU + BN
        x = self.conv1(ipt)
        x = paddle.nn.functional.relu(x)
        x = self.bn1(x)
        # 卷积 + ReLU + BN
        x = self.conv2(x)
        x = paddle.nn.functional.relu(x)
        x = self.bn2(x)
        # 卷积 + ReLU
        x = self.conv3(x)
        x = paddle.nn.functional.relu(x)
        # 将3维特征转换为2维特征 - 此处可以使用reshape代替
        x = paddle.tensor.flatten(x, 2)
        # 全连接 + ReLU
        x = self.linear(x)
        x = paddle.nn.functional.relu(x)
        # 双向LSTM - [0]代表取双向结果,[1][0]代表forward结果,[1][1]代表backward结果,详细说明可在官方文档中搜索'LSTM'
        x = self.lstm(x)[0]
        # 输出层 - Shape = (Batch Size, Max label len, Signal)
        x = self.linear2(x)

        # 在计算损失时ctc-loss会自动进行softmax,所以在预测模式中需额外做softmax获取标签概率
        if self.is_infer:
            # 输出层 - Shape = (Batch Size, Max label len, Prob)
            x = paddle.nn.functional.softmax(x)
            # 转换为标签
            x = paddle.argmax(x, axis=-1)
        return x
示例#11
0
    def update(self, logits, labels):
        """
        Update the states based on the current mini-batch prediction results.

        Args:
            logits (Tensor): The predicted value is a Tensor with 
                shape [batch_size, seq_len, num_classes] and type float32 or 
                float64.
            labels (Tensor): The ground truth value is a 2D Tensor, 
                its shape is [batch_size, seq_len] and type is int64.
        """
        probs = paddle.argmax(logits, axis=-1)
        probs = probs.numpy()
        labels = labels.numpy()
        assert probs.shape[0] == labels.shape[0]
        assert probs.shape[1] == labels.shape[1]
        for i in range(probs.shape[0]):
            start, end = 1, probs.shape[1]
            while end > start:
                if labels[i][end - 1] != 0:
                    break
                end -= 1
            prob, label = probs[i][start:end], labels[i][start:end]
            for y_pred, y in zip(prob, label):
                if y_pred == y:
                    self.tp[y] = self.tp.get(y, 0) + 1
                else:
                    self.fp[y_pred] = self.fp.get(y_pred, 0) + 1
                    self.fn[y] = self.fn.get(y, 0) + 1
示例#12
0
 def forward_test(self, src):
     bs = paddle.shape(src)[0]
     if self.encoder is not None:
         src = self.positional_encoding(paddle.transpose(src, [1, 0, 2]))
         memory = self.encoder(src)
     else:
         memory = paddle.transpose(paddle.squeeze(src, 2), [2, 0, 1])
     dec_seq = paddle.full((bs, 1), 2, dtype=paddle.int64)
     dec_prob = paddle.full((bs, 1), 1., dtype=paddle.float32)
     for len_dec_seq in range(1, 25):
         dec_seq_embed = paddle.transpose(self.embedding(dec_seq),
                                          [1, 0, 2])
         dec_seq_embed = self.positional_encoding(dec_seq_embed)
         tgt_mask = self.generate_square_subsequent_mask(
             paddle.shape(dec_seq_embed)[0])
         output = self.decoder(dec_seq_embed,
                               memory,
                               tgt_mask=tgt_mask,
                               memory_mask=None,
                               tgt_key_padding_mask=None,
                               memory_key_padding_mask=None)
         dec_output = paddle.transpose(output, [1, 0, 2])
         dec_output = dec_output[:, -1, :]
         word_prob = F.softmax(self.tgt_word_prj(dec_output), axis=1)
         preds_idx = paddle.argmax(word_prob, axis=1)
         if paddle.equal_all(
                 preds_idx,
                 paddle.full(paddle.shape(preds_idx), 3, dtype='int64')):
             break
         preds_prob = paddle.max(word_prob, axis=1)
         dec_seq = paddle.concat(
             [dec_seq, paddle.reshape(preds_idx, [-1, 1])], axis=1)
         dec_prob = paddle.concat(
             [dec_prob, paddle.reshape(preds_prob, [-1, 1])], axis=1)
     return [dec_seq, dec_prob]
示例#13
0
    def compute(self, pred, label, *args):
        """
        Compute the top-k (maxinum value in `topk`) indices.

        Args:
            pred (Tensor): The predicted value is a Tensor with dtype
                float32 or float64. Shape is [batch_size, d0, ..., dN].
            label (Tensor): The ground truth value is Tensor with dtype
                int64. Shape is [batch_size, d0, ..., 1], or
                [batch_size, d0, ..., num_classes] in one hot representation.
                
        Return:
            Tensor: Correct mask, a tensor with shape [batch_size, topk].
        """
        pred = paddle.argsort(pred, descending=True)
        pred = paddle.slice(pred,
                            axes=[len(pred.shape) - 1],
                            starts=[0],
                            ends=[self.maxk])
        if (len(label.shape) == 1) or \
           (len(label.shape) == 2 and label.shape[-1] == 1):
            # In static mode, the real label data shape may be different
            # from shape defined by paddle.static.InputSpec in model
            # building, reshape to the right shape.
            label = paddle.reshape(label, (-1, 1))
        elif label.shape[-1] != 1:
            # one-hot label
            label = paddle.argmax(label, axis=-1, keepdim=True)
        correct = pred == label
        return paddle.cast(correct, dtype='float32')
示例#14
0
    def pbtPredict_Callback(self):
        __img, img_array = [], [
        ]  # 将图像统一从qimage->pil image -> np.array [1, 1, 28, 28]

        # 获取qimage格式图像
        if self.mode == MODE_MNIST:
            __img = self.lbDataArea.pixmap()  # label内若无图像返回None
            if __img == None:  # 无图像则用纯黑代替
                # __img = QImage(224, 224, QImage.Format_Grayscale8)
                __img = ImageQt.ImageQt(
                    Image.fromarray(np.uint8(np.zeros([224, 224]))))
            else:
                __img = __img.toImage()
        elif self.mode == MODE_WRITE:
            __img = self.paintBoard.getContentAsQImage()

        # 转换成pil image类型处理
        pil_img = ImageQt.fromqimage(__img)
        pil_img = pil_img.resize((28, 28), Image.ANTIALIAS)

        img_array = np.array(pil_img.convert('L')).reshape(1, 1, 28, 28)
        img = normalize(img_array)
        img = paddle.Tensor(img)
        __result = network(img)
        argmax__result = paddle.argmax(__result).numpy()

        self.result[0] = argmax__result[0]  # 置信度

        m = F.sigmoid(__result).numpy()

        self.result[1] = m[0][self.result[0]]

        self.lbResult.setText("%d" % (self.result[0]))
        self.lbCofidence.setText("%.8f" % (self.result[1]))
示例#15
0
def farthest_point_sample(xyz, npoint):
    """
        Input:
            xyz: pointcloud data, [B, N, 3]
            npoint: number of samples
        Return:
            centroids: sampled pointcloud index, [B, npoint]
    """
    B, N, C = xyz.shape
    centroids = paddle.zeros([B, npoint])
    distance = paddle.ones([B, N])
    farthest = paddle.randint(0, N, (B,))
    batch_indices = paddle.arange(B)

    for i in range(npoint):
        centroids[:, i] = farthest
        xyz_np = xyz.numpy()
        batch_indices_np = batch_indices.numpy().astype('int64')
        farthest_np = farthest.numpy().astype('int64')
        centroid = xyz_np[batch_indices_np, farthest_np, :]
        centroid = paddle.to_tensor(centroid).unsqueeze(1)
        dist = paddle.sum((xyz - centroid) ** 2, -1)
        mask = dist < distance
        distance_np = distance.numpy()
        dist_np = dist.numpy()
        mask_np = mask.numpy()
        distance_np[mask_np] = dist_np[mask_np]
        distance = paddle.to_tensor(distance_np)
        farthest = paddle.argmax(distance, -1)

    return centroids
示例#16
0
def paddle_argmax(name: str, x, axis):
    import paddle
    paddle.enable_static()

    with paddle.static.program_guard(paddle.static.Program(),
                                     paddle.static.Program()):
        node_x = paddle.static.data(name='x', shape=x.shape, dtype='float32')
        out = paddle.argmax(x=node_x, axis=axis)
        out = paddle.cast(out, np.float32)
        cpu = paddle.static.cpu_places(1)
        exe = paddle.static.Executor(cpu[0])
        # startup program will call initializer to initialize the parameters.
        exe.run(paddle.static.default_startup_program())

        outs = exe.run(feed={'x': x}, fetch_list=[out])

        saveModel(name,
                  exe,
                  feedkeys=['x'],
                  fetchlist=[out],
                  inputs=[x],
                  outputs=[outs[0]],
                  target_dir=sys.argv[1])

    return outs[0]
示例#17
0
def train():
    global e_greed, update_num
    total_reward = 0
    # 重置游戏状态
    obs = env.reset()
    obs = preprocess(obs)

    while True:
        # 使用贪心策略获取游戏动作的来源
        e_greed = max(0.01, e_greed - e_greed_decrement)
        if np.random.rand() < e_greed:
            # 随机生成动作
            action = env.action_space()
        else:
            # 策略模型预测游戏动作
            obs1 = np.expand_dims(obs, axis=0)
            action = policyQ(paddle.to_tensor(obs1, dtype='float32'))
            action = paddle.argmax(action).numpy()[0]
        # 执行游戏
        next_obs, reward, done, info = env.step(action)
        next_obs = preprocess(next_obs)
        n_step_buffer.append((obs, action, reward, next_obs, done))
        if len(n_step_buffer) > n_step:
            n_step_reward = sum([n_step_buffer[i][2]*(gamma**i) for i in range(n_step)])
            total_reward += n_step_reward
            n_step_obs, n_step_action, _, n_step_next_obs, n_step_done = n_step_buffer.pop(0)
            rpm.append((n_step_obs, n_step_action, n_step_reward, n_step_next_obs, n_step_done))
        obs = next_obs
        # 游戏结束
        if done:
            while len(n_step_buffer) > 0:
                n_step_reward = sum([n_step_buffer[i][2] * (gamma ** i) for i in range(len(n_step_buffer))])
                n_step_obs, n_step_action, _, n_step_next_obs, n_step_done = n_step_buffer.pop(0)
                rpm.append((n_step_obs, n_step_action, n_step_reward, n_step_next_obs, n_step_done))
            break
        # 记录的数据打印batch_size就开始训练
        if len(rpm) > batch_size:
            # 获取训练数据
            batch_obs, batch_action, batch_reword, batch_next_obs, batch_done = rpm.sample(batch_size)
            # 计算损失函数
            action_value = policyQ(batch_obs)
            action_onehot = paddle.nn.functional.one_hot(batch_action, action_dim)
            pred_action_value = paddle.sum(action_value * action_onehot, axis=1)

            best_v = targetQ(batch_next_obs)
            best_v = paddle.max(best_v, axis=1)

            best_v.stop_gradient = True
            target = batch_reword + gamma ** n_step * best_v * (1.0 - batch_done)

            cost = paddle.nn.functional.mse_loss(pred_action_value, target)
            # 梯度更新
            cost.backward()
            optimizer.step()
            optimizer.clear_grad()
            # 指定的训练次数更新一次目标模型的参数
            if update_num % 200 == 0:
                targetQ.load_dict(policyQ.state_dict())
            update_num += 1
    return total_reward
示例#18
0
def main():
    # 初始化游戏
    env = DinoGame()
    # 图像输入形状和动作维度
    obs_dim = env.observation_space.shape[0]
    action_dim = env.action_space.n

    # 创建模型
    model = Model(obs_dim, action_dim)
    model.load_dict(paddle.load(save_model_path))
    model.eval()

    # 开始游戏
    obs = env.reset()
    episode_reward = 0
    done = False
    last_time = time.time()
    # 游戏未结束执行一直执行游戏
    while not done:
        obs = np.expand_dims(obs, axis=0)
        obs = paddle.to_tensor(obs, dtype='float32')
        action = model(obs)
        action = paddle.argmax(action).numpy()[0]
        obs, reward, done, info = env.step(action)
        episode_reward += reward
        # 防止截图太快
        # fps_now = 1 / (time.time() - last_time)
        # if fps_now > FPS:
        #     time.sleep(1 / FPS - 1 / fps_now)
        # last_time = time.time()
    print("最终得分为:{:.2f}".format(episode_reward))
示例#19
0
 def forward(self, inputs):
     x = self.bn1(inputs)
     x = paddle.reshape(x, [1, 3 * 16 * 16])
     x = self.fc1(x)
     x = paddle.fluid.layers.unsqueeze(input=x, axes=[2])
     x = self.relu1(x)
     y = paddle.fluid.layers.fill_constant(x.shape,
                                           dtype=paddle.float32,
                                           value=1)
     # x = paddle.stack([x, y], axis=3)
     x = paddle.slice(x, axes=[0], starts=[0], ends=[1])
     x = paddle.exp(x)
     # y += paddle.fluid.layers.uniform_random(y.shape)
     y = paddle.expand(y, shape=[1, 768, 768, 2])
     x = paddle.expand(x, shape=[1, 768, 768, 2])
     out = paddle.concat([x, y])
     out = self.dp(out)
     out = channel_shuffle(out, 2)
     out1, out2 = paddle.split(out, num_or_sections=2, axis=1)
     outshape = out1.shape
     max_idx = paddle.argmax(out1.reshape(
         (outshape[0], outshape[1], outshape[2] * outshape[3])),
                             axis=-1)
     out2 = out2.reshape(
         (outshape[0], outshape[1], outshape[2] * outshape[3]))
     res, _ = self.lstm(out2)
     return res, max_idx
示例#20
0
    def forward(self, input_ids, end_id):
        output, cached_kvs = self.model(input_ids, use_cache=True, cache=None)
        src_ids = input_ids
        nid = paddle.argmax(output[:, -1, :], axis=-1).reshape([-1, 1])
        src_ids = paddle.concat([src_ids, nid], axis=1)
        cur_len = 0
        while (cur_len < self.max_predict_len):
            output, cached_kvs = self.model(
                nid, use_cache=True, cache=cached_kvs)

            nid = paddle.argmax(output[:, -1, :], axis=-1).reshape([-1, 1])
            src_ids = paddle.concat([src_ids, nid], axis=1)
            cur_len += 1
            if paddle.max(nid) == end_id:
                break
        return src_ids
示例#21
0
def soft_nms(box_scores, score_threshold, sigma=0.5, top_k=-1):
    """Soft NMS implementation.

    References:
        https://arxiv.org/abs/1704.04503
        https://github.com/facebookresearch/Detectron/blob/master/detectron/utils/cython_nms.pyx

    Args:
        box_scores (N, 5): boxes in corner-form and probabilities.
        score_threshold: boxes with scores less than value are not considered.
        sigma: the parameter in score re-computation.
            scores[i] = scores[i] * exp(-(iou_i)^2 / simga)
        top_k: keep top_k results. If k <= 0, keep all the results.
    Returns:
         picked_box_scores (K, 5): results of NMS.
    """
    picked_box_scores = []
    while box_scores.size(0) > 0:
        max_score_index = paddle.argmax(box_scores[:, 4])
        cur_box_prob = paddle.to_tensor(box_scores[max_score_index, :])
        picked_box_scores.append(cur_box_prob)
        if len(picked_box_scores) == top_k > 0 or box_scores.size(0) == 1:
            break
        cur_box = cur_box_prob[:-1]
        box_scores[max_score_index, :] = box_scores[-1, :]
        box_scores = box_scores[:-1, :]
        ious = iou_of(cur_box.unsqueeze(0), box_scores[:, :-1])
        box_scores[:,
                   -1] = box_scores[:, -1] * paddle.exp(-(ious * ious) / sigma)
        box_scores = box_scores[box_scores[:, -1] > score_threshold, :]
    if len(picked_box_scores) > 0:
        return paddle.stack(picked_box_scores)
    else:
        return paddle.to_tensor([])
示例#22
0
            def predict_fn(data, label):
                """predict_fn for input gradients based interpreters,
                    for image classification models only.

                Args:
                    data ([type]): [description]
                    label ([type]): can be None.

                Returns:
                    [type]: [description]
                """
                import paddle
                assert len(data.shape) == 4  # [bs, h, w, 3]

                with paddle.no_grad():
                    logits = self.paddle_model(paddle.to_tensor(data))  # get logits, [bs, num_c]
                    probas = paddle.nn.functional.softmax(logits, axis=1)  # get probabilities.
                    pred = paddle.argmax(probas, axis=1)  # get predictions.

                    if label is None:
                        label = pred.numpy()  # label is an integer.

                    if output == 'logit':
                        return logits.numpy(), label
                    else:
                        return probas.numpy(), label
示例#23
0
def evaluate(model, metric, data_loader, do_pred=False):
    model.eval()
    if not do_pred:
        metric.reset()
        for batch in data_loader:
            input_ids, segment_ids, labels = batch
            logits = model(input_ids, segment_ids)
            correct = metric.compute(logits, labels)
            metric.update(correct)
            accu = metric.accumulate()
        print("accu: %f" % (accu))
    else:
        res = {}
        for batch in data_loader:
            input_ids, segment_ids, qas_id = batch
            logits = model(input_ids, segment_ids)
            qas_id = qas_id.numpy()
            preds = paddle.argmax(logits, axis=1).numpy()
            for i in range(len(preds)):
                res[str(
                    qas_id[i])] = data_loader.dataset.get_labels()[preds[i]]
        with open('prediction.json', "w") as writer:
            writer.write(json.dumps(res, ensure_ascii=False, indent=4) + "\n")

    model.train()
示例#24
0
def ctc_greedy_decoder(probs_seq, vocabulary, blank=0):
    """CTC贪婪(最佳路径)解码器。
    由最可能的令牌组成的路径被进一步后处理
    删除连续的重复和所有的空白。
    :param probs_seq: 每个词汇表上概率的二维列表字符。
                      每个元素都是浮点概率列表为一个字符。
    :type probs_seq: list
    :param vocabulary: 词汇表
    :type vocabulary: list
    :param blank: 空白索引
    :type blank: int
    :return: 解码结果字符串
    :rtype: baseline
    """
    # 尺寸验证
    for probs in probs_seq:
        if not len(probs) == len(vocabulary):
            raise ValueError("probs_seq 尺寸与词汇不匹配")
    # argmax以获得每个时间步长的最佳指标
    max_index_list = paddle.argmax(probs_seq, -1).numpy()
    # 删除连续的重复索引
    index_list = [index_group[0] for index_group in groupby(max_index_list)]
    # 删除空白索引
    index_list = [index for index in index_list if index != blank]
    # 将索引列表转换为字符串
    return ''.join([vocabulary[index] for index in index_list])
示例#25
0
def aug_inference(model,
                  im,
                  ori_shape,
                  transforms,
                  scales=1.0,
                  flip_horizontal=False,
                  flip_vertical=False,
                  is_slide=False,
                  stride=None,
                  crop_size=None):
    """
    Infer with augmentation.

    Args:
        model (paddle.nn.Layer): model to get logits of image.
        im (Tensor): the input image.
        ori_shape (list): Origin shape of image.
        transforms (list): Transforms for image.
        scales (float|tuple|list):  Scales for resize. Default: 1.
        flip_horizontal (bool): Whether to flip horizontally. Default: False.
        flip_vertical (bool): Whether to flip vertically. Default: False.
        is_slide (bool): Whether to infer by sliding wimdow. Default: False.
        crop_size (tuple|list). The size of sliding window, (w, h). It should be probided if is_slide is True.
        stride (tuple|list). The size of stride, (w, h). It should be probided if is_slide is True.

    Returns:
        Tensor: Prediction of image with shape (1, 1, h, w) is returned.
    """
    if isinstance(scales, float):
        scales = [scales]
    elif not isinstance(scales, (tuple, list)):
        raise TypeError(
            '`scales` expects float/tuple/list type, but received {}'.format(
                type(scales)))
    final_logit = 0
    h_input, w_input = im.shape[-2], im.shape[-1]
    flip_comb = flip_combination(flip_horizontal, flip_vertical)
    for scale in scales:
        h = int(h_input * scale + 0.5)
        w = int(w_input * scale + 0.5)
        im = F.interpolate(im, (h, w), mode='bilinear')
        for flip in flip_comb:
            im_flip = tensor_flip(im, flip)
            logit = inference(model,
                              im_flip,
                              is_slide=is_slide,
                              crop_size=crop_size,
                              stride=stride)
            logit = tensor_flip(logit, flip)
            logit = F.interpolate(logit, (h_input, w_input), mode='bilinear')

            logit = F.softmax(logit, axis=1)
            final_logit = final_logit + logit

    pred = reverse_transform(final_logit,
                             ori_shape,
                             transforms,
                             mode='bilinear')
    pred = paddle.argmax(pred, axis=1, keepdim=True, dtype='int32')
    return pred
示例#26
0
def predict(model, data, tokenizer, label_map, batch_size=1):
    """
    Predicts the data labels.

    Args:
        model (obj:`paddle.nn.Layer`): A model to classify texts.
        data (obj:`List(Example)`): The processed data whose each element is a Example (numedtuple) object.
            A Example object contains `text`(word_ids) and `se_len`(sequence length).
        tokenizer(obj:`PretrainedTokenizer`): This tokenizer inherits from :class:`~paddlenlp.transformers.PretrainedTokenizer` 
            which contains most of the methods. Users should refer to the superclass for more information regarding methods.
        label_map(obj:`dict`): The label id (key) to label str (value) map.
        batch_size(obj:`int`, defaults to 1): The number of batch.

    Returns:
        results(obj:`dict`): All the predictions labels.
    """
    examples = []
    for text_pair in data:
        query_input_ids, query_segment_ids, title_input_ids, title_segment_ids = convert_example(
            text_pair,
            tokenizer,
            label_list=label_map.values(),
            max_seq_length=args.max_seq_length,
            is_test=True)
        examples.append((query_input_ids, query_segment_ids, title_input_ids,
                         title_segment_ids))

    # Seperates data into some batches.
    batches = [
        examples[idx:idx + batch_size]
        for idx in range(0, len(examples), batch_size)
    ]
    batchify_fn = lambda samples, fn=Tuple(
        Pad(axis=0, pad_val=tokenizer.pad_token_id),  # query_input
        Pad(axis=0, pad_val=tokenizer.pad_token_type_id),  # query_segment
        Pad(axis=0, pad_val=tokenizer.pad_token_id),  # title_input
        Pad(axis=0, pad_val=tokenizer.pad_token_type_id),  # tilte_segment
    ): [data for data in fn(samples)]

    results = []
    model.eval()
    for batch in batches:
        query_input_ids, query_segment_ids, title_input_ids, title_segment_ids = batchify_fn(
            batch)

        query_input_ids = paddle.to_tensor(query_input_ids)
        query_segment_ids = paddle.to_tensor(query_segment_ids)
        title_input_ids = paddle.to_tensor(title_input_ids)
        title_segment_ids = paddle.to_tensor(title_segment_ids)

        probs = model(query_input_ids,
                      title_input_ids,
                      query_token_type_ids=query_segment_ids,
                      title_token_type_ids=title_segment_ids)
        idx = paddle.argmax(probs, axis=1).numpy()
        idx = idx.tolist()
        labels = [label_map[i] for i in idx]
        results.extend(labels)
    return results
示例#27
0
    def forward(self, text, text_pair=None):

        _, pooled_output = self.ernie(text, text_pair)

        pooled_output = self.dropout(pooled_output)
        logits = self.classifier(pooled_output)
        predictions = paddle.argmax(logits, axis=-1)
        return logits, predictions
示例#28
0
    def forward(self, text, text_pair=None):

        sequence_output, _ = self.ernie(text, text_pair)

        sequence_output = self.dropout(sequence_output)
        logits = self.classifier(sequence_output)
        predictions = paddle.argmax(logits, axis=-1)
        return logits, predictions
示例#29
0
def do_predict():
    paddle.set_device(args.device)

    tokenizer = ErnieTokenizer.from_pretrained("ernie-1.0")
    label_map = load_dict(args.tag_path)
    id2label = {val: key for key, val in label_map.items()}
    model = ErnieForTokenClassification.from_pretrained("ernie-1.0", num_classes=len(label_map))

    no_entity_label = "O"
    ignore_label = len(label_map)

    print("============start predict==========")
    if not args.init_ckpt or not os.path.isfile(args.init_ckpt):
        raise Exception("init checkpoints {} not exist".format(args.init_ckpt))
    else:
        state_dict = paddle.load(args.init_ckpt)
        model.set_dict(state_dict)
        print("Loaded parameters from %s" % args.init_ckpt)

    # load data from predict file
    sentences = read_by_lines(args.predict_data) # origin data format
    sentences = [json.loads(sent) for sent in sentences]

    encoded_inputs_list = []
    for sent in sentences:
        sent = sent["text"].replace(" ", "\002")
        input_ids, token_type_ids, seq_len = convert_example_to_feature([list(sent), []], tokenizer,
                    max_seq_len=args.max_seq_len, is_test=True)
        encoded_inputs_list.append((input_ids, token_type_ids, seq_len))

    batchify_fn = lambda samples, fn=Tuple(
        Pad(axis=0, pad_val=tokenizer.vocab[tokenizer.pad_token], dtype='int32'), # input_ids
        Pad(axis=0, pad_val=tokenizer.vocab[tokenizer.pad_token], dtype='int32'), # token_type_ids
        Stack(dtype='int64') # sequence lens
    ): fn(samples)
    # Seperates data into some batches.
    batch_encoded_inputs = [encoded_inputs_list[i: i + args.batch_size]
                            for i in range(0, len(encoded_inputs_list), args.batch_size)]
    results = []
    model.eval()
    for batch in batch_encoded_inputs:
        input_ids, token_type_ids, seq_lens = batchify_fn(batch)
        input_ids = paddle.to_tensor(input_ids)
        token_type_ids = paddle.to_tensor(token_type_ids)
        logits = model(input_ids, token_type_ids)
        probs = F.softmax(logits, axis=-1)
        probs_ids = paddle.argmax(probs, -1).numpy()
        probs = probs.numpy()
        for p_list, p_ids, seq_len in zip(probs.tolist(), probs_ids.tolist(), seq_lens.tolist()):
            prob_one = [p_list[index][pid] for index, pid in enumerate(p_ids[1: seq_len - 1])]
            label_one = [id2label[pid] for pid in p_ids[1: seq_len - 1]]
            results.append({"probs": prob_one, "labels": label_one})
    assert len(results) == len(sentences)
    for sent, ret in zip(sentences, results):
        sent["pred"] = ret
    sentences = [json.dumps(sent, ensure_ascii=False) for sent in sentences]
    write_by_lines(args.predict_save_path, sentences)
    print("save data {} to {}".format(len(sentences), args.predict_save_path))
示例#30
0
    def train_iter(self, *inputs, **kwargs):

        current_iter = kwargs['current_iter']
        total_iters = kwargs['total_iters']

        if self.target_decay_method == 'cosine':
            self.m = 1 - (1 - self.base_m) * (1 + math.cos(
                math.pi * current_iter / total_iters)) / 2.0  # 47.0
        elif self.target_decay_method == 'fixed':
            self.m = self.base_m  # 55.7
        else:
            raise NotImplementedError

        if self.prob_moving_type == 'fixed':
            self.pos_prob = self.max_use_other_prob
        elif self.prob_moving_type == 'linear':
            self.pos_prob = self.max_use_other_prob * current_iter / total_iters
        else:
            raise NotImplementedError

        use_other = self.sample()
        if paddle.distributed.get_world_size() > 1:
            paddle.distributed.broadcast(use_other, src=0)

        # self.update_target_network()
        img_a, img_b = inputs
        a1 = self.predictor(self.towers[0](img_a))
        a1 = nn.functional.normalize(a1, axis=1)
        b1 = self.towers[1](img_b)
        b1 = nn.functional.normalize(b1, axis=1)
        b1.stop_gradient = True

        if bool(use_other):
            with paddle.no_grad():
                similarities = paddle.matmul(
                    b1,
                    self.queue.clone().detach()).detach()
                indices = paddle.argmax(similarities, axis=1).detach()
                c = paddle.gather(self.queue.clone().detach(), indices, axis=1)
                c = paddle.transpose(c, perm=[1, 0]).detach()
                c.stop_gradient = True

        a2 = self.predictor(self.towers[0](img_b))
        a2 = nn.functional.normalize(a2, axis=1)
        if not bool(use_other):
            b2 = self.towers[1](img_a)
            b2 = nn.functional.normalize(b2, axis=1)
            b2.stop_gradient = True

        if bool(use_other):
            outputs = self.head(a1, c, a2, c)
        else:
            outputs = self.head(a1, b1, a2, b2)

        with paddle.no_grad():
            self._dequeue_and_enqueue(b1)

        return outputs