def restore_architecture(self, restore_from_config=True): """ To be called only after setting the self.config field :param restore_from_config: whether to build convnet architecture from the configuration dict, or alternatively randomize a new architecture """ if self.config is None: raise ValueError( "Configuration dict must be set to restore architecture!") self.n_filters = self.config["wavelet_kernels"] rand = torch.randn(self.n_filters, self.window_size) torch.tanh_(rand) # restrict domain to [-1, 1] self.filterbank = torch.nn.Parameter(rand) config_to_use = self.config["arch"] if restore_from_config else None self.conv_blocks, self.resid_blocks, out_channels, self.config["arch"] = \ self.make_arch_from_config(config_to_use) self.linear = nn.Linear(out_channels, self.n_filters) self.restore_optimizer() self.set_device(self.device)
def draw_func(self, M, batch_size=1, local_context=False): λ = self.ll_pars['p_act'] * torch.tanh( M / self.ll_pars['p_act']).to('cuda') λ = λ.reshape(1, 1, λ.shape[-2], λ.shape[-1]).repeat_interleave(batch_size, 0) locs1 = torch.distributions.Binomial(1, λ).sample().to('cuda') zeros = torch.zeros_like(locs1).to('cuda') z = torch.distributions.Normal( zeros + self.ll_pars['z_prior'][0], zeros + self.ll_pars['z_prior'][1]).sample().to('cuda') if '3D' in self.psf_pars['modality']: torch.tanh_(z) x_os = torch.distributions.Uniform(zeros - 0.5, zeros + 0.5).sample().to('cuda') y_os = torch.distributions.Uniform(zeros - 0.5, zeros + 0.5).sample().to('cuda') if 'backg_max' in self.ll_pars: bg = torch.distributions.Uniform( torch.zeros(batch_size).to('cuda') + 0.01, torch.ones(batch_size).to('cuda') - 0.01).sample().to('cuda') else: bg = None if local_context: α = self.ll_pars['surv_p'] a11 = 1 - (1 - λ) * (1 - α) locs2 = torch.distributions.Binomial( 1, (1 - locs1) * λ + locs1 * a11).sample().to('cuda') locs3 = torch.distributions.Binomial( 1, (1 - locs2) * λ + locs2 * a11).sample().to('cuda') locs = torch.cat([locs1, locs2, locs3], 1) x_os = x_os.repeat_interleave(3, 1) y_os = y_os.repeat_interleave(3, 1) z = z.repeat_interleave(3, 1) else: locs = locs1 ints = torch.distributions.Uniform( torch.zeros_like(locs) + self.ll_pars['min_int'], torch.ones_like(locs)).sample().to('cuda') x_os *= locs y_os *= locs z *= locs ints *= locs return locs, x_os, y_os, z, ints, bg
def test_tanh(self): x = torch.randn(4, 5, dtype=torch.float32) * 10 mkldnn_x = x.to_mkldnn() self.assertEqual( torch.tanh(x), torch.tanh(mkldnn_x).to_dense(), ) # inplace torch.tanh_(x) torch.tanh_(mkldnn_x) self.assertEqual(x, mkldnn_x.to_dense())
def forward(self, x): h = x for i, layer in enumerate(self.layers): h = layer(h) h = self.out_layer(h) h = torch.tanh_(h) return h
def f(self, x, c_0, p_0): c_1, p_1 = c_0, p_0 allIs = list(range(len(self.noise.noise))) flipped = False # Have we misclassified this image? # For all tiles in the noise for i in allIs: for j in allIs: q = random.choice( self.Q ) # Choose a vector to create the difference along (R, G or B) # For every direction on this vector, with the size of the learning rate for a in [self.learning_rate, -self.learning_rate]: # Make a copy of the noise possibly_changing_noise = self.noise.noise.detach().clone() # Change the noise copy with the randomly chosen vector in direction a possibly_changing_noise[i][j][0] += a * q[0] possibly_changing_noise[i][j][1] += a * q[1] possibly_changing_noise[i][j][2] += a * q[2] # Get the new class and probability of the direction along the chosen vector x += self.noise.to_image_tensor( 'cuda', noise=possibly_changing_noise) c_1, p_1 = self.internal_model.run(x) # If we haven't flipped yet, and do so now, we can flip if not flipped and c_1 != c_0: flipped = True # If we have flipped and still are flipped, and the percentage is larger than it was before, # perform the change. if flipped and c_1 != c_0 and p_1.item() > p_0.item(): self.noise.noise = possibly_changing_noise break # Otherwise, try the other way on this vector. # If that doesn't work, we choose a new random vector # Smoothen the noise self.noise.noise = torch.tanh_(self.noise.noise) return c_1, p_1
def forward(self, x): t0, t1 = torch.chunk(x, ngates, dim=1) t0 = torch.tanh_(t0) t1.sub_(2) t1 = torch.sigmoid_(t1) return t1 * t0
def forward(self, x): # (batch_size, max_seq_length) -> (batch_size, max_seq_length, embedding_size) # random_embedding = self.random_embedding(x) embedding = self.embedding(x) # static_embedding = self.static_embedding(x) # embedding = torch.cat([dynamic_embedding, static_embedding], dim=2) # (batch_size, max_seq_length, embedding_size) -> (batch_size, embedding_size, max_seq_length) embedding = embedding.permute(0, 2, 1) # conv_out = conv(embedding): (batch_size, embedding_size, max_seq_length) -> (batch_size, num_filters, _) # # pool(conv_out): # if global pool: (batch_size, num_filters) # otherwise: (batch_size, num_filters, _) # conv_pool_out = [ self.pool(torch.tanh_(conv(embedding))).flatten(1) for conv in self.conv_list ] conv_pool_out = torch.cat(conv_pool_out, dim=1) conv_pool_out = self.dropout(conv_pool_out) logit1 = self.fc1(conv_pool_out) logit2 = self.fc2(conv_pool_out) return logit1, logit2
def weighted_tanh(input, weight = 1, inplace = False): ''' Applies the weighted tanh function element-wise: .. math:: weightedtanh(x) = tanh(x * weight) See additional documentation for :mod:`echoAI.Activation.Torch.weightedTanh`. ''' if inplace == False: return torch.tanh(weight * input) else: input *= weight torch.tanh_(input) return input
def forward(self, v): p, batch_size, _ = v.size() Wv1 = self.Wvp1(v) self.dropout(Wv1) Wv2 = self.Wvp2(v) self.dropout(Wv2) Wv1 = Wv1.repeat(p, 1, 1, 1) Wv2 = Wv2.repeat(p, 1, 1, 1).permute([1, 0, 2, 3]) s = Wv2 + Wv1 torch.tanh_(s) s = self.v(s) a = nn.functional.softmax(s, 0) c = a * v.repeat(p, 1, 1, 1) c = c.sum(0) g = torch.cat((v, c), 2) g = self.gate(g) h, _ = self.rnn(g) return h
def mish(input, inplace = False): ''' Applies the mish function element-wise: .. math:: mish(x) = x * tanh(softplus(x)) = x * tanh(ln(1 + e^{x})) See additional documentation for :mod:`echoAIAI.Activation.Torch.mish`. ''' if inplace: inp = input.clone() torch.exp_(input) input += 1 torch.tanh_(torch.log_(input)) input *= inp return input else: return input * torch.tanh(F.softplus(input))
def mish(input: Tensor, inplace: bool = False): r"""Applies the mish activation function. See :class:`~ark.nn.Mish` for details. """ if not torch.jit.is_scripting(): x_ts = torch.tanh_(F.softplus(input)) return input.mul_(x_ts) if inplace else input.mul(x_ts) return MishFunction.apply(input, inplace)
def forward(self, x): x = self.head(x) for i in range(len(self.body) - 1): x = self.body[i](x) x = self.body[-1](x) x = self.tail(x) x = self.tail_rgb(x) if self.input_nc == 4: x = nn.functional.pixel_shuffle(x, 2) x = self.output(x) return (torch.tanh_(x) + 1) / 2
def forward(self, passage, question): p, batch_size, _ = passage.size() q, _, _ = question.size() Wp = self.Wup(passage) self.dropout(Wp) Wq = self.Wuq(question) self.dropout(Wq) Wq = Wq.repeat(p, 1, 1, 1).permute([1, 0, 2, 3]) Wp = Wp.repeat(q, 1, 1, 1) s = Wq + Wp torch.tanh_(s) s = self.v(s) a = nn.functional.softmax(s, 0) u = question.repeat(p, 1, 1, 1).permute([1, 0, 2, 3]) c = a * u c = c.sum(0) g = torch.cat((passage, c), 2) g = self.gate(g) _, c = torch.split(g, (self.input_size, self.input_size), 2) v, _ = self.rnn(c) return v
def backward(ctx, g_out): if len(ctx.saved_tensors) == 2: inp, scale = ctx.saved_tensors else: inp, = ctx.saved_tensors scale = 1 if g_bingrad_soft_tanh_scale is not None: scale = scale * g_bingrad_soft_tanh_scale tanh = torch.tanh_(inp * scale) return (1 - tanh.mul_(tanh)).mul_(g_out), None # grad as sign(hardtanh(x)) g_self = (inp.abs() <= 1).to(g_out.dtype) return g_self.mul_(g_out), None
def penalty(y_true, y_pred0, device): const_coeff = y_true.type(torch.FloatTensor).to(device) denom = torch.sum(const_coeff) if denom == 0.0: return 0.0 ss = torch.exp(y_pred0) ss1 = torch.sum(ss, dim=1) y_pred = ss[:, 0] / ss1 res = torch.tanh_(slope * y_pred + incep) res = half0 * (res + oneex) m1 = res * const_coeff result = torch.sum(m1) / denom return coef * result
def det_Pnet(self, img): scale = 1 scale_img = img w, h = img.size min_side = min(w, h) boxes = [] while min_side > 12: input = tf1(scale_img)[None, ...].to(device) print(input.shape) pre = self.pnet(input) pre = pre.cpu().detach() # 置信度 print(torch.sigmoid_(pre[0, 0])) mask = pre[0, 0] > p_cls # 偏移量 offsets = torch.tanh_(pre[0, 1:5, mask]) # 索引 indexs = mask.nonzero() # 算出建议框的左上角和右下角坐标 anchor_x1, anchor_y1 = indexs[:, 1] * 2, indexs[:, 0] * 2 anchor_x2, anchor_y2 = anchor_x1 + 12, anchor_y1 + 12 # 实际框=偏移量*w+建议框对应的坐标 再出以缩放比例就等于原图上的坐标点 act_x1 = (offsets[0] * 12 + anchor_x1) / scale act_y1 = (offsets[1] * 12 + anchor_y1) / scale act_x2 = (offsets[2] * 12 + anchor_x2) / scale act_y2 = (offsets[3] * 12 + anchor_y2) / scale conf = pre[0, 0, mask] # 将坐标点与置信度组合成张量进行Nms _boxes = torch.stack([act_x1, act_y1, act_x2, act_y2, conf], dim=1) boxes.append(_boxes) # 图像金字塔 scale *= 0.702 _w, _h = int(w * scale), int(h * scale) scale_img = img.resize((_w, _h)) min_side = min(_w, _h) boxes = torch.cat(boxes, dim=0) return nms(boxes, p_nms)
def forward(self, x): for name, layer in self.layers[:-1]: x = layer(x) if self.activation_type == 'relu': x = F.relu_(x) elif self.activation_type == 'leaky_relu': x = F.leaky_relu_(x) elif self.activation_type == 'elu': x = F.elu_(x) elif self.activation_type == 'selu': x = F.selu_(x) elif self.activation_type == 'tanh': x = torch.tanh_(x) elif self.activation_type == 'sigmoid': x = torch.sigmoid_(x) elif self.activation_type == 'none': pass else: raise ValueError('Unknown activation function "%s"' % self.activation_type) x = self.layers[-1][1](x) # No activation on output of last layer x = F.normalize(x, dim=-1) # Normalize return x
def forward(self, my_input, hidden_state): print('starting forward') # output is of shape(seq_len x batch x input_size) # apply the gru to obtain the tensor of the output features (seq_len x batch x num_directions * hidden_dim) # and the hidden state h_n (num_layers * num_directions x batch x hidden_dim) # num_directions = 2 because the RNN is bidirectional #print('output shape is: {}'.format(output.shape)) out_features, h_n = self.gru(my_input.float(), hidden_state) # print('a', my_input.shape) # U_t = matrix_mul(out_features, self.word_weight, self.word_bias) # U_w = matrix_mul(my_input, self.context_weight).permute(1, 0) # alpha_t = F.softmax(my_input) # weight_sum = element_wise_mul(out_features, output.permute(1, 0)) print('out_features shape is: {}'.format(out_features.shape)) print('h_n shape is: {}'.format(h_n.shape)) U_t = torch.tanh_(self.attn(out_features)) alpha_t = F.softmax(self.contx(U_t), dim=1) # check that it actually cycles multiplying the right weight with the right hidden features weighted_sum = (alpha_t * out_features).sum(1) print('finishing forward') return alpha_t.permute(0, 2, 1), weighted_sum
def det_R_ONet(self, img, boxes, s): if boxes.shape[0] == 0: print("1") return torch.tensor([]) imgs = [] for box in boxes: # box = make_square(box) # x1, y1, x2, y2 = torch.ceil(box[0]).numpy(), torch.ceil(box[1]).numpy(), torch.ceil(box[2]).numpy(), torch.ceil(box[3]).numpy() x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3]) crop_img = img.crop((x1, y1, x2, y2)) resize_img = crop_img.resize((s, s)) imgs.append(tf(resize_img)) imgs = torch.stack(imgs, dim=0).to(device) if s == 24: pre = self.rnet(imgs) else: pre = self.onet(imgs) pre = pre.cpu().detach() torch.sigmoid_(pre[:, 0]) torch.tanh_(pre[:, 1:]) if s == 24: mask = pre[:, 0] > r_cls else: mask = pre[:, 0] > o_cls pre_offset = pre[mask] _boxes = boxes[mask] w, h = _boxes[:, 2] - _boxes[:, 0], _boxes[:, 3] - _boxes[:, 1] # print(pre_offset.shape, pre_offset[:, 1].shape, _boxes.shape, _boxes[0].shape, w.shape, h.shape) x1 = pre_offset[:, 1] * w + _boxes[:, 0] y1 = pre_offset[:, 2] * h + _boxes[:, 1] x2 = pre_offset[:, 3] * w + _boxes[:, 2] y2 = pre_offset[:, 4] * h + _boxes[:, 3] off_lefteye_x = pre_offset[:, 5] * w + _boxes[:, 0] off_lefteye_y = pre_offset[:, 6] * h + _boxes[:, 1] off_righteye_x = pre_offset[:, 7] * w + _boxes[:, 0] off_righteye_y = pre_offset[:, 8] * h + _boxes[:, 1] off_nose_x = pre_offset[:, 9] * w + _boxes[:, 0] off_nose_y = pre_offset[:, 10] * h + _boxes[:, 1] off_leftmouth_x = pre_offset[:, 11] * w + _boxes[:, 0] off_leftmouth_y = pre_offset[:, 12] * h + _boxes[:, 1] off_rightmouth_x = pre_offset[:, 13] * w + _boxes[:, 0] off_rightmouth_y = pre_offset[:, 14] * h + _boxes[:, 1] # 预测出的框偏窄 if s == 24: _w, _h = (x2 - x1), (y2 - y1) x1, x2 = (x1 - _w * 0.25), (x2 + _w * 0.25) conf = pre_offset[:, 0] _boxes = torch.stack([ x1, y1, x2, y2, conf, off_lefteye_x, off_lefteye_y, off_righteye_x, off_righteye_y, off_nose_x, off_nose_y, off_leftmouth_x, off_leftmouth_y, off_rightmouth_x, off_rightmouth_y ], dim=1) return _boxes
def forward(self, InterfaceMatrices, Prev_Memory, PrevHeadOpsTensors): """ PrevHeadOpsTensors is a namedtuple of same type as PrevTensors. It should have a list Prev_W_list which should be of length total_heads and shape of each tensor [batch_size, N] """ K_t_list = torch.split(InterfaceMatrices['K_t'], self.split_K_t, dim=2) assert list(K_t_list[0].shape)[1:] == [self.M1, self.M2], '{}'.format( list(K_t_list[0].shape)) AE_list = torch.split(InterfaceMatrices['A_t_and_E_t'], self.split_A_t_E_t, dim=2) assert list(AE_list[0].shape)[1:] == [self.M1, self.M2], '{}'.format( list(AE_list[0].shape)) A_t_list = AE_list[:self.num_WH] E_t_list = AE_list[self.num_WH:2 * self.num_WH] s_t_list = torch.split(InterfaceMatrices['s_t'], self.split_s_t, dim=2) assert list(s_t_list[0].shape)[1:] == [2 * self.shift_range + 1, 1], '{}'.format( list(s_t_list[0].shape)) bgg_list = torch.split(InterfaceMatrices['beta_g_gamma'], self.split_beta_g_gamma, dim=2) assert list(bgg_list[0].shape)[1:] == [3, 1], '{}'.format( list(bgg_list[0].shape)) #beta_g_gamma as bgg # assert list(Interface_t.shape)[1:] == [ self.M1, 2*self.M2*self.num_WH + (self.M2 + 1)*self.total_heads ] # assert list(Prev_Memory.shape)[1:] == [ self.N, self.M1, self.M2 ] # parameters = torch.split(Interface_t, self.split_list, dim = 2) # K_t_list = parameters[ : self.total_heads] # E_t_list = parameters[self.total_heads : self.total_heads + self.num_WH] # A_t_list = parameters[self.total_heads + self.num_WH : self.total_heads + 2*self.num_WH] # Others_params_list = parameters[self.total_heads + 2*self.num_WH : 2*self.total_heads + 2*self.num_WH] # Each element of this list is a vector of length M2, where we take first 2*shift_range+1, next 1, next 1 and then the next 1 as s_t, beta_t, g_t, gamma_t respectively. If some other scalar still remains (i.e. M2 > 2*shift_range+1 + 3), we let them be. # self.K_t_list = [] # self.beta_t_list = [] # self.softmax_weights = [] # self.W_c_t_list = [] # self.g_t_list = [] # self.W_g_t_list = [] # self.gamma_t_list = [] # self.W_hat_t_list = [] # self.s_t_list = [] # self.W_hat_t_sharpened_list = [] New_W_list = [] for i in range(self.num_WH): # For erase_t assert list(E_t_list[i].shape)[1:] == self.MemorySlot_dims torch.sigmoid_(E_t_list[i]) # For a_t assert list(A_t_list[i].shape)[1:] == self.MemorySlot_dims torch.tanh_(A_t_list[i]) for i in range(self.total_heads): #Param_Vec = Others_params_list[i].squeeze(2) #assert list(Param_Vec.shape)[1:] == [self.M1] s_t = s_t_list[i].squeeze(-1) assert list(s_t.shape)[1:] == [2 * self.shift_range + 1 ], '{}'.format(list(s_t.shape)) beta_t = bgg_list[i][:, 0] assert list(beta_t.shape)[1:] == [1], '{}'.format( list(beta_t.shape)) g_t = bgg_list[i][:, 1] assert list(g_t.shape)[1:] == [1], '{}'.format(list(g_t.shape)) gamma_t = bgg_list[i][:, 2] assert list(gamma_t.shape)[1:] == [1], '{}'.format( list(gamma_t.shape)) # For W_c_t K_t = K_t_list[i] assert list(K_t.shape)[1:] == self.MemorySlot_dims torch.tanh_(K_t) # To bring it into (-1,1) #self.K_t_list.append(K_t) Mat_Sim = self.MatrixSimilarity(K_t, Prev_Memory, self.eps) #Shape : [batch_size, N] beta_t_compat = 1 + self.softplus(beta_t) #self.beta_t_list.append(beta_t_compat) softmax_weights = torch.mul( beta_t_compat, Mat_Sim) # beta_t_compat*5 to increase key's strength. #Shape : [batch_size, N] #self.softmax_weights.append(softmax_weights) exponents = torch.exp(softmax_weights.clamp(0.0, 80.0)) sums = (torch.sum(exponents, dim=1) + self.eps) W_c_t = exponents / sums.unsqueeze(1) #self.W_c_t_list.append(W_c_t) assert list(W_c_t.shape)[1:] == [self.N] # For W_g_t assert list(g_t.shape)[1:] == [1] g_t_compat = torch.sigmoid(g_t) #self.g_t_list.append(g_t_compat) W_g_t = g_t_compat * W_c_t + ( 1 - g_t_compat) * PrevHeadOpsTensors.Prev_W_List[i] #self.W_g_t_list.append(W_g_t) assert list(W_g_t.shape)[1:] == [self.N] # For W_hat_t assert list(s_t.shape)[1:] == [2 * self.shift_range + 1] s_t_softmaxed = self.softmax(s_t, dim=1, eps=self.eps) # s_t is softmax'ed W_hat_t = self.CircularConvolution(W_g_t, s_t_softmaxed, self.shift_range) #Shape : [batch_size, N] #self.s_t_list.append(s_t_softmaxed) #self.W_hat_t_list.append(W_hat_t) # For W_t assert list(gamma_t.shape)[1:] == [1] gamma_t_compat = 1 + self.softplus(gamma_t) #self.gamma_t_list.append(gamma_t_compat) W_hat_t_sharpened = torch.pow(W_hat_t, gamma_t_compat) #Shape : [batch_size, N] #self.W_hat_t_sharpened_list.append(W_hat_t_sharpened) denom = torch.sum(W_hat_t_sharpened, dim=1) W_t = W_hat_t_sharpened / (denom.unsqueeze(1) + self.eps) assert list(W_t.shape)[1:] == [self.N] with torch.no_grad(): ################# Special Check, don't remove!! ######## if torch.any(torch.isnan(W_t) + torch.isinf(W_t)): raise ValueError( "Yo... the Head Ops turned Anti-Christ bruh. At Head {} ... " .format(i)) New_W_list.append(W_t) R_weighings_list = New_W_list[:self.num_RH] W_weighings_list = New_W_list[self.num_RH:self.num_RH + self.num_WH] # t2 = time.time() # print("MNTMHeadOps called. Time taken: {}".format(t2-t1)) return HeadOpsOutput(AllWeights=New_W_list, ReadWeighings=R_weighings_list, WriteWeighings=W_weighings_list, EraseMatList=E_t_list, AddMatList=A_t_list)
def forward(self, inp): x = inp.contiguous().view(-1, self.hidden_dim) u = torch.tanh_(torch.mm(x, self.weight).view(-1, self.seq_len) + self.bias) a = F.softmax(u, dim=1) s = (inp * torch.unsqueeze(a, 2)).sum(1) return a, s
def forward(self, input): hid_sum = self.in_hid(input) hidden = torch.tanh_(hid_sum) out_sum = self.out_hid(hidden) output = torch.sigmoid(out_sum) return output
'relu': dict(func=lambda x, **_: torch.relu_(x), alpha=None, gain=math.sqrt(2), cuda_idx=2, ref='y', zero_2nd_grad=True), 'lrelu': dict(func=lambda x, alpha, **_: F.leaky_relu_(x, alpha), alpha=0.2, gain=math.sqrt(2), cuda_idx=3, ref='y', zero_2nd_grad=True), 'tanh': dict(func=lambda x, **_: torch.tanh_(x), alpha=None, gain=1.0, cuda_idx=4, ref='y', zero_2nd_grad=False), 'sigmoid': dict(func=lambda x, **_: torch.sigmoid_(x), alpha=None, gain=1.0, cuda_idx=5, ref='y', zero_2nd_grad=False), 'elu': dict(func=lambda x, **_: F.elu_(x), alpha=None,
def forward(self, inp): u = torch.tanh_(self.attn(inp)) a = F.softmax(self.contx(u), dim=1) s = (a * inp).sum(1) return a.permute(0, 2, 1), s
def __call__(self, num): for epoch in range(num): trainLoss = 0 for i, (img, lable) in enumerate(self.dataloader): img, lable = img.to(self.device), lable.to(self.device) pre = self.net(img) if self.img_size == 12: pre = pre.reshape(-1, 15) real_conf = lable[:, 0] pre_conf = pre[:, 0] conf_mask = real_conf < 2 conf_loss = self.conf_loss_fn(pre_conf[conf_mask], real_conf[conf_mask]) off_mask = real_conf > 0 pre_off = pre[off_mask] real_off = lable[off_mask] off_loss = self.off_loss_fn(torch.tanh_(pre_off[:, 1:5]), real_off[:, 1:5]) landmask_loss = self.off_loss_fn(pre_off[:, 5:], real_off[:, 5:]) loss = conf_loss + off_loss + landmask_loss self.opt.zero_grad() loss.backward() self.opt.step() trainLoss += loss.cpu().detach().item() # print("i", i, "置信度损失:", conf_loss.cpu().detach().item(), "偏移量损失:", off_loss.cpu().detach().item(), # "五官损失:", landmask_loss.cpu().detach().item()) if (i + 1) % 50 == 0: print(i) torch.save(self.net.state_dict(), f"../param/test_param/net.pt") avgTrainLoss = trainLoss / len(self.dataloader) print( "批次:", epoch, ",训练集损失:", avgTrainLoss, "time:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) self.summary.add_scalar("loss", avgTrainLoss, epoch) if epoch % 5 == 0: if self.img_size == 12: torch.save(self.net.state_dict(), f"../param/{epoch}_pnet.pt") elif self.img_size == 24: torch.save(self.net.state_dict(), f"../param/{epoch}_rnet.pt") else: torch.save(self.net.state_dict(), f"../param/{epoch}_onet.pt")
def forward(self, events, timestamps, batch_idx, imsize, raw=True, intermediate=False): # compute extended image size outsize = [ tuple(map(lambda x: x // 2**i, imsize)) for i in range(len(self.enc)) ][::-1] # compute event_image if raw: extended_size = self._extend_size(imsize) with torch.no_grad(): xb = compute_event_image(events, timestamps, batch_idx, extended_size, device=self.device, dtype=torch.float32) else: xb = events y = [] skip = [xb] if intermediate: intermediate_output = {'input': xb} # encoder for enc_block in self.enc: skip.append(enc_block(skip[-1])) if intermediate: intermediate_output[f'enc_{len(skip)-2}'] = skip[-1] # transition h = skip[-1] for idx, res in enumerate(self.tr): h = res(h) if intermediate: intermediate_output[f'tr_{idx}'] = h # decoder n = len(skip) for idx, (s, d, f) in enumerate(zip(skip[n:0:-1], self.dec, self.flow)): h = torch.cat((h, s), 1) if intermediate: intermediate_output[f'dec_cat_{idx}'] = h h = d(h) if intermediate: intermediate_output[f'dec_op_{idx}'] = h h_flow = f(h) if intermediate: intermediate_output[f'dec_flow_arth_{idx}'] = h_flow # clone is required for backward pass y.append(torch.tanh(h_flow).clone()) else: y.append(torch.tanh_(h_flow)) y[-1].mul_(256.) h = torch.cat((h, y[-1]), 1) # shrink image to original size result = self._get_result(y, outsize) add_info = (intermediate_output, ) if intermediate else tuple() return (result, timestamps.reshape(-1, 2), batch_idx) + add_info
def forward(ctx, x: Tensor, inplace: bool = False): ctx.save_for_backward(x) x_ts = torch.tanh_(F.softplus(x)) return x.mul_(x_ts) if inplace else x.mul(x_ts)
def backward(ctx, grad_output): x = ctx.saved_variables[0] x_s = torch.sigmoid(x) x_ts = torch.tanh_(F.softplus(x)) return grad_output * (x_ts + x * x_s * (1 - x_ts * x_ts))