Python keras.backend.backend() Examples

The following are code examples for showing how to use keras.backend.backend(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: FasterRCNN_KERAS   Author: akshaylamba   File: resnet.py    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 2
Project: spark-deep-learning   Author: databricks   File: shared_params.py    Apache License 2.0 6 votes vote down vote up
def _loadTFGraph(self, sess, graph):
        """
        Loads the Keras model into memory, then uses the passed-in session to load the
        model's inference-related ops into the passed-in Tensorflow graph.

        :return: A tuple (graph, input_name, output_name) where graph is the TF graph
        corresponding to the Keras model's inference subgraph, input_name is the name of the
        Keras model's input tensor, and output_name is the name of the Keras model's output tensor.
        """
        keras_backend = K.backend()
        assert keras_backend == "tensorflow", \
            "Only tensorflow-backed Keras models are supported, tried to load Keras model " \
            "with backend %s." % (keras_backend)
        with graph.as_default():
            K.set_learning_phase(0)  # Inference phase
            model = load_model(self.getModelFile())
            out_op_name = tfx.op_name(model.output, graph)
            stripped_graph = tfx.strip_and_freeze_until([out_op_name], graph, sess,
                                                        return_graph=True)
            return stripped_graph, model.input.name, model.output.name 
Example 3
Project: AI2-Reasoning-Challenge-ARC   Author: SebiSebi   File: kv_attention.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, key_size, value_size,
                 return_attention_scores=False, **kwargs):
        if K.backend() != 'tensorflow':
            raise RuntimeError('KVAttention is only available with '
                               'the TensorFlow backend.')
        assert(isinstance(key_size, int) and key_size >= 1)
        assert(isinstance(value_size, int) and value_size >= 1)
        assert(isinstance(return_attention_scores, bool))

        self.key_size = key_size
        self.value_size = value_size
        self.return_attention_scores = return_attention_scores
        self.imput_dim = None
        self.timestamps = None

        super(KVAttention, self).__init__(**kwargs)

    # The model receives an input with shape (batch_size, timestamp, input_dim) 
Example 4
Project: image-processing-service   Author: aetrapp   File: vgg.py    GNU General Public License v3.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(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 5
Project: image-processing-service   Author: aetrapp   File: resnet.py    GNU General Public License v3.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 6
Project: BERT   Author: yyht   File: test_transformer.py    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 7
Project: BERT   Author: yyht   File: test_transformer.py    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 8
Project: iMIMIC-RCVs   Author: medgift   File: test_utils.py    MIT License 6 votes vote down vote up
def across_data_formats(func):
    """Function wrapper to run tests on multiple keras data_format and clean up after TensorFlow tests.

    Args:
        func: test function to clean up after.

    Returns:
        A function wrapping the input function.
    """
    @six.wraps(func)
    def wrapper(*args, **kwargs):
        for data_format in {'channels_first', 'channels_last'}:
            K.set_image_data_format(data_format)
            func(*args, **kwargs)
            if K.backend() == 'tensorflow':
                K.clear_session()
                tf.reset_default_graph()
    return wrapper 
Example 9
Project: iMIMIC-RCVs   Author: medgift   File: test_utils.py    MIT License 6 votes vote down vote up
def skip_backends(backends):
    """Function wrapper to specify which backends should skip the test.

    Args:
        backends: The list of backends to skip.

    Returns:
        A function wrapping the input function.
    """
    backends = set(utils.listify(backends))

    def decorator(func):
        @six.wraps(func)
        def wrapper(*args, **kwargs):
            if K.backend() in backends:
                return
            func(*args, **kwargs)
        return wrapper
    return decorator 
Example 10
Project: table_detection_with_faster_rcnn   Author: Dimetrix   File: vgg.py    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 11
Project: table_detection_with_faster_rcnn   Author: Dimetrix   File: resnet.py    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 12
Project: keras-frcnn   Author: kbardool   File: vgg.py    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 13
Project: keras-frcnn   Author: kbardool   File: resnet.py    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 14
Project: CarCrash_forecasting_and_detection   Author: ankitshah009   File: vgg.py    GNU General Public License v3.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(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 15
Project: imgclsmob   Author: osmr   File: common.py    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 16
Project: cor-asv-ann   Author: ASVLeipzig   File: seq2seq.py    Apache License 2.0 6 votes vote down vote up
def _regularise_chars(self, embedding_matrix):
        '''Calculate L2 loss of the char embedding weights
        to control for underspecification at zero
        (by interpolating between other embedding vectors).
        '''
        from keras import backend as K
        
        em_dims = embedding_matrix.shape.as_list()
        if em_dims[0] == 0: # voc_size starts with 0 before first training
            return 0
        vec0 = K.slice(embedding_matrix, [0, 0], [1, em_dims[1]])            # zero vector only,
        #vec0 = K.repeat_elements(vec0, em_dims[0]-1, axis=0)                 # repeated
        vecs = K.slice(embedding_matrix, [1, 0], [em_dims[0]-1, em_dims[1]]) # all vectors except zero
        # make sure only vec0 is affected, i.e. vecs change only via global loss:
        vecs = K.stop_gradient(K.mean(vecs, axis=0))
        # scale to make gradients benign:
        underspecification = 1 * K.sum(K.square(vec0 - vecs)) # c='\0' ~ mean of others

        #lowrank = K.sum(0.01 * K.square(embedding_matrix)) # generalization/sparsity
        norms = K.sum(K.square(embedding_matrix), axis=1)
        norm0 = K.ones_like(norms) # square of target (non-zero) weight norm
        lowrank = 0.01 * K.sum(K.square(norm0 - norms))
        
        return K.in_train_phase(lowrank + underspecification, 0.) 
Example 17
Project: VisualNN   Author: angelhunt   File: test_views.py    GNU General Public License v3.0 5 votes vote down vote up
def test_keras_backend(self):
        dim_order = K.image_dim_ordering()
        backend = K.backend()
        if(backend == 'tensorflow'):
            self.assertEqual(dim_order, 'tf')
        elif(backend == 'theano'):
            self.assertNotEqual(dim_order, 'th')
            self.assertEqual(dim_order, 'tf')
        else:
            self.fail('%s backend not supported' % backend)


# ********** Import json tests ********** 
Example 18
Project: VisualNN   Author: angelhunt   File: cifar10_cnn_capsule.py    GNU General Public License v3.0 5 votes vote down vote up
def call(self, inputs):
        """Following the routing algorithm from Hinton's paper,
        but replace b = b + <u,v> with b = <u,v>.

        This change can improve the feature representation of Capsule.

        However, you can replace
            b = K.batch_dot(outputs, hat_inputs, [2, 3])
        with
            b += K.batch_dot(outputs, hat_inputs, [2, 3])
        to realize a standard routing.
        """

        if self.share_weights:
            hat_inputs = K.conv1d(inputs, self.kernel)
        else:
            hat_inputs = K.local_conv1d(inputs, self.kernel, [1], [1])

        batch_size = K.shape(inputs)[0]
        input_num_capsule = K.shape(inputs)[1]
        hat_inputs = K.reshape(hat_inputs,
                               (batch_size, input_num_capsule,
                                self.num_capsule, self.dim_capsule))
        hat_inputs = K.permute_dimensions(hat_inputs, (0, 2, 1, 3))

        b = K.zeros_like(hat_inputs[:, :, :, 0])
        for i in range(self.routings):
            c = softmax(b, 1)
            o = self.activation(K.batch_dot(c, hat_inputs, [2, 2]))
            if i < self.routings - 1:
                b = K.batch_dot(o, hat_inputs, [2, 3])
                if K.backend() == 'theano':
                    o = K.sum(o, axis=1)

        return o 
Example 19
Project: keras-utility-layer-collection   Author: zimmerrol   File: image.py    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 20
Project: phoneticSimilarity   Author: ronggong   File: attentionWithContext.py    GNU Affero General Public License v3.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 21
Project: phoneticSimilarity   Author: ronggong   File: attention.py    GNU Affero General Public License v3.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':
        # todo: check that this is correct
        kernel = K.expand_dims(kernel)
        return K.squeeze(K.dot(x, kernel), axis=-1)
    else:
        return K.dot(x, kernel) 
Example 22
Project: cdc   Author: ckbjimmy   File: EmbCRNN.py    MIT License 5 votes vote down vote up
def set_keras_backend(backend):
        if K.backend() != backend:
            os.environ['KERAS_BACKEND'] = backend
            reload(K)
            assert K.backend() == backend
    #set_keras_backend("theano") 
Example 23
Project: cdc   Author: ckbjimmy   File: Attention.py    MIT License 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 24
Project: cdc   Author: ckbjimmy   File: EmbGRUattention.py    MIT License 5 votes vote down vote up
def set_keras_backend(backend):
        if K.backend() != backend:
            os.environ['KERAS_BACKEND'] = backend
            reload(K)
            assert K.backend() == backend
    #set_keras_backend("theano") 
Example 25
Project: gandlf   Author: codekansas   File: models.py    MIT License 5 votes vote down vote up
def _sort_weights_by_name(self, weights):
        """Sorts weights by name and returns them."""

        if not weights:
            return []

        if K.backend() == 'theano':
            key = lambda x: x.name if x.name else x.auto_name
        else:
            key = lambda x: x.name

        weights.sort(key=key)
        return weights 
Example 26
Project: FasterRCNN_KERAS   Author: akshaylamba   File: resnet.py    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 27
Project: dense_tensor   Author: bstriner   File: __init__.py    MIT License 5 votes vote down vote up
def keras_backend():
    if hasattr(K, 'backend'):
        return K.backend()
    else:
        return K._BACKEND 
Example 28
Project: ICASSP2019_TCN   Author: DSIP-UPatras   File: custom_layers.py    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 29
Project: lmtc-eurlex57k   Author: iliaschalkidis   File: attention.py    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 30
Project: Document-Classifier-LSTM   Author: AlexGidiotis   File: attention.py    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 31
Project: DisplaceNet   Author: GKalliatakis   File: keras_layer_AnchorBoxes.py    MIT License 5 votes vote down vote up
def compute_output_shape(self, input_shape):
        if K.image_dim_ordering() == 'tf':
            batch_size, feature_map_height, feature_map_width, feature_map_channels = input_shape
        else: # Not yet relevant since TensorFlow is the only supported backend right now, but it can't harm to have this in here for the future
            batch_size, feature_map_channels, feature_map_height, feature_map_width = input_shape
        return (batch_size, feature_map_height, feature_map_width, self.n_boxes, 8) 
Example 32
Project: dialectal_arabic_segmenter   Author: qcri   File: callbacks.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __init__(self, log_dir='./logs',
                 histogram_freq=0,
                 write_graph=True,
                 write_images=False):
        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_images = write_images 
Example 33
Project: keras_nade   Author: jgrnt   File: orderless_nade.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def call(self, x, **kwargs):
        from theano import tensor as T
        from theano.tensor.shared_randomstreams import RandomStreams
        if K.backend() == "theano":
            import theano
            mask_rng = RandomStreams(self.seed)

            ints = mask_rng.random_integers(size=K.expand_dims(x.shape[0], 0), high=x.shape[1] - 1)

            def set_value_at_position(i, ns_x):
                zeros = T.zeros_like(ns_x[0, :])
                return T.set_subtensor(zeros[:i], 1)

            result, updates = theano.scan(fn=set_value_at_position,
                                          outputs_info=None,
                                          sequences=ints,
                                          non_sequences=x)
            mask = mask_rng.shuffle_row_elements(result)
        elif K.backend() == "tensorflow":
            import tensorflow as tf
            tf.set_random_seed(self.seed)
            ints = tf.random_uniform(shape=K.expand_dims(tf.shape(x)[0], 0),
                                     maxval=x.shape[1],
                                     dtype=tf.int32)
            result = tf.sequence_mask(ints, maxlen=x.shape[1])
            parallel_iterations = self._deterministic and 1 or 10
            mask = tf.cast(tf.map_fn(tf.random_shuffle, result, parallel_iterations=parallel_iterations), K.floatx())
        else:
            raise NotImplementedError()
        return K.concatenate([x * mask, mask]) 
Example 34
Project: keras_extension   Author: k1414st   File: core_sparse.py    MIT License 5 votes vote down vote up
def compute_output_shape(self, input_shape):
        if self._output_shape is None:
            # With TensorFlow or CNTK, we can infer the output shape directly:
            if K.backend() in ('tensorflow', 'cntk'):
                if isinstance(input_shape, list):
                    xs = [K.placeholder(shape=shape, sparse=is_sparse)
                          for shape, is_sparse in zip(input_shape, self._is_sparse)]
                    x = self.call(xs)
                else:
                    x = K.placeholder(shape=input_shape, sparse=self._is_sparse)
                    x = self.call(x)
                if isinstance(x, list):
                    return [K.int_shape(x_elem) for x_elem in x]
                else:
                    return K.int_shape(x)
            # Otherwise, we default to the input shape.
            warnings.warn('`output_shape` argument not specified for layer {} '
                          'and cannot be automatically inferred '
                          'with the Theano backend. '
                          'Defaulting to output shape `{}` '
                          '(same as input shape). '
                          'If the expected output shape is different, '
                          'specify it via the `output_shape` argument.'
                          .format(self.name, input_shape))
            return input_shape
        elif isinstance(self._output_shape, (tuple, list)):
            if isinstance(input_shape, list):
                num_samples = input_shape[0][0]
            else:
                num_samples = input_shape[0] if input_shape else None
            return (num_samples,) + tuple(self._output_shape)
        else:
            shape = self._output_shape(input_shape)
            if not isinstance(shape, (list, tuple)):
                raise ValueError('`output_shape` function must return a tuple or '
                                 'a list of tuples.')
            if isinstance(shape, list):
                if isinstance(shape[0], int) or shape[0] is None:
                    shape = tuple(shape)
            return shape 
Example 35
Project: keras_extension   Author: k1414st   File: core_sparse.py    MIT License 5 votes vote down vote up
def compute_output_shape(self, input_shape):
        if self._output_shape is None:
            # With TensorFlow or CNTK, we can infer the output shape directly:
            if K.backend() in ('tensorflow', 'cntk'):
                if isinstance(input_shape, list):
                    xs = [K.placeholder(shape=shape, sparse=is_sparse)
                          for shape, is_sparse in zip(input_shape, self._is_sparse)]
                    x = self.call(xs)
                else:
                    x = K.placeholder(shape=input_shape, sparse=self._is_sparse)
                    x = self.call(x)
                if isinstance(x, list):
                    return [K.int_shape(x_elem) for x_elem in x]
                else:
                    return K.int_shape(x)
            # Otherwise, we default to the input shape.
            warnings.warn('`output_shape` argument not specified for layer {} '
                          'and cannot be automatically inferred '
                          'with the Theano backend. '
                          'Defaulting to output shape `{}` '
                          '(same as input shape). '
                          'If the expected output shape is different, '
                          'specify it via the `output_shape` argument.'
                          .format(self.name, input_shape))
            return input_shape
        elif isinstance(self._output_shape, (tuple, list)):
            if isinstance(input_shape, list):
                num_samples = input_shape[0][0]
            else:
                num_samples = input_shape[0] if input_shape else None
            return (num_samples,) + tuple(self._output_shape)
        else:
            shape = self._output_shape(input_shape)
            if not isinstance(shape, (list, tuple)):
                raise ValueError('`output_shape` function must return a tuple or '
                                 'a list of tuples.')
            if isinstance(shape, list):
                if isinstance(shape[0], int) or shape[0] is None:
                    shape = tuple(shape)
            return shape 
Example 36
Project: kuaikai_perception   Author: pixmoving-moveit   File: keras_layer_AnchorBoxes.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def compute_output_shape(self, input_shape):
        if K.image_dim_ordering() == 'tf':
            batch_size, feature_map_height, feature_map_width, feature_map_channels = input_shape
        else: # Not yet relevant since TensorFlow is the only supported backend right now, but it can't harm to have this in here for the future
            batch_size, feature_map_channels, feature_map_height, feature_map_width = input_shape
        return (batch_size, feature_map_height, feature_map_width, self.n_boxes, 8) 
Example 37
Project: dtc-pointpillars-keras   Author: dtczhl   File: keras_layer_AnchorBoxes.py    MIT License 5 votes vote down vote up
def compute_output_shape(self, input_shape):
        if K.image_dim_ordering() == 'tf':
            batch_size, feature_map_height, feature_map_width, feature_map_channels = input_shape
        else: # Not yet relevant since TensorFlow is the only supported backend right now, but it can't harm to have this in here for the future
            batch_size, feature_map_channels, feature_map_height, feature_map_width = input_shape
        return (batch_size, feature_map_height, feature_map_width, self.n_boxes, 8) 
Example 38
Project: image-processing-service   Author: aetrapp   File: vgg.py    GNU General Public License v3.0 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 39
Project: image-processing-service   Author: aetrapp   File: resnet.py    GNU General Public License v3.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 40
Project: DeepResearch   Author: Hsankesara   File: attention_with_context.py    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 41
Project: BERT   Author: yyht   File: test_transformer.py    Apache License 2.0 5 votes vote down vote up
def set_keras_backend(backend):
    if K.backend() != backend:
        os.environ['KERAS_BACKEND'] = backend
        reload(K)
        assert K.backend() == backend 
Example 42
Project: BERT   Author: yyht   File: test_transformer.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, method_name: str = 'runTest') -> None:
        super().__init__(methodName=method_name)
        self.vocab_size = 23
        self.num_heads = 2
        self.num_layers = 2
        self.embedding_dim = 6
        self.d_hid = 12
        self.max_len = 7
        self.supported_backends = {'tensorflow', 'theano'}
        self.original_backend = K.backend() 
Example 43
Project: BERT   Author: yyht   File: test_transformer.py    Apache License 2.0 5 votes vote down vote up
def list_backends(self, orig_backend=None):
        if orig_backend is None:
            orig_backend = K.backend()
        # always start from the default backend
        return [orig_backend] + list(self.supported_backends - {orig_backend}) 
Example 44
Project: BERT   Author: yyht   File: test_transformer.py    Apache License 2.0 5 votes vote down vote up
def test_different_backends_work(self):
        for use_attn_mask in [True, False]:
            orig_backend = K.backend()
            for backend in self.list_backends(orig_backend):
                try:
                    set_keras_backend(backend)
                except ModuleNotFoundError:
                    pass
                K.set_learning_phase(0)  # test
                model = self.create_small_model(use_attn_mask)
                del model
            set_keras_backend(orig_backend) 
Example 45
Project: BERT   Author: yyht   File: funcs.py    Apache License 2.0 5 votes vote down vote up
def shape_list(x):
    if K.backend() != 'theano':
        tmp = K.int_shape(x)
    else:
        tmp = x.shape
    tmp = list(tmp)
    tmp[0] = -1
    return tmp 
Example 46
Project: BERT   Author: yyht   File: funcs.py    Apache License 2.0 5 votes vote down vote up
def multihead_attention(x, attn_mask, n_head: int, n_state: int, attention_dropout: float):
    _q, _k, _v = x[:, :, :n_state], x[:, :, n_state:2 * n_state], x[:, :, -n_state:]
    q = split_heads(_q, n_head)  # B, H, L, C//H
    k = split_heads(_k, n_head, k=True)  # B, H, C//H, L
    v = split_heads(_v, n_head)  # B, H, L, C//H
    if K.backend() == 'tensorflow':
        a = scaled_dot_product_attention_tf(q, k, v, attn_mask, attention_dropout)
    else:
        a = scaled_dot_product_attention_th(q, k, v, attn_mask, attention_dropout)
    return merge_heads(a) 
Example 47
Project: table_detection_with_faster_rcnn   Author: Dimetrix   File: vgg.py    Apache License 2.0 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 48
Project: table_detection_with_faster_rcnn   Author: Dimetrix   File: resnet.py    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 49
Project: wtte-rnn   Author: ragulpr   File: wtte.py    MIT License 5 votes vote down vote up
def _keras_unstack_hack(ab):
    """Implements tf.unstack(y_true_keras, num=2, axis=-1).

       Keras-hack adopted to be compatible with Theano backend.

       :param ab: stacked variables
       :return a, b: unstacked variables
    """
    ndim = len(K.int_shape(ab))
    if ndim == 0:
        print('can not unstack with ndim=0')
    else:
        a = ab[..., 0]
        b = ab[..., 1]
    return a, b 
Example 50
Project: keras-frcnn   Author: kbardool   File: vgg.py    Apache License 2.0 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 51
Project: keras-frcnn   Author: kbardool   File: resnet.py    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 52
Project: cloudsat-gan   Author: jleinonen   File: data_utils.py    MIT License 5 votes vote down vote up
def save_opt_weights(model, filepath):
    with h5py.File(filepath, 'w') as f:
        # Save optimizer weights.
        symbolic_weights = getattr(model.optimizer, 'weights')
        if symbolic_weights:
            optimizer_weights_group = f.create_group('optimizer_weights')
            weight_values = K.batch_get_value(symbolic_weights)
            weight_names = []
            for i, (w, val) in enumerate(zip(symbolic_weights, weight_values)):
                # Default values of symbolic_weights is /variable for theano
                if K.backend() == 'theano':
                    if hasattr(w, 'name') and w.name != "/variable":
                        name = str(w.name)
                    else:
                        name = 'param_' + str(i)
                else:
                    if hasattr(w, 'name') and w.name:
                        name = str(w.name)
                    else:
                        name = 'param_' + str(i)
                weight_names.append(name.encode('utf8'))
            optimizer_weights_group.attrs['weight_names'] = weight_names
            for name, val in zip(weight_names, weight_values):
                param_dset = optimizer_weights_group.create_dataset(
                    name,
                    val.shape,
                    dtype=val.dtype)
                if not val.shape:
                    # scalar
                    param_dset[()] = val
                else:
                    param_dset[:] = val 
Example 53
Project: CarCrash_forecasting_and_detection   Author: ankitshah009   File: vgg.py    GNU General Public License v3.0 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 54
Project: imgclsmob   Author: osmr   File: model_store.py    MIT License 5 votes vote down vote up
def _preprocess_weights_for_loading(layer,
                                    weights):
    """
    Converts layers weights.

    Parameters
    ----------
    layer : Layer
        Layer instance.
    weights : list of np.array
        List of weights values.

    Returns
    -------
    list of np.array
        A list of weights values.
    """
    is_channels_first = (K.image_data_format() == "channels_first")
    if ((K.backend() == "mxnet") and (not is_channels_first)) or (K.backend() == "tensorflow"):
        if layer.__class__.__name__ == "Conv2D":
            weights[0] = np.transpose(weights[0], (2, 3, 1, 0))
        elif layer.__class__.__name__ == "DepthwiseConv2D":
            weights[0] = np.transpose(weights[0], (2, 3, 0, 1))
    for i in range(len(weights)):
        assert (K.int_shape(layer.weights[i]) == weights[i].shape)
    return weights 
Example 55
Project: imgclsmob   Author: osmr   File: model_store.py    MIT License 5 votes vote down vote up
def load_model(net,
               file_path,
               skip_mismatch=False):
    """
    Load model state dictionary from a file.

    Parameters
    ----------
    net : Model
        Network in which weights are loaded.
    file_path : str
        Path to the file.
    skip_mismatch : bool, default False
        Whether to skip loading of layers with wrong names.
    """
    # if (K.backend() == "mxnet") and (K.image_data_format() == "channels_first"):
    #     net.load_weights(filepath=file_path, by_name=skip_mismatch)
    #     return
    with h5py.File(file_path, mode='r') as f:
        if ("layer_names" not in f.attrs) and ("model_weights" in f):
            f = f["model_weights"]
        if ("keras_version" not in f.attrs) or ("backend" not in f.attrs):
            raise ImportError("Unsupported version of Keras checkpoint file.")
        # original_keras_version = f.attrs["keras_version"].decode("utf8")
        original_backend = f.attrs["backend"].decode("utf8")
        assert (original_backend == "mxnet")
        if skip_mismatch:
            _load_weights_from_hdf5_group_by_name(
                f=f,
                layers=net.layers)
        else:
            _load_weights_from_hdf5_group(
                f=f,
                layers=net.layers) 
Example 56
Project: imgclsmob   Author: osmr   File: common.py    MIT License 5 votes vote down vote up
def get_activation_layer(x,
                         activation,
                         name="activ"):
    """
    Create activation layer from string/function.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    activation : function or str
        Activation function or name of activation function.
    name : str, default 'activ'
        Block name.

    Returns
    -------
    keras.backend tensor/variable/symbol
        Resulted tensor/variable/symbol.
    """
    assert (activation is not None)
    if isfunction(activation):
        x = activation()(x)
    elif isinstance(activation, str):
        if activation == "relu":
            x = nn.Activation("relu", name=name)(x)
        elif activation == "relu6":
            x = nn.ReLU(max_value=6.0, name=name)(x)
        elif activation == "swish":
            x = swish(x=x, name=name)
        elif activation == "hswish":
            x = HSwish(name=name)(x)
        else:
            raise NotImplementedError()
    else:
        x = activation(x)
    return x 
Example 57
Project: imgclsmob   Author: osmr   File: common.py    MIT License 5 votes vote down vote up
def update_keras_shape(x):
    """
    Update Keras shape property.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    """
    if not hasattr(x, "_keras_shape"):
        x._keras_shape = tuple([int(d) if (d is not None) and (d != 0) else None for d in x.shape]) 
Example 58
Project: imgclsmob   Author: osmr   File: common.py    MIT License 5 votes vote down vote up
def batchnorm(x,
              momentum=0.9,
              epsilon=1e-5,
              name=None):
    """
    Batch normalization layer.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    momentum : float, default 0.9
        Momentum for the moving average.
    epsilon : float, default 1e-5
        Small float added to variance to avoid dividing by zero.
    name : str, default None
        Layer name.

    Returns
    -------
    keras.backend tensor/variable/symbol
        Resulted tensor/variable/symbol.
    """
    if K.backend() == "mxnet":
        x = GluonBatchNormalization(
            momentum=momentum,
            epsilon=epsilon,
            name=name)(x)
    else:
        x = nn.BatchNormalization(
            axis=get_channel_axis(),
            momentum=momentum,
            epsilon=epsilon,
            name=name)(x)
    return x 
Example 59
Project: imgclsmob   Author: osmr   File: common.py    MIT License 5 votes vote down vote up
def conv1x1(x,
            in_channels,
            out_channels,
            strides=1,
            groups=1,
            use_bias=False,
            name="conv1x1"):
    """
    Convolution 1x1 layer.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    strides : int or tuple/list of 2 int, default 1
        Strides of the convolution.
    groups : int, default 1
        Number of groups.
    use_bias : bool, default False
        Whether the layer uses a bias vector.
    name : str, default 'conv1x1'
        Layer name.

    Returns
    -------
    keras.backend tensor/variable/symbol
        Resulted tensor/variable/symbol.
    """
    return conv2d(
        x=x,
        in_channels=in_channels,
        out_channels=out_channels,
        kernel_size=1,
        strides=strides,
        groups=groups,
        use_bias=use_bias,
        name=name) 
Example 60
Project: imgclsmob   Author: osmr   File: common.py    MIT License 5 votes vote down vote up
def depthwise_conv3x3(x,
                      channels,
                      strides,
                      name="depthwise_conv3x3"):
    """
    Depthwise convolution 3x3 layer.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    channels : int
        Number of input/output channels.
    strides : int or tuple/list of 2 int
        Strides of the convolution.
    name : str, default 'depthwise_conv3x3'
        Block name.

    Returns
    -------
    keras.backend tensor/variable/symbol
        Resulted tensor/variable/symbol.
    """
    return conv2d(
        x=x,
        in_channels=channels,
        out_channels=channels,
        kernel_size=3,
        strides=strides,
        padding=1,
        groups=channels,
        use_bias=False,
        name=name) 
Example 61
Project: imgclsmob   Author: osmr   File: common.py    MIT License 5 votes vote down vote up
def pre_conv3x3_block(x,
                      in_channels,
                      out_channels,
                      strides=1,
                      return_preact=False,
                      name="pre_conv3x3_block"):
    """
    3x3 version of the pre-activated convolution block.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    strides : int or tuple/list of 2 int, default 1
        Strides of the convolution.
    return_preact : bool, default False
        Whether return pre-activation.
    name : str, default 'pre_conv3x3_block'
        Block name.

    Returns
    -------
    tuple of two keras.backend tensor/variable/symbol
        Resulted tensor and preactivated input tensor.
    """
    return pre_conv_block(
        x=x,
        in_channels=in_channels,
        out_channels=out_channels,
        kernel_size=3,
        strides=strides,
        padding=1,
        return_preact=return_preact,
        name=name) 
Example 62
Project: imgclsmob   Author: osmr   File: common.py    MIT License 5 votes vote down vote up
def channel_shuffle(x,
                    groups):
    """
    Channel shuffle operation from 'ShuffleNet: An Extremely Efficient Convolutional Neural Network for Mobile Devices,'
    https://arxiv.org/abs/1707.01083.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    groups : int
        Number of groups.

    Returns
    -------
    keras.backend tensor/variable/symbol
        Resulted tensor/variable/symbol.
    """

    if is_channels_first():
        batch, channels, height, width = x._keras_shape
    else:
        batch, height, width, channels = x._keras_shape

    # assert (channels % groups == 0)
    channels_per_group = channels // groups

    if is_channels_first():
        x = K.reshape(x, shape=(-1, groups, channels_per_group, height, width))
        x = K.permute_dimensions(x, pattern=(0, 2, 1, 3, 4))
        x = K.reshape(x, shape=(-1, channels, height, width))
    else:
        x = K.reshape(x, shape=(-1, height, width, groups, channels_per_group))
        x = K.permute_dimensions(x, pattern=(0, 1, 2, 4, 3))
        x = K.reshape(x, shape=(-1, height, width, channels))

    update_keras_shape(x)
    return x 
Example 63
Project: poker   Author: surgebiswas   File: callbacks.py    MIT License 5 votes vote down vote up
def __init__(self, log_dir='./logs',
                 histogram_freq=0,
                 write_graph=True,
                 write_images=False):
        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_images = write_images 
Example 64
Project: spacy_zh_model   Author: algteam   File: __main__.py    MIT License 5 votes vote down vote up
def set_keras_backend(backend):
    if K.backend() != backend:
        os.environ['KERAS_BACKEND'] = backend
        importlib.reload(K)
        assert K.backend() == backend
    if backend == "tensorflow":
        K.get_session().close()
        cfg = K.tf.ConfigProto()
        cfg.gpu_options.allow_growth = True
        K.set_session(K.tf.Session(config=cfg))
        K.clear_session() 
Example 65
Project: smach_based_introspection_framework   Author: birlrobotics   File: layer_utils.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def _time_distributed_dense(x, w, b=None, dropout=None,
                            input_dim=None, output_dim=None,
                            timesteps=None, training=None):
    """Apply `y . w + b` for every temporal slice y of x.

    # Arguments
        x: input tensor.
        w: weight matrix.
        b: optional bias vector.
        dropout: wether to apply dropout (same dropout mask
            for every temporal slice of the input).
        input_dim: integer; optional dimensionality of the input.
        output_dim: integer; optional dimensionality of the output.
        timesteps: integer; optional number of timesteps.
        training: training phase tensor or boolean.

    # Returns
        Output tensor.
    """
    if not input_dim:
        input_dim = K.shape(x)[2]
    if not timesteps:
        timesteps = K.shape(x)[1]
    if not output_dim:
        output_dim = K.int_shape(w)[1]

    if dropout is not None and 0. < dropout < 1.:
        # apply the same dropout pattern at every timestep
        ones = K.ones_like(K.reshape(x[:, 0, :], (-1, input_dim)))
        dropout_matrix = K.dropout(ones, dropout)
        expanded_dropout_matrix = K.repeat(dropout_matrix, timesteps)
        x = K.in_train_phase(x * expanded_dropout_matrix, x, training=training)

    # collapse time dimension and batch dimension together
    x = K.reshape(x, (-1, input_dim))
    x = K.dot(x, w)
    if b is not None:
        x = K.bias_add(x, b)
    # reshape to 3D tensor
    if K.backend() == 'tensorflow':
        x = K.reshape(x, K.stack([-1, timesteps, output_dim]))
        x.set_shape([None, None, output_dim])
    else:
        x = K.reshape(x, (-1, timesteps, output_dim))
    return x 
Example 66
Project: keras-minimal-rnn   Author: titu1994   File: minimal_rnn.py    MIT License 4 votes vote down vote up
def _time_distributed_dense(x, w, b=None, dropout=None,
                            input_dim=None, output_dim=None,
                            timesteps=None, training=None):
    """Apply `y . w + b` for every temporal slice y of x.

    # Arguments
        x: input tensor.
        w: weight matrix.
        b: optional bias vector.
        dropout: wether to apply dropout (same dropout mask
            for every temporal slice of the input).
        input_dim: integer; optional dimensionality of the input.
        output_dim: integer; optional dimensionality of the output.
        timesteps: integer; optional number of timesteps.
        training: training phase tensor or boolean.

    # Returns
        Output tensor.
    """
    if not input_dim:
        input_dim = K.shape(x)[2]
    if not timesteps:
        timesteps = K.shape(x)[1]
    if not output_dim:
        output_dim = K.int_shape(w)[1]

    if dropout is not None and 0. < dropout < 1.:
        # apply the same dropout pattern at every timestep
        ones = K.ones_like(K.reshape(x[:, 0, :], (-1, input_dim)))
        dropout_matrix = K.dropout(ones, dropout)
        expanded_dropout_matrix = K.repeat(dropout_matrix, timesteps)
        x = K.in_train_phase(x * expanded_dropout_matrix, x, training=training)

    # collapse time dimension and batch dimension together
    x = K.reshape(x, (-1, input_dim))
    x = K.dot(x, w)
    if b is not None:
        x = K.bias_add(x, b)
    # reshape to 3D tensor
    if K.backend() == 'tensorflow':
        x = K.reshape(x, K.stack([-1, timesteps, output_dim]))
        x.set_shape([None, None, output_dim])
    else:
        x = K.reshape(x, (-1, timesteps, output_dim))
    return x 
Example 67
Project: keras_extension   Author: k1414st   File: layer.py    MIT License 4 votes vote down vote up
def _batch_dot(x, y, axes=None):
    """
    K.batch_dot is only limited in the case of (x.ndim<=3 and y.ndim<=3).
    this wrapped function is not limited to that case.
    (using "tf.einsum" or "K.batch_dot with reshape".)
    """
    if K.ndim(x) <= 3 and K.ndim(y) <= 3:
        return K.batch_dot(x, y, axes=axes)
    else:
        if (axes[0] == 0) or (axes[1] == 0):
            raise ValueError('each axis must not be 0 (N_batch place).')
        if backend() == 'tensorflow':
            if K.ndim(x) + K.ndim(y) >= 27:
                raise ValueError('x.ndim + y.ndim must be less than 27')
            import tensorflow as tf

            # make einsum string
            # ex). NabcXefg, NhijkXlm -> Nabcefghijklm (N: pos_batch, X: pos_axes)
            str_x = ''.join([chr(97+i) for i in range(K.ndim(x))])
            str_y = ''.join([chr(97+len(str_x)+i) for i in range(K.ndim(y))])
            str_y = str_y.replace(str_y[0], str_x[0])
            str_y = str_y.replace(str_y[axes[1]], str_x[axes[0]])
            str_out = str_x.replace(str_x[axes[0]], '') + \
                str_y.replace(str_y[axes[1]], '')[1:]
            str_einsum = '%s,%s->%s' % (str_x, str_y, str_out)

            return tf.einsum(str_einsum, x, y)
        else:
            # set shape, targat-idim, target-shape
            sx, sy = x.shape, y.shape
            ax0, ax1 = axes[0], axes[1]
            s0, s1 = sx[ax0], sy[ax1]

            # reshape: (B, a1, a2, ... axis, ... an) -> (B, prod(~axis), axis)
            dx_rm = [i for i in range(len(sx)) if i != ax0]
            dy_rm = [i for i in range(len(sy)) if i != ax1]
            sx_rm = [sx[i] for i in dx_rm]
            sy_rm = [sy[i] for i in dy_rm]
            x = K.permute_dimensions(x, dx_rm + [ax0])
            y = K.permute_dimensions(y, dy_rm + [ax1])
            x = K.reshape(x, [-1, np.prod(sx_rm[1:]), s0])
            y = K.reshape(y, [-1, np.prod(sy_rm[1:]), s1])

            # reshape: (B, prod(sx_rm), prod(sy_rm)) -> (B, sx_rm, sy_rm)
            out = K.batch_dot(x, y, axes=(K.ndim(x)-1, K.ndim(y)-1))
            return K.reshape(out, [-1] + sx_rm[1:] + sy_rm[1:]) 
Example 68
Project: keras_extension   Author: k1414st   File: layer.py    MIT License 4 votes vote down vote up
def _batch_dot(x, y, axes=None):
    """
    K.batch_dot is only limited in the case of (x.ndim<=3 and y.ndim<=3).
    this wrapped function is not limited to that case.
    (using "tf.einsum" or "K.batch_dot with reshape".)
    """
    if K.ndim(x) <= 3 and K.ndim(y) <= 3:
        return K.batch_dot(x, y, axes=axes)
    else:
        if (axes[0] == 0) or (axes[1] == 0):
            raise ValueError('each axis must not be 0 (N_batch place).')
        if backend() == 'tensorflow':
            if K.ndim(x) + K.ndim(y) >= 27:
                raise ValueError('x.ndim + y.ndim must be less than 27')
            import tensorflow as tf

            # make einsum string
            # ex). NabcXefg, NhijkXlm -> Nabcefghijklm (N: pos_batch, X: pos_axes)
            str_x = ''.join([chr(97+i) for i in range(K.ndim(x))])
            str_y = ''.join([chr(97+len(str_x)+i) for i in range(K.ndim(y))])
            str_y = str_y.replace(str_y[0], str_x[0])
            str_y = str_y.replace(str_y[axes[1]], str_x[axes[0]])
            str_out = str_x.replace(str_x[axes[0]], '') + \
                str_y.replace(str_y[axes[1]], '')[1:]
            str_einsum = '%s,%s->%s' % (str_x, str_y, str_out)

            return tf.einsum(str_einsum, x, y)
        else:
            # set shape, targat-idim, target-shape
            sx, sy = x.shape, y.shape
            ax0, ax1 = axes[0], axes[1]
            s0, s1 = sx[ax0], sy[ax1]

            # reshape: (B, a1, a2, ... axis, ... an) -> (B, prod(~axis), axis)
            dx_rm = [i for i in range(len(sx)) if i != ax0]
            dy_rm = [i for i in range(len(sy)) if i != ax1]
            sx_rm = [sx[i] for i in dx_rm]
            sy_rm = [sy[i] for i in dy_rm]
            x = K.permute_dimensions(x, dx_rm + [ax0])
            y = K.permute_dimensions(y, dy_rm + [ax1])
            x = K.reshape(x, [-1, np.prod(sx_rm[1:]), s0])
            y = K.reshape(y, [-1, np.prod(sy_rm[1:]), s1])

            # reshape: (B, prod(sx_rm), prod(sy_rm)) -> (B, sx_rm, sy_rm)
            out = K.batch_dot(x, y, axes=(K.ndim(x)-1, K.ndim(y)-1))
            return K.reshape(out, [-1] + sx_rm[1:] + sy_rm[1:]) 
Example 69
Project: quickcnn   Author: CG1507   File: hist_callback.py    MIT License 4 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,
				 embeddings_data=None,
				 update_freq='epoch'):
		super(TensorBoard, self).__init__()
		global tf, projector
		try:
			import tensorflow as tf
			from tensorflow.contrib.tensorboard.plugins import projector
		except ImportError:
			raise ImportError('You need the TensorFlow module installed to '
							  'use TensorBoard.')

		if K.backend() != 'tensorflow':
			if histogram_freq != 0:
				warnings.warn('You are not using the TensorFlow backend. '
							  'histogram_freq was set to 0')
				histogram_freq = 0
			if write_graph:
				warnings.warn('You are not using the TensorFlow backend. '
							  'write_graph was set to False')
				write_graph = False
			if write_images:
				warnings.warn('You are not using the TensorFlow backend. '
							  'write_images was set to False')
				write_images = False
			if embeddings_freq != 0:
				warnings.warn('You are not using the TensorFlow backend. '
							  'embeddings_freq was set to 0')
				embeddings_freq = 0

		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
		self.embeddings_data = embeddings_data
		if update_freq == 'batch':
			# It is the same as writing as frequently as possible.
			self.update_freq = 1
		else:
			self.update_freq = update_freq
		self.samples_seen = 0
		self.samples_seen_at_last_write = 0 
Example 70
Project: group-ksparse-temporal-cnns   Author: srph25   File: layers.py    MIT License 4 votes vote down vote up
def __init__(self, filters,
                 kernel_size,
                 strides=(1, 1),
                 padding='valid',
                 data_format=None,
                 dilation_rate=(1, 1),
                 activation='tanh',
                 recurrent_activation='hard_sigmoid',
                 use_bias=True,
                 kernel_initializer='glorot_uniform',
                 recurrent_initializer='orthogonal',
                 bias_initializer='zeros',
                 kernel_regularizer=None,
                 recurrent_regularizer=None,
                 bias_regularizer=None,
                 kernel_constraint=None,
                 recurrent_constraint=None,
                 bias_constraint=None,
                 dropout=0.,
                 recurrent_dropout=0.,
                 **kwargs):
        super(ConvMinimalRNN2DCell, self).__init__(**kwargs)
        self.filters = filters
        self.kernel_size = conv_utils.normalize_tuple(kernel_size, 2, 'kernel_size')
        self.strides = conv_utils.normalize_tuple(strides, 2, 'strides')
        self.padding = conv_utils.normalize_padding(padding)
        #self.data_format = K.normalize_data_format(data_format)
        self.data_format = 'channels_last'
        self.dilation_rate = conv_utils.normalize_tuple(dilation_rate, 2, 'dilation_rate')
        self.activation = activations.get(activation)
        self.recurrent_activation = activations.get(recurrent_activation)
        self.use_bias = use_bias

        self.kernel_initializer = initializers.get(kernel_initializer)
        self.recurrent_initializer = initializers.get(recurrent_initializer)
        self.bias_initializer = initializers.get(bias_initializer)

        self.kernel_regularizer = regularizers.get(kernel_regularizer)
        self.recurrent_regularizer = regularizers.get(recurrent_regularizer)
        self.bias_regularizer = regularizers.get(bias_regularizer)

        self.kernel_constraint = constraints.get(kernel_constraint)
        self.recurrent_constraint = constraints.get(recurrent_constraint)
        self.bias_constraint = constraints.get(bias_constraint)

        if K.backend() == 'theano' and (dropout or recurrent_dropout):
            warnings.warn(
                'RNN dropout is no longer supported with the Theano backend '
                'due to technical limitations. '
                'You can either set `dropout` and `recurrent_dropout` to 0, '
                'or use the TensorFlow backend.')
            dropout = 0.
            recurrent_dropout = 0.
        self.dropout = min(1., max(0., dropout))
        self.recurrent_dropout = min(1., max(0., recurrent_dropout))
        self.state_size = self.filters
        self._dropout_mask = None
        self._recurrent_dropout_mask = None 
Example 71
Project: iMIMIC-RCVs   Author: medgift   File: test_backend.py    MIT License 4 votes vote down vote up
def test_advanced_activations():
    """ Tests that various ways of specifying activations in keras models are handled when replaced with Relu
    """
    inp = Input(shape=(2, ))
    x = Dense(5, activation='elu')(inp)
    x = advanced_activations.LeakyReLU()(x)
    x = Activation('elu')(x)
    model = Model(inp, x)

    # Ensure that layer.activation, Activation and advanced activations are replaced with relu
    modified_model = modify_model_backprop(model, 'guided')
    assert modified_model.layers[1].activation == get('relu')
    assert modified_model.layers[2].activation == get('relu')
    assert modified_model.layers[3].activation == get('relu')

    # Ensure that original model is unchanged.
    assert model.layers[1].activation == get('elu')
    assert isinstance(model.layers[2], advanced_activations.LeakyReLU)
    assert model.layers[3].activation == get('elu')


# @skip_backends('theano')
# def test_rectified_grad_modifier():
#     # Only test tensorflow implementation for now.
#     if K.backend() == 'theano':
#         return
#
#     # Create a simple linear sequence x -> linear(w.x) with weights w1 = -1, w2 = 1.
#     inp = Input(shape=(2, ))
#     out = Dense(1, activation='linear', use_bias=False, kernel_initializer=Constant([-1., 1.]))(inp)
#     model = Model(inp, out)
#
#     # Original model gradient should be [w1, w2]
#     assert np.array_equal(_compute_grads(model, [1., -1.]), [-1., 1.])
#
#     # Original gradient is [-1, 1] but new gradient should be [0, 1]
#     # First one is clipped because of negative gradient.
#     modified_model = modify_model_backprop(model, 'rectified')
#
#     # TODO: Interestingly this does not work for some reason.
#     # It is failing at tf.cast(grad > 0., dtype)
#     assert np.array_equal(_compute_grads(modified_model, [1., -1.]), [0., 1.])
#
#     # Ensure that the original model reference remains unchanged.
#     assert model.layers[1].activation == get('linear')
#     assert modified_model.layers[1].activation == get('relu') 
Example 72
Project: imgclsmob   Author: osmr   File: common.py    MIT License 4 votes vote down vote up
def conv3x3(x,
            in_channels,
            out_channels,
            strides=1,
            padding=1,
            groups=1,
            name="conv3x3"):
    """
    Convolution 3x3 layer.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    strides : int or tuple/list of 2 int, default 1
        Strides of the convolution.
    padding : int or tuple/list of 2 int, default 1
        Padding value for convolution layer.
    groups : int, default 1
        Number of groups.
    name : str, default 'conv3x3'
        Block name.

    Returns
    -------
    keras.backend tensor/variable/symbol
        Resulted tensor/variable/symbol.
    """
    return conv2d(
        x=x,
        in_channels=in_channels,
        out_channels=out_channels,
        kernel_size=3,
        strides=strides,
        padding=padding,
        groups=groups,
        use_bias=False,
        name=name) 
Example 73
Project: imgclsmob   Author: osmr   File: common.py    MIT License 4 votes vote down vote up
def conv1x1_block(x,
                  in_channels,
                  out_channels,
                  strides=1,
                  groups=1,
                  use_bias=False,
                  bn_epsilon=1e-5,
                  activation="relu",
                  name="conv1x1_block"):
    """
    1x1 version of the standard convolution block.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    strides : int or tuple/list of 2 int, default 1
        Strides of the convolution.
    groups : int, default 1
        Number of groups.
    use_bias : bool, default False
        Whether the layer uses a bias vector.
    bn_epsilon : float, default 1e-5
        Small float added to variance in Batch norm.
    activation : function or str or None, default 'relu'
        Activation function or name of activation function.
    name : str, default 'conv1x1_block'
        Block name.

    Returns
    -------
    keras.backend tensor/variable/symbol
        Resulted tensor/variable/symbol.
    """
    return conv_block(
        x=x,
        in_channels=in_channels,
        out_channels=out_channels,
        kernel_size=1,
        strides=strides,
        padding=0,
        groups=groups,
        use_bias=use_bias,
        bn_epsilon=bn_epsilon,
        activation=activation,
        name=name) 
Example 74
Project: imgclsmob   Author: osmr   File: common.py    MIT License 4 votes vote down vote up
def conv5x5_block(x,
                  in_channels,
                  out_channels,
                  strides=1,
                  padding=2,
                  dilation=1,
                  groups=1,
                  use_bias=False,
                  bn_epsilon=1e-5,
                  activation="relu",
                  name="conv3x3_block"):
    """
    5x5 version of the standard convolution block.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    strides : int or tuple/list of 2 int, default 1
        Strides of the convolution.
    padding : int or tuple/list of 2 int, default 2
        Padding value for convolution layer.
    dilation : int or tuple/list of 2 int, default 1
        Dilation value for convolution layer.
    groups : int, default 1
        Number of groups.
    use_bias : bool, default False
        Whether the layer uses a bias vector.
    bn_epsilon : float, default 1e-5
        Small float added to variance in Batch norm.
    activation : function or str or None, default 'relu'
        Activation function or name of activation function.
    name : str, default 'conv3x3_block'
        Block name.

    Returns
    -------
    keras.backend tensor/variable/symbol
        Resulted tensor/variable/symbol.
    """
    return conv_block(
        x=x,
        in_channels=in_channels,
        out_channels=out_channels,
        kernel_size=5,
        strides=strides,
        padding=padding,
        dilation=dilation,
        groups=groups,
        use_bias=use_bias,
        bn_epsilon=bn_epsilon,
        activation=activation,
        name=name) 
Example 75
Project: imgclsmob   Author: osmr   File: common.py    MIT License 4 votes vote down vote up
def conv7x7_block(x,
                  in_channels,
                  out_channels,
                  strides=1,
                  padding=3,
                  use_bias=False,
                  activation="relu",
                  name="conv7x7_block"):
    """
    3x3 version of the standard convolution block.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    strides : int or tuple/list of 2 int, default 1
        Strides of the convolution.
    padding : int or tuple/list of 2 int, default 3
        Padding value for convolution layer.
    use_bias : bool, default False
        Whether the layer uses a bias vector.
    activation : function or str or None, default 'relu'
        Activation function or name of activation function.
    name : str, default 'conv7x7_block'
        Block name.

    Returns
    -------
    keras.backend tensor/variable/symbol
        Resulted tensor/variable/symbol.
    """
    return conv_block(
        x=x,
        in_channels=in_channels,
        out_channels=out_channels,
        kernel_size=7,
        strides=strides,
        padding=padding,
        use_bias=use_bias,
        activation=activation,
        name=name) 
Example 76
Project: imgclsmob   Author: osmr   File: common.py    MIT License 4 votes vote down vote up
def dwconv3x3_block(x,
                    in_channels,
                    out_channels,
                    strides=1,
                    padding=1,
                    dilation=1,
                    use_bias=False,
                    bn_epsilon=1e-5,
                    activation="relu",
                    name="dwconv3x3_block"):
    """
    3x3 depthwise version of the standard convolution block.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    strides : int or tuple/list of 2 int, default 1
        Strides of the convolution.
    padding : int or tuple/list of 2 int, default 1
        Padding value for convolution layer.
    dilation : int or tuple/list of 2 int, default 1
        Dilation value for convolution layer.
    use_bias : bool, default False
        Whether the layer uses a bias vector.
    bn_epsilon : float, default 1e-5
        Small float added to variance in Batch norm.
    activation : function or str or None, default 'relu'
        Activation function or name of activation function.
    name : str, default 'dwconv3x3_block'
        Block name.

    Returns
    -------
    keras.backend tensor/variable/symbol
        Resulted tensor/variable/symbol.
    """
    return conv3x3_block(
        x=x,
        in_channels=in_channels,
        out_channels=out_channels,
        strides=strides,
        padding=padding,
        dilation=dilation,
        groups=out_channels,
        use_bias=use_bias,
        bn_epsilon=bn_epsilon,
        activation=activation,
        name=name) 
Example 77
Project: imgclsmob   Author: osmr   File: common.py    MIT License 4 votes vote down vote up
def dwconv5x5_block(x,
                    in_channels,
                    out_channels,
                    strides=1,
                    padding=2,
                    dilation=1,
                    use_bias=False,
                    bn_epsilon=1e-5,
                    activation="relu",
                    name="dwconv3x3_block"):
    """
    5x5 depthwise version of the standard convolution block.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    strides : int or tuple/list of 2 int, default 1
        Strides of the convolution.
    padding : int or tuple/list of 2 int, default 2
        Padding value for convolution layer.
    dilation : int or tuple/list of 2 int, default 1
        Dilation value for convolution layer.
    use_bias : bool, default False
        Whether the layer uses a bias vector.
    bn_epsilon : float, default 1e-5
        Small float added to variance in Batch norm.
    activation : function or str or None, default 'relu'
        Activation function or name of activation function.
    name : str, default 'dwconv3x3_block'
        Block name.

    Returns
    -------
    keras.backend tensor/variable/symbol
        Resulted tensor/variable/symbol.
    """
    return conv5x5_block(
        x=x,
        in_channels=in_channels,
        out_channels=out_channels,
        strides=strides,
        padding=padding,
        dilation=dilation,
        groups=out_channels,
        use_bias=use_bias,
        bn_epsilon=bn_epsilon,
        activation=activation,
        name=name) 
Example 78
Project: imgclsmob   Author: osmr   File: common.py    MIT License 4 votes vote down vote up
def pre_conv_block(x,
                   in_channels,
                   out_channels,
                   kernel_size,
                   strides,
                   padding,
                   return_preact=False,
                   name="pre_conv_block"):
    """
    Convolution block with Batch normalization and ReLU pre-activation.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    in_channels : int
        Number of input channels.
    out_channels : int
        Number of output channels.
    kernel_size : int or tuple/list of 2 int
        Convolution window size.
    strides : int or tuple/list of 2 int
        Strides of the convolution.
    padding : int or tuple/list of 2 int
        Padding value for convolution layer.
    return_preact : bool, default False
        Whether return pre-activation. It's used by PreResNet.
    name : str, default 'pre_conv_block'
        Block name.

    Returns
    -------
    tuple of two keras.backend tensor/variable/symbol
        Resulted tensor and preactivated input tensor.
    """
    x = batchnorm(
        x=x,
        name=name + "/bn")
    x = nn.Activation("relu", name=name + "/activ")(x)
    if return_preact:
        x_pre_activ = x
    x = conv2d(
        x=x,
        in_channels=in_channels,
        out_channels=out_channels,
        kernel_size=kernel_size,
        strides=strides,
        padding=padding,
        use_bias=False,
        name=name + "/conv")
    if return_preact:
        return x, x_pre_activ
    else:
        return x 
Example 79
Project: imgclsmob   Author: osmr   File: common.py    MIT License 4 votes vote down vote up
def se_block(x,
             channels,
             reduction=16,
             approx_sigmoid=False,
             round_mid=False,
             activation="relu",
             name="se_block"):
    """
    Squeeze-and-Excitation block from 'Squeeze-and-Excitation Networks,' https://arxiv.org/abs/1709.01507.

    Parameters:
    ----------
    x : keras.backend tensor/variable/symbol
        Input tensor/variable/symbol.
    channels : int
        Number of channels.
    reduction : int, default 16
        Squeeze reduction value.
    approx_sigmoid : bool, default False
        Whether to use approximated sigmoid function.
    round_mid : bool, default False
        Whether to round middle channel number (make divisible by 8).
    activation : function or str, default 'relu'
        Activation function or name of activation function.
    name : str, default 'se_block'
        Block name.

    Returns
    -------
    keras.backend tensor/variable/symbol
        Resulted tensor/variable/symbol.
    """
    assert(len(x._keras_shape) == 4)
    mid_channels = channels // reduction if not round_mid else round_channels(float(channels) / reduction)
    pool_size = x._keras_shape[2:4] if is_channels_first() else x._keras_shape[1:3]

    w = nn.AvgPool2D(
        pool_size=pool_size,
        name=name + "/pool")(x)
    w = conv1x1(
        x=w,
        in_channels=channels,
        out_channels=mid_channels,
        use_bias=True,
        name=name + "/conv1")
    w = get_activation_layer(
        x=w,
        activation=activation,
        name=name + "/activ")
    w = conv1x1(
        x=w,
        in_channels=mid_channels,
        out_channels=channels,
        use_bias=True,
        name=name + "/conv2")
    w = HSigmoid(name=name + "/hsigmoid")(w) if approx_sigmoid else nn.Activation("sigmoid", name=name + "/sigmoid")(w)
    x = nn.multiply([x, w], name=name + "/mul")
    return x 
Example 80
Project: poker   Author: surgebiswas   File: local.py    MIT License 4 votes vote down vote up
def call(self, x, mask=None):
        stride_row, stride_col = self.subsample
        _, feature_dim, nb_filter = self.W_shape

        if self.dim_ordering == 'th':
            if K.backend() == 'theano':
                output = []
                for i in range(self.output_row):
                    for j in range(self.output_col):
                        slice_row = slice(i * stride_row,
                                          i * stride_row + self.nb_row)
                        slice_col = slice(j * stride_col,
                                          j * stride_col + self.nb_col)
                        x_flatten = K.reshape(x[:, :, slice_row, slice_col], (1, -1, feature_dim))
                        output.append(K.dot(x_flatten, self.W[i * self.output_col + j, :, :]))
                output = K.concatenate(output, axis=0)
            else:
                xs = []
                for i in range(self.output_row):
                    for j in range(self.output_col):
                        slice_row = slice(i * stride_row,
                                          i * stride_row + self.nb_row)
                        slice_col = slice(j * stride_col,
                                          j * stride_col + self.nb_col)
                        xs.append(K.reshape(x[:, :, slice_row, slice_col], (1, -1, feature_dim)))
                x_aggregate = K.concatenate(xs, axis=0)
                output = K.batch_dot(x_aggregate, self.W)
            output = K.reshape(output, (self.output_row, self.output_col, -1, nb_filter))
            output = K.permute_dimensions(output, (2, 3, 0, 1))
        elif self.dim_ordering == 'tf':
            xs = []
            for i in range(self.output_row):
                for j in range(self.output_col):
                    slice_row = slice(i * stride_row,
                                      i * stride_row + self.nb_row)
                    slice_col = slice(j * stride_col,
                                      j * stride_col + self.nb_col)
                    xs.append(K.reshape(x[:, slice_row, slice_col, :], (1, -1, feature_dim)))
            x_aggregate = K.concatenate(xs, axis=0)
            output = K.batch_dot(x_aggregate, self.W)
            output = K.reshape(output, (self.output_row, self.output_col, -1, nb_filter))
            output = K.permute_dimensions(output, (2, 0, 1, 3))
        else:
            raise ValueError('Invalid dim_ordering:', self.dim_ordering)

        if self.bias:
            if self.dim_ordering == 'th':
                output += K.reshape(self.b, (1, nb_filter, self.output_row, self.output_col))
            elif self.dim_ordering == 'tf':
                output += K.reshape(self.b, (1, self.output_row, self.output_col, nb_filter))

        output = self.activation(output)
        return output