77 lines
2.9 KiB
Java
77 lines
2.9 KiB
Java
/*
|
|
* ******************************************************************************
|
|
* *
|
|
* *
|
|
* * 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.
|
|
* *
|
|
* * See the NOTICE file distributed with this work for additional
|
|
* * information regarding copyright ownership.
|
|
* * 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.evaluation.serde;
|
|
|
|
import org.nd4j.evaluation.classification.ConfusionMatrix;
|
|
import com.fasterxml.jackson.core.JsonParser;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
public class ConfusionMatrixDeserializer extends JsonDeserializer<ConfusionMatrix<Integer>> {
|
|
@Override
|
|
public ConfusionMatrix<Integer> deserialize(JsonParser jp, DeserializationContext ctxt)
|
|
throws IOException {
|
|
JsonNode n = jp.getCodec().readTree(jp);
|
|
|
|
//Get class names/labels
|
|
ArrayNode classesNode = (ArrayNode) n.get("classes");
|
|
List<Integer> classes = new ArrayList<>();
|
|
for (JsonNode cn : classesNode) {
|
|
classes.add(cn.asInt());
|
|
}
|
|
|
|
ConfusionMatrix<Integer> cm = new ConfusionMatrix<>(classes);
|
|
|
|
ObjectNode matrix = (ObjectNode) n.get("matrix");
|
|
Iterator<Map.Entry<String, JsonNode>> matrixIter = matrix.fields();
|
|
while (matrixIter.hasNext()) {
|
|
Map.Entry<String, JsonNode> e = matrixIter.next();
|
|
|
|
int actualClass = Integer.parseInt(e.getKey());
|
|
ArrayNode an = (ArrayNode) e.getValue();
|
|
|
|
ArrayNode innerMultiSetKey = (ArrayNode) an.get(0);
|
|
ArrayNode innerMultiSetCount = (ArrayNode) an.get(1);
|
|
|
|
Iterator<JsonNode> iterKey = innerMultiSetKey.iterator();
|
|
Iterator<JsonNode> iterCnt = innerMultiSetCount.iterator();
|
|
while (iterKey.hasNext()) {
|
|
int predictedClass = iterKey.next().asInt();
|
|
int count = iterCnt.next().asInt();
|
|
|
|
cm.add(actualClass, predictedClass, count);
|
|
}
|
|
}
|
|
|
|
return cm;
|
|
}
|
|
}
|