def project_sample(self, x: TensorList, proj_matrix=None): # Apply projection matrix if proj_matrix is None: proj_matrix = self.projection_matrix with fluid.dygraph.guard(): return operation.conv2d(x.apply(n2p), proj_matrix.apply(n2p)).apply( self.projection_activation).numpy()
def __call__(self, x: TensorList, mask): """ Compute residuals :param x: [filters] :return: [data_terms, filter_regularizations] """ # Do convolution and compute residuals residuals = operation.conv2d(self.training_samples, x).apply(self.response_activation) #residuals = self.response_activation(residuals) residuals = residuals - self.y residuals = self.sample_weights.sqrt().view(-1, 1, 1, 1) * residuals # Add regularization for projection matrix residuals.extend(self.filter_reg.apply(math.sqrt) * x) return residuals
def ip_input(self, a: TensorList, b: TensorList): num = len(a) // 2 # Number of filters a_filter = a #[:num] b_filter = b #[:num] a_P = a[num:] b_P = b[num:] # Filter inner product # ip_out = a_filter.reshape(-1) @ b_filter.reshape(-1) ip_out = operation.conv2d(a_filter, b_filter).view(-1) # Add projection matrix part # ip_out += a_P.reshape(-1) @ b_P.reshape(-1) #print('ip_out', type(ip_out),ip_out.size()) #ip_out += operation.conv2d(a_P.view(1,-1,1,1), b_P.view(1,-1,1,1)).view(-1) # Have independent inner products for each filter return ip_out.concat(ip_out.clone())
def __call__(self, x: TensorList, scope=''): """ Compute residuals :param x: [filters] :return: [data_terms, filter_regularizations] """ training_samples, y, samples_weights = self.get_inputs(scope) # Do convolution and compute residuals residuals = operation.conv2d(training_samples, x, mode='same').apply( self.response_activation) residuals = residuals - y residuals = residuals * samples_weights.sqrt() # Add regularization for projection matrix residuals.extend( x.apply(static_identity) * self.filter_reg.apply(math.sqrt)) return residuals
def __call__(self, x: TensorList, scope=''): """ Compute residuals :param x: [filters, projection_matrices] :return: [data_terms, filter_regularizations, proj_mat_regularizations] """ training_samples, y, samples_weights = self.get_inputs(scope) filter = x[:len(x) // 2] # w2 in paper P = x[len(x) // 2:] # w1 in paper # Do first convolution compressed_samples = operation.conv1x1(training_samples, P).apply( self.projection_activation) # Do second convolution residuals = operation.conv2d(compressed_samples, filter, mode='same').apply( self.response_activation) # Compute data residuals residuals = residuals - y residuals = residuals * samples_weights.sqrt() # Add regularization for projection matrix # TODO: remove static_identity # for now, this is needed. Otherwise the gradient is None residuals.extend( filter.apply(static_identity) * self.filter_reg.apply(math.sqrt)) # Add regularization for projection matrix residuals.extend( P.apply(static_identity) * self.projection_reg.apply(math.sqrt)) return residuals
def __call__(self, x: TensorList): """ Compute residuals :param x: [filters, projection_matrices] :return: [data_terms, filter_regularizations, proj_mat_regularizations] """ filter = x #[:len(x)//2] # w2 in paper #P = x[len(x)//2:] # w1 in paper #print('filter', type(filter),filter.size()) #print('P', type(P), P.size()) #print('self.training_samples', type(self.training_samples), self.training_samples.size()) # Do first convolution #test = operation.conv1x1(self.training_samples, P) #print('test_type:', type(test), test.size()) #compressed_samples = operation.conv1x1(self.training_samples, P).apply(self.projection_activation) # Do second convolution residuals = operation.conv2d(self.training_samples, filter, mode='same').apply( self.response_activation) # Compute data residuals residuals = residuals - self.y residuals = self.sample_weights.sqrt().view(-1, 1, 1, 1) * residuals # Add regularization for projection matrix residuals.extend(self.filter_reg.apply(math.sqrt) * filter) # Add regularization for projection matrix #residuals.extend(self.projection_reg.apply(math.sqrt) * P) return residuals
def ip_input(self, a: TensorList, b: TensorList): # return a.reshape(-1) @ b.reshape(-1) # return (a * b).sum() return operation.conv2d(a, b).view(-1)
def apply_filter(self, sample_x: TensorList): with fluid.dygraph.guard(): sample_x = sample_x.apply(n2p) filter = self.filter.apply(n2p) return operation.conv2d(sample_x, filter, mode='same').numpy()