Python tensorflow.keras.backend.flatten() Examples

The following are 9 code examples of tensorflow.keras.backend.flatten(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow.keras.backend , or try the search function .
Example #1
Source File: RigidTransformation3DImputation.py    From aitom with GNU General Public License v3.0 6 votes vote down vote up
def _rotation_matrix_zyz(self, params):
        phi = params[0] * 2 * np.pi - np.pi;       theta = params[1] * 2 * np.pi - np.pi;     psi_t = params[2] * 2 * np.pi - np.pi;
        
        loc_r = params[3:6] * 2 - 1
        
        
        a1 = self._rotation_matrix_axis(2, psi_t)       # first rotate about z axis for angle psi_t
        a2 = self._rotation_matrix_axis(1, theta)
        a3 = self._rotation_matrix_axis(2, phi)     
        rm = K.dot(K.dot(a3,a2),a1)
        
        rm = tf.transpose(rm)
        
        c = K.dot(-rm, K.expand_dims(loc_r))
        
        rm = K.flatten(rm)
        
        theta = K.concatenate([rm[:3], c[0], rm[3:6], c[1], rm[6:9], c[2]])

        return theta 
Example #2
Source File: RigidTransformation3DImputation.py    From aitom with GNU General Public License v3.0 6 votes vote down vote up
def _mask_rotation_matrix_zyz(self, params):
        phi = params[0] * 2 * np.pi - np.pi;       theta = params[1] * 2 * np.pi - np.pi;     psi_t = params[2] * 2 * np.pi - np.pi;
        
        loc_r = params[3:6] * 0    # magnitude of Fourier transformation is translation-invariant 
        
        
        a1 = self._rotation_matrix_axis(2, psi_t)
        a2 = self._rotation_matrix_axis(1, theta)
        a3 = self._rotation_matrix_axis(2, phi)     
        rm = K.dot(K.dot(a3,a2),a1)
        
        rm = tf.transpose(rm)
        
        c = K.dot(-rm, K.expand_dims(loc_r))
        
        rm = K.flatten(rm)
        
        theta = K.concatenate([rm[:3], c[0], rm[3:6], c[1], rm[6:9], c[2]])

        return theta 
Example #3
Source File: metrics.py    From keras-unet with MIT License 6 votes vote down vote up
def dice_coef(y_true, y_pred, smooth=1.):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return (2. * intersection + smooth) / (
                K.sum(y_true_f) + K.sum(y_pred_f) + smooth) 
Example #4
Source File: metrics.py    From MIScnn with GNU General Public License v3.0 6 votes vote down vote up
def dice_coefficient(y_true, y_pred, smooth=0.00001):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return (2. * intersection + smooth) / \
           (K.sum(y_true_f) + K.sum(y_pred_f) + smooth) 
Example #5
Source File: utils.py    From neuron with GNU General Public License v3.0 5 votes vote down vote up
def flatten(v):
    """
    flatten Tensor v
    
    Parameters:
        v: Tensor to be flattened
    
    Returns:
        flat Tensor
    """

    return tf.reshape(v, [-1]) 
Example #6
Source File: regularizers.py    From neuron with GNU General Public License v3.0 5 votes vote down vote up
def soft_l0_wrap(wt = 1.):

    def soft_l0(x):
        """
        maximize the number of 0 weights
        """
        nb_weights = tf.cast(tf.size(x), tf.float32)
        nb_zero_wts = tf.reduce_sum(soft_delta(K.flatten(x)))
        return wt * (nb_weights - nb_zero_wts) / nb_weights

    return soft_l0 
Example #7
Source File: metrics.py    From keras-unet with MIT License 5 votes vote down vote up
def iou(y_true, y_pred, smooth=1.):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return (intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) - intersection + smooth) 
Example #8
Source File: metrics.py    From keras-unet with MIT License 5 votes vote down vote up
def iou_thresholded(y_true, y_pred, threshold=0.5, smooth=1.):
    y_pred = threshold_binarize(y_pred, threshold)
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return (intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) - intersection + smooth) 
Example #9
Source File: metrics.py    From solaris with Apache License 2.0 5 votes vote down vote up
def dice_coef_binary(y_true, y_pred, smooth=1e-7):
    '''
    Dice coefficient for 2 categories. Ignores background pixel label 0
    Pass to model as metric during compile statement
    '''
    y_true_f = K.flatten(K.one_hot(K.cast(y_true, 'int32'),
                                   num_classes=2)[..., 1:])
    y_pred_f = K.flatten(y_pred[..., 1:])
    intersect = K.sum(y_true_f * y_pred_f, axis=-1)
    denom = K.sum(y_true_f + y_pred_f, axis=-1)
    return K.mean((2. * intersect / (denom + smooth)))