def test_d4_image2label(): x = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8], [9, 0, 1, 2], [3, 4, 5, 6]]).unsqueeze(0).unsqueeze(0).float() model = SumAll() output = tta.d4_image2label(model, x) expected = int(x.sum()) assert int(output) == expected
def predict(self, image_path): ''' 模型预测返回结果 :param input: 评估传入样例 {"image_path":"image\/172691.jpg"} :return: 模型预测成功之后返回给系统样例 {"label":"Loxura_atymnus"} ''' outputs = [] for submodel, transform in zip(self.models, self.val_transforms): img = Image.open(image_path).convert('RGB') img = transform(img) img = img.unsqueeze(0) img = img.cuda() with torch.no_grad(): output = tta.d4_image2label(submodel, img) outputs.append(output) final = torch.mean(torch.stack(outputs, 0), 0) _,pred = torch.max(final,1) pred_name = self.id_name_dict[pred.detach().cpu().item()] return {"label": pred_name}