Pytorch、Caffe、OpenCV 读图片

Pytorch、Caffe、OpenCV 读图片_第1张图片

文章目录

  • 1 图片通道顺序
  • 2 图片归一化情况(像素值范围)


1 图片通道顺序

框架 颜色通道顺序 读入图片通道顺序 像素值范围
Pytorch RGB CHW 0-1
Caffe BGR CHW 0-1
OpenCV BGR HWC 0-255

2 图片归一化情况(像素值范围)

1)pytroch

torchvision.datasets.ImageFolder 读数据的时候,默认会除以一个 255,也即像素值在 0~1 之间

可以把结果打印出来查看,打印不全可以用 torch.set_printoptions(profile="full"),参考 Pytorch:输出整个tensor的方法

可以用下面的方式查看

import torchvision.datasets as datasets
import torchvision.transforms as transforms
train_data_pth = '/home/yanmeng/DataSet/Weather_Cls_55_320/val_data/'
import torch
torch.set_printoptions(profile="full")
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1"

input_size = (320,320)
n_worker = 6
batch_size = 2

normalize = transforms.Normalize(mean = [x / 255 for x in [122.81996305,119.96165628,119.49948514]],
                                 std = [x / 255 for x in [57.95208714,55.04555452,56.21179612]])

train_dataset = datasets.ImageFolder(
    train_data_pth,
    transforms.Compose([
        transforms.ToTensor(),
        normalize,
    ]))

train_loader = torch.utils.data.DataLoader(
    train_dataset,
    batch_size=batch_size,
    shuffle=True,
    num_workers=n_worker,
    pin_memory=True)

for batch_images, batch_labels in train_loader:
    print(batch_images[0])
    print(batch_images[0].shape) # CHW
    break

2)opencv

可以用下面的方式查看,打印不全可以用 np.set_printoptions,参考 python - Print 数组无法完整输出解决方法

import cv2
import numpy as np
np.set_printoptions(threshold=np.inf)

img = cv2.imread("/home/yanmeng/DataSet/Weather_Cls_55_320/val_data/cloudy/cloudy_00000.jpg")
print(img.shape) # HWC
print(img)

你可能感兴趣的:(Pytorch、Caffe、OpenCV 读图片)