def get_updates(self, params, constraints, loss):
        grads = self.get_gradients(loss, params)
        self.updates = [K.update_add(self.iterations, 1)]

        t = self.iterations + 1
        lr_t = self.lr * K.sqrt(1. - K.pow(self.beta_2, t)) / (1. - K.pow(self.beta_1, t))

        shapes = [K.get_variable_shape(p) for p in params]
        ms = [K.zeros(shape) for shape in shapes]
        vs = [K.zeros(shape) for shape in shapes]
        self.weights = [self.iterations] + ms + vs

        for p, g, m, v in zip(params, grads, ms, vs):
            m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
            v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g)
            p_t = p - self.get_param_learning_rate_t(p,t,lr_t) * m_t / (K.sqrt(v_t) + self.epsilon)

            self.updates.append(K.update(m, m_t))
            self.updates.append(K.update(v, v_t))

            new_p = p_t
            # apply constraints
            if p in constraints:
                c = constraints[p]
                new_p = c(new_p)
            self.updates.append(K.update(p, new_p))
        return self.updates
def get_model(inputdim, outputdim, regularization_strength=0.01, lr=0.000, cosine=False, **kwargs):
    transformation = Dense(inputdim, init='identity',
                           W_constraint=Orthogonal())

    model = Graph()
    model.add_input(name='embeddings1', input_shape=(inputdim,))
    model.add_input(name='embeddings2', input_shape=(inputdim,))
    model.add_shared_node(transformation, name='transformation',
                          inputs=['embeddings1', 'embeddings2'],
                          outputs=['transformed1', 'transformed2'])
    model.add_node(Lambda(lambda x: x[:, :outputdim]), input='transformed1', name='projected1')
    model.add_node(Lambda(lambda x: -x[:, :outputdim]), input='transformed2', name='negprojected2')

    if cosine:
        model.add_node(Lambda(lambda x:  x / K.reshape(K.sqrt(K.sum(x * x, axis=1)), (x.shape[0], 1))),
                       name='normalized1', input='projected1')
        model.add_node(Lambda(lambda x:  x / K.reshape(K.sqrt(K.sum(x * x, axis=1)), (x.shape[0], 1))),
                       name='negnormalized2', input='negprojected2')
        model.add_node(Lambda(lambda x: K.reshape(K.sum(x, axis=1), (x.shape[0], 1))),
                       name='distances', inputs=['normalized1', 'negnormalized2'], merge_mode='mul')
    else:
        model.add_node(Lambda(lambda x: K.reshape(K.sqrt(K.sum(x * x, axis=1)), (x.shape[0], 1))),
                       name='distances', inputs=['projected1', 'negprojected2'], merge_mode='sum')

    model.add_output(name='y', input='distances')
    model.compile(loss={'y': lambda y, d: K.mean(y * d)}, optimizer=SimpleSGD())
    return model
    def get_updates(self, params, constraints, loss):
        grads = self.get_gradients(loss, params)
        shapes = [K.get_variable_shape(p) for p in params]
        accumulators = [K.zeros(shape) for shape in shapes]
        delta_accumulators = [K.zeros(shape) for shape in shapes]
        self.weights = accumulators + delta_accumulators
        self.updates = []

        for p, g, a, d_a in zip(params, grads, accumulators, delta_accumulators):
            # update accumulator
            new_a = self.rho * a + (1. - self.rho) * K.square(g)
            self.updates.append(K.update(a, new_a))

            # use the new accumulator and the *old* delta_accumulator
            update = g * K.sqrt(d_a + self.epsilon) / K.sqrt(new_a + self.epsilon)

            new_p = p - get_learing_rate(p,self.lr) * update
            # apply constraints
            if p in constraints:
                c = constraints[p]
                new_p = c(new_p)
            self.updates.append(K.update(p, new_p))

            # update delta_accumulator
            new_d_a = self.rho * d_a + (1 - self.rho) * K.square(update)
            self.updates.append(K.update(d_a, new_d_a))
        return self.updates
Example #4
0
    def call(self, inputs, mask=None):
        if not isinstance(inputs, list) or len(inputs) <= 1:
            raise TypeError('SpkLifeLongMemory must be called on a list of tensors '
                            '(at least 2). Got: ' + str(inputs))
        # (None(batch), 1), index of speaker
        target_spk_l = inputs[0]
        target_spk_l = K.reshape(target_spk_l, (target_spk_l.shape[0], ))
        if K.dtype(target_spk_l) != 'int32':
            target_spk_l = K.cast(target_spk_l, 'int32')
        # (None(batch), embed_dim)
        spk_vector_l = inputs[1]
        # Start to update life-long memory based on the learned speech vector
        # First do normalization
        spk_vector_eps = K.switch(K.equal(spk_vector_l, 0.), np.spacing(1), spk_vector_l)  # avoid zero
        spk_vector_eps = K.sqrt(K.sum(spk_vector_eps**2, axis=1))
        spk_vector_eps = spk_vector_eps.dimshuffle((0, 'x'))
        spk_vector = T.true_div(spk_vector_l, K.repeat_elements(spk_vector_eps, self.vec_dim, axis=1))
        # Store speech vector into life-long memory according to the speaker identity.
        life_long_mem = T.inc_subtensor(self.life_long_mem[target_spk_l, :], spk_vector)
        # Normalization for memory
        life_long_mem_eps = K.switch(K.equal(life_long_mem, 0.), np.spacing(1), life_long_mem)  # avoid 0
        life_long_mem_eps = K.sqrt(K.sum(life_long_mem_eps**2, axis=1))
        life_long_mem_eps = life_long_mem_eps.dimshuffle((0, 'x'))
        life_long_mem = T.true_div(life_long_mem, K.repeat_elements(life_long_mem_eps, self.vec_dim, axis=1))

        # (None(batch), spk_size, embed_dim)
        return life_long_mem
Example #5
0
def semantic_matrix(argv):
	assert len(argv) == 2
	q = argv[0]
	a = argv[1]
	q_sqrt = K.sqrt((q ** 2).sum(axis=2, keepdims=True))
	a_sqrt = K.sqrt((a ** 2).sum(axis=2, keepdims=True))
	denominator = K.batch_dot(q_sqrt, K.permute_dimensions(a_sqrt, [0,2,1]))
	return K.batch_dot(q, K.permute_dimensions(a, [0,2,1])) / (denominator + SAFE_EPSILON)
Example #6
0
 def __call__(self, x):
     xshape = K.int_shape(x)
     if self.division_idx is None:
         self.division_idx = xshape[-1]/2
     x = K.reshape(x, (-1, xshape[-1]))
     x /= K.sqrt(K.sum(K.square(x), axis=0, keepdims=True))
     xx = K.sum(x[:,:self.division_idx] * x[:,self.division_idx:], axis=0)
     return self.gamma * K.sqrt(K.sum(K.square(xx)) + K.epsilon())
    def vae_loss(x, x_decoded_mean):
        #     xent_loss = objectives.binary_crossentropy(x, x_decoded_mean)
        #     bott_loss = objectives.binary_crossentropy(sp, z)
        #     print(sp.shape)µ
        kl_loss = - 0.5 * K.mean(1 + z_log_sigma - K.square(z_mean) - K.exp(z_log_sigma), axis=-1)

        return K.sqrt(K.sum(K.square(x - x_decoded_mean), axis=-1)) + l * K.sqrt(
            K.sum(K.square(z - sp), axis=-1)) + beta * kl_loss
 def nme(self, y_true, y_pred):
     y_pred_reshape = K.reshape(y_pred, (-1, self.config.num_output//2, 2))
     y_true_reshape = K.reshape(y_true, (-1, self.config.num_output//2, 2))
     interocular_distance = K.sqrt(K.sum(
         (K.mean(y_true_reshape[:, 36:42, :], axis=1) - K.mean(y_true_reshape[:, 42:48, :], axis=1)) ** 2, axis=-1))
     return K.mean(K.sum(
         K.sqrt(K.sum((y_pred_reshape - y_true_reshape) ** 2, axis=-1))
         , axis=-1)) / K.mean((interocular_distance * self.config.num_output / 2))
    def get_similarity(self):
        ''' Specify similarity in configuration under 'similarity_params' -> 'mode'
        If a parameter is needed for the model, specify it in 'similarity_params'

        Example configuration:

        config = {
            ... other parameters ...
            'similarity_params': {
                'mode': 'gesd',
                'gamma': 1,
                'c': 1,
            }
        }

        cosine: dot(a, b) / sqrt(dot(a, a) * dot(b, b))
        polynomial: (gamma * dot(a, b) + c) ^ d
        sigmoid: tanh(gamma * dot(a, b) + c)
        rbf: exp(-gamma * l2_norm(a-b) ^ 2)
        euclidean: 1 / (1 + l2_norm(a - b))
        exponential: exp(-gamma * l2_norm(a - b))
        gesd: euclidean * sigmoid
        aesd: (euclidean + sigmoid) / 2
        '''

        params = self.similarity_params
        similarity = params['mode']

        axis = lambda a: len(a._keras_shape) - 1
        dot = lambda a, b: K.batch_dot(a, b, axes=axis(a))
        l2_norm = lambda a, b: K.sqrt(K.sum((a - b) ** 2, axis=axis(a), keepdims=True))
        l1_norm = lambda a, b: K.sum(K.abs(a - b), axis=axis(a), keepdims=True)

        if similarity == 'cosine':
            return lambda x: dot(x[0], x[1]) / K.sqrt(dot(x[0], x[0]) * dot(x[1], x[1]))
        elif similarity == 'polynomial':
            return lambda x: (params['gamma'] * dot(x[0], x[1]) + params['c']) ** params['d']
        elif similarity == 'sigmoid':
            return lambda x: K.tanh(params['gamma'] * dot(x[0], x[1]) + params['c'])
        elif similarity == 'rbf':
            return lambda x: K.exp(-1 * params['gamma'] * l2_norm(x[0], x[1]) ** 2)
        elif similarity == 'euclidean':
            return lambda x: 1 / (1 + l2_norm(x[0], x[1]))
        elif similarity == 'l1':
            return lambda x: -l1_norm(x[0], x[1])
        elif similarity == 'exponential':
            return lambda x: K.exp(-1 * params['gamma'] * l2_norm(x[0], x[1]))
        elif similarity == 'gesd':
            euclidean = lambda x: 1 / (1 + l2_norm(x[0], x[1]))
            sigmoid = lambda x: 1 / (1 + K.exp(-1 * params['gamma'] * (dot(x[0], x[1]) + params['c'])))
            return lambda x: euclidean(x) * sigmoid(x)
        elif similarity == 'aesd':
            euclidean = lambda x: 0.5 / (1 + l2_norm(x[0], x[1]))
            sigmoid = lambda x: 0.5 / (1 + K.exp(-1 * params['gamma'] * (dot(x[0], x[1]) + params['c'])))
            return lambda x: euclidean(x) + sigmoid(x)
        else:
            raise Exception('Invalid similarity: {}'.format(similarity))
Example #10
0
 def __call__(self, x):
     regularization = 0
     dimorder = self.axes + list(set(range(K.ndim(x))) - set(self.axes))
     p = K.permute_dimensions(x, dimorder)
     if self.TV:
         regularization += self.TV*K.sum(K.sqrt(K.square(diffr(p)) + K.square(diffc(p)) + K.epsilon()))
     if self.TV2:
         regularization += self.TV2*K.sum(K.sqrt(K.square(diffrr(p)) + K.square(diffcc(p)) + 2*K.square(diffrc(p)) + K.epsilon()))
     return regularization
Example #11
0
    def call(self, inputs, mask=None):
        if type(inputs) is not list or len(inputs) <= 1:
            raise Exception('Merge must be called on a list of tensors '
                            '(at least 2). Got: ' + str(inputs))
        # case: "mode" is a lambda or function.
        if hasattr(self.mode, '__call__'):
            # TODO: consider making it possible to
            # pass custom arguments to lambda.
            arguments = {}
            return self.mode(inputs, **arguments)

        if self.mode == 'sum' or self.mode == 'ave':
            s = inputs[0]
            for i in range(1, len(inputs)):
                s += inputs[i]
            if self.mode == 'ave':
                s /= len(inputs)
            return s

        elif self.mode == 'concat':
            return K.concatenate(inputs, axis=self.concat_axis)

        elif self.mode == 'mul':
            s = inputs[0]
            for i in range(1, len(inputs)):
                s *= inputs[i]
            return s

        elif self.mode == 'dot':
            l1 = inputs[0]
            l2 = inputs[1]
            output = K.batch_dot(l1, l2, self.dot_axes)
            return output

        elif self.mode == 'cos':
            l1 = inputs[0]
            l2 = inputs[1]
            denominator = K.sqrt(K.batch_dot(l1, l1, self.dot_axes) *
                                 K.batch_dot(l2, l2, self.dot_axes))
            output = K.batch_dot(l1, l2, self.dot_axes) / denominator
            output = K.expand_dims(output, 1)
            return output

        elif self.mode == 'abs':
			s = inputs[0] * inputs[0]
			for i in range(1, len(inputs)):
				s += inputs[i] * inputs[i]
			return K.sqrt(s)

        elif self.mode == 'atan2':
			return T.tensor.arctan2(inputs[1], inputs[0])

        else:
            raise Exception('Unknown merge mode.')
Example #12
0
 def call(self, x, mask=None):
     if mask is not None:
         mask = K.cast(mask, K.floatx())
         mask = K.expand_dims(mask, axis=-1)
         s = K.sum(mask, axis=1)
         if K.equal(s, K.zeros_like(s)) is None:
             return K.mean(x, axis=1)
         else:
             return K.cast(K.sum(x * mask, axis=1) / K.sqrt(s), K.floatx())
     else:
         return K.sum(x, axis=1)/K.sqrt(len(x))
Example #13
0
 def __call__(self, x):
     xshape = K.int_shape(x)
     if self.axis is 'last':
         x = K.reshape(x, (-1, xshape[-1]))
         x /= K.sqrt(K.sum(K.square(x), axis=0, keepdims=True))
         xx = K.dot(K.transpose(x), x)
         return self.gamma * K.sum(K.log(1.0 + K.exp(self.lam * (xx - 1.0))) * (1.0 - K.eye(xshape[-1])))
     elif self.axis is 'first':
         x = K.reshape(x, (xshape[0], -1))
         x /= K.sqrt(K.sum(K.square(x), axis=1, keepdims=True))
         xx = K.dot(x, K.transpose(x))
         return self.gamma * K.sum(K.log(1.0 + K.exp(self.lam * (xx - 1.0))) * (1.0 - K.eye(xshape[0])))
Example #14
0
def normals_metric(y_true, y_pred):

    y_true = K.variable(y_true)
    y_pred = K.variable(y_pred)

    y_true = K.expand_dims(y_true,0)


    filter_y = K.variable(np.array([[ 0., -0.5 , 0.],
                               [0., 0., 0.],
                               [0., 0.5, 0.]]).reshape(3, 3, 1, 1))


    filter_x = K.variable(np.array([ [0, 0., 0.],
                               [0.5, 0., -0.5],
                               [0., 0., 0.]]).reshape(3, 3, 1, 1))

    dzdx = K.conv2d(K.exp(y_true), filter_x, padding='same')
    dzdy = K.conv2d(K.exp(y_true), filter_y, padding='same')

    dzdx_ = dzdx * -1.0#K.constant(-1.0, shape=[batch_size,K.int_shape(y_pred)[1],K.int_shape(y_pred)[2],K.int_shape(y_pred)[3]]) #K.constant(-1.0, shape=K.int_shape(dzdx))
    dzdy_ = dzdy * -1.0#K.constant(-1.0, shape=[batch_size,K.int_shape(y_pred)[1],K.int_shape(y_pred)[2],K.int_shape(y_pred)[3]]) #K.constant(-1.0, shape=K.int_shape(dzdy))

    mag_norm = K.pow(dzdx,2) + K.pow(dzdy,2) + 1.0#K.constant(1.0, shape=[batch_size,K.int_shape(y_pred)[1],K.int_shape(y_pred)[2],K.int_shape(y_pred)[3]]) #K.constant(1.0, shape=K.int_shape(dzdx))

    mag_norm = K.sqrt(mag_norm)
    N3 = 1.0 / mag_norm #K.constant(1.0, shape=K.int_shape(dzdx)) / mag_norm
    N1 = dzdx_ / mag_norm
    N2 = dzdy_ / mag_norm

    normals = K.concatenate(tensors=[N1,N2,N3],axis=-1)

    dzdx_pred = K.conv2d(K.exp(y_pred), filter_x, padding='same')
    dzdy_pred = K.conv2d(K.exp(y_pred), filter_y, padding='same')

    mag_norm_pred = K.pow(dzdx_pred,2) + K.pow(dzdy_pred,2) + 1.0
    mag_norm_pred = K.sqrt(mag_norm_pred)

    grad_x = K.concatenate(tensors=[1.0/ mag_norm_pred,
                                    0.0/ mag_norm_pred, dzdx_pred/ mag_norm_pred],axis=-1)
    grad_y = K.concatenate(tensors=[0.0/ mag_norm_pred,
                                    1.0/ mag_norm_pred, dzdy_pred/ mag_norm_pred],axis=-1)


    dot_term_x = K.mean(K.sum(normals[0,:,:,:] * grad_x[0,:,:,:], axis=-1, keepdims=True), axis=-1)
    dot_term_y = K.mean(K.sum(normals[0,:,:,:] * grad_y[0,:,:,:], axis=-1, keepdims=True), axis=-1)


    dot_term_x = K.abs(dot_term_x)
    dot_term_y = K.abs(dot_term_y)

    return K.eval(K.mean(dot_term_x)),K.eval(K.mean(dot_term_y))
Example #15
0
def dssm_loss(d, y):
    q = y[:, :y.shape[1]/3]
    d_positive = y[:, y.shape[1]/3:2*y.shape[1]/3]
    d_negative = y[:, 2*y.shape[1]/3:]
    eps = 0.00001
    inf = 100000
    q_len = K.sqrt(K.sum(K.square(q), axis=1))
    d_positive_len = K.sqrt(K.sum(K.square(d_positive), axis=1))
    d_negative_len = K.sqrt(K.sum(K.square(d_negative), axis=1))
    positive_cosine_distance = K.sum(q * d_positive, axis=1) / (q_len * d_positive_len)
    negative_cosine_distance = K.sum(q * d_negative, axis=1) / (q_len * d_negative_len)
    all_cosine_distance = K.exp(positive_cosine_distance) + K.exp(negative_cosine_distance)
    positive_cosine_distance = K.exp(positive_cosine_distance) / (all_cosine_distance)
    loss = -K.mean(K.log(positive_cosine_distance))
    return ifelse(T.isnan(loss), 0., loss)
def visualize_mser_classifier():
	model = load_model('mser_classifier')
	# Get the symbolic outputs of each "key" layer
	layer_dict = dict([(layer.name, layer) for layer in model.layers])
	layer_name = 'block5_conv3'
	# Can be any integer from 0 to 511, as there are 512 filters in that layer
	kept_filters = []
	for filter_index in range(200):
		print(filter_index)
		# Loss function that maximizes the activation
		# of the nth filter of the layer considered
		layer_output = layer_dict[layer_name].output
		loss = K.mean(layer_output[:, :, :, filter_index])
		# Placeholder for the input images
		input_img = model.input
		# Dimensions of the generated pictures for each filter.
		img_width = 128
		img_height = 128
		# Compute the gradient of the input picture wrt this loss
		grads = K.gradients(loss, input_img)[0]
		# Normalize the gradient
		grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)
		# This function returns the loss and grads given the input picture
		iterate = K.function([input_img], [loss, grads])
		# Step size for gradient ascent
		step = 1
		# Start with a random gray image
		input_img_data = np.random.random((1, img_width, img_height, 3)) * 20 + 128
		# run gradient ascent for 20 steps
		for i in range(20):
		    loss_value, grads_value = iterate([input_img_data])
		    input_img_data += grads_value * step
		    print('Current loss value:', loss_value)
		    if loss_value <= 0.:
		    	# some filters get stuck to 0, we can skip them
		    	break
		# Append generated image
		if loss_value > 0:
			img = deprocess_image(input_img_data[0])
			kept_filters.append((img, loss_value))
	# Stich the best 16 filters on a 4 x 4 grid
	n = 4
	# The filters that have the highest loss are assumed to be better-looking.
	# Keep the best 64 filters.
	kept_filters.sort(key=lambda x: x[1], reverse=True)
	kept_filters = kept_filters[:n * n]
	# Build a black picture with enough space for
	# all 4 x 4 filters of size 128 x 128, with a 5px margin in between
	margin = 5
	width = n * img_width + (n - 1) * margin
	height = n * img_height + (n - 1) * margin
	stitched_filters = np.zeros((width, height, 3))
	# Fill the picture with the saved filters
	for i in range(n):
	    for j in range(n):
	        img, loss = kept_filters[i * n + j]
	        stitched_filters[(img_width + margin) * i: (img_width + margin) * i + img_width,
	                         (img_height + margin) * j: (img_height + margin) * j + img_height, :] = img
	# Save the result to disk
	imsave('mser_classifier_stitched_filters_%dx%d.png' % (n, n), stitched_filters)
Example #17
0
    def call(self, x, mask=None):
        x = K.permute_dimensions(x, (0, 2, 1))
        x = K.reshape(x, (-1, self.input_length))
        x = K.expand_dims(x, 1)
        x = K.expand_dims(x, -1)
        if self.real_filts is not None:
            conv_out_r = K.conv2d(x, self.W_r, strides=self.subsample,
                                  border_mode=self.border_mode,
                                  dim_ordering='th')
        else:
            conv_out_r = x

        if self.complex_filts is not None:
            conv_out_c1 = K.conv2d(x, self.W_c1, strides=self.subsample,
                                   border_mode=self.border_mode,
                                   dim_ordering='th')
            conv_out_c2 = K.conv2d(x, self.W_c2, strides=self.subsample,
                                   border_mode=self.border_mode,
                                   dim_ordering='th')
            conv_out_c = K.sqrt(K.square(conv_out_c1) + K.square(conv_out_c2) + K.epsilon())
            output = K.concatenate((conv_out_r, conv_out_c), axis=1)
        else:
            output = conv_out_r

        output_shape = self.get_output_shape_for((None, self.input_length, self.input_dim))
        output = K.squeeze(output, 3)  # remove the dummy 3rd dimension
        output = K.permute_dimensions(output, (2, 1, 0))
        output = K.reshape(output, (-1, output_shape[1], output.shape[1]*output.shape[2]))
        return output
    def call(self, x):
        # sample from noise distribution
        e_i = K.random_normal((self.input_dim, self.units))
        e_j = K.random_normal((self.units,))

        # We use the factorized Gaussian noise variant from Section 3 of Fortunato et al.
        eW = K.sign(e_i) * (K.sqrt(K.abs(e_i))) * K.sign(e_j) * (K.sqrt(K.abs(e_j)))
        eB = K.sign(e_j) * (K.abs(e_j) ** (1 / 2))

        noise_injected_weights = K.dot(x, self.mu_weight + (self.sigma_weight * eW))
        noise_injected_bias = self.mu_bias + (self.sigma_bias * eB)

        output = K.bias_add(noise_injected_weights, noise_injected_bias)
        if self.activation is not None:
            output = self.activation(output)
        return output
Example #19
0
 def func(y_true, y_pred):
     Gxx = _get_kernel(y_true, y_true)
     Gzz = _get_kernel(y_pred, y_pred)
     Gxz = _get_kernel(y_true, y_pred)
     cost = K.log(K.sqrt(K.mean(Gxx)*K.mean(Gzz)) /
                  K.mean(Gxz))
     return cost
Example #20
0
    def __init__(self, n_units, sgd_lr, gamma, memory_length):
        nD, nV = n_units[0], n_units[-1]
        Wreg_l2 = 1e-6
        drop_proba = 0.2
        self.gamma = gamma
        self.train_batch_size = 128
        self.mem = PlayMemory(memory_length, nD)
        self.train_count = 0

        s = Input(shape=(nD,))
        a = Input(shape=(1,), dtype='uint8')

        z = s
        for nH in n_units[1:-1]:
            z = Dense(nH, W_regularizer=l2(Wreg_l2))(z)
            z = BatchNormalization()(z)
            z = PReLU()(z)
            z = Dropout(drop_proba)(z)
        z = Dense(nV + 1, W_regularizer=l2(Wreg_l2), init='zero')(z)

        self.Q_model = Model(input=s, output=DuelQ()(z))

        self.train_model = Model(input=[s, a], output=DuelQa()([z, a]))
        self.train_model.compile(
            loss=lambda y_true, y_pred: K.sqrt((y_true - y_pred)**2 + 1) - 1,
            optimizer=RMSprop(lr=sgd_lr))
Example #21
0
def gradient_penalty_loss(y_true, y_pred, averaged_samples, gradient_penalty_weight):
    """Calculates the gradient penalty loss for a batch of "averaged" samples.

    In Improved WGANs, the 1-Lipschitz constraint is enforced by adding a term to the loss function
    that penalizes the network if the gradient norm moves away from 1. However, it is impossible to evaluate
    this function at all points in the input space. The compromise used in the paper is to choose random points
    on the lines between real and generated samples, and check the gradients at these points. Note that it is the
    gradient w.r.t. the input averaged samples, not the weights of the discriminator, that we're penalizing!

    In order to evaluate the gradients, we must first run samples through the generator and evaluate the loss.
    Then we get the gradients of the discriminator w.r.t. the input averaged samples.
    The l2 norm and penalty can then be calculated for this gradient.

    Note that this loss function requires the original averaged samples as input, but Keras only supports passing
    y_true and y_pred to loss functions. To get around this, we make a partial() of the function with the
    averaged_samples argument, and use that for model training."""
    # first get the gradients:
    #   assuming: - that y_pred has dimensions (batch_size, 1)
    #             - averaged_samples has dimensions (batch_size, nbr_features)
    # gradients afterwards has dimension (batch_size, nbr_features), basically
    # a list of nbr_features-dimensional gradient vectors
    gradients = K.gradients(y_pred, averaged_samples)[0]
    # compute the euclidean norm by squaring ...
    gradients_sqr = K.square(gradients)
    #   ... summing over the rows ...
    gradients_sqr_sum = K.sum(gradients_sqr,
                              axis=np.arange(1, len(gradients_sqr.shape)))
    #   ... and sqrt
    gradient_l2_norm = K.sqrt(gradients_sqr_sum)
    # compute lambda * (1 - ||grad||)^2 still for each single sample
    gradient_penalty = gradient_penalty_weight * K.square(1 - gradient_l2_norm)
    # return the mean as loss over all the batch samples
    return K.mean(gradient_penalty)
Example #22
0
    def call(self, x, mask=None):
        input_shape = self.input_spec[0].shape

        reduction_axes = list(range(len(input_shape)))
        del reduction_axes[self.axis]
        broadcast_shape = [1] * len(input_shape)
        broadcast_shape[self.axis] = input_shape[self.axis]

        # case: train mode (uses stats of the current batch)
        mean = K.mean(x, axis=reduction_axes)
        brodcast_mean = K.reshape(mean, broadcast_shape)
        std = K.mean(K.square(x - brodcast_mean) + self.epsilon, axis=reduction_axes)
        std = K.sqrt(std)
        brodcast_std = K.reshape(std, broadcast_shape)
        mean_update = self.momentum * self.running_mean + (1 - self.momentum) * mean
        std_update = self.momentum * self.running_std + (1 - self.momentum) * std
        self.updates = [(self.running_mean, mean_update),
                        (self.running_std, std_update)]
        x_normed = (x - brodcast_mean) / (brodcast_std + self.epsilon)

        # case: test mode (uses running averages)
        brodcast_running_mean = K.reshape(self.running_mean, broadcast_shape)
        brodcast_running_std = K.reshape(self.running_std, broadcast_shape)
        x_normed_running = ((x - brodcast_running_mean) / (brodcast_running_std + self.epsilon))

        # pick the normalized form of x corresponding to the training phase
        x_normed = K.in_train_phase(x_normed, x_normed_running)
        out = K.reshape(self.gamma, broadcast_shape) * x_normed + K.reshape(self.beta, broadcast_shape)

        return out
Example #23
0
def loss_DSSIM_theano(y_true, y_pred):
    patches_true = T.nnet.neighbours.images2neibs(y_true, [4, 4])
    patches_pred = T.nnet.neighbours.images2neibs(y_pred, [4, 4])
    u_true = K.mean(patches_true, axis=-1)
    u_pred = K.mean(patches_pred, axis=-1)
    var_true = K.var(patches_true, axis=-1)
    var_pred = K.var(patches_pred, axis=-1)
    eps = 1e-9
    std_true = K.sqrt(var_true + eps)
    std_pred = K.sqrt(var_pred + eps)
    c1 = 0.01 ** 2
    c2 = 0.03 ** 2
    ssim = (2 * u_true * u_pred + c1) * (2 * std_pred * std_true + c2)
    denom = (u_true ** 2 + u_pred ** 2 + c1) * (var_pred + var_true + c2)
    ssim /= denom  # no need for clipping, c1 and c2 make the denom non-zero
    return K.mean((1.0 - ssim) / 2.0)
Example #24
0
def root_mean_squared_logarithmic_loss(y_true, y_pred):
    y_true = tf.Print(y_true, [y_true], message='y_true', summarize=30)
    y_pred = tf.Print(y_pred, [y_pred], message='y_pred', summarize=30)
    first_log = K.log(K.clip(y_pred, K.epsilon(), None) + 1.)
    second_log = K.log(K.clip(y_true, K.epsilon(), None) + 1.)

    return K.sqrt(K.mean(K.square(first_log - second_log), axis=-1)+0.00001)
def make_patches_grid(x, patch_size, patch_stride):
    '''Break image `x` up into a grid of patches.

    input shape: (channels, rows, cols)
    output shape: (rows, cols, channels, patch_rows, patch_cols)
    '''
    from theano.tensor.nnet.neighbours import images2neibs  # TODO: all K, no T
    x = K.expand_dims(x, 0)
    xs = K.shape(x)
    num_rows = 1 + (xs[-2] - patch_size) // patch_stride
    num_cols = 1 + (xs[-1] - patch_size) // patch_stride
    num_channels = xs[-3]
    patches = images2neibs(
        x, (patch_size, patch_size), (patch_stride, patch_stride),
        mode='valid')
    # neibs are sorted per-channel
    patches = K.reshape(patches,
                        (num_channels, K.shape(patches)[0] // num_channels,
                         patch_size, patch_size))
    patches = K.permute_dimensions(patches, (1, 0, 2, 3))
    # arrange in a 2d-grid (rows, cols, channels, px, py)
    patches = K.reshape(
        patches, (num_rows, num_cols, num_channels, patch_size, patch_size))
    patches_norm = K.sqrt(
        K.sum(K.square(patches), axis=(2, 3, 4), keepdims=True))
    return patches, patches_norm
Example #26
0
    def visualize_layer(self, layer, model, keep_filters, out_path, vis_size):
        layer_output = layer.output
        num_filters = layer.nb_filter
        filters = []
        img_width = vis_size[0]
        img_height = vis_size[1]

        for filter_index in xrange(num_filters):
            loss = K.mean(K.mean(layer_output[:, filter_index, :, :]))

            # compute the gradient of the input picture wrt this loss
            grads = K.gradients(loss, model.layers[0].input)[0]

            # normalization trick: we normalize the gradient
            grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)

            # this function returns the loss and grads given the input picture
            iterate = K.function([model.layers[0].input], [loss, grads])

            # step size for gradient ascent
            step = 1.

            input_img_data = np.random.random((1, 1, img_width, img_height)) * 20 + 128.

            for i in xrange(50):
                loss_value, grads_value = iterate([input_img_data])
                input_img_data += grads_value * step

                if loss_value <= 0:
                    break

            img = self.deprocess_image(input_img_data[0])
            filters.append((img, loss_value))

        filters.sort(key=lambda x: x[1], reverse=True)
        filters = filters[:keep_filters]

        # get number of grid rows and columns
        grid_r, grid_c = helpers.get_grid_dim(keep_filters)

        # create figure and axes
        fig, axes = plt.subplots(min([grid_r, grid_c]),
                                 max([grid_r, grid_c]))


        # iterate filters inside every channel
        for l, ax in enumerate(axes.flat):
            # get a single filter
            img = filters[l][0][:, :, 0]
            # put it on the grid
            ax.imshow(img, interpolation='bicubic', cmap='Greys')
            # remove any labels from the axes
            ax.set_xticks([])
            ax.set_yticks([])
        # save figure
        out_path = os.path.join(out_path, layer.name) + '.png'
        plt.savefig(out_path, bbox_inches='tight')

        """
 def get_gradients(self, loss, params):
     grads = K.gradients(loss, params)
     if hasattr(self, 'clipnorm') and self.clipnorm > 0:
         norm = K.sqrt(sum([K.sum(K.square(g)) for g in grads]))
         grads = [clip_norm(g, self.clipnorm, norm) for g in grads]
     if hasattr(self, 'clipvalue') and self.clipvalue > 0:
         grads = [K.clip(g, -self.clipvalue, self.clipvalue) for g in grads]
     return grads
Example #28
0
 def call(self, x, mask=None):
     sin = x[:, self.sin_idx : self.sin_idx + 1]
     cos = x[:, self.cos_idx : self.cos_idx + 1]
     eps = 1e-7
     scale = K.sqrt(1.0 / (eps + sin ** 2 + cos ** 2))
     sin_scaled = K.clip(scale * sin, -1, 1)
     cos_scaled = K.clip(scale * cos, -1, 1)
     return K.concatenate([x[:, : self.sin_idx], sin_scaled, cos_scaled, x[:, self.cos_idx + 1 :]], axis=1)
Example #29
0
    def __call__(self, p):
        # if self.data_format == 'channels_last':
        rotate_axis = list(range(K.ndim(p)))
        rotate_axis = [rotate_axis[-1]] + rotate_axis[:-1]
        p = K.permute_dimensions(p, rotate_axis)

        sumaxes = tuple(range(1, K.ndim(p)))

        if self.interleave:
            if self.singles:
                v = p[::3]
                w = p[1::3]
            else:
                v = p[::2]
                w = p[1::2]
        else:
            v = p[:self.m]
            w = p[self.m:2*self.m]

        v2 = w - v*K.sum(v*w, axis=sumaxes, keepdims=True)/K.sum(v*v, axis=sumaxes, keepdims=True)

        # norms_paired = K.sqrt(K.sum(v**2 + v2**2, axis=sumaxes, keepdims=True))
        # v /= norms_paired
        # v2 /= norms_paired
        norms_v = K.sqrt(K.sum(v**2, axis=sumaxes, keepdims=True))
        v /= norms_v
        norms_v2 = K.sqrt(K.sum(v2**2, axis=sumaxes, keepdims=True))
        v2 /= norms_v2

        if self.singles:
            if self.interleave:
                x = p[2::3]
            else:
                x = p[2*self.m:]
            norms_single = K.sqrt(K.sum(x**2, axis=sumaxes, keepdims=True))
            x /= norms_single
            out = K.concatenate((v, v2, x), axis=0)
        else:
            out = K.concatenate((v, v2), axis=0)

        # if self.dim_ordering == 'tf':
        rotate_axis = list(range(K.ndim(out)))
        rotate_axis = rotate_axis[1:] + [rotate_axis[0]]
        out = K.permute_dimensions(out, rotate_axis)
        return out
Example #30
0
 def euclidian_dist(self, x_pair):
     x1_norm = K.l2_normalize(x_pair[0], axis=1)
     x2_norm = K.l2_normalize(x_pair[1], axis=1)
     diff = x1_norm - x2_norm
     square = K.square(diff)
     sum = K.sum(square, axis=1)
     sum = K.clip(sum, min_value=1e-12, max_value=None)
     dist = K.sqrt(sum)
     return dist
def root_mean_squared_error(y_true, y_pred):
    return K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1))
def omega_distance(signals, protos, omegas,
                   order_p=2,
                   squared=False,
                   epsilon=K.epsilon()):
    # Note: omegas is always assumed as transposed!
    # shape(signals): batch x proto_number x channels x dim1 x dim2 x ... x dimN
    # shape(protos): proto_number x dim1 x dim2 x ... x dimN
    # shape(omegas): (optional [proto_number]) x prod(dim1 * dim2 * ... * dimN)  x prod(projected_atom_shape)
    #
    # the speedup with sparse_signals is only possible if order_p == 2 otherwise the identity between the distance and
    # the dot product is not valid
    signal_shape, signal_int_shape = _int_and_mixed_shape(signals)
    proto_shape, proto_int_shape = _int_and_mixed_shape(protos)

    # check if the shapes are correct
    _check_shapes(signal_int_shape, proto_int_shape)

    with K.name_scope('omega_distance'):
        # for sparse signals, we use the memory efficient implementation
        if signal_int_shape[1] == 1 and order_p == 2:
            # clean solution without map_fn if the matrix_scope is global
            atom_axes = list(range(3, len(signal_int_shape)))
            signals = K.reshape(signals, [-1, np.prod(signal_shape[3:])])

            if len(atom_axes) > 1:
                protos = K.reshape(protos, [proto_shape[0], -1])

            if K.ndim(omegas) == 2:
                with K.name_scope('omega_projections'):
                    projected_signals = K.dot(signals, omegas)
                    projected_protos = K.dot(protos, omegas)

                diss = euclidean_distance(projected_signals, projected_protos, squared=squared, epsilon=epsilon)

                diss = K.reshape(diss, [signal_shape[0], signal_shape[2], proto_shape[0]])

                return K.permute_dimensions(diss, [0, 2, 1])

            else:
                # no solution without map_fn possible --> memory efficient but slow!
                with K.name_scope('omega_projections'):
                    squared_omegas = K.batch_dot(omegas, omegas, [2, 2])
                    squared_projected_protos = K.batch_dot(protos, squared_omegas, [1, 2])
                    projected_protos = K.batch_dot(omegas, protos, [1, 1])

                with K.name_scope('euclidean_distance'):
                    def projected_norm(omega):
                        return K.sum(K.square(K.dot(signals, omega)), axis=1)

                    diss = K.transpose(K.map_fn(projected_norm, omegas)) \
                           - 2 * K.dot(signals, K.transpose(squared_projected_protos)) \
                           + K.sum(K.square(K.transpose(projected_protos)), axis=0, keepdims=True)

                    if not squared:
                        if epsilon == 0:
                            diss = K.sqrt(diss)
                        else:
                            diss = K.sqrt(K.maximum(diss, epsilon))

                diss = K.reshape(diss, [signal_shape[0], signal_shape[2], proto_shape[0]])

                return K.permute_dimensions(diss, [0, 2, 1])

        else:
            projected_atom_shape = K.int_shape(omegas)[-1]

            signals = K.permute_dimensions(signals, [0, 2, 1] + list(range(3, len(signal_shape))))
            diff = signals - protos

            if K.ndim(omegas) == 2:
                with K.name_scope('omega_projections'):
                    diff = K.reshape(diff, (signal_shape[0] * signal_shape[2], proto_shape[0], -1))
                    projected_diff = K.dot(diff, omegas)
                    projected_diff = K.reshape(projected_diff,
                                               (signal_shape[0], signal_shape[2],
                                                proto_shape[0], projected_atom_shape))

                diss = p_norm(projected_diff, order_p=order_p, axis=3, squared=squared, keepdims=False, epsilon=epsilon)
                return K.permute_dimensions(diss, [0, 2, 1])

            else:
                with K.name_scope('omega_projections'):
                    diff = K.reshape(diff, (signal_shape[0] * signal_shape[2], proto_shape[0], -1))
                    diff = K.permute_dimensions(diff, [1, 0, 2])
                    projected_diff = K.batch_dot(diff, omegas)
                    projected_diff = K.reshape(projected_diff,
                                               (proto_shape[0], signal_shape[0],
                                                signal_shape[2], projected_atom_shape))

                diss = p_norm(projected_diff, order_p=order_p, axis=3, squared=squared, keepdims=False, epsilon=epsilon)
                return K.permute_dimensions(diss, [1, 0, 2])
Example #33
0
    def get_updates(self, loss, params):
        grads = self.get_gradients(loss, params)
        self.updates = [K.update_add(self.iterations, 1)]
        self.updates.append(K.update_add(self.t_cur, 1))

        t = K.cast(self.iterations, K.floatx()) + 1

        # Due to the recommendations in [2], i.e. warming momentum schedule
        momentum_cache_t = self.beta_1 * (
            1. - 0.5 *
            (K.pow(K.cast_to_floatx(0.96), t * self.schedule_decay)))
        momentum_cache_t_1 = self.beta_1 * (
            1. - 0.5 * (K.pow(K.cast_to_floatx(0.96),
                              (t + 1) * self.schedule_decay)))
        m_schedule_new = self.m_schedule * momentum_cache_t
        m_schedule_next = self.m_schedule * momentum_cache_t * momentum_cache_t_1
        self.updates.append((self.m_schedule, m_schedule_new))

        shapes = [K.int_shape(p) for p in params]
        ms = [K.zeros(shape) for shape in shapes]
        vs = [K.zeros(shape) for shape in shapes]

        self.weights = [self.iterations] + ms + vs

        total_iterations = self.total_iterations
        # Cosine annealing
        if self.use_cosine_annealing and total_iterations != 0:
            self.eta_t = _compute_eta_t(self)
        self.lr_t = self.lr * self.eta_t  # for external tracking

        for p, g, m, v in zip(params, grads, ms, vs):
            # Learning rate multipliers
            lr_t = self.lr
            if self.lr_multipliers is not None:
                lr_t = _apply_lr_multiplier(self, lr_t, p)

            # the following equations given in [1]
            g_prime = g / (1. - m_schedule_new)
            m_t = self.beta_1 * m + (1. - self.beta_1) * g
            m_t_prime = m_t / (1. - m_schedule_next)
            v_t = self.beta_2 * v + (1. - self.beta_2) * K.square(g)
            v_t_prime = v_t / (1. - K.pow(self.beta_2, t))
            m_t_bar = (1. - momentum_cache_t) * g_prime + (momentum_cache_t_1 *
                                                           m_t_prime)

            self.updates.append(K.update(m, m_t))
            self.updates.append(K.update(v, v_t))
            p_t = p - self.eta_t * lr_t * m_t_bar / (K.sqrt(v_t_prime) +
                                                     self.epsilon)

            # Weight decays
            if p.name in self.weight_decays.keys() and total_iterations != 0:
                p_t = _apply_weight_decays(self, p, p_t)
            new_p = p_t

            # Apply constraints.
            if getattr(p, 'constraint', None) is not None:
                new_p = p.constraint(new_p)

            self.updates.append(K.update(p, new_p))

        self._init_notified = True
        return self.updates
def normalize(x):
    # utility function to normalize a tensor by its L2 norm
    return x / (K.sqrt(K.mean(K.square(x))) + 1e-5)
def error_metric(y_true, y_pred):
    return K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1))
def tangent_distance(signals, protos, subspaces,
                     squared=False,
                     epsilon=K.epsilon()):
    # Note: subspaces is always assumed as transposed and must be orthogonal!
    # shape(signals): batch x proto_number x channels x dim1 x dim2 x ... x dimN
    # shape(protos): proto_number x dim1 x dim2 x ... x dimN
    # shape(subspaces): (optional [proto_number]) x prod(dim1 * dim2 * ... * dimN)  x prod(projected_atom_shape)
    # subspace should be orthogonalized

    signal_shape, signal_int_shape = _int_and_mixed_shape(signals)
    proto_shape, proto_int_shape = _int_and_mixed_shape(protos)
    subspace_int_shape = K.int_shape(subspaces)

    # check if the shapes are correct
    _check_shapes(signal_int_shape, proto_int_shape)

    with K.name_scope('tangent_distance'):
        atom_axes = list(range(3, len(signal_int_shape)))
        # for sparse signals, we use the memory efficient implementation
        if signal_int_shape[1] == 1:
            signals = K.reshape(signals, [-1, np.prod(signal_shape[3:])])

            if len(atom_axes) > 1:
                protos = K.reshape(protos, [proto_shape[0], -1])

            if K.ndim(subspaces) == 2:
                # clean solution without map_fn if the matrix_scope is global
                with K.name_scope('projectors'):
                    projectors = K.eye(subspace_int_shape[-2]) - K.dot(subspaces, K.transpose(subspaces))

                with K.name_scope('tangentspace_projections'):
                    projected_signals = K.dot(signals, projectors)
                    projected_protos = K.dot(protos, projectors)

                diss = euclidean_distance(projected_signals, projected_protos, squared=squared, epsilon=epsilon)

                diss = K.reshape(diss, [signal_shape[0], signal_shape[2], proto_shape[0]])

                return K.permute_dimensions(diss, [0, 2, 1])

            else:
                # no solution without map_fn possible --> memory efficient but slow!
                with K.name_scope('projectors'):
                    projectors = K.eye(subspace_int_shape[-2]) - K.batch_dot(subspaces, subspaces, [2, 2])

                with K.name_scope('tangentspace_projections'):
                    projected_protos = K.transpose(K.batch_dot(projectors, protos, [1, 1]))

                with K.name_scope('euclidean_distance'):
                    def projected_norm(projector):
                        return K.sum(K.square(K.dot(signals, projector)), axis=1)

                    diss = K.transpose(K.map_fn(projected_norm, projectors)) \
                           - 2 * K.dot(signals, projected_protos) \
                           + K.sum(K.square(projected_protos), axis=0, keepdims=True)

                    if not squared:
                        if epsilon == 0:
                            diss = K.sqrt(diss)
                        else:
                            diss = K.sqrt(K.maximum(diss, epsilon))

                diss = K.reshape(diss, [signal_shape[0], signal_shape[2], proto_shape[0]])

                return K.permute_dimensions(diss, [0, 2, 1])

        else:
            signals = K.permute_dimensions(signals, [0, 2, 1] + atom_axes)
            diff = signals - protos

            # global tangent space
            if K.ndim(subspaces) == 2:
                with K.name_scope('projectors'):
                    projectors = K.eye(subspace_int_shape[-2]) - K.dot(subspaces, K.transpose(subspaces))

                with K.name_scope('tangentspace_projections'):
                    diff = K.reshape(diff, (signal_shape[0] * signal_shape[2], signal_shape[1], -1))
                    projected_diff = K.dot(diff, projectors)
                    projected_diff = K.reshape(projected_diff,
                                               (signal_shape[0], signal_shape[2], signal_shape[1]) + signal_shape[3:])

                diss = p_norm(projected_diff, order_p=2, axis=atom_axes, squared=squared, keepdims=False, epsilon=epsilon)
                return K.permute_dimensions(diss, [0, 2, 1])

            # local tangent spaces
            else:
                with K.name_scope('projectors'):
                    projectors = K.eye(subspace_int_shape[-2]) - K.batch_dot(subspaces, subspaces, [2, 2])

                with K.name_scope('tangentspace_projections'):
                    diff = K.reshape(diff, (signal_shape[0] * signal_shape[2], signal_shape[1], -1))
                    diff = K.permute_dimensions(diff, [1, 0, 2])
                    projected_diff = K.batch_dot(diff, projectors)
                    projected_diff = K.reshape(projected_diff,
                                               (signal_shape[1], signal_shape[0], signal_shape[2]) + signal_shape[3:])

                diss = p_norm(projected_diff, order_p=2, axis=atom_axes, squared=squared, keepdims=False, epsilon=epsilon)
                return K.permute_dimensions(diss, [1, 0, 2])
Example #37
0
def euclidean_distance(vects):
    x, y = vects
    sum_square = K.sum(K.square(x - y), axis=1, keepdims=True)
    return K.sqrt(K.maximum(sum_square, K.epsilon()))
Example #38
0
g_model.trainable = False


def interpolating(x):
    u = K.random_uniform((K.shape(x[0])[0], ) + (1, ) * (K.ndim(x[0]) - 1))
    return x[0] * u + x[1] * (1 - u)


x_fake = g_model(z_in)
x_inter = Lambda(interpolating)([x_in, x_fake])
x_real_score = d_model(x_in)
x_fake_score = d_model(x_fake)
x_inter_score = d_model(x_inter)

grads = K.gradients(x_inter_score, [x_inter])[0]
grad_norms = K.sqrt(K.sum(grads**2, range(1, K.ndim(grads))) + 1e-9)

d_train_model = Model([x_in, z_in],
                      [x_real_score, x_fake_score, x_inter_score])

d_loss = K.mean(-(x_real_score - x_fake_score)) + 10 * K.mean(
    (grad_norms - 1)**2)
d_train_model.add_loss(d_loss)
d_train_model.compile(optimizer=Adam(2e-4, 0.5))

# 整合模型(训练生成器)
g_model.trainable = True
d_model.trainable = False
x_fake_score = d_model(g_model(z_in))

g_train_model = Model(z_in, x_fake_score)
Example #39
0
 def normalize(x):
     return x / (K.sqrt(K.mean(K.square(x))) + K.epsilon())
Example #40
0
def corr(y_true, y_pred):
    cov = covariance(y_true, y_pred)
    var1 = covariance(y_true, y_true)
    var2 = covariance(y_pred, y_pred)
    return cov / (K.sqrt(var1 * var2) + K.epsilon())
Example #41
0
    plt.figure(figsize=(scale * display_grid.shape[1],
                        scale * display_grid.shape[0]))
    plt.title(layer_name)
    plt.grid(False)
    plt.imshow(display_grid, aspect='auto', cmap='viridis')

plt.show()
from keras.applications import VGG16
from keras import backend as K
model = VGG16(weights='imagenet', include_top=False)
layer_name = 'block3_conv1'
filter_index = 0
layer_output = model.get_layer(layer_name).output
loss = K.mean(layer_output[:, :, :, filter_index])
grads = K.gradients(loss, model.input)[0]
grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)
iterate = K.function([model.input], [loss, grads])
import numpy as np
loss_value, grads_value = iterate([np.zeros((1, 150, 150, 3))])
input_img_data = np.random.random((1, 150, 150, 3)) * 20 + 128.
step = 1.  # this is the magnitude of each gradient update
for i in range(40):
    # Compute the loss value and gradient value
    loss_value, grads_value = iterate([input_img_data])
    # Here we adjust the input image in the direction that maximizes the loss
    input_img_data += grads_value * step


def deprocess_image(x):
    # normalize tensor: center on 0., ensure std is 0.1
    x -= x.mean()
def earth_movers_distance(y_true, y_pred):
    cdf_true = K.cumsum(y_true, axis=-1)
    cdf_pred = K.cumsum(y_pred, axis=-1)
    emd = K.sqrt(K.mean(K.square(cdf_true - cdf_pred), axis=-1))
    return K.mean(emd)
Example #43
0
def sqrt(shape, dtype=None):
    value = (1 / K.sqrt(2)) * K.ones(shape)
    return value
Example #44
0
def _lorentz(x, boosts, weights=None, sphereCoords=False, sum_input=False):
    ''' Outputs a backend variable that Calculates the vectorial sum of 4-vectors
        boosted individually into different reference frames
    '''
    if (sum_input):
        x = K.sum(x, axis=1)
        x_shape = K.shape(x)
        x = K.reshape(x, (x_shape[0], 1, x_shape[1]))
    # K.clip(boosts, 0.0, .33-_EPSILON)
    #Initialize Helpful variables
    x_shape = K.shape(x)
    # printed_x = theano.printing.Print('this is a very important value')(x_shape)
    # f = function([x], printed_x)

    batch_size = x_shape[0]
    vector_cluster_size = x_shape[1]
    _bI = K.repeat_elements(K.reshape(K.eye(4), (1, 4, 4)),
                            vector_cluster_size,
                            axis=0)
    _b1 = K.repeat_elements(K.eye(1), vector_cluster_size, axis=0)

    #Get _mag and _n from boost which can be formatted in either
    # Cartesian or Spherical coordinates
    if (sphereCoords):
        #split up the boost by components. dtype='int64' is to cast to a theano.tensor.lvector
        _splits = K.variable([1, 1, 1],
                             dtype='int64')  #theano.tensor.lvector([1,1,1])
        _theta, _phi, _mag = theano.tensor.split(boosts, _splits, 3, axis=1)
        _mag = K.clip(_mag, _EPSILON, 1.0 - _EPSILON)
        _theta = _theta * np.pi
        _phi = _phi * (2 * np.pi)
        _nx = K.sin(_theta) * K.cos(_phi)
        _ny = K.sin(_theta) * K.sin(_phi)
        _nz = K.cos(_theta)
        _n = K.concatenate([_nx, _ny, _nz], axis=1)
    else:
        _mag = K.sqrt(K.sum(K.square(boosts), axis=1, keepdims=True))
        _mag = K.clip(_mag, _EPSILON, 1.0 - _EPSILON)
        _inv_mag = 1 / _mag
        _n = boosts * _inv_mag

    #Calculate the Lorentz factor of the boost
    _sqrMag = K.square(_mag)
    _g = 1 / K.sqrt((1. - _sqrMag))

    #Repeat the K tensor b=vector_cluster_size times
    _bK = K.reshape(_K, (1, 3, 4, 4))
    _bK = K.repeat_elements(_bK, vector_cluster_size, axis=0)
    #Dot K with n for each cluster vector to get _nk = Bxnx + Byny + Bznz
    _nK = K.batch_dot(_n, _bK, axes=[[1], [1]])
    #Reshape _nk so that we can batch dot it along the [1] axis
    _nK = K.reshape(_nK, (vector_cluster_size, 1, 4, 4))
    #Square _nK and reshape it correctly for each cluster vector
    _nK2 = K.reshape(K.batch_dot(_nK, _nK), (vector_cluster_size, 1, 4, 4))
    #Calculate the boost matrix
    _B = _bI - K.batch_dot(_g * _mag, _nK, axes=[[1], [1]]) + K.batch_dot(
        _g - _b1, _nK2, axes=[[1], [1]])

    #Apply trained weights to each Boost in the cluster
    if (weights != None):
        _B = K.reshape(_B, (vector_cluster_size, 1, 4, 4))
        weights = K.reshape(weights, (vector_cluster_size, 1, 1, 1))
        _B = K.batch_dot(weights, _B, axes=[[1], [1]])

    # print("Total size:", batch_size* vector_cluster_size)
    #Reshape x and _B so we can dot them along the cluster axis
    x = K.reshape(x, (batch_size * vector_cluster_size, 4))
    _B = K.reshape(_B, (1, vector_cluster_size, 4, 4))
    _mB = K.repeat_elements(_B, batch_size, axis=0)
    _mB = K.reshape(_mB, (batch_size * vector_cluster_size, 4, 4))
    #Dot x and _B along the cluster axis to give the summed result of the boosted vectors
    dot = K.batch_dot(_mB, x, axes=[[1], [1]])
    dot = K.reshape(dot, (batch_size, vector_cluster_size, 4))

    # #Reshape x and _B so we can dot them along the cluster axis
    # x = K.reshape(x, (batch_size, vector_cluster_size, 1, 4))
    # _B = K.reshape(_B, (1,vector_cluster_size,4,4))
    # _mB = K.repeat_elements(_B,batch_size, axis=0)

    # #Dot x and _B along the cluster axis to give the summed result of the boosted vectors
    # out = K.reshape(K.batch_dot(_mB, x, axes=[[1,3],[1,3]]), (batch_size, 1,4))
    return dot
Example #45
0
def euclidean_distance(vects):
    x, y = vects
    ##K.epsilon()以数值形式返回一个(一般来说很小的)数,用以防止除0错误
    return K.sqrt(
        K.maximum(K.sum(K.square(x - y), axis=1, keepdims=True), K.epsilon()))
Example #46
0
def rmse(y_true, y_pred):
    return K.sqrt(K.mean(K.square(y_pred - y_true), axis=-1))
Example #47
0
 def call(self, inputs, **kwargs):
     return K.sqrt(K.sum(K.square(inputs), -1))
Example #48
0
def rmse(y_true, y_pred):
    from keras import backend
    return backend.sqrt(backend.mean(backend.square(y_pred - y_true), axis=-1))
Example #49
0
def euclidean_distance(vects):
    x, y = vects
    return K.sqrt(K.sum(K.square(x - y), axis=1, keepdims=True))
Example #50
0
def euclidean_distance(vects):
    x,y=vects
    return k.sqrt(k.maximum(k.sum(k.square(x-y),axis=1,keepdims=True),k.epsilon()))
Example #51
0
def euclidean_distance(tensors):
    """Make euclidean distance."""
    return K.sqrt((K.square(tensors[0] - tensors[1])).sum(axis=1,
                                                          keepdims=True))
Example #52
0
def eldistance(input_1, input_2):
    "Substract element-wise"
    out_ = Lambda(lambda x: K.sqrt(K.square(x[0] - x[1])))([input_1, input_2])
    return out_
Example #53
0
def l2_reg(weight_matrix):
	return 5e-7 * K.sqrt(K.sum(K.abs(weight_matrix)**2))
 def _euclidean_dis_loss(self, y_true, y_pred):
     return K.sqrt(K.sum(K.square(y_pred - y_true), axis=0, keepdims=True))
Example #55
0
 def euclidean_content_loss(self, y_true, y_pred):
     return K.sqrt(
         K.sum(K.square(
             self.model(self.preprocess_vgg(y_true)) -
             self.model(self.preprocess_vgg(y_pred))),
               axis=None))
Example #56
0
def rmse(y_true, y_pred):
    return K.sqrt(K.mean(((y_pred - y_true)**2)))
Example #57
0
def euclidean_loss(y_true, y_pred):
    return K.sqrt(K.sum(K.square(y_pred - y_true), axis=None))
Example #58
0
def charbonnier(y_true, y_pred):
    epsilon = 1e-3
    error = y_true - y_pred
    p = K.sqrt(K.square(error) + K.square(epsilon))
    return K.mean(p)
def rmse(y_obs, y_pred):
    return backend.sqrt(backend.mean(backend.square(y_pred - y_obs), axis=-1))
Example #60
0
def hubert_loss(y_true, y_pred):
    err = y_pred - y_true
    return K.mean(K.sqrt(1 + K.square(err)) - 1, axis=-1)