代码之家  ›  专栏  ›  技术社区  ›  Alex

为多个型号的TysFraceJava工作指定CPU或GPU

  •  10
  • Alex  · 技术社区  · 8 年前

    .铅 方法。那些 通过保存Keras模型获得文件。

    public class Model implements Closeable {
    
    private String inputName;
    private String outputName;
    private Session session;
    private int inputSize;
    
    public Model(String modelDir, String input_name, String output_name, int inputSize) {
        SavedModelBundle bundle = SavedModelBundle.load(modelDir, "serve");
        this.inputName = input_name;
        this.outputName = output_name;
        this.inputSize = inputSize;
        this.session = bundle.session();
    }
    
    public void close() {
        session.close();
    }
    
    public Tensor predict(Tensor t) {
        return session.runner().feed(inputName, t).fetch(outputName).run().get(0);
    }
    }
    

    对象对应于我的A、B和C模型,并在同一Java程序中对这3种模型进行预测。

    然而,

        public Model(String modelDir, String input_name, String output_name, int inputSize) {
          SavedModelBundle bundle = SavedModelBundle.load(modelDir, "serve");
          this.inputName = input_name;
          this.outputName = output_name;
          this.inputSize = inputSize;
          ConfigProto configProto = ConfigProto.newBuilder().setAllowSoftPlacement(false).setGpuOptions(GPUOptions.newBuilder().setVisibleDeviceList("").build()).build();
          this.session = new Session(bundle.graph(),configProto.toByteArray());
    }
    

    谢谢你的回答。

    2 回复  |  直到 8 年前
        1
  •  1
  •   Kraizee    8 年前

    this issue these instructions

    然后您可以测试:

    ConfigProto configProto = ConfigProto.newBuilder()
                    .setAllowSoftPlacement(true) // allow less GPUs than configured
                    .setGpuOptions(GPUOptions.newBuilder().setPerProcessGpuMemoryFraction(0.01).build())
                    .build();
    SavedModelBundle  bundle = SavedModelBundle.loader(modelDir).withTags("serve").withConfigProto(configProto.toByteArray()).load();
    
        2
  •  1
  •   modesitt    8 年前

    device TensorFlow图的配置。这是一些相关的代码 [source]

    ...
    byte[] config = ConfigProto.newBuilder()
                               .setLogDevicePlacement(true)
                               .setAllowSoftPlacement(true)
                               .build()
                               .toByteArray()
    
    Session sessions[] = new Session[numModels];
    
    // function to move the graph definition to a new device 
    public static byte[] modifyGraphDef(byte[] graphDef, String device) throws Exception {
      GraphDef.Builder builder = GraphDef.parseFrom(graphDef).toBuilder();
      for (int i = 0; i < builder.getNodeCount(); ++i) {
        builder.getNodeBuilder(i).setDevice(device);
      }
      return builder.build().toByteArray();
    } 
    
    graphA.importGraphDef(modifyGraphDef(graphDef, "/gpu:0"));
    graphB.importGraphDef(modifyGraphDef(graphDef, "/cpu:0"));
    

    CUDA_VISIBLE_DEVICES ""