Beispiel #1
0
    def __init__(self, input_dim, output_dim,
                 init='uniform', input_length=None,
                 W_regularizer=None, activity_regularizer=None,
                 W_constraint=None,
                 weights=None, dropout=0., **kwargs):
        self.input_dim = input_dim
        self.output_dim = output_dim
        self.init = initializers.get(init)
        self.input_length = input_length
        self.dropout = dropout

        self.W_constraint = constraints.get(W_constraint)
        self.W_regularizer = regularizers.get(W_regularizer)
        self.activity_regularizer = regularizers.get(activity_regularizer)

        if 0. < self.dropout < 1.:
            self.uses_learning_phase = True
        self.initial_weights = weights
        kwargs['input_shape'] = (self.input_length,)
        kwargs['input_dtype'] = K.floatx()
        super(WeightedAspectEmb, self).__init__(**kwargs)
Beispiel #2
0
    def build(self, input_shape):
        """Creates the layer weights.

        Args:
            input_shape (list(tuple, tuple)): [(batch_size, n_steps, n_classes), (batch_size, 1)]
        """
        assert len(input_shape) == 2
        assert len(input_shape[0]) == 3
        assert len(input_shape[1]) == 2
        n_steps = input_shape[0][1].value
        n_classes = input_shape[0][2].value
        assert n_steps is None or n_steps >= 2

        self.transition_params = self.add_weight(shape=(n_classes, n_classes),
                                                 initializer='uniform',
                                                 name='transition')
        self.input_spec = [
            InputSpec(dtype=K.floatx(), shape=(None, n_steps, n_classes)),
            InputSpec(dtype='int32', shape=(None, 1))
        ]
        self.built = True
Beispiel #3
0
    def classification_loss(self, y_true, y_pred):
        # TODO: try weighted_categorical_crossentropy
        labels = y_true[..., :-1]
        # -1 for ignore, 0 for background, 1 for object
        anchor_state = y_true[..., -1]

        classification = y_pred
        # filter out "ignore" anchors
        indices = tf.where(K.not_equal(anchor_state, -1))
        labels = tf.gather_nd(labels, indices)
        classification = tf.gather_nd(classification, indices)

        # compute the loss
        loss = focal(labels, classification, alpha=self.alpha, gamma=self.gamma)

        # compute the normalizer: the number of positive anchors
        normalizer = tf.where(K.equal(anchor_state, 1))
        normalizer = K.cast(K.shape(normalizer)[0], K.floatx())
        normalizer = K.maximum(K.cast_to_floatx(1.0), normalizer)

        return K.sum(loss) / normalizer
Beispiel #4
0
    def _get_seed_input(self, seed_input):
        """Creates a random `seed_input` if None. Otherwise:
            - Ensures batch_size dim on provided `seed_input`.
            - Shuffle axis according to expected `image_data_format`.
        """
        desired_shape = (1, ) + K.int_shape(self.input_tensor)[1:]
        if seed_input is None:
            return utils.random_array(
                desired_shape,
                mean=np.mean(self.input_range),
                std=0.05 * (self.input_range[1] - self.input_range[0]))

        # Add batch dim if needed.
        if len(seed_input.shape) != len(desired_shape):
            seed_input = np.expand_dims(seed_input, 0)

        # Only possible if channel idx is out of place.
        if seed_input.shape[-1] != desired_shape[-1] and \
           seed_input.shape[1] != desired_shape[1]:
            seed_input = np.moveaxis(seed_input, -1, 1)
        return seed_input.astype(K.floatx())
Beispiel #5
0
    def _remove_bad_images(self):
        """Remove any batch of images that has fewer than 2 cell IDs."""
        good_batches = []
        for batch in range(self.x.shape[0]):
            # There should be at least 3 id's - 2 cells and 1 background
            if len(np.unique(self.y[batch])) > 2:
                good_batches.append(batch)

        X_new_shape = tuple([len(good_batches)] + list(self.x.shape)[1:])
        y_new_shape = tuple([len(good_batches)] + list(self.y.shape)[1:])

        X_new = np.zeros(X_new_shape, dtype=K.floatx())
        y_new = np.zeros(y_new_shape, dtype='int32')

        for k, batch in enumerate(good_batches):
            X_new[k] = self.x[batch]
            y_new[k] = self.y[batch]

        self.x = X_new
        self.y = y_new
        self.daughters = [self.daughters[i] for i in good_batches]
Beispiel #6
0
    def build(self, input_shape):
        if isinstance(input_shape, list):
            assert len(input_shape) == 2
            input_ids_shape, token_type_ids_shape = input_shape
            self.input_spec = [
                keras.layers.InputSpec(shape=input_ids_shape),
                keras.layers.InputSpec(shape=token_type_ids_shape)
            ]
        else:
            input_ids_shape = input_shape
            self.input_spec = keras.layers.InputSpec(shape=input_ids_shape)

        self.word_embeddings_layer = keras.layers.Embedding(
            input_dim=self.params.vocab_size,
            output_dim=self.params.hidden_size if
            self.params.embedding_size is None else self.params.embedding_size,
            mask_zero=True,  # =self.params.mask_zero,
            name="word_embeddings")
        if self.params.embedding_size is not None:
            # ALBERT word embeddings projection
            self.word_embeddings_2_layer = self.add_weight(
                name="word_embeddings_2/embeddings",
                shape=[self.params.embedding_size, self.params.hidden_size],
                dtype=K.floatx())

        if self.params.use_token_type:
            self.token_type_embeddings_layer = keras.layers.Embedding(
                input_dim=self.params.token_type_vocab_size,
                output_dim=self.params.hidden_size,
                mask_zero=False,
                name="token_type_embeddings")
        if self.params.use_position_embeddings:
            self.position_embeddings_layer = PositionEmbeddingLayer.from_params(
                self.params, name="position_embeddings")

        self.layer_norm_layer = pf.LayerNormalization(name="LayerNorm")
        self.dropout_layer = keras.layers.Dropout(
            rate=self.params.hidden_dropout)

        super(BertEmbeddingsLayer, self).build(input_shape)
Beispiel #7
0
    def _get_batches_of_transformed_samples(self, index_array):
        if self.channel_axis == 1:
            shape = (len(index_array), self.x.shape[self.channel_axis],
                     2 * self.win_z + 1, 2 * self.win_x + 1,
                     2 * self.win_y + 1)
        else:
            shape = (len(index_array), 2 * self.win_z + 1, 2 * self.win_x + 1,
                     2 * self.win_y + 1, self.x.shape[self.channel_axis])

        batch_x = np.zeros(shape, dtype=self.x.dtype)
        for i, j in enumerate(index_array):
            b, pz, px, py = self.batch[j], self.pixels_z[j], self.pixels_x[
                j], self.pixels_y[j]
            x = self._sample_image(b, pz, px, py)
            x = self.movie_data_generator.random_transform(x.astype(
                K.floatx()))
            x = self.movie_data_generator.standardize(x)

            batch_x[i] = x

        if self.save_to_dir:
            time_axis = 2 if self.data_format == 'channels_first' else 1
            for i, j in enumerate(index_array):
                for frame in range(batch_x.shape[time_axis]):
                    if time_axis == 2:
                        img = batch_x[i, :, frame]
                    else:
                        img = batch_x[i, frame]
                    img = array_to_img(img, self.data_format, scale=True)
                    fname = '{prefix}_{index}_{hash}.{format}'.format(
                        prefix=self.save_prefix,
                        index=j,
                        hash=np.random.randint(1e4),
                        format=self.save_format)
                    img.save(os.path.join(self.save_to_dir, fname))

        if self.y is None:
            return batch_x
        batch_y = self.y[index_array]
        return batch_x, batch_y
Beispiel #8
0
def weighted_focal_loss(y_true,
                        y_pred,
                        n_classes=3,
                        gamma=2.,
                        axis=None,
                        from_logits=False):
    """Focal loss between an output tensor and a target tensor.
    Automatically computes the class weights from the target image and uses
    them to weight the cross entropy

    Args:
        y_true: A tensor of the same shape as ``y_pred``.
        y_pred: A tensor resulting from a softmax
            (unless ``from_logits`` is ``True``, in which
            case ``y_pred`` is expected to be the logits).
        from_logits: Boolean, whether ``y_pred`` is the
            result of a softmax, or is a tensor of logits.

    Returns:
        tensor: Output tensor.
    """
    if from_logits:
        raise Exception('weighted_focal_loss cannot take logits')
    if axis is None:
        axis = 1 if K.image_data_format(
        ) == 'channels_first' else K.ndim(y_pred) - 1
    reduce_axis = [x for x in list(range(K.ndim(y_pred))) if x != axis]
    # scale preds so that the class probas of each sample sum to 1
    y_pred = y_pred / K.sum(y_pred, axis=axis, keepdims=True)
    # manual computation of crossentropy
    _epsilon = tf.convert_to_tensor(K.epsilon(), y_pred.dtype.base_dtype)
    y_pred = tf.clip_by_value(y_pred, _epsilon, 1. - _epsilon)
    y_true_cast = K.cast(y_true, K.floatx())
    total_sum = K.sum(y_true_cast)
    class_sum = K.sum(y_true_cast, axis=reduce_axis, keepdims=True)
    class_weights = 1.0 / K.cast_to_floatx(n_classes) * tf.divide(
        total_sum, class_sum + 1.)
    temp_loss = (K.pow(1. - y_pred, gamma) * K.log(y_pred) * class_weights)
    focal_loss = -K.sum(y_true * temp_loss, axis=axis)
    return focal_loss
Beispiel #9
0
    def build(self, input_shape):
        assert len(input_shape) == 2
        self.input_dim = input_shape[1]
        input_dim=self.input_dim
        self.input_spec = [InputSpec(dtype=K.floatx(),
                                     shape=(None, input_dim))]

        # self.W_mu = self.init((input_dim, self.output_dim),
                        #    name='{}_W_mu'.format(self.name))
        self.W_mu = self.init(input_dim, self.output_dim)
        self.W_log_sigma = self.init_sigma((input_dim, self.output_dim),
                           name='{}_W_log_sigma'.format(self.name))
        if self.bias:
            self.b_mu = self.init((self.output_dim,),
                             name='{}_b_mu'.format(self.name))
            self.b_log_sigma = self.init_sigma((self.output_dim,),
                             name='{}_b_log_sigma'.format(self.name))
            self.trainable_weights = [self.W_mu, self.W_log_sigma, self.b_mu, self.b_log_sigma]
        else:
            self.trainable_weights = [self.W_mu, self.W_log_sigma]

        self.regularizers = []
        if self.W_regularizer:
            self.W_regularizer.set_param(self.W_mu)
            self.regularizers.append(self.W_regularizer)
        if self.b_regularizer:
            self.b_regularizer.set_param(self.b_mu)
            self.regularizers.append(self.b_regularizer)
        if self.W_sigma_regularizer:
            self.W_sigma_regularizer.set_param(self.W_log_sigma)
            self.regularizers.append(self.W_sigma_regularizer)
        if self.b_sigma_regularizer:
            self.b_sigma_regularizer.set_param(self.b_log_sigma)
            self.regularizers.append(self.b_sigma_regularizer)
        if self.activity_regularizer:
            self.activity_regularizer.set_layer(self)
            self.regularizers.append(self.activity_regularizer)
        if self.initial_weights is not None:
            self.set_weights(self.initial_weights)
            del self.initial_weights
Beispiel #10
0
    def __init__(self, epsilon, model, num_iterations=5, norm='l2', **kwargs):
        super().__init__(**kwargs)

        self._epsilon = tf.Variable(
            epsilon, dtype=K.floatx(), name='epsilon', trainable=False)
        self._num_iterations = tf.Variable(
            num_iterations, name='num_iterations', trainable=False)

        # Check norm compatibility.
        if norm != 'l2':
            # For now we only support the l2 norm.
            raise ValueError(f'{norm} norm not supported')

        self._norm = norm

        self._layer_computers = LipschitzComputer.for_model(
            model, self._num_iterations)
        self._W = model.layers[-1].kernel

        self._lc_frozen = tf.Variable(False, name='lc_frozen', trainable=False)
        self._hardcoded_lc = tf.Variable(
            1., name='hardcoded_lc', trainable=False)
    def __init__(self,
                 train_dict,
                 image_data_generator,
                 batch_size=1,
                 skip=None,
                 shuffle=False,
                 transform=None,
                 transform_kwargs={},
                 seed=None,
                 data_format='channels_last',
                 save_to_dir=None,
                 save_prefix='',
                 save_format='png'):
        X, y = train_dict['X'], train_dict['y']
        if X.shape[0] != y.shape[0]:
            raise ValueError('Training batches and labels should have the same'
                             'length. Found X.shape: {} y.shape: {}'.format(
                                 X.shape, y.shape))
        self.x = np.asarray(X, dtype=K.floatx())

        if self.x.ndim != 4:
            raise ValueError(
                'Input data in `ImageFullyConvIterator` '
                'should have rank 4. You passed an array '
                'with shape', self.x.shape)

        self.y = _transform_masks(y,
                                  transform,
                                  data_format=data_format,
                                  **transform_kwargs)
        self.channel_axis = 3 if data_format == 'channels_last' else 1
        self.skip = skip
        self.image_data_generator = image_data_generator
        self.data_format = data_format
        self.save_to_dir = save_to_dir
        self.save_prefix = save_prefix
        self.save_format = save_format
        super(ImageFullyConvIterator, self).__init__(self.x.shape[0],
                                                     batch_size, shuffle, seed)
Beispiel #12
0
    def _postprocess_conv2d_output(input_tensor, data_format):
        """Transpose and cast the output from conv2d if needed.

        Parameters
        ----------
        input_tensor: tensor
            The input that requires transposing and casting
        data_format: str
            `"channels_last"` or `"channels_first"`

        Returns
        -------
        tensor
            The transposed and cast input tensor
        """

        if data_format == "channels_first":
            input_tensor = tf.transpose(input_tensor, (0, 3, 1, 2))

        if K.floatx() == "float64":
            input_tensor = tf.cast(input_tensor, "float64")
        return input_tensor
Beispiel #13
0
    def call(self, x):
        """
        Args:
            x (`Tensor`): a 2d batch (b, t, f, ch) or (b, ch, t, f)

        Returns:
            (`Tensor`): A tensor with the same shape as input data.
        """
        if self.data_format == 'channels_first':
            x = K.permute_dimensions(x, (0, 2, 3, 1))

        x = tf.pad(
            x, tf.constant([[0, 0], [self.n, self.n], [0, 0], [0, 0]]), mode=self.mode
        )  # pad over time
        kernel = K.arange(-self.n, self.n + 1, 1, dtype=K.floatx())
        kernel = K.reshape(kernel, (-1, 1, 1, 1))  # time, freq, in_ch, out_ch

        x = K.conv2d(x, kernel, data_format=_CH_LAST_STR) / self.denom
        if self.data_format == _CH_FIRST_STR:
            x = K.permute_dimensions(x, (0, 3, 1, 2))

        return x
Beispiel #14
0
    def get_updates(self, loss, params):
        grads = self.get_gradients(loss, params)
        self.updates = [state_ops.assign_add(self.iterations, 1)]

        lr = self.lr
        if self.initial_decay > 0:
            lr = lr * (  # pylint: disable=g-no-augmented-assignment
                1. / (1. + self.decay * math_ops.cast(self.iterations,
                                                    K.dtype(self.decay))))

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

        ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
        vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
        if self.amsgrad:
            vhats = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
        else:
            vhats = [K.zeros(1) for _ in params]
        self.weights = [self.iterations] + ms + vs + vhats

        for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats):
            m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
            #v_t = (self.beta_2 * v) + (1. - self.beta_2) * math_ops.square(g) # from amsgrad
            v_t = v - (1-self.beta_2)*K.sign(v-math_ops.square(g))*math_ops.square(g)
            p_t = p - lr_t * m_t / (K.sqrt(v_t) + self.epsilon)

            self.updates.append(state_ops.assign(m, m_t))
            self.updates.append(state_ops.assign(v, v_t))
            new_p = p_t

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

            self.updates.append(state_ops.assign(p, new_p))
        return self.updates
Beispiel #15
0
    def __call__(self, shape, dtype=K.floatx(), partition_info=None):
        """Initialization for convolutional kernels.

        The main difference with tf.variance_scaling_initializer is that
        tf.variance_scaling_initializer uses a truncated normal with an uncorrected
        standard deviation, whereas here we use a normal distribution. Similarly,
        tf.contrib.layers.variance_scaling_initializer uses a truncated normal with
        a corrected standard deviation.

        Args:
        shape: shape of variable
        dtype: dtype of variable
        partition_info: unused

        Returns:
        an initialization for the variable
        """
        del partition_info
        kernel_height, kernel_width, _, out_filters = shape
        fan_out = int(kernel_height * kernel_width * out_filters)
        return tf.random.normal(
            shape, mean=0.0, stddev=np.sqrt(2.0 / fan_out), dtype=dtype)
Beispiel #16
0
	def dense_loss(self, y_true, y_pred):
		"""y_true需要是one hot形式
		"""
		# 导出mask并转换数据类型
		mask = K.all(K.greater(y_pred, -1e6), axis=2, keepdims=True)
		mask = K.cast(mask, K.floatx())
		# 计算目标分数
		y_true, y_pred = y_true * mask, y_pred * mask
		target_score = self.target_score(y_true, y_pred)
		# 递归计算log Z
		init_states = [y_pred[:, 0]]
		y_pred = K.concatenate([y_pred, mask], axis=2)
		input_length = K.int_shape(y_pred[:, 1:])[1]
		log_norm, _, _ = K.rnn(
			self.log_norm_step,
			y_pred[:, 1:],
			init_states,
			input_length=input_length
		)  # 最后一步的log Z向量
		log_norm = tf.reduce_logsumexp(log_norm, 1)  # logsumexp得标量
		# 计算损失 -log p
		return log_norm - target_score
    def get_updates(self, loss, params):
        grads = self.get_gradients(loss, params)
        self.updates = [K.update_add(self.iterations, 1)]
        # decoupled weight decay (4/6)
        wd = self.wd

        lr = self.lr
        if self.initial_decay > 0:
            lr *= (1. /
                   (1. +
                    self.decay * K.cast(self.iterations, K.dtype(self.decay))))
        # decoupled weight decay (5/6)
        eta_t = lr / self.init_lr

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

        ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
        vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
        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)
            # decoupled weight decay (6/6)
            p_t = p - lr_t * m_t / (K.sqrt(v_t) +
                                    self.epsilon) - eta_t * wd * p

            self.updates.append(K.update(m, m_t))
            self.updates.append(K.update(v, v_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))
        return self.updates
Beispiel #18
0
    def _focal(y_true, y_pred):
        """ Compute the focal loss given the target tensor and the predicted tensor.

        As defined in https://arxiv.org/abs/1708.02002

        Args
            y_true: Tensor of target data from the generator with shape (B, N, num_classes).
            y_pred: Tensor of predicted data from the network with shape (B, N, num_classes).

        Returns
            The focal loss of y_pred w.r.t. y_true.
        """
        labels = y_true[:, :, :-1]
        anchor_state = y_true[:, :,
                              -1]  # -1 for ignore, 0 for background, 1 for object
        classification = y_pred

        # filter out "ignore" anchors
        indices = tf.where(K.not_equal(anchor_state, -1))
        labels = tf.gather_nd(labels, indices)
        classification = tf.gather_nd(classification, indices)

        # compute the focal loss
        alpha_factor = K.ones_like(labels) * alpha
        alpha_factor = tf.where(K.equal(labels, 1), alpha_factor,
                                1 - alpha_factor)
        focal_weight = tf.where(K.equal(labels, 1), 1 - classification,
                                classification)
        focal_weight = alpha_factor * focal_weight**gamma

        cls_loss = focal_weight * K.binary_crossentropy(labels, classification)

        # compute the normalizer: the number of positive anchors
        normalizer = tf.where(K.equal(anchor_state, 1))
        normalizer = K.cast(K.shape(normalizer)[0], K.floatx())
        normalizer = K.maximum(K.cast_to_floatx(1.0), normalizer)

        return K.sum(cls_loss) / normalizer
Beispiel #19
0
    def call(self, inputs, start=1):
        """
            Args:
                inputs: a float32 Tensor with shape [batch_size, sequence_length, hidden_size]

            Returns:
                embedding: a float32 Tensor with shape [batch_size, sequence_length, hidden_size]
        """
        inputs.shape.assert_has_rank(3)
        if not self.concat:
            assert inputs.shape[
                -1] == self.hidden_size, 'Input final dim must match model hidden size'
        batch_size = get_shape(inputs, 0)
        sequence_length = get_shape(inputs, 1)
        seq_pos = K.arange(start, sequence_length + start,
                           dtype=K.floatx())[None, :]  # 1-index positions

        index = seq_pos[:, :, None] / self.divisor

        sin_embedding = tf.sin(index)
        cos_embedding = tf.cos(index)

        position_embedding = tf.stack((sin_embedding, cos_embedding), -1)
        position_shape = (1, sequence_length, self.hidden_size)

        position_embedding = tf.reshape(position_embedding, position_shape)

        if self.concat:
            position_embedding = tf.tile(position_embedding,
                                         (batch_size, 1, 1))
            output = tf.concat((inputs, position_embedding), -1)
            # Return the reprojection to the hidden size of the layer, if we're doing
            # that, otherwise, just return the layer
            if self.reproject_embedding:
                output = self.projection_layer(output)
            return output

        return inputs + position_embedding
Beispiel #20
0
    def build(self, input_shape):
        assert len(input_shape) == 3
        n_classes = input_shape[2]
        n_steps = input_shape[1]
        assert n_steps is None or n_steps >= 2
        self.input_spec = [
            InputSpec(dtype=K.floatx(), shape=(None, n_steps, n_classes))
        ]

        self.U = self.add_weight(
            name="U",
            shape=(n_classes, n_classes),
            initializer=self.init,
            regularizer=self.U_regularizer,
            constraint=self.U_constraint,
        )

        self.b_start = self.add_weight(
            shape=(n_classes, ),
            initializer="zero",
            name="b_start",
            regularizer=self.b_start_regularizer,
            constraint=self.b_start_constraint,
        )

        self.b_end = self.add_weight(
            name="b_end",
            shape=(n_classes, ),
            initializer="zero",
            regularizer=self.b_end_regularizer,
            constraint=self.b_end_constraint,
        )

        if self.initial_weights is not None:
            self.set_weights(self.initial_weights)
            del self.initial_weights

        self.built = True
Beispiel #21
0
    def call(self, inputs, mask=None):
        # Compute the metric
        metric = self.metric_func(*inputs)
        if K.int_shape(metric)[-1] == 1:
            metric = K.squeeze(metric, axis=-1)
        if len(K.int_shape(metric)) == 0:
            metric = K.ones(K.shape(inputs[0])[0]) * metric

        # Apply the mask if needed
        if mask is not None:
            if not isinstance(mask, list):
                mask = [mask]
            mask = [K.cast(m, K.floatx()) for m in mask if m is not None]
            mask = reduce(lambda a, b: a*b, mask)
            metric *= mask
            metric /= K.mean(mask, axis=-1, keepdims=True)

        # Make sure that the tensor returned is (None, 1)
        dims = len(K.int_shape(metric))
        if dims > 1:
            metric = K.mean(metric, axis=list(range(1, dims)))

        return K.expand_dims(metric)
    def call(self, inputs, **kwargs):
        y_true = inputs[0]
        y_pred = inputs[1]

        y_true = K.cast(y_true, tf.int32)
        blank_mask = tf.not_equal(y_true, K.cast(self.blank_value, tf.int32))

        y_true_초성, y_true_중성, y_true_종성 = JamoDeCompose()(y_true)
        y_pred_초성, y_pred_중성, y_pred_종성 = tf.split(
            y_pred,
            [len(초성) + 1, len(중성) + 1, len(종성) + 1], axis=-1)

        mask = tf.cast(blank_mask, dtype=K.floatx())
        loss_초성 = K.sparse_categorical_crossentropy(y_true_초성,
                                                    y_pred_초성) * mask
        loss_중성 = K.sparse_categorical_crossentropy(y_true_중성,
                                                    y_pred_중성) * mask
        loss_종성 = K.sparse_categorical_crossentropy(y_true_종성,
                                                    y_pred_종성) * mask

        mask = K.sum(mask, axis=1)
        loss_jamo = K.sum(loss_초성 + loss_중성 + loss_종성, axis=1)
        return loss_jamo / mask
Beispiel #23
0
def test_binary_auto_po2():
    """Test binary auto_po2 scale quantizer."""

    np.random.seed(42)
    N = 1000000
    m_list = [1.0, 0.1, 0.01, 0.001]

    for m in m_list:
        x = np.random.uniform(-m, m, (N, 10)).astype(K.floatx())
        x = K.constant(x)

        quantizer_ref = binary(alpha="auto")
        quantizer = binary(alpha="auto_po2")

        q_ref = K.eval(quantizer_ref(x))
        q = K.eval(quantizer(x))

        ref = get_weight_scale(quantizer_ref, q_ref)

        expected = np.power(2.0, np.round(np.log2(ref)))
        result = get_weight_scale(quantizer, q)

        assert_allclose(result, expected, rtol=0.0001)
        def _roi_align(args):
            boxes = args[0]
            fpn = args[1]  # process the feature map
            x1 = boxes[:, 0]
            y1 = boxes[:, 1]
            x2 = boxes[:, 2]
            y2 = boxes[:, 3]

            fpn_shape = K.cast(K.shape(fpn), dtype=K.floatx())
            norm_boxes = K.stack([
                (y1 / image_shape[1] * fpn_shape[0]) / (fpn_shape[0] - 1),
                (x1 / image_shape[2] * fpn_shape[1]) / (fpn_shape[1] - 1),
                (y2 / image_shape[1] * fpn_shape[0] - 1) / (fpn_shape[0] - 1),
                (x2 / image_shape[2] * fpn_shape[1] - 1) / (fpn_shape[1] - 1)
            ],
                                 axis=1)

            rois = tf.image.crop_and_resize(
                K.expand_dims(fpn, axis=0), norm_boxes,
                tf.zeros((K.shape(norm_boxes)[0], ), dtype='int32'),
                self.crop_size)

            return rois
    def get_updates(self, loss, params):
        if len(self.updates) > 0:
            return self.updates
        multiplies = {}
        for param in params:
            multiplier = self._get_multiplier(param.name)
            if multiplier not in multiplies:
                multiplies[multiplier] = []
            multiplies[multiplier].append(param)

        for multiplier, params in multiplies.items():
            lr = self.lr
            if callable(multiplier):
                lr = lr * multiplier(
                    backend.cast(self.optimizer.iterations, backend.floatx()))
            elif multiplier != 1.0:
                lr = lr * multiplier
            self.optimizer.lr = lr
            self.updates += self.optimizer.get_updates(loss, params)
            self.weights += self.optimizer.weights
        self.optimizer.lr = self.lr

        return self.updates
Beispiel #26
0
def create_mha_pool(
        embedding_dim: int = 768,
        max_len: int = 512,
        num_heads: int = 12,
        num_layers: int = 12,
        attention_dropout: float = 0.1,
        use_attn_mask: bool = True,
        neg_inf: float = -1e9,
        internal_dim: int = 768,
        output_projection: bool = False,
        output_dim: int = 768,
        input_ffn=None,
        input_ffn_dim=768,
        gated_ffn: bool = False,
        kernel_initializer='glorot_uniform',
        kernel_constraint=None,
        use_dense_connection: bool = False) -> tensorflow.keras.Model:
    input_ = Input(batch_shape=(None, max_len, embedding_dim),
                   name='input',
                   dtype='float32')
    attn_mask = Input(batch_shape=(None, 1, max_len, max_len),
                      name='attention_mask_input',
                      dtype=K.floatx()) if use_attn_mask else None
    inputs = [input_]
    x = input_
    for i in range(num_layers):
        out = MhaPoolLayer(internal_dim, num_heads, attention_dropout,
                           use_attn_mask, i, neg_inf, output_projection,
                           output_dim, input_ffn, input_ffn_dim, gated_ffn,
                           kernel_initializer, kernel_constraint)(x, attn_mask)
        if use_dense_connection:
            x = tf.keras.backend.concatenate([x, out], axis=2)
        else:
            x = out
    if use_attn_mask:
        inputs.append(attn_mask)
    return tensorflow.keras.Model(inputs=inputs, outputs=[x], name='MhaPool')
Beispiel #27
0
def CR_spline_coeffs(t0, t1, t2, t3, t, dif=False, dtype=None) -> List[Tensor]:
    """
    Returns the coefficients of the control points p0, p1, p2, p3 
    in the Catmull-Rom spline.
    t is between t1, t2
    """
    dtype = dtype or K.floatx()
    t0, t1, t2, t3, t = [tf.cast(x, dtype) for x in [t0, t1, t2, t3, t]]

    s = (t - t1) / (t2 - t1)

    assert dif in [0, 1]
    if dif:
        sss = tf.stack([tf.zeros_like(s),
                        tf.ones_like(s), 2 * s, 3 * s**2],
                       axis=-1) / (t2 - t1)
    else:
        sss = tf.stack([tf.ones_like(s), s, s**2, s**3], axis=-1)

    coeffs = sss

    if len(sss.shape) == 1:  # hack to make matmul work
        coeffs = coeffs[None, :]

    coeffs = coeffs @ [[0, 1, 0, 0], [1, 0, 0, 0], [-2, -3, 3, -1],
                       [1, 2, -2, 1]]
    coeffs = coeffs * tf.stack([(t2 - t1) / (t2 - t0),
                                tf.ones_like(t0),
                                tf.ones_like(t0), (t2 - t1) / (t3 - t1)],
                               axis=-1)
    coeffs = coeffs @ [[-1, 0, 1, 0], [0, 1, 0, 0], [0, 0, 1, 0],
                       [0, -1, 0, 1]]

    if len(sss.shape) == 1:  # caused by hack to make matmul work
        coeffs = coeffs[0]

    return tf.unstack(coeffs, axis=-1)
Beispiel #28
0
        def _fd_batch(y_true, y_pred, iou_threshold=0.75, parallel_iterations=32):
            if K.ndim(y_pred) == 4:
                y_pred_shape = tf.shape(y_pred)
                new_y_pred_shape = [y_pred_shape[0] * y_pred_shape[1],
                                    y_pred_shape[2], y_pred_shape[3]]
                y_pred = tf.reshape(y_pred, new_y_pred_shape)

                y_true_shape = tf.shape(y_true)
                new_y_true_shape = [y_true_shape[0] * y_true_shape[1],
                                    y_true_shape[2], y_true_shape[3]]
                y_true = tf.reshape(y_true, new_y_true_shape)

            # split up the different predicted blobs
            boxes = y_pred[:, :, :4]
            scores = y_pred[:, :, 4:5]

            # split up the different blobs
            annotations = y_true[:, :, :5]

            def _fd(args):
                boxes = args[0]
                scores = args[1]
                annotations = args[2]

                return compute_fd_loss(
                    boxes,
                    scores,
                    annotations,
                    iou_threshold=iou_threshold)

            fd_batch_loss = tf.map_fn(
                _fd,
                elems=[boxes, scores, annotations],
                dtype=K.floatx(),
                parallel_iterations=parallel_iterations)

            return K.mean(fd_batch_loss)
Beispiel #29
0
 def call(self,
          inputs: tensorflow.Tensor,
          mask: Optional[tensorflow.Tensor] = None) -> tensorflow.Tensor:
     input_shape = K.shape(inputs)
     if self.mode == self.MODE_ADD:
         batch_size, seq_len, output_dim = input_shape[0], input_shape[
             1], input_shape[2]
         pos_input = K.tile(K.expand_dims(K.arange(seq_len), axis=0),
                            [batch_size, 1])
     elif self.mode == self.MODE_CONCAT:
         batch_size, seq_len, output_dim = input_shape[0], input_shape[
             1], self.output_dim
         pos_input = K.tile(K.expand_dims(K.arange(seq_len), axis=0),
                            [batch_size, 1])
     else:
         output_dim = self.output_dim
         pos_input = inputs
     if K.dtype(pos_input) != K.floatx():
         pos_input = K.cast(pos_input, K.floatx())
     evens = K.arange(output_dim // 2) * 2
     odds = K.arange(output_dim // 2) * 2 + 1
     even_embd = K.sin(
         K.dot(
             K.expand_dims(pos_input, -1),
             K.expand_dims(
                 1.0 / K.pow(
                     10000.0,
                     K.cast(evens, K.floatx()) /
                     K.cast(output_dim, K.floatx())), 0)))
     odd_embd = K.cos(
         K.dot(
             K.expand_dims(pos_input, -1),
             K.expand_dims(
                 1.0 / K.pow(
                     10000.0,
                     K.cast((odds - 1), K.floatx()) /
                     K.cast(output_dim, K.floatx())), 0)))
     embd = K.stack([even_embd, odd_embd], axis=-1)
     output = K.reshape(embd, [-1, K.shape(inputs)[1], output_dim])
     if self.mode == self.MODE_CONCAT:
         output = K.concatenate([inputs, output], axis=-1)
     if self.mode == self.MODE_ADD:
         output += inputs
     return output
        def score(y_true, y_pred):
            y_t_rank = len(y_true.shape.as_list())
            y_p_rank = len(y_pred.shape.as_list())
            y_t_last_dim = y_true.shape.as_list()[-1]
            y_p_last_dim = y_pred.shape.as_list()[-1]

            is_binary = y_p_last_dim == 1
            is_sparse_categorical = (y_t_rank < y_p_rank
                                     or y_t_last_dim == 1 and y_p_last_dim > 1)

            if isinstance(metric_function, six.string_types):
                if metric_function in ["accuracy", "acc"]:
                    if is_binary:
                        metric = binary_accuracy(y_true, y_pred)
                    elif is_sparse_categorical:
                        metric = sparse_categorical_accuracy(y_true, y_pred)
                    else:
                        metric = categorical_accuracy(y_true, y_pred)
                else:
                    metric = categorical_accuracy(y_true, y_pred)
            else:
                metric = metric_function(y_true, y_pred)

            return K.cast(metric * (1.0 + delta), K.floatx())