Beispiel #1
0
    def __getitem__(self, index):
        if self.mode == 0:
            img = None
            while img is None:
                with open(self.c_imgs[index], 'rb') as f:
                    img = Image.open(f).convert('RGB')
                index += 1
            index -= 1
        elif self.mode == 1:
            img = None
            while img is None:
                img = load_image(self.d_imgs[index])
                index += 1
            index -= 1
        elif self.mode == 2:
            c_img = None
            d_img = None
            while (c_img is None) or (d_img is None):
                c_img = load_image(self.c_imgs[index])
                d_img = load_image(self.d_imgs[index])
                index += 1
            img = [c_img, d_img]
            index -= 1
        else:
            raise Exception('Wrong mode {:d}'.format(self.mode))

        if self.transform is not None:
            if self.mode == 2:
                img = [self.transform(i) for i in img]
            else:
                img = self.transform(img)

        return img
Beispiel #2
0
 def __img_resize__(self, img_path, transform, resolution, semantic=False):
     """# Check if image needs to be resized (Nope too slow)
     old_img = Image.open(img_path)
     o_w, o_h = old_img.size
     if o_w == resolution == o_h:
         return img_path
     """
     # Resize image
     direct, name = os.path.split(img_path)
     new_dir = os.path.join(direct, 'resized')
     if not os.path.isdir(new_dir):
         os.mkdir(new_dir)
     if new_dir not in self.resize_directories:
         self.resize_directories[new_dir] = set(os.listdir(new_dir))
     new_name = 'resized_{}px_{:s}'.format(resolution, name)
     new_path = os.path.join(new_dir, new_name)
     if new_name not in self.resize_directories[new_dir]:
         if semantic:
             img = Image.open(img_path)
         else:
             img = load_image(img_path)
         try:
             img = transform(img)
         except:
             print('Problem with image {}'.format(img_path))
             exit()
         img.save(new_path)
     return new_path
Beispiel #3
0
    def __getitem__(self, index):
        point = self.points[index]
        inps = []
        outs = []
        for inp in self.input_types:
            if inp == 'img':
                img_path = os.path.join(self.data_path, self.scene,
                                        point.img_path)
                img = load_image(img_path)
                img = None if img is None else self.transforms['img'](img)
                inps.append(img)
            elif inp == 'label':
                img = single_channel_loader(point.sem_path)
                img = None if img is None else self.transforms['label'](img)
                inps.append(img)
            else:
                raise NotImplementedError('Not implemented yet (%s)' %
                                          str(inp))
        for out in self.output_types:
            if out == 'pose':
                p = point.pose
                p = None if p is None else self.transforms['pose'](p)
                outs.append(p)
            elif out == 'label':
                #print('GET SEMANTIC LAB')
                img = single_channel_loader(point.sem_path)
                #print(img.shape)
                img = None if img is None else self.transforms['label'](img)
                #print(img.shape)
                outs.append(img)
            else:
                raise NotImplementedError('Not implemented yet')

        return inps[0] if len(inps) <= 1 else inps, outs[0] if len(
            outs) <= 1 else outs
Beispiel #4
0
 def __getitem__(self, index):
     """
     Augmentation index defines which type of images is returned:
     0: standard daytime image
     1: artificial nighttime images
     >=2: stylized images
     """
     augmentation_index = self.types[index]
     inps = []
     outs = []
     img_sample = self.images[index % len(self.images)]
     for inp in self.input_types:
         if inp == 'img':
             if augmentation_index == 0:
                 img_path = img_sample.path
             elif augmentation_index == 1:
                 img_path = img_sample.nighttime
             elif augmentation_index >= 2:
                 img_path = img_sample.stylized[augmentation_index - 2]
             else:
                 raise NotImplementedError(
                     'Whats going on? Augmentation index: {:s}\t Index: {:d}'
                     .format(str(augmentation_index), index))
             img = load_image(img_path)
             img = None if img is None else self.transforms['img'](img)
             inps.append(img)
         elif inp == 'label':
             img = single_channel_loader(img_sample.semantic)
             img = None if img is None else self.transforms['label'](img)
             inps.append(img)
         elif inp == 'point_cloud':
             pc = self.sift_points[img_sample.point_cloud]
             #Normalize
             #print('----------- NORMALIZE ---------')
             #print(pc.shape)
             #print(pc.mean(axis=1).shape)
             pc = pc - pc.mean(axis=0)
             pc = pc / pc.std(axis=0)
             pc = pc.T
             #print(pc.mean(axis=1))
             #print(pc.std(axis=1))
             pc = None if pc is None else self.transforms['point_cloud'](pc)
             inps.append(pc)
         else:
             raise NotImplementedError('Not implemented yet (%s)' %
                                       str(inp))
     for out in self.output_types:
         if out == 'pose':
             p = img_sample.pose
             p = None if p is None else self.transforms['pose'](p).squeeze()
             outs.append(p)
         elif out == 'label':
             img = single_channel_loader(img_sample.semantic)
             img = None if img is None else self.transforms['label'](img)
             outs.append(img)
         else:
             raise NotImplementedError('Not implemented yet')
     return inps[0] if len(inps) <= 1 else tuple(
         inps), outs[0] if len(outs) <= 1 else outs
Beispiel #5
0
    def __getitem__(self, index):

        if self.skip_images:
            img = None
            pose = self.poses[index]
        else:
            if self.mode == 0:
                img = None
                while img is None:
                    img = load_image(self.c_imgs[index])
                    pose = self.poses[index]
                    index += 1
                index -= 1
            elif self.mode == 1:
                img = None
                while img is None:
                    img = load_image(self.d_imgs[index])
                    pose = self.poses[index]
                    index += 1
                index -= 1
            elif self.mode == 2:
                c_img = None
                d_img = None
                while (c_img is None) or (d_img is None):
                    c_img = load_image(self.c_imgs[index])
                    d_img = load_image(self.d_imgs[index])
                    pose = self.poses[index]
                    index += 1
                img = [c_img, d_img]
                index -= 1
            else:
                raise Exception('Wrong mode {:d}'.format(self.mode))

        if self.target_transform is not None:
            pose = self.target_transform(pose)

        if self.skip_images:
            return img, pose

        if self.transform is not None:
            if self.mode == 2:
                img = [self.transform(i) for i in img]
            else:
                img = self.transform(img)

        return img, pose
Beispiel #6
0
    def __getitem__(self, index):
        point = self.points[index]
        inps = []
        outs = []
        img_path = os.path.join(self.data_path, self.scene, point.img_path)
        img = load_image(img_path)
        img = self.transforms['img'](img)
        p = point.pose
        p = self.transforms['pose'](p)

        return img, p
Beispiel #7
0
 def __getitem__(self, index):
     augmentation_index = 0
     if self.night_augmentation:
         augmentation_index = index // self.len
         index = index % self.len
     elif self.only_augmentation:
         augmentation_index = 1
     inps = []
     outs = []
     for inp in self.input_types:
         if inp == 'img':
             if augmentation_index == 0:
                 img_path = os.path.join(self.data_path,
                                         self.img_paths[index])
             else:
                 img_path = self.night_img_paths[index]
             img = load_image(img_path)
             img = None if img is None else self.transforms['img'](img)
             inps.append(img)
         elif inp == 'label':
             img = single_channel_loader(self.sem_labels[index])
             img = None if img is None else self.transforms['label'](img)
             inps.append(img)
         elif inp == 'point_cloud':
             pc = self.sift_points[self.points_per_img[index]]
             #Normalize
             #print('----------- NORMALIZE ---------')
             #print(pc.shape)
             #print(pc.mean(axis=1).shape)
             pc = pc - pc.mean(axis=0)
             pc = pc / pc.std(axis=0)
             pc = pc.T
             #print(pc.mean(axis=1))
             #print(pc.std(axis=1))
             pc = None if pc is None else self.transforms['point_cloud'](pc)
             inps.append(pc)
         else:
             raise NotImplementedError('Not implemented yet (%s)' %
                                       str(inp))
     for out in self.output_types:
         if out == 'pose':
             p = self.poses[index]
             p = None if p is None else self.transforms['pose'](p).squeeze()
             outs.append(p)
         elif out == 'label':
             img = single_channel_loader(self.sem_labels[index])
             img = None if img is None else self.transforms['label'](img)
             outs.append(img)
         else:
             raise NotImplementedError('Not implemented yet')
     return inps[0] if len(inps) == 1 else tuple(inps), outs[0] if len(
         outs) == 1 else outs
    def __load_items__(self, index):
        img = load_image(os.path.join(self.dir_path, self.imgs[index].name))
        imfullsize = max(img.size)
        if self.imsize is not None:
            img = imresize(img, self.imsize)

        if self.transform is not None:
            img = self.transform(img)

        pt_ids = self.imgs[index].point3D_ids
        pts3d = np.vstack([self.pts[i].xyz for i in pt_ids])
        mean, std = np.mean(pts3d, axis=0), np.std(pts3d, axis=0)
        if self.demean:
            pts3d -= mean
            pts3d /= std
        pts3d = torch.from_numpy(pts3d).float()
        return img, pts3d
Beispiel #9
0
 def _loader(self, name, index):
     """
     Loads an image stored in the internal dataframe or gets an pose from the poses list
     
     :param name : string (valid names: 'left', 'right', 'label_colorized', 'label', 'depth')
         used to select the type of data to load 
     :param index: : int, the index of a certain image
     
     :returns : the loaded data
         if no path is given for the certain image, None is returned
     """
     if name == 'pose':
         return self.poses[index]
     else:
         path = self.df[name].iloc[index]
         if pd.isna(path):
             return None
         else:
             return load_image(path, self.LOADERS[name])
Beispiel #10
0
torch.manual_seed(seed)
if CUDA:
    torch.cuda.manual_seed(seed)
    model.cuda()

pred_poses = np.zeros((L, 7))  # store all predicted poses
targ_poses = np.zeros((L, 7))  # store all target poses

with open(args.import_file) as f:
    features_file = json.load(f)

results = {}
for uid, path in tqdm(features_file.items()):
    # import pdb; pdb.set_trace()
    image_filename = osp.join(data_dir, path)
    image = data_utils.load_image(image_filename)
    image_transformed = data_set.transform(image)
    features = model.feature_extractor(image_transformed.unsqueeze(0))
    results[uid] = features.detach().numpy()

np.save(args.export_file, results)
exit(0)

# inference loop
for batch_idx, (data, target) in enumerate(loader):
    if batch_idx % 200 == 0:
        print 'Image {:d} / {:d}'.format(batch_idx, len(loader))

    # indices into the global arrays storing poses
    if (args.model.find('vid') >= 0) or args.pose_graph:
        idx = data_set.get_indices(batch_idx)
Beispiel #11
0
# activate GPUs
CUDA = torch.cuda.is_available()
torch.manual_seed(seed)
if CUDA:
    torch.cuda.manual_seed(seed)
    model.cuda()

L = len(files)
pred_poses = np.zeros((L, 7))  # store all predicted poses

for i, file in tqdm.tqdm(enumerate(files), total=L):
    #if i % 200 == 0:
    #    print('Image {:d} / {:d}'.format(i, len(files)))
    
    #print('Load file %s'%file)
    data = load_image(file[1])
    data = data_transform(data).unsqueeze(0).unsqueeze(0)
    _, output, _ = step_feedfwd(data, model, CUDA, train=False)
    output = output[0]
    s = output.size()
    output = output.cpu().data.numpy().reshape((-1, s[-1]))
    q = [qexp(p[3:]) for p in output]
    output = np.hstack((output[:, :3], np.asarray(q)))
    output[:, :3] = (output[:, :3] * pose_s) + pose_m
    
    pred_poses[i, :] = output[len(output) // 2]
    
    
        
output_file = open('logs/'+args.output_file, 'w')
for i, file in enumerate(files):
Beispiel #12
0
    else:
        c.execute(
            '''CREATE TABLE global_features (image_id INTEGER PRIMARY_KEY NOT NULL, cols INTEGER, data BLOB)'''
        )
        if args.augmented:
            c.execute(
                '''CREATE TABLE global_augmented_features (image_id INTEGER PRIMARY_KEY NOT NULL, cols INTEGER, data BLOB)'''
            )

except sqlite3.Error as e:
    print(e)
    exit()

print('Loading image information')
images = get_images()
get_img = lambda i: load_image(args.data_path + images[i].name)
get_img_augmented = lambda i: load_image(
    os.path.join(args.augmented_path,
                 os.path.split(images[i].name)[-1]).replace('.jpg', '.png'))

print('Found %d images' % len(images))
predictions = []

times = []
model.eval()
print('Start processing images')
for cnt, i in enumerate(images.keys()):
    if cnt % (len(images) // 10) == 0:
        c.commit()
        print('%4d/%d' % (cnt, len(images)), end='\t')
        if len(times) > 0: