Does CoreML support Maxpooling1D?(keras)

I am trying to convert a keras model into a CoreML model, and folloing is summary.

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1 (Conv1D)               (None, 6000, 5)           725       
_________________________________________________________________
activation_1 (Activation)    (None, 6000, 5)           0         
_________________________________________________________________
conv2 (Conv1D)               (None, 6000, 5)           305       
_________________________________________________________________
activation_2 (Activation)    (None, 6000, 5)           0         
_________________________________________________________________
conv3 (Conv1D)               (None, 6000, 5)           305       
_________________________________________________________________
activation_3 (Activation)    (None, 6000, 5)           0         
_________________________________________________________________
conv4 (Conv1D)               (None, 6000, 5)           305       
_________________________________________________________________
activation_4 (Activation)    (None, 6000, 5)           0         
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 1200, 5)           0         
=================================================================



In Xcode, my code is following

    @objc func count(){
        var csvArray: [NSNumber] = loadCSV(filename: "x1")
        csvArray.removeLast()
        guard let mlarray = try? MLMultiArray(shape:[1, 6000, 12], dataType:MLMultiArrayDataType.float32) else {
            fatalError("Unexpected runtime error. MLMultiArray")
        }
        for (index, element) in csvArray.enumerated() {
            mlarray[index] = element
        }
  
        let i = fit_hoge6Input(input1: mlarray)
        let model = fit_hoge6()
        guard let result = try? model.prediction(input: i) else {
            fatalError("error")
        }//1*1200*5
        print(result.output1)






The output is following

Double 6000 x 5 matrix
[0.4664617776870728,0.4912139475345612,0.4796954393386841,0.484947144985199,0.4818514585494995;
0.4664617776870728,0.4912139475345612,0.4796954393386841,0.484947144985199,0.4818514585494995;
0.4664617776870728,0.4912139475345612,0.4796954393386841,0.484947144985199,0.4818514585494995;
0.4664617776870728,0.4912139475345612,0.4796954393386841,0.484947144985199,0.4818514585494995;
...
0.4664617776870728,0.4912139475345612,0.4796954393386841,0.484947144985199,0.4818514585494995;
0.4664617776870728,0.4912139475345612,0.4796954393386841,0.484947144985199,0.4818514585494995;
0.4664617776870728,0.4912139475345612,0.4796954393386841,0.484947144985199,0.4818514585494995;
0.4664617776870728,0.4912139475345612,0.4796954393386841,0.484947144985199,0.4818514585494995;
0.4664617776870728,0.4912139475345612,0.4796954393386841,0.484947144985199,0.4818514585494995;
0.4664617776870728,0.4912139475345612,0.4796954393386841,0.484947144985199,0.4818514585494995;
0.4664617776870728,0.4912139475345612,0.4796954393386841,0.484947144985199,0.4818514585494995;]




All value is same.
and model.predition output is following message.

"Extension[37287:2436870] BNNS_POOLING: supported pooling dimensions: k_width == k_height and x_stride == y_stride"



On the other hand, when I execute code in python, output is following.

[[[0.4664617e-01,0.49121394e-01, 0.47969544e-01,0.484947144e-01,0.48185145e-01,
  [3.2913774e-01, 3.9123061e-01, 2.86454583e-01, 4.83056443e-01, 2.7176388e-01,
  ...,
  ]




My model use maxpooling1d, but error message is k_width == k_height.

This is strange.

I want to know whether maxpooling1d is supported or not.

Accepted Reply

According to coremltools, MaxPooling1D is supported (which is why you were able to convert your model). It, however, creates a 2D pooling layer in Core ML with height set to 1 and width set to the pooling kernel size. (Likewise for the Conv1D layers.)


But the error message you're getting means that BNNS (the library that runs neural networks on the CPU) doesn't actually know how to handle pooling where the kernel height and width and not the same. This seems to be a limitation of BNNS.


If this is really the last layer of your model, then I would remove it and just write the pooling code by hand.


(You could try two max-pooling layers with size 2 instead of one with size 4, to see if that maybe works...)

Replies

According to coremltools, MaxPooling1D is supported (which is why you were able to convert your model). It, however, creates a 2D pooling layer in Core ML with height set to 1 and width set to the pooling kernel size. (Likewise for the Conv1D layers.)


But the error message you're getting means that BNNS (the library that runs neural networks on the CPU) doesn't actually know how to handle pooling where the kernel height and width and not the same. This seems to be a limitation of BNNS.


If this is really the last layer of your model, then I would remove it and just write the pooling code by hand.


(You could try two max-pooling layers with size 2 instead of one with size 4, to see if that maybe works...)

@kerfuffle
Thank you for replying.
I deleted maxpooling1d, and made new .mlmodel, and executed code.
Then, I successed to delete BNNS error, but the output remained to be same value in all row.
I want to teach the solution, if you have any idea.