def __init__(self, image, region): self.window = max(region.width, region.height) * 2 left = max(region.x, 0) top = max(region.y, 0) right = min(region.x + region.width, image.shape[1] - 1) bottom = min(region.y + region.height, image.shape[0] - 1) self.position = (region.x + region.width / 2, region.y + region.height / 2) self.size = (region.width, region.height) self.siam = SiameseNet(BaselineEmbeddingNet()) # weights_init(siam) siamfc_path = project_path + "/models/siamfc_pretrained.pth" pretrained_siam = torch.load(siamfc_path) siam_dict = self.siam.state_dict() pretrained_siam = {k: v for k, v in pretrained_siam.items() if k in siam_dict} siam_dict.update(pretrained_siam) self.siam.load_state_dict(siam_dict) self.pi = T_Policy(T_N) pretrained_pi_dict = torch.load(project_path + '/models/template_policy/234400_template_policy.pth') pi_dict = self.pi.state_dict() pretrained_pi_dict = {k: v for k, v in pretrained_pi_dict.items() if k in pi_dict} # pretrained_pi_dict = {k: v for k, v in pretrained_pi_dict.items() if k in pi_dict and k.startswith("conv")} pi_dict.update(pretrained_pi_dict) self.pi.load_state_dict(pi_dict) self.actor = Actor() # .load_state_dict(torch.load("../Models/500_actor.pth")) pretrained_act_dict = torch.load(project_path + "/models/Double_agent/234400_DA_actor.pth") actor_dict = self.actor.state_dict() pretrained_act_dict = {k: v for k, v in pretrained_act_dict.items() if k in actor_dict} actor_dict.update(pretrained_act_dict) self.actor.load_state_dict(actor_dict) self.tracker = SiamFCTracker(model_path=siamfc_path, gpu_id=0) if torch.cuda.is_available(): self.siam = self.siam.cuda() self.pi = self.pi.cuda() init_bbox = np.array([left, top, region.width, region. height]) self.rate = init_bbox[2] / init_bbox[3] self.template = self.tracker.init(image, init_bbox) self.templates = [] for i in range(T_N): self.templates.append(self.template) self.deta_flag, self.out_flag_first = init_actor(self.actor, image, init_bbox)
def main(env_name, n_epochs, eval_frequency, actor_net_dim, critic_net_dim, dsicriminator_net_dim, lr, gamma, tau, grad_clip, batch_size, entropy_weight, min_buffer_size, clip, ppo_updates, expert, activation, value_coef, betas, max_steps, tag, record): seed = np.random.randint(0, 1000000) import pybulletgym discriminator_updates = 1 expert, activation = initiate_run( env_name, actor_net_dim, critic_net_dim, dsicriminator_net_dim, lr, gamma, tau, grad_clip, batch_size, entropy_weight, min_buffer_size, clip, ppo_updates, discriminator_updates, expert, activation, value_coef, betas, max_steps, seed, tag, record) env = Env(env_name) actor = Actor(env, actor_net_dim, activation, env.env.action_space.high, env.env.action_space.low) critic = Critic(env, critic_net_dim, activation) discriminator = Discriminator(env, dsicriminator_net_dim, lr, batch_size, activation, betas) agent = Agent(gamma, clip, actor, critic, lr, batch_size, grad_clip, entropy_weight, value_coef, betas) memory = PPOMemory(gamma, tau) args = [ min_buffer_size, eval_frequency, ppo_updates, discriminator_updates, expert, seed ] gail = GAIL(env, actor, critic, discriminator, agent, memory, *args) epoch_to_best = gail.update(n_epochs, max_steps, record) if record: neptune.log_metric('best_epoch', epoch_to_best) neptune.stop()
def __init__(self, state_dim, action_dim, max_action, train_with_conv): self.device = torch.device( "cuda" if torch.cuda.is_available() else "cpu") self.train_with_conv = train_with_conv self.actor = Actor(state_dim, action_dim, max_action, train_with_conv).to(self.device) self.actor_target = Actor(state_dim, action_dim, max_action, train_with_conv).to(self.device) self.actor_target.load_state_dict(self.actor.state_dict()) self.actor_optimizer = torch.optim.Adam(self.actor.parameters()) self.critic = Critic(state_dim, action_dim, train_with_conv).to(self.device) self.critic_target = Critic(state_dim, action_dim, train_with_conv).to(self.device) self.critic_target.load_state_dict(self.critic.state_dict()) self.critic_optimizer = torch.optim.Adam(self.critic.parameters()) self.max_action = max_action
def __init__(self, net_path=None): super().init(name='ACTTracker', is_deterministic=True) np.random.seed(123) torch.manual_seed(456) torch.cuda.manual_seed(789) self.model = MDNet() self.actor = Actor() self.result = [] self.result_bb = [] self.success = 1 if opts['use_gpu']: self.model = self.model.cuda() self.actor = self.actor.cuda() self.model.set_learnable_params(opts['ft_layers']) self.criterion = BinaryLoss() self.init_optimizer = set_optimizer(self.model, opts['lr_init']) self.update_optimizer = set_optimizer(self.model, opts['lr_update']) self.detetion = 0 self.frame = 0
def main(): args = setup_parser() config = load_config_from_yaml(args.config) config.set_args(args) env_name = config['env'].get() env = gym.make(env_name) state_dim = env.observation_space.shape[0] action_dim = env.action_space.shape[0] seed = np.random.randint(0, 100000) torch.manual_seed(seed) np.random.seed(seed) actor = Actor(state_dim, action_dim, config['actor_net_dim'].get(), config['lr'].get(), config['betas'].get()).to( device) expert = Expert(config['expert'].get(), state_dim) discriminator = Discriminator(state_dim, action_dim, config['discriminator_net_dim'].get(), config['lr'].get(), config['betas'].get(), expert, actor, config['batch_size'].get()).to(device) gail = GAIL(actor, discriminator) # training procedure start_time = time.time() max_reward = float('-inf') n_epochs = config['n_epochs'].get() n_iter = config['n_iter'].get() eval_frequency = config['eval_frequency'].get() n_eval_episodes = config['n_eval_episodes'].get() max_timesteps = config['max_timesteps'].get() for epoch in range(1, n_epochs + 1): # update policy n_iter times params = gail.update(n_iter) if epoch % eval_frequency == 0: # evaluate in environment avg_reward = evaluate(n_eval_episodes, env, max_timesteps, gail) if avg_reward > max_reward: torch.save(params, "./actor_weights" + str(env_name) + str(seed)) max_reward = max(avg_reward, max_reward) print("Epoch: {}\tAvg Reward: {} in {}".format(epoch, avg_reward, datetime.timedelta( seconds=(time.time() - start_time).__round__(0))))
class TD3: def __init__(self, state_dim, action_dim, max_action, train_with_conv): self.device = torch.device( "cuda" if torch.cuda.is_available() else "cpu") self.train_with_conv = train_with_conv self.actor = Actor(state_dim, action_dim, max_action, train_with_conv).to(self.device) self.actor_target = Actor(state_dim, action_dim, max_action, train_with_conv).to(self.device) self.actor_target.load_state_dict(self.actor.state_dict()) self.actor_optimizer = torch.optim.Adam(self.actor.parameters()) self.critic = Critic(state_dim, action_dim, train_with_conv).to(self.device) self.critic_target = Critic(state_dim, action_dim, train_with_conv).to(self.device) self.critic_target.load_state_dict(self.critic.state_dict()) self.critic_optimizer = torch.optim.Adam(self.critic.parameters()) self.max_action = max_action def select_action(self, state): if self.train_with_conv: state = torch.Tensor(state.reshape(1, 3, 96, 96)).to( self.device) # TODO re-write for multi and single dim input else: state = torch.Tensor(state.reshape(1, -1)).to(self.device) return self.actor(state).cpu().data.numpy().flatten() def train(self, replay_buffer, iterations, batch_size=100, discount=0.99, tau=0.005, policy_noise=0.2, noise_clip=0.5, policy_freq=2): start_time = time.time() for it in range(iterations): print(it / iterations) # Step 4: We sample a batch of transitions (s, s’, a, r) from the memory batch_states, batch_next_states, batch_actions, batch_rewards, batch_dones = replay_buffer.sample( batch_size) state = torch.Tensor(batch_states).to(self.device) next_state = torch.Tensor(batch_next_states).to(self.device) action = torch.Tensor(batch_actions).to(self.device) reward = torch.Tensor(batch_rewards).to(self.device) done = torch.Tensor(batch_dones).to(self.device) # Step 5: From the next state s’, the Actor target plays the next action a’ next_action = self.actor_target(next_state) # Step 6: We add Gaussian noise to this next action a’ and we clamp it in a range of values supported by the environment noise = torch.Tensor(batch_actions).data.normal_( 0, policy_noise).to(self.device) noise = noise.clamp(-noise_clip, noise_clip) next_action = (next_action + noise).clamp(-self.max_action, self.max_action) # Step 7: The two Critic targets take each the couple (s’, a’) as input and return two Q-values Qt1(s’,a’) and Qt2(s’,a’) as outputs target_Q1, target_Q2 = self.critic_target(next_state, next_action) # Step 8: We keep the minimum of these two Q-values: min(Qt1, Qt2) target_Q = torch.min(target_Q1, target_Q2) # Step 9: We get the final target of the two Critic models, which is: Qt = r + γ * min(Qt1, Qt2), where γ is the discount factor target_Q = reward + ((1 - done) * discount * target_Q).detach() # Step 10: The two Critic models take each the couple (s, a) as input and return two Q-values Q1(s,a) and Q2(s,a) as outputs current_Q1, current_Q2 = self.critic(state, action) # Step 11: We compute the loss coming from the two Critic models: Critic Loss = MSE_Loss(Q1(s,a), Qt) + MSE_Loss(Q2(s,a), Qt) critic_loss = F.mse_loss(current_Q1, target_Q) + F.mse_loss( current_Q2, target_Q) # Step 12: We backpropagate this Critic loss and update the parameters of the two Critic models with a SGD optimizer self.critic_optimizer.zero_grad() critic_loss.backward() self.critic_optimizer.step() # Step 13: Once every two iterations, we update our Actor model by performing gradient ascent on the output of the first Critic model if it % policy_freq == 0: actor_loss = -self.critic.Q1(state, self.actor(state)).mean() self.actor_optimizer.zero_grad() actor_loss.backward() self.actor_optimizer.step() # Step 14: Still once every two iterations, we update the weights of the Actor target by polyak averaging for param, target_param in zip(self.actor.parameters(), self.actor_target.parameters()): target_param.data.copy_(tau * param.data + (1 - tau) * target_param.data) # Step 15: Still once every two iterations, we update the weights of the Critic target by polyak averaging for param, target_param in zip( self.critic.parameters(), self.critic_target.parameters()): target_param.data.copy_(tau * param.data + (1 - tau) * target_param.data) end_time = time.time() training_iteration = (end_time - start_time) return training_iteration # Making a save method to save a trained model def save(self, filename, directory): torch.save(self.actor.state_dict(), '%s/%s_actor.pth' % (directory, filename)) torch.save(self.critic.state_dict(), '%s/%s_critic.pth' % (directory, filename)) # Making a load method to load a pre-trained model def load(self, filename, directory): self.actor.load_state_dict( torch.load('%s/%s_actor.pth' % (directory, filename))) self.critic.load_state_dict( torch.load('%s/%s_critic.pth' % (directory, filename)))
class DATracker(object): def __init__(self, image, region): self.window = max(region.width, region.height) * 2 left = max(region.x, 0) top = max(region.y, 0) right = min(region.x + region.width, image.shape[1] - 1) bottom = min(region.y + region.height, image.shape[0] - 1) self.position = (region.x + region.width / 2, region.y + region.height / 2) self.size = (region.width, region.height) self.siam = SiameseNet(BaselineEmbeddingNet()) # weights_init(siam) siamfc_path = project_path + "/models/siamfc_pretrained.pth" pretrained_siam = torch.load(siamfc_path) siam_dict = self.siam.state_dict() pretrained_siam = {k: v for k, v in pretrained_siam.items() if k in siam_dict} siam_dict.update(pretrained_siam) self.siam.load_state_dict(siam_dict) self.pi = T_Policy(T_N) pretrained_pi_dict = torch.load(project_path + '/models/template_policy/234400_template_policy.pth') pi_dict = self.pi.state_dict() pretrained_pi_dict = {k: v for k, v in pretrained_pi_dict.items() if k in pi_dict} # pretrained_pi_dict = {k: v for k, v in pretrained_pi_dict.items() if k in pi_dict and k.startswith("conv")} pi_dict.update(pretrained_pi_dict) self.pi.load_state_dict(pi_dict) self.actor = Actor() # .load_state_dict(torch.load("../Models/500_actor.pth")) pretrained_act_dict = torch.load(project_path + "/models/Double_agent/234400_DA_actor.pth") actor_dict = self.actor.state_dict() pretrained_act_dict = {k: v for k, v in pretrained_act_dict.items() if k in actor_dict} actor_dict.update(pretrained_act_dict) self.actor.load_state_dict(actor_dict) self.tracker = SiamFCTracker(model_path=siamfc_path, gpu_id=0) if torch.cuda.is_available(): self.siam = self.siam.cuda() self.pi = self.pi.cuda() init_bbox = np.array([left, top, region.width, region. height]) self.rate = init_bbox[2] / init_bbox[3] self.template = self.tracker.init(image, init_bbox) self.templates = [] for i in range(T_N): self.templates.append(self.template) self.deta_flag, self.out_flag_first = init_actor(self.actor, image, init_bbox) def update(self, image): np_img = np.array(cv2.resize(image, (255, 255), interpolation=cv2.INTER_AREA)).transpose(2, 0, 1) np_imgs = [] for i in range(T_N): np_imgs.append(np_img) with torch.no_grad(): responses = self.siam(torch.Tensor(self.templates).permute(0, 3, 1, 2).float().cuda(), torch.Tensor(np_imgs).float().cuda()) action = self.pi(responses.permute(1, 0, 2, 3).cuda()).cpu().detach().numpy() action_id = np.argmax(action) # print(action_id) if action[0][action_id] * 0.9 > action[0][0]: template = self.templates[action_id] else: template = self.templates[0] with torch.no_grad(): siam_box = self.tracker.update(image, template) siam_box = np.round([siam_box[0], siam_box[1], siam_box[2] -siam_box[0], siam_box[3] - siam_box[1]]) bbox = siam_box for i in range(5): img_g, img_l, out_flag = getbatch_actor(np.array(image), np.array(bbox).reshape([1, 4])) with torch.no_grad(): deta_pos = self.actor(img_l, img_g) deta_pos = deta_pos.data.clone().cpu().numpy() if deta_pos[:, 2] > 0.2 or deta_pos[:, 2] < -0.2: deta_pos[:, 2] = 0 if self.deta_flag or (out_flag and not self.out_flag_first): deta_pos[:, 2] = 0 pos_ = np.round(move_crop_tracking(np.array(siam_box), deta_pos, (image.shape[1], image.shape[0]), self.rate)) bbox = pos_ result = bbox return result
def objective(trial): n_epochs = 20000 eval_frequency = 100 max_steps = 300 actor_net_dim = (128, 128) critic_net_dim = (128, 128) dsicriminator_net_dim = (128, 128) lr = trial.suggest_float("lr", 0, 0.01) gamma = trial.suggest_float("gamma", 0.9, 1) tau = trial.suggest_float("tau", 0.9, 1) grad_clip = 40 batch_size = 2**trial.suggest_int('batch_size', 4, 8) betas = (0.9, 0.999) entropy_weight = trial.suggest_float("entropy_weight", 0, 0.1) min_buffer_size = 2048 clip = trial.suggest_float("clip", 0, 0.5) ppo_updates = trial.suggest_int('ppo_updates', 1, 20) expert = trial.suggest_categorical("expert", ["1", "3", "10"]) value_coef = trial.suggest_float("value_coef", 0, 1) activation = "tanh" env_name = "LunarLander-v2" record = True if expert == "1": expert = 1 elif expert == "3": expert = 3 else: expert = 10 seed = 99 discriminator_updates = 1 expert, activation = initiate_run( env_name, actor_net_dim, critic_net_dim, dsicriminator_net_dim, lr, gamma, tau, grad_clip, batch_size, entropy_weight, min_buffer_size, clip, ppo_updates, discriminator_updates, expert, activation, value_coef, betas, max_steps, seed, "", record) env = Env(env_name) actor = Actor(env, actor_net_dim, activation) critic = Critic(env, critic_net_dim, activation) discriminator = Discriminator(env, dsicriminator_net_dim, lr, batch_size, activation, betas) agent = Agent(gamma, clip, actor, critic, lr, batch_size, grad_clip, entropy_weight, value_coef, betas) memory = PPOMemory(gamma, tau) args = [ min_buffer_size, eval_frequency, ppo_updates, discriminator_updates, expert, seed ] gail = GAIL(env, actor, critic, discriminator, agent, memory, *args) epoch_to_best = gail.update(n_epochs, max_steps, record) if record: neptune.log_metric('best_epoch', epoch_to_best) neptune.stop() sys.stdout.flush() return epoch_to_best
from modules.actor import Actor from modules.critic import Critic Actor = Actor() Critic = Critic()
class ACTTracker(Tracker): def __init__(self, net_path=None): super().init(name='ACTTracker', is_deterministic=True) np.random.seed(123) torch.manual_seed(456) torch.cuda.manual_seed(789) self.model = MDNet() self.actor = Actor() self.result = [] self.result_bb = [] self.success = 1 if opts['use_gpu']: self.model = self.model.cuda() self.actor = self.actor.cuda() self.model.set_learnable_params(opts['ft_layers']) self.criterion = BinaryLoss() self.init_optimizer = set_optimizer(self.model, opts['lr_init']) self.update_optimizer = set_optimizer(self.model, opts['lr_update']) self.detetion = 0 self.frame = 0 def init(self, image, init_bbox): self.rate = init_bbox[2] / init_bbox[3] self.target_bbox = np.array(init_bbox) self.init_bbox = np.array(init_bbox) self.result.append(self.target_bbox) self.result_bb.append(self.target_bbox) image = np.asarray(image) # Init model bbreg_examples = gen_samples( SampleGenerator('uniform', image.shape, 0.3, 1.5, 1.1), self.target_bbox, opts['n_bbreg'], opts['overlap_bbreg'], opts['scale_bbreg']) bbreg_feats = forward_samples(self.model, image, bbreg_examples) self.bbreg = BBRegressor(image.size) self.bbreg.train(bbreg_feats, bbreg_examples, self.target_bbox) pos_examples = gen_samples( SampleGenerator('gaussian', image.shape, 0.1, 1.2), self.target_bbox, opts['n_pos_init'], opts['overlap_pos_init']) neg_examples = np.concatenate([ gen_samples(SampleGenerator('uniform', image.shape, 1, 2, 1.1), self.target_bbox, opts['n_neg_init'] // 2, opts['overlap_neg_init']), gen_samples(SampleGenerator('whole', image.shape, 0, 1.2, 1.1), self.target_bbox, opts['n_neg_init'] // 2, opts['overlap_neg_init']) ]) neg_examples = np.random.permutation(neg_examples) pos_feats = forward_samples(self.model, image, pos_examples) neg_feats = forward_samples(self.model, image, neg_examples) train(self.model, self.criterion, self.init_optimizer, pos_feats, neg_feats, opts['maxiter_init']) self.deta_flag = init_actor(self.actor, image, self.target_bbox) self.init_generator = SampleGenerator('gaussian', image.shape, opts['trans_f'], 1, valid=False) self.sample_generator = SampleGenerator('gaussian', image.shape, opts['trans_f'], opts['scale_f'], valid=False) self.pos_generator = SampleGenerator('gaussian', image.shape, 0.1, 1.2) self.neg_generator = SampleGenerator('uniform', image.shape, 1.5, 1.2) self.pos_feats_all = [pos_feats[:opts['n_pos_update']]] self.neg_feats_all = [neg_feats[:opts['n_neg_update']]] pos_score = forward_samples(self.model, image, np.array(init_bbox).reshape([1, 4]), out_layer='fc6') self.img_learn = [image] self.pos_learn = [init_bbox] self.score_pos = [pos_score.cpu().numpy()[0][1]] self.frame_learn = [0] self.pf_frame = [] self.imageVar_first = cv2.Laplacian( crop_image_blur(np.array(image), self.target_bbox), cv2.CV_64F).var() def update(self, image): # image = loader(image.resize((225,225),Image.ANTIALIAS)).unsqueeze(0).cuda() self.frame += 1 update_lenth = 10 np_image = np.array(image) if self.imageVar_first > 200: imageVar = cv2.Laplacian( crop_image_blur(np_image, self.target_bbox), cv2.CV_64F).var() else: imageVar = 200 img_l = getbatch_actor(np_image, self.target_bbox.reshape([1, 4])) torch_image = loader(image.resize( (225, 225), Image.ANTIALIAS)).unsqueeze(0).cuda() deta_pos = self.actor(img_l, torch_image) deta_pos = deta_pos.data.clone().cpu().numpy() if self.deta_flag: deta_pos[:, 2] = 0 if deta_pos[:, 2] > 0.05 or deta_pos[:, 2] < -0.05: deta_pos[:, 2] = 0 if len(self.pf_frame) and self.frame == (self.pf_frame[-1] + 1): deta_pos[:, 2] = 0 pos_ = np.round( move_crop(self.target_bbox, deta_pos, (image.size[1], image.size[0]), self.rate)) r = forward_samples(self.model, image, np.array(pos_).reshape([1, 4]), out_layer='fc6') r = r.cpu().numpy() if r[0][1] > 0 and imageVar > 100: self.target_bbox = pos_ target_score = r[0][1] bbreg_bbox = pos_ success = 1 if True: fin_score = r[0][1] self.img_learn.append(image) self.pos_learn.append(self.target_bbox) self.score_pos.append(fin_score) self.frame_learn.append(self.frame) while len(self.img_learn) > update_lenth * 2: del self.img_learn[0] del self.pos_learn[0] del self.score_pos[0] del self.frame_learn[0] self.result[self.frame] = self.target_bbox self.result_bb[self.frame] = bbreg_bbox else: self.detetion += 1 if len(self.pf_frame) == 0: self.pf_frame = [self.frame] else: self.pf_frame.append(self.frame) if (len(self.frame_learn) == update_lenth * 2 and self.data_frame[-1] not in self.frame_learn) or self.data_frame[-1] == 0: for num in range(max(0, self.img_learn.__len__() - update_lenth), self.img_learn.__len__()): if self.frame_learn[num] not in self.data_frame: gt_ = self.pos_learn[num] image_ = self.img_learn[num] pos_examples = np.round( gen_samples(self.pos_generator, gt_, opts['n_pos_update'], opts['overlap_pos_update'])) neg_examples = np.round( gen_samples(self.neg_generator, gt_, opts['n_neg_update'], opts['overlap_neg_update'])) pos_feats_ = forward_samples(self.model, image_, pos_examples) neg_feats_ = forward_samples(self.model, image_, neg_examples) self.pos_feats_all.append(pos_feats_) self.neg_feats_all.append(neg_feats_) self.data_frame.append(self.frame_learn[num]) if len(self.pos_feats_all) > 10: del self.pos_feats_all[0] del self.neg_feats_all[0] del self.data_frame[0] else: pos_feats_ = self.pos_feats_all[self.data_frame.index( self.frame_learn[num])] neg_feats_ = self.neg_feats_all[self.data_frame.index( self.frame_learn[num])] if num == max(0, self.img_learn.__len__() - update_lenth): pos_feats = pos_feats_ neg_feats = neg_feats_ else: pos_feats = torch.cat([pos_feats, pos_feats_], 0) neg_feats = torch.cat([neg_feats, neg_feats_], 0) train(self.model, self.criterion, self.update_optimizer, pos_feats, neg_feats, opts['maxiter_update']) if success: self.sample_generator.set_trans_f(opts['trans_f']) else: self.sample_generator.set_trans_f(opts['trans_f_expand']) if imageVar < 100: samples = gen_samples(self.init_generator, self.target_bbox, opts['n_samples']) else: samples = gen_samples(self.sample_generator, self.target_bbox, opts['n_samples']) if i < 20 or ((self.init_bbox[2] * self.init_bbox[3]) > 1000 and (self.target_bbox[2] * self.target_bbox[3] / (self.init_bbox[2] * self.init_bbox[3]) > 2.5 or self.target_bbox[2] * self.target_bbox[3] / (self.init_bbox[2] * self.init_bbox[3]) < 0.4)): self.sample_generator.set_trans_f(opts['trans_f_expand']) samples_ = np.round( gen_samples( self.sample_generator, np.hstack([ self.target_bbox[0:2] + self.target_bbox[2:4] / 2 - self.init_bbox[2:4] / 2, self.init_bbox[2:4] ]), opts['n_samples'])) samples = np.vstack([samples, samples_]) sample_scores = forward_samples(self.model, image, samples, out_layer='fc6') top_scores, top_idx = sample_scores[:, 1].topk(5) top_idx = top_idx.cpu().numpy() target_score = top_scores.mean() self.target_bbox = samples[top_idx].mean(axis=0) success = target_score > opts['success_thr'] # Bbox regression if success: bbreg_samples = samples[top_idx] bbreg_feats = forward_samples(self.model, image, bbreg_samples) bbreg_samples = self.bbreg.predict(bbreg_feats, bbreg_samples) bbreg_bbox = bbreg_samples.mean(axis=0) self.img_learn.append(image) self.pos_learn.append(self.target_bbox) self.score_pos.append(self.target_score) self.frame_learn.append(i) while len(self.img_learn) > 2 * update_lenth: del self.img_learn[0] del self.pos_learn[0] del self.score_pos[0] del self.frame_learn[0] else: bbreg_bbox = self.target_bbox # Copy previous result at failure if not success: target_bbox = self.result[self.frame - 1] bbreg_bbox = self.result_bb[self.frame - 1] # Save result self.result[self.frame] = target_bbox self.result_bb[self.frame] = bbreg_bbox return self.target_bbox
def __init__(self): pyxel.init(WINDOW_WIDTH, WINDOW_HEIGHT, caption="8vana: Network Map") self.dir = os.path.dirname(os.path.abspath("__file__")) + "/" self.x = 0 discover = r"^discovery?$" attack = r"^attack$" self.re_discover = re.compile(discover, re.IGNORECASE) self.re_attack = re.compile(attack, re.IGNORECASE) self.font_scale = 1 self.font_position_x = 0 self.font_position_y = 0 print('width: ' + str(pyxel.width)) print('height: ' + str(pyxel.height)) self.log = [] self.log_path = self.dir + env.INPUT_LOG self.log_hash = "" self.log_lastupdate = 0 self.time_begin = 0 if (os.path.exists(self.log_path)): self.log_hash = digest.file_hash(self.log_path) with open(self.dir + env.INPUT_LOG, 'r') as json_file: self.log = json.load(json_file) print("[option]log_time: " + str(args.log_time)) if (args.log_time != False): self.time_begin = self.log[0][ "time"] - env.VISUALIZE_TIME_RANGE - env.VISUALIZE_TIME_WAIT else: now = datetime.datetime.now() epoch = int(now.timestamp()) self.time_begin = epoch print("[%s] %s, hash: %s\n" % (str(self.time_begin), "log update", self.log_hash)) print(self.dir) pyxel.image(0).load(0, 0, self.dir + "images/zei255-red.png") pyxel.image(1).load(0, 0, self.dir + "images/debian64.png") pyxel.image(2).load(0, 0, self.dir + "images/icons255.png") self.icon = {} self.actor = {} self.bg = {} self.title = {} self.title["ma"] = {"base_y": 177, "x": 177, "y": 177} self.title["block"] = {"base_y": 127, "x": 107, "y": 127} # ImageLoaderを利用したアイコンの登録 self.icon["world"] = Actor(env.NETMAP_BASE_X, 175) self.icon["world"].set_size(32, 32) self.icon["world"].imageload(self.dir + "images/internet1_32.png") self.icon["world"].imageload(self.dir + "images/internet2_32.png") self.icon["arrow"] = Actor() self.icon["arrow"].imageload(self.dir + "images/arrow1_up_8.png") self.icon["search"] = Actor() self.icon["search"].set_size(8, 8) self.icon["search"].imageload(self.dir + "images/search3_8.png") self.icon["search"].imageload(self.dir + "images/search2_8.png") self.icon["log1"] = Actor() self.icon["log1"].set_size(8, 8) self.icon["log1"].imageload(self.dir + "images/logarea1_8_16.png") self.icon["log2"] = Actor() self.icon["log2"].set_size(8, 8) self.icon["log2"].imageload(self.dir + "images/logarea2_8_16.png") self.icon["log3"] = Actor() self.icon["log3"].set_size(8, 8) self.icon["log3"].imageload(self.dir + "images/logarea3_8_16.png") self.icon["log4"] = Actor() self.icon["log4"].set_size(8, 8) self.icon["log4"].imageload(self.dir + "images/logarea4_8_16.png") self.icon["logvl"] = Actor() self.icon["logvl"].set_size(8, 8) self.icon["logvl"].imageload(self.dir + "images/logarea_vl_8_16.png") self.icon["logvr"] = Actor() self.icon["logvr"].set_size(8, 8) self.icon["logvr"].imageload(self.dir + "images/logarea_vr_8_16.png") self.icon["loght"] = Actor() self.icon["loght"].set_size(8, 8) self.icon["loght"].imageload(self.dir + "images/logarea_ht_8_16.png") self.icon["loghb"] = Actor() self.icon["loghb"].set_size(8, 8) self.icon["loghb"].imageload(self.dir + "images/logarea_hb_8_16.png") self.icon["fire"] = Actor() self.icon["fire"].imageload(self.dir + "images/fire1_8_16.png") self.icon["fire"].imageload(self.dir + "images/fire2_8_16.png") self.icon["mono"] = Actor() self.icon["mono"].imageload(self.dir + "images/mono/01.png") self.icon["mono"].imageload(self.dir + "images/mono/02.png") self.icon["mono"].imageload(self.dir + "images/mono/03.png") self.icon["mono"].imageload(self.dir + "images/mono/04.png") self.icon["mono"].imageload(self.dir + "images/mono/05.png") self.icon["mono"].imageload(self.dir + "images/mono/06.png") self.icon["mono"].imageload(self.dir + "images/mono/07.png") self.icon["mono"].imageload(self.dir + "images/mono/08.png") self.icon["mono"].imageload(self.dir + "images/mono/09.png") self.icon["mono"].imageload(self.dir + "images/mono/10.png") self.icon["mono"].imageload(self.dir + "images/mono/11.png") self.icon["unknown"] = Actor(96, 24) self.icon["unknown"].imageload(self.dir + "images/unknown64.png") self.icon["debian"] = Actor(96, 24) self.icon["debian"].imageload(self.dir + "images/debian64.png") self.icon["zeijyaku255"] = Actor() self.icon["zeijyaku255"].imageload(self.dir + "images/zei255-red.png") #self.fonts = fonts.Fonts() #self.fonts.imageload(self.dir + "images/alpha/num_alpha.png") #pixmap_index=0, width=8, height=8, count_x=1, count_y=1, margin=0, bg_color=env.WHITE, ignore_empty=True #self.fonts.imagesplit(0, 24, 44, 10, 5, 1) #pixmap_index=0, width=8, height=8, count_x=1, count_y=1, margin=0, bg_color=env.WHITE, ignore_empty=True #self.fonts.imageload(self.dir + "images/alpha/num_alpha2.png") #self.fonts.imagesplit(0, 5, 11, 10, 5, 1) #self.fonts_white = fonts.Fonts() #self.fonts_white.imageload(self.dir + "images/alpha/num_alpha2_white_pink.png", env.PINK) #self.fonts_white.imagesplit(0, 5, 11, 10, 5, 1) #self.fonts_white150 = fonts.Fonts() #self.fonts_white150.imageload(self.dir + "images/alpha/num_alpha2_white_pink_150.png", env.PINK) #self.fonts_white150.imagesplit(0, 8, 17, 10, 5, 1) self.view_object = 0 self.mouse_x = 0 self.mouse_y = 0 self.page = {"log": 0, "monolith": 0} self.monolith_nextline_x1 = 0 self.monolith_nextline_y1 = 0 self.monolith_nextline_x2 = 0 self.monolith_nextline_y2 = 0 self.view_object_scale = 0 self.view_object_scale_x = 0 self.view_object_scale_y = 0 self.view_object_scale_x2 = 0 self.view_object_scale_y2 = 0 self.frame_counter = 0 self.frame_appstart = 1 self.passed_seconds = 0 self.passed_seconds_real = 0 self.bgcolor = env.DARK_BLUE self.netmap_att = { "netmap": 0, "selected_obj": -1, "addr_obj": [], "list_obj_maps": [], "move_right": 0, "move_left": 0, "move_up": 0, "move_down": 0, "offset_x": 0, "offset_y": 0, } self.node_status = {} self.targets = [] self.targets_len = 0 self.bullets = [] self.bullet_init = 1 # there is include map address of display objects. x = env.NETMAP_BASE_X y = env.NETMAP_BASE_Y + env.NETMAP_OBJ_MARGIN_TOP for num in range(0, 256, 1): if (num != 0 and num % 16 == 0): y += env.NETMAP_OBJ_MARGIN_DOWN x = env.NETMAP_BASE_X x = (env.NETMAP_BASE_X + (num % 16) * env.NETMAP_OBJ_WIDTH) + \ env.NETMAP_OBJ_MARGIN_RIGHT line_color = env.BLACK bg_color = env.DARK_GRAY font_color = env.WHITE self.netmap_att["list_obj_maps"].append([ num, x, y, x + env.NETMAP_OBJ_WIDTH, y + env.NETMAP_OBJ_HEIGHT, line_color, bg_color, font_color ]) # draw mouse cursor pyxel.mouse(True) pyxel.run(self.update, self.draw)