def test(filenameSave, model, dataloader_test, args): for step, (images, labels, filename, filenameGt) in enumerate(dataloader_test): if (args.cuda): images = images.cuda() # labels = labels.cuda() inputs = Variable(images) # targets = Variable(labels) with torch.no_grad(): outputs = model(inputs) label = outputs[0].max(0)[1].byte().cpu().data # label_cityscapes = cityscapes_trainIds2labelIds(label.unsqueeze(0)) label_color = Colorize()(label.unsqueeze(0)) # image_transform(label.byte()).save(filenameSave) # label_save = ToPILImage()(label_color) label_save = label_color.numpy() label_save = label_save.transpose(1, 2, 0) # label_save.save(filenameSave) images = images.cpu().numpy().squeeze(axis=0).transpose(1, 2, 0) images = (images*255).astype(np.uint8) for i in range(len(filename)): fileSave = '../eval/'+ args.savedir + "/" + filename[i].split("leftImg8bit/")[1] os.makedirs(os.path.dirname(fileSave), exist_ok=True) output = cv2.addWeighted(images, 0.4, label_save, 0.6, 0) cv2.imwrite(fileSave,output)
def inference(model, args): image_folder = "/media/pandongwei/ExtremeSSD/work_relative/extract_img/2020.10.16_1/" video_save_path = "/home/pandongwei/work_repository/erfnet_pytorch/eval/" # parameters about saving video fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter(video_save_path + 'output_new.avi', fourcc, 10.0, (640, 480)) cuda = True model.eval() paths = [] for root, dirs, files in os.walk(image_folder, topdown=True): for file in files: image_path = os.path.join(image_folder, file) paths.append(image_path) paths.sort() font = cv2.FONT_HERSHEY_SIMPLEX angle_pre = 0 for i, path in enumerate(paths): start_time = time.time() image = cv2.imread(path) image = (image / 255.).astype(np.float32) image = ToTensor()(image).unsqueeze(0) if (cuda): image = image.cuda() input = Variable(image) with torch.no_grad(): output = model(input) label = output[0].max(0)[1].byte().cpu().data # label_cityscapes = cityscapes_trainIds2labelIds(label.unsqueeze(0)) label_color = Colorize()(label.unsqueeze(0)) label_save = label_color.numpy() label_save = label_save.transpose(1, 2, 0) # 加上路径规划 label_save, angle = perception_to_angle(label_save, angle_pre) # 加一个滤波以防止角度突然跳变 TODO if abs(angle - angle_pre) > 10: angle = angle_pre angle_pre = angle # label_save.save(filenameSave) image = image.cpu().numpy().squeeze(axis=0).transpose(1, 2, 0) image = (image * 255).astype(np.uint8) output = cv2.addWeighted(image, 0.5, label_save, 0.5, 0) cv2.putText(output,str(round(angle,3)),(50,50),cv2.FONT_HERSHEY_SIMPLEX,2,(0,0,0),2) #output = np.hstack([label_save, image]) out.write(output) print(i, " time: %.2f s" % (time.time() - start_time)) out.release()
def inference(model, image, angle_pre): # pre-process image = (image / 255.).astype(np.float32) image = ToTensor()(image).unsqueeze(0) image = image.cuda() input = Variable(image) # inference with torch.no_grad(): output = model(input) # post-process label = output[0].max(0)[1].byte().cpu().data label_color = Colorize()(label.unsqueeze(0)) label_save = label_color.numpy() label_save = label_save.transpose(1, 2, 0) # 加上路径规划 label_save, angle = perception_to_angle(label_save, angle_pre) return angle
def ensemble_predict(prob_fns, outfile='sample.png', out_npy_file='sample.npy', out_vis_file='sample_vis.png', method='averaging', out_shape=(2048, 1024)): """Output predict file from two npy files by the given method.""" probs = [np.load(prob_fn) for prob_fn in prob_fns] if method == 'averaging': prob = sum(probs) / len(probs) elif method == 'nms': prob = np.max(probs, 0) # -- output npy -- np.save(out_npy_file, prob) # -- output label-predict -- pred = prob[:N_CLASS].argmax(0) img = Image.fromarray(np.uint8(pred)) img = img.resize(out_shape, Image.NEAREST) img.save(outfile) # -- output vis-predict -- # ToTensor function: ndarray -> Tensor # * H, W, C -> C, H, W # * 0, 255 -> 0, 1 # prob.shape == (20, 512, 1024) prob_for_tensor = np.transpose(prob[:N_CLASS], (1, 2, 0)) prob_tensor = ToTensor()(prob_for_tensor) pred = prob_tensor.max(0)[1] pred = pred.view(1, pred.size()[0], pred.size()[1]) # Colorize function: Tensor -> Tensor vis_tensor = Colorize()(pred) # Tensor -> ndarray -> image(save) vis = np.transpose(vis_tensor.numpy(), (1, 2, 0)) vis_img = Image.fromarray(vis, 'RGB') vis_img = vis_img.resize(out_shape, Image.NEAREST) vis_img.save(out_vis_file)
def main(args): modelpath = args.loadDir + args.loadModel weightspath = args.loadDir + args.loadWeights print("Loading model: " + modelpath) print("Loading weights: " + weightspath) model = Net(NUM_CLASSES) model = torch.nn.DataParallel(model) if (not args.cpu): model = model.cuda() #model.load_state_dict(torch.load(args.state)) #model.load_state_dict(torch.load(weightspath)) #not working if missing key def load_my_state_dict( model, state_dict ): #custom function to load model when not all dict elements own_state = model.state_dict() for name, param in state_dict.items(): if name not in own_state: continue own_state[name].copy_(param) return model model = load_my_state_dict(model, torch.load(weightspath)) print("Model and weights LOADED successfully") model.eval() if (not os.path.exists(args.datadir)): print("Error: datadir could not be loaded") loader = DataLoader(cityscapes(args.datadir, input_transform_cityscapes, target_transform_cityscapes, subset=args.subset), num_workers=args.num_workers, batch_size=args.batch_size, shuffle=False) # For visualizer: # must launch in other window "python3.6 -m visdom.server -port 8097" # and access localhost:8097 to see it if (args.visualize): vis = visdom.Visdom() for step, (images, labels, filename, filenameGt) in enumerate(loader): if (not args.cpu): images = images.cuda() #labels = labels.cuda() inputs = Variable(images) #targets = Variable(labels) with torch.no_grad(): outputs = model(inputs) label = outputs[0].max(0)[1].byte().cpu().data #label_cityscapes = cityscapes_trainIds2labelIds(label.unsqueeze(0)) label_color = Colorize()(label.unsqueeze(0)) filenameSave = "./save_color/" + filename[0].split("leftImg8bit/")[1] os.makedirs(os.path.dirname(filenameSave), exist_ok=True) #image_transform(label.byte()).save(filenameSave) label_save = ToPILImage()(label_color) label_save.save(filenameSave) if (args.visualize): vis.image(label_color.numpy()) print(step, filenameSave)
model.eval() # 10 13 48 86 101 img = Image.open("./data/VOC2012test/JPEGImages/2008_000101.jpg").convert("RGB") original_size = img.size img.save("original.png") img = img.resize((256, 256), Image.BILINEAR) img = ToTensor()(img) img = Variable(img).unsqueeze(0) outputs = model(img) # 22 256 256 for i, output in enumerate(outputs): output = output[0].data.max(0)[1] output = Colorize()(output) output = np.transpose(output.numpy(), (1, 2, 0)) img = Image.fromarray(output, "RGB") if i == 0: img = img.resize(original_size, Image.NEAREST) img.save("test-%d.png" % i) ''' for index, (imgs, name, size) in tqdm(enumerate(testloader)): imgs = Variable(imgs.cuda()) outputs = model(imgs) output = outputs[0][0].data.max(0)[1] output = Colorize()(output) print(output) output = np.transpose(output.numpy(), (1, 2, 0))
def main(args): modelpath = args.loadDir + args.loadModel weightspath = args.loadDir + args.loadWeights print("Loading model: " + modelpath) print("Loading weights: " + weightspath) #Import ERFNet model from the folder #Net = importlib.import_module(modelpath.replace("/", "."), "ERFNet") model = ERFNet(NUM_CLASSES) model = torch.nn.DataParallel(model) if (not args.cpu): model = model.cuda() #model.load_state_dict(torch.load(args.state)) #model.load_state_dict(torch.load(weightspath)) #not working if missing key fourcc = cv2.VideoWriter_fourcc(*'MP4V') # Save as video out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640, 352)) def load_my_state_dict( model, state_dict ): #custom function to load model when not all dict elements own_state = model.state_dict() for name, param in state_dict.items(): if name not in own_state: continue own_state[name].copy_(param) return model model = load_my_state_dict( model, torch.load(weightspath, map_location=torch.device('cpu'))) print("Model and weights LOADED successfully") model.eval() # loader = DataLoader(cityscapes(args.datadir, input_transform_cityscapes, target_transform_cityscapes, subset=args.subset), # num_workers=args.num_workers, batch_size=args.batch_size, shuffle=False) # For visualizer: # must launch in other window "python3.6 -m visdom.server -port 8097" # and access localhost:8097 to see it # if (args.visualize): # vis = visdom.Visdom() # for step, (images, labels, filename, filenameGt) in enumerate(loader): # cap = cv2.VideoCapture(0) cap = cv2.VideoCapture('project_video_trimmed.mp4') while (True): # Capture frame-by-frame ret, images = cap.read() # print(images.shape) images = trans(images) images = images.float() images = images.view((1, 3, 352, 640)) # vidoe # Our operations on the frame come here if (not args.cpu): images = images.cuda() inputs = Variable(images) with torch.no_grad(): outputs = model(inputs) label = outputs[0].max(0)[1].byte().cpu().data #label_cityscapes = cityscapes_trainIds2labelIds(label.unsqueeze(0)) label_color = Colorize()(label.unsqueeze(0)) frame = label_color.numpy().transpose(1, 2, 0) # label_save = ToPILImage()(label_color) # label_save.save("result_1.png") # Display the resulting frame cv2.imshow('Segmented Image', frame) out.write(frame) # To save video file if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() out.release() cv2.destroyAllWindows() if (args.visualize): vis.image(label_color.numpy()) print(step, filenameSave)
def main(args): modelpath = args.loadDir + args.loadModel weightspath = args.loadDir + args.loadWeights if (not os.path.exists(args.datadir)): print("Error: datadir could not be loaded") else: print("Loading model: " + modelpath) print("Loading weights: " + weightspath) # Import ERFNET model = ERFNet(NUM_CLASSES) model = torch.nn.DataParallel(model) if (not args.cpu): model = model.cuda() def load_my_state_dict( model, state_dict ): #custom function to load model when not all dict elements own_state = model.state_dict() for name, param in state_dict.items(): if name not in own_state: continue own_state[name].copy_(param) return model model = load_my_state_dict(model, torch.load(weightspath)) print("Model and weights LOADED successfully") # Set model to Evaluation mode model.eval() # Setup the dataset loader ### RELLIS-3D Dataloader enc = False loader_test = custom_datasets.setup_loaders(args, enc) # For visualizer: # must launch in other window "python3.6 -m visdom.server -port 8097" # and access localhost:8097 to see it if (args.visualize): vis = visdom.Visdom() for step, (images, labels, img_name, _) in enumerate(loader_test): start_time = time.time() if (not args.cpu): images = images.cuda() #labels = labels.cuda() inputs = Variable(images) with torch.no_grad(): outputs = model(inputs) label = outputs[0].max(0)[1].byte().cpu().data label_color = Colorize()(label.unsqueeze(0)) eval_save_path = "./save_colour_rellis/" if not os.path.exists(eval_save_path): os.makedirs(eval_save_path) _, file_name = os.path.split(img_name[0]) file_name = file_name + ".png" #image_transform(label.byte()).save(filenameSave) label_save = ToPILImage()(label_color) label_save.save(os.path.join(eval_save_path, file_name)) if (args.visualize): vis.image(label_color.numpy()) if step != 0: #first run always takes some time for setup fwt = time.time() - start_time time_train.append(fwt) print("Forward time per img (b=%d): %.3f (Mean: %.3f)" % (args.batch_size, fwt / args.batch_size, sum(time_train) / len(time_train) / args.batch_size)) print(step, os.path.join(eval_save_path, file_name))
imgs = Variable(imgs.cuda()) start_time = time.time() outputs_4, outputs_2, outputs = model(imgs) end_time = time.time() if j > 9: avr_time += (end_time - start_time) if j == 109: print avr_time/100.0 exit() # 22 256 256 filename = list(names)[0] for i, (output_4, output_2, output) in enumerate(zip(outputs_4, outputs_2, outputs)): output_4 = output_4.data.max(0)[1] output_4 = Colorize()(output_4) output_4 = np.transpose(output_4.numpy(), (1, 2, 0)) img_4 = Image.fromarray(output_4, "RGB") output_2 = output_2.data.max(0)[1] output_2 = Colorize()(output_2) output_2 = np.transpose(output_2.numpy(), (1, 2, 0)) img_2 = Image.fromarray(output_2, "RGB") output = output.data.max(0)[1] output = Colorize()(output) output = np.transpose(output.numpy(), (1, 2, 0)) img = Image.fromarray(output, "RGB") if i == 0: img_4 = img_4.resize((width, height), Image.NEAREST)
def main(args): modelpath = args.loadDir + args.loadModel #weightspath = args.loadDir + args.loadWeights #TODO weightspath = "../save/feriburgForest_3/model_best.pth" print("Loading model: " + modelpath) print("Loading weights: " + weightspath) #Import ERFNet model from the folder #Net = importlib.import_module(modelpath.replace("/", "."), "ERFNet") model = ERFNet(NUM_CLASSES) model = torch.nn.DataParallel(model) if (not args.cpu): model = model.cuda() #model.load_state_dict(torch.load(args.state)) #model.load_state_dict(torch.load(weightspath)) #not working if missing key def load_my_state_dict( model, state_dict ): #custom function to load model when not all dict elements own_state = model.state_dict() for name, param in state_dict.items(): if name not in own_state: continue own_state[name].copy_(param) return model model = load_my_state_dict(model, torch.load(weightspath)) print("Model and weights LOADED successfully") model.eval() if (not os.path.exists(args.datadir)): print("Error: datadir could not be loaded") loader = DataLoader(freiburgForest(args.datadir, input_transform_cityscapes, target_transform_cityscapes, subset=args.subset), num_workers=args.num_workers, batch_size=args.batch_size, shuffle=False) # For visualizer: # must launch in other window "python3.6 -m visdom.server -port 8097" # and access localhost:8097 to see it if (args.visualize): vis = visdom.Visdom() for step, (images, labels, filename, filenameGt) in enumerate(loader): if (not args.cpu): images = images.cuda() #labels = labels.cuda() inputs = Variable(images) #targets = Variable(labels) with torch.no_grad(): outputs = model(inputs) print(outputs.shape) label = outputs[0].max(0)[1].byte().cpu().data #label_cityscapes = cityscapes_trainIds2labelIds(label.unsqueeze(0)) label_color = Colorize()(label.unsqueeze(0)) filenameSave = "./freiburgforest_1/" + filename[0].split( "freiburg_forest_annotated/")[1] os.makedirs(os.path.dirname(filenameSave), exist_ok=True) #image_transform(label.byte()).save(filenameSave) print(label_color.shape) # label_save = ToPILImage()(label_color) label_save = label_color.numpy() label_save = label_save.transpose(1, 2, 0) print(label_save.shape) # label_save.save(filenameSave) images = images.cpu().numpy().squeeze(axis=0) images = images.transpose(1, 2, 0) # print(images.shape) # print(label_save.shape) plt.figure(figsize=(10.24, 5.12), dpi=100) plt.imshow(images) plt.imshow(label_save, alpha=0.6) plt.axis('off') # plt.show() plt.savefig(filenameSave, dpi=100) plt.close() if (args.visualize): vis.image(label_color.numpy()) print(step, filenameSave)
def main(args): modelpath = args.loadDir + args.loadModel weightspath = args.loadDir + args.loadWeights print("Loading model: " + modelpath) print("Loading weights: " + weightspath) model = Net(NUM_CLASSES) model = torch.nn.DataParallel(model) if (not args.cpu): model = model.cuda() # model.load_state_dict(torch.load(args.state)) # model.load_state_dict(torch.load(weightspath)) #not working if missing key def load_my_state_dict( model, state_dict ): # custom function to load model when not all dict elements own_state = model.state_dict() for name, param in state_dict.items(): if name not in own_state: continue own_state[name].copy_(param) return model model = load_my_state_dict(model, torch.load(weightspath)) print("Model and weights LOADED successfully") model.eval() if (not os.path.exists(args.datadir)): print("Error: datadir could not be loaded") # loader = DataLoader( # cityscapes('/home/liqi/PycharmProjects/LEDNet/4.png', input_transform_cityscapes, target_transform_cityscapes, subset=args.subset), # num_workers=args.num_workers, batch_size=1 ,shuffle=False) input_transform_cityscapes = Compose([ Resize((512, 1024), Image.BILINEAR), ToTensor(), # Normalize([.485, .456, .406], [.229, .224, .225]), ]) name = "4.png" with open(image_path_city('/home/gongyiqun/images', name), 'rb') as f: images = load_image(f).convert('RGB') images = input_transform_cityscapes(images) # For visualizer: # must launch in other window "python3.6 -m visdom.server -port 8097" # and access localhost:8097 to see it if (args.visualize): vis = visdom.Visdom() if (not args.cpu): images = images.cuda() # labels = labels.cuda() a = torch.unsqueeze(images, 0) inputs = Variable(a) # targets = Variable(labels) with torch.no_grad(): outputs = model(inputs) label = outputs[0].max(0)[1].byte().cpu().data # label_cityscapes = cityscapes_trainIds2labelIds(label.unsqueeze(0)) label_color = Colorize()(label.unsqueeze(0)) filenameSave = "./save_color/" + "Others/" + name os.makedirs(os.path.dirname(filenameSave), exist_ok=True) # image_transform(label.byte()).save(filenameSave) label_save = ToPILImage()(label_color) label_save = label_save.resize((1241, 376), Image.BILINEAR) # label_save = cv2.resize(label_save, (376, 1224),interpolation=cv2.INTER_AREA) label_save.save(filenameSave) if (args.visualize): vis.image(label_color.numpy())