def __init__(self): self.anchor_config = anchors(cfg.model) self.priorbox = PriorBox(self.anchor_config) self.net = build_net('test', cfg.model.input_size, cfg.model) init_net(self.net, cfg, args.trained_model) self.net.eval() self.num_classes = cfg.model.num_classes with torch.no_grad(): self.priors = self.priorbox.forward() # self.net = self.net.cuda() # self.priors = self.priors.cuda() cudnn.benchmark = True self._preprocess = BaseTransform(cfg.model.input_size, cfg.model.rgb_means, (2, 0, 1)) self.detector = Detect(num_classes, cfg.loss.bkg_label, self.anchor_config)
parser.add_argument('-t', '--thresh', default=0.25, type=float, help='visidutation threshold') parser.add_argument('--show', action='store_true', help='Whether to display the images') args = parser.parse_args() print_info(' ----------------------------------------------------------------------\n' '| Pelee Demo Program |\n' ' ----------------------------------------------------------------------', ['yellow', 'bold']) global cfg cfg = Config.fromfile(args.config) anchor_config = anchors(cfg.model) print_info('The Anchor info: \n{}'.format(anchor_config)) priorbox = PriorBox(anchor_config) net = build_net('test', cfg.model.input_size, cfg.model) init_net(net, cfg, args.trained_model) print_info('===> Finished constructing and loading model', ['yellow', 'bold']) net.eval() num_classes = cfg.model.num_classes with torch.no_grad(): priors = priorbox.forward() if cfg.test_cfg.cuda: net = net.cuda() priors = priors.cuda() cudnn.benchmark = True else: net = net.cpu() _preprocess = BaseTransform(
'--tensorboard', type=bool, default=False, help='Use tensorborad to show the Loss Graph') args = parser.parse_args() print_info( '----------------------------------------------------------------------\n' '| Pelee Training Program |\n' '----------------------------------------------------------------------', ['yellow', 'bold']) logger = set_logger(args.tensorboard) global cfg cfg = Config.fromfile(args.config) net = build_net('train', cfg.model.input_size, cfg.model) init_net(net, cfg, args.resume_net) # init the network with pretrained if args.ngpu > 1: net = torch.nn.DataParallel(net) if cfg.train_cfg.cuda: net.cuda() cudnn.benckmark = True optimizer = set_optimizer(net, cfg) criterion = set_criterion(cfg) priorbox = PriorBox(anchors(cfg.model)) with torch.no_grad(): priors = priorbox.forward() if cfg.train_cfg.cuda: priors = priors.cuda()
def Model_Params(self, model_dir="output", use_gpu=True): ''' User Function - Set Model Params Args: model_dir (str): Select the right model name as per training model_path (str): Relative path to params file use_gpu (bool): If True use GPU else run on CPU Returns: None ''' f = open(model_dir +"/config_final.py", 'r'); lines = f.read(); f.close(); if(not use_gpu): lines = lines.replace("cuda=True", "cuda=False"); f = open(model_dir +"/config_test.py", 'w'); f.write(lines); f.close(); print("Loading model for inference"); self.system_dict["cfg"] = Config.fromfile(model_dir +"/config_test.py") anchor_config = anchors(self.system_dict["cfg"].model) self.system_dict["priorbox"] = PriorBox(anchor_config) self.system_dict["net"] = build_net('test', self.system_dict["cfg"].model.input_size, self.system_dict["cfg"].model) init_net(self.system_dict["net"], self.system_dict["cfg"], model_dir + "/VOC/Final_Pelee_VOC_size304.pth") print_info('===> Finished constructing and loading model', ['yellow', 'bold']) self.system_dict["net"].eval() with torch.no_grad(): self.system_dict["priors"] = self.system_dict["priorbox"].forward() if self.system_dict["cfg"].test_cfg.cuda: self.system_dict["net"] = self.system_dict["net"].cuda() self.system_dict["priors"] = self.system_dict["priors"].cuda() cudnn.benchmark = True else: self.system_dict["net"] = self.system_dict["net"].cpu() self.system_dict["_preprocess"] = BaseTransform(self.system_dict["cfg"].model.input_size, self.system_dict["cfg"].model.rgb_means, (2, 0, 1)) self.system_dict["num_classes"] = self.system_dict["cfg"].model.num_classes self.system_dict["detector"] = Detect(self.system_dict["num_classes"], self.system_dict["cfg"].loss.bkg_label, anchor_config) print("Done...."); print("Loading other params"); base = int(np.ceil(pow(self.system_dict["num_classes"], 1. / 3))) self.system_dict["colors"] = [self._to_color(x, base) for x in range(self.system_dict["num_classes"])] cats = ['__background__']; f = open(self.system_dict["class_list"]); lines = f.readlines(); f.close(); for i in range(len(lines)): if(lines != ""): cats.append(lines[i][:len(lines[i])-1]) self.system_dict["labels"] = cats; print("Done....");
def setup(self): f = open( "Monk_Object_Detection/15_pytorch_peleenet/lib/configs/Pelee_VOC.py" ) lines = f.read() f.close() lines = lines.replace( "save_epochs=10", "save_epochs=" + str(self.system_dict["params"]["num_epochs"])) lines = lines.replace( "print_epochs=10", "print_epochs=" + str(self.system_dict["params"]["num_epochs"])) lines = lines.replace( "weights_save='weights/'", "weights_save='" + self.system_dict["params"]["model_output_dir"] + "/'") if (self.system_dict["params"]["use_gpu"]): lines = lines.replace("cuda=True", "cuda=True") else: lines = lines.replace("cuda=True", "cuda=False") lines = lines.replace( "per_batch_size=64", "per_batch_size=" + str(self.system_dict["params"]["batch_size"])) lines = lines.replace( "num_workers=8", "num_workers=" + str(self.system_dict["params"]["num_workers"])) f = open("config.py", 'w') f.write(lines) f.close() self.system_dict["local"]["cfg"] = Config.fromfile("config.py") print_info('===> Loading Dataset...', ['yellow', 'bold']) self.system_dict["local"]["dataset"] = get_dataloader( self.system_dict["local"]["cfg"], train_img_dir=self.system_dict["params"]["train_img_dir"], train_anno_dir=self.system_dict["params"]["train_anno_dir"], class_file=self.system_dict["params"]["class_file"]) print_info('===> Done...', ['yellow', 'bold']) print_info('===> Setting up epoch details...', ['yellow', 'bold']) self.system_dict["local"]["epoch_size"] = len( self.system_dict["local"]["dataset"]) // ( self.system_dict["local"]["cfg"].train_cfg.per_batch_size * self.system_dict["params"]["ngpu"]) self.system_dict["local"]["max_iter"] = self.system_dict["local"][ "epoch_size"] * self.system_dict["params"]["num_epochs"] self.system_dict["local"]["stepvalues"] = [ self.system_dict["local"]["max_iter"] // 3, 2 * self.system_dict["local"]["max_iter"] // 3 ] f = open("config.py") lines = f.read() f.close() lines = lines.replace( "step_lr=[80000, 100000, 120000,160000]", "step_lr=" + str(self.system_dict["local"]["stepvalues"])) lines = lines.replace( "num_classes=21", "num_classes=" + str(len(self.system_dict["local"]["dataset"].class_to_ind))) lines = lines.replace("lr=5e-3", "lr=" + str(self.system_dict["params"]["lr"])) lines = lines.replace( "gamma=0.1", "gamma=" + str(self.system_dict["params"]["gamma"])) lines = lines.replace( "momentum=0.9", "momentum=" + str(self.system_dict["params"]["momentum"])) lines = lines.replace( "weight_decay=0.0005", "weight_decay=" + str(self.system_dict["params"]["weight_decay"])) f = open("config_final.py", 'w') f.write(lines) f.close() print_info('===> Done...', ['yellow', 'bold']) self.system_dict["local"]["cfg"] = Config.fromfile("config_final.py") #print(self.system_dict["local"]["cfg"]) self.system_dict["local"]["net"] = build_net( 'train', self.system_dict["local"]["cfg"].model.input_size, self.system_dict["local"]["cfg"].model) if (self.system_dict["params"]["resume_net"]): init_net(self.system_dict["local"]["net"], self.system_dict["local"]["cfg"], self.system_dict["params"] ["resume_net"]) # init the network with pretrained if self.system_dict["params"]["ngpu"] > 1: self.system_dict["local"]["net"] = torch.nn.DataParallel( self.system_dict["local"]["net"]) if self.system_dict["local"]["cfg"].train_cfg.cuda: self.system_dict["local"]["net"].cuda() cudnn.benckmark = True self.system_dict["local"]["optimizer"] = set_optimizer( self.system_dict["local"]["net"], self.system_dict["local"]["cfg"]) self.system_dict["local"]["criterion"] = set_criterion( self.system_dict["local"]["cfg"]) self.system_dict["local"]["priorbox"] = PriorBox( anchors(self.system_dict["local"]["cfg"].model)) with torch.no_grad(): self.system_dict["local"]["priors"] = self.system_dict["local"][ "priorbox"].forward() if self.system_dict["local"]["cfg"].train_cfg.cuda: self.system_dict["local"]["priors"] = self.system_dict[ "local"]["priors"].cuda()