Python keras.backend.backend() Examples

The following are 30 code examples of keras.backend.backend(). 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: util.py    From keras-transfer-learning-for-oxford102 with MIT License 7 votes vote down vote up
def set_img_format():
    try:
        if K.backend() == 'theano':
            K.set_image_data_format('channels_first')
        else:
            K.set_image_data_format('channels_last')
    except AttributeError:
        if K._BACKEND == 'theano':
            K.set_image_dim_ordering('th')
        else:
            K.set_image_dim_ordering('tf') 
Example #2
Source File: inception_resnet_v2.py    From Keras-FasterRCNN with MIT License 6 votes vote down vote up
def classifier(base_layers, input_rois, num_rois, nb_classes=21, trainable=False):

    # compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround

    if K.backend() == 'tensorflow':
        pooling_regions = 14
        # Changed the input shape to 1088 from 1024 because of nn_base's output being 1088. Not sure if this is correct
        input_shape = (num_rois, 14, 14, 1088)
    elif K.backend() == 'theano':
        pooling_regions = 7
        input_shape = (num_rois, 1024, 7, 7)

    out_roi_pool = RoiPoolingConv(pooling_regions, num_rois)([base_layers, input_rois])
    out = classifier_layers(out_roi_pool, input_shape=input_shape, trainable=True)

    out = TimeDistributed(Flatten())(out)

    out_class = TimeDistributed(Dense(nb_classes, activation='softmax', kernel_initializer='zero'), name='dense_class_{}'.format(nb_classes))(out)
    # note: no regression target for bg class
    out_regr = TimeDistributed(Dense(4 * (nb_classes-1), activation='linear', kernel_initializer='zero'), name='dense_regress_{}'.format(nb_classes))(out)
    return [out_class, out_regr] 
Example #3
Source File: test_transformer.py    From BERT-keras with GNU General Public License v3.0 6 votes vote down vote up
def test_save_load_all(self):
        for backend in self.list_backends():
            try:
                set_keras_backend(backend)
            except ModuleNotFoundError:
                continue
            K.set_learning_phase(0)  # test
            for use_attn_mask in [True, False]:
                model = self.create_small_model(use_attn_mask)
                path = '/tmp/{}.model'.format(uuid.uuid4())
                try:
                    model.save(path)
                    new_model = keras.models.load_model(path, custom_objects={'MultiHeadAttention': MultiHeadAttention,
                                                                              'LayerNormalization': LayerNormalization,
                                                                              'Gelu': Gelu})
                    TestTransformer.compare_two_models(model, new_model)
                except Exception as e:
                    raise e
                finally:
                    if os.path.exists(path):
                        os.remove(path) 
Example #4
Source File: test_transformer.py    From BERT-keras with GNU General Public License v3.0 6 votes vote down vote up
def test_save_load_weights(self):
        for backend in self.list_backends():
            try:
                set_keras_backend(backend)
            except ModuleNotFoundError:
                continue
            K.set_learning_phase(0)  # test
            for use_attn_mask in [True, False]:
                model = self.create_small_model(use_attn_mask)
                path = '/tmp/{}.model'.format(uuid.uuid4())
                try:
                    model.save_weights(path)
                    model.load_weights(path)
                except Exception as e:
                    raise e
                finally:
                    if os.path.exists(path):
                        os.remove(path) 
Example #5
Source File: common.py    From imgclsmob with MIT License 6 votes vote down vote up
def swish(x,
          name="swish"):
    """
    Swish activation function from 'Searching for Activation Functions,' https://arxiv.org/abs/1710.05941.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    name : str, default 'swish'
        Block name.

    Returns
    -------
    keras.backend tensor/variable/symbol
        Resulted tensor/variable/symbol.
    """
    w = nn.Activation("sigmoid", name=name + "/sigmoid")(x)
    x = nn.multiply([x, w], name=name + "/mul")
    return x 
Example #6
Source File: resnet.py    From Keras-FasterRCNN with MIT License 6 votes vote down vote up
def classifier(base_layers, input_rois, num_rois, nb_classes=21, trainable=False):

    # compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround

    if K.backend() == 'tensorflow':
        pooling_regions = 14
        input_shape = (num_rois, 14, 14, 1024)
    elif K.backend() == 'theano':
        pooling_regions = 7
        input_shape = (num_rois, 1024, 7, 7)

    out_roi_pool = RoiPoolingConv(pooling_regions, num_rois)([base_layers, input_rois])
    out = classifier_layers(out_roi_pool, input_shape=input_shape, trainable=True)

    out = TimeDistributed(Flatten())(out)

    out_class = TimeDistributed(Dense(nb_classes, activation='softmax', kernel_initializer='zero'), name='dense_class_{}'.format(nb_classes))(out)
    # note: no regression target for bg class
    out_regr = TimeDistributed(Dense(4 * (nb_classes-1), activation='linear', kernel_initializer='zero'), name='dense_regress_{}'.format(nb_classes))(out)
    return [out_class, out_regr] 
Example #7
Source File: resnet.py    From keras-frcnn with Apache License 2.0 6 votes vote down vote up
def classifier(base_layers, input_rois, num_rois, nb_classes = 21, trainable=False):

    # compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround

    if K.backend() == 'tensorflow':
        pooling_regions = 14
        input_shape = (num_rois,14,14,1024)
    elif K.backend() == 'theano':
        pooling_regions = 7
        input_shape = (num_rois,1024,7,7)

    out_roi_pool = RoiPoolingConv(pooling_regions, num_rois)([base_layers, input_rois])
    out = classifier_layers(out_roi_pool, input_shape=input_shape, trainable=True)

    out = TimeDistributed(Flatten())(out)

    out_class = TimeDistributed(Dense(nb_classes, activation='softmax', kernel_initializer='zero'), name='dense_class_{}'.format(nb_classes))(out)
    # note: no regression target for bg class
    out_regr = TimeDistributed(Dense(4 * (nb_classes-1), activation='linear', kernel_initializer='zero'), name='dense_regress_{}'.format(nb_classes))(out)
    return [out_class, out_regr] 
Example #8
Source File: vgg.py    From keras-frcnn with Apache License 2.0 6 votes vote down vote up
def classifier(base_layers, input_rois, num_rois, nb_classes = 21, trainable=False):

    # compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround

    if K.backend() == 'tensorflow':
        pooling_regions = 7
        input_shape = (num_rois,7,7,512)
    elif K.backend() == 'theano':
        pooling_regions = 7
        input_shape = (num_rois,512,7,7)

    out_roi_pool = RoiPoolingConv(pooling_regions, num_rois)([base_layers, input_rois])

    out = TimeDistributed(Flatten(name='flatten'))(out_roi_pool)
    out = TimeDistributed(Dense(4096, activation='relu', name='fc1'))(out)
    out = TimeDistributed(Dropout(0.5))(out)
    out = TimeDistributed(Dense(4096, activation='relu', name='fc2'))(out)
    out = TimeDistributed(Dropout(0.5))(out)

    out_class = TimeDistributed(Dense(nb_classes, activation='softmax', kernel_initializer='zero'), name='dense_class_{}'.format(nb_classes))(out)
    # note: no regression target for bg class
    out_regr = TimeDistributed(Dense(4 * (nb_classes-1), activation='linear', kernel_initializer='zero'), name='dense_regress_{}'.format(nb_classes))(out)

    return [out_class, out_regr] 
Example #9
Source File: vgg.py    From Keras-FasterRCNN with MIT License 6 votes vote down vote up
def classifier(base_layers, input_rois, num_rois, nb_classes = 21, trainable=False):

    # compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround

    if K.backend() == 'tensorflow':
        pooling_regions = 7
        input_shape = (num_rois, 7, 7, 512)
    elif K.backend() == 'theano':
        pooling_regions = 7
        input_shape = (num_rois, 512, 7, 7)

    out_roi_pool = RoiPoolingConv(pooling_regions, num_rois)([base_layers, input_rois])

    out = TimeDistributed(Flatten(name='flatten'))(out_roi_pool)
    out = TimeDistributed(Dense(4096, activation='relu', name='fc1'))(out)
    out = TimeDistributed(Dense(4096, activation='relu', name='fc2'))(out)

    out_class = TimeDistributed(Dense(nb_classes, activation='softmax', kernel_initializer='zero'), name='dense_class_{}'.format(nb_classes))(out)
    # note: no regression target for bg class
    out_regr = TimeDistributed(Dense(4 * (nb_classes-1), activation='linear', kernel_initializer='zero'), name='dense_regress_{}'.format(nb_classes))(out)

    return [out_class, out_regr] 
Example #10
Source File: devol.py    From devol with MIT License 6 votes vote down vote up
def _handle_broken_model(self, model, error):
        del model

        n = self.genome_handler.n_classes
        loss = log_loss(np.concatenate(([1], np.zeros(n - 1))), np.ones(n) / n)
        accuracy = 1 / n
        gc.collect()

        if K.backend() == 'tensorflow':
            K.clear_session()
            tf.reset_default_graph()

        print('An error occurred and the model could not train:')
        print(error)
        print(('Model assigned poor score. Please ensure that your model'
               'constraints live within your computational resources.'))
        return loss, accuracy 
Example #11
Source File: utils.py    From keras-transformer with MIT License 6 votes vote down vote up
def contain_tf_gpu_mem_usage():
    """
    By default TensorFlow may try to reserve all available GPU memory
    making it impossible to train multiple networks at once.
    This function will disable such behaviour in TensorFlow.
    """
    from keras import backend
    if backend.backend() != 'tensorflow':
        return
    try:
        # noinspection PyPackageRequirements
        import tensorflow as tf
    except ImportError:
        pass
    else:
        from keras.backend.tensorflow_backend import set_session
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True  # dynamically grow the memory
        sess = tf.Session(config=config)
        set_session(sess) 
Example #12
Source File: test_transformer.py    From BERT with Apache License 2.0 6 votes vote down vote up
def test_save_load_weights(self):
        for backend in self.list_backends():
            try:
                set_keras_backend(backend)
            except ModuleNotFoundError:
                continue
            K.set_learning_phase(0)  # test
            for use_attn_mask in [True, False]:
                model = self.create_small_model(use_attn_mask)
                path = '/tmp/{}.model'.format(uuid.uuid4())
                try:
                    model.save_weights(path)
                    model.load_weights(path)
                except Exception as e:
                    raise e
                finally:
                    if os.path.exists(path):
                        os.remove(path) 
Example #13
Source File: test_transformer.py    From BERT with Apache License 2.0 6 votes vote down vote up
def test_save_load_all(self):
        for backend in self.list_backends():
            try:
                set_keras_backend(backend)
            except ModuleNotFoundError:
                continue
            K.set_learning_phase(0)  # test
            for use_attn_mask in [True, False]:
                model = self.create_small_model(use_attn_mask)
                path = '/tmp/{}.model'.format(uuid.uuid4())
                try:
                    model.save(path)
                    new_model = keras.models.load_model(path, custom_objects={'MultiHeadAttention': MultiHeadAttention,
                                                                              'LayerNormalization': LayerNormalization,
                                                                              'Gelu': Gelu})
                    TestTransformer.compare_two_models(model, new_model)
                except Exception as e:
                    raise e
                finally:
                    if os.path.exists(path):
                        os.remove(path) 
Example #14
Source File: nn_arch_resnet50.py    From Keras_object_detection with Apache License 2.0 6 votes vote down vote up
def classifier(base_layers, input_rois, num_rois, nb_classes = 21, trainable=True):

    # compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround

    if K.backend() == 'tensorflow':
        pooling_regions = 14
        input_shape = (num_rois,14,14,1024)
    elif K.backend() == 'theano':
        raise ValueError("Theano backend not supported")

    out_roi_pool = RoiPoolingConv(pooling_regions, num_rois,trainable=trainable)([base_layers, input_rois])
    out = classifier_layers(out_roi_pool, input_shape=input_shape, trainable=True)

    out = TimeDistributed(Flatten())(out)

    out_class = TimeDistributed(Dense(nb_classes, activation='softmax', kernel_initializer='zero',trainable=trainable), name='dense_class_{}'.format(nb_classes),trainable=trainable)(out)
    # note: no regression target for bg class
    out_regr = TimeDistributed(Dense(4 * (nb_classes-1), activation='linear', kernel_initializer='zero',trainable=trainable), name='dense_regress_{}'.format(nb_classes),trainable=trainable)(out)
    return [out_class, out_regr] 
Example #15
Source File: Keras_utils.py    From coling2018_fake-news-challenge with Apache License 2.0 6 votes vote down vote up
def __init__(self, log_dir='./logs',
                 histogram_freq=0,
                 batch_size=32,
                 write_graph=True,
                 write_grads=False,
                 write_images=False,
                 embeddings_freq=0,
                 embeddings_layer_names=None,
                 embeddings_metadata=None):
        super(TensorBoard, self).__init__()
        if K.backend() != 'tensorflow':
            raise RuntimeError('TensorBoard callback only works '
                               'with the TensorFlow backend.')
        self.log_dir = log_dir
        self.histogram_freq = histogram_freq
        self.merged = None
        self.write_graph = write_graph
        self.write_grads = write_grads
        self.write_images = write_images
        self.embeddings_freq = embeddings_freq
        self.embeddings_layer_names = embeddings_layer_names
        self.embeddings_metadata = embeddings_metadata or {}
        self.batch_size = batch_size 
Example #16
Source File: keras_patches.py    From keras-image-captioning with MIT License 6 votes vote down vote up
def clip_norm(g, c, n):
    if c > 0:
        if K.backend() == 'tensorflow':
            import tensorflow as tf
            import copy
            condition = n >= c
            then_expression = tf.scalar_mul(c / n, g)
            else_expression = g

            if hasattr(then_expression, 'get_shape'):
                g_shape = copy.copy(then_expression.get_shape())
            elif hasattr(then_expression, 'dense_shape'):
                g_shape = copy.copy(then_expression.dense_shape)
            if condition.dtype != tf.bool:
                condition = tf.cast(condition, 'bool')
            g = K.tensorflow_backend.control_flow_ops.cond(
                condition, lambda: then_expression, lambda: else_expression)
            if hasattr(then_expression, 'get_shape'):
                g.set_shape(g_shape)
            elif hasattr(then_expression, 'dense_shape'):
                g._dense_shape = g_shape
        else:
            g = K.switch(n >= c, g * c / n, g)
    return g 
Example #17
Source File: resnet.py    From FasterRCNN_KERAS with Apache License 2.0 6 votes vote down vote up
def classifier(base_layers, input_rois, num_rois, nb_classes = 21, trainable=False):

    # compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround

    if K.backend() == 'tensorflow':
        pooling_regions = 14
        input_shape = (num_rois,14,14,1024)
    elif K.backend() == 'theano':
        pooling_regions = 7
        input_shape = (num_rois,1024,7,7)

    out_roi_pool = RoiPoolingConv(pooling_regions, num_rois)([base_layers, input_rois])
    out = classifier_layers(out_roi_pool, input_shape=input_shape, trainable=True)

    out = TimeDistributed(Flatten())(out)

    out_class = TimeDistributed(Dense(nb_classes, activation='softmax', kernel_initializer='zero'), name='dense_class_{}'.format(nb_classes))(out)
    # note: no regression target for bg class
    out_regr = TimeDistributed(Dense(4 * (nb_classes-1), activation='linear', kernel_initializer='zero'), name='dense_regress_{}'.format(nb_classes))(out)
    return [out_class, out_regr] 
Example #18
Source File: attention_custom.py    From coling2018_fake-news-challenge with Apache License 2.0 5 votes vote down vote up
def dot_product(x, kernel):
    """
    Wrapper for dot product operation, in order to be compatible with both
    Theano and Tensorflow
    Args:
        x (): input
        kernel (): weights
    Returns:
    """
    if K.backend() == 'tensorflow':
        return K.squeeze(K.dot(x, K.expand_dims(kernel)), axis=-1)
    else:
        return K.dot(x, kernel) 
Example #19
Source File: nn_arch_resnet50.py    From Keras_object_detection with Apache License 2.0 5 votes vote down vote up
def classifier_layers(x, input_shape, trainable=False):

    # compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround
    # (hence a smaller stride in the region that follows the ROI pool)
    if K.backend() == 'tensorflow':
        x = conv_block_td(x, 3, [512, 512, 2048], stage=5, block='a', input_shape=input_shape, strides=(2, 2), trainable=trainable)
    elif K.backend() == 'theano':
        x = conv_block_td(x, 3, [512, 512, 2048], stage=5, block='a', input_shape=input_shape, strides=(1, 1), trainable=trainable)

    x = identity_block_td(x, 3, [512, 512, 2048], stage=5, block='b', trainable=trainable)
    x = identity_block_td(x, 3, [512, 512, 2048], stage=5, block='c', trainable=trainable)
    x = TimeDistributed(AveragePooling2D((7, 7)), name='avg_pool')(x)

    return x 
Example #20
Source File: test_transformer.py    From BERT-keras with GNU General Public License v3.0 5 votes vote down vote up
def set_keras_backend(backend):
    global K
    if K.backend() != backend:
        os.environ['KERAS_BACKEND'] = backend
        reload(K)
        assert K.backend() == backend 
Example #21
Source File: image.py    From keras-utility-layer-collection with MIT License 5 votes vote down vote up
def __init__(self, output_shape, method="bilinear", *kwargs):
        """Initializes a new Resize2D layer.
        
        Arguments:
            output_shape {(int, int)} -- 2D tuple with the (height, width) of the new image
        
        Keyword Arguments:
            method {str} -- The method to use to resize the image. (default: {"bilinear"})
                            Possible values are: bilinear, nearest_neighbor, bicubic, area
        
        Raises:
            ValueError -- ValueError will be raised if arguments are invalid.
        """

        if not isinstance(output_shape, (list, tuple)) or len(output_shape) != 2:
            raise ValueError("`output_shape` must be a tuple or list of length 2.")

        if K.backend() != "tensorflow":
            raise ValueError("This layer is only supported using the tensorflow backend.")

        global tf
        import tensorflow as tf
        allowed_methods = {
            "bilinear": tf.image.ResizeMethod.BILINEAR,
            "nearest_neighbor": tf.image.ResizeMethod.NEAREST_NEIGHBOR,
            "bicubic": tf.image.ResizeMethod.BICUBIC,
            "area": tf.image.ResizeMethod.AREA
        }

        if method not in allowed_methods:
            raise ValueError("`modeĀ“ has to be one of the values: {0}".format(allowed_methods.keys()))


        self._kwargs = kwargs
        self._output_shape = output_shape
        self._method = allowed_methods[method]
        super().__init__(*kwargs) 
Example #22
Source File: layers.py    From keras-utilities with MIT License 5 votes vote down vote up
def dot_product(x, kernel):
    """
    Wrapper for dot product operation, in order to be compatible with both
    Theano and Tensorflow
    Args:
        x (): input
        kernel (): weights
    Returns:
    """
    if K.backend() == 'tensorflow':
        # todo: check that this is correct
        return K.squeeze(K.dot(x, K.expand_dims(kernel)), axis=-1)
    else:
        return K.dot(x, kernel) 
Example #23
Source File: test_switchnorm.py    From keras-switchnorm with MIT License 5 votes vote down vote up
def test_switchnorm_mode_twice():
    # This is a regression test for issue #4881 with the old
    # switch normalization functions in the Theano backend.
    model = Sequential()
    model.add(SwitchNormalization(input_shape=(10, 5, 5), axis=1))
    model.add(SwitchNormalization(input_shape=(10, 5, 5), axis=1))
    model.compile(loss='mse', optimizer='sgd')

    x = np.random.normal(loc=5.0, scale=10.0, size=(20, 10, 5, 5))
    model.fit(x, x, epochs=1, verbose=0)
    model.predict(x) 
Example #24
Source File: test_switchnorm.py    From keras-switchnorm with MIT License 5 votes vote down vote up
def test_basic_switchnorm():
    layer_test(SwitchNormalization,
               kwargs={'momentum': 0.9,
                       'epsilon': 0.1,
                       'gamma_regularizer': regularizers.l2(0.01),
                       'beta_regularizer': regularizers.l2(0.01)},
               input_shape=(3, 4, 2))
    layer_test(SwitchNormalization,
               kwargs={'momentum': 0.9,
                       'epsilon': 0.1,
                       'axis': 1},
               input_shape=(3, 4, 2))
    layer_test(SwitchNormalization,
               kwargs={'gamma_initializer': 'ones',
                       'beta_initializer': 'ones',
                       'moving_mean_initializer': 'zeros',
                       'moving_variance_initializer': 'ones'},
               input_shape=(3, 4, 2, 4))
    if K.backend() != 'theano':
        layer_test(SwitchNormalization,
                   kwargs={'momentum': 0.9,
                           'epsilon': 0.1,
                           'axis': 1,
                           'scale': False,
                           'center': False},
                   input_shape=(3, 4, 2, 4)) 
Example #25
Source File: models.py    From DigiX_HuaWei_Population_Age_Attribution_Predict with MIT License 5 votes vote down vote up
def dot_product(x, kernel):
    """
    Wrapper for dot product operation, in order to be compatible with both
    Theano and Tensorflow
    Args:
        x (): input
        kernel (): weights
    Returns:
    """
    if K.backend() == 'tensorflow':
        return K.squeeze(K.dot(x, K.expand_dims(kernel)), axis=-1)
    else:
        return K.dot(x, kernel) 
Example #26
Source File: resnet.py    From Keras-FasterRCNN with MIT License 5 votes vote down vote up
def classifier_layers(x, input_shape, trainable=False):

    # compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround
    # (hence a smaller stride in the region that follows the ROI pool)
    if K.backend() == 'tensorflow':
        x = conv_block_td(x, 3, [512, 512, 2048], stage=5, block='a', input_shape=input_shape, strides=(2, 2), trainable=trainable)
    elif K.backend() == 'theano':
        x = conv_block_td(x, 3, [512, 512, 2048], stage=5, block='a', input_shape=input_shape, strides=(1, 1), trainable=trainable)

    x = identity_block_td(x, 3, [512, 512, 2048], stage=5, block='b', trainable=trainable)
    x = identity_block_td(x, 3, [512, 512, 2048], stage=5, block='c', trainable=trainable)
    x = TimeDistributed(AveragePooling2D((7, 7)), name='avg_pool')(x)

    return x 
Example #27
Source File: models.py    From DigiX_HuaWei_Population_Age_Attribution_Predict with MIT License 5 votes vote down vote up
def dot_product(x, kernel):
    """
    Wrapper for dot product operation, in order to be compatible with both
    Theano and Tensorflow
    Args:
        x (): input
        kernel (): weights
    Returns:
    """
    if K.backend() == 'tensorflow':
        return K.squeeze(K.dot(x, K.expand_dims(kernel)), axis=-1)
    else:
        return K.dot(x, kernel) 
Example #28
Source File: vgg.py    From Keras-FasterRCNN with MIT License 5 votes vote down vote up
def get_weight_path():
    if K.image_dim_ordering() == 'th':
        print('pretrained weights not available for VGG with theano backend')
        return
    else:
        return 'vgg16_weights_tf_dim_ordering_tf_kernels.h5' 
Example #29
Source File: attention.py    From deephlapan with GNU General Public License v2.0 5 votes vote down vote up
def dot_product(x, kernel):
    if K.backend() == 'tensorflow':
        return K.squeeze(K.dot(x, K.expand_dims(kernel)), axis=-1)
    else:
        return K.dot(x, kernel) 
Example #30
Source File: rnn_feature.py    From DigiX_HuaWei_Population_Age_Attribution_Predict with MIT License 5 votes vote down vote up
def dot_product(x, kernel):
    """
    Wrapper for dot product operation, in order to be compatible with both
    Theano and Tensorflow
    Args:
        x (): input
        kernel (): weights
    Returns:
    """
    if K.backend() == 'tensorflow':
        return K.squeeze(K.dot(x, K.expand_dims(kernel)), axis=-1)
    else:
        return K.dot(x, kernel)