Python keras.backend.image_dim_ordering() Examples
The following are 30
code examples of keras.backend.image_dim_ordering().
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
keras.backend
, or try the search function
.
Example #1
Source File: grad_cam.py From Emotion with MIT License | 6 votes |
def deprocess_image(x): """ Same normalization as in: https://github.com/fchollet/keras/blob/master/examples/conv_filter_visualization.py """ if np.ndim(x) > 3: x = np.squeeze(x) # normalize tensor: center on 0., ensure std is 0.1 x = x - x.mean() x = x / (x.std() + 1e-5) x = x * 0.1 # clip to [0, 1] x = x + 0.5 x = np.clip(x, 0, 1) # convert to RGB array x = x * 255 if K.image_dim_ordering() == 'th': x = x.transpose((1, 2, 0)) x = np.clip(x, 0, 255).astype('uint8') return x
Example #2
Source File: inception_v4.py From cnn_evaluation_smoke with GNU General Public License v3.0 | 6 votes |
def conv2d_bn(x, nb_filter, nb_row, nb_col, border_mode='same', subsample=(1, 1), bias=False): """ Utility function to apply conv + BN. (Slightly modified from https://github.com/fchollet/keras/blob/master/keras/applications/inception_v3.py) """ if K.image_dim_ordering() == "th": channel_axis = 1 else: channel_axis = -1 x = Convolution2D(nb_filter, nb_row, nb_col, subsample=subsample, border_mode=border_mode, bias=bias)(x) x = BatchNormalization(axis=channel_axis)(x) x = Activation('relu')(x) return x
Example #3
Source File: evaluation.py From EUSIPCO2017 with GNU Affero General Public License v3.0 | 6 votes |
def _load_features(self, audio_filename): features = list() for feature_filename in self.feature_filenames: if audio_filename in feature_filename: filename_full_path = os.path.join(IRMAS_TEST_FEATURE_BASEPATH, self.model_module.BASE_NAME, feature_filename) feature = np.load(filename_full_path) feature -= self.dataset_mean features.append(feature) if K.image_dim_ordering() == "th": features = np.array(features).reshape(-1, 1, self.model_module.N_MEL_BANDS, self.model_module.SEGMENT_DUR) else: features = np.array(features).reshape(-1, self.model_module.N_MEL_BANDS, self.model_module.SEGMENT_DUR, 1) return features
Example #4
Source File: inception_v4.py From cnn_evaluation_smoke with GNU General Public License v3.0 | 6 votes |
def block_inception_a(input): if K.image_dim_ordering() == "th": channel_axis = 1 else: channel_axis = -1 branch_0 = conv2d_bn(input, 96, 1, 1) branch_1 = conv2d_bn(input, 64, 1, 1) branch_1 = conv2d_bn(branch_1, 96, 3, 3) branch_2 = conv2d_bn(input, 64, 1, 1) branch_2 = conv2d_bn(branch_2, 96, 3, 3) branch_2 = conv2d_bn(branch_2, 96, 3, 3) branch_3 = AveragePooling2D((3,3), strides=(1,1), border_mode='same')(input) branch_3 = conv2d_bn(branch_3, 96, 1, 1) x = merge([branch_0, branch_1, branch_2, branch_3], mode='concat', concat_axis=channel_axis) return x
Example #5
Source File: features.py From detection-2016-nipsws with MIT License | 6 votes |
def get_conv_image_descriptor_for_image(image, model): im = cv2.resize(image, (224, 224)).astype(np.float32) dim_ordering = K.image_dim_ordering() if dim_ordering == 'th': # 'RGB'->'BGR' im = im[::-1, :, :] # Zero-center by mean pixel im[0, :, :] -= 103.939 im[1, :, :] -= 116.779 im[2, :, :] -= 123.68 else: # 'RGB'->'BGR' im = im[:, :, ::-1] # Zero-center by mean pixel im[:, :, 0] -= 103.939 im[:, :, 1] -= 116.779 im[:, :, 2] -= 123.68 im = im.transpose((2, 0, 1)) im = np.expand_dims(im, axis=0) inputs = [K.learning_phase()] + model.inputs _convout1_f = K.function(inputs, [model.layers[31].output]) return _convout1_f([0] + [im])
Example #6
Source File: inception_v4.py From cnn_evaluation_smoke with GNU General Public License v3.0 | 6 votes |
def block_reduction_a(input): if K.image_dim_ordering() == "th": channel_axis = 1 else: channel_axis = -1 branch_0 = conv2d_bn(input, 384, 3, 3, subsample=(2,2), border_mode='valid') branch_1 = conv2d_bn(input, 192, 1, 1) branch_1 = conv2d_bn(branch_1, 224, 3, 3) branch_1 = conv2d_bn(branch_1, 256, 3, 3, subsample=(2,2), border_mode='valid') branch_2 = MaxPooling2D((3,3), strides=(2,2), border_mode='valid')(input) x = merge([branch_0, branch_1, branch_2], mode='concat', concat_axis=channel_axis) return x
Example #7
Source File: inception_v4.py From cnn_evaluation_smoke with GNU General Public License v3.0 | 6 votes |
def block_inception_b(input): if K.image_dim_ordering() == "th": channel_axis = 1 else: channel_axis = -1 branch_0 = conv2d_bn(input, 384, 1, 1) branch_1 = conv2d_bn(input, 192, 1, 1) branch_1 = conv2d_bn(branch_1, 224, 1, 7) branch_1 = conv2d_bn(branch_1, 256, 7, 1) branch_2 = conv2d_bn(input, 192, 1, 1) branch_2 = conv2d_bn(branch_2, 192, 7, 1) branch_2 = conv2d_bn(branch_2, 224, 1, 7) branch_2 = conv2d_bn(branch_2, 224, 7, 1) branch_2 = conv2d_bn(branch_2, 256, 1, 7) branch_3 = AveragePooling2D((3,3), strides=(1,1), border_mode='same')(input) branch_3 = conv2d_bn(branch_3, 128, 1, 1) x = merge([branch_0, branch_1, branch_2, branch_3], mode='concat', concat_axis=channel_axis) return x
Example #8
Source File: features.py From detection-2016-nipsws with MIT License | 6 votes |
def get_image_descriptor_for_image(image, model): im = cv2.resize(image, (224, 224)).astype(np.float32) dim_ordering = K.image_dim_ordering() if dim_ordering == 'th': # 'RGB'->'BGR' im = im[::-1, :, :] # Zero-center by mean pixel im[0, :, :] -= 103.939 im[1, :, :] -= 116.779 im[2, :, :] -= 123.68 else: # 'RGB'->'BGR' im = im[:, :, ::-1] # Zero-center by mean pixel im[:, :, 0] -= 103.939 im[:, :, 1] -= 116.779 im[:, :, 2] -= 123.68 im = im.transpose((2, 0, 1)) im = np.expand_dims(im, axis=0) inputs = [K.learning_phase()] + model.inputs _convout1_f = K.function(inputs, [model.layers[33].output]) return _convout1_f([0] + [im])
Example #9
Source File: features.py From detection-2016-nipsws with MIT License | 6 votes |
def get_feature_map_4(model, im): im = im.astype(np.float32) dim_ordering = K.image_dim_ordering() if dim_ordering == 'th': # 'RGB'->'BGR' im = im[::-1, :, :] # Zero-center by mean pixel im[0, :, :] -= 103.939 im[1, :, :] -= 116.779 im[2, :, :] -= 123.68 else: # 'RGB'->'BGR' im = im[:, :, ::-1] # Zero-center by mean pixel im[:, :, 0] -= 103.939 im[:, :, 1] -= 116.779 im[:, :, 2] -= 123.68 im = im.transpose((2, 0, 1)) im = np.expand_dims(im, axis=0) inputs = [K.learning_phase()] + model.inputs _convout1_f = K.function(inputs, [model.layers[23].output]) feature_map = _convout1_f([0] + [im]) feature_map = np.array([feature_map]) feature_map = feature_map[0, 0, 0, :, :, :] return feature_map
Example #10
Source File: inception_v4.py From cnn_evaluation_smoke with GNU General Public License v3.0 | 6 votes |
def block_reduction_b(input): if K.image_dim_ordering() == "th": channel_axis = 1 else: channel_axis = -1 branch_0 = conv2d_bn(input, 192, 1, 1) branch_0 = conv2d_bn(branch_0, 192, 3, 3, subsample=(2, 2), border_mode='valid') branch_1 = conv2d_bn(input, 256, 1, 1) branch_1 = conv2d_bn(branch_1, 256, 1, 7) branch_1 = conv2d_bn(branch_1, 320, 7, 1) branch_1 = conv2d_bn(branch_1, 320, 3, 3, subsample=(2,2), border_mode='valid') branch_2 = MaxPooling2D((3, 3), strides=(2, 2), border_mode='valid')(input) x = merge([branch_0, branch_1, branch_2], mode='concat', concat_axis=channel_axis) return x
Example #11
Source File: losses.py From FasterRCNN_KERAS with Apache License 2.0 | 6 votes |
def rpn_loss_regr(num_anchors): def rpn_loss_regr_fixed_num(y_true, y_pred): if K.image_dim_ordering() == 'th': x = y_true[:, 4 * num_anchors:, :, :] - y_pred x_abs = K.abs(x) x_bool = K.less_equal(x_abs, 1.0) return lambda_rpn_regr * K.sum( y_true[:, :4 * num_anchors, :, :] * (x_bool * (0.5 * x * x) + (1 - x_bool) * (x_abs - 0.5))) / K.sum(epsilon + y_true[:, :4 * num_anchors, :, :]) else: x = y_true[:, :, :, 4 * num_anchors:] - y_pred x_abs = K.abs(x) x_bool = K.cast(K.less_equal(x_abs, 1.0), tf.float32) return lambda_rpn_regr * K.sum( y_true[:, :, :, :4 * num_anchors] * (x_bool * (0.5 * x * x) + (1 - x_bool) * (x_abs - 0.5))) / K.sum(epsilon + y_true[:, :, :, :4 * num_anchors]) return rpn_loss_regr_fixed_num
Example #12
Source File: grad_cam.py From face_classification with MIT License | 6 votes |
def deprocess_image(x): """ Same normalization as in: https://github.com/fchollet/keras/blob/master/examples/conv_filter_visualization.py """ if np.ndim(x) > 3: x = np.squeeze(x) # normalize tensor: center on 0., ensure std is 0.1 x = x - x.mean() x = x / (x.std() + 1e-5) x = x * 0.1 # clip to [0, 1] x = x + 0.5 x = np.clip(x, 0, 1) # convert to RGB array x = x * 255 if K.image_dim_ordering() == 'th': x = x.transpose((1, 2, 0)) x = np.clip(x, 0, 255).astype('uint8') return x
Example #13
Source File: imagenet_utils.py From DeepLearning with MIT License | 6 votes |
def preprocess_input(x, dim_ordering='default'): if dim_ordering == 'default': dim_ordering = K.image_dim_ordering() assert dim_ordering in {'tf', 'th'} if dim_ordering == 'th': x[:, 0, :, :] -= 103.939 x[:, 1, :, :] -= 116.779 x[:, 2, :, :] -= 123.68 # 'RGB'->'BGR' x = x[:, ::-1, :, :] else: x[:, :, :, 0] -= 103.939 x[:, :, :, 1] -= 116.779 x[:, :, :, 2] -= 123.68 # 'RGB'->'BGR' x = x[:, :, :, ::-1] return x
Example #14
Source File: inception_v3.py From DeepLearning with MIT License | 6 votes |
def conv2d_bn(x, nb_filter, nb_row, nb_col, border_mode='same', subsample=(1, 1), name=None): '''Utility function to apply conv + BN. ''' if name is not None: bn_name = name + '_bn' conv_name = name + '_conv' else: bn_name = None conv_name = None if K.image_dim_ordering() == 'th': bn_axis = 1 else: bn_axis = 3 x = Convolution2D(nb_filter, nb_row, nb_col, subsample=subsample, activation='relu', border_mode=border_mode, name=conv_name)(x) x = BatchNormalization(axis=bn_axis, name=bn_name)(x) return x
Example #15
Source File: model.py From udacity-SDC-baseline with MIT License | 6 votes |
def build_vgg16(image_size=None): image_size = image_size or (240, 240) if K.image_dim_ordering() == 'th': input_shape = (3,) + image_size else: input_shape = image_size + (3, ) bottleneck_model = vgg16.VGG16(include_top=False, input_tensor=Input(input_shape)) #bottleneck_model.trainable = False for layer in bottleneck_model.layers: layer.trainable = False x = bottleneck_model.input y = bottleneck_model.output y = Flatten()(y) y = BatchNormalization()(y) y = Dense(2048, activation='relu')(y) y = Dropout(.5)(y) y = Dense(1024, activation='relu')(y) y = Dropout(.5)(y) y = Dense(1)(y) model = Model(input=x, output=y) model.compile(optimizer=Adam(lr=1e-4), loss = 'mse') return model
Example #16
Source File: image.py From deep-mil-for-whole-mammogram-classification with MIT License | 6 votes |
def array_to_img(x, dim_ordering='default', scale=True): from PIL import Image if dim_ordering == 'default': dim_ordering = K.image_dim_ordering() if dim_ordering == 'th': x = x.transpose(1, 2, 0) if scale: x += max(-np.min(x), 0) x_max = np.max(x) if x_max != 0: x /= x_max x *= 255 if x.shape[2] == 3: # RGB return Image.fromarray(x.astype('uint8'), 'RGB') elif x.shape[2] == 1: # grayscale return Image.fromarray(x[:, :, 0].astype('uint8'), 'L') else: raise Exception('Unsupported channel number: ', x.shape[2])
Example #17
Source File: image.py From deep-mil-for-whole-mammogram-classification with MIT License | 6 votes |
def img_to_array(img, dim_ordering='default'): if dim_ordering == 'default': dim_ordering = K.image_dim_ordering() if dim_ordering not in ['th', 'tf']: raise Exception('Unknown dim_ordering: ', dim_ordering) # image has dim_ordering (height, width, channel) x = np.asarray(img, dtype='float32') if len(x.shape) == 3: if dim_ordering == 'th': x = x.transpose(2, 0, 1) elif len(x.shape) == 2: if dim_ordering == 'th': x = x.reshape((1, x.shape[0], x.shape[1])) else: x = x.reshape((x.shape[0], x.shape[1], 1)) else: raise Exception('Unsupported image shape: ', x.shape) return x
Example #18
Source File: image.py From deep-mil-for-whole-mammogram-classification with MIT License | 6 votes |
def __init__(self, X, y, image_data_generator, batch_size=32, shuffle=False, seed=None, dim_ordering='default', save_to_dir=None, save_prefix='', save_format='jpeg'): if y is not None and len(X) != len(y): raise Exception('X (images tensor) and y (labels) ' 'should have the same length. ' 'Found: X.shape = %s, y.shape = %s' % (np.asarray(X).shape, np.asarray(y).shape)) if dim_ordering == 'default': dim_ordering = K.image_dim_ordering() self.X = X self.y = y self.image_data_generator = image_data_generator self.dim_ordering = dim_ordering self.save_to_dir = save_to_dir self.save_prefix = save_prefix self.save_format = save_format super(NumpyArrayIterator, self).__init__(X.shape[0], batch_size, shuffle, seed)
Example #19
Source File: losses.py From keras-frcnn with Apache License 2.0 | 6 votes |
def rpn_loss_regr(num_anchors): def rpn_loss_regr_fixed_num(y_true, y_pred): if K.image_dim_ordering() == 'th': x = y_true[:, 4 * num_anchors:, :, :] - y_pred x_abs = K.abs(x) x_bool = K.less_equal(x_abs, 1.0) return lambda_rpn_regr * K.sum( y_true[:, :4 * num_anchors, :, :] * (x_bool * (0.5 * x * x) + (1 - x_bool) * (x_abs - 0.5))) / K.sum(epsilon + y_true[:, :4 * num_anchors, :, :]) else: x = y_true[:, :, :, 4 * num_anchors:] - y_pred x_abs = K.abs(x) x_bool = K.cast(K.less_equal(x_abs, 1.0), tf.float32) return lambda_rpn_regr * K.sum( y_true[:, :, :, :4 * num_anchors] * (x_bool * (0.5 * x * x) + (1 - x_bool) * (x_abs - 0.5))) / K.sum(epsilon + y_true[:, :, :, :4 * num_anchors]) return rpn_loss_regr_fixed_num
Example #20
Source File: losses.py From Keras-FasterRCNN with MIT License | 6 votes |
def rpn_loss_regr(num_anchors): def rpn_loss_regr_fixed_num(y_true, y_pred): if K.image_dim_ordering() == 'th': x = y_true[:, 4 * num_anchors:, :, :] - y_pred x_abs = K.abs(x) x_bool = K.less_equal(x_abs, 1.0) return lambda_rpn_regr * K.sum( y_true[:, :4 * num_anchors, :, :] * (x_bool * (0.5 * x * x) + (1 - x_bool) * (x_abs - 0.5))) / K.sum(epsilon + y_true[:, :4 * num_anchors, :, :]) else: x = y_true[:, :, :, 4 * num_anchors:] - y_pred x_abs = K.abs(x) x_bool = K.cast(K.less_equal(x_abs, 1.0), tf.float32) return lambda_rpn_regr * K.sum( y_true[:, :, :, :4 * num_anchors] * (x_bool * (0.5 * x * x) + (1 - x_bool) * (x_abs - 0.5))) / K.sum(epsilon + y_true[:, :, :, :4 * num_anchors]) return rpn_loss_regr_fixed_num
Example #21
Source File: train.py From FaceLock with MIT License | 6 votes |
def predict(self, image, img_channels=3): if K.image_dim_ordering() == 'th' and image.shape != (1,img_channels, IMAGE_SIZE, IMAGE_SIZE): image = resize_with_pad(image) image = image.reshape((1, img_channels, IMAGE_SIZE, IMAGE_SIZE)) elif K.image_dim_ordering() == 'tf' and image.shape != (1, IMAGE_SIZE, IMAGE_SIZE, img_channels): image = resize_with_pad(image) image = image.reshape((1, IMAGE_SIZE, IMAGE_SIZE, img_channels)) image = image.astype('float32') image /= 255 if DEBUG_MUTE: result = self.model.predict_proba(image, verbose=0) result = self.model.predict_classes(image, verbose=0) else: result = self.model.predict_proba(image) print(result) result = self.model.predict_classes(image) print(result) return result[0]
Example #22
Source File: densenet_fast.py From semantic-embeddings with MIT License | 6 votes |
def transition_block(ip, nb_filter, dropout_rate=None, weight_decay=1E-4): ''' Apply BatchNorm, Relu 1x1, Conv2D, optional dropout and Maxpooling2D Args: ip: keras tensor nb_filter: number of filters dropout_rate: dropout rate weight_decay: weight decay factor Returns: keras tensor, after applying batch_norm, relu-conv, dropout, maxpool ''' concat_axis = 1 if K.image_dim_ordering() == "th" else -1 x = Convolution2D(nb_filter, 1, 1, init="he_uniform", border_mode="same", bias=False, W_regularizer=l2(weight_decay))(ip) if dropout_rate: x = Dropout(dropout_rate)(x) x = AveragePooling2D((2, 2), strides=(2, 2))(x) x = BatchNormalization(mode=0, axis=concat_axis, gamma_regularizer=l2(weight_decay), beta_regularizer=l2(weight_decay))(x) return x
Example #23
Source File: grad_cam.py From Face-and-Emotion-Recognition with MIT License | 6 votes |
def deprocess_image(x): """ Same normalization as in: https://github.com/fchollet/keras/blob/master/examples/conv_filter_visualization.py """ if np.ndim(x) > 3: x = np.squeeze(x) # normalize tensor: center on 0., ensure std is 0.1 x = x - x.mean() x = x / (x.std() + 1e-5) x = x * 0.1 # clip to [0, 1] x = x + 0.5 x = np.clip(x, 0, 1) # convert to RGB array x = x * 255 if K.image_dim_ordering() == 'th': x = x.transpose((1, 2, 0)) x = np.clip(x, 0, 255).astype('uint8') return x
Example #24
Source File: custom.py From WannaPark with GNU General Public License v3.0 | 6 votes |
def call(self, x, mask=None): if K.image_dim_ordering == "th": _, f, r, c = self.shape else: _, r, c, f = self.shape squared = K.square(x) pooled = K.pool2d(squared, (self.n, self.n), strides=(1, 1), padding="same", pool_mode="avg") if K.image_dim_ordering == "th": summed = K.sum(pooled, axis=1, keepdims=True) averaged = self.alpha * K.repeat_elements(summed, f, axis=1) else: summed = K.sum(pooled, axis=3, keepdims=True) averaged = self.alpha * K.repeat_elements(summed, f, axis=3) denom = K.pow(self.k + averaged, self.beta) return x / denom
Example #25
Source File: neural_styler.py From style-transfer with MIT License | 5 votes |
def loss(self, x): # reshape if K.image_dim_ordering() == 'th': x = x.reshape((1, 3, self.img_nrows, self.img_ncols)) else: x = x.reshape((1, self.img_nrows, self.img_ncols, 3)) outs = self.loss_and_grads([x]) loss_value = outs[0] return loss_value
Example #26
Source File: neural_styler.py From style-transfer with MIT License | 5 votes |
def grads(self, x): # reshape if K.image_dim_ordering() == 'th': x = x.reshape((1, 3, self.img_nrows, self.img_ncols)) else: x = x.reshape((1, self.img_nrows, self.img_ncols, 3)) outs = self.loss_and_grads([x]) if len(outs[1:]) == 1: grad_values = outs[1].flatten().astype('float64') else: grad_values = np.array(outs[1:]).flatten().astype('float64') return grad_values
Example #27
Source File: SSRNET_model.py From FSA-Net with Apache License 2.0 | 5 votes |
def __init__(self, image_size,num_classes,stage_num,lambda_d): if K.image_dim_ordering() == "th": logging.debug("image_dim_ordering = 'th'") self._channel_axis = 1 self._input_shape = (3, image_size, image_size) else: logging.debug("image_dim_ordering = 'tf'") self._channel_axis = -1 self._input_shape = (image_size, image_size, 3) self.num_classes = num_classes self.stage_num = stage_num self.lambda_d = lambda_d
Example #28
Source File: densenet_fast.py From semantic-embeddings with MIT License | 5 votes |
def dense_block(x, nb_layers, nb_filter, growth_rate, dropout_rate=None, weight_decay=1E-4): ''' Build a dense_block where the output of each conv_block is fed to subsequent ones Args: x: keras tensor nb_layers: the number of layers of conv_block to append to the model. nb_filter: number of filters growth_rate: growth rate dropout_rate: dropout rate weight_decay: weight decay factor Returns: keras tensor with nb_layers of conv_block appended ''' concat_axis = 1 if K.image_dim_ordering() == "th" else -1 feature_list = [x] for i in range(nb_layers): x = conv_block(x, growth_rate, dropout_rate, weight_decay) feature_list.append(x) x = merge(feature_list, mode='concat', concat_axis=concat_axis) nb_filter += growth_rate return x, nb_filter
Example #29
Source File: FEC.py From FECNet with MIT License | 5 votes |
def dense_block(x, nb_layers, nb_filter, growth_rate, dropout_rate=None, weight_decay=1E-4): concat_axis = 1 if K.image_dim_ordering() == "th" else -1 feature_list = [x] for i in range(nb_layers): x = conv_block(x, growth_rate, dropout_rate, weight_decay) feature_list.append(x) x = Concatenate(axis=concat_axis)(feature_list) nb_filter += growth_rate return x, nb_filter
Example #30
Source File: SpatialPyramidPooling.py From FSA-Net with Apache License 2.0 | 5 votes |
def __init__(self, pool_list, pool_type, **kwargs): self.dim_ordering = K.image_dim_ordering() assert self.dim_ordering in {'tf', 'th'}, 'dim_ordering must be in {tf, th}' self.pool_list = pool_list self.pool_type = pool_type self.num_outputs_per_channel = sum([i * i for i in pool_list]) super(SpatialPyramidPooling, self).__init__(**kwargs)