Repository: deeplearning4j/deeplearning4j
Issue: M1: Parameters of Keras MaxPooling1D layer are incorrectly read

#### Issue Description

I trained a model using Tensorflow/Keras version 2.4.1 in Python. This model contains a MaxPooling1D layer. I then saved the model as the (default) .hdf5 file. Loading this file in Keras and saving its config as json, reveals the following layer:

``` json
{
  "class_name": "MaxPooling1D",
  "config": {
      "name": "max_pooling1d_3",
      "trainable": true,
      "dtype": "float32",
      "strides": [
          836
      ],
      "pool_size": [
          836
      ],
      "padding": "valid",
      "data_format": "channels_last"
  },
  "name": "max_pooling1d_3",
  "inbound_nodes": [
      [
          [
              "conv1d_3",
              0,
              0,
              {}
          ]
      ]
  ]
},
```
Please note that the **data_format** is set to "channels_last" which is the default value in Keras. This corresponds to NHWC. The stride is loaded as `(836,)` in Keras - a 1-tuple holding only the value (836). 

If I now load the model in 1.0.0-M1 using `importKerasModelAndWeights()`, this specific layer is loaded as a `Subsampling1DLayer`. Unfortunately, the parameter values are incorrectly parsed. The `Cnn2dDataFormat` is set to `NCHW`. The stride is loaded as `[836,2]` which leads to incorrect dimensions in the following layer. I believe that `[836,1]` is the correct value for the stride.

When I overwrite these two parameter values in my loaded network, then everything runs as expected.

#### Version Information

Please indicate relevant versions, including, if relevant:

* Deeplearning4j version: 1.0.0-M1
* Tensorflow version 2.4.1 (same error exists for models trained in Tensorflow 1.14)
* Platform information: Ubuntu 20.04


Changed files:
deeplearning4j/deeplearning4j-modelimport/src/test/java/org/deeplearning4j/nn/modelimport/keras/layers/pooling/KerasPooling1DTest.java
deeplearning4j/deeplearning4j-modelimport/src/main/java/org/deeplearning4j/nn/modelimport/keras/layers/pooling/KerasPooling1D.java
deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/layers/convolution/subsampling/SubsamplingLayer.java
deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/SubsamplingLayer.java
deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/nn/layers/HelperUtilsTest.java

diff --git a/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/nn/layers/HelperUtilsTest.java b/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/nn/layers/HelperUtilsTest.java
index 71f01b0235..81de2dae85 100644
--- a/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/nn/layers/HelperUtilsTest.java
+++ b/deeplearning4j/deeplearning4j-core/src/test/java/org/deeplearning4j/nn/layers/HelperUtilsTest.java
@@ -57,6 +57,7 @@ import org.nd4j.linalg.lossfunctions.LossFunctions;
 
 import java.util.List;
 
+import static org.deeplearning4j.common.config.DL4JSystemProperties.DISABLE_HELPER_PROPERTY;
 import static org.junit.jupiter.api.Assertions.*;
 
 /**
@@ -75,7 +76,7 @@ public class HelperUtilsTest extends BaseDL4JTest {
     @Test
     @DisplayName("Test instance creation of various helpers")
     public void testOneDnnHelperCreation() {
-        System.setProperty(HelperUtils.DISABLE_HELPER_PROPERTY,"false");
+        System.setProperty(DISABLE_HELPER_PROPERTY,"false");
         assertNotNull(HelperUtils.createHelper("",
                 MKLDNNLSTMHelper.class.getName(), LSTMHelper.class,"layername",getDataType()));
         assertNotNull(HelperUtils.createHelper("", MKLDNNBatchNormHelper.class.getName(),
diff --git a/deeplearning4j/deeplearning4j-modelimport/src/main/java/org/deeplearning4j/nn/modelimport/keras/layers/pooling/KerasPooling1D.java b/deeplearning4j/deeplearning4j-modelimport/src/main/java/org/deeplearning4j/nn/modelimport/keras/layers/pooling/KerasPooling1D.java
index bb27e7d816..6030e5c38f 100644
--- a/deeplearning4j/deeplearning4j-modelimport/src/main/java/org/deeplearning4j/nn/modelimport/keras/layers/pooling/KerasPooling1D.java
+++ b/deeplearning4j/deeplearning4j-modelimport/src/main/java/org/deeplearning4j/nn/modelimport/keras/layers/pooling/KerasPooling1D.java
@@ -66,6 +66,7 @@ public class KerasPooling1D extends KerasLayer {
         Subsampling1DLayer.Builder builder = new Subsampling1DLayer.Builder(
                 KerasPoolingUtils.mapPoolingType(this.className, conf)).name(this.layerName)
                 .dropOut(this.dropout)
+                .dataFormat(dimOrder == DimOrder.TENSORFLOW ? CNN2DFormat.NHWC : CNN2DFormat.NCHW)
                 .convolutionMode(getConvolutionModeFromConfig(layerConfig, conf))
                 .kernelSize(getKernelSizeFromConfig(layerConfig, 1, conf, kerasMajorVersion)[0])
                 .stride(getStrideFromConfig(layerConfig, 1, conf)[0]);
diff --git a/deeplearning4j/deeplearning4j-modelimport/src/test/java/org/deeplearning4j/nn/modelimport/keras/layers/pooling/KerasPooling1DTest.java b/deeplearning4j/deeplearning4j-modelimport/src/test/java/org/deeplearning4j/nn/modelimport/keras/layers/pooling/KerasPooling1DTest.java
index a7163d1cfb..a81a7bb7af 100644
--- a/deeplearning4j/deeplearning4j-modelimport/src/test/java/org/deeplearning4j/nn/modelimport/keras/layers/pooling/KerasPooling1DTest.java
+++ b/deeplearning4j/deeplearning4j-modelimport/src/test/java/org/deeplearning4j/nn/modelimport/keras/layers/pooling/KerasPooling1DTest.java
@@ -19,21 +19,31 @@
  */
 package org.deeplearning4j.nn.modelimport.keras.layers.pooling;
 
+import org.deeplearning4j.nn.api.Layer;
+import org.deeplearning4j.nn.conf.CNN2DFormat;
 import org.deeplearning4j.nn.conf.ConvolutionMode;
 import org.deeplearning4j.nn.conf.layers.PoolingType;
 import org.deeplearning4j.nn.conf.layers.Subsampling1DLayer;
 import org.deeplearning4j.BaseDL4JTest;
+import org.deeplearning4j.nn.graph.ComputationGraph;
+import org.deeplearning4j.nn.graph.vertex.GraphVertex;
+import org.deeplearning4j.nn.modelimport.keras.KerasModelImport;
 import org.deeplearning4j.nn.modelimport.keras.config.Keras1LayerConfiguration;
 import org.deeplearning4j.nn.modelimport.keras.config.Keras2LayerConfiguration;
 import org.deeplearning4j.nn.modelimport.keras.config.KerasLayerConfiguration;
 import org.junit.jupiter.api.Tag;
 import org.junit.jupiter.api.Test;
+
+import java.io.File;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Map;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
 import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.extension.ExtendWith;
+import org.nd4j.common.resources.Resources;
 import org.nd4j.common.tests.tags.NativeTag;
 import org.nd4j.common.tests.tags.TagNames;
 
@@ -111,4 +121,18 @@ class KerasPooling1DTest extends BaseDL4JTest {
         assertEquals(ConvolutionMode.Truncate, layer.getConvolutionMode());
         assertEquals(VALID_PADDING[0], layer.getPadding()[0]);
     }
+
+
+    @Test
+    public void testPooling1dNWHC() throws  Exception {
+        File file = Resources.asFile("modelimport/keras/tfkeras/issue_9349.hdf5");
+        ComputationGraph computationGraph = KerasModelImport.importKerasModelAndWeights(file.getAbsolutePath());
+        GraphVertex maxpooling1d = computationGraph.getVertex("max_pooling1d");
+        assertNotNull(maxpooling1d);
+        Layer layer = maxpooling1d.getLayer();
+        org.deeplearning4j.nn.layers.convolution.subsampling.Subsampling1DLayer subsampling1DLayer = (org.deeplearning4j.nn.layers.convolution.subsampling.Subsampling1DLayer) layer;
+        assertEquals(CNN2DFormat.NHWC,subsampling1DLayer.layerConf().getCnn2dDataFormat());
+    }
+
+
 }
diff --git a/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/SubsamplingLayer.java b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/SubsamplingLayer.java
index f0ebd4e7a9..91d97ff437 100644
--- a/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/SubsamplingLayer.java
+++ b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/SubsamplingLayer.java
@@ -100,10 +100,12 @@ public class SubsamplingLayer extends NoParamLayer {
         this.stride = builder.stride;
         this.padding = builder.padding;
         this.convolutionMode = builder.convolutionMode;
+        this.cnn2dDataFormat = builder.cnn2DFormat;
+
         if (builder instanceof Builder) {
             this.dilation = ((Builder) builder).dilation;
-            this.cnn2dDataFormat = ((Builder) builder).dataFormat;
         }
+        
         this.pnorm = builder.pnorm;
         this.eps = builder.eps;
         this.cudnnAllowFallback = builder.cudnnAllowFallback;
@@ -239,7 +241,6 @@ public class SubsamplingLayer extends NoParamLayer {
          * Dilation for kernel
          */
         private int[] dilation = new int[] {1, 1};
-        protected CNN2DFormat dataFormat = CNN2DFormat.NCHW;
 
         public Builder(PoolingType poolingType, int[] kernelSize, int[] stride) {
             super(poolingType, kernelSize, stride);
@@ -318,16 +319,7 @@ public class SubsamplingLayer extends NoParamLayer {
             return this;
         }
 
-        /**
-         * Set the data format for the CNN activations - NCHW (channels first) or NHWC (channels last).
-         * See {@link CNN2DFormat} for more details.<br>
-         * Default: NCHW
-         * @param format Format for activations (in and out)
-         */
-        public Builder dataFormat(CNN2DFormat format){
-            this.dataFormat = format;
-            return this;
-        }
+
 
         /**
          * Kernel dilation. Default: {1, 1}, which is standard convolutions. Used for implementing dilated convolutions,
@@ -382,7 +374,7 @@ public class SubsamplingLayer extends NoParamLayer {
         }
 
         public void setDataFormat(CNN2DFormat format){
-            this.dataFormat = format;
+            this.cnn2DFormat = format;
         }
     }
 
@@ -418,6 +410,11 @@ public class SubsamplingLayer extends NoParamLayer {
         protected boolean cudnnAllowFallback = true;
         protected boolean avgPoolIncludePadInDivisor = false;
 
+        /**
+         * Configure the 2d data format
+         */
+        protected CNN2DFormat cnn2DFormat = CNN2DFormat.NCHW;
+
         protected BaseSubsamplingBuilder(PoolingType poolingType, int[] kernelSize, int[] stride) {
             this.setPoolingType(poolingType.toPoolingType());
             this.setKernelSize(kernelSize);
@@ -477,7 +474,7 @@ public class SubsamplingLayer extends NoParamLayer {
             this.pnorm = pnorm;
         }
 
-        public void setEps(double eps){
+        public void setEps(double eps) {
             ValidationUtils.validateNonNegative(eps, "eps");
             this.eps = eps;
         }
@@ -490,6 +487,17 @@ public class SubsamplingLayer extends NoParamLayer {
             this.convolutionMode = convolutionMode;
         }
 
+        /**
+         * Set the data format for the CNN activations - NCHW (channels first) or NHWC (channels last).
+         * See {@link CNN2DFormat} for more details.<br>
+         * Default: NCHW
+         * @param cnn2DFormat Format for activations (in and out)
+         */
+        public T dataFormat(CNN2DFormat cnn2DFormat) {
+            this.cnn2DFormat = cnn2DFormat;
+            return (T) this;
+        }
+
         /**
          * Set the convolution mode for the Convolution layer. See {@link ConvolutionMode} for more details
          *
diff --git a/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/layers/convolution/subsampling/SubsamplingLayer.java b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/layers/convolution/subsampling/SubsamplingLayer.java
index ebaa7008a3..3e5dc3c377 100644
--- a/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/layers/convolution/subsampling/SubsamplingLayer.java
+++ b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/layers/convolution/subsampling/SubsamplingLayer.java
@@ -87,7 +87,7 @@ public class SubsamplingLayer extends AbstractLayer<org.deeplearning4j.nn.conf.l
         CNN2DFormat dataFormat = layerConf().getCnn2dDataFormat();
         int hIdx = 2;
         int wIdx = 3;
-        if(dataFormat == CNN2DFormat.NHWC){
+        if(dataFormat == CNN2DFormat.NHWC) {
             hIdx = 1;
             wIdx = 2;
         }