Coder Social home page Coder Social logo

keras-spp's Introduction

keras-spp

Spatial pyramid pooling layers for keras, based on https://arxiv.org/abs/1406.4729 . This code requires Keras version 2.0 or greater.

spp

(Image credit: Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition, K. He, X. Zhang, S. Ren, J. Sun)

Three types of pooling layers are currently available:

  • SpatialPyramidPooling: apply the pooling procedure on the entire image, given an image batch. This is especially useful if the image input can have varying dimensions, but needs to be fed to a fully connected layer.

For example, this trains a network on images of both 32x32 and 64x64 size:

import numpy as np
from keras.models import Sequential
from keras.layers import Convolution2D, Activation, MaxPooling2D, Dense
from spp.SpatialPyramidPooling import SpatialPyramidPooling

batch_size = 64
num_channels = 3
num_classes = 10

model = Sequential()

# uses theano ordering. Note that we leave the image size as None to allow multiple image sizes
model.add(Convolution2D(32, 3, 3, border_mode='same', input_shape=(3, None, None)))
model.add(Activation('relu'))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(64, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(SpatialPyramidPooling([1, 2, 4]))
model.add(Dense(num_classes))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', optimizer='sgd')

# train on 64x64x3 images
model.fit(np.random.rand(batch_size, num_channels, 64, 64), np.zeros((batch_size, num_classes)))
# train on 32x32x3 images
model.fit(np.random.rand(batch_size, num_channels, 32, 32), np.zeros((batch_size, num_classes)))
  • RoiPooling: extract multiple rois from a single image. In roi pooling, the spatial pyramid pooling is applied at the specified subregions of the image. This is useful for object detection, and is used in fast-RCNN and faster-RCNN. Note that the batch_size is limited to 1 currently.
pooling_regions = [1, 2, 4]
num_rois = 2
num_channels = 3

if dim_ordering == 'tf':
    in_img = Input(shape=(None, None, num_channels))
elif dim_ordering == 'th':
    in_img = Input(shape=(num_channels, None, None))

in_roi = Input(shape=(num_rois, 4))

out_roi_pool = RoiPooling(pooling_regions, num_rois)([in_img, in_roi])

model = Model([in_img, in_roi], out_roi_pool)

if dim_ordering == 'th':
    X_img = np.random.rand(1, num_channels, img_size, img_size)
    row_length = [float(X_img.shape[2]) / i for i in pooling_regions]
    col_length = [float(X_img.shape[3]) / i for i in pooling_regions]
elif dim_ordering == 'tf':
    X_img = np.random.rand(1, img_size, img_size, num_channels)
    row_length = [float(X_img.shape[1]) / i for i in pooling_regions]
    col_length = [float(X_img.shape[2]) / i for i in pooling_regions]

X_roi = np.array([[0, 0, img_size / 1, img_size / 1],
                  [0, 0, img_size / 2, img_size / 2]])

X_roi = np.reshape(X_roi, (1, num_rois, 4))

Y = model.predict([X_img, X_roi])

  • RoiPoolingConv: like RoiPooling, but maintains spatial information.

  • Thank you to @jlhbaseball15 for his contribution

keras-spp's People

Contributors

johnhenning avatar yhenon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

keras-spp's Issues

Not working with Keras 2

Example code:

model = Sequential([
		Conv2D( 4, kernel_size=(9,9), padding='same', input_shape=(1, h, w)),
		MaxPooling2D((2,2)),
		SpatialPyramidPooling( [1, 2, 4, 8, 16]),
		Activation( 'relu')
	])

	print( model.summary())

The spatial_pyramid_pooling layer output is not as expected one-dimensional, but equal to the input shape.

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 4, 480, 752)       328       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 4, 240, 376)       0         
_________________________________________________________________
spatial_pyramid_pooling_1 (S (None, 4, 240, 376)       0         
_________________________________________________________________
activation_1 (Activation)    (None, 4, 240, 376)       0         
=================================================================
Total params: 328.0
Trainable params: 328.0
Non-trainable params: 0.0

Unable to load a saved model with Spatial-Pyramid-Layer.

I successfully trained a model using Spatial-Pyramid-Pooling, however for evaluation purposes, I'm unable to load the saved model as SPP doesn't exist in original Keras model load function. Could you please suggest any work-around to this ?

ROI Example

Hey @yhenon!
Thanks for creating a repository on SPP methods!

Could you please post an example of a convolutional neural network that classifies the image and works on creating the binding box?

get_config() missing

In order to save and read a network to/from a json file a proper get_config() methode has to be provided. For the SpatialPyramidPooling layer the following would work:

class SpatialPyramidPooling(Layer):
  ...
  def get_config(self):
        config = {'pool_list': self.pool_list}
        base_config = super(SpatialPyramidPooling, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))

Not working dim_ordering, if you image_data_format(), SPP layer shape become <None, None>

Hi,
This code is not working for dim_ordering. I changed it to image_data_format() , now its working but the problem is that after adding spp layer, the output dimension becomes < None, None> . So can not add a dense layer after that.

The error is:

ValueError: The last dimension of the inputs to Dense should be defined. Found None.

Any idea how to solve this problem ?

TypeError: Expected int32, got list containing Tensors of type '_Message' instead.

My code is

model = Sequential()

model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1],
                        border_mode='valid',
                        input_shape=input_shape))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(0.25))

model.add(SpatialPyramidPooling([1, 2, 4]))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))

But it can`t work, here is the error

Traceback (most recent call last):
  File "/Users/liyuan/Desktop/keras-spp/test_spp.py", line 20, in <module>
    model.add(SpatialPyramidPooling(pooling_regions, input_shape=input_shape))
  File "/Library/Python/2.7/site-packages/keras/models.py", line 299, in add
    layer.create_input_layer(batch_input_shape, input_dtype)
  File "/Library/Python/2.7/site-packages/keras/engine/topology.py", line 401, in create_input_layer
    self(x)
  File "/Library/Python/2.7/site-packages/keras/engine/topology.py", line 572, in __call__
    self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
  File "/Library/Python/2.7/site-packages/keras/engine/topology.py", line 635, in add_inbound_node
    Node.create_node(self, inbound_layers, node_indices, tensor_indices)
  File "/Library/Python/2.7/site-packages/keras/engine/topology.py", line 166, in create_node
    output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
  File "/Users/liyuan/Desktop/keras-spp/SpatialPyramidPooling.py", line 110, in call
    outputs = K.concatenate(outputs,axis = 0)
  File "/Library/Python/2.7/site-packages/keras/backend/tensorflow_backend.py", line 1427, in concatenate
    return tf.concat(axis, [to_dense(x) for x in tensors])
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/array_ops.py", line 1029, in concat
    dtype=dtypes.int32).get_shape(
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/ops.py", line 637, in convert_to_tensor
    as_ref=False)
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/ops.py", line 702, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/constant_op.py", line 110, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/constant_op.py", line 99, in constant
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 367, in make_tensor_proto
    _AssertCompatible(values, dtype)
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 302, in _AssertCompatible
    (dtype.name, repr(mismatch), type(mismatch).__name__))
TypeError: Expected int32, got list containing Tensors of type '_Message' instead.

I don`t know how to solve the error:

TypeError: Expected int32, got list containing Tensors of type '_Message' instead.

my keras version is 1.2.1, and my tensorflow version is 1.0.1

Potential tf.keras fix

Posting this for future people who may run into the issues I did.

I couldn't get the original implementation to work properly for me, using tf.keras. Hence, I made the necessary modifications to produce the correct output shapes. Currently the output shape is defined as

(None, 1, total_n_pyramid_sections, n_channels)

The code I used to achieve this is:

# from tensorflow.keras.layers import Layer
# pylint: disable=import-error
import tensorflow as tf
from tensorflow.keras.layers import Layer, Reshape, concatenate, Permute
from tensorflow.keras import backend as K

# taken from https://github.com/yhenon/keras-spp


class SpatialPyramidPooling(Layer):
    """Spatial pyramid pooling layer for 2D inputs.
    See Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition,
    K. He, X. Zhang, S. Ren, J. Sun
    # Arguments
        pool_list: list of int
            List of pooling regions to use. The length of the list is the number of pooling regions,
            each int in the list is the number of regions in that pool. For example [1,2,4] would be 3
            regions with 1, 2x2 and 4x4 max pools, so 21 outputs per feature map
    # Input shape
        4D tensor with shape:
        `(samples, channels, rows, cols)` if dim_ordering='th'
        or 4D tensor with shape:
        `(samples, rows, cols, channels)` if dim_ordering='tf'.
    # Output shape
        2D tensor with shape:
        `(samples, channels * sum([i * i for i in pool_list])`
    """

    def __init__(self, pool_list, **kwargs):


        self.pool_list = pool_list

        self.num_outputs_per_channel = sum([i * i for i in pool_list])

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

    def build(self, input_shape):
        self.nb_channels = input_shape[3]

    def compute_output_shape(self, input_shape):
        return (input_shape[0], self.nb_channels * self.num_outputs_per_channel)

    def get_config(self):
        config = {'pool_list': self.pool_list}
        base_config = super(SpatialPyramidPooling, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))

    def call(self, x, mask=None):

        input_shape = K.shape(x)
        num_rows = input_shape[1]
        num_cols = input_shape[2]

        row_length = [K.cast(num_rows, 'float32') / i for i in self.pool_list]
        col_length = [K.cast(num_cols, 'float32') / i for i in self.pool_list]

        outputs = []

        for pool_num, num_pool_regions in enumerate(self.pool_list):
            for jy in range(num_pool_regions):
                for ix in range(num_pool_regions):
                    x1 = ix * col_length[pool_num]
                    x2 = ix * col_length[pool_num] + col_length[pool_num]
                    y1 = jy * row_length[pool_num]
                    y2 = jy * row_length[pool_num] + row_length[pool_num]

                    x1 = K.cast(K.round(x1), 'int32')
                    x2 = K.cast(K.round(x2), 'int32')
                    y1 = K.cast(K.round(y1), 'int32')
                    y2 = K.cast(K.round(y2), 'int32')

                    new_shape = [input_shape[0], y2 - y1,
                                    x2 - x1, input_shape[3]]

                    x_crop = x[:, y1:y2, x1:x2, :]
                    xm = Reshape(new_shape)(x_crop)
                    pooled_val = K.max(xm, axis=(1, 2))
                    outputs.append(pooled_val)


        outputs = concatenate(outputs, axis=2)
       # ---change this line to alter output shape---
        outputs = Reshape((input_shape[0], 1, self.num_outputs_per_channel, self.nb_channels))(outputs)
        outputs = K.squeeze(outputs, axis=1) # makes output only rank 4, instead of 5
        # outputs = concatenate(outputs)
        # outputs = Reshape((len(self.pool_list),self.num_outputs_per_channel,input_shape[0],input_shape[1]))(outputs)
        # outputs = Permute(outputs,(3,1,0,2))

        return outputs

Error when using with Keras 2

The following code gives an error with Keras 2:

def test(weights_path=None):
    tr = False
    model = Sequential()
    model.add(ZeroPadding2D((1,1),input_shape=(3,None,None), name='layer_0'))
    model.add(Convolution2D(64, (3, 3), activation='relu', trainable=tr))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(64, (3, 3), activation='relu', trainable=tr))
    model.add(MaxPooling2D((2,2), strides=(2,2)))
    
    model.add(SpatialPyramidPooling([1,2,4]))

    model.add(Dense(2, activation='softmax'))
    
    model.compile(loss=mean_squared_error,
              optimizer='adam')
    return model

t = test()
t.summary()

The error is:

TypeError Traceback (most recent call last)
in ()
----> 1 t = test()
2 t.summary()

in test(weights_path)
10 model.add(SpatialPyramidPooling([1,2,4]))
11
---> 12 model.add(Dense(2, activation='softmax'))
13
14 model.compile(loss=mean_squared_error,

/usr/local/lib/python2.7/dist-packages/keras/models.pyc in add(self, layer)
461 output_shapes=[self.outputs[0]._keras_shape])
462 else:
--> 463 output_tensor = layer(self.outputs[0])
464 if isinstance(output_tensor, list):
465 raise TypeError('All layers in a Sequential model '

/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in call(self, inputs, **kwargs)
549 'layer.build(batch_input_shape)')
550 if len(input_shapes) == 1:
--> 551 self.build(input_shapes[0])
552 else:
553 self.build(input_shapes)

/usr/local/lib/python2.7/dist-packages/keras/layers/core.pyc in build(self, input_shape)
825 name='kernel',
826 regularizer=self.kernel_regularizer,
--> 827 constraint=self.kernel_constraint)
828 if self.use_bias:
829 self.bias = self.add_weight((self.units,),

/usr/local/lib/python2.7/dist-packages/keras/engine/topology.pyc in add_weight(self, shape, initializer, name, trainable, regularizer, constraint)
382 """
383 initializer = initializers.get(initializer)
--> 384 weight = K.variable(initializer(shape), dtype=K.floatx(), name=name)
385 if regularizer is not None:
386 self.add_loss(regularizer(weight))

/usr/local/lib/python2.7/dist-packages/keras/initializers.pyc in call(self, shape, dtype)
198 scale /= max(1., fan_out)
199 else:
--> 200 scale /= max(1., float(fan_in + fan_out) / 2)
201 if self.distribution == 'normal':
202 stddev = np.sqrt(scale)

TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

Can anyone see what the problem might be?

Not working with tf.keras

I tried the given example with tf 1.14.

`
from tensorflow.keras.models import Sequential
from keras_spp.spp.SpatialPyramidPooling import SpatialPyramidPooling
from keras.layers import Convolution2D, Activation, MaxPooling2D, Dense

model = Sequential()

model.add(Convolution2D(32, 3, 3, border_mode='same', input_shape=(3, None, None)))
model.add(Activation('relu'))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(64, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(SpatialPyramidPooling([1, 2, 4]))
model.add(Dense(num_classes))
model.add(Activation('softmax'))`

Error received:

`---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
6
7 # uses theano ordering. Note that we leave the image size as None to allow multiple image sizes
----> 8 model.add(Convolution2D(32, 3, 3, border_mode='same', input_shape=(3, None, None)))
9 model.add(Activation('relu'))
10 model.add(Convolution2D(32, 3, 3))

~/anaconda3/envs/py3_tf14/lib/python3.6/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
455 self._self_setattr_tracking = False # pylint: disable=protected-access
456 try:
--> 457 result = method(self, *args, **kwargs)
458 finally:
459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access

~/anaconda3/envs/py3_tf14/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py in add(self, layer)
152 raise TypeError('The added layer must be '
153 'an instance of class Layer. '
--> 154 'Found: ' + str(layer))
155
156 tf_utils.assert_no_legacy_layers([layer])

TypeError: The added layer must be an instance of class Layer. Found: <keras.layers.convolutional.Conv2D object at 0x7f8057c3ddd8>
`

"TypeError: slice indices must be integers...." on roi_pooling and roi_pooling_conv

I installed the keras-spp package in my anaconda python 3.5 environment. The keras in use is supported by either "theano" or "tensorflow" backend. But running both test files test_roi_pooling and test_roi_pooling_conv.py give similar errors on TypeError. For example, when the keras use "tensorflow":

test_roi_pooling.py", line 54, in <module>
     X_curr = X_img[0, X_roi[0, roi, 0]:X_roi[0, roi, 2], X_roi[0, roi, 1]:X_roi[0, roi, 3], :]

TypeError: slice indices must be integers or None or have an __index__ method


test_roi_pooling_conv.py", line 59, in <module>
   X_roi[0, roi, 0]:X_roi[0, roi, 0] + X_roi[0, roi, 2], :]

TypeError: slice indices must be integers or None or have an index method

Unknown layer: SpatialPyramidPooling in predict stage

Hello. I tried to import SPP in my model with RNN. I build it and my model.fit function is working. System learned. But in System testing stage, which i call in different function after model learned finished and model saved, in "model.predict" command, i got error. that error is:
Unknown layer: SpatialPyramidPooling
I run test_spp.py file and it worked. In my model, what should i do?
Thanks

keras-rcnn problems

your repository of keras faster r-cnn is perfect, I think you should not deprecated it. I have a problem when I run the test_frcnn. But I have no place to ask my issue. So I turn to there to ask you, when I run the test_frcnn code, it cant't detect the object in the image, no rectangle was found in the picture , but I don't find any problems, Could you give me some advice to solve this problem? Thank you very much!

ROI pooling examples issues

Hello people

thanks for sharing this and providing the examples.

I've the test_spp file running without any issues (i.e. it prints the final message) but I get errors in the test_roi_pooling, the basic one being related to line 55:
X_curr = X_img[0, X_roi[0, roi, 0]:X_roi[0, roi, 2], X_roi[0, roi, 1]:X_roi[0, roi, 3], :] TypeError: slice indices must be integers or None or have an __index__ method

I tried manually changing the initials to different settings but can't get really get it working, or get my head around it. Given the first test is successful, I thought this might be a common issue?

Btw, I get the exact same error when running the other roi test: test_roi_pooling_conv, at line 59:

X_roi[0, roi, 0]:X_roi[0, roi, 0] + X_roi[0, roi, 2], :] TypeError: slice indices must be integers or None or have an __index__ method

.. which makes me believe there is a typo or something?

Batch_size

Hi, in your documentation of the ROIPooling layer, you mention the batch size is 1. Does this mean you can't train/predict with an arbitrary batch_size?

RoiPooling is slow

I am trying to create faster rcnn by hand, and use this RoiPooling.py.
However, i am facing one problem.
RoiPooling is very slow for loading model with large minibatch.
Do you know this cause?

Input of CAReduce{maximum} has zero-size on axis 2

Hi, I was using the SpatialPyramidPooling you wrote. Excellent works and Thanks.

But when I run the model with SpatialPyramidPooling, first time the shape including number sample is (32, 19567, 50), and it goes well. The second batch has shape (32, 7010, 50). And this is where the problem occurs.

--------- Error Message ---------

Traceback (most recent call last):
      File "all_models_v2.py", line 755, in <module>
        main()
      File "all_models_v2.py", line 700, in main
       model.fit_generator( generator=train_data_gen, steps_per_epoch=TRAIN_LENGTH // 
BATCH_SIZE, epochs = 10, callbacks=[early_stopper], validation_data = val_data_gen, 
validation_steps = VAL_LENGTH // BATCH_SIZE)
      File "/home/k/shankai/app/anaconda2/lib/python2.7/site-
packages/keras/legacy/interfaces.py", line 88, in wrapper
    return func(*args, **kwargs)
  File "/home/k/shankai/app/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 1902, in fit_generator
    class_weight=class_weight)
  File "/home/k/shankai/app/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 1642, in train_on_batch
    outputs = self.train_function(ins)
  File "/home/k/shankai/app/anaconda2/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 1196, in __call__
    return self.function(*inputs)
  File "/home/k/shankai/app/anaconda2/lib/python2.7/site-packages/theano/compile/function_module.py", line 898, in __call__
    storage_map=getattr(self.fn, 'storage_map', None))
  File "/home/k/shankai/app/anaconda2/lib/python2.7/site-packages/theano/gof/link.py", line 325, in raise_with_op
    reraise(exc_type, exc_value, exc_trace)
  File "/home/k/shankai/app/anaconda2/lib/python2.7/site-packages/theano/compile/function_module.py", line 884, in __call__
    self.fn() if output_subset is None else\
ValueError: Input of CAReduce{maximum} has zero-size on axis 2
Apply node that caused the error: Reduce{maximum}{1, 2}(Reshape{4}.0)
Toposort index: 666
Inputs types: [TensorType(float32, 4D)]
Inputs shapes: [(32, 19564, 0, 1)]
Inputs strides: [(234768, 4, 4, 4)]
Inputs values: [array([], shape=(32, 19564, 0, 1), dtype=float32)]
Outputs clients: [[Join(TensorConstant{1}, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max), InplaceDimShuffle{0,x,x,1}(max)]]

I am using Theano backend of Keras. Ubuntu, since there is no reshape in my model except maybe in SpatialPyramidPooling. Following is my model:

main_input = Input(shape=input_shape, dtype = "float32", name = "main_input")

conv11 = Conv2D(1, (3,50), activation = "relu", data_format="channels_last")(main_input)
conv11 = BatchNormalization()(conv11)

conv12 = Conv2D(1, (4,50), activation = "relu", data_format="channels_last")(main_input)
conv12 = BatchNormalization()(conv12)

conv13 = Conv2D(1, (5,50), activation = "relu", data_format="channels_last")(main_input)
conv13 = BatchNormalization()(conv13)

conv1 = Concatenate(1)([conv11, conv12, conv13])

if spp_shape is not None:
	conv1 = SpatialPyramidPooling(spp_shape)(conv1)
	output = Dense(NUM_CLASS, input_shape = (50,), activation = "softmax")(conv1)
else:
	conv1 = Flatten()(conv1)
	output = Dense(NUM_CLASS, input_shape = (2991,), activation = "softmax" )(conv1)

model = Model(inputs=[main_input], outputs=[output] )

Thanks

Roi_pooling_conv for Fast RCNN ?

I'm trying to implement Fast RCNN using your helper layers. I wanted to ask that I would have to use roi_pooling_conv right ?

Setup Issue

Line 3 of the setup.py. Unexpected newline. Says I added too many arguments, none were added.

roi pooling with vgg

Hi @yhenon!
Thanks for creating a repository on SPP methods!
I've used your code in the following model, but I encountered the following error. Please help me.
.
.
...
pooling_regions = [1, 2, 4]
num_rois = 2
num_channels = 3
in_img = Input(shape=(224,224, num_channels))
in_roi = Input(shape=(num_rois, 4))

model = Sequential([
ZeroPadding2D((1,1),input_shape=input_shape),
Conv2D(64, (3, 3),
activation='relu'),
ZeroPadding2D((1,1)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
ZeroPadding2D((1,1)),
Conv2D(128, (3, 3), activation='relu'),
ZeroPadding2D((1,1)),
Conv2D(128, (3, 3), activation='relu'),
ZeroPadding2D((1,1)),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
ZeroPadding2D((1,1)),
Conv2D(256, (3, 3), activation='relu'),
ZeroPadding2D((1,1)),
Conv2D(256, (3, 3), activation='relu'),
ZeroPadding2D((1,1)),
Conv2D(256, (3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
ZeroPadding2D((1,1)),
Conv2D(512, (3, 3), activation='relu'),
ZeroPadding2D((1,1)),
Conv2D(512, (3, 3), activation='relu'),
ZeroPadding2D((1,1)),
Conv2D(512, (3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
ZeroPadding2D((1,1)),
Conv2D(512, (3, 3), activation='relu'),
ZeroPadding2D((1,1)),
Conv2D(512, (3, 3), activation='relu'),
ZeroPadding2D((1,1)),
Conv2D(512, (3, 3), activation='relu'),
Conv2DTranspose(128, (1, 1), activation='relu', padding='same',strides=(1, 1)),
Conv2DTranspose(256, (4, 4), activation='relu', padding='same', strides=(2, 2)),
Conv2DTranspose(256, (8, 8), activation='relu', padding='same', strides=(4, 4)),
RoiPooling(pooling_regions, num_rois)([in_img, in_roi]),
SpatialPyramidPooling([1,2,4]),
Dense(4096, activation='relu'),
Dropout(0.5),
Dense(4096, activation='relu'),
Dropout(0.5),
Dense(2, activation='softmax')
])
...
TypeError: The added layer must be an instance of class Layer. Found: Tensor("roi_pooling_1/Reshape_42:0", shape=(1, 2, 63), dtype=float32)

The dimension of the inputs

Hi

I was trying to run the example in the front page, but got an error as
"The channel dimension of the inputs should be defined. Found None "
It seems that Convolution2D doesn't accept the input shape as "None".
How to make it working? Thanks.

The full error message is as follows:
ValueError Traceback (most recent call last)
in ()
11
12 # uses theano ordering. Note that we leave the image size as None to allow multiple image sizes
---> 13 model.add(Convolution2D(32, 3, 3, border_mode='same', input_shape=(3, None, None)))
14 model.add(Activation('relu'))
15 model.add(Convolution2D(32, 3, 3))

/home/u3714/.conda/envs/test_env/lib/python3.5/site-packages/keras/models.py in add(self, layer)
420 # and create the node connecting the current layer
421 # to the input layer we just created.
--> 422 layer(x)
423
424 if len(layer.inbound_nodes) != 1:

/home/u3714/.conda/envs/test_env/lib/python3.5/site-packages/keras/engine/topology.py in call(self, inputs, **kwargs)
526 'layer.build(batch_input_shape)')
527 if len(input_shapes) == 1:
--> 528 self.build(input_shapes[0])
529 else:
530 self.build(input_shapes)

/home/u3714/.conda/envs/test_env/lib/python3.5/site-packages/keras/layers/convolutional.py in build(self, input_shape)
123 channel_axis = -1
124 if input_shape[channel_axis] is None:
--> 125 raise ValueError('The channel dimension of the inputs '
126 'should be defined. Found None.')
127 input_dim = input_shape[channel_axis]

ValueError: The channel dimension of the inputs should be defined. Found None.

Error in test_roi_pooling_conv.py

When I tried to run the script it gave me the error
AttributeError: 'RoiPoolingConv' object has no attribute 'pool_list'

Also can you please create an example for using the scripts

thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.