io.mxnet.caffetranslator.misc.CollectStats.java Source code

Java tutorial

Introduction

Here is the source code for io.mxnet.caffetranslator.misc.CollectStats.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://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.
 */

/*!
 * \file CollectStats.java
 * \brief Print all unique layers used in a prototxt along with the parameters used in each layer type.
 */

package io.mxnet.caffetranslator.misc;

import io.mxnet.caffetranslator.CaffePrototxtLexer;
import io.mxnet.caffetranslator.CaffePrototxtParser;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;

import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class CollectStats {

    public static void main(String arsg[]) {
        String filePath = "path";

        CharStream cs = null;
        try {
            FileInputStream fis = new FileInputStream(new File(filePath));
            cs = CharStreams.fromStream(fis, StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
        }

        CaffePrototxtLexer lexer = new CaffePrototxtLexer(cs);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        CaffePrototxtParser parser = new CaffePrototxtParser(tokens);

        StatsListener statsListener = new StatsListener();
        parser.addParseListener(statsListener);
        parser.prototxt();

        Map<String, Set<String>> attrMap = statsListener.getAttrMap();

        Iterator it = attrMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Set<String>> pair = (Map.Entry) it.next();
            System.out.println(pair.getKey() + ":");
            for (String value : pair.getValue()) {
                System.out.println("    " + value);
            }
        }
    }

}