Skip to content

Commit ae5a376

Browse files
committed
Fix path and parcel masking in predictions visualization script
1 parent 3895a5f commit ae5a376

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

visualize_predictions.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def get_window(idx, window_len, image_size, coco_file):
4949
parser.add_argument('--image_idx', nargs='+', required=False,
5050
help='A list of indices of the image batches to evaluate on. If not given, a random one is chosen.')
5151

52-
parser.add_argument('--parcel_loss', action='store_true', default=False, required=False,
53-
help='Use a loss function that takes into account parcel pixels only.')
52+
parser.add_argument('--mask_parcels', action='store_true', default=False, required=False,
53+
help='Mask non-parcel pixels, i.e. predictions for non-parcel pixels will be discarded.')
5454

5555
parser.add_argument('--binary_labels', action='store_true', default=False, required=False,
5656
help='Map categories to 0 background, 1 parcel. Default False')
@@ -120,14 +120,15 @@ def get_window(idx, window_len, image_size, coco_file):
120120

121121
# Normalize paths for different OSes
122122
root_path_coco = Path(args.root_path_coco)
123-
netcdf_path = Path(netcdf_path)
123+
netcdf_path = Path(args.netcdf_path)
124124

125125
# Check existence of data folder
126126
if not root_path_coco.is_dir():
127127
print(f'{font_colors.RED}Coco path doesn\'t exist!{font_colors.ENDC}')
128128
exit(1)
129129

130130
run_path = Path(*Path(args.load_checkpoint).parts[:-2])
131+
print(f'Exporting to: {run_path}')
131132

132133
if args.binary_labels:
133134
n_classes = 2
@@ -137,7 +138,7 @@ def get_window(idx, window_len, image_size, coco_file):
137138
if args.model == 'convlstm':
138139
args.img_size = [int(dim) for dim in args.img_size]
139140

140-
model = ConvLSTM(run_path, LINEAR_ENCODER, parcel_loss=args.parcel_loss)
141+
model = ConvLSTM(run_path, LINEAR_ENCODER, parcel_loss=args.mask_parcels)
141142

142143
# Load the model for testing
143144
checkpoint_epoch = Path(args.load_checkpoint).stem.split('=')[1].split('-')[0]
@@ -149,7 +150,7 @@ def get_window(idx, window_len, image_size, coco_file):
149150
elif args.model == 'convstar':
150151
args.img_size = [int(dim) for dim in args.img_size]
151152

152-
model = ConvSTAR(run_path, LINEAR_ENCODER, parcel_loss=args.parcel_loss)
153+
model = ConvSTAR(run_path, LINEAR_ENCODER, parcel_loss=args.mask_parcels)
153154

154155
# Load the model for testing
155156
checkpoint_epoch = Path(args.load_checkpoint).stem.split('=')[1].split('-')[0]
@@ -161,7 +162,7 @@ def get_window(idx, window_len, image_size, coco_file):
161162
elif args.model == 'unet':
162163
args.img_size = [int(dim) for dim in args.img_size]
163164

164-
model = UNet(run_path, LINEAR_ENCODER, parcel_loss=args.parcel_loss, num_layers=3)
165+
model = UNet(run_path, LINEAR_ENCODER, parcel_loss=args.mask_parcels, num_layers=3)
165166

166167
# Load the model for testing
167168
checkpoint_epoch = Path(args.load_checkpoint).stem.split('=')[1].split('-')[0]
@@ -198,7 +199,7 @@ def get_window(idx, window_len, image_size, coco_file):
198199
batch_size=1,
199200
num_workers=args.num_workers,
200201
binary_labels=args.binary_labels,
201-
return_parcels=args.parcel_loss
202+
return_parcels=args.mask_parcels
202203
)
203204

204205
# TRAINING
@@ -246,14 +247,17 @@ def get_window(idx, window_len, image_size, coco_file):
246247

247248
pred = pred.to(torch.float32)
248249

249-
parcels = torch.from_numpy(batch['parcels'])[None, :, :].cuda() # (B, H, W)
250-
parcels_K = parcels[:, None, :, :].repeat(1, pred.size(1), 1, 1) # (B, K, H, W)
251-
#pred = torch.clamp(pred, 0, max(LINEAR_ENCODER.values()))
250+
if args.mask_parcels:
251+
parcels = torch.from_numpy(batch['parcels'])[None, :, :].cuda() # (B, H, W)
252+
parcels_K = parcels[:, None, :, :].repeat(1, pred.size(1), 1, 1) # (B, K, H, W)
253+
#pred = torch.clamp(pred, 0, max(LINEAR_ENCODER.values()))
252254

253-
label = torch.from_numpy(batch['labels'][None, :, :]).to(torch.long).cuda() # (B, H, W)
255+
label = torch.from_numpy(batch['labels'][None, :, :]).to(torch.long).cuda() # (B, H, W)
254256

255-
mask_K = (parcels_K) & (label[:, None, :, :].repeat(1, pred.size(1), 1, 1) != 0)
256-
pred[~mask_K] = 0
257+
mask_K = (parcels_K) & (label[:, None, :, :].repeat(1, pred.size(1), 1, 1) != 0)
258+
pred[~mask_K] = 0
259+
else:
260+
label = torch.from_numpy(batch['labels'][None, :, :]).to(torch.long).cuda() # (B, H, W)
257261

258262
pred_sparse = pred.argmax(axis=1).squeeze().cpu().detach().numpy()
259263

0 commit comments

Comments
 (0)