def predict(self, image_path): ''' 模型预测返回结果 :param input: 评估传入样例 {"image_path": "./data/input/cloudy/00000.jpg"} :return: 模型预测成功中户 {"label": 0} ''' print(image_path) # Normalize = {'mean': (0.485, 0.456, 0.406), 'std': (0.229, 0.224, 0.225)} result = [] for size in SIZES: img = cv.imread(image_path) img = cv.resize(img, dsize=(size, size)) tensor = img_to_tensor(img) tensor = Variable(torch.unsqueeze(tensor, dim=0).float(), requires_grad=False) for subModel in self.models: output = tta.fliplr_image2label(subModel, tensor.to(device)) result.append(output) new_output = torch.mean(torch.stack(result, 0), 0) pred = new_output.max(1, keepdim=True)[1] # output = tta.fliplr_image2label(self.model, tensor.to(device)) # pred = output.max(1, keepdim=True)[1].item() return {"label": pred}
def test_fliplr_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.fliplr_image2label(model, x) expected = int(x.sum()) assert int(output) == expected
def test_fliplr_image2label(): x = torch.rand((4, 3, 224, 224)) model = GlobalAvgPool2d(flatten=True) output = tta.fliplr_image2label(model, x) np.testing.assert_allclose(to_numpy(output), to_numpy(x.mean(dim=(2, 3))), atol=1e-6, rtol=1e-6)
imgPath = './data/input/COVIDClassification/image/1.jpg' Normalize = {'mean': (0.485, 0.456, 0.406), 'std': (0.229, 0.224, 0.225)} img = Image.open(imgPath).convert('RGB') img = np.array(img) img = cv.resize(img, dsize=(224, 224)) tensor = img_to_tensor(img, Normalize) tensor = Variable(torch.unsqueeze(tensor, dim=0).float(), requires_grad=False).to(device) # model = [] # for fold in range(5): # submodel = seresnet34() # submodel.last_linear = torch.nn.Linear(512, 6) # model.append(submodel.to(device)) # # result = [] # for subModel in model: # output = tta.fliplr_image2label(subModel, tensor.to(device)) # result.append(output) # output = tta.fliplr_image2label(model, tensor.to(device)) # pred = output.max(1, keepdim=True)[1] model = seresnet34() model.last_linear = torch.nn.Linear(512, 6) model = model.to(device) output = tta.fliplr_image2label(model, tensor.to(device)) pred = output.max(1, keepdim=True)[1].item() print(pred)
submit = pd.read_csv('./data/result.csv', header=None) submit.columns = ['name'] model = EfficientNet.from_pretrained(model_name='efficientnet-b7', num_classes=2) net_weight = './ckpt/efficientnet/efficientnet_30.pth' model.load_state_dict(torch.load(net_weight)) model = model.cuda() model.eval() opt = Config() # infer_transforms = T.Compose([ T.Resize((opt.input_size, opt.input_size)), T.ToTensor(), T.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) ]) result = [] test_dir = './data/test/' for name in submit['name'].values: img_path = os.path.join(test_dir, name) data = Image.open(img_path) data = data.convert('RGB') data = infer_transforms(data) data = data.unsqueeze(0) inputs = data.cuda() with torch.no_grad(): outputs = tta.fliplr_image2label(model, inputs) _, preds = torch.max(outputs.data, 1) result.append(preds.cpu().data.numpy()[0]) # submit['label'] = result submit.to_csv('submit.csv', index=False, header=None)