jbohnslav / opencv_transforms

OpenCV implementation of Torchvision's image augmentations
MIT License
375 stars 46 forks source link

pytorch tranformer output is different form the opencv_transorms #15

Closed Usernamezhx closed 4 years ago

Usernamezhx commented 4 years ago

first of all. thanks for work. it is what i want. the opencv _transform snippet output

def custom_transform(path):
    img = cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2RGB)
    img_resize = transforms.Resize(size=(224,224))(img)
    img_tensor = transforms.ToTensor()(img_resize)
    img_norm = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(img_tensor)
    out = np.expand_dims(img_norm, 0)
    print(out[0][0][0][0])

the pytorch transform output

def torch_transform(path):
    transform_list = torch_transforms.Compose([
            torch_transforms.Resize(224),
            torch_transforms.ToTensor(),
            torch_transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225]),
        ])
    img = Image.open(path).convert('RGB')
    out = transform_list(img).unsqueeze_(0)
    print(out[0][0][0][0])

the result : tensor(0.2453) 0.1939379

liboyue commented 4 years ago

This is very likely caused by the difference between Pillow and OpenCV. PyTorch uses Pillow to load & resize images, cv2 uses OpenCV. There are a lot of similar discussions: https://discuss.pytorch.org/t/torchvision-resize-vs-cv2-resize/47530, https://discuss.pytorch.org/t/difference-between-pil-resize-and-opencv-resize/33138.

Usernamezhx commented 4 years ago

thanks for your reply.