def setUp(self): resolutions = utils.load_yaml(self, "data/resolutions.yaml") hwinfo = HWinfo() try: hwinfo_data = hwinfo.dmi_load() manufacturer = hwinfo_data['sys_vendor'].lower() model = hwinfo_data['product_name'].lower() self.log.debug( 'checking for graphics device "%s" from vendor "%s"', manufacturer, model) if manufacturer not in resolutions: for manu in resolutions: manu = manu.lower() if manufacturer in manu or manu in manufacturer: manufacturer = manu self.expected_width = int( resolutions[manufacturer][model]['width']) self.expected_height = int( resolutions[manufacturer][model]['height']) except: self.skip('Screen info not found')
def main(): args = parse_args() config_path = Path(args.config.strip("/")) experiment_folder = config_path.parents[0] inference_config = load_yaml(config_path) dict_dir = Path(experiment_folder, inference_config["DICT_FOLDER"]) dict_dir.mkdir(exist_ok=True, parents=True) train_df, submission = prepare_train( os.path.join(os.getcwd(), "", "cloudsimg")) model = smp.Unet( encoder_name=inference_config['ENCODER'], encoder_weights="imagenet", classes=4, activation=None, ).to(device) preprocessing_fn = smp.encoders.get_preprocessing_fn( inference_config['ENCODER'], "imagenet") data_factory = DataFactory(train_df, get_preprocessing(preprocessing_fn), inference_config["DATA_PARAMS"]) test_loader = data_factory.make_test_loader() checkpoints_list = build_checkpoints_list(inference_config) for pred_idx, checkpoint_path in enumerate(checkpoints_list): torch.cuda.empty_cache() model.load_state_dict(torch.load(checkpoint_path)["state_dict"]) model.eval() current_mask_dict = predict(test_loader, model) result_path = Path(dict_dir, get_pkl_file_name(checkpoint_path)) with open(result_path, "wb") as handle: pickle.dump(current_mask_dict, handle, protocol=pickle.HIGHEST_PROTOCOL) del current_mask_dict plk_list = build_masks_list(dict_dir) avarage_masks(plk_list, experiment_folder, inference_config)
def setUp(self): wifidata = utils.load_yaml(self, "data/internet_data.yaml") if 'access_point_1' not in wifidata: self.skip("First AP not found in the yaml config") if ('ssid' not in wifidata['access_point_1'] or 'pass' not in wifidata['access_point_1']): self.skip("First AP data not found in the yaml config") if 'access_point_2' not in wifidata: self.skip("Second AP not found in the yaml config") if ('ssid' not in wifidata['access_point_2'] or 'pass' not in wifidata['access_point_2']): self.skip("Second AP data not found in the yaml config") if 'access_point_5ghz' not in wifidata: self.skip("5GHz AP not found in the yaml config") if ('ssid' not in wifidata['access_point_5ghz'] or 'pass' not in wifidata['access_point_5ghz']): self.skip("5GHz AP data not found in the yaml config") self.ap1_ssid = wifidata['access_point_1']['ssid'] self.ap1_pass = wifidata['access_point_1']['pass'] self.ap2_ssid = wifidata['access_point_2']['ssid'] self.ap2_pass = wifidata['access_point_2']['pass'] self.ap5ghz_ssid = wifidata['access_point_5ghz']['ssid'] self.ap5ghz_pass = wifidata['access_point_5ghz']['pass'] wifi_dev = internet.get_active_device('wifi', self) self.wireless_interface = wifi_dev.get_iface()
def main(): args = parse_args() config_path = Path(args.config.strip("/")) sub_config = load_yaml(config_path) sample_sub = pd.read_csv(sub_config["SAMPLE_SUB"]) mask_dict = load_mask_dict(sub_config) rle_dict = build_rle_dict(mask_dict) buid_submission(sample_sub, rle_dict, sub_config["SUB_FILE"])
def init(self, im, target_pos, target_sz, model, hp=None): state = dict() state['im_h'] = im.shape[0] state['im_w'] = im.shape[1] p = RPNConfig() # single test if not hp and not self.info.epoch_test: prefix = [x for x in ['OTB', 'VOT'] if x in self.info.dataset] cfg = load_yaml('./experiments/test/{0}/{1}.yaml'.format( prefix[0], self.info.arch)) cfg_benchmark = cfg[self.info.dataset] p.update(cfg_benchmark) p.renew() # for vot17 or vot18: from siamrpn released if '2017' in self.info.dataset: if ((target_sz[0] * target_sz[1]) / float(state['im_h'] * state['im_w'])) < 0.004: p.instance_size = 287 p.renew() else: p.instance_size = 271 p.renew() net = model p.anchor = generate_anchor(p.total_stride, p.scales, p.ratios, p.score_size) avg_chans = np.mean(im, axis=(0, 1)) wc_z = target_sz[0] + p.context_amount * sum(target_sz) hc_z = target_sz[1] + p.context_amount * sum(target_sz) s_z = python2round(np.sqrt(wc_z * hc_z)) z_crop = get_subwindow_tracking(im, target_pos, p.exemplar_size, s_z, avg_chans) z = Variable(z_crop.unsqueeze(0)) net.template(z.cuda()) if p.windowing == 'cosine': window = np.outer(np.hanning(p.score_size), np.hanning(p.score_size)) # [17,17] elif p.windowing == 'uniform': window = np.ones((p.score_size, p.score_size)) window = np.expand_dims(window, axis=0) # [1,17,17] window = np.repeat(window, p.anchor_num, axis=0) # [5,17,17] state['p'] = p state['net'] = net state['avg_chans'] = avg_chans state['window'] = window state['target_pos'] = target_pos state['target_sz'] = target_sz return state
def setUp(self): wifidata = utils.load_yaml(self, "data/internet_data.yaml") bluetoothdata = utils.load_yaml(self, "data/bluetooth_data.yaml") if 'access_point_1' not in wifidata: self.skip("No AP found in the yaml config") if ('ssid' not in wifidata['access_point_1'] or 'pass' not in wifidata['access_point_1']): self.skip("No AP data found in the yaml config") if 'testdata' not in bluetoothdata: self.skip("No bluetooth data found in the yaml config") if 'addr' not in bluetoothdata['testdata']: self.skip("No bluetooth addr found in the yaml config") self.ap_ssid = wifidata['access_point_1']['ssid'] self.ap_pass = wifidata['access_point_1']['pass'] self.targetDeviceMac = bluetoothdata['testdata']['addr']
def setUp(self): self.testdata = utils.load_yaml(self, "data/bluetooth_data.yaml") try: self.targetDeviceMac = self.testdata['testdata']['addr'] except: self.skip('Invalid testdata') if not bool( re.match('^' + '[\:\-]'.join(['([0-9a-f]{2})'] * 6) + '$', self.targetDeviceMac.lower())): self.skip('Target Device mac address invalid')
def setUp(self): wifidata = utils.load_yaml(self, "data/internet_data.yaml") if 'access_point_1' not in wifidata: self.skip("First AP not found in the yaml config") if 'access_point_2' not in wifidata: self.skip("Second AP not found in the yaml config") self.ap1_ssid = wifidata['access_point_1']['ssid'] self.ap2_ssid = wifidata['access_point_2']['ssid']
def setUp(self): wifidata = utils.load_yaml(self, "data/internet_data.yaml") if 'access_point_1' not in wifidata: self.skip("No AP found in the yaml config") if ('ssid' not in wifidata['access_point_1'] or 'pass' not in wifidata['access_point_1']): self.skip("No AP found in the yaml config") self.ap_ssid = wifidata['access_point_1']['ssid'] self.ap_pass = wifidata['access_point_1']['pass']
def tf_model_checker(torch_ckpt, tf_model): cfg = load_yaml('config.yaml') torch_model = get_fpn_net(cfg['net']) torch_model.load_state_dict(torch_ckpt) torch_model.eval() x = np.random.rand(2, 3, 320, 320).astype(np.float32) x_tf = tf.convert_to_tensor(np.transpose(x, [0, 2, 3, 1])) x_torch = torch.from_numpy(x) y_tf = tf_model(x_tf) y_torch = torch_model(x_torch) for f, c in zip(y_tf[0], y_torch[0]): f = np.transpose(f.numpy(), [0, 3, 1, 2]) c = c.detach().numpy() np.testing.assert_allclose(c, f, rtol=1e-4, atol=1e-5) print('All Good!')
def main(): config_path = sys.argv[1] opt = util.load_yaml(config_path) if opt['path']['resume_state']: # resuming training resume_state = torch.load(opt['path']['resume_state']) else: resume_state = None util.mkdir(opt['path']['log']) util.setup_logger(None, opt['path']['log'], 'train', level=logging.INFO, screen=True) util.setup_logger('val', opt['path']['log'], 'val', level=logging.INFO) logger = logging.getLogger('base') set_random_seed(0) # tensorboard log writer = SummaryWriter(log_dir=opt['path']['tb_logger']) torch.backends.cudnn.benckmark = True for phase, dataset_opt in opt['datasets'].items(): if phase == 'train': train_set = data.create_dataset(dataset_opt, phase) train_size = int( math.ceil(len(train_set) / dataset_opt['batch_size'])) logger.info('Number of train images: {:,d}, iters: {:,d}'.format( len(train_set), train_size)) total_iters = int(opt['train']['niter']) total_epochs = int(math.ceil(total_iters / train_size)) logger.info('Total epochs needed: {:d} for iters {:,d}'.format( total_epochs, total_iters)) train_loader = data.create_dataloader(train_set, dataset_opt, phase) elif phase == 'valid': val_set = data.create_dataset(dataset_opt, phase) val_loader = data.create_dataloader(val_set, dataset_opt, phase) logger.info('Number of validation images in [{:s}]: {:d}'.format( dataset_opt['name'], len(val_set))) else: raise NotImplementedError( 'Phase [{:s}] is not recognized.'.format(phase)) assert train_loader is not None # create model model = Model(opt) # resume training if resume_state: start_epoch = resume_state['epoch'] current_step = resume_state['iter'] model.load_model(current_step) model.resume_training(resume_state) # handle optimizers and schedulers else: current_step = 0 start_epoch = 0 # training logger.info('Start training from epoch: {:d}, iter: {:d}'.format( start_epoch, current_step)) for epoch in range(start_epoch, total_epochs): for _, train_data in enumerate(train_loader): current_step += 1 if current_step > total_iters: break # update learning rate model.update_learning_rate() # training model.train(train_data, current_step) # log if current_step % opt['train']['print_freq'] == 0: logs = model.get_current_log() message = '<epoch:{:3d}, iter:{:8,d}, lr:{:.3e}> '.format( epoch, current_step, model.get_current_learning_rate()) for k, v in logs.items(): message += '{:s}: {:.4e} '.format(k, v) # tensorboard logger writer.add_scalar(k, v, current_step) logger.info(message) if current_step % opt['train']['val_freq'] == 0: psnr, ssim = model.validate(val_loader, current_step) # log logger.info('# Validation # PSNR: {:.4e} SSIM: {:.4e}'.format( psnr, ssim)) logger_val = logging.getLogger('val') # validation logger logger_val.info( '<epoch:{:3d}, iter:{:8,d}> psnr: {:.4e} ssim: {:.4e}'. format(epoch, current_step, psnr, ssim)) # tensorboard logger writer.add_scalar('VAL_PSNR', psnr, current_step) writer.add_scalar('VAL_SSIM', ssim, current_step) # save models and training states if current_step % opt['train']['save_step'] == 0: logger.info('Saving models and training states.') model.save_model(epoch, current_step)
def init(self, im, target_pos, target_sz, model, hp=None): state = dict() # epoch test p = DAGConfig() state['im_h'] = im.shape[0] state['im_w'] = im.shape[1] if not hp and self.info.epoch_test: prefix = [x for x in ['OTB', 'VOT'] if x in self.info.dataset] if len(prefix) == 0: prefix = [self.info.dataset] absPath = os.path.abspath(os.path.dirname(__file__)) yname = 'DAG.yaml' yamlPath = os.path.join( absPath, '../../experiments/test/{0}/'.format(prefix[0]), yname) cfg = load_yaml(yamlPath) if self.online: temp = self.info.dataset + 'ON' cfg_benchmark = cfg[temp] else: cfg_benchmark = cfg[self.info.dataset] p.update(cfg_benchmark) p.renew() if ((target_sz[0] * target_sz[1]) / float(state['im_h'] * state['im_w'])) < 0.004: p.instance_size = cfg_benchmark['big_sz'] p.renew() else: p.instance_size = cfg_benchmark['small_sz'] p.renew() if hp: p.update(hp) p.renew() if ((target_sz[0] * target_sz[1]) / float(state['im_h'] * state['im_w'])) < 0.004: p.instance_size = hp['big_sz'] p.renew() else: p.instance_size = hp['small_sz'] p.renew() if self.trt: print( '====> TRT version testing: only support 255 input, the hyper-param is random <====' ) p.instance_size = 255 p.renew() self.grids(p) net = model wc_z = target_sz[0] + p.context_amount * sum(target_sz) hc_z = target_sz[1] + p.context_amount * sum(target_sz) s_z = round(np.sqrt(wc_z * hc_z)) avg_chans = np.mean(im, axis=(0, 1)) z_crop, _ = get_subwindow_tracking(im, target_pos, p.exemplar_size, s_z, avg_chans) z = z_crop.unsqueeze(0) net.template(z.cuda()) if p.windowing == 'cosine': window = np.outer(np.hanning(p.score_size), np.hanning(p.score_size)) # [17,17] elif p.windowing == 'uniform': window = np.ones(int(p.score_size), int(p.score_size)) state['p'] = p state['net'] = net state['avg_chans'] = avg_chans state['window'] = window state['target_pos'] = target_pos state['target_sz'] = target_sz return state
def init(self, im, target_pos, target_sz, model, hp=None): state = dict() # epoch test p = FCConfig() # single test if not hp and not self.info.epoch_test: prefix = [x for x in ['OTB', 'VOT'] if x in self.info.dataset] cfg = load_yaml('./experiments/test/{0}/{1}.yaml'.format(prefix[0], self.info.arch)) cfg_benchmark = cfg[self.info.dataset] p.update(cfg_benchmark) p.renew() # param tune if hp: p.update(hp) p.renew() net = model avg_chans = np.mean(im, axis=(0, 1)) wc_z = target_sz[0] + p.context_amount * sum(target_sz) hc_z = target_sz[1] + p.context_amount * sum(target_sz) s_z = round(np.sqrt(wc_z * hc_z)) scale_z = p.exemplar_size / s_z z_crop = get_subwindow_tracking(im, target_pos, p.exemplar_size, s_z, avg_chans) z = Variable(z_crop.unsqueeze(0)) z_f = net.feature_extractor(z.cuda()) net.template(z.cuda()) d_search = (p.instance_size - p.exemplar_size) / 2 pad = d_search / scale_z s_x = s_z + 2 * pad min_s_x = 0.2 * s_x max_s_x = 5 * s_x s_x_serise = {'s_x': s_x, 'min_s_x': min_s_x, 'max_s_x': max_s_x} p.update(s_x_serise) z = Variable(z_crop.unsqueeze(0)) z_f = net.feature_extractor(z.cuda()) net.kernel(z_f) if p.windowing == 'cosine': window = np.outer(np.hanning(int(p.score_size) * int(p.response_up)), np.hanning(int(p.score_size) * int(p.response_up))) elif p.windowing == 'uniform': window = np.ones(int(p.score_size) * int(p.response_up), int(p.score_size) * int(p.response_up)) window /= window.sum() p.scales = p.scale_step ** (range(p.num_scale) - np.ceil(p.num_scale // 2)) state['p'] = p state['net'] = net state['avg_chans'] = avg_chans state['window'] = window state['target_pos'] = target_pos state['target_sz'] = target_sz state['z_0'] = z_f.cpu().data state['z_f'] = z_f.cpu().data state['im_h'] = im.shape[0] state['im_w'] = im.shape[1] return state
def __init__(self, root, augment, strides=(4, ), mode='train'): cfg = load_yaml('config.yaml') self.min_area = cfg['train']['transforms']['min_area'] self.in_size = cfg['train']['transforms']['in_size'] super().__init__(root, augment, strides, mode)
def init(self, im, target_pos, target_sz, model, hp=None): self.frame_num = 1 self.temp_max = 0 self.lost_count = 0 state = dict() state['im_h'] = im.shape[0] state['im_w'] = im.shape[1] p = RPNConfig() # single test if not hp and not self.info.epoch_test: prefix = [x for x in ['OTB', 'VOT'] if x in self.info.dataset] cfg_ = load_yaml('../experiments/test/{0}/{1}.yaml'.format(prefix[0], self.info.arch)) cfg_benchmark = cfg_[self.info.dataset] p.update(cfg_benchmark) p.renew() # for vot17 or vot18: from siamrpn released if '2017' in self.info.dataset: if ((target_sz[0] * target_sz[1]) / float(state['im_h'] * state['im_w'])) < 0.004: p.instance_size = 287 p.renew() else: p.instance_size = 271 p.renew() # param tune if hp: p.update(hp) p.renew() # for small object (from DaSiamRPN released) if ((target_sz[0] * target_sz[1]) / float(state['im_h'] * state['im_w'])) < 0.004: p.instance_size = hp['big_sz'] p.renew() else: p.instance_size = hp['small_sz'] p.renew() net = model p.anchor = generate_anchor(p.total_stride, p.scales, p.ratios, p.score_size) avg_chans = np.mean(im, axis=(0, 1)) wc_z = target_sz[0] + p.context_amount * sum(target_sz) hc_z = target_sz[1] + p.context_amount * sum(target_sz) s_z = python2round(np.sqrt(wc_z * hc_z)) z_crop = get_subwindow_tracking(im, target_pos, p.exemplar_size, s_z, avg_chans) z = Variable(z_crop.unsqueeze(0)) net.template(z.cuda()) if p.windowing == 'cosine': window = np.outer(np.hanning(p.score_size), np.hanning(p.score_size)) # [17,17] elif p.windowing == 'uniform': window = np.ones((p.score_size, p.score_size)) window = np.expand_dims(window, axis=0) # [1,17,17] window = np.repeat(window, p.anchor_num, axis=0) # [5,17,17] if cfg.TRACK.USE_CLASSIFIER: # atom = ATOM().cuda().eval() # checkpoint_dict = torch.load("/home/zhuyi/Code/CRPN_511/atom_pretrain.pth") # atom.load_state_dict(checkpoint_dict["net"],strict=False) self.classifier = BaseClassifier(net) self.center_pos = np.array(target_pos) self.size = np.array(target_sz) bbox = [target_pos[0]-(target_sz[0]-1)/2,target_pos[1]-(target_sz[1]-1)/2,target_sz[0],target_sz[1]] #bbox = [cx - (w - 1) / 2, cy - (h - 1) / 2, w, h] if cfg.TRACK.TEMPLATE_UPDATE: with torch.no_grad(): net.template_short_term(self.z_crop) s_xx = s_z * (cfg.TRACK.INSTANCE_SIZE * 2 / cfg.TRACK.EXEMPLAR_SIZE) x_crop = get_subwindow_tracking(im, self.center_pos, cfg.TRACK.INSTANCE_SIZE * 2, round(s_xx), avg_chans) x_crop =x_crop.unsqueeze(0) self.classifier.initialize(x_crop.type(torch.FloatTensor), bbox) state['p'] = p state['net'] = net state['avg_chans'] = avg_chans state['window'] = window state['target_pos'] = target_pos state['target_sz'] = target_sz return state
if box_area > self.min_area: return True return False def name(self): return 'virat' if __name__ == "__main__": from transformations import get_train_transforms, get_val_transforms import matplotlib.pyplot as plt import cv2 from api import decode from torchvision.transforms.functional import to_pil_image cfg = load_yaml('config.yaml') root = cfg['paths']['virat_dir'] D = CamNetDataset(root, augment=get_val_transforms(cfg['train']['transforms']), mode='train') print(len(D)) for d in range(0, len(D)): # print(d) img, hms, labels = D[d] show = np.zeros([hms[0].shape[1], hms[0].shape[2], 3]) show[:, :, 0] = hms[0][0] show = np.array(to_pil_image(img)) hm = torch.Tensor(hms[0][0]).unsqueeze(0).unsqueeze(0).float().cuda() of = torch.Tensor(hms[0][1:3]).unsqueeze(0).float().cuda() wh = torch.Tensor(hms[0][3:5]).unsqueeze(0).float().cuda()
def cluster_loader(base_config): cmd_line_options = cli_parser() testconfig = load_yaml(cmd_line_options.config_file) oc_client = cmd.oc.ocBase(cmd_line_options, base_config, testconfig) oc_client.check_oc_version()
data = data.cuda() labels = [label.cuda() for label in labels] with amp.autocast(): out = net(data) loss_dict, _ = self.criterion(out, labels) loss_metric.add_sample(loss_dict) self.writer.log_eval(step, loss_metric) def test_ap(self, net, epoch): for dataset in self.test_datasets: ap, _ = test(net, dataset, batch_size=self.batch_size) self.writer.log_ap(epoch, ap, dataset.name()) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-gpu", type=int, default=0, help='gpu to run on.') args = parser.parse_args() os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu) os.system('echo $CUDA_VISIBLE_DEVICES') cfg_path = 'config.yaml' cfg = load_yaml(cfg_path) trainer = Trainer(cfg) trainer.train()
def init(self, im, target_pos, target_sz, model, hp=None, online=False, mask=None, debug=False): # in: whether input infrared image state = dict() # epoch test p = AdaConfig() self.debug = debug state['im_h'] = im.shape[0] state['im_w'] = im.shape[1] self.imh = state['im_h'] self.imw = state['im_w'] # single test # if not hp and not self.info.epoch_test: if True: prefix = [x for x in ['OTB', 'VOT'] if x in self.info.dataset] if len(prefix) == 0: prefix = [self.info.dataset] absPath = os.path.abspath(os.path.dirname(__file__)) yname='OceanPlus.yaml' yamlPath = os.path.join(absPath, '../../experiments/test/VOT/', yname) cfg = load_yaml(yamlPath) if self.info.dataset not in list(cfg.keys()): print('[*] unsupported benchmark, use VOT2020 hyper-parameters (not optimal)') cfg_benchmark = cfg['VOT2020'] else: cfg_benchmark = cfg[self.info.dataset] p.update(cfg_benchmark) p.renew() if ((target_sz[0] * target_sz[1]) / float(state['im_h'] * state['im_w'])) < 0.004: p.instance_size = cfg_benchmark['big_sz'] p.renew() else: p.instance_size = cfg_benchmark['small_sz'] p.renew() self.grids(p) # self.grid_to_search_x, self.grid_to_search_y net = model # param tune if hp: p.update(hp) if 'lambda_u' in hp.keys() or 'lambda_s' in hp.keys(): net.update_lambda(hp['lambda_u'], hp['lambda_s']) if 'iter1' in hp.keys() or 'iter2' in hp.keys(): net.update_iter(hp['iter1'], hp['iter2']) print('======= hyper-parameters: pk: {:.3f}, wi: {:.2f}, lr: {:.2f} ======='.format(p.penalty_k, p.window_influence, p.lr)) wc_z = target_sz[0] + p.context_amount * sum(target_sz) hc_z = target_sz[1] + p.context_amount * sum(target_sz) s_z = round(np.sqrt(wc_z * hc_z)) avg_chans = np.mean(im, axis=(0, 1)) z_crop, _ = get_subwindow_tracking(im, target_pos, p.exemplar_size, s_z, avg_chans) mask_crop, _ = get_subwindow_tracking_mask(mask, target_pos, p.exemplar_size, s_z, out_mode=None) mask_crop = (mask_crop > 0.5).astype(np.uint8) mask_crop = torch.from_numpy(mask_crop) # vis zcrop # vis = 0.5 * z_crop.permute(1,2,0) + 255 * mask_crop.unsqueeze(-1).float() # cv2.imwrite('zcrop.jpg', vis.numpy()) z = z_crop.unsqueeze(0) net.template(z.cuda(), mask_crop.unsqueeze(0).cuda()) if p.windowing == 'cosine': window = np.outer(np.hanning(p.score_size), np.hanning(p.score_size)) # [17,17] elif p.windowing == 'uniform': window = np.ones(int(p.score_size), int(p.score_size)) state['p'] = p state['net'] = net state['avg_chans'] = avg_chans state['window'] = window state['target_pos'] = target_pos state['target_sz'] = target_sz self.p = p self.debug_on_crop = False self.debug_on_ori = False self.save_mask = False # save all mask results self.mask_ratio = False self.update_template = True if self.debug_on_ori or self.debug_on_crop: print('Warning: debuging...') print('Warning: turning off debugging mode after this process') self.debug = True return state
def setUp(self): self.inference_yaml = load_yaml(os.path.join( get_project_root(), "tests/test_inference.yaml"))
import sys import os.path import glob import cv2 import numpy as np import torch from networks.generator import Generator import utils.utils as util cfg = util.load_yaml('../Configs/Train/config_sr.yml') model_path = sys.argv[1] device = torch.device('cuda') # device = torch.device('cpu') input_dir = '/content/drive/MyDrive/MajorProject/results_r2b/*' output_dir = '/content/drive/MyDrive/MajorProject/results_sr/' util.mkdir(output_dir) model = Generator(cfg['network_G']) model.load_state_dict(torch.load(model_path), strict=False) model.eval() for k, v in model.named_parameters(): v.requires_grad = False model = model.to(device) print('Model path {:s}. \nTesting...'.format(model_path)) idx = 0 for path in glob.glob(input_dir): idx += 1 base = os.path.splitext(os.path.basename(path))[0]
import sys import os.path import glob import cv2 import numpy as np import torch from R2B_Network.networks.generator import Generator as r2b_gen from SR_Network.networks.generator import Generator as sr_gen import utils.utils as util cfg_test = util.load_yaml('Config/Test/test_config.yml') r2b_model_path = cfg_test['r2b_model_path'] sr_model_path = cfg_test['sr_model_path'] device = torch.device('cuda') # device = torch.device('cpu') input_dir = cfg_test['LR_dir'] output_dir = cfg_test['SR_dir'] util.mkdir(output_dir) r2b_model = r2b_gen(cfg_test['network_R2B']) sr_model = sr_gen(cfg_test['network_SR']) r2b_model.load_state_dict(torch.load(r2b_model_path), strict=False) print('Loaded R2B model at {}'.format(r2b_model_path)) sr_model.load_state_dict(torch.load(sr_model_path), strict=False) print('Loaded SR model at {}'.format(sr_model_path)) r2b_model.eval()
def init(self, im, target_pos, target_sz, model, hp=None): state = dict() # epoch test p = FCConfig() # single test if not hp and not self.info.epoch_test: prefix = [x for x in ['OTB', 'VOT'] if x in self.info.dataset] absPath = os.path.abspath(os.path.dirname(__file__)) yname = 'SiamDW.yaml' yamlPath = os.path.join( absPath, '../../experiments/test/{0}/'.format(prefix[0]), yname) cfg = load_yaml(yamlPath) cfg_benchmark = cfg[self.info.dataset] p.update(cfg_benchmark) p.renew() # param tune if hp: p.update(hp) p.renew() print( '======= hyper-parameters: scale_step: {}, scale_penalty: {}, scale_lr: {} =======' .format(p.scale_step, p.scale_penalty, p.scale_lr)) net = model avg_chans = np.mean(im, axis=(0, 1)) wc_z = target_sz[0] + p.context_amount * sum(target_sz) hc_z = target_sz[1] + p.context_amount * sum(target_sz) s_z = round(np.sqrt(wc_z * hc_z)) scale_z = p.exemplar_size / s_z z_crop, _ = get_subwindow_tracking(im, target_pos, p.exemplar_size, s_z, avg_chans) d_search = (p.instance_size - p.exemplar_size) / 2 pad = d_search / scale_z s_x = s_z + 2 * pad min_s_x = 0.2 * s_x max_s_x = 5 * s_x s_x_serise = {'s_x': s_x, 'min_s_x': min_s_x, 'max_s_x': max_s_x} p.update(s_x_serise) z = Variable(z_crop.unsqueeze(0)) net.template(z.cuda()) if p.windowing == 'cosine': window = np.outer( np.hanning(int(p.score_size) * int(p.response_up)), np.hanning(int(p.score_size) * int(p.response_up))) elif p.windowing == 'uniform': window = np.ones( int(p.score_size) * int(p.response_up), int(p.score_size) * int(p.response_up)) window /= window.sum() p.scales = p.scale_step**(range(p.num_scale) - np.ceil(p.num_scale // 2)) state['p'] = p state['net'] = net state['avg_chans'] = avg_chans state['window'] = window state['target_pos'] = target_pos state['target_sz'] = target_sz state['im_h'] = im.shape[0] state['im_w'] = im.shape[1] return state
def init(self, im, target_pos, target_sz, model, hp=None): # in: whether input infrared image state = dict() # epoch test p = OceanConfig() state['im_h'] = im.shape[0] state['im_w'] = im.shape[1] # single test if not hp and not self.info.epoch_test: prefix = [ x for x in ['OTB', 'VOT', 'GOT10K', 'LASOT'] if x in self.info.dataset ] if len(prefix) == 0: prefix = [self.info.dataset] absPath = os.path.abspath(os.path.dirname(__file__)) yname = 'Ocean.yaml' yamlPath = os.path.join( absPath, '../../experiments/test/{0}/'.format(prefix[0]), yname) cfg = load_yaml(yamlPath) if self.online: temp = self.info.dataset + 'ON' cfg_benchmark = cfg[temp] else: cfg_benchmark = cfg[self.info.dataset] p.update(cfg_benchmark) p.renew() if ((target_sz[0] * target_sz[1]) / float(state['im_h'] * state['im_w'])) < 0.004: p.instance_size = cfg_benchmark['big_sz'] p.renew() else: p.instance_size = cfg_benchmark['small_sz'] p.renew() # double check # print('======= hyper-parameters: penalty_k: {}, wi: {}, lr: {}, ratio: {}, instance_sz: {}, score_sz: {} ======='.format(p.penalty_k, p.window_influence, p.lr, p.ratio, p.instance_size, p.score_size)) # param tune if hp: p.update(hp) p.renew() # for small object (from DaSiamRPN released) if ((target_sz[0] * target_sz[1]) / float(state['im_h'] * state['im_w'])) < 0.004: p.instance_size = hp['big_sz'] p.renew() else: p.instance_size = hp['small_sz'] p.renew() if self.trt: print( '====> TRT version testing: only support 255 input, the hyper-param is random <====' ) p.instance_size = 255 p.renew() self.grids(p) # self.grid_to_search_x, self.grid_to_search_y net = model wc_z = target_sz[0] + p.context_amount * sum(target_sz) hc_z = target_sz[1] + p.context_amount * sum(target_sz) s_z = round(np.sqrt(wc_z * hc_z)) avg_chans = np.mean(im, axis=(0, 1)) z_crop, _ = get_subwindow_tracking(im, target_pos, p.exemplar_size, s_z, avg_chans) z = z_crop.unsqueeze(0) net.template(z.cuda()) if p.windowing == 'cosine': window = np.outer(np.hanning(p.score_size), np.hanning(p.score_size)) # [17,17] elif p.windowing == 'uniform': window = np.ones(int(p.score_size), int(p.score_size)) state['p'] = p state['net'] = net state['avg_chans'] = avg_chans state['window'] = window state['target_pos'] = target_pos state['target_sz'] = target_sz return state
def init(self, bgr_image, image, siam_net, target_pos, target_sz, load_online, dataname='VOT2019', resume=None): # Initialize some stuff self.frame_num = 1 self.load_online = load_online # -------------------------------------------- # Init hyper-parameter # print('====> init phase: load default parameters') self.p = ONLINEConfig() absPath = os.path.abspath(os.path.dirname(__file__)) yname = 'ONLINE.yaml' if 'VOT' in dataname else 'ONLINE_NV.yaml' yamlPath = os.path.join(absPath, '../../experiments/test/VOT/', yname) cfg = load_yaml(yamlPath, subset=False) self.p.update(cfg) self.params_convert() # --------------------------------------------- # Init feature extractor # --------------------------------------------- self.params.net = models.__dict__['ONLINEnet50'](backbone=None).cuda() # Initialize network self.features_initialized = True # ads_path = os.path.dirname(os.path.abspath(__file__)) resume_path = os.path.join(absPath, '../../', resume) self.params.net = load_pretrain(self.params.net, resume_path, print_unuse=False) self.params.net.feature_extractor = siam_net self.params.net.cuda() self.params.net.eval() # --------------------------------------------- # ONLINE init phase # --------------------------------------------- if not hasattr(self.p, 'device'): self.p.device = 'cuda' if self.p.use_gpu else 'cpu' # The ONLINE network self.net = self.params.net # Time initialization tic = time.time() # Get target position and size self.pos = torch.Tensor([target_pos[1], target_pos[0]]) # height, width self.target_sz = torch.Tensor([target_sz[1], target_sz[0]]) # Set sizes sz = self.p.image_sample_size self.img_sample_sz = torch.Tensor( [sz, sz] if isinstance(sz, int) else sz) self.img_support_sz = self.img_sample_sz # Set search area search_area = torch.prod(self.target_sz * self.p.search_area_scale).item() self.target_scale = math.sqrt( search_area) / self.img_sample_sz.prod().sqrt() # Target size in base scale self.base_target_sz = self.target_sz / self.target_scale # Convert image im = numpy_to_torch(image) # image is RGB # Setup scale factors if not hasattr(self.p, 'scale_factors'): self.p.scale_factors = torch.ones(1) elif isinstance(self.p.scale_factors, (list, tuple)): self.p.scale_factors = torch.Tensor(self.p.scale_factors) # Setup scale bounds self.image_sz = torch.Tensor([im.shape[2], im.shape[3]]) self.min_scale_factor = torch.max(10 / self.base_target_sz) self.max_scale_factor = torch.min(self.image_sz / self.base_target_sz) # Extract and transform sample init_backbone_feat = self.generate_init_samples(im) # Initialize classifier self.init_classifier(init_backbone_feat) # Initialize IoUNet if getattr(self.p, 'use_iou_net', True): self.init_iou_net(init_backbone_feat) out = {'time': time.time() - tic} return out