def get_graphs(opts, device): adj2_ = torch.from_numpy(graph.cihp2pascal_nlp_adj).float() #adj2 = adj2_.unsqueeze(0).unsqueeze(0).expand(opts.gpus, 1, 7, 20).transpose(2, 3).cuda() adj2 = adj2_.unsqueeze(0).unsqueeze(0).expand(opts.gpus, 1, 7, 20).transpose(2, 3).to(device) adj2_test = adj2_.unsqueeze(0).unsqueeze(0).expand(1, 1, 7, 20).transpose(2, 3) adj1_ = Variable( torch.from_numpy(graph.preprocess_adj(graph.pascal_graph)).float()) #adj3 = adj1_.unsqueeze(0).unsqueeze(0).expand(opts.gpus, 1, 7, 7).cuda() adj3 = adj1_.unsqueeze(0).unsqueeze(0).expand(opts.gpus, 1, 7, 7).to(device) adj3_test = adj1_.unsqueeze(0).unsqueeze(0).expand(1, 1, 7, 7) # adj2 = torch.from_numpy(graph.cihp2pascal_adj).float() # adj2 = adj2.unsqueeze(0).unsqueeze(0).expand(opts.gpus, 1, 7, 20) cihp_adj = graph.preprocess_adj(graph.cihp_graph) adj3_ = Variable(torch.from_numpy(cihp_adj).float()) #adj1 = adj3_.unsqueeze(0).unsqueeze(0).expand(opts.gpus, 1, 20, 20).cuda() adj1 = adj3_.unsqueeze(0).unsqueeze(0).expand(opts.gpus, 1, 20, 20).to(device) adj1_test = adj3_.unsqueeze(0).unsqueeze(0).expand(1, 1, 20, 20) train_graph = [adj1, adj2, adj3] test_graph = [adj1_test, adj2_test, adj3_test] return train_graph, test_graph
def get_graphs(opts): '''source is pascal; target is cihp; middle is atr''' # target 1 cihp_adj = graph.preprocess_adj(graph.cihp_graph) adj1_ = Variable(torch.from_numpy(cihp_adj).float()) adj1 = adj1_.unsqueeze(0).unsqueeze(0).expand(opts.gpus, 1, 20, 20).cuda() adj1_test = adj1_.unsqueeze(0).unsqueeze(0).expand(1, 1, 20, 20) #source 2 adj2_ = Variable(torch.from_numpy(graph.preprocess_adj(graph.pascal_graph)).float()) adj2 = adj2_.unsqueeze(0).unsqueeze(0).expand(opts.gpus, 1, 7, 7).cuda() adj2_test = adj2_.unsqueeze(0).unsqueeze(0).expand(1, 1, 7, 7) # s to target 3 adj3_ = torch.from_numpy(graph.cihp2pascal_nlp_adj).float() adj3 = adj3_.unsqueeze(0).unsqueeze(0).expand(opts.gpus, 1, 7, 20).transpose(2,3).cuda() adj3_test = adj3_.unsqueeze(0).unsqueeze(0).expand(1, 1, 7, 20).transpose(2,3) # middle 4 atr_adj = graph.preprocess_adj(graph.atr_graph) adj4_ = Variable(torch.from_numpy(atr_adj).float()) adj4 = adj4_.unsqueeze(0).unsqueeze(0).expand(opts.gpus, 1, 18, 18).cuda() adj4_test = adj4_.unsqueeze(0).unsqueeze(0).expand(1, 1, 18, 18) # source to middle 5 adj5_ = torch.from_numpy(graph.pascal2atr_nlp_adj).float() adj5 = adj5_.unsqueeze(0).unsqueeze(0).expand(opts.gpus, 1, 7, 18).cuda() adj5_test = adj5_.unsqueeze(0).unsqueeze(0).expand(1, 1, 7, 18) # target to middle 6 adj6_ = torch.from_numpy(graph.cihp2atr_nlp_adj).float() adj6 = adj6_.unsqueeze(0).unsqueeze(0).expand(opts.gpus, 1, 20, 18).cuda() adj6_test = adj6_.unsqueeze(0).unsqueeze(0).expand(1, 1, 20, 18) train_graph = [adj1, adj2, adj3, adj4, adj5, adj6] test_graph = [adj1_test, adj2_test, adj3_test, adj4_test, adj5_test, adj6_test] return train_graph, test_graph
def inference(net, img=None, use_gpu=True): """ :param net: :return: """ # adj adj2_ = torch.from_numpy(graph.cihp2pascal_nlp_adj).float() adj2_test = ( adj2_.unsqueeze(0).unsqueeze(0).expand(1, 1, 7, 20).cuda().transpose(2, 3) ) adj1_ = Variable(torch.from_numpy(graph.preprocess_adj(graph.pascal_graph)).float()) adj3_test = adj1_.unsqueeze(0).unsqueeze(0).expand(1, 1, 7, 7).cuda() cihp_adj = graph.preprocess_adj(graph.cihp_graph) adj3_ = Variable(torch.from_numpy(cihp_adj).float()) adj1_test = adj3_.unsqueeze(0).unsqueeze(0).expand(1, 1, 20, 20).cuda() # multi-scale scale_list = [1, 0.5, 0.75, 1.25, 1.5, 1.75] # scale_list = [1, 0.5, 0.75, 1.25] # NOTE: this part of the code assumes img is PIL image in RGB color space # We provide torch tensor in range [-1, 1] # Bring range to [0, 255] img = torch.clamp(img, -1, 1) img = (img + 1.0) / 2.0 img *= 255 testloader_list = [] testloader_flip_list = [] for pv in scale_list: composed_transforms_ts = transforms.Compose( [ tr.Scale_only_img(pv), tr.Normalize_xception_tf_only_img(), tr.ToTensor_only_img(), ] ) composed_transforms_ts_flip = transforms.Compose( [ tr.Scale_only_img(pv), tr.HorizontalFlip_only_img(), tr.Normalize_xception_tf_only_img(), tr.ToTensor_only_img(), ] ) # NOTE: img [1, 3, 256, 256], (min, max) = (0, 255) # print("original:", img.shape, img.min(), img.max()) testloader_list.append(img_transform(img, composed_transforms_ts)) # print(img_transform(img, composed_transforms_ts)) testloader_flip_list.append(img_transform(img, composed_transforms_ts_flip)) # print(testloader_list) start_time = timeit.default_timer() # One testing epoch # net.eval() # 1 0.5 0.75 1.25 1.5 1.75 ; flip: # NOTE: testloader_list[0]['image'].shape = 3, 420, 620 for iii, sample_batched in enumerate(zip(testloader_list, testloader_flip_list)): inputs, labels = sample_batched[0]["image"], sample_batched[0]["label"] inputs_f, _ = sample_batched[1]["image"], sample_batched[1]["label"] inputs = inputs.unsqueeze(0) inputs_f = inputs_f.unsqueeze(0) inputs = torch.cat((inputs, inputs_f), dim=0) if iii == 0: _, _, h, w = inputs.size() # assert inputs.size() == inputs_f.size() # Forward pass of the mini-batch # TODO: check requires grad functionality # inputs = Variable(inputs, requires_grad=False) with torch.no_grad(): if use_gpu >= 0: inputs = inputs.cuda() # outputs = net.forward(inputs) outputs = net.forward( inputs, adj1_test.cuda(), adj3_test.cuda(), adj2_test.cuda() ) outputs = (outputs[0] + flip(flip_cihp(outputs[1]), dim=-1)) / 2 outputs = outputs.unsqueeze(0) if iii > 0: outputs = F.upsample( outputs, size=(h, w), mode="bilinear", align_corners=True ) outputs_final = outputs_final + outputs else: outputs_final = outputs.clone() ################ plot pic predictions = torch.max(outputs_final, 1)[1] # results = predictions.cpu().numpy() # vis_res = decode_labels(results) # parsing_im = Image.fromarray(vis_res[0]) # return parsing_im hair_mask, face_mask = custom_decode_labels(predictions) return outputs_final, hair_mask, face_mask
def inference(net, img_path="", output_path="./", use_gpu=True): """ :param net: :param img_path: :param output_path: :return: """ # adj adj2_ = torch.from_numpy(graph.cihp2pascal_nlp_adj).float() adj2_test = (adj2_.unsqueeze(0).unsqueeze(0).expand(1, 1, 7, 20).cuda().transpose( 2, 3)) adj1_ = Variable( torch.from_numpy(graph.preprocess_adj(graph.pascal_graph)).float()) adj3_test = adj1_.unsqueeze(0).unsqueeze(0).expand(1, 1, 7, 7).cuda() cihp_adj = graph.preprocess_adj(graph.cihp_graph) adj3_ = Variable(torch.from_numpy(cihp_adj).float()) adj1_test = adj3_.unsqueeze(0).unsqueeze(0).expand(1, 1, 20, 20).cuda() # multi-scale scale_list = [1, 0.5, 0.75, 1.25, 1.5, 1.75] real_img = read_img(img_path) img = real_img.resize((256, 256)) testloader_list = [] testloader_flip_list = [] for pv in scale_list: composed_transforms_ts = transforms.Compose([ tr.Scale_only_img(pv), tr.Normalize_xception_tf_only_img(), tr.ToTensor_only_img(), ]) composed_transforms_ts_flip = transforms.Compose([ tr.Scale_only_img(pv), tr.HorizontalFlip_only_img(), tr.Normalize_xception_tf_only_img(), tr.ToTensor_only_img(), ]) testloader_list.append(img_transform(img, composed_transforms_ts)) # print(img_transform(img, composed_transforms_ts)) testloader_flip_list.append( img_transform(img, composed_transforms_ts_flip)) # print(testloader_list) start_time = timeit.default_timer() # One testing epoch net.eval() # 1 0.5 0.75 1.25 1.5 1.75 ; flip: for iii, sample_batched in enumerate( zip(testloader_list, testloader_flip_list)): inputs, labels = sample_batched[0]["image"], sample_batched[0]["label"] inputs_f, _ = sample_batched[1]["image"], sample_batched[1]["label"] inputs = inputs.unsqueeze(0) inputs_f = inputs_f.unsqueeze(0) inputs = torch.cat((inputs, inputs_f), dim=0) if iii == 0: _, _, h, w = inputs.size() # assert inputs.size() == inputs_f.size() # Forward pass of the mini-batch inputs = Variable(inputs, requires_grad=False) with torch.no_grad(): if use_gpu >= 0: inputs = inputs.cuda() # outputs = net.forward(inputs) outputs = net.forward(inputs, adj1_test.cuda(), adj3_test.cuda(), adj2_test.cuda()) outputs = (outputs[0] + flip(flip_cihp(outputs[1]), dim=-1)) / 2 outputs = outputs.unsqueeze(0) if iii > 0: outputs = F.upsample(outputs, size=(h, w), mode="bilinear", align_corners=True) outputs_final = outputs_final + outputs else: outputs_final = outputs.clone() #foreground_unroll = torch.resize(foreground_unroll, (H, W)) ################ plot pic predictions = torch.max(outputs_final, 1)[1] results = predictions.cpu().numpy() vis_res = decode_labels(results) parsing_im = Image.fromarray(vis_res[0]) parsing_im.save(os.path.join(output_path, 'real_mask.png')) #cv2.imwrite("outputs/{}_gray.png".format(output_name), results[0, :, :]) end_time = timeit.default_timer() print("time used for the multi-scale image inference" + " is :" + str(end_time - start_time)) # guided filter code goes here guidance_map = real_img.resize((512, 512)) guidance_map = transforms.ToTensor()(guidance_map) guidance_map = guidance_map.unsqueeze(0).cuda() guide = GuidedFilter( #guidance_map.shape[-2:], r=16, eps=1e-3, downsample_stride=4, ).cuda() guide.conv_mean_weights = guide.conv_mean_weights.cuda() predictions = F.interpolate(outputs_final, size=guidance_map.shape[-2:], mode='bilinear', align_corners=False) # get guided predictions; # guidance_map: [1, 3, 512, 512], predictions: [1, 20, 512, 512] for idx, channel in enumerate(predictions[0, ...]): current_channel = predictions[:, idx, :, :] current_channel = torch.unsqueeze(current_channel, axis=0) # [1, 1, 512, 512] if idx == 0: guided_predictions = guide(guidance_map, current_channel) # [1, 1, 512, 512] else: guided_predictions = torch.cat( (guided_predictions, guide(guidance_map, current_channel)), axis=1) # guided_predictions: [1, 20, 512, 512] guided_predictions = F.softmax(guided_predictions, dim=1) foreground_predictions = guided_predictions[ 0, 1:, ...] # [19, 512, 512], 0 is background foreground_soft_mask = torch.sum(foreground_predictions, axis=0) # [H, W] foreground_soft_mask = torch.unsqueeze(foreground_soft_mask, axis=-1) # [H, W, 1] foreground_soft_mask = torch.cat( (foreground_soft_mask, foreground_soft_mask, foreground_soft_mask), axis=-1) foreground_soft_mask = foreground_soft_mask.cpu().numpy() foreground_soft_mask = (foreground_soft_mask * 255).astype(np.uint8) cv2.imwrite(os.path.join(output_path, 'guided_mask.png'), foreground_soft_mask) hair_predictions = guided_predictions[0, 2, :, :] face_predictions = guided_predictions[0, 13, :, :] foreground_softmask = hair_predictions + face_predictions # [512, 512] foreground_softmask = torch.unsqueeze(foreground_softmask, axis=-1) foreground_softmask = torch.cat( (foreground_softmask, foreground_softmask, foreground_softmask), axis=-1) foreground_softmask = foreground_softmask.cpu().numpy() with open(os.path.join(output_path, 'hair_face_softmask.pkl'), 'wb') as handle: pickle.dump(foreground_softmask, handle) foreground_softmask = (foreground_softmask * 255).astype(np.uint8) cv2.imwrite(os.path.join(output_path, 'guided_hair_face_mask.png'), foreground_softmask)
def inference(net, img_path='', output_path='./', output_name='f', use_gpu=True): ''' :param net: :param img_path: :param output_path: :return: ''' # adj adj2_ = torch.from_numpy(graph.cihp2pascal_nlp_adj).float() adj2_test = adj2_.unsqueeze(0).unsqueeze(0).expand(1, 1, 7, 20).cuda().transpose( 2, 3) adj1_ = Variable( torch.from_numpy(graph.preprocess_adj(graph.pascal_graph)).float()) adj3_test = adj1_.unsqueeze(0).unsqueeze(0).expand(1, 1, 7, 7).cuda() cihp_adj = graph.preprocess_adj(graph.cihp_graph) adj3_ = Variable(torch.from_numpy(cihp_adj).float()) adj1_test = adj3_.unsqueeze(0).unsqueeze(0).expand(1, 1, 20, 20).cuda() # multi-scale scale_list = [1, 0.5, 0.75, 1.25, 1.5, 1.75] img = read_img(img_path) testloader_list = [] testloader_flip_list = [] for pv in scale_list: composed_transforms_ts = transforms.Compose([ tr.Scale_only_img(pv), tr.Normalize_xception_tf_only_img(), tr.ToTensor_only_img() ]) composed_transforms_ts_flip = transforms.Compose([ tr.Scale_only_img(pv), tr.HorizontalFlip_only_img(), tr.Normalize_xception_tf_only_img(), tr.ToTensor_only_img() ]) testloader_list.append(img_transform(img, composed_transforms_ts)) # print(img_transform(img, composed_transforms_ts)) testloader_flip_list.append( img_transform(img, composed_transforms_ts_flip)) # print(testloader_list) start_time = timeit.default_timer() # One testing epoch net.eval() # 1 0.5 0.75 1.25 1.5 1.75 ; flip: for iii, sample_batched in enumerate( zip(testloader_list, testloader_flip_list)): inputs, labels = sample_batched[0]['image'], sample_batched[0]['label'] inputs_f, _ = sample_batched[1]['image'], sample_batched[1]['label'] inputs = inputs.unsqueeze(0) inputs_f = inputs_f.unsqueeze(0) inputs = torch.cat((inputs, inputs_f), dim=0) if iii == 0: _, _, h, w = inputs.size() # assert inputs.size() == inputs_f.size() # Forward pass of the mini-batch inputs = Variable(inputs, requires_grad=False) with torch.no_grad(): if use_gpu >= 0: inputs = inputs.cuda() # outputs = net.forward(inputs) outputs = net.forward(inputs, adj1_test.cuda(), adj3_test.cuda(), adj2_test.cuda()) outputs = (outputs[0] + flip(flip_cihp(outputs[1]), dim=-1)) / 2 outputs = outputs.unsqueeze(0) if iii > 0: outputs = F.upsample(outputs, size=(h, w), mode='bilinear', align_corners=True) outputs_final = outputs_final + outputs else: outputs_final = outputs.clone() ################ plot pic predictions = torch.max(outputs_final, 1)[1] results = predictions.cpu().numpy() vis_res = decode_labels(results) parsing_im = Image.fromarray(vis_res[0]) parsing_im.save(output_path + '/{}.png'.format(output_name)) cv2.imwrite(output_path + '/{}_gray.png'.format(output_name), results[0, :, :]) end_time = timeit.default_timer() print('time used for the multi-scale image inference' + ' is :' + str(end_time - start_time))
def get_graph(self): spine_adj = graph.preprocess_adj(graph.spine_graph) spine_adj_ = torch.from_numpy(spine_adj).float() spine_adj = spine_adj_.unsqueeze(0).to(self.device) return spine_adj
def main(opts): adj2_ = torch.from_numpy(graph.cihp2pascal_nlp_adj).float() adj2_test = (adj2_.unsqueeze(0).unsqueeze(0).expand(1, 1, 7, 20).cuda().transpose( 2, 3)) adj1_ = Variable( torch.from_numpy(graph.preprocess_adj(graph.pascal_graph)).float()) adj3_test = adj1_.unsqueeze(0).unsqueeze(0).expand(1, 1, 7, 7).cuda() cihp_adj = graph.preprocess_adj(graph.cihp_graph) adj3_ = Variable(torch.from_numpy(cihp_adj).float()) adj1_test = adj3_.unsqueeze(0).unsqueeze(0).expand(1, 1, 20, 20).cuda() p = OrderedDict() # Parameters to include in report p["trainBatch"] = opts.batch # Training batch size p["nAveGrad"] = 1 # Average the gradient of several iterations p["lr"] = opts.lr # Learning rate p["lrFtr"] = 1e-5 p["lraspp"] = 1e-5 p["lrpro"] = 1e-5 p["lrdecoder"] = 1e-5 p["lrother"] = 1e-5 p["wd"] = 5e-4 # Weight decay p["momentum"] = 0.9 # Momentum p["epoch_size"] = 10 # How many epochs to change learning rate p["num_workers"] = opts.numworker backbone = "xception" # Use xception or resnet as feature extractor, with open(opts.txt_file, "r") as f: img_list = f.readlines() max_id = 0 save_dir_root = os.path.join(os.path.dirname(os.path.abspath(__file__))) exp_name = os.path.dirname(os.path.abspath(__file__)).split("/")[-1] runs = glob.glob(os.path.join(save_dir_root, "run", "run_*")) for r in runs: run_id = int(r.split("_")[-1]) if run_id >= max_id: max_id = run_id + 1 # run_id = int(runs[-1].split('_')[-1]) + 1 if runs else 0 # Network definition if backbone == "xception": net = deeplab_xception_transfer.deeplab_xception_transfer_projection_savemem( n_classes=opts.classes, os=16, hidden_layers=opts.hidden_layers, source_classes=7, ) elif backbone == "resnet": # net = deeplab_resnet.DeepLabv3_plus(nInputChannels=3, n_classes=7, os=16, pretrained=True) raise NotImplementedError else: raise NotImplementedError if gpu_id >= 0: net.cuda() # net load weights if not opts.loadmodel == "": x = torch.load(opts.loadmodel) net.load_source_model(x) print("load model:", opts.loadmodel) else: print("no model load !!!!!!!!") ## multi scale scale_list = [1, 0.5, 0.75, 1.25, 1.5, 1.75] testloader_list = [] testloader_flip_list = [] for pv in scale_list: composed_transforms_ts = transforms.Compose( [tr.Scale_(pv), tr.Normalize_xception_tf(), tr.ToTensor_()]) composed_transforms_ts_flip = transforms.Compose([ tr.Scale_(pv), tr.HorizontalFlip(), tr.Normalize_xception_tf(), tr.ToTensor_(), ]) voc_val = cihp.VOCSegmentation(split="test", transform=composed_transforms_ts) voc_val_f = cihp.VOCSegmentation(split="test", transform=composed_transforms_ts_flip) testloader = DataLoader(voc_val, batch_size=1, shuffle=False, num_workers=p["num_workers"]) testloader_flip = DataLoader(voc_val_f, batch_size=1, shuffle=False, num_workers=p["num_workers"]) testloader_list.append(copy.deepcopy(testloader)) testloader_flip_list.append(copy.deepcopy(testloader_flip)) print("Eval Network") if not os.path.exists(opts.output_path + "cihp_output_vis/"): os.makedirs(opts.output_path + "cihp_output_vis/") if not os.path.exists(opts.output_path + "cihp_output/"): os.makedirs(opts.output_path + "cihp_output/") start_time = timeit.default_timer() # One testing epoch total_iou = 0.0 net.eval() for ii, large_sample_batched in enumerate( zip(*testloader_list, *testloader_flip_list)): print(ii) # 1 0.5 0.75 1.25 1.5 1.75 ; flip: sample1 = large_sample_batched[:6] sample2 = large_sample_batched[6:] for iii, sample_batched in enumerate(zip(sample1, sample2)): inputs, labels = sample_batched[0]["image"], sample_batched[0][ "label"] inputs_f, _ = sample_batched[1]["image"], sample_batched[1][ "label"] inputs = torch.cat((inputs, inputs_f), dim=0) if iii == 0: _, _, h, w = inputs.size() # assert inputs.size() == inputs_f.size() # Forward pass of the mini-batch inputs, labels = Variable(inputs, requires_grad=False), Variable(labels) with torch.no_grad(): if gpu_id >= 0: inputs, labels = inputs.cuda(), labels.cuda() # outputs = net.forward(inputs) # pdb.set_trace() outputs = net.forward(inputs, adj1_test.cuda(), adj3_test.cuda(), adj2_test.cuda()) outputs = (outputs[0] + flip(flip_cihp(outputs[1]), dim=-1)) / 2 outputs = outputs.unsqueeze(0) if iii > 0: outputs = F.upsample(outputs, size=(h, w), mode="bilinear", align_corners=True) outputs_final = outputs_final + outputs else: outputs_final = outputs.clone() ################ plot pic predictions = torch.max(outputs_final, 1)[1] prob_predictions = torch.max(outputs_final, 1)[0] results = predictions.cpu().numpy() prob_results = prob_predictions.cpu().numpy() vis_res = decode_labels(results) parsing_im = Image.fromarray(vis_res[0]) parsing_im.save(opts.output_path + "cihp_output_vis/{}.png".format(img_list[ii][:-1])) cv2.imwrite( opts.output_path + "cihp_output/{}.png".format(img_list[ii][:-1]), results[0, :, :], ) # np.save('../../cihp_prob_output/{}.npy'.format(img_list[ii][:-1]), prob_results[0, :, :]) # pred_list.append(predictions.cpu()) # label_list.append(labels.squeeze(1).cpu()) # loss = criterion(outputs, labels, batch_average=True) # running_loss_ts += loss.item() # total_iou += utils.get_iou(predictions, labels) end_time = timeit.default_timer() print("time use for " + str(ii) + " is :" + str(end_time - start_time)) # Eval pred_path = opts.output_path + "cihp_output/" eval_( pred_path=pred_path, gt_path=opts.gt_path, classes=opts.classes, txt_file=opts.txt_file, )
def inference(net, imgFile, opts): print('Processing file {}'.format(imgFile)) im = Image.open(os.path.join(opts.input_path, imgFile)) imgNp = np.array(im) adj2_ = torch.from_numpy(graph.cihp2pascal_nlp_adj).float() adj2_test = adj2_.unsqueeze(0).unsqueeze(0).expand(1, 1, 7, 20).cuda() adj1_ = Variable( torch.from_numpy(graph.preprocess_adj(graph.pascal_graph)).float()) adj1_test = adj1_.unsqueeze(0).unsqueeze(0).expand(1, 1, 7, 7).cuda() cihp_adj = graph.preprocess_adj(graph.cihp_graph) adj3_ = Variable(torch.from_numpy(cihp_adj).float()) adj3_test = adj3_.unsqueeze(0).unsqueeze(0).expand(1, 1, 20, 20).cuda() net.eval() img = convertImg(imgNp) ## multi scale scale_list = [1, 0.5, 0.75, 1.25, 1.5, 1.75] testloader_list = [] testloader_flip_list = [] for pv in scale_list: composed_transforms_ts = transforms.Compose([ tr.Scale_only_img(pv), tr.Normalize_xception_tf_only_img(), tr.ToTensor_only_img() ]) composed_transforms_ts_flip = transforms.Compose([ tr.Scale_only_img(pv), tr.HorizontalFlip_only_img(), tr.Normalize_xception_tf_only_img(), tr.ToTensor_only_img() ]) testloader_list.append(img_transform(img, composed_transforms_ts)) testloader_flip_list.append( img_transform(img, composed_transforms_ts_flip)) for iii, sample_batched in enumerate( zip(testloader_list, testloader_flip_list)): inputs, labels = sample_batched[0]['image'], sample_batched[0]['label'] inputs_f, _ = sample_batched[1]['image'], sample_batched[1]['label'] inputs = inputs.unsqueeze(0) inputs_f = inputs_f.unsqueeze(0) inputs = torch.cat((inputs, inputs_f), dim=0) if iii == 0: _, _, h, w = inputs.size() # assert inputs.size() == inputs_f.size() # Forward pass of the mini-batch inputs = Variable(inputs, requires_grad=False) with torch.no_grad(): inputs = inputs.cuda() # outputs = net.forward(inputs) outputs = net.forward(inputs, adj1_test.cuda(), adj3_test.cuda(), adj2_test.cuda()) outputs = (outputs[0] + flip(outputs[1], dim=-1)) / 2 outputs = outputs.unsqueeze(0) if iii > 0: outputs = F.upsample(outputs, size=(h, w), mode='bilinear', align_corners=True) outputs_final = outputs_final + outputs else: outputs_final = outputs.clone() predictions = torch.max(outputs_final, 1)[1] prob_predictions = torch.max(outputs_final, 1)[0] results = predictions.cpu().numpy() prob_results = prob_predictions.cpu().numpy() vis_res = decode_labels(results) if not os.path.isdir(opts.output_path): os.makedirs(opts.output_path) parsing_im = Image.fromarray(vis_res[0]) parsing_im.save(opts.output_path + '/{}.vis.png'.format(imgFile.split('.')[0])) cv2.imwrite(opts.output_path + '/{}.png'.format(imgFile.split('.')[0]), results[0, :, :])