diff --git a/testdata/dnn/layers/lstm.hidden.B.npy b/testdata/dnn/layers/lstm.hidden.B.npy new file mode 100644 index 000000000..ca178d675 Binary files /dev/null and b/testdata/dnn/layers/lstm.hidden.B.npy differ diff --git a/testdata/dnn/layers/lstm.hidden.R.npy b/testdata/dnn/layers/lstm.hidden.R.npy new file mode 100644 index 000000000..b4ff454c1 Binary files /dev/null and b/testdata/dnn/layers/lstm.hidden.R.npy differ diff --git a/testdata/dnn/layers/lstm.hidden.W.npy b/testdata/dnn/layers/lstm.hidden.W.npy new file mode 100644 index 000000000..f35a32424 Binary files /dev/null and b/testdata/dnn/layers/lstm.hidden.W.npy differ diff --git a/testdata/dnn/layers/lstm.hidden.c0.npy b/testdata/dnn/layers/lstm.hidden.c0.npy new file mode 100644 index 000000000..1f43a8dce Binary files /dev/null and b/testdata/dnn/layers/lstm.hidden.c0.npy differ diff --git a/testdata/dnn/layers/lstm.hidden.h0.npy b/testdata/dnn/layers/lstm.hidden.h0.npy new file mode 100644 index 000000000..889bdd4ca Binary files /dev/null and b/testdata/dnn/layers/lstm.hidden.h0.npy differ diff --git a/testdata/dnn/layers/lstm.hidden.input.npy b/testdata/dnn/layers/lstm.hidden.input.npy new file mode 100644 index 000000000..4b36d6f6f Binary files /dev/null and b/testdata/dnn/layers/lstm.hidden.input.npy differ diff --git a/testdata/dnn/layers/lstm.hidden.output.npy b/testdata/dnn/layers/lstm.hidden.output.npy new file mode 100644 index 000000000..8770791d5 Binary files /dev/null and b/testdata/dnn/layers/lstm.hidden.output.npy differ diff --git a/testdata/dnn/onnx/data/input_hidden_lstm.npy b/testdata/dnn/onnx/data/input_hidden_lstm.npy new file mode 100644 index 000000000..d0a66a994 Binary files /dev/null and b/testdata/dnn/onnx/data/input_hidden_lstm.npy differ diff --git a/testdata/dnn/onnx/data/input_hidden_lstm_bi.npy b/testdata/dnn/onnx/data/input_hidden_lstm_bi.npy new file mode 100644 index 000000000..406844163 Binary files /dev/null and b/testdata/dnn/onnx/data/input_hidden_lstm_bi.npy differ diff --git a/testdata/dnn/onnx/data/output_hidden_lstm.npy b/testdata/dnn/onnx/data/output_hidden_lstm.npy new file mode 100644 index 000000000..1de0fa377 Binary files /dev/null and b/testdata/dnn/onnx/data/output_hidden_lstm.npy differ diff --git a/testdata/dnn/onnx/data/output_hidden_lstm_bi.npy b/testdata/dnn/onnx/data/output_hidden_lstm_bi.npy new file mode 100644 index 000000000..1b3ebdc3d Binary files /dev/null and b/testdata/dnn/onnx/data/output_hidden_lstm_bi.npy differ diff --git a/testdata/dnn/onnx/generate_onnx_models.py b/testdata/dnn/onnx/generate_onnx_models.py index 234118f73..6187bbe54 100644 --- a/testdata/dnn/onnx/generate_onnx_models.py +++ b/testdata/dnn/onnx/generate_onnx_models.py @@ -769,6 +769,33 @@ def forward(self, x): lstm = LSTM(features, hidden, batch, bidirectional=True) save_data_and_model("lstm_bidirectional", input, lstm) + + +class HiddenLSTM(nn.Module): + def __init__(self, input_size, hidden_size, num_layers=1, is_bidirectional=False): + super().__init__() + self.hidden_size = hidden_size + self.num_layers = num_layers + self.bi_coeff = 2 if is_bidirectional else 1 + self.lstm = nn.LSTM(input_size=input_size, hidden_size=hidden_size, + num_layers=num_layers, bidirectional=is_bidirectional) + + def forward(self, t): + h_0 = torch.ones(self.num_layers * self.bi_coeff, t.size(1), + self.hidden_size) + c_0 = torch.ones(self.num_layers * self.bi_coeff, t.size(1), + self.hidden_size) + return self.lstm(t, (h_0, c_0))[0] + +input = torch.randn(seq_len, batch, features) +hidden_lstm = HiddenLSTM(features, hidden, num_layers=3, is_bidirectional=False) +save_data_and_model("hidden_lstm", input, hidden_lstm, version=11, export_params=True) + +input = torch.randn(seq_len, batch, features) +hidden_lstm = HiddenLSTM(features, hidden, num_layers=3, is_bidirectional=True) +save_data_and_model("hidden_lstm_bi", input, hidden_lstm, version=11, export_params=True) + + class MatMul(nn.Module): def __init__(self): super(MatMul, self).__init__() diff --git a/testdata/dnn/onnx/models/hidden_lstm.onnx b/testdata/dnn/onnx/models/hidden_lstm.onnx new file mode 100644 index 000000000..62152f83c Binary files /dev/null and b/testdata/dnn/onnx/models/hidden_lstm.onnx differ diff --git a/testdata/dnn/onnx/models/hidden_lstm_bi.onnx b/testdata/dnn/onnx/models/hidden_lstm_bi.onnx new file mode 100644 index 000000000..fe5b959be Binary files /dev/null and b/testdata/dnn/onnx/models/hidden_lstm_bi.onnx differ diff --git a/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nchw_in.npy b/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nchw_in.npy new file mode 100644 index 000000000..3dc59dbb9 Binary files /dev/null and b/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nchw_in.npy differ diff --git a/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nchw_net.pb b/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nchw_net.pb new file mode 100644 index 000000000..4fb64436a Binary files /dev/null and b/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nchw_net.pb differ diff --git a/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nchw_out.npy b/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nchw_out.npy new file mode 100644 index 000000000..822c21fea Binary files /dev/null and b/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nchw_out.npy differ diff --git a/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nhwc_in.npy b/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nhwc_in.npy new file mode 100644 index 000000000..a67675944 Binary files /dev/null and b/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nhwc_in.npy differ diff --git a/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nhwc_net.pb b/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nhwc_net.pb new file mode 100644 index 000000000..14fce4876 Binary files /dev/null and b/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nhwc_net.pb differ diff --git a/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nhwc_out.npy b/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nhwc_out.npy new file mode 100644 index 000000000..11bedf41e Binary files /dev/null and b/testdata/dnn/tensorflow/conv2d_asymmetric_pads_nhwc_out.npy differ diff --git a/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nchw_in.npy b/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nchw_in.npy new file mode 100644 index 000000000..7f7a1f372 Binary files /dev/null and b/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nchw_in.npy differ diff --git a/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nchw_net.pb b/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nchw_net.pb new file mode 100644 index 000000000..533cc309a Binary files /dev/null and b/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nchw_net.pb differ diff --git a/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nchw_out.npy b/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nchw_out.npy new file mode 100644 index 000000000..9b884a818 Binary files /dev/null and b/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nchw_out.npy differ diff --git a/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nhwc_in.npy b/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nhwc_in.npy new file mode 100644 index 000000000..f2b9746e6 Binary files /dev/null and b/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nhwc_in.npy differ diff --git a/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nhwc_net.pb b/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nhwc_net.pb new file mode 100644 index 000000000..f2716d484 Binary files /dev/null and b/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nhwc_net.pb differ diff --git a/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nhwc_out.npy b/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nhwc_out.npy new file mode 100644 index 000000000..24246981d Binary files /dev/null and b/testdata/dnn/tensorflow/conv2d_backprop_input_asymmetric_pads_nhwc_out.npy differ diff --git a/testdata/dnn/tensorflow/generate_tf2_models.py b/testdata/dnn/tensorflow/generate_tf2_models.py index c5e6d2143..0834ec85d 100644 --- a/testdata/dnn/tensorflow/generate_tf2_models.py +++ b/testdata/dnn/tensorflow/generate_tf2_models.py @@ -15,16 +15,16 @@ def gen_data(placeholder): return np.random.standard_normal(shape).astype(placeholder.dtype.as_numpy_dtype()) -def writeBlob(data, name): +def writeBlob(data, name, nchw = False): try: data = data.numpy() except: pass - if data.ndim == 4: + if not nchw and data.ndim == 4: # NHWC->NCHW data = data.transpose(0, 3, 1, 2) - elif data.ndim == 5: + elif not nchw and data.ndim == 5: # NDHWC->NCDHW data = data.transpose(0, 4, 1, 2, 3) @@ -32,7 +32,7 @@ def writeBlob(data, name): np.save(name + '.npy', data) -def save(model, name, **kwargs): +def save(model, name, nchw = False, **kwargs): model.save(name) assert(len(kwargs) == 1) @@ -40,8 +40,8 @@ def save(model, name, **kwargs): inputData = gen_data(next(iter(kwargs.values()))) outputData = model(inputData) - writeBlob(inputData, name + '_in') - writeBlob(outputData, name + '_out') + writeBlob(inputData, name + '_in', nchw) + writeBlob(outputData, name + '_out', nchw) # Freeze model loaded = tf.saved_model.load(name) @@ -105,6 +105,49 @@ def saveBroken(graph, name): graph = getGraph(tf.keras.Model([x_0, x_1], out)) graph.node[2].input.pop() # break the connection in the graph saveBroken(graph, 'broken_layer') +# TF 2.5.0 + python 3.6.13 +tf.keras.backend.set_image_data_format('channels_first') +x = tf.keras.layers.Input(batch_shape = (1, 2, 3, 4), name='x') +kernel = np.random.standard_normal((3, 3, 2, 3)).astype(np.float32) +y = tf.nn.conv2d(x, tf.constant(kernel, dtype=tf.float32), data_format = 'NCHW', padding = [[0, 0], [0, 0], [2, 1], [2, 1]], strides = [1, 1, 3, 2]) +model = tf.keras.Model(x, y) +save(model, 'conv2d_asymmetric_pads_nchw', True, x=tf.TensorSpec(shape=[1, 2, 3, 4], dtype=tf.float32)) +################################################################################ +# TF 2.5.0 + python 3.6.13 +tf.keras.backend.set_image_data_format('channels_last') +x = tf.keras.layers.Input(batch_shape = (1, 3, 4, 2), name='x') +kernel = np.random.standard_normal((3, 3, 2, 3)).astype(np.float32) +y = tf.nn.conv2d(x, tf.constant(kernel, dtype=tf.float32), data_format = 'NHWC', padding = [[0, 0], [2, 1], [2, 1], [0, 0]], strides = [1, 3, 2, 1]) +model = tf.keras.Model(x, y) +save(model, 'conv2d_asymmetric_pads_nhwc', False, x=tf.TensorSpec(shape=[1, 3, 4, 2], dtype=tf.float32)) +################################################################################ +# TF 2.5.0 + python 3.6.13 +tf.keras.backend.set_image_data_format('channels_first') +x = tf.keras.layers.Input(batch_shape = (1, 1, 2, 3), name='x') +y = tf.nn.max_pool(x, ksize=2, data_format = "NCHW", padding = [[0, 0], [0, 0], [1, 0], [1, 1]], strides = [1, 1, 3, 2]) +model = tf.keras.Model(x, y) +save(model, 'max_pool2d_asymmetric_pads_nchw', True, x=tf.TensorSpec(shape=(1, 1, 2, 3), dtype=tf.float32)) +################################################################################ +# TF 2.5.0 + python 3.6.13 +tf.keras.backend.set_image_data_format('channels_last') +x = tf.keras.layers.Input(batch_shape = (1, 2, 3, 1), name='x') +y = tf.nn.max_pool(x, ksize=2, data_format = "NHWC", padding = [[0, 0], [1, 0], [1, 1], [0, 0]], strides = [1, 3, 2, 1]) +model = tf.keras.Model(x, y) +save(model, 'max_pool2d_asymmetric_pads_nhwc', False, x=tf.TensorSpec(shape=(1, 2, 3, 1), dtype=tf.float32)) +################dd################################################################ +tf.keras.backend.set_image_data_format('channels_first') +x = tf.keras.layers.Input(batch_shape = (1, 3, 2, 3), name='x') +kernel = np.random.standard_normal((3, 3, 2, 3)).astype(np.float32) +y = tf.compat.v1.nn.conv2d_backprop_input(input_sizes=tf.constant([1, 2, 3, 4]), filter=kernel, out_backprop=x, data_format = "NCHW", padding = [[0, 0], [0, 0], [2, 1], [2, 1]], strides = [1, 1, 3, 2]) +model = tf.keras.Model(x, y) +save(model, 'conv2d_backprop_input_asymmetric_pads_nchw', True, x=tf.TensorSpec(shape=(1, 3, 2, 3), dtype=tf.float32)) +################################################################################ +tf.keras.backend.set_image_data_format('channels_last') +x = tf.keras.layers.Input(batch_shape = (1, 2, 3, 3), name='x') +kernel = np.random.standard_normal((3, 3, 2, 3)).astype(np.float32) +y = tf.compat.v1.nn.conv2d_backprop_input(input_sizes=tf.constant([1, 3, 4, 2]), filter=kernel, out_backprop=x, data_format = "NHWC", padding = [[0, 0], [2, 1], [2, 1], [0, 0]], strides = [1, 3, 2, 1]) +model = tf.keras.Model(x, y) +save(model, 'conv2d_backprop_input_asymmetric_pads_nhwc', False, x=tf.TensorSpec(shape=(1, 2, 3, 3), dtype=tf.float32)) # Uncomment to print the final graph. # with tf.io.gfile.GFile('tf2_prelu_net.pb', 'rb') as f: diff --git a/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nchw_in.npy b/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nchw_in.npy new file mode 100644 index 000000000..17434123f Binary files /dev/null and b/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nchw_in.npy differ diff --git a/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nchw_net.pb b/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nchw_net.pb new file mode 100644 index 000000000..8160bc4d6 Binary files /dev/null and b/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nchw_net.pb differ diff --git a/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nchw_out.npy b/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nchw_out.npy new file mode 100644 index 000000000..e09869948 Binary files /dev/null and b/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nchw_out.npy differ diff --git a/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nhwc_in.npy b/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nhwc_in.npy new file mode 100644 index 000000000..095c93c7b Binary files /dev/null and b/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nhwc_in.npy differ diff --git a/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nhwc_net.pb b/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nhwc_net.pb new file mode 100644 index 000000000..3d642661e Binary files /dev/null and b/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nhwc_net.pb differ diff --git a/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nhwc_out.npy b/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nhwc_out.npy new file mode 100644 index 000000000..b135a46c3 Binary files /dev/null and b/testdata/dnn/tensorflow/max_pool2d_asymmetric_pads_nhwc_out.npy differ