import data import loss import torch import model from trainer import Trainer from option import args import utils.utility as utility ckpt = utility.checkpoint(args) loader = data.Data(args) model = model.Model(args, ckpt) loss = loss.Loss(args, ckpt) if not args.test_only else None trainer = Trainer(args, model, loss, loader, ckpt) n = 200 while not trainer.terminate(): n += 1 trainer.train() if args.test_every != 0 and n % args.test_every == 0: trainer.test()
import data import loss import torch import model from trainer import Trainer from option import args import utils.utility as utility ckpt = utility.checkpoint(args) #一个class,load,save,写日志 loader = data.Data(args) model = model.Model(args, ckpt) loss = loss.Loss(args, ckpt) if not args.test_only else None trainer = Trainer(args, model, loss, loader, ckpt) n = 0 while not trainer.terminate(): n += 1 trainer.train() # if args.test_every!=0 and n%args.test_every==0: # trainer.test() if n % 10 == 0: #每10个epoach保存下模型 model.save("result/", n)
def main(): # parser = argparse.ArgumentParser() # parser.add_argument('--root', type=str) # parser.add_argument('-d', '--dataset', type=str, default='market1501') # parser.add_argument('-m', '--model', type=str, default='osnet_x1_0') # parser.add_argument('--weights', type=str) # parser.add_argument('--save-dir', type=str, default='log') # parser.add_argument('--height', type=int, default=256) # parser.add_argument('--width', type=int, default=128) # args = parser.parse_args() if args.config != '': with open(args.config, 'r') as f: config = yaml.load(f) for op in config: setattr(args, op, config[op]) # loader = data.Data(args) ckpt = utility.checkpoint(args) loader = data_v2.ImageDataManager(args) model = Model(args, ckpt) optimzer = optim.make_optimizer(args, model) # loss = loss.make_loss(args, ckpt) if not args.test_only else None start = -1 if args.load != '': start = ckpt.resume_from_checkpoint( osp.join(ckpt.dir, 'model-latest.pth'), model, optimzer) - 1 if args.pre_train != '': ckpt.load_pretrained_weights(model, args.pre_train) scheduler = optim.make_scheduler(args, optimzer, start) # print('[INFO] System infomation: \n {}'.format(get_pretty_env_info())) ckpt.write_log('[INFO] Model parameters: {com[0]} flops: {com[1]}'.format( com=compute_model_complexity(model, (1, 3, args.height, args.width)))) use_gpu = torch.cuda.is_available() # datamanager = torchreid.data.ImageDataManager( # root=args.root, # sources=args.dataset, # height=args.height, # width=args.width, # batch_size_train=100, # batch_size_test=100, # transforms=None, # train_sampler='SequentialSampler' # ) # test_loader = loader.testloader test_loader = loader.test_loader query_loader = loader.query_loader query_dataset = loader.queryset.query gallery_dataset = loader.galleryset.gallery # model = torchreid.models.build_model( # name=args.model, # num_classes=datamanager.num_train_pids, # use_gpu=use_gpu # ) if use_gpu: model = model.cuda() device = torch.device('cuda' if use_gpu else 'cpu') model.eval() # self.ckpt.add_log(torch.zeros(1, 6)) # qf = self.extract_feature(self.query_loader,self.args).numpy() # gf = self.extract_feature(self.test_loader,self.args).numpy() with torch.no_grad(): qf, query_ids, query_cams = extract_feature( model, device, query_loader, args) gf, gallery_ids, gallery_cams = extract_feature( model, device, test_loader, args) if args.re_rank: q_g_dist = np.dot(qf, np.transpose(gf)) q_q_dist = np.dot(qf, np.transpose(qf)) g_g_dist = np.dot(gf, np.transpose(gf)) dist = re_ranking(q_g_dist, q_q_dist, g_g_dist) else: # dist = cdist(qf, gf,metric='cosine') # cosine distance dist =1- torch.mm(qf, gf.t()).cpu().numpy() # if args.weights and check_isfile(args.weights): # load_pretrained_weights(model, args.weights) save_dir = ckpt.dir visualize_ranked_results( dist, query_dataset, gallery_dataset, loader.data_type, width=loader.width, height=loader.height, save_dir=osp.join(save_dir, 'visrank'), # topk=visrank_topk) )
import data import loss import torch import model from trainer import Trainer from option import args import utils.utility as utility import os # os.environ['CUDA_VISIBLE_DEVICES']='0' # 指定GPU # print(args) # after run option.py can get these 参数 ckpt = utility.checkpoint(args) # args,log,dir,log_file...functions... # print(ckpt) # 以下3步可单独测试 loader = data.Data(args) model = model.Model(args, ckpt) loss = loss.Loss(args, ckpt) if not args.test_only else None # if没有冒号 # print(1) if not True else None # if没有冒号 output: # print(2) if not False else None # if没有冒号 output: 2 trainer = Trainer(args, model, loss, loader, ckpt) n = 0 while not trainer.terminate(): n += 1 trainer.train()
def main(): # parser = argparse.ArgumentParser() # parser.add_argument('--root', type=str) # parser.add_argument('-d', '--dataset', type=str, default='market1501') # parser.add_argument('-m', '--model', type=str, default='osnet_x1_0') # parser.add_argument('--weights', type=str) # parser.add_argument('--save-dir', type=str, default='log') # parser.add_argument('--height', type=int, default=256) # parser.add_argument('--width', type=int, default=128) # args = parser.parse_args() if args.config != '': with open(args.config, 'r') as f: config = yaml.load(f) for op in config: setattr(args, op, config[op]) # loader = data.Data(args) ckpt = utility.checkpoint(args) loader = data_v2.ImageDataManager(args) model = Model(args, ckpt) optimzer = optim.make_optimizer(args, model) # loss = loss.make_loss(args, ckpt) if not args.test_only else None start = -1 if args.load != '': start = ckpt.resume_from_checkpoint( osp.join(ckpt.dir, 'model-latest.pth'), model, optimzer) - 1 if args.pre_train != '': ckpt.load_pretrained_weights(model, args.pre_train) scheduler = optim.make_scheduler(args, optimzer, start) # print('[INFO] System infomation: \n {}'.format(get_pretty_env_info())) ckpt.write_log('[INFO] Model parameters: {com[0]} flops: {com[1]}'.format( com=compute_model_complexity(model, (1, 3, args.height, args.width)))) use_gpu = torch.cuda.is_available() # datamanager = torchreid.data.ImageDataManager( # root=args.root, # sources=args.dataset, # height=args.height, # width=args.width, # batch_size_train=100, # batch_size_test=100, # transforms=None, # train_sampler='SequentialSampler' # ) test_loader = loader.testloader # model = torchreid.models.build_model( # name=args.model, # num_classes=datamanager.num_train_pids, # use_gpu=use_gpu # ) if use_gpu: model = model.cuda() # if args.weights and check_isfile(args.weights): # load_pretrained_weights(model, args.weights) save_dir = ckpt.dir visactmap(model, test_loader, save_dir, args.width, args.height, use_gpu)