def model(X, Y, learning_rate=0.01, num_iteration=15000, initialization='He', print_cost=False): grads = {} costs = [] layer_dims = [X.shape[0], 10, 5, 1] if initialization.lower() == 'zeros': params = initialize_parameters_zeros(layer_dims) elif initialization.lower() == 'random': params = initialize_parameters_random(layer_dims) elif initialization.lower() == 'he': params = initialize_parameters_he(layer_dims) for i in range(0, num_iteration): a3, cache = forward_propagation(X, params) cost = compute_loss(a3, Y) grads = backward_propagation(X, Y, cache) params = update_parameters(params, grads, learning_rate) if i % 1000 == 0 and print_cost: print('cost after {} iterations : {}'.format(i, cost)) costs.append(cost) plt.plot(costs) plt.ylabel('cost') plt.xlabel('iterations') plt.title('learning_rate =' + str(learning_rate)) plt.show() return params
def forward(self, data): data = prepare_data(data) full_p_states, p_mask, full_q_states, q_mask = self.encode(data) logits1, logits2, has_log = self.decode(full_p_states, p_mask, full_q_states, q_mask) loss = compute_loss(logits1, logits2, has_log, data['y1'], data['y2'], data['has_ans']) self.train_loss.update(loss.data.item()) del full_p_states, p_mask, full_q_states, q_mask, logits1, logits2, has_log return loss
for idx, decay_rate in enumerate(decay): np.random.seed( 7 ) # seed NumPy's random number generator for reproducibility of results # Initialize neural network nn = MLPClassifier(hidden_layer_sizes=(5, 2), random_state=7, max_iter=1, warm_start=True) nn.fit(X_train, y_train) # Initialize weights nn.coefs_, nn.intercepts_ = init_weights(X_train.shape[1], list(nn.hidden_layer_sizes)) loss_next = compute_loss(X_train, y_train, nn) T = T_init loss = [] start = time.time() for i in range(num_iters): # Save current parameters coefs_prev = nn.coefs_ intercepts_prev = nn.intercepts_ loss_prev = loss_next if debug: print('Iteration # %d' % i) print('Loss = ', loss_prev) # Update parameters
def optimize_ransac(self, num_iter, rot_loss_w, num_ransac_iter, inlier_thr, vis): """ :param num_iter: :param rot_loss_w: :param num_ransac_iter: :param inlier_thr: :param vis: :return: """ max_inliers = None for n in range(num_ransac_iter): # sample random num_points from data to optimize paramters print("\n training with {} data points".format( self.num_train_points)) train_indx = random.sample(range(self.num_points), self.num_train_points) train_trans_B_G = torch.stack( [self.trans_B_G[i] for i in train_indx], dim=0) train_rot_B_G = torch.stack([self.rot_B_G[i] for i in train_indx], dim=0) train_trans_C_A = torch.stack( [self.trans_C_A[i] for i in train_indx], dim=0) train_rot_C_A = torch.stack([self.rot_C_A[i] for i in train_indx], dim=0) test_trans_B_G = torch.stack([ self.trans_B_G[i] for i in range(self.num_points) if i not in train_indx ], dim=0) test_rot_B_G = torch.stack([ self.rot_B_G[i] for i in range(self.num_points) if i not in train_indx ], dim=0) test_trans_C_A = torch.stack([ self.trans_C_A[i] for i in range(self.num_points) if i not in train_indx ], dim=0) test_rot_C_A = torch.stack([ self.rot_C_A[i] for i in range(self.num_points) if i not in train_indx ], dim=0) # start with some random guess quat_B_C = torch.rand(1, 3).double().requires_grad_(True) trans_B_C = torch.rand(1, 3).double().requires_grad_(True) quat_G_A = torch.rand(1, 3).double().requires_grad_(True) trans_G_A = torch.rand(1, 3).double().requires_grad_(True) optimizer = optim.Adam([quat_B_C, trans_B_C, trans_G_A, quat_G_A], lr=0.1) criterion = torch.nn.MSELoss(reduction='none') best_train_loss, best_train_quat_B_C, best_train_trans_B_C, best_train_quat_G_A, best_train_trans_G_A = \ None, None, None, None, None ################### # optimize on the train set the B<-->C & G<-->A for it in range(num_iter): _, train_loss = compute_loss(train_trans_B_G, train_rot_B_G, train_trans_C_A, train_rot_C_A, trans_G_A, quat_G_A, trans_B_C, quat_B_C, criterion, rot_loss_w) optimizer.zero_grad() train_loss.backward() optimizer.step() if best_train_loss is None or train_loss.item( ) < best_train_loss: best_train_loss = train_loss.item() best_train_quat_B_C = quat_B_C.detach().numpy() best_train_trans_B_C = trans_B_C.detach().numpy() best_train_quat_G_A = quat_G_A.detach().numpy() best_train_trans_G_A = trans_G_A.detach().numpy() if it % 100 == 0: print("train_loss = {:05f}".format(train_loss.item())) ################### # find inliers with torch.no_grad(): test_loss, _ = compute_loss( test_trans_B_G, test_rot_B_G, test_trans_C_A, test_rot_C_A, torch.from_numpy(best_train_trans_G_A), torch.from_numpy(best_train_quat_G_A), torch.from_numpy(best_train_trans_B_C), torch.from_numpy(best_train_quat_B_C), criterion, rot_loss_w) # include all inliers in train set num_inliers = 0 for indx, l in enumerate(test_loss): if l.item() < inlier_thr: train_trans_B_G = torch.cat( (train_trans_B_G, test_trans_B_G[indx].unsqueeze_(0)), dim=0) train_rot_B_G = torch.cat( (train_rot_B_G, test_rot_B_G[indx].unsqueeze_(0)), dim=0) train_trans_C_A = torch.cat( (train_trans_C_A, test_trans_C_A[indx].unsqueeze_(0)), dim=0) train_rot_C_A = torch.cat( (train_rot_C_A, test_rot_C_A[indx].unsqueeze_(0)), dim=0) num_inliers += 1 print("num_inliers = {}".format(num_inliers)) # fine tune the params if num_inliers == 0: continue if max_inliers is None or num_inliers > max_inliers: max_inliers = num_inliers print("training with {} data points".format( train_trans_B_G.shape[0])) # train again best_loss, best_quat_B_C, best_trans_B_C, best_quat_G_A, best_trans_G_A = None, None, None, None, None for it in range(num_iter): # optimize paramters optimizer.zero_grad() _, train_loss = compute_loss(train_trans_B_G, train_rot_B_G, train_trans_C_A, train_rot_C_A, trans_G_A, quat_G_A, trans_B_C, quat_B_C, criterion, rot_loss_w) if best_loss is None or train_loss.item() < best_loss: best_loss = train_loss.item() best_quat_B_C = quat_B_C.detach().numpy() best_trans_B_C = trans_B_C[0].detach().numpy() best_trans_G_A = trans_G_A[0].detach().numpy() train_loss.backward() optimizer.step() if it % 100 == 0: print("train_loss = {:05f}".format(train_loss.item())) best_rot_B_C, best_quat_B_C = quat2mat(torch.from_numpy(best_quat_B_C)) best_rot_B_C, best_quat_B_C = best_rot_B_C[0].detach().numpy( ), best_quat_B_C[0].detach().numpy() print("\n for B<-->C ") cmd = "rosrun tf static_transform_publisher " + str(float(best_trans_B_C[0])) + ' ' + \ str(float(best_trans_B_C[1])) + ' ' + str(float(best_trans_B_C[2])) + ' ' + str(best_quat_B_C[1]) + ' ' \ + str(best_quat_B_C[2]) + ' ' + str(best_quat_B_C[3]) + ' ' + str(best_quat_B_C[0]) + ' ' + \ self.base_frame + ' '+ self.camera_frame + ' 10' print("Run Command") print(cmd) # plot the points for visualization if vis: trans_B_G_A = self.trans_B_G.numpy().reshape(-1, 3) + np.array([ np.matmul(self.rot_B_G[i].numpy(), best_trans_G_A.reshape(-1, 3).T).T for i in range(self.num_points) ]).reshape(-1, 3) trans_B_C_A = np.matmul(best_rot_B_C, self.trans_C_A.numpy().reshape( -1, 3).T).T + best_trans_B_C.reshape( -1, 3) ax = plt.axes(projection='3d') ax.scatter3D(trans_B_G_A[:, 0], trans_B_G_A[:, 1], trans_B_G_A[:, 2]) ax.scatter3D(trans_B_C_A[:, 0], trans_B_C_A[:, 1], trans_B_C_A[:, 2], color='red') scatter1_proxy = matplotlib.lines.Line2D([0], [0], linestyle="none", marker='o') scatter2_proxy = matplotlib.lines.Line2D([0], [0], linestyle="none", c='red', marker='o') ax.legend([scatter1_proxy, scatter2_proxy], ['Base to Ar from Gripper', 'Base to Ar from Camera'], numpoints=1) plt.show()
for i, (im, file_name) in enumerate(dataset_loader): im = im.cuda() # Prepare hints, mask, and get current classification data, target = util.get_colorization_data(im, opt, model, classifier) opt.target = opt.target if opt.targeted else target optimizer = torch.optim.Adam( [data['hints'].requires_grad_(), data['mask'].requires_grad_()], lr=opt.lr, betas=(0.9, 0.999)) prev_diff = 0 for itr in range(opt.num_iter): out_rgb, y = util.forward(model, classifier, opt, data) val, idx, labels = util.compute_class(opt, y) loss = util.compute_loss(opt, y, criterion) print(f'[{itr+1}/{opt.num_iter}] Loss: {loss:.3f} Labels: {labels}') optimizer.zero_grad() loss.backward() optimizer.step() print("%.5f" % (loss.item())) diff = val[0] - val[1] if opt.targeted: if idx[0] == opt.target and diff > threshold and ( diff - prev_diff).abs() < 1e-3: break else: if idx[0] != opt.target and diff > threshold and ( diff - prev_diff).abs() < 1e-3:
def optimize(self, num_iter, rot_loss_w, vis): # start with some random guess quat_B_C = torch.rand(1, 3).requires_grad_(True) trans_B_C = torch.rand(1, 3).requires_grad_(True) quat_G_A = torch.rand(1, 3).requires_grad_(True) trans_G_A = torch.rand(1, 3).requires_grad_(True) optimizer = optim.Adam([quat_B_C, trans_B_C, trans_G_A, quat_G_A], lr=0.1) criterion = torch.nn.MSELoss(reduction='none') best_quat_B_C, best_trans_B_C, best_loss = None, None, None ################### # optimize the B<-->C & G<-->A for it in range(num_iter): _, loss = compute_loss(self.trans_B_G, self.rot_B_G, self.trans_C_A, self.rot_C_A, trans_G_A, quat_G_A, trans_B_C, quat_B_C, criterion, rot_loss_w) optimizer.zero_grad() loss.backward() optimizer.step() if best_loss is None or best_loss > loss.item(): best_loss = loss.item() best_quat_B_C = quat_B_C.detach().numpy() best_trans_B_C = trans_B_C[0].detach().numpy() best_trans_G_A = trans_G_A[0].detach().numpy() print("iter = {:04d} loss = {}".format(it, loss.item())) best_rot_B_C, best_quat_B_C = quat2mat(torch.from_numpy(best_quat_B_C)) best_rot_B_C, best_quat_B_C = best_rot_B_C[0].detach().numpy( ), best_quat_B_C[0].detach().numpy() print("\n for B<-->C ") print(" org_rot = {} \n pred_rot = {}".format(self.gt_rot_B_C.numpy(), best_rot_B_C)) print(" org_trans = {} \n pred_trans = {}".format( self.gt_trans_B_C.numpy(), best_trans_B_C)) print("\n for G<-->A ") print("org_trans = {} \n pred_trans = {}".format( self.gt_trans_G_A.numpy(), best_trans_G_A)) # plot the points for visualization if vis: trans_B_G_A = self.trans_B_G.numpy().reshape(-1, 3) + np.array([ np.matmul(self.rot_B_G[i].numpy(), best_trans_G_A.reshape(-1, 3).T).T for i in range(self.num_points) ]).reshape(-1, 3) trans_B_C_A = np.matmul(best_rot_B_C, self.trans_C_A.numpy().reshape( -1, 3).T).T + best_trans_B_C.reshape( -1, 3) ax = plt.axes(projection='3d') ax.scatter3D(trans_B_G_A[:, 0], trans_B_G_A[:, 1], trans_B_G_A[:, 2]) ax.scatter3D(trans_B_C_A[:, 0], trans_B_C_A[:, 1], trans_B_C_A[:, 2], color='red') scatter1_proxy = matplotlib.lines.Line2D([0], [0], linestyle="none", marker='o') scatter2_proxy = matplotlib.lines.Line2D([0], [0], linestyle="none", c='red', marker='o') ax.legend([scatter1_proxy, scatter2_proxy], ['Base to Ar from Gripper', 'Base to Ar from Camera'], numpoints=1) plt.show()