モノクロ画像がモノクロで表示されなくて困っています.ご助言いただければ幸いです.
画像は,pytorchで作成するデータローダーから表示を行います.
python
1syn_dir = 'gaze.h5' 2class synth_Datasets(torch.utils.data.Dataset): 3 def __init__(self,syndir,tfms): 4 self.syndir = syndir 5 self.tfms = tfms 6 7 with h5py.File(self.syndir,'r') as t_file: 8 self.len = len(t_file['image']) 9 assert 'image' in t_file, "Images are missing" 10 assert 'look_vec' in t_file, "Look vector is missing" 11 assert 'path' in t_file, "Paths are missing" 12 self.stack = np.expand_dims(np.stack([a for a in t_file['image'].values()],0), -1) 13 14 15 def __len__(self): 16 return self.len 17 18 def __getitem__(self,idx): 19 img = self.stack[idx,:,:,0] 20 img = np.asarray(img) 21 img = Image.fromarray(img) 22 23 if self.tfms: 24 img = self.tfms(img) 25 26 return img
python
1def synth_images(imgdir,bs): 2 3 tfms = transforms.Compose([transforms.ToTensor()]) 4 synth_ds = synth_Datasets(imgdir, tfms) 5 synth_dl = torch.utils.data.DataLoader(synth_ds, bs, shuffle=True) 6 7 return synth_dl
python
1synth_images = synth_images(syn_dir,bs) 2for i in synth_images: 3 break 4 5class ToNDarray(object): 6 def __init__(self): 7 pass 8 9 def __call__(self, x): 10 x_shape = x.shape 11 x = x.detach().clone().cpu() 12 x = x.numpy() 13 x = x[0] #x=(C,H,W)-->(H,W)にする 14 15 return x 16 17trans = ToNDarray() 18im = trans(i[0])*255 19print(type(im),im) 20plt.imshow(im, cmap='gray')
あなたの回答
tips
プレビュー