def gram_matrix(x): assert K.ndim(x) == 3 if K.image_dim_ordering() == "th": features = K.batch_flatten(x) else: features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1))) gram = K.dot(features, K.transpose(features)) return gram
def gram_matrix(x): assert K.ndim(x) == 3 if K.image_data_format() == "channels_first": features = K.batch_flatten(x) else: features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1))) gram = K.dot(features, K.transpose(features)) return gram
def gram_matrix(x): assert K.ndim(x) == 4 grams = list() for i in range(self.Batch_Size): img = x[i, :, :, :] if K.image_data_format() == 'channels_first': features = K.batch_flatten(img) else: features = K.batch_flatten( K.permute_dimensions(img, (2, 0, 1))) grams.append(K.dot(features, K.transpose(features))) gram = tf.keras.backend.stack(grams) return gram
def gram_matrix(x): assert K.ndim(x) == 3 if image_dim_ordering() == 'th': features = K.batch_flatten(x) else: features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1))) shape = K.shape(x) C, W, H = (shape[0],shape[1], shape[2]) cf = K.reshape(features ,(C,-1)) gram = K.dot(cf, K.transpose(cf)) / K.cast(C*W*H,dtype='float32') return gram
def gram_matrix(x, norm_by_channels=False): ''' Returns the Gram matrix of the tensor x. ''' if K.ndim(x) == 3: features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1))) shape = K.shape(x) C, H, W = shape[0], shape[1], shape[2] gram = K.dot(features, K.transpose(features)) elif K.ndim(x) == 4: # Swap from (H, W, C) to (B, C, H, W) x = K.permute_dimensions(x, (0, 3, 1, 2)) shape = K.shape(x) B, C, H, W = shape[0], shape[1], shape[2], shape[3] # Reshape as a batch of 2D matrices with vectorized channels features = K.reshape(x, K.stack([B, C, H * W])) # This is a batch of Gram matrices (B, C, C). gram = K.batch_dot(features, features, axes=2) else: raise ValueError( 'The input tensor should be either a 3d (H, W, C) or 4d (B, H, W, C) tensor.' ) # Normalize the Gram matrix if norm_by_channels: denominator = C * H * W # Normalization from Johnson else: denominator = H * W # Normalization from Google gram = gram / K.cast(denominator, x.dtype) return gram
def gram_matrix(x, y): """ shift = -1 features = tf.reshape(x, tf.shape(x)[ 1 : ]) features = backend.batch_flatten(backend.permute_dimensions(features, (2, 0, 1))) return backend.dot(features + shift, backend.transpose(features + shift)) """ y_up = tf.image.resize_images(y, tf.shape(x)[1:3]) shift = -1 features_A = tf.reshape(x, tf.shape(x)[1:]) + shift features_A = backend.batch_flatten( backend.permute_dimensions(features_A, (2, 0, 1))) features_B = tf.reshape(y_up, tf.shape(y_up)[1:]) + shift features_B = backend.batch_flatten( backend.permute_dimensions(features_B, (2, 0, 1))) gram = backend.dot(features_A, backend.transpose(features_B)) return gram
def call(self, inputs, **kwargs): if type(inputs) is list: assert len(inputs) == 2 input, mask = inputs _, hei, wid, _, _ = input.get_shape() if self.resize_masks: mask = tf.image.resize_bicubic(mask, (hei.value, wid.value)) mask = K.expand_dims(mask, -1) if input.get_shape().ndims == 3: masked = K.batch_flatten(mask * input) else: masked = mask * input else: if inputs.get_shape().ndims == 3: x = K.sqrt(K.sum(K.square(inputs), -1)) mask = K.one_hot(indices=K.argmax(x, 1), num_classes=x.get_shape().as_list()[1]) masked = K.batch_flatten(K.expand_dims(mask, -1) * inputs) else: masked = inputs return masked
def gram_matrix(x): features = K.batch_flatten(K.permute_dimensions(x, (2, 0, 1))) gram = K.dot(features, K.transpose(features)) return gram
def gram_matrix(x): assert K.ndim(x) == 3 features = K.batch_flatten(x) gram = K.dot(features - 1, K.transpose(features - 1)) return gram
def call(self, x, mask=None): x = x[:, :self.mydeletedim] return K.batch_flatten(x)