From fe516ae6cff74b25b088705019dfbcd0b10956e8 Mon Sep 17 00:00:00 2001 From: Alex Black Date: Mon, 20 Apr 2020 22:33:11 +1000 Subject: [PATCH] nd4j-jackson dependency fix (#398) * Remove old nd4j-jackson dependencies Signed-off-by: Alex Black * Fix use of old/deprecated JSON serializer Signed-off-by: Alex Black * Fix deserialization Signed-off-by: Alex Black * Delete test using deleted ser/de classes Signed-off-by: Alex Black * Delete another copy of old test Signed-off-by: Alex Black --- .../datavec-spark-inference-client/pom.xml | 5 -- .../pom.xml | 5 -- deeplearning4j/deeplearning4j-nn/pom.xml | 6 -- .../objdetect/BoundingBoxesDeserializer.java | 65 +++++++++++++++++ .../layers/objdetect/Yolo2OutputLayer.java | 7 +- .../deeplearning4j-ui/pom.xml | 5 -- .../shaded/NDArrayTextDeSerializer.java | 4 ++ .../nd4j-backend-impls/nd4j-cuda/pom.xml | 6 -- nd4j/nd4j-backends/nd4j-tests/pom.xml | 6 -- .../serde/jackson/NdArraySerializerTest.java | 72 ------------------- .../serde/jackson/VectorSerializeTest.java | 72 ------------------- nd4j/nd4j-uberjar/pom.xml | 5 -- 12 files changed, 72 insertions(+), 186 deletions(-) create mode 100644 deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/objdetect/BoundingBoxesDeserializer.java delete mode 100644 nd4j/nd4j-backends/nd4j-tests/src/test/java/org/nd4j/serde/jackson/NdArraySerializerTest.java delete mode 100644 nd4j/nd4j-backends/nd4j-tests/src/test/java/org/nd4j/serde/jackson/VectorSerializeTest.java diff --git a/datavec/datavec-spark-inference-parent/datavec-spark-inference-client/pom.xml b/datavec/datavec-spark-inference-parent/datavec-spark-inference-client/pom.xml index 57e50d127..3b564b1b3 100644 --- a/datavec/datavec-spark-inference-parent/datavec-spark-inference-client/pom.xml +++ b/datavec/datavec-spark-inference-parent/datavec-spark-inference-client/pom.xml @@ -30,11 +30,6 @@ - - org.nd4j - nd4j-jackson - ${nd4j.version} - org.datavec datavec-spark-inference-server_2.11 diff --git a/deeplearning4j/deeplearning4j-nearestneighbors-parent/deeplearning4j-nearestneighbors-client/pom.xml b/deeplearning4j/deeplearning4j-nearestneighbors-parent/deeplearning4j-nearestneighbors-client/pom.xml index e3ca20366..0886e8d5b 100644 --- a/deeplearning4j/deeplearning4j-nearestneighbors-parent/deeplearning4j-nearestneighbors-client/pom.xml +++ b/deeplearning4j/deeplearning4j-nearestneighbors-parent/deeplearning4j-nearestneighbors-client/pom.xml @@ -41,11 +41,6 @@ deeplearning4j-nearestneighbors-model ${project.version} - - org.nd4j - nd4j-jackson - ${nd4j.version} - diff --git a/deeplearning4j/deeplearning4j-nn/pom.xml b/deeplearning4j/deeplearning4j-nn/pom.xml index 77acb2dc7..b817c0dc6 100644 --- a/deeplearning4j/deeplearning4j-nn/pom.xml +++ b/deeplearning4j/deeplearning4j-nn/pom.xml @@ -89,12 +89,6 @@ ${nd4j.version} - - org.nd4j - nd4j-jackson - ${nd4j.version} - - com.github.oshi diff --git a/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/objdetect/BoundingBoxesDeserializer.java b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/objdetect/BoundingBoxesDeserializer.java new file mode 100644 index 000000000..dec2c6c33 --- /dev/null +++ b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/objdetect/BoundingBoxesDeserializer.java @@ -0,0 +1,65 @@ +/* ****************************************************************************** + * Copyright (c) 2020 Konduit K.K. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ******************************************************************************/ +package org.deeplearning4j.nn.conf.layers.objdetect; + +import org.nd4j.linalg.api.buffer.DataBuffer; +import org.nd4j.linalg.api.ndarray.INDArray; +import org.nd4j.linalg.factory.Nd4j; +import org.nd4j.serde.jackson.shaded.NDArrayTextDeSerializer; +import org.nd4j.shade.jackson.core.JsonParser; +import org.nd4j.shade.jackson.core.JsonProcessingException; +import org.nd4j.shade.jackson.databind.DeserializationContext; +import org.nd4j.shade.jackson.databind.JsonDeserializer; +import org.nd4j.shade.jackson.databind.JsonNode; + +import java.io.IOException; + +/** + * Custom deserializer to handle change in format between beta6 (and earlier) and later versions + * + * @author Alex Black + */ +public class BoundingBoxesDeserializer extends JsonDeserializer { + @Override + public INDArray deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { + JsonNode node = jp.getCodec().readTree(jp); + if(node.has("dataBuffer")){ + //Must be legacy format serialization + JsonNode arr = node.get("dataBuffer"); + int rank = node.get("rankField").asInt(); + int numElements = node.get("numElements").asInt(); + int offset = node.get("offsetField").asInt(); + JsonNode shape = node.get("shapeField"); + JsonNode stride = node.get("strideField"); + int[] shapeArr = new int[rank]; + int[] strideArr = new int[rank]; + DataBuffer buff = Nd4j.createBuffer(numElements); + for (int i = 0; i < numElements; i++) { + buff.put(i, arr.get(i).asDouble()); + } + + String ordering = node.get("orderingField").asText(); + for (int i = 0; i < rank; i++) { + shapeArr[i] = shape.get(i).asInt(); + strideArr[i] = stride.get(i).asInt(); + } + + return Nd4j.create(buff, shapeArr, strideArr, offset, ordering.charAt(0)); + } + //Standard/new format + return new NDArrayTextDeSerializer().deserialize(node); + } +} diff --git a/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/objdetect/Yolo2OutputLayer.java b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/objdetect/Yolo2OutputLayer.java index 32f5627d5..24bda07f6 100644 --- a/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/objdetect/Yolo2OutputLayer.java +++ b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/conf/layers/objdetect/Yolo2OutputLayer.java @@ -34,10 +34,9 @@ import org.nd4j.linalg.api.ndarray.INDArray; import org.nd4j.linalg.learning.regularization.Regularization; import org.nd4j.linalg.lossfunctions.ILossFunction; import org.nd4j.linalg.lossfunctions.impl.LossL2; +import org.nd4j.serde.jackson.shaded.NDArrayTextSerializer; import org.nd4j.shade.jackson.databind.annotation.JsonDeserialize; import org.nd4j.shade.jackson.databind.annotation.JsonSerialize; -import org.nd4j.shade.serde.jackson.VectorDeSerializer; -import org.nd4j.shade.serde.jackson.VectorSerializer; import java.util.Arrays; import java.util.Collection; @@ -77,8 +76,8 @@ public class Yolo2OutputLayer extends org.deeplearning4j.nn.conf.layers.Layer { private double lambdaNoObj; private ILossFunction lossPositionScale; private ILossFunction lossClassPredictions; - @JsonSerialize(using = VectorSerializer.class) - @JsonDeserialize(using = VectorDeSerializer.class) + @JsonSerialize(using = NDArrayTextSerializer.class) + @JsonDeserialize(using = BoundingBoxesDeserializer.class) private INDArray boundingBoxes; private Yolo2OutputLayer() { diff --git a/deeplearning4j/deeplearning4j-ui-parent/deeplearning4j-ui/pom.xml b/deeplearning4j/deeplearning4j-ui-parent/deeplearning4j-ui/pom.xml index 282867e7e..44b868ae6 100644 --- a/deeplearning4j/deeplearning4j-ui-parent/deeplearning4j-ui/pom.xml +++ b/deeplearning4j/deeplearning4j-ui-parent/deeplearning4j-ui/pom.xml @@ -44,11 +44,6 @@ deeplearning4j-nlp ${project.version} - - org.nd4j - nd4j-jackson - ${nd4j.version} - junit diff --git a/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/serde/jackson/shaded/NDArrayTextDeSerializer.java b/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/serde/jackson/shaded/NDArrayTextDeSerializer.java index d50143e77..11761a00d 100644 --- a/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/serde/jackson/shaded/NDArrayTextDeSerializer.java +++ b/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/serde/jackson/shaded/NDArrayTextDeSerializer.java @@ -36,6 +36,10 @@ public class NDArrayTextDeSerializer extends JsonDeserializer { @Override public INDArray deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException { JsonNode n = jp.getCodec().readTree(jp); + return deserialize(n); + } + + public INDArray deserialize(JsonNode n){ //First: check for backward compatilibity (RowVectorSerializer/Deserializer) if(!n.has("dataType")){ diff --git a/nd4j/nd4j-backends/nd4j-backend-impls/nd4j-cuda/pom.xml b/nd4j/nd4j-backends/nd4j-backend-impls/nd4j-cuda/pom.xml index 371386898..c181d4328 100644 --- a/nd4j/nd4j-backends/nd4j-backend-impls/nd4j-cuda/pom.xml +++ b/nd4j/nd4j-backends/nd4j-backend-impls/nd4j-cuda/pom.xml @@ -274,12 +274,6 @@ junit test - - org.nd4j - nd4j-jackson - ${project.version} - test - org.nd4j nd4j-api diff --git a/nd4j/nd4j-backends/nd4j-tests/pom.xml b/nd4j/nd4j-backends/nd4j-tests/pom.xml index 8bfe85ea6..c621330d4 100644 --- a/nd4j/nd4j-backends/nd4j-tests/pom.xml +++ b/nd4j/nd4j-backends/nd4j-tests/pom.xml @@ -76,12 +76,6 @@ ${project.version} - - org.nd4j - nd4j-jackson - ${project.version} - - org.nd4j diff --git a/nd4j/nd4j-backends/nd4j-tests/src/test/java/org/nd4j/serde/jackson/NdArraySerializerTest.java b/nd4j/nd4j-backends/nd4j-tests/src/test/java/org/nd4j/serde/jackson/NdArraySerializerTest.java deleted file mode 100644 index 910f535d0..000000000 --- a/nd4j/nd4j-backends/nd4j-tests/src/test/java/org/nd4j/serde/jackson/NdArraySerializerTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2015-2018 Skymind, Inc. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - ******************************************************************************/ - -package org.nd4j.serde.jackson; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.nd4j.linalg.BaseNd4jTest; -import org.nd4j.linalg.api.ndarray.INDArray; -import org.nd4j.linalg.factory.Nd4j; -import org.nd4j.linalg.factory.Nd4jBackend; -import org.nd4j.shade.jackson.databind.ObjectMapper; -import org.nd4j.shade.jackson.databind.module.SimpleModule; -import org.nd4j.shade.serde.jackson.shaded.NDArrayDeSerializer; -import org.nd4j.shade.serde.jackson.shaded.NDArraySerializer; - -import static org.junit.Assert.assertEquals; - -/** - * Created by agibsonccc on 6/23/16. - */ -public class NdArraySerializerTest extends BaseNd4jTest { - private static ObjectMapper objectMapper; - - public NdArraySerializerTest(Nd4jBackend backend) { - super(backend); - } - - @Override - public char ordering() { - return 'c'; - } - - - @BeforeClass - public static void beforeClass() { - objectMapper = objectMapper(); - - } - - - @Test - public void testSerde() throws Exception { - String json = objectMapper.writeValueAsString(Nd4j.create(2, 2)); - INDArray assertion = Nd4j.create(2, 2); - INDArray test = objectMapper.readValue(json, INDArray.class); - assertEquals(assertion, test); - } - - private static ObjectMapper objectMapper() { - ObjectMapper mapper = new ObjectMapper(); - SimpleModule nd4j = new SimpleModule("nd4j"); - nd4j.addDeserializer(INDArray.class, new NDArrayDeSerializer()); - nd4j.addSerializer(INDArray.class, new NDArraySerializer()); - mapper.registerModule(nd4j); - return mapper; - - } -} diff --git a/nd4j/nd4j-backends/nd4j-tests/src/test/java/org/nd4j/serde/jackson/VectorSerializeTest.java b/nd4j/nd4j-backends/nd4j-tests/src/test/java/org/nd4j/serde/jackson/VectorSerializeTest.java deleted file mode 100644 index f046cb97f..000000000 --- a/nd4j/nd4j-backends/nd4j-tests/src/test/java/org/nd4j/serde/jackson/VectorSerializeTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2015-2018 Skymind, Inc. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - ******************************************************************************/ - -package org.nd4j.serde.jackson; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.nd4j.linalg.BaseNd4jTest; -import org.nd4j.linalg.api.ndarray.INDArray; -import org.nd4j.linalg.factory.Nd4j; -import org.nd4j.linalg.factory.Nd4jBackend; -import org.nd4j.shade.jackson.databind.ObjectMapper; -import org.nd4j.shade.jackson.databind.module.SimpleModule; -import org.nd4j.shade.serde.jackson.VectorDeSerializer; -import org.nd4j.shade.serde.jackson.VectorSerializer; - -import static org.junit.Assert.assertEquals; - -/** - * Created by agibsonccc on 6/23/16. - */ -public class VectorSerializeTest extends BaseNd4jTest { - private static ObjectMapper objectMapper; - - public VectorSerializeTest(Nd4jBackend backend) { - super(backend); - } - - @Override - public char ordering() { - return 'c'; - } - - @BeforeClass - public static void beforeClass() { - objectMapper = objectMapper(); - } - - - - @Test - public void testSerde() throws Exception { - String json = objectMapper.writeValueAsString(Nd4j.create(2, 2)); - INDArray assertion = Nd4j.create(2, 2); - INDArray test = objectMapper.readValue(json, INDArray.class); - assertEquals(assertion, test); - } - - - private static ObjectMapper objectMapper() { - ObjectMapper mapper = new ObjectMapper(); - SimpleModule nd4j = new SimpleModule("nd4j"); - nd4j.addDeserializer(INDArray.class, new VectorDeSerializer()); - nd4j.addSerializer(INDArray.class, new VectorSerializer()); - mapper.registerModule(nd4j); - return mapper; - - } -} diff --git a/nd4j/nd4j-uberjar/pom.xml b/nd4j/nd4j-uberjar/pom.xml index 84f1c0d4a..bb0d45b5e 100644 --- a/nd4j/nd4j-uberjar/pom.xml +++ b/nd4j/nd4j-uberjar/pom.xml @@ -180,11 +180,6 @@ nd4j-aeron ${project.version} - - org.nd4j - nd4j-jackson - ${project.version} - org.nd4j nd4j-kryo_2.11