pytorch 彩色影象轉灰度影象
阿新 • • 發佈:2019-01-03
pytorch 彩色影象轉灰度影象
pytorch 庫
pytorch 本身具有載入cifar10等資料集的函式,但是載入的是3*200*200的張量,當碰到要使用灰度影象時,可以使用他本身的函式進行修改,以較快速的完成彩色影象轉灰度影象
pytorch函式
dataset = dset.CIFAR10(root='../train/data', download=True,transform=transforms.Compose([
transforms.Scale(200),
transforms.ToTensor(),
transforms.Normalize ((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]))
這裡包含了對影象進行的一些處理,尺寸的控制,Normalize等
修改pytorch 庫函式
找到transforms 的庫函式位置,在後面新增 類 Gray()
class Gray(object)
def __call__(self, tensor):
# TODO: make efficient
R = tensor[0]
G = tensor[1]
B = tensor[2]
tensor[0 ]=0.299*R+0.587*G+0.114*B
tensor = tensor[0]
tensor = tensor.view(1,200,200)
return tensor
tensor.view 這裡使用是為了將tensor 做成 1*200*200的大小,否則出來是200*200