def scale_invariant_gradient( inp, deltas=[1,2,4,8,16], weights=[1,1,1,1,1],epsilon=0.001): """Computes the scale invariant gradient images inp: Tensor deltas: list of int The pixel delta for the difference. This vector must be the same length as weight. weights: list of float The weight factor for each difference. This vector must be the same length as delta. epsilon: float epsilon value for avoiding division by zero """ inbatch = len(inp.shape.as_list())==4 if not inbatch: inp = tf.expand_dims(inp,0) inp = convert_NHWC_to_NCHW(inp) assert len(deltas)==len(weights) sig_images = [] for delta, weight in zip(deltas,weights): sig_images.append(sops.scale_invariant_gradient(inp, deltas=[delta], weights=[weight], epsilon=epsilon)) output = tf.concat(sig_images,axis=1) output = convert_NCHW_to_NHWC(output) if not inbatch: output = tf.squeeze(output,0) return output
def scale_invariant_gradient(inp, deltas, weights, epsilon=0.001): """Computes the scale invariant gradient images inp: Tensor deltas: list of int The pixel delta for the difference. This vector must be the same length as weight. weights: list of float The weight factor for each difference. This vector must be the same length as delta. epsilon: float epsilon value for avoiding division by zero """ assert len(deltas) == len(weights) sig_images = [] for delta, weight in zip(deltas, weights): sig_images.append( sops.scale_invariant_gradient(inp, deltas=[delta], weights=[weight], epsilon=epsilon)) return tf.concat(sig_images, axis=1)
def get_multi_sig_list(inp, sig_param_list): """ Get multi sig with the pattern in the sig_param_list, result is concat in axis=1 inp: Tensor sig_param_list: a list of sig_param dictionary """ sig_list = [] for sig_param in sig_param_list: sig_list.append(sops.scale_invariant_gradient(inp, **sig_param)) #multi_sig = tf.concat(sig_list,axis=1) return sig_list
def test_shape(self): with self.test_session(use_gpu=False, force_gpu=False): input1 = np.empty((8, 40, 31)) input2 = np.empty((8, 1, 40, 31)) input3 = np.empty((2, 2, 2, 40, 31)) inputs = (input1, input2, input3) expected_shape = [8, 2, 40, 31] for i in inputs: output_tensor = ops.scale_invariant_gradient(input=i) out_shape = output_tensor.get_shape().as_list() self.assertAllEqual(out_shape, expected_shape)
def _test_grad(self, dtype): #A = np.random.rand(3,4).astype(dtype) A = np.linspace(1, 2, num=16, dtype=dtype).reshape((4, 4)) shape = A.shape data = tf.constant(A) output = ops.scale_invariant_gradient(input=data, deltas=[1, 2, 4], weights=[1, 0.5, 0.25], epsilon=0.001) #print(A) #print(output.eval()) err = tf.test.compute_gradient_error(data, shape, output, output.get_shape().as_list(), x_init_value=A) print('error', err, flush=True) self.assertLess(err, 1e-3)