def run_inference(args): model = UNet(input_channels=3, num_classes=3) model.load_state_dict(torch.load(args.model_path, map_location='cpu'), strict=False) print('Log: Loaded pretrained {}'.format(args.model_path)) model.eval() # annus/Desktop/palsar/ test_image_path = '/home/annus/Desktop/palsar/palsar_dataset_full/palsar_dataset/palsar_{}_region_{}.tif'.format( args.year, args.region) test_label_path = '/home/annus/Desktop/palsar/palsar_dataset_full/palsar_dataset/fnf_{}_region_{}.tif'.format( args.year, args.region) inference_loader = get_inference_loader(image_path=test_image_path, label_path=test_label_path, model_input_size=128, num_classes=4, one_hot=True, batch_size=args.bs, num_workers=4) # we need to fill our new generated test image generated_map = np.empty(shape=inference_loader.dataset.get_image_size()) weights = torch.Tensor([1, 1, 2]) focal_criterion = FocalLoss2d(weight=weights) un_confusion_meter = tnt.meter.ConfusionMeter(2, normalized=False) confusion_meter = tnt.meter.ConfusionMeter(2, normalized=True) total_correct, total_examples = 0, 0 net_loss = [] for idx, data in enumerate(inference_loader): coordinates, test_x, label = data['coordinates'].tolist( ), data['input'], data['label'] out_x, softmaxed = model.forward(test_x) pred = torch.argmax(softmaxed, dim=1) not_one_hot_target = torch.argmax(label, dim=1) # convert to binary classes # 0-> noise, 1-> forest, 2-> non-forest, 3-> water pred[pred == 0] = 2 pred[pred == 3] = 2 not_one_hot_target[not_one_hot_target == 0] = 2 not_one_hot_target[not_one_hot_target == 3] = 2 # now convert 1, 2 to 0, 1 pred -= 1 not_one_hot_target -= 1 pred_numpy = pred.numpy().transpose(1, 2, 0) for k in range(test_x.shape[0]): x, x_, y, y_ = coordinates[k] generated_map[x:x_, y:y_] = pred_numpy[:, :, k] loss = focal_criterion( softmaxed, not_one_hot_target) # dice_criterion(softmaxed, label) # accurate = (pred == not_one_hot_target).sum().item() numerator = float(accurate) denominator = float( pred.view(-1).size(0)) # test_x.size(0) * dimension ** 2) total_correct += numerator total_examples += denominator net_loss.append(loss.item()) un_confusion_meter.add(predicted=pred.view(-1), target=not_one_hot_target.view(-1)) confusion_meter.add(predicted=pred.view(-1), target=not_one_hot_target.view(-1)) # if idx % 5 == 0: accuracy = float(numerator) * 100 / denominator print( '{}, {} -> ({}/{}) output size = {}, loss = {}, accuracy = {}/{} = {:.2f}%' .format(args.year, args.region, idx, len(inference_loader), out_x.size(), loss.item(), numerator, denominator, accuracy)) ################################# mean_accuracy = total_correct * 100 / total_examples mean_loss = np.asarray(net_loss).mean() print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$') print('log: test:: total loss = {:.5f}, total accuracy = {:.5f}%'.format( mean_loss, mean_accuracy)) print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$') print('---> Confusion Matrix:') print(confusion_meter.value()) # class_names = ['background/clutter', 'buildings', 'trees', 'cars', # 'low_vegetation', 'impervious_surfaces', 'noise'] with open('normalized.pkl', 'wb') as this: pkl.dump(confusion_meter.value(), this, protocol=pkl.HIGHEST_PROTOCOL) with open('un_normalized.pkl', 'wb') as this: pkl.dump(un_confusion_meter.value(), this, protocol=pkl.HIGHEST_PROTOCOL) # save_path = 'generated_maps/generated_{}_{}.npy'.format(args.year, args.region) save_path = '/home/annus/Desktop/palsar/generated_maps/using_separate_models/generated_{}_{}.npy'.format( args.year, args.region) np.save(save_path, generated_map) #########################################################################################3 inference_loader.dataset.clear_mem() pass
max_score = 0 offset = 2001 for epoch_idx in range(1, args.epochs + 1): losses, val_losses = 0, 0 dices, val_dices = 0, 0 for batch_idx, (image, mask) in enumerate(train_dataloader): x = image.to(device=cuda, dtype=torch.float32) y = mask.to(device=cuda, dtype=torch.float32) optimizer.zero_grad() masks_pred = unet.forward(x) loss = criterion(masks_pred, y) loss.backward() optimizer.step() losses += loss masks_pred = F.sigmoid(masks_pred) dice = dice_coeff(masks_pred, y) dices += dice # summary.add_scalar('train_loss', loss, epoch_idx) # summary.add_scalar('train_dice', dice, epoch_idx) # if (epoch_idx+1)%10 == 0 :
def run_inference(args): model = UNet(topology=args.model_topology, input_channels=len(args.bands), num_classes=len(args.classes)) model.load_state_dict(torch.load(args.model_path, map_location='cpu'), strict=False) print('Log: Loaded pretrained {}'.format(args.model_path)) model.eval() if args.cuda: print('log: Using GPU') model.cuda(device=args.device) # all_districts = ["abbottabad", "battagram", "buner", "chitral", "hangu", "haripur", "karak", "kohat", "kohistan", "lower_dir", "malakand", "mansehra", # "nowshehra", "shangla", "swat", "tor_ghar", "upper_dir"] all_districts = ["abbottabad"] # years = [2014, 2016, 2017, 2018, 2019, 2020] years = [2016] # change this to do this for all the images in that directory for district in all_districts: for year in years: print("(LOG): On District: {} @ Year: {}".format(district, year)) # test_image_path = os.path.join(args.data_path, 'landsat8_4326_30_{}_region_{}.tif'.format(year, district)) test_image_path = os.path.join(args.data_path, 'landsat8_{}_region_{}.tif'.format( year, district)) #added(nauman) inference_loader, adjustment_mask = get_inference_loader( rasterized_shapefiles_path=args.rasterized_shapefiles_path, district=district, image_path=test_image_path, model_input_size=128, bands=args.bands, num_classes=len(args.classes), batch_size=args.bs, num_workers=4) # inference_loader = get_inference_loader(rasterized_shapefiles_path=args.rasterized_shapefiles_path, district=district, # image_path=test_image_path, model_input_size=128, bands=args.bands, # num_classes=len(args.classes), batch_size=args.bs, num_workers=4) # we need to fill our new generated test image generated_map = np.empty( shape=inference_loader.dataset.get_image_size()) for idx, data in enumerate(inference_loader): coordinates, test_x = data['coordinates'].tolist( ), data['input'] test_x = test_x.cuda( device=args.device) if args.cuda else test_x out_x, softmaxed = model.forward(test_x) pred = torch.argmax(softmaxed, dim=1) pred_numpy = pred.cpu().numpy().transpose(1, 2, 0) if idx % 5 == 0: print('LOG: on {} of {}'.format(idx, len(inference_loader))) for k in range(test_x.shape[0]): x, x_, y, y_ = coordinates[k] generated_map[x:x_, y:y_] = pred_numpy[:, :, k] # adjust the inferred map generated_map += 1 # to make forest pixels: 2, non-forest pixels: 1, null pixels: 0 generated_map = np.multiply(generated_map, adjustment_mask) # save generated map as png image, not numpy array forest_map_rband = np.zeros_like(generated_map) forest_map_gband = np.zeros_like(generated_map) forest_map_bband = np.zeros_like(generated_map) forest_map_gband[generated_map == FOREST_LABEL] = 255 forest_map_rband[generated_map == NON_FOREST_LABEL] = 255 forest_map_for_visualization = np.dstack( [forest_map_rband, forest_map_gband, forest_map_bband]).astype(np.uint8) save_this_map_path = os.path.join( args.dest, '{}_{}_inferred_map.png'.format(district, year)) matimg.imsave(save_this_map_path, forest_map_for_visualization) print('Saved: {} @ {}'.format(save_this_map_path, forest_map_for_visualization.shape))
def train(epochs=10, lr=0.001, n_class=1, in_channel=1, loss_fn='BCE', display=False, save=False, \ load=False, directory='../Data/train/', img_size=None, data_size=None, load_file=None, save_file=None): #if torch.cuda.is_available(): # torch.cuda.set_device(1) # Dataset dataset = get_dataset(directory, img_size, data_size) #optimizer = torch.optim.SGD(model.parameters(), lr = lr, momentum = momentum, weight_decay = decay) print("Epochs:\t{}\nLearning Rate:\t{}\nOutput classes:\t{}\nInput channels:\t{}\n\ Loss function:\t{}\nImage cropping size:\t{}\nDataset size:\t{}\n" .format(epochs, lr, n_class, \ in_channel, loss_fn, img_size, data_size)) # Neural network model model = UNet(n_class, in_channel).cuda() if torch.cuda.is_available() else UNet( n_class, in_channel) # Optimizer optimizer = torch.optim.Adam(model.parameters(), lr=lr) loss_log = [] if load: get_checkpoint(model, optimizer, loss_log, load_file) criterion = torch.nn.BCELoss() if loss_fn == 'CE': weights = torch.Tensor([10, 90]) if torch.cuda.is_available(): weights = weights.cuda() criterion = torch.nn.CrossEntropyLoss(weight=weights) for epoch in range(epochs): #print("Starting Epoch #{}".format(epoch)) train_loader = DataLoader(dataset=dataset, batch_size=1, shuffle=True) epoch_loss = 0 for i, images in enumerate(train_loader): # get the inputs image, label = images['image'], images['label'] # zero the parameter gradients optimizer.zero_grad() ## Run the forward pass outputs = model.forward(image).cuda() if torch.cuda.is_available( ) else model.forward(image) if display: T.ToPILImage()(outputs[0].float()).show() if loss_fn == 'CE': label = label.squeeze(1).long() elif loss_fn == 'BCE': label = label.float() loss = criterion(outputs, label) loss.backward() epoch_loss = epoch_loss + loss.item() optimizer.step() #if i % 10 == 0 : # print("Epoch #{} Batch #{} Loss: {}".format(epoch,i,loss.item())) loss_log.append(epoch_loss) #print("Epoch",epoch," finished. Loss :",loss.item()) print(epoch, loss.item()) epoch_loss = 0 if save: save_checkpoint( { 'state_dict': model.state_dict(), 'optimizer': optimizer.state_dict(), 'loss_log': loss_log, }, save_file) print(loss_log) #T.ToPILImage()(outputs[0].float()).show() if display: testloader = DataLoader(dataset=dataset, batch_size=1, shuffle=True) dataiter = iter(testloader) testimg = dataiter.next() img, lbl = testimg['image'], testimg['label'] trained = model(img) thresholded = (trained > torch.tensor([0.5])) T.ToPILImage()(img[0]).show() T.ToPILImage()(lbl.float()).show() T.ToPILImage()((trained[0]).float()).show() T.ToPILImage()((thresholded[0]).float()).show() matching = (thresholded[0].long() == lbl.long()).sum() accuracy = float(matching) / lbl.numel() print("matching {}, total {}, accuracy {}".format(matching, lbl.numel(),\ accuracy))