| 
11 | 11 | parse = argparse.ArgumentParser()  | 
12 | 12 | parse.add_argument('--im_root', dest='im_root', type=str, default='./datasets/cityscapes',)  | 
13 | 13 | parse.add_argument('--im_anns', dest='im_anns', type=str, default='./datasets/cityscapes/train.txt',)  | 
 | 14 | +parse.add_argument('--lb_ignore', dest='lb_ignore', type=int, default=255)  | 
14 | 15 | args = parse.parse_args()  | 
15 | 16 | 
 
  | 
 | 17 | +lb_ignore = args.lb_ignore  | 
 | 18 | + | 
16 | 19 | 
 
  | 
17 | 20 | with open(args.im_anns, 'r') as fr:  | 
18 | 21 |     lines = fr.read().splitlines()  | 
 | 
54 | 57 |     if shape[1] < min_shape_width[1]:  | 
55 | 58 |         min_shape_width = shape  | 
56 | 59 | 
 
  | 
57 |  | -    max_lb_val = max(max_lb_val, np.max(lb.ravel()))  | 
58 |  | -    min_lb_val = min(min_lb_val, np.min(lb.ravel()))  | 
59 |  | - | 
 | 60 | +    lb = lb[lb != lb_ignore]  | 
 | 61 | +    if lb.size > 0:  | 
 | 62 | +        max_lb_val = max(max_lb_val, np.max(lb))  | 
 | 63 | +        min_lb_val = min(min_lb_val, np.min(lb))  | 
60 | 64 | 
 
  | 
 | 65 | +min_lb_val = 0  | 
 | 66 | +max_lb_val = 181  | 
 | 67 | +lb_minlength = 182  | 
61 | 68 | ## label info  | 
62 | 69 | lb_minlength = max_lb_val+1-min_lb_val  | 
63 | 70 | lb_hist = np.zeros(lb_minlength)  | 
64 |  | -for impth in tqdm(impaths):  | 
65 |  | -    lb = cv2.imread(lbpth, 0).ravel() + min_lb_val  | 
 | 71 | +for lbpth in tqdm(lbpaths):  | 
 | 72 | +    lb = cv2.imread(lbpth, 0)  | 
 | 73 | +    lb = lb[lb != lb_ignore] + min_lb_val  | 
66 | 74 |     lb_hist += np.bincount(lb, minlength=lb_minlength)  | 
67 | 75 | 
 
  | 
68 | 76 | lb_missing_vals = [ind + min_lb_val  | 
 | 
75 | 83 | n_pixels = 0  | 
76 | 84 | for impth in tqdm(impaths):  | 
77 | 85 |     im = cv2.imread(impth)[:, :, ::-1].astype(np.float32)  | 
78 |  | -    im = im.reshape(-1, 3)  | 
 | 86 | +    im = im.reshape(-1, 3) / 255.  | 
79 | 87 |     n_pixels += im.shape[0]  | 
80 | 88 |     rgb_mean += im.sum(axis=0)  | 
81 |  | -rgb_mean = rgb_mean / n_pixels  | 
 | 89 | +rgb_mean = (rgb_mean / n_pixels)  | 
82 | 90 | 
 
  | 
83 | 91 | rgb_std = np.zeros(3).astype(np.float32)  | 
84 | 92 | for impth in tqdm(impaths):  | 
85 | 93 |     im = cv2.imread(impth)[:, :, ::-1].astype(np.float32)  | 
86 |  | -    im = im.reshape(-1, 3)  | 
 | 94 | +    im = im.reshape(-1, 3) / 255.  | 
87 | 95 | 
 
  | 
88 | 96 |     a = (im - rgb_mean.reshape(1, 3)) ** 2  | 
89 | 97 |     rgb_std += a.sum(axis=0)  | 
90 |  | -rgb_std = (rgb_std / n_pixels) ** (0.5)  | 
 | 98 | +rgb_std = (rgb_std / n_pixels) ** 0.5  | 
 | 99 | + | 
 | 100 | +rgb_mean  = rgb_mean.tolist()  | 
 | 101 | +rgb_std  = rgb_std.tolist()  | 
91 | 102 | 
 
  | 
92 | 103 | 
 
  | 
 | 104 | +print('\n')  | 
93 | 105 | print(f'there are {n_pairs} lines in {args.im_anns}, which means {n_pairs} image/label image pairs')  | 
94 | 106 | print('\n')  | 
95 | 107 | 
 
  | 
96 |  | -print('max and min image shapes by area are: ')  | 
97 |  | -print(f'\t{max_shape_area}, {min_shape_area}')  | 
98 |  | -print('max and min image shapes by height are: ')  | 
99 |  | -print(f'\t{max_shape_height}, {min_shape_height}')  | 
100 |  | -print('max and min image shapes by width are: ')  | 
101 |  | -print(f'\t{max_shape_width}, {min_shape_width}')  | 
 | 108 | +print(f'max and min image shapes by area are: {max_shape_area}, {min_shape_area}')  | 
 | 109 | +print(f'max and min image shapes by height are: {max_shape_height}, {min_shape_height}')  | 
 | 110 | +print(f'max and min image shapes by width are: {max_shape_width}, {min_shape_width}')  | 
102 | 111 | print('\n')  | 
103 | 112 | 
 
  | 
104 |  | -print(f'label values are within range of ({min_lb_val}, {max_lb_val})')  | 
105 |  | -print('label values that are missing: ')  | 
106 |  | -print('\t', lb_missing_vals)  | 
 | 113 | +print(f'we ignore label value of {args.lb_ignore} in label images')  | 
 | 114 | +print(f'label values are within range of [{min_lb_val}, {max_lb_val}]')  | 
 | 115 | +print(f'label values that are missing: {lb_missing_vals}')  | 
107 | 116 | print('ratios of each label value: ')  | 
108 | 117 | print('\t', lb_ratios)  | 
109 | 118 | print('\n')  | 
110 | 119 | 
 
  | 
111 |  | -print('pixel mean rgb: ', mean)  | 
112 |  | -print('pixel std rgb: ', std)  | 
 | 120 | +print('pixel mean rgb: ', rgb_mean)  | 
 | 121 | +print('pixel std rgb: ', rgb_std)  | 
0 commit comments