-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jiwei
committed
Oct 12, 2019
1 parent
0ead1f3
commit eec374e
Showing
10 changed files
with
1,087 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import os | ||
|
||
import numpy as np | ||
import PIL.Image | ||
import scipy.io as sio | ||
import torch | ||
from torch.utils import data | ||
|
||
class MyData(data.Dataset): # inherit | ||
""" | ||
load data in a folder | ||
""" | ||
mean_rgb = np.array([0.447, 0.407, 0.386]) | ||
std_rgb = np.array([0.244, 0.250, 0.253]) | ||
def __init__(self, root, transform=False): | ||
super(MyData, self).__init__() | ||
self.root = root | ||
|
||
self._transform = transform | ||
|
||
img_root = os.path.join(self.root, 'train_images') | ||
lbl_root = os.path.join(self.root, 'train_masks') | ||
depth_root = os.path.join(self.root, 'train_depth') | ||
|
||
file_names = os.listdir(img_root) | ||
self.img_names = [] | ||
self.lbl_names = [] | ||
self.depth_names = [] | ||
for i, name in enumerate(file_names): | ||
if not name.endswith('.jpg'): | ||
continue | ||
self.lbl_names.append( | ||
os.path.join(lbl_root, name[:-4]+'.png') | ||
) | ||
self.img_names.append( | ||
os.path.join(img_root, name) | ||
) | ||
self.depth_names.append( | ||
os.path.join(depth_root, name[:-4]+'.png') | ||
) | ||
|
||
def __len__(self): | ||
return len(self.img_names) | ||
|
||
def __getitem__(self, index): | ||
# load image | ||
img_file = self.img_names[index] | ||
img = PIL.Image.open(img_file) | ||
# img = img.resize((256, 256)) | ||
img = np.array(img, dtype=np.uint8) | ||
# load label | ||
lbl_file = self.lbl_names[index] | ||
lbl = PIL.Image.open(lbl_file) | ||
# lbl = lbl.resize((256, 256)) | ||
lbl = np.array(lbl, dtype=np.int32) | ||
lbl[lbl != 0] = 1 | ||
# load depth | ||
depth_file = self.depth_names[index] | ||
depth = PIL.Image.open(depth_file) | ||
# depth = depth.resize(256, 256) | ||
depth = np.array(depth, dtype=np.uint8) | ||
|
||
|
||
|
||
if self._transform: | ||
return self.transform(img, lbl, depth) | ||
else: | ||
return img, lbl, depth | ||
|
||
|
||
# Translating numpy_array into format that pytorch can use on Code. | ||
def transform(self, img, lbl, depth): | ||
|
||
img = img.astype(np.float64)/255.0 | ||
img -= self.mean_rgb | ||
img /= self.std_rgb | ||
img = img.transpose(2, 0, 1) # to verify | ||
img = torch.from_numpy(img).float() | ||
lbl = torch.from_numpy(lbl).long() | ||
depth = depth.astype(np.float64)/255.0 | ||
depth = torch.from_numpy(depth).float() | ||
return img, lbl, depth | ||
|
||
|
||
class MyTestData(data.Dataset): | ||
""" | ||
load data in a folder | ||
""" | ||
mean_rgb = np.array([0.447, 0.407, 0.386]) | ||
std_rgb = np.array([0.244, 0.250, 0.253]) | ||
|
||
|
||
def __init__(self, root, transform=False): | ||
super(MyTestData, self).__init__() | ||
self.root = root | ||
self._transform = transform | ||
|
||
img_root = os.path.join(self.root, 'test_images') | ||
depth_root = os.path.join(self.root, 'test_depth') | ||
file_names = os.listdir(img_root) | ||
self.img_names = [] | ||
self.names = [] | ||
self.depth_names = [] | ||
|
||
for i, name in enumerate(file_names): | ||
if not name.endswith('.jpg'): | ||
continue | ||
self.img_names.append( | ||
os.path.join(img_root, name) | ||
) | ||
self.names.append(name[:-4]) | ||
self.depth_names.append( | ||
# os.path.join(depth_root, name[:-4]+'_depth.png') # Test RGBD135 dataset | ||
os.path.join(depth_root, name[:-4] + '.png') | ||
) | ||
|
||
def __len__(self): | ||
return len(self.img_names) | ||
|
||
def __getitem__(self, index): | ||
# load image | ||
img_file = self.img_names[index] | ||
img = PIL.Image.open(img_file) | ||
img_size = img.size | ||
# img = img.resize((256, 256)) | ||
img = np.array(img, dtype=np.uint8) | ||
|
||
# load focal | ||
depth_file = self.depth_names[index] | ||
depth = PIL.Image.open(depth_file) | ||
# depth = depth.resize(256, 256) | ||
depth = np.array(depth, dtype=np.uint8) | ||
if self._transform: | ||
img, focal = self.transform(img, depth) | ||
return img, focal, self.names[index], img_size | ||
else: | ||
return img, depth, self.names[index], img_size | ||
|
||
def transform(self, img, depth): | ||
img = img.astype(np.float64)/255.0 | ||
img -= self.mean_rgb | ||
img /= self.std_rgb | ||
img = img.transpose(2, 0, 1) | ||
img = torch.from_numpy(img).float() | ||
|
||
depth = depth.astype(np.float64)/255.0 | ||
depth = torch.from_numpy(depth).float() | ||
|
||
return img, depth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import torch | ||
from scipy.misc import imresize | ||
|
||
def imsave(file_name, img, img_size): | ||
""" | ||
save a torch tensor as an image | ||
:param file_name: 'image/folder/image_name' | ||
:param img: 3*h*w torch tensor | ||
:return: nothing | ||
""" | ||
assert(type(img) == torch.FloatTensor, | ||
'img must be a torch.FloatTensor') | ||
ndim = len(img.size()) | ||
assert(ndim == 2 or ndim == 3, | ||
'img must be a 2 or 3 dimensional tensor') | ||
|
||
img = img.numpy() | ||
img = imresize(img, [img_size[1][0], img_size[0][0]], interp='nearest') | ||
if ndim == 3: | ||
plt.imsave(file_name, np.transpose(img, (1, 2, 0))) | ||
else: | ||
plt.imsave(file_name, img, cmap='gray') |
Oops, something went wrong.