Beispiel #1
0
def test_tf_map_coordinates():
    np.random.seed(42)
    input = np.random.random((100, 100))
    coords = np.random.random((10000, 2)) * 99

    sp_mapped_vals = map_coordinates(input, coords.T, order=1)
    tf_mapped_vals = tf_map_coordinates(K.variable(input), K.variable(coords))
    assert np.allclose(sp_mapped_vals, K.eval(tf_mapped_vals), atol=1e-5)
Beispiel #2
0
def test_tf_batch_map_offsets():
    np.random.seed(42)
    input = np.random.random((4, 100, 100))
    offsets = np.random.random((4, 100, 100, 2)) * 2

    sp_mapped_vals = sp_batch_map_offsets(input, offsets)
    tf_mapped_vals = tf_batch_map_offsets(K.variable(input),
                                          K.variable(offsets))
    assert np.allclose(sp_mapped_vals, K.eval(tf_mapped_vals), atol=1e-5)
Beispiel #3
0
def test_tf_batch_map_offsets_grad():
    np.random.seed(42)
    input = np.random.random((4, 100, 100))
    offsets = np.random.random((4, 100, 100, 2)) * 2

    input = K.variable(input)
    offsets = K.variable(offsets)

    tf_mapped_vals = tf_batch_map_offsets(input, offsets)
    grad = K.gradients(tf_mapped_vals, input)[0]
    grad = K.eval(grad)
    assert not np.allclose(grad, 0)
Beispiel #4
0
 def tsne(P, activations):
     #     d = K.shape(activations)[1]
     v = d - 1.
     eps = K.variable(
         10e-15
     )  # needs to be at least 10e-8 to get anything after Q /= K.sum(Q)
     sum_act = K.sum(K.square(activations), axis=1)
     Q = K.reshape(sum_act, [-1, 1]) + -2 * K.dot(activations,
                                                  K.transpose(activations))
     Q = (sum_act + Q) / v
     Q = K.pow(1 + Q, -(v + 1) / 2)
     Q *= K.variable(1 - np.eye(n))
     Q /= K.sum(Q)
     Q = K.maximum(Q, eps)
     C = K.log((P + eps) / (Q + eps))
     C = K.sum(P * C)
     return C
Beispiel #5
0
    def build(self, input_shape):
        self.input_spec = [InputSpec(shape=input_shape)]
        shape = (input_shape[self.axis], )

        self.gamma = self.add_weight(shape=shape,
                                     initializer=self.gamma_init,
                                     regularizer=self.gamma_regularizer,
                                     name='{}_gamma'.format(self.name))
        self.beta = self.add_weight(shape=shape,
                                    initializer=self.beta_init,
                                    regularizer=self.beta_regularizer,
                                    name='{}_beta'.format(self.name))
        self.running_mean = self.add_weight(shape=shape,
                                            initializer='zero',
                                            name='{}_running_mean'.format(
                                                self.name),
                                            trainable=False)
        # Note: running_std actually holds the running variance, not the running std.
        self.running_std = self.add_weight(shape=shape,
                                           initializer='one',
                                           name='{}_running_std'.format(
                                               self.name),
                                           trainable=False)

        self.r_max = K.variable(np.ones((1, )),
                                name='{}_r_max'.format(self.name))

        self.d_max = K.variable(np.zeros((1, )),
                                name='{}_d_max'.format(self.name))

        self.t = K.variable(np.zeros((1, )), name='{}_t'.format(self.name))

        if self.initial_weights is not None:
            self.set_weights(self.initial_weights)
            del self.initial_weights
        self.built = True
Beispiel #6
0
    def call(self, x, mask=None):
        if self.mode == 0 or self.mode == 2:
            assert self.built, 'Layer must be built before being called'
            input_shape = K.int_shape(x)

            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]

            mean_batch, var_batch = _moments(x,
                                             reduction_axes,
                                             shift=None,
                                             keep_dims=False)
            std_batch = (K.sqrt(var_batch + self.epsilon))

            r_max_value = K.get_value(self.r_max)
            r = std_batch / (K.sqrt(self.running_std + self.epsilon))
            r = K.stop_gradient(K.clip(r, 1 / r_max_value, r_max_value))

            d_max_value = K.get_value(self.d_max)
            d = (mean_batch - self.running_mean) / K.sqrt(self.running_std +
                                                          self.epsilon)
            d = K.stop_gradient(K.clip(d, -d_max_value, d_max_value))

            if sorted(reduction_axes) == range(K.ndim(x))[:-1]:
                x_normed_batch = (x - mean_batch) / std_batch
                x_normed = (x_normed_batch * r + d) * self.gamma + self.beta
            else:
                # need broadcasting
                broadcast_mean = K.reshape(mean_batch, broadcast_shape)
                broadcast_std = K.reshape(std_batch, broadcast_shape)
                broadcast_r = K.reshape(r, broadcast_shape)
                broadcast_d = K.reshape(d, broadcast_shape)
                broadcast_beta = K.reshape(self.beta, broadcast_shape)
                broadcast_gamma = K.reshape(self.gamma, broadcast_shape)

                x_normed_batch = (x - broadcast_mean) / broadcast_std
                x_normed = (x_normed_batch * broadcast_r +
                            broadcast_d) * broadcast_gamma + broadcast_beta

            # explicit update to moving mean and standard deviation
            self.add_update([
                K.moving_average_update(self.running_mean, mean_batch,
                                        self.momentum),
                K.moving_average_update(self.running_std, std_batch**2,
                                        self.momentum)
            ], x)

            # update r_max and d_max
            r_val = self.r_max_value / (
                1 + (self.r_max_value - 1) * K.exp(-self.t))
            d_val = self.d_max_value / (1 + (
                (self.d_max_value / 1e-3) - 1) * K.exp(-(2 * self.t)))

            self.add_update([
                K.update(self.r_max, r_val),
                K.update(self.d_max, d_val),
                K.update_add(self.t, K.variable(np.array([self.t_delta])))
            ], x)

            if self.mode == 0:
                if sorted(reduction_axes) == range(K.ndim(x))[:-1]:
                    x_normed_running = K.batch_normalization(
                        x,
                        self.running_mean,
                        self.running_std,
                        self.beta,
                        self.gamma,
                        epsilon=self.epsilon)
                else:
                    # need broadcasting
                    broadcast_running_mean = K.reshape(self.running_mean,
                                                       broadcast_shape)
                    broadcast_running_std = K.reshape(self.running_std,
                                                      broadcast_shape)
                    broadcast_beta = K.reshape(self.beta, broadcast_shape)
                    broadcast_gamma = K.reshape(self.gamma, broadcast_shape)
                    x_normed_running = K.batch_normalization(
                        x,
                        broadcast_running_mean,
                        broadcast_running_std,
                        broadcast_beta,
                        broadcast_gamma,
                        epsilon=self.epsilon)

                # pick the normalized form of x corresponding to the training phase
                # for batch renormalization, inference time remains same as batchnorm
                x_normed = K.in_train_phase(x_normed, x_normed_running)

        elif self.mode == 1:
            # sample-wise normalization
            m = K.mean(x, axis=self.axis, keepdims=True)
            std = K.sqrt(
                K.var(x, axis=self.axis, keepdims=True) + self.epsilon)
            x_normed_batch = (x - m) / (std + self.epsilon)

            r_max_value = K.get_value(self.r_max)
            r = std / (self.running_std + self.epsilon)
            r = K.stop_gradient(K.clip(r, 1 / r_max_value, r_max_value))

            d_max_value = K.get_value(self.d_max)
            d = (m - self.running_mean) / (self.running_std + self.epsilon)
            d = K.stop_gradient(K.clip(d, -d_max_value, d_max_value))

            x_normed = ((x_normed_batch * r) + d) * self.gamma + self.beta

            # update r_max and d_max
            t_val = K.get_value(self.t)
            r_val = self.r_max_value / (
                1 + (self.r_max_value - 1) * np.exp(-t_val))
            d_val = self.d_max_value / (1 + (
                (self.d_max_value / 1e-3) - 1) * np.exp(-(2 * t_val)))
            t_val += float(self.t_delta)

            self.add_update([
                K.update(self.r_max, r_val),
                K.update(self.d_max, d_val),
                K.update(self.t, t_val)
            ], x)

        return x_normed
        img = img.reshape((3, img_h, img_w))
        img = img.transpose((1, 2, 0))
    else:
        img = img.reshape((img_h, img_w, 3))
    # Remove zero-center by mean pixel
    img[:, :, 0] += 103.939
    img[:, :, 1] += 116.779
    img[:, :, 2] += 123.68
    # 'BGR'->'RGB'
    img = img[:, :, ::-1]
    img = np.clip(img, 0, 255).astype('uint8')
    return img


# Create Keras variables of input images
content_img = K.variable(preprocess(CONTENT_IMG_PATH))
style_img = K.variable(preprocess(STYLE_IMG_PATH))

if K.image_data_format() == 'channels_first':
    gen_img = K.placeholder(shape=(1, 3, img_h, img_w))
else:
    gen_img = K.placeholder(shape=(1, img_h, img_w, 3))

# Create a single tensor containing all three images
input_tensor = K.concatenate([content_img, style_img, gen_img], axis=0)

# Create a vgg19 model by running the input tensor though the vgg19 convolutional
# neural network, excluding the fully connected layers
model = vgg19.VGG19(include_top=False,
                    weights='imagenet',
                    input_tensor=input_tensor)