Esempio n. 1
0
 def __init__(self,importer_path,useCache=True,cacheDir='/media/data_cifs/lu/cache',sequence,Nmax,batch_size):
     """Constructor for the DataLoader class.
     :param importer_path: Path to the input dataset
     :param useCache: Flag to use cached data from a pickle file
     :param cacheDir: Directory where the cached pickle file is stored (Only if useCache is set)
     :param batch_size: Batch size to fetch data
     :param sequence: Sequence to load, eg. train/val/test
     :param Nmax: Maximum number of images to load
     """
     self.batch_size = batch_size
     self.importer = NYUImporter(importer_path,useCache=useCache,cacheDir=cacheDir)
     self.sequence = sequence
     self.imgSeq = importer.loadSequence(sequence,Nmax=Nmax,allJoints=True)
     self.dataset = NYUDataset(imgSeqs=[self.imgSeq])
     self.data, self.labels, seqconfig, train_com3D,train_M = dataset.imgStackDepthOnly(sequence)
     self.data = self.data.squeeze(1)
     self.nImgs = len(self.data)
Esempio n. 2
0
def main():
    bs = 8
    sz = (512, 512)
    mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
    mean, std = torch.tensor(mean), torch.tensor(std)
    unnormalize = UnNormalizeImgBatch(mean, std)

    tfms = transforms.Compose([
        ResizeImgAndDepth(sz),
        ImgAndDepthToTensor()  #,      
        #NormalizeImg(mean, std)
    ])

    ds = NYUDataset('/home/pebert/lcnn/', tfms)
    dl = torch.utils.data.DataLoader(ds, bs, shuffle=True)
    for i in range(len(ds)):
        pth = 'utils/images/img_depth' + str(i) + '.jpg'
        if i > 200:
            save_image(ds[i][1], pth)
        if i > 220:
            break
Esempio n. 3
0
def Datareader(config):
    print("create training data")
    cwd = os.getcwd()
    print(cwd)
    rng = np.random.RandomState(23455)
    ds = NYUImporter('/media/data_cifs/lu/NYU/dataset', useCache=True)
    #Seq1= ds.loadAugSequence('train', shuffle=True, rng=rng, docom=True,allJoints=True,num_aug=1,Nmax=1)
    Seq1 = ds.loadSequence('train',
                           shuffle=True,
                           rng=rng,
                           docom=True,
                           allJoints=True)
    trainSeqs = [Seq1]

    trainDataSet = NYUDataset(imgSeqs=trainSeqs)
    #trainDataSet.check()
    train_data, train_gt3D, seqconfig, train_com3D, train_M = trainDataSet.imgStackDepthOnly(
        'train')

    # print("shape of train_data {}".format(train_data.shape))
    # print("shape of train lable {}".format(train_gt3D.shape))
    # print("seqconfig:{}".format(seqconfig))

    # #check if the data is already now
    # for check_num in range(val_data.shape[0]):
    #     if (check_num > 10) and (check_num<15):
    #         check_data = val_data[check_num]
    #         check_gt3D = val_gt3D[check_num]
    #         check_com3D = val_com3D[check_num]
    #         check_M = val_M[check_num]
    #         print("shape of check data:{}".format(check_data.shape))
    #         print("shape of check gt3D:{}".format(check_gt3D.shape))
    #         print("shape of check com3D:{}".format(check_com3D.shape))
    #         print("shape of check M:{}".format(check_M.shape))
    #         jtorig = check_gt3D * seqconfig['cube'][2] / 2.
    #         jcrop = trans3DsToImg(jtorig, check_com3D, check_M)
    #         im_g = check_data.reshape([128,128])
    #         # showImageLable(im_g,jcrop)
    #         showImageJoints(im_g, jcrop)

    print('Get {} training data.'.format(train_data.shape[0]))
    create_tf_record(train_data, train_gt3D,
                     os.path.join(config.tfrecord_dir, config.train_tfrecords),
                     config, train_com3D, train_M)
    print('create testing data and validation data')
    Seq2 = ds.loadSequence('test',
                           shuffle=True,
                           rng=rng,
                           docom=True,
                           allJoints=True)
    testSeqs = [Seq2]
    testDataSet = NYUDataset(imgSeqs=testSeqs, val_prop=0.3)
    test_data, test_gt3D, val_data, val_gt3D, testconfig, test_com3D, val_com3D, test_M, val_M = testDataSet.imgStackDepthOnly(
        'test')
    create_tf_record(val_data, val_gt3D,
                     os.path.join(config.tfrecord_dir, config.val_tfrecords),
                     config, val_com3D, val_M)
    create_tf_record(test_data, test_gt3D,
                     os.path.join(config.tfrecord_dir, config.test_tfrecords),
                     config, test_com3D, test_M)
    print('Get {} test data'.format(test_data.shape[0]))
    print('Get {} validation data'.format(val_data.shape[0]))

    return seqconfig
Esempio n. 4
0
        type=str,
        required=True,
        help=
        "Extension of the samples, should only match inputs or ground_truths but not both."
    )

    args = parser.parse_args()

    # Load train partition:
    train_depth = np.load(args.dataset_partitions,
                          allow_pickle=True).item()['train']

    # Create dataset instance and dataloader
    # Select dataset loader
    if args.dataset.lower() == 'nyudataset':
        dataset = NYUDataset(train_depth, is_train=False)

    means = []
    stds = []
    vec_maxs = []
    vec_mins = []
    depth_bins = set()
    for _idx in tqdm(range(len(train_depth))):
        # Read and convert to numpies
        depth, rgb, path = dataset.__getitem__(_idx)
        depth, rgb = np.array(depth), np.array(rgb, dtype=float) / 255.
        #print(depth.shape, rgb.shape)
        depth_bins.update(set(list(depth.flatten())))
        # Calculate interesting data
        mean = np.mean(rgb, axis=(0, 1))
        std = np.std(rgb, axis=(0, 1))
Esempio n. 5
0
import math

bs = 8
sz = (320,240)
mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
mean, std = torch.tensor(mean), torch.tensor(std)
unnormalize = UnNormalizeImgBatch(mean, std)

tfms = transforms.Compose([
    ResizeImgAndDepth(sz),
    RandomHorizontalFlip(),
    ImgAndDepthToTensor(),
    NormalizeImg(mean, std)
])

ds = NYUDataset('/content/gdrive/My Drive/data/', tfms) # 경록
dl = torch.utils.data.DataLoader(ds, bs, shuffle=True)

model = Net()
model.to(device)

model.load_state_dict(torch.load('/content/gdrive/My Drive/data/answer.ckpt', map_location="cpu")) # 경록
criterion = nn.MSELoss()


model.eval()
error_0 = 0 # scale-Invariant Error
error_1 = 0 # RMS linear
error_2 = 0 # RMS log
error_3 = 0 # abs rel
error_4 = 0 # sqr rel
# f = h5py.File(filename, 'r')
bs = 32  # batch size
train_transform = transforms.Compose([
    # transforms.Resize((304, 228)),
    # transforms.RandomRotation(degrees=(-5,5)), # doesnt work
    # transforms.RandomCrop((304, 228)),
    transforms.RandomCrop((228, 304)),
    transforms.RandomHorizontalFlip(),
    transforms.Resize((228, 304)),
])
test_transform = transforms.Compose([
    # transforms.Resize((304, 228)),
    transforms.Resize((228, 304)),
])
train_loader = DataLoader(NYUDataset(filename,
                                     'training',
                                     transform=train_transform),
                          batch_size=bs,
                          drop_last=True,
                          shuffle=True)
validation_loader = DataLoader(NYUDataset(filename,
                                          'validation',
                                          transform=test_transform),
                               batch_size=bs,
                               drop_last=True,
                               shuffle=False)

test_loader = DataLoader(NYUDataset(filename, 'test',
                                    transform=test_transform),
                         batch_size=1,
                         drop_last=True,
Esempio n. 7
0
import numpy as np
from REN import REN
from Importer import NYUImporter
from dataset import NYUDataset

#Script to train REN for hand pose estimation
dataset_path = '/media/data_cifs/lu/NYU/dataset/' #Path where dataset splits train, test and val are stored
cache_path = '/media/data_cifs/lu/cache' #Directory where cache files are stored while reading the dataset
useCache = True
Nmax = 40000
sequence = 'train'
print_loss_every = 100
save_model_every = 5000
imp = NYUImporter(dataset_path,useCache=useCache,cacheDir=cache_path)
trainSeq = imp.loadSequence(sequence,Nmax=Nmax,allJoints=True) #Sequence of training images
dataset = NYUDataset([trainSeq])
train_data, train_gt3D, seqconfig, train_com3D,train_M = dataset.imgStackDepthOnly(sequence)

def fetch_curr_batch(batch_size):
    """
    Function to yield a batch of depth images and labels
    :param batch_size: Yield batch_size number of images and labels
    """
    idxs = np.random.choice(Nmax,batch_size)
    yield train_data[idxs],train_gt3D[idxs]


def run(batch_size,Nmax,l2_lambda,n_epochs,learning_rate,init_vars=True):
        ren = REN(batch_size,Nmax,l2_lambda=l2_lambda)
        ren.init_variables()
        ren.build_model()
Esempio n. 8
0
n_epochs = 20
lr = 2e-5
bs = 8
sz = (320,240)
seed = np.random.seed(1)
mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
mean, std = torch.tensor(mean), torch.tensor(std)
unnormalize = UnNormalizeImgBatch(mean, std)
tfms = transforms.Compose([
    ResizeImgAndDepth(sz),
    RandomHorizontalFlip(),
    ImgAndDepthToTensor(),
    NormalizeImg(mean, std)
])
ds = NYUDataset('data/', tfms)
dl = torch.utils.data.DataLoader(ds, bs, shuffle=True)
train_loader, val_loader, test_loader = ds.create_split_loaders(bs, seed, tfms,0.1, 0.1,True)

# i = 1
# plot_utils.plot_image(get_unnormalized_ds_item(unnormalize, ds[i]))

model = Net()
model.to(device)
output_dir = "nyu"
make_dir(output_dir)
images_dir = os.path.join(output_dir,"saved_images")
make_dir(images_dir)
epoch_tracker = EpochTracker(os.path.join(output_dir, "nyu_epoch.txt"))

if epoch_tracker.epoch > 0: