def _get_Local_Loss(self, batch_y, h, batch_size, k): cos = nn.CosineSimilarity(dim=0, eps=1e-6) ###### calculate cosine similarity for imput space Matrix_imput = torch.empty(batch_size, batch_size, dtype=torch.float) for row_imput_1 in range(0, batch_size): for column_imput_1 in range(row_imput_1, batch_size): if column_imput_1 == row_imput_1: Matrix_imput.data[row_imput_1][ column_imput_1] = -10 # not calculate cosine for self else: Matrix_imput.data[row_imput_1][column_imput_1] = cos( batch_y.data[row_imput_1][:], batch_y.data[column_imput_1][:]) for row_imput_2 in range(0, batch_size): for column_imput_2 in range(0, row_imput_2): Matrix_imput.data[row_imput_2][ column_imput_2] = Matrix_imput.data[column_imput_2][ row_imput_2] Matrix_imput_top_k = torch.topk(Matrix_imput, k, dim=1, largest=True, sorted=True) ###### cacluate cosine similarity for embedding space Matrix_embedding = torch.empty(batch_size, batch_size, dtype=torch.float) for row_1 in range(0, batch_size): for column_1 in range(row_1, batch_size): if column_1 == row_1: Matrix_embedding.data[row_1][column_1] = -10 else: Matrix_embedding.data[row_1][column_1] = cos( h.data[row_1][:], h.data[column_1][:]) for row_2 in range(0, batch_size): for column_2 in range(0, row_2): Matrix_embedding.data[row_2][column_2] = Matrix_embedding.data[ column_2][row_2] Matrix_embedding_top_k = torch.topk(Matrix_embedding, k, dim=1, largest=True, sorted=True) ''' x = torch.ones(batch_size, k, dtype=torch.float) Mat_loss = torch.ones(batch_size, k, dtype=torch.float) Mat_loss = torch.sub(x,Matrix_sorted[0]) local_loss_result = torch.mean(Mat_loss) ''' Mat_loss = torch.abs( torch.sub(Matrix_imput_top_k[0], Matrix_embedding_top_k[0])) local_loss_result = torch.mean(Mat_loss) return local_loss_result
eta = 1 / (n + 10) elif args.sqrt: eta = 1 / (np.sqrt(n) + 10) elif args.log: eta = 1 / (np.log(n) + 10) else: #constant eta eta = 1e-3 optimizer.zero_grad() with torch.no_grad(): #normalise V is not super relevant here, but in the gcn model we need to call this before calling forward. #V = normalise(indices, V, size) perturbation = torch.FloatTensor(np.random.choice([-1, 1], nnz_sph)) * eta V0, V1 = torch.add(V, perturbation), torch.sub(V, perturbation) #normalized0, normalized1 = normalise(indices, V0, size), normalise(indices, V1, size) K, K0, K1 = KemenySpherical(indices, V, size), KemenySpherical( indices, V0, size), KemenySpherical(indices, V1, size) #try: # (K, nEc, nTc), (K0, _, _), (K1, _, _) = Kemeny(indices, V, size), Kemeny(indices, normalized0, size), Kemeny(indices, normalized1, size) #except np.linalg.LinAlgError as err: # print ('This should NOT happen!') # break loss = -gamma * torch.sum( torch.mul( V, torch.mul(torch.pow(perturbation, exponent=-1), (K0 - K1) / 2))) ###loss = sum_i V_{ii}### ''' loss = 0
def loss_mse(y_pred, y_true, y_mask): y_mse = torch.sub(y_pred, y_true) y_mse = torch.mul(y_mse, y_mse) y_mse = torch.mul(y_mse, y_mask) y_mse = torch.div(torch.sum(y_mse), torch.sum(y_mask)) return y_mse
def test_sub(x, y): c = torch.sub(torch.add(x, y), x) return c
out=better_tmp) parents = population[better] # create children picked_parents = torch.randint(0, num_parents, size=[4, num_children], out=picked_parents_tmp) crossover_sample = torch.gt(torch.rand( size=(num_children, args.dimension), device=dev, out=crossover_sample_tmp), args.CR, out=crossover_sample_bool) mutated = torch.sub(parents[picked_parents[1]], parents[picked_parents[2]], out=mutated_tmp) mutated = torch.mul(mutated, args.F, out=mutated) mutated = torch.add(mutated, parents[picked_parents[0]], out=mutated) mutated[crossover_sample] = parents[ picked_parents[3]][crossover_sample] mutated = torch.clamp(mutated, -10, 10, out=mutated) population = torch.cat([parents, mutated], dim=0, out=population) relative_progress[iter] = torch.mean(real_fitness, dim=0).item()
print(torch.cummin(x, dim=0)) print(torch.cumprod(x, dim=0)) print(torch.cumsum(x, dim=0)) """vec <> vec""" a = torch.tensor([9.7, float('nan'), 3.1, float('nan')]) b = torch.tensor([-2.2, 0.5, float('nan'), float('nan')]) c = torch.tensor([9.7, 1, 3.1, 4]) d = torch.tensor([1.7, 1.2, 3.1, 2]) print(torch.maximum(a, b)) print(torch.minimum(a, b)) print(torch.fmod(a, 2)) print(torch.dist(c, d, 1)) # p-norm print(torch.norm(c)) print(torch.div(c, d)) print(torch.true_divide(c, d)) # rounding_mode=None print(torch.sub(c, d, alpha=2.)) print(c.add(d)) print(torch.dot(c, d)) print(torch.sigmoid(c)) # print(torch.inner(c, d)) """flip""" x = torch.arange(4).view(2, 2) print(torch.flipud(x)) print(torch.fliplr(x)) # logical print("logical function:") print(torch.eq(c, d)) print(torch.ne(c, d)) print(torch.gt(c, d)) print(torch.logical_and(c, d))
def forward(self, label, scope, token, pos1, pos2, mask=None, train=True, bag_size=0): """ Args: label: (B), label of the bag scope: (B), scope for each bag token: (nsum, L), index of tokens pos1: (nsum, L), relative position to head entity pos2: (nsum, L), relative position to tail entity mask: (nsum, L), used for piece-wise CNN Return: logits, (B, N_class) """ if bag_size > 0: token = token.view(-1, token.size(-1)) pos1 = pos1.view(-1, pos1.size(-1)) pos2 = pos2.view(-1, pos2.size(-1)) if mask is not None: mask = mask.view(-1, mask.size(-1)) else: begin, end = scope[0][0], scope[-1][1] token = token[begin:end, :].view(-1, token.size(-1)) pos1 = pos1[begin:end, :].view(-1, pos1.size(-1)) pos2 = pos2[begin:end, :].view(-1, pos2.size(-1)) if mask is not None: mask = mask[begin:end, :].view(-1, mask.size(-1)) scope = torch.sub(scope, torch.zeros_like(scope).fill_(begin)) if mask is not None: rep = self.sentence_encoder(token, pos1, pos2, mask) # (nsum, H) else: rep = self.sentence_encoder(token, pos1, pos2) # (nsum, H) # Attention if train: if bag_size == 0: bag_rep = [] query = torch.zeros((rep.size(0))).long() if torch.cuda.is_available(): query = query.cuda() for i in range(len(scope)): query[scope[i][0]:scope[i][1]] = label[i] att_mat = self.fc.weight.data[query] # (nsum, H) att_score = (rep * att_mat).sum(-1) # (nsum) for i in range(len(scope)): bag_mat = rep[scope[i][0]:scope[i][1]] # (n, H) softmax_att_score = self.softmax( att_score[scope[i][0]:scope[i][1]]) # (n) bag_rep.append( (softmax_att_score.unsqueeze(-1) * bag_mat).sum(0)) # (n, 1) * (n, H) -> (n, H) -> (H) bag_rep = torch.stack(bag_rep, 0) # (B, H) else: batch_size = label.size(0) query = label.unsqueeze(1) # (B, 1) att_mat = self.fc.weight.data[query] # (B, 1, H) rep = rep.view(batch_size, bag_size, -1) att_score = (rep * att_mat).sum(-1) # (B, bag) softmax_att_score = self.softmax(att_score) # (B, bag) bag_rep = (softmax_att_score.unsqueeze(-1) * rep).sum( 1) # (B, bag, 1) * (B, bag, H) -> (B, bag, H) -> (B, H) bag_rep = self.drop(bag_rep) bag_logits = self.fc(bag_rep) # (B, N_class) else: if bag_size == 0: bag_logits = [] att_score = torch.matmul( rep, self.fc.weight.data.transpose( 0, 1)) # (nsum, H) * (H, N) -> (nsum, N) for i in range(len(scope)): bag_mat = rep[scope[i][0]:scope[i][1]] # (n, H) softmax_att_score = self.softmax( att_score[scope[i][0]:scope[i][1]].transpose( 0, 1)) # (N_class, (softmax)n) rep_for_each_rel = torch.matmul( softmax_att_score, bag_mat) # (N_class, n) * (n, H) -> (N, H) logit_for_each_rel = self.softmax(self.fc( rep_for_each_rel)) # ((each rel)N_class, (logit)N) logit_for_each_rel = logit_for_each_rel.diag() # (N_class) bag_logits.append(logit_for_each_rel) bag_logits = torch.stack(bag_logits, 0) # after **softmax** else: batch_size = rep.size(0) // bag_size att_score = torch.matmul( rep, self.fc.weight.data.transpose( 0, 1)) # (nsum, H) * (H, N) -> (nsum, N) att_score = att_score.view(batch_size, bag_size, -1) # (B, bag, N) rep = rep.view(batch_size, bag_size, -1) # (B, bag, H) softmax_att_score = self.softmax(att_score.transpose( 1, 2)) # (B, N_class, (softmax)bag) rep_for_each_rel = torch.matmul( softmax_att_score, rep) # (B, N_class, bag) * (B, bag, H) -> (B, N_class, H) bag_logits = self.softmax(self.fc(rep_for_each_rel)).diagonal( dim1=1, dim2=2) # (B, (each rel)N_class) return bag_logits
sign = torch.sign argmax = torch.argmax zeros_like = torch.zeros_like all = torch.all var = torch.var allclose = torch.allclose # ptp emulation: definition extracted from numpy ptp = lambda x, axis=None: torch.sub( torch.max(x, dim=axis)[0], torch.min(x, dim=axis)[0]) count_nonzero = torch.nonzero nonzero = torch.nonzero arange = torch.arange sin = torch.sin cos = torch.cos isscalar = np.isscalar std = torch.std
def fit(self, x, y): """ Regressor training function Arguments: - x {pd.DataFrame} -- Raw input array of shape (batch_size, input_size). - y {pd.DataFrame} -- Raw output array of shape (batch_size, 1). Returns: self {Regressor} -- Trained model. """ ####################################################################### # ** START OF YOUR CODE ** ####################################################################### #Preprocess input data X, Y = self._preprocessor(x, y = y, training = True) # Do not forget #Saves losses #[0,fold,epoch] -> for training losses #[1,fold,epoch] -> for validation losses rel_losses = np.zeros((2,self.nb_epoch)) abs_losses= np.zeros((2,self.nb_epoch)) #Randomly splits into 90% train and 10% val train_index,val_index = train_test_split(np.arange(X.shape[0]),train_size = 0.9) #Data to train x_train = X[train_index].detach() y_train = Y[train_index].detach() #Data to evaluate x_val = X[val_index].detach() y_val = Y[val_index].detach() #To do batching on training data torch_dataset_train = data.TensorDataset(x_train,y_train) data_loader_train = data.DataLoader(dataset = torch_dataset_train,batch_size = self.batch_size,shuffle = True) #We do early stopping if moving average validation loss over N episodes increases N = 20 cumsum, moving_averages = [0],[] old_average = np.inf for epoch in range(self.nb_epoch): #Set network to training mode self.network.train() #Batching in every epoch for step, (batch_x, batch_y) in enumerate(data_loader_train): #Forward prop prediction = self.network(batch_x) #Compute Loss loss = nn.MSELoss()(prediction,batch_y) #Backward prop self.optimizer.zero_grad() loss.backward() #Update parameters self.optimizer.step() #Evaluate after every epoch self.network.eval() #Compute total loss on training data prediction = self.network.forward(x_train).detach() #Absolute training loss train_loss_abs = nn.MSELoss()(prediction,y_train) abs_losses[0,epoch] = train_loss_abs #Relative training loss train_loss_rel = torch.sum(torch.div(torch.abs(torch.sub(prediction,y_train)),y_train)) / y_train.shape[0] rel_losses[0,epoch] = train_loss_rel #Compute total loss on validation data prediction = self.network.forward(x_val).detach() #Absolute validation loss val_loss_abs = nn.MSELoss()(prediction,y_val) abs_losses[1,epoch] = val_loss_abs #Relative validation loss val_loss_rel = torch.sum(torch.div(torch.abs(torch.sub(prediction,y_val)),y_val)) / y_val.shape[0] rel_losses[1,epoch] = val_loss_rel #Print losses every 20 folds if ((epoch % 20) == 0): print("Episode: {}\t - Training Loss: {}\t - Validation Loss: {}".format(epoch,train_loss_rel,val_loss_rel)) #Compute moving average of last N losses cumsum.append(cumsum[epoch - 1] + val_loss_abs.numpy()) if epoch >= N: moving_average = (cumsum[epoch] - cumsum[epoch - N]) / N moving_averages.append(moving_average) #If moving average rising, stop -> not optimal yet if (moving_average > old_average): print("Episode: {}\t - Training Loss: {}\t - Validation Loss: {}".format(epoch,train_loss_rel,val_loss_rel)) print("Average Validation Loss rising -> break") if self.early_stopping: break else: old_average = moving_average self.loss_abs = abs_losses self.loss_rel = rel_losses return self
def forward(self, input): # backup bias for bias correction feature if (not self.param_saved): if NndctOption.nndct_param_corr.value > 0: # backup orignal float parameters if self.quant_mode == 1: self.weight_bak = self.weight.detach().clone() if self.bias is not None: self.bias_bak = self.bias.detach().clone() # adjust bias if self.quant_mode == 2 and self.bias is not None: if self.node.name not in self.quantizer.bias_corr.keys(): NndctScreenLogger().error( f"Bias correction file in quantization result directory does not match current model." ) exit(2) self.bias.data = torch.sub( self.bias.data, torch.tensor(self.quantizer.bias_corr[self.node.name], device=self.bias.data.device)) self.param_saved = True # quantize parameters qweight = None qbias = None inplace = (NndctOption.nndct_quant_off.value or self.quantizer is not None and self.quantizer.inplace) if (not self.param_quantized): if inplace: _ = quantize_tensors([self.weight], self.node, tensor_names=[self.params_name[0]], tensor_type='param')[0] qweight = self.weight if self.bias is not None: _ = quantize_tensors([self.bias], self.node, tensor_names=[self.params_name[1]], tensor_type='param')[0] qbias = self.bias else: qweight = quantize_tensors([self.weight], self.node, tensor_names=[self.params_name[0]], tensor_type='param')[0] if self.bias is not None: qbias = quantize_tensors( [self.bias], self.node, tensor_names=[self.params_name[1]], tensor_type='param')[0] self.param_quantized = True else: qweight = self.weight qbias = self.bias # quantize input tensor qinput = quantize_tensors([input], self.node, tensor_type='input')[0] output = torch.nn.functional.conv1d(qinput, weight=qweight, bias=qbias, stride=self.stride, padding=self.padding, dilation=self.dilation, groups=self.groups) output = quantize_tensors([output], self.node)[0] # correct weights and bias in calibation if NndctOption.nndct_param_corr.value > 0: #rate = NndctOption.nndct_param_corr_rate.value # statistic of quantization error if (self.quant_mode == 1 and not self.stop): res_f = torch.nn.functional.conv1d(input, self.weight_bak, bias=self.bias_bak, stride=self.stride, padding=self.padding, dilation=self.dilation, groups=self.groups) error, rate, self.stop, self.efficency, self.deviation = eval_qnoise( output, res_f, self.efficency, self.deviation, self.rate, self.stop) if (not self.stop) and (self.bias is not None): error = error.mean(dim=[0, 1, 2]) self.bias.data = torch.sub(self.bias.data, error, alpha=rate) self.param_quantized = False return output
def __rsub__(self, other): return torch.sub(other, self)
def bias_corr(self): if self.bias is not None and self.bias_bak is not None: bias_err = torch.sub(self.bias_bak, self.bias.data) return bias_err.cpu().numpy().tolist() else: return None
def compute_loss(self, model, net_output, sample, reduce=True): lprobs, common_params, segment, extra_params = model.get_normalized_probs(net_output, log_probs=False) # flow_res = extra_params['mean_flow_res'] # first_input_feed = extra_params['first_input_feed'] internal_params = {} internal_params['common_params'] = common_params internal_params['segment_params'] = segment target = model.get_targets(sample, net_output).float() y_b = (target * self.all_stds) + self.all_means target_mask = y_b > 1e-6 y = y_b[target_mask] outs = (lprobs * self.all_stds) + self.all_means outputs = outs[target_mask] num_valid = target_mask.float().sum() wmape = 100. * torch.div( torch.div(torch.sum(torch.abs(torch.sub(outputs,y))),torch.sum(torch.abs(y))),num_valid) mape_loss = torch.mean((torch.abs(torch.div(torch.sub(outputs,y),(y + 1e-6)))).clamp(0,1)) # mape_loss = mape_loss / num_valid accuracy = 1. - mape_loss accuracy = accuracy.clamp(0,1) if num_valid>=1: target_loss = self.loss_fn(outputs, y) else: target_loss = 0.0 total_loss = target_loss #+ 100*input_feed_consistancy_loss #+ self.common_lambda*common_loss + self.segment_time_lambda*segment_time_loss + self.segment_lambda*segment_loss #+ volume_loss if str(total_loss.detach().item())=='nan': #from fairseq import pdb; pdb.set_trace(); target_loss = 0.0 * self.loss_fn(outputs, y) try: wandb.log( {'normal_loss':total_loss, 'mape_loss': mape_loss, 'target_loss': target_loss, 'accuracy': accuracy, 'wmape': wmape, 'w-accuracy': 100. - wmape, 'target_v0' : wandb.Histogram(y_b[:,:,1].detach()), 'target_q4' : wandb.Histogram(y_b[:,:,2].detach()), 'output_v0' : wandb.Histogram(outs[:,:,1].detach()), 'output_q4' : wandb.Histogram(outs[:,:,2].detach()), 'target_q0' : wandb.Histogram(y_b[:,:,0].detach()), 'target_v4' : wandb.Histogram(y_b[:,:,3].detach()), 'output_q0' : wandb.Histogram(outs[:,:,0].detach()), 'output_v4' : wandb.Histogram(outs[:,:,3].detach()), 'target_s2' : wandb.Histogram(y_b[:,:,5].detach()), 'target_r4' : wandb.Histogram(y_b[:,:,4].detach()), 'output_s2' : wandb.Histogram(outs[:,:,5].detach()), 'output_r4' : wandb.Histogram(outs[:,:,4].detach()), } ) except Exception as e: print(e) return total_loss, internal_params
import numpy as np np_arr = np.zeros((5, 5)) tensor2 = torch.from_numpy(np_arr) np_arr_back = tensor2.numpy() #======================================================# # Tensor Math and operations #======================================================# x1 = torch.tensor([1, 2, 3]) x2 = torch.tensor([9, 8, 7]) # Addition/Subtraction y1 = torch.add(x1, x2) y2 = torch.sub(x1, x2) print(y1, y2) # Division y3 = torch.true_divide(x1, x2) print(y3) # exponentiation y4 = x1.pow(2) #======================================================# # Inplace Operation (with _ at end) #======================================================# x1 = torch.tensor([1, 2, 3]) x2 = torch.tensor([9, 8, 7])
c = torch.rand(4, 4, 32, 32) print(torch.cat([a, c], dim=1).shape) # 在第1个维度上进行拼接 a1 = torch.rand(4, 3, 16, 32) a2 = torch.rand(4, 3, 16, 32) c = torch.stack([a1, a2], dim=2) print(c.shape) # 会创建一个新的维度 aa, bb = c.split([2, 1], dim=1) # 将3拆成2和1 print(aa.shape, bb.shape) aa, bb = c.chunk(2, dim=0) # 将0维度的数据平均拆成两份 print(aa.shape, bb.shape) # 运算符 a = torch.rand(3, 4) b = torch.rand(4) print(torch.equal(a - b, torch.sub(a, b))) print(torch.equal(a + b, torch.add(a, b))) print(torch.equal(a * b, torch.mul(a, b))) # matmul (a,b)*(c,d) = (ac,bd) 仅限于2d数据 a = torch.tensor([[2., 2.], [2., 2.]]) b = torch.ones(2, 2) print(torch.matmul(a, b)) print(torch.mm(a, b)) print(a @ b) a = torch.rand(4, 3, 28, 64) b = torch.rand(4, 3, 64, 32) print(torch.matmul(a, b).shape) # 只对最后两个维度进行扩展 b = torch.rand(4, 1, 64, 32) print(torch.matmul(a, b).shape) # 会先对第1个维度进行broadcast,将1扩展为3
def forward(self, x): return torch.sub(x, y)
def forward(self, predictions, targets): n = len(predictions) return torch.sum((torch.abs(torch.sub(targets, predictions))))
def __call__(self, x, x_recon, z, l_lambda, v): mse = reconstruction_loss(x, x_recon) mean = z.mean(dim=0) var = torch.norm((z - mean), dim=1).pow(2).mean() reg = torch.mul(torch.sub(var, v).abs(), l_lambda) return mse + reg
# Operations y = torch.rand(2, 2) x = torch.rand(2, 2) # elementwise addition z = x + y # torch.add(x,y) # in place addition, everythin with a trailing underscore is an inplace operation # i.e. it will modify the variable # y.add_(x) # substraction z = x - y z = torch.sub(x, y) # multiplication z = x * y z = torch.mul(x,y) # division z = x / y z = torch.div(x,y) # Slicing x = torch.rand(5,3) print(x) print(x[:, 0]) # all rows, column 0 print(x[1, :]) # row 1, all columns print(x[1,1]) # element at 1, 1
def bit_bottleneck_layer(x, name, firstepoch=FirstEpoch, Print_Act=RecordActivation): ''' This is the Bit Bottleneck layer. :param x: input tensor :param name: the name :param firstepoch: if ture, Bit Bottleneck only quantize the feature without compression with method of DoReFa-Net, or it will compress feature as well using the alpha vector we calculated :param print_feature: if ture, it will print the feature. :return: output ''' global RecordActivation global FirstEpoch if FirstEpoch: # print("Training without BIB in first epoch....") rank = x.ndim assert rank is not None # DoReFa quantization maxx = torch.abs(x) for i in range(1, rank): maxx, _ = torch.max(input=torch.abs(maxx), dim=i, keepdim=True, out=None) x = x / maxx t = torch.zeros_like(x) x_normal = x * 0.5 + t.uniform_(-0.5 / maximum, 0.5 / maximum) # Print the activation if RecordActivation: x_print = x_normal * maximum convert_x_1(name, x_print) back_round = x_normal infer_round = (x_normal * maximum).round() / maximum y_round = back_round + (infer_round - back_round).detach() y_round = y_round + 0.5 y_round = torch.clamp(y_round, 0.0, 1.0) y_round = y_round - 0.5 output = y_round * maxx * 2 else: # print("Training with BIB layer...") origin_beta = np.ones(shape=(bit_num, 1), dtype=np.float32) init_beta = origin_beta # Import the vector alpha which was saved as a .npz file alpha_array = np.load("./alpha_file/alpha_array.npz")["arr_0"] # Import different vector \alpha according to the names of layers if name == 'conv0': alpha = alpha_array[0].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv1_0_2': alpha = alpha_array[1].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv1_1_1': alpha = alpha_array[2].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv1_1_2': alpha = alpha_array[3].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_0_1': alpha = alpha_array[4].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_0_2': alpha = alpha_array[5].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_1_1': alpha = alpha_array[6].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_1_2': alpha = alpha_array[7].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_0_1': alpha = alpha_array[8].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_0_2': alpha = alpha_array[9].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_1_1': alpha = alpha_array[10].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_1_2': alpha = alpha_array[11].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) else: print('There is something wrong !') '''elif name == 'conv1_2_1': alpha = alpha_array[4].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv1_2_2': alpha = alpha_array[5].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv1_3_1': alpha = alpha_array[6].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv1_3_2': alpha = alpha_array[7].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv1_4_1': alpha = alpha_array[8].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv1_4_2': alpha = alpha_array[9].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv1_5_1': alpha = alpha_array[10].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv1_5_2': alpha = alpha_array[11].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv1_6_1': alpha = alpha_array[12].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv1_6_2': alpha = alpha_array[13].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv1_7_1': alpha = alpha_array[14].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv1_7_2': alpha = alpha_array[15].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_0_1': alpha = alpha_array[16].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_0_2': alpha = alpha_array[17].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_1_1': alpha = alpha_array[18].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_1_2': alpha = alpha_array[19].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_2_1': alpha = alpha_array[20].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_2_2': alpha = alpha_array[21].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_3_1': alpha = alpha_array[22].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_3_2': alpha = alpha_array[23].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_4_1': alpha = alpha_array[24].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_4_2': alpha = alpha_array[25].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_5_1': alpha = alpha_array[26].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_5_2': alpha = alpha_array[27].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_6_1': alpha = alpha_array[28].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_6_2': alpha = alpha_array[29].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_7_1': alpha = alpha_array[30].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv2_7_2': alpha = alpha_array[31].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_0_1': alpha = alpha_array[32].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_0_2': alpha = alpha_array[33].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_1_1': alpha = alpha_array[34].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_1_2': alpha = alpha_array[35].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_2_1': alpha = alpha_array[36].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_2_2': alpha = alpha_array[37].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_3_1': alpha = alpha_array[38].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_3_2': alpha = alpha_array[39].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_4_1': alpha = alpha_array[40].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_4_2': alpha = alpha_array[41].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_5_1': alpha = alpha_array[42].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_5_2': alpha = alpha_array[43].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_6_1': alpha = alpha_array[44].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_6_2': alpha = alpha_array[45].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_7_1': alpha = alpha_array[46].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha) elif name == 'conv3_7_2': alpha = alpha_array[47].reshape(bit_num, 1) init_beta = np.multiply(origin_beta, alpha)''' '''else: print('There is something wrong !') ''' init_beta = torch.reshape(torch.from_numpy(init_beta), shape=[bit_num, 1]) beta_back = torch.ones(size=[bit_num, 1], dtype=torch.float32) rank = x.ndim assert rank is not None # DoReFa quantization maxx = torch.abs(x) for i in range(1, rank): maxx, _ = torch.max(input=torch.abs(maxx), dim=i, keepdim=True, out=None) x = x / maxx t = torch.zeros_like(x) x_normal = x * 0.5 + t.uniform_(-0.5 / maximum, 0.5 / maximum) # Print the activation if RecordActivation: x_print = x_normal * maximum convert_x_1(name, x_print) round_back = x_normal * maximum round_infer = torch.round(x_normal * maximum) y = round_back + (round_infer - round_back).detach() y_sign = torch.sign(y) y_shape = y.shape y = torch.mul(y, y_sign) y = torch.reshape(y, [-1]) # print("y size:", y.size()) # Obtain the bits array of feature y_divisor = torch.ones_like(y) y_divisor = torch.mul(y_divisor, 2.) fdiv_back_0 = torch.div(y, 2.) fdiv_forward_0 = torch.floor_divide(y, y_divisor) y_fdiv2 = fdiv_back_0 + (fdiv_forward_0 - fdiv_back_0).detach() xbit0 = y + (torch.sub(y, torch.mul(y_fdiv2, 2.)) - y).detach() fdiv_back_1 = torch.div(y_fdiv2, 2.) fdiv_forward_1 = torch.floor_divide(y_fdiv2, y_divisor) y_fdiv4 = fdiv_back_1 + (fdiv_forward_1 - fdiv_back_1).detach() xbit1 = y + (torch.sub(y_fdiv2, torch.mul(y_fdiv4, 2.)) - y).detach() fdiv_back_2 = torch.div(y_fdiv4, 2.) fdiv_forward_2 = torch.floor_divide(y_fdiv4, y_divisor) y_fdiv8 = fdiv_back_2 + (fdiv_forward_2 - fdiv_back_2).detach() xbit2 = y + (torch.sub(y_fdiv4, torch.mul(y_fdiv8, 2.)) - y).detach() fdiv_back_3 = torch.div(y_fdiv8, 2.) fdiv_forward_3 = torch.floor_divide(y_fdiv8, y_divisor) y_fdiv16 = fdiv_back_3 + (fdiv_forward_3 - fdiv_back_3).detach() xbit3 = y + (torch.sub(y_fdiv8, torch.mul(y_fdiv16, 2.)) - y).detach() fdiv_back_4 = torch.div(y_fdiv16, 2.) fdiv_forward_4 = torch.floor_divide(y_fdiv16, y_divisor) y_fdiv32 = fdiv_back_4 + (fdiv_forward_4 - fdiv_back_4).detach() xbit4 = y + (torch.sub(y_fdiv16, torch.mul(y_fdiv32, 2.)) - y).detach() fdiv_back_5 = torch.div(y_fdiv32, 2.) fdiv_forward_5 = torch.floor_divide(y_fdiv32, y_divisor) y_fdiv64 = fdiv_back_5 + (fdiv_forward_5 - fdiv_back_5).detach() xbit5 = y + (torch.sub(y_fdiv32, torch.mul(y_fdiv64, 2.)) - y).detach() fdiv_back_6 = torch.div(y_fdiv64, 2.) fdiv_forward_6 = torch.floor_divide(y_fdiv64, y_divisor) y_fdiv128 = fdiv_back_6 + (fdiv_forward_6 - fdiv_back_6).detach() xbit6 = y + (torch.sub(y_fdiv64, torch.mul(y_fdiv128, 2.)) - y).detach() fdiv_back_7 = torch.div(y_fdiv128, 2.) fdiv_forward_7 = torch.floor_divide(y_fdiv128, y_divisor) y_fdiv256 = fdiv_back_7 + (fdiv_forward_7 - fdiv_back_7).detach() xbit7 = y + (torch.sub(y_fdiv128, torch.mul(y_fdiv256, 2.)) - y).detach() y_stack = torch.stack( [xbit7, xbit6, xbit5, xbit4, xbit3, xbit2, xbit1, xbit0], dim=1) # The bits arrays multiply the vector \alpha y_recov = torch.matmul(y_stack.cuda().float(), init_beta.cuda().float()) y_recov_back = torch.div( torch.matmul(y_stack.cuda().float(), beta_back.cuda().float()), float(bit_num)) y_output = y_recov_back + (y_recov - y_recov_back).detach() # print("y_output" + str(y_output)) y_output = torch.reshape(y_output, shape=y_shape) y_output = torch.mul(y_output, y_sign) output = y_output / maximum + 0.5 output = torch.clamp(output, 0.0, 1.0) output = output - 0.5 output = 2 * maxx * output # print("output" + str(output)) return output
def easy(x, y, z): aaa = torch.add(x, y) bbb = torch.sub(aaa, z) return bbb
def forward_kinematic_angles(self, images, gender_switch, synth_real_switch, CTRL_PNL, OUTPUT_EST_DICT, targets=None, is_training=True, betas=None, angles_gt=None, root_shift=None): reg_angles = CTRL_PNL['regr_angles'] filepath_prefix = CTRL_PNL['filepath_prefix'] OUTPUT_DICT = {} self.GPU = CTRL_PNL['GPU'] self.dtype = CTRL_PNL['dtype'] #print(torch.cuda.max_memory_allocated(), 'conv0', images.size()) if CTRL_PNL['first_pass'] == False: x = self.meshDepthLib.bounds #print blah #self.GPU = False #self.dtype = torch.FloatTensor else: if CTRL_PNL['GPU'] == True: self.GPU = True self.dtype = torch.cuda.FloatTensor else: self.GPU = False self.dtype = torch.FloatTensor if CTRL_PNL['depth_map_output'] == True: self.verts_list = "all" else: self.verts_list = [ 1325, 336, 1032, 4515, 1374, 4848, 1739, 5209, 1960, 5423 ] self.meshDepthLib = MeshDepthLib( loss_vector_type=self.loss_vector_type, filepath_prefix=filepath_prefix, batch_size=images.size(0), verts_list=self.verts_list) #print(torch.cuda.max_memory_allocated(), 'conv1') #for i in range(images.size()[1]): # print "channel:", i, " mean:", torch.mean(images[:, i, :, :]), " std:", torch.std(images[:, i, :, :]) #for i in range(0, 25): # print # for j in range(images.size()[1]): # print "channel:", j, " mean:", torch.mean(images[i, j, :, :]), " std:", torch.std(images[i, j, :, :]) # VisualizationLib().visualize_pressure_map(images[i, 0, :, :].cpu()*20., None, None, # images[i, 1, :, :].cpu()*20., None, None, # images[i, 2, :, :].cpu()*20., None, None, # images[i, 5, :, :].cpu()*20., None, None, # block=False) # time.sleep(0.5) if CTRL_PNL['all_tanh_activ'] == True: if CTRL_PNL['double_network_size'] == False: scores_cnn = self.CNN_packtanh(images) else: scores_cnn = self.CNN_packtanh_double(images) else: scores_cnn = self.CNN_pack1(images) scores_size = scores_cnn.size() # This combines the height, width, and filters into a single dimension scores_cnn = scores_cnn.view( images.size(0), scores_size[1] * scores_size[2] * scores_size[3]) # this output is N x 85: betas, root shift, angles if CTRL_PNL['double_network_size'] == False: scores = self.CNN_fc1(scores_cnn) else: scores = self.CNN_fc1_double(scores_cnn) # weight the outputs, which are already centered around 0. First make them uniformly smaller than the direct output, which is too large. if CTRL_PNL['adjust_ang_from_est'] == True: scores = torch.mul(scores.clone(), 0.01) else: scores = torch.mul(scores.clone(), 0.01) #normalize the output of the network based on the range of the parameters #if self.GPU == True: # output_norm = 10*[6.0] + [0.91, 1.98, 0.15] + 6*[2.0] + list(torch.abs(self.meshDepthLib.bounds.view(72,2)[3:, 1] - self.meshDepthLib.bounds.view(72,2)[3:, 0]).cpu().numpy()) #else: # output_norm = 10*[6.0] + [0.91, 1.98, 0.15] + 6*[2.0] + list(torch.abs(self.meshDepthLib.bounds.view(72, 2)[3:, 1] - self.meshDepthLib.bounds.view(72, 2)[3:, 0]).numpy()) if self.GPU == True: output_norm = 10 * [6.0] + [0.91, 1.98, 0.15] + list( torch.abs( self.meshDepthLib.bounds.view(72, 2)[:, 1] - self.meshDepthLib.bounds.view(72, 2)[:, 0]).cpu().numpy()) else: output_norm = 10 * [6.0] + [0.91, 1.98, 0.15] + list( torch.abs( self.meshDepthLib.bounds.view(72, 2)[:, 1] - self.meshDepthLib.bounds.view(72, 2)[:, 0]).numpy()) #print scores.size(), len(output_norm) #print output_norm for i in range(85): scores[:, i] = torch.mul(scores[:, i].clone(), output_norm[i]) #add a factor so the model starts close to the home position. Has nothing to do with weighting. if CTRL_PNL['lock_root'] == True: scores[:, 10] = torch.add(scores[:, 10].clone(), 0.6).detach() scores[:, 11] = torch.add(scores[:, 11].clone(), 1.2).detach() scores[:, 12] = torch.add(scores[:, 12].clone(), 0.1).detach() elif CTRL_PNL['adjust_ang_from_est'] == True: pass else: scores[:, 10] = torch.add(scores[:, 10].clone(), 0.6) scores[:, 11] = torch.add(scores[:, 11].clone(), 1.2) scores[:, 12] = torch.add(scores[:, 12].clone(), 0.1) #scores[:, 12] = torch.add(scores[:, 12].clone(), 0.06) if CTRL_PNL['full_body_rot'] == True: scores = scores.unsqueeze(0) scores = scores.unsqueeze(0) scores = F.pad(scores, (0, 3, 0, 0)) scores = scores.squeeze(0) scores = scores.squeeze(0) if CTRL_PNL['adjust_ang_from_est'] == True: scores[:, 13:19] = scores[:, 13:19].clone( ) + OUTPUT_EST_DICT['root_atan2'] scores[:, 22:91] = scores[:, 19:88].clone() scores[:, 19] = torch.atan2(scores[:, 16].clone(), scores[:, 13].clone()) #pitch x, y scores[:, 20] = torch.atan2(scores[:, 17].clone(), scores[:, 14].clone()) #roll x, y scores[:, 21] = torch.atan2(scores[:, 18].clone(), scores[:, 15].clone()) #yaw x, y OSA = 6 #output size adder else: OSA = 0 #print scores[0, 0:10] if CTRL_PNL['adjust_ang_from_est'] == True: scores[:, 0:10] = OUTPUT_EST_DICT['betas'] + scores[:, 0:10].clone() scores[:, 10:13] = OUTPUT_EST_DICT[ 'root_shift'] + scores[:, 10:13].clone() if CTRL_PNL['full_body_rot'] == True: scores[:, 22:91] = scores[:, 22:91].clone( ) + OUTPUT_EST_DICT['angles'][:, 3:72] else: scores[:, 13:85] = scores[:, 13:85].clone( ) + OUTPUT_EST_DICT['angles'] #scores[:, 13:85] = OUTPUT_EST_DICT['angles'] OUTPUT_DICT['batch_betas_est'] = scores[:, 0:10].clone().data if CTRL_PNL['full_body_rot'] == True: OUTPUT_DICT['batch_root_atan2_est'] = scores[:, 13:19].clone().data OUTPUT_DICT['batch_angles_est'] = scores[:, 13 + OSA:85 + OSA].clone().data OUTPUT_DICT['batch_root_xyz_est'] = scores[:, 10:13].clone().data if reg_angles == True: add_idx = 72 else: add_idx = 0 if CTRL_PNL['clip_betas'] == True: scores[:, 0:10] /= 3. scores[:, 0:10] = scores[:, 0:10].tanh() scores[:, 0:10] *= 3. #print self.meshDepthLib.bounds test_ground_truth = False #can only use True when the dataset is entirely synthetic AND when we use anglesDC #is_training = True if test_ground_truth == False or is_training == False: # make sure the estimated betas are reasonable. betas_est = scores[:, 0:10].clone( ) #.detach() #make sure to detach so the gradient flow of joints doesn't corrupt the betas root_shift_est = scores[:, 10:13].clone() #if CTRL_PNL['full_body_rot'] == True: # if CTRL_PNL['loss_vector_type'] == 'anglesEU': # self.meshDepthLib.bounds[0:3, 0] = torch.Tensor(np.array(-2*np.pi)) # self.meshDepthLib.bounds[0:3, 1] = torch.Tensor(np.array(2*np.pi)) # normalize for tan activation function scores[:, 13 + OSA:85 + OSA] -= torch.mean( self.meshDepthLib.bounds[0:72, 0:2], dim=1) scores[:, 13 + OSA:85 + OSA] *= (2. / torch.abs(self.meshDepthLib.bounds[0:72, 0] - self.meshDepthLib.bounds[0:72, 1])) scores[:, 13 + OSA:85 + OSA] = scores[:, 13 + OSA:85 + OSA].tanh() scores[:, 13 + OSA:85 + OSA] /= (2. / torch.abs(self.meshDepthLib.bounds[0:72, 0] - self.meshDepthLib.bounds[0:72, 1])) scores[:, 13 + OSA:85 + OSA] += torch.mean( self.meshDepthLib.bounds[0:72, 0:2], dim=1) #print scores[:, 13+OSA:85+OSA] if self.loss_vector_type == 'anglesDC': Rs_est = KinematicsLib().batch_rodrigues( scores[:, 13 + OSA:85 + OSA].view(-1, 24, 3).clone()).view( -1, 24, 3, 3) elif self.loss_vector_type == 'anglesEU': Rs_est = KinematicsLib().batch_euler_to_R( scores[:, 13 + OSA:85 + OSA].view(-1, 24, 3).clone(), self.meshDepthLib.zeros_cartesian, self.meshDepthLib.ones_cartesian).view(-1, 24, 3, 3) else: #print betas[13, :], 'betas' betas_est = betas scores[:, 0:10] = betas.clone() scores[:, 13 + OSA:85 + OSA] = angles_gt.clone() root_shift_est = root_shift if self.loss_vector_type == 'anglesDC': #normalize for tan activation function #scores[:, 13+OSA:85+OSA] -= torch.mean(self.meshDepthLib.bounds[0:72,0:2], dim = 1) #scores[:, 13+OSA:85+OSA] *= (2. / torch.abs(self.meshDepthLib.bounds[0:72, 0] - self.meshDepthLib.bounds[0:72, 1])) #scores[:, 13+OSA:85+OSA] = scores[:, 13+OSA:85+OSA].tanh() #scores[:, 13+OSA:85+OSA] /= (2. / torch.abs(self.meshDepthLib.bounds[0:72, 0] - self.meshDepthLib.bounds[0:72, 1])) #scores[:, 13+OSA:85+OSA] += torch.mean(self.meshDepthLib.bounds[0:72,0:2], dim = 1) Rs_est = KinematicsLib().batch_rodrigues( scores[:, 13 + OSA:85 + OSA].view(-1, 24, 3).clone()).view( -1, 24, 3, 3) #print Rs_est[0, :] OUTPUT_DICT['batch_betas_est_post_clip'] = scores[:, 0:10].clone().data if self.loss_vector_type == 'anglesEU': OUTPUT_DICT['batch_angles_est_post_clip'] = KinematicsLib( ).batch_dir_cos_angles_from_euler_angles( scores[:, 13 + OSA:85 + OSA].view(-1, 24, 3).clone(), self.meshDepthLib.zeros_cartesian, self.meshDepthLib.ones_cartesian) elif self.loss_vector_type == 'anglesDC': OUTPUT_DICT['batch_angles_est_post_clip'] = scores[:, 13 + OSA:85 + OSA].view( -1, 24, 3).clone() OUTPUT_DICT['batch_root_xyz_est_post_clip'] = scores[:, 10:13].clone( ).data gender_switch = gender_switch.unsqueeze(1) current_batch_size = gender_switch.size()[0] if CTRL_PNL['depth_map_output'] == True: # break things up into sub batches and pass through the mesh num_normal_sub_batches = current_batch_size / self.meshDepthLib.N if current_batch_size % self.meshDepthLib.N != 0: sub_batch_incr_list = num_normal_sub_batches * [ self.meshDepthLib.N ] + [current_batch_size % self.meshDepthLib.N] else: sub_batch_incr_list = num_normal_sub_batches * [ self.meshDepthLib.N ] start_incr, end_incr = 0, 0 #print len(sub_batch_incr_list), current_batch_size for sub_batch_incr in sub_batch_incr_list: end_incr += sub_batch_incr verts_sub, J_est_sub, targets_est_sub = self.meshDepthLib.compute_tensor_mesh( gender_switch, betas_est, Rs_est, root_shift_est, start_incr, end_incr, self.GPU) if start_incr == 0: verts = verts_sub.clone() J_est = J_est_sub.clone() targets_est = targets_est_sub.clone() else: verts = torch.cat((verts, verts_sub), dim=0) J_est = torch.cat((J_est, J_est_sub), dim=0) targets_est = torch.cat((targets_est, targets_est_sub), dim=0) start_incr += sub_batch_incr bed_ang_idx = -1 if CTRL_PNL['incl_ht_wt_channels'] == True: bed_ang_idx -= 2 bed_angle_batch = torch.mean(images[:, bed_ang_idx, 1:3, 0], dim=1) OUTPUT_DICT['batch_mdm_est'], OUTPUT_DICT[ 'batch_cm_est'] = self.meshDepthLib.compute_depth_contact_planes( verts, bed_angle_batch, CTRL_PNL['mesh_bottom_dist']) OUTPUT_DICT['batch_mdm_est'] = OUTPUT_DICT['batch_mdm_est'].type( self.dtype) OUTPUT_DICT['batch_cm_est'] = OUTPUT_DICT['batch_cm_est'].type( self.dtype) verts_red = torch.stack([ verts[:, 1325, :], verts[:, 336, :], # head verts[:, 1032, :], # l knee verts[:, 4515, :], # r knee verts[:, 1374, :], # l ankle verts[:, 4848, :], # r ankle verts[:, 1739, :], # l elbow verts[:, 5209, :], # r elbow verts[:, 1960, :], # l wrist verts[:, 5423, :] ]).permute(1, 0, 2) # r wrist verts_offset = verts_red.clone().detach().cpu() verts_offset = torch.Tensor(verts_offset.numpy()).type(self.dtype) else: shapedirs = torch.bmm(gender_switch, self.meshDepthLib.shapedirs_repeat[0:current_batch_size, :, :])\ .view(current_batch_size, self.meshDepthLib.B, self.meshDepthLib.R*self.meshDepthLib.D) betas_shapedirs_mult = torch.bmm(betas_est.unsqueeze(1), shapedirs)\ .squeeze(1)\ .view(current_batch_size, self.meshDepthLib.R, self.meshDepthLib.D) v_template = torch.bmm(gender_switch, self.meshDepthLib.v_template_repeat[0:current_batch_size, :, :])\ .view(current_batch_size, self.meshDepthLib.R, self.meshDepthLib.D) v_shaped = betas_shapedirs_mult + v_template J_regressor_repeat = torch.bmm(gender_switch, self.meshDepthLib.J_regressor_repeat[0:current_batch_size, :, :])\ .view(current_batch_size, self.meshDepthLib.R, 24) Jx = torch.bmm(v_shaped[:, :, 0].unsqueeze(1), J_regressor_repeat).squeeze(1) Jy = torch.bmm(v_shaped[:, :, 1].unsqueeze(1), J_regressor_repeat).squeeze(1) Jz = torch.bmm(v_shaped[:, :, 2].unsqueeze(1), J_regressor_repeat).squeeze(1) J_est = torch.stack( [Jx, Jy, Jz], dim=2 ) # these are the joint locations with home pose (pose is 0 degree on all angles) #J_est = J_est - J_est[:, 0:1, :] + root_shift_est.unsqueeze(1) targets_est, A_est = KinematicsLib( ).batch_global_rigid_transformation(Rs_est, J_est, self.meshDepthLib.parents, self.GPU, rotate_base=False) targets_est = targets_est - J_est[:, 0: 1, :] + root_shift_est.unsqueeze( 1) # assemble a reduced form of the transformed mesh v_shaped_red = torch.stack([ v_shaped[:, self.verts_list[0], :], v_shaped[:, self.verts_list[1], :], # head v_shaped[:, self.verts_list[2], :], # l knee v_shaped[:, self.verts_list[3], :], # r knee v_shaped[:, self.verts_list[4], :], # l ankle v_shaped[:, self.verts_list[5], :], # r ankle v_shaped[:, self.verts_list[6], :], # l elbow v_shaped[:, self.verts_list[7], :], # r elbow v_shaped[:, self.verts_list[8], :], # l wrist v_shaped[:, self.verts_list[9], :] ]).permute(1, 0, 2) # r wrist pose_feature = (Rs_est[:, 1:, :, :]).sub( 1.0, torch.eye(3).type(self.dtype)).view(-1, 207) posedirs_repeat = torch.bmm(gender_switch, self.meshDepthLib.posedirs_repeat[0:current_batch_size, :, :]) \ .view(current_batch_size, 10 * self.meshDepthLib.D, 207) \ .permute(0, 2, 1) v_posed = torch.bmm(pose_feature.unsqueeze(1), posedirs_repeat).view(-1, 10, self.meshDepthLib.D) v_posed = v_posed.clone() + v_shaped_red weights_repeat = torch.bmm(gender_switch, self.meshDepthLib.weights_repeat[0:current_batch_size, :, :]) \ .squeeze(1) \ .view(current_batch_size, 10, 24) T = torch.bmm(weights_repeat, A_est.view(current_batch_size, 24, 16)).view(current_batch_size, -1, 4, 4) v_posed_homo = torch.cat([ v_posed, torch.ones(current_batch_size, v_posed.shape[1], 1).type( self.dtype) ], dim=2) v_homo = torch.matmul(T, torch.unsqueeze(v_posed_homo, -1)) verts = v_homo[:, :, :3, 0] - J_est[:, 0:1, :] + root_shift_est.unsqueeze(1) verts_offset = torch.Tensor( verts.clone().detach().cpu().numpy()).type(self.dtype) OUTPUT_DICT['batch_mdm_est'] = None OUTPUT_DICT['batch_cm_est'] = None #print verts[0:10], 'VERTS EST INIT' #if CTRL_PNL['dropout'] == True: OUTPUT_DICT['verts'] = verts.clone().detach().cpu().numpy() targets_est_detached = torch.Tensor( targets_est.clone().detach().cpu().numpy()).type(self.dtype) synth_joint_addressed = [3, 15, 4, 5, 7, 8, 18, 19, 20, 21] for real_joint in range(10): verts_offset[:, real_joint, :] = verts_offset[:, real_joint, :] - targets_est_detached[:, synth_joint_addressed[ real_joint], :] #here we need to the ground truth to make it a surface point for the mocap markers #if is_training == True: synth_real_switch_repeated = synth_real_switch.unsqueeze(1).repeat( 1, 3) for real_joint in range(10): targets_est[:, synth_joint_addressed[real_joint], :] = synth_real_switch_repeated * targets_est[:, synth_joint_addressed[real_joint], :].clone() \ + torch.add(-synth_real_switch_repeated, 1) * (targets_est[:, synth_joint_addressed[real_joint], :].clone() + verts_offset[:, real_joint, :]) targets_est = targets_est.contiguous().view(-1, 72) OUTPUT_DICT[ 'batch_targets_est'] = targets_est.data * 1000. #after it comes out of the forward kinematics scores = scores.unsqueeze(0) scores = scores.unsqueeze(0) scores = F.pad(scores, (0, 100 + add_idx, 0, 0)) scores = scores.squeeze(0) scores = scores.squeeze(0) #tweak this to change the lengths vector scores[:, 34 + add_idx + OSA:106 + add_idx + OSA] = torch.mul( targets_est[:, 0:72], 1.) scores[:, 0:10] = torch.mul(synth_real_switch.unsqueeze(1), torch.sub(scores[:, 0:10], betas)) #*.2 if CTRL_PNL['full_body_rot'] == True: scores[:, 10:16] = scores[:, 13:19].clone() if self.loss_vector_type == 'anglesEU': scores[:, 10:13] = scores[:, 10:13].clone() - torch.cos( KinematicsLib().batch_euler_angles_from_dir_cos_angles( angles_gt[:, 0:3].view( -1, 1, 3).clone()).contiguous().view(-1, 3)) scores[:, 13:16] = scores[:, 13:16].clone() - torch.sin( KinematicsLib().batch_euler_angles_from_dir_cos_angles( angles_gt[:, 0:3].view( -1, 1, 3).clone()).contiguous().view(-1, 3)) elif self.loss_vector_type == 'anglesDC': scores[:, 10:13] = scores[:, 10:13].clone() - torch.cos( angles_gt[:, 0:3].clone()) scores[:, 13:16] = scores[:, 13:16].clone() - torch.sin( angles_gt[:, 0:3].clone()) #print euler_root_rot_gt[0, :], 'body rot angles gt' #compare the output angles to the target values if reg_angles == True: if self.loss_vector_type == 'anglesDC': scores[:, 34 + OSA:106 + OSA] = angles_gt.clone().view( -1, 72) - scores[:, 13 + OSA:85 + OSA] scores[:, 34 + OSA:106 + OSA] = torch.mul( synth_real_switch.unsqueeze(1), torch.sub(scores[:, 34 + OSA:106 + OSA], angles_gt.clone().view(-1, 72))) elif self.loss_vector_type == 'anglesEU': scores[:, 34 + OSA:106 + OSA] = KinematicsLib( ).batch_euler_angles_from_dir_cos_angles( angles_gt.view(-1, 24, 3).clone()).contiguous().view( -1, 72) - scores[:, 13 + OSA:85 + OSA] scores[:, 34 + OSA:106 + OSA] = torch.mul( synth_real_switch.unsqueeze(1), scores[:, 34 + OSA:106 + OSA].clone()) #compare the output joints to the target values scores[:, 34 + add_idx + OSA:106 + add_idx + OSA] = targets[:, 0:72] / 1000 - scores[:, 34 + add_idx + OSA:106 + add_idx + OSA] scores[:, 106 + add_idx + OSA:178 + add_idx + OSA] = ( (scores[:, 34 + add_idx + OSA:106 + add_idx + OSA].clone()) + 0.0000001).pow(2) for joint_num in range(24): #print scores[:, 10+joint_num].size(), 'score size' #print synth_real_switch.size(), 'switch size' if joint_num in [ 0, 1, 2, 6, 9, 10, 11, 12, 13, 14, 16, 17, 22, 23 ]: #torso is 3 but forget training it scores[:, 10 + joint_num + OSA] = torch.mul( synth_real_switch, (scores[:, 106 + add_idx + joint_num * 3 + OSA] + scores[:, 107 + add_idx + joint_num * 3 + OSA] + scores[:, 108 + add_idx + joint_num * 3 + OSA]).sqrt()) else: scores[:, 10 + joint_num + OSA] = ( scores[:, 106 + add_idx + joint_num * 3 + OSA] + scores[:, 107 + add_idx + joint_num * 3 + OSA] + scores[:, 108 + add_idx + joint_num * 3 + OSA]).sqrt() #print scores[:, 10+joint_num], 'score size' #print synth_real_switch, 'switch size' scores = scores.unsqueeze(0) scores = scores.unsqueeze(0) scores = F.pad(scores, (0, -151, 0, 0)) scores = scores.squeeze(0) scores = scores.squeeze(0) #here multiply by 24/10 when you are regressing to real data so it balances with the synthetic data #scores = torch.mul(torch.add(1.0, torch.mul(1.4, torch.sub(1, synth_real_switch))).unsqueeze(1), scores) #scores = torch.mul(torch.add(1.0, torch.mul(1.937984, torch.sub(1, synth_real_switch))).unsqueeze(1), scores) #data bag ratio. if you duplicate things get rid of this #scores = torch.mul(torch.mul(2.4, torch.sub(1, synth_real_switch)).unsqueeze(1), scores) # here multiply by 5 when you are regressing to real data because there is only 1/5 the amount of it #scores = torch.mul(torch.mul(5.0, torch.sub(1, synth_real_switch)).unsqueeze(1), scores) #print scores[0, :] #print scores[7, :] scores[:, 0:10] = torch.mul(scores[:, 0:10].clone(), ( 1 / 1.728158146914805)) #1.7312621950698526)) #weight the betas by std if CTRL_PNL['full_body_rot'] == True: scores[:, 10:16] = torch.mul( scores[:, 10:16].clone(), (1 / 0.3684988513298487) ) #0.2130542427733348)*np.pi) #weight the body rotation by the std scores[:, 10 + OSA:34 + OSA] = torch.mul( scores[:, 10 + OSA:34 + OSA].clone(), (1 / 0.1752780723422608 )) #0.1282715100608753)) #weight the 24 joints by std if reg_angles == True: scores[:, 34 + OSA:106 + OSA] = torch.mul( scores[:, 34 + OSA:106 + OSA].clone(), (1 / 0.29641429463719227) ) #0.2130542427733348)) #weight the angles by how many there are #scores[:, 0:10] = torch.mul(scores[:, 0:10].clone(), (1./10)) #weight the betas by how many betas there are #scores[:, 10:34] = torch.mul(scores[:, 10:34].clone(), (1./24)) #weight the joints by how many there are #if reg_angles == True: scores[:, 34:106] = torch.mul(scores[:, 34:106].clone(), (1./72)) #weight the angles by how many there are return scores, OUTPUT_DICT
def batch_sub(data1, mask1, dims1, data2, mask2, dims2, alpha_): alpha = float(alpha_) data = torch.sub(data1, data2, alpha=alpha) mask = mask1 * mask2 dims = dims1.__or__(dims2) return data, mask, dims
def l2_loss(prediction, y): loss = torch.sub(y, prediction)**2 loss = torch.mean(loss) return loss
def fn1(members: torch.Tensor): dot = torch.sub(members, xopt[None, :], out=dot_tmp) value = torch.sum(torch.mul(dot, dot, out=dot), dim=1, out=sum_tmp) return (torch.add(value, fopt, out=fitness_tmp), value)
def forward(self, input_data, embedding_list=None): input_data = input_data.t().expand(self.layers.size(0), *input_data.t().size()) input_data = input_data.permute(2, 0, 1) comp = self.layers.mul(input_data) if not self.vectorized: comp = comp.sum(dim=2).unsqueeze(-1) comp = comp.sub( self.comparators.expand(input_data.size(0), *self.comparators.size())) comp = comp.mul(self.alpha) sig_vals = self.sig(comp) if self.vectorized: s_temp_main = self.selector.expand(input_data.size(0), *self.selector.size()) sig_vals = sig_vals.mul(s_temp_main) sig_vals = sig_vals.sum(dim=2) sig_vals = sig_vals.view(input_data.size(0), -1) one_minus_sig = torch.ones(sig_vals.size()).to(self.device) one_minus_sig = torch.sub(one_minus_sig, sig_vals) if input_data.size(0) > 1: left_path_probs = self.left_path_sigs.t() right_path_probs = self.right_path_sigs.t() left_path_probs = left_path_probs.expand( input_data.size(0), * left_path_probs.size()) * sig_vals.unsqueeze(1) right_path_probs = right_path_probs.expand( input_data.size(0), * right_path_probs.size()) * one_minus_sig.unsqueeze(1) left_path_probs = left_path_probs.permute(0, 2, 1) right_path_probs = right_path_probs.permute(0, 2, 1) # We don't want 0s to ruin leaf probabilities, so replace them with 1s so they don't affect the product left_filler = torch.zeros(self.left_path_sigs.size()).to( self.device) left_filler[self.left_path_sigs == 0] = 1 right_filler = torch.zeros(self.right_path_sigs.size()).to( self.device) right_filler[self.right_path_sigs == 0] = 1 left_path_probs = left_path_probs.add(left_filler) right_path_probs = right_path_probs.add(right_filler) probs = torch.cat((left_path_probs, right_path_probs), dim=1) probs = probs.prod(dim=1) actions = probs.mm(self.action_probs) else: left_path_probs = self.left_path_sigs * sig_vals.t() right_path_probs = self.right_path_sigs * one_minus_sig.t() # We don't want 0s to ruin leaf probabilities, so replace them with 1s so they don't affect the product left_filler = torch.zeros(self.left_path_sigs.size(), dtype=torch.float).to(self.device) left_filler[self.left_path_sigs == 0] = 1 right_filler = torch.zeros(self.right_path_sigs.size(), dtype=torch.float).to(self.device) right_filler[self.right_path_sigs == 0] = 1 left_path_probs = torch.add(left_path_probs, left_filler) right_path_probs = torch.add(right_path_probs, right_filler) probs = torch.cat((left_path_probs, right_path_probs), dim=0) probs = probs.prod(dim=0) actions = (self.action_probs * probs.view(1, -1).t()).sum(dim=0) if not self.is_value: return self.softmax(actions) else: return actions
real_slabel = Variable(torch.ones( batch_size * 48, 1)).cuda() # define the real sample label fake_slabel = Variable(torch.zeros( batch_size * 48, 1)).cuda() # define the fake sample label real_sout, smetric_real = ds(sdisc_real_aqi, sdisc_real_meo, adj, adj_norm) fake_sout, smetric_fake = ds(sdisc_fake_aqi, sdisc_fake_meo, adj, adj_norm) ds_loss_sreal = criterion(real_sout, real_slabel) ds_loss_sfake = criterion(fake_sout, fake_slabel) ds_loss = ds_loss_sreal + ds_loss_sfake # m_weight = torch.mul(mmetric_real.detach(), mmetric_fake.detach()) # m_weight = torch.sum(m_weight, 1) m_weight = torch.sub(mmetric_real.detach(), mmetric_fake.detach()) m_weight = torch.mul(m_weight, m_weight) m_weight = torch.sum(m_weight, 1) m_weight = torch.mean(m_weight) # t_weight = torch.mul(tmetric_real.detach(), tmetric_fake.detach()) # t_weight = torch.sum(t_weight, 2) t_weight = torch.sub(tmetric_real.detach(), tmetric_fake.detach()) t_weight = torch.mul(t_weight, t_weight) t_weight = torch.sum(t_weight, 2) t_weight = torch.mean(t_weight) # s_weight = torch.mul(smetric_real.detach(), smetric_fake.detach()) # s_weight = torch.sum(s_weight, 2) s_weight = torch.sub(smetric_real.detach(), smetric_fake.detach()) s_weight = torch.mul(s_weight, s_weight) s_weight = torch.sum(s_weight, 1) s_weight = torch.mean(s_weight)
def get_loss(self, means, stds, samples): diff = torch.sub(samples, means) loss = torch.mean(torch.div(torch.pow(diff, 2), torch.pow(stds, 2))) \ + 2 * torch.mean(torch.log(stds)) return loss
def forward(self, output: torch.Tensor, target: torch.Tensor): if len(output) > 1: return torch.mean(torch.abs(torch.sub(target, output) / target)) + \ self.variance_penalty * torch.std(torch.sub(target, output)) else: return torch.mean(torch.abs(torch.sub(target, output) / target))
trainer.train() plt.figure() plt.plot(np.arange(len(trainer.train_losses)), trainer.train_losses, label='Training loss') plt.plot(np.arange(len(trainer.train_losses)), trainer.validation_losses, label='Validation loss') plt.xlabel('Epoch') plt.ylabel('Loss') plt.yscale('log') plt.legend() plt.show() trainer.load_best_model() prediction, truth = trainer.evaluate_test_samples() prediction=torch.from_numpy(prediction) truth=torch.from_numpy(truth[train_config['training_target']].flatten()) avrg=torch.mean(torch.square(torch.sub(prediction, truth))).item() #Average print(avrg) plt.figure() bins = np.linspace(0,3,100) plt.hist2d(truth['energy'].flatten(), prediction, bins=[bins,bins]) plt.plot(bins, bins) plt.colorbar() plt.ylabel(r'$\mathregular{log_{10}(E_{predicted})}$') plt.xlabel(r'$\mathregular{log_{10}(E_{true})}$') plt.show() trainer.save_network_info('test.p') info = pickle.load(open('test.p', 'rb')) m = Model(info) test_set = Dataset([FileLocation], info['normalization_parameters'][0])