How to work with variable size image in CNN using Keras?

I am currently working on CNN on an image to extract a function using keras. All images - 276 rows, x columns and 3 color dimensions (RGB). The number of columns is equal to the length of the vector of output functions that it should generate.

Presentation of input - change:

The input data for the image consists of columnar fragments of the image. which means that the actual input to the image is (276.3), and the number of columns is equal to the length of the function that it should generate.

My initial model is this:

    print "Model Definition"
    model = Sequential()

    model.add(Convolution2D(64,row,1,input_shape=(row,None,3)))
    print model.output_shape
    model.add(MaxPooling2D(pool_size=(1,64)))
    print model.output_shape
    model.add(Dense(1,activation='relu'))

My prints are between images output.shape, and I seem a bit confused on the output.

Model Definition
(None, 1, None, 64)
(None, 1, None, 64)

Why is 3D data becoming 4d? And that keeps up after the maxpoolling2d layer ?.

/ :

Traceback (most recent call last):
  File "keras_convolutional_feature_extraction.py", line 466, in <module>
    model(0,train_input_data,output_data_train,test_input_data,output_data_test)
  File "keras_convolutional_feature_extraction.py", line 440, in model
    model.add(Dense(1,activation='relu'))
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 324, in add
    output_tensor = layer(self.outputs[0])
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 474, in __call__
    self.assert_input_compatibility(x)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 415, in assert_input_compatibility
    str(K.ndim(x)))
Exception: Input 0 is incompatible with layer dense_1: expected ndim=2, found ndim=4

, 1 .

+4
1

276 x None x 3, 64 , 276 x 1 ( rows = 276). 1 x None. , , . , 64 ( Theano) 64 x 1 x None. Tensorflow , 1 x None x 64. Keras-Theano - . , None x 64 x 1 x None. Tensorflow None x 1 x None x 64. Keras.

, , , Dense.

model.add(Flatten())

, . , , , . , None , , . 1 x None, average , 1 x 1 x None.

. 276 x n x 3, , 1 x n, :

model = Sequential()
model.add(Convolution2D(64,row,1,input_shape=(row,None,3)))
model.add(Convolution2D(1,1,1))
print model.output_shape  # this should print `None x 1 x None x 1`
model.add(flatten())

, , 64 . (, 276 - ). :

  • , .. , 3 .
  • .

, 50. :

model = Sequential()
model.add(Convolution2D(32,3,1,activation='relu',
          init='he_normal',input_shape=(row,None,3)))  # row = 50
model.add(Convolution2D(32,3,1,activation='relu',init='he_normal'))
model.add(MaxPooling2D(pool_size=(2,1), strides=(2,1), name='pool1'))
model.add(Convolution2D(64,3,1,activation='relu',init='he_normal'))
model.add(Convolution2D(64,3,1,activation='relu',init='he_normal'))
model.add(MaxPooling2D(pool_size=(2,1), strides=(2,1), name='pool2'))
model.add(Convolution2D(128,3,1,activation='relu',init='he_normal'))
model.add(Convolution2D(128,3,1,activation='relu',init='he_normal'))
model.add(Convolution2D(128,3,1,activation='relu',init='he_normal'))
model.add(MaxPooling2D(pool_size=(2,1), strides=(2,1), name='pool3'))
model.add(Convolution2D(1,1,1), name='squash_channels')
print model.output_shape  # this should print `None x 1 x None x 1`
model.add(flatten(), name='flatten_input')

, 50 1 .

- , . 224. 224 x n, (, ). , , , p x n' p > 224 n' != n. 224 x n' . -.

, , , ( ) . , , .

Edit:

. CNN, 3 x 3 . , 50 x n x 3. , p x q x r , f, 3 x 3, 1. . (p-2) x (q-2) x f , . (2,1) (2,1). y ( ). , ( , CNN, ).

CNN: None x 50 x n x 3

pool1: None x 46 x n x 32
pool1: None x 23 x n x 32

pool2: None x 19 x n x 64
pool2: None x 9 x n x 64 ( , Keras , (19/2) = 9)

pool3: None x 3 x n x 128
pool3: None x 1 x n x 128

squash_channels: None x 1 x n x 128
squash_channels: None x 1 x n x 1

flatten_input: None x 1 x n x 1
flatten_input: None x n

, , . , .

+2

Source: https://habr.com/ru/post/1671950/


All Articles