|
| 1 | +import torch |
| 2 | +try: |
| 3 | + from .abstract_datasets import DetectionDataset |
| 4 | +except: |
| 5 | + from abstract_datasets import DetectionDataset |
| 6 | +import cv2 |
| 7 | +import os |
| 8 | +import numpy as np |
| 9 | +import json |
| 10 | +try: |
| 11 | + import datasets.preprocessing_transforms as pt |
| 12 | +except: |
| 13 | + import preprocessing_transforms as pt |
| 14 | + |
| 15 | +class DHF1K(DetectionDataset): |
| 16 | + def __init__(self, *args, **kwargs): |
| 17 | + super(DHF1K, self).__init__(*args, **kwargs) |
| 18 | + |
| 19 | + # Get model object in case preprocessing other than default is used |
| 20 | + self.model_object = kwargs['model_obj'] |
| 21 | + self.load_type = kwargs['load_type'] |
| 22 | + |
| 23 | + print(self.load_type) |
| 24 | + if self.load_type=='train': |
| 25 | + self.transforms = kwargs['model_obj'].train_transforms |
| 26 | + |
| 27 | + else: |
| 28 | + self.transforms = kwargs['model_obj'].test_transforms |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + def __getitem__(self, idx): |
| 34 | + vid_info = self.samples[idx] |
| 35 | + |
| 36 | + |
| 37 | + base_path = vid_info['base_path'] |
| 38 | + vid_size = vid_info['frame_size'] |
| 39 | + |
| 40 | + input_data = [] |
| 41 | + map_data = [] |
| 42 | + bin_data = [] |
| 43 | + |
| 44 | + for frame_ind in range(len(vid_info['frames'])): |
| 45 | + frame = vid_info['frames'][frame_ind] |
| 46 | + frame_path = frame['img_path'] |
| 47 | + map_path = frame['map_path'] |
| 48 | + bin_path = frame['bin_path'] |
| 49 | + |
| 50 | + # Load frame, convert to RGB from BGR and normalize from 0 to 1 |
| 51 | + input_data.append(cv2.imread(os.path.join(base_path, frame_path))[...,::-1]/255.) |
| 52 | + |
| 53 | + # Load frame, Normalize from 0 to 1 |
| 54 | + # All frame channels have repeated values |
| 55 | + map_data.append(cv2.imread(map_path)/255.) |
| 56 | + bin_data.append(cv2.imread(bin_path)/255.) |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + vid_data = self.transforms(input_data) |
| 61 | + |
| 62 | + # Annotations must be resized in the loss/metric |
| 63 | + map_data = torch.Tensor(map_data) |
| 64 | + bin_data = torch.Tensor(bin_data) |
| 65 | + |
| 66 | + # Permute the PIL dimensions (Frame, Height, Width, Chan) to pytorch (Chan, frame, height, width) |
| 67 | + vid_data = vid_data.permute(3, 0, 1, 2) |
| 68 | + map_data = map_data.permute(3, 0, 1, 2) |
| 69 | + bin_data = bin_data.permute(3, 0, 1, 2) |
| 70 | + # All channels are repeated so remove the unnecessary channels |
| 71 | + map_data = map_data[0].unsqueeze(0) |
| 72 | + bin_data = bin_data[0].unsqueeze(0) |
| 73 | + |
| 74 | + |
| 75 | + ret_dict = dict() |
| 76 | + ret_dict['data'] = vid_data |
| 77 | + |
| 78 | + annot_dict = dict() |
| 79 | + annot_dict['map'] = map_data |
| 80 | + annot_dict['bin'] = bin_data |
| 81 | + annot_dict['input_shape'] = vid_data.size() |
| 82 | + annot_dict['name'] = base_path |
| 83 | + ret_dict['annots'] = annot_dict |
| 84 | + |
| 85 | + return ret_dict |
| 86 | + |
| 87 | + |
| 88 | +if __name__=='__main__': |
| 89 | + |
| 90 | + class tts(): |
| 91 | + def __call__(self, x): |
| 92 | + return pt.ToTensorClip()(x) |
| 93 | + class debug_model(): |
| 94 | + def __init__(self): |
| 95 | + self.train_transforms = tts() |
| 96 | + |
| 97 | + |
| 98 | + json_path = '/path/to/DHF1K' #### Change this when testing #### |
| 99 | + |
| 100 | + |
| 101 | + dataset = DHF1K(model_obj=debug_model(), json_path=json_path, load_type='train', clip_length=16, clip_offset=0, clip_stride=1, num_clips=0, random_offset=0, resize_shape=0, crop_shape=0, crop_type='Center', final_shape=0, batch_size=1) |
| 102 | + train_loader = torch.utils.data.DataLoader(dataset=dataset, batch_size=1, shuffle=False) |
| 103 | + |
| 104 | + |
| 105 | + import matplotlib.pyplot as plt |
| 106 | + for x in enumerate(train_loader): |
| 107 | + dat = x[1]['data'][0,:,0].permute(1,2,0).numpy() |
| 108 | + bin = x[1]['annots']['bin'][0,:,0].permute(1,2,0).numpy().repeat(3,axis=2) |
| 109 | + map = x[1]['annots']['map'][0,:,0].permute(1,2,0).numpy().repeat(3, axis=2) |
| 110 | + img = np.concatenate([dat,bin,map], axis=0) |
| 111 | + plt.imshow(img) |
| 112 | + plt.show() |
| 113 | + import pdb; pdb.set_trace() |
0 commit comments