Ejemplo n.º 1
0
    def write(self):
        if( self.inputName is None or self.outputNames is None ):
            raise ValueError("inputName or outputName not set")

        graph = self.loadGraph()
        image = self.getImage(False)

        if(image is None):
            raise ValueError("Null image")

        print("Input name: ", self.inputName)
        print("Output names: ", self.outputNames)
        print("Input shape: ", image.shape)
        with tf.Session(graph=graph) as sess:
            outputs = sess.run(
                # self.outputName,
                self.outputNames,
                feed_dict={self.inputName: [image]})
            print(outputs)

        print("Outputs: ",outputs)

        toSave = {}
        toSave_dtype_dict = {}
        for i in range(len(outputs)):
            toSave[self.outputNames[i]] = outputs[i]
            print("Output: ", self.outputNames[i])
            print("Dtype: ", outputs[i].dtype)
            toSave_dtype_dict[self.outputNames[i]] = str(outputs[i].dtype)

        #print("Values to save: ", toSave)
        tfp = TensorFlowPersistor(base_dir=self.baseDir, save_dir=self.name, verbose=False)
        tfp._save_input(self.getImage(True), self.inputName)
        dtype_dict = {}
        dtype_dict[self.inputName] = str(image.dtype)
        tfp._save_node_dtypes(dtype_dict)
        # tfp._save_predictions({self.outputName:outputs})
        tfp._save_predictions(toSave)
        tfp._save_node_dtypes(toSave_dtype_dict)
    in_arg = args.in_name
    out_arg = args.out_name

    shape = parse_shape(str_shape)
    save_dir = get_save_dir(model_file)

    model = load_model(model_file)
    layers = model.layers
    first = layers[0]
    last = layers[-1]

    graph = K.get_session().graph

    if args.verbose:
        print_nodes()

    in_name = '{}{}'.format(first.name, in_arg)
    in_node = graph.get_tensor_by_name(in_name + ':0')
    out_name = '{}{}'.format(last.name, out_arg)
    out_node = graph.get_tensor_by_name(out_name + ':0')

    with tf.Session(graph=graph) as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)
        inValue = np.random.rand(*shape)
        out = sess.run(out_node, feed_dict={in_node: inValue})

        tfp = TensorFlowPersistor(base_dir=base_dir, save_dir=save_dir)
        tfp._save_input(inValue, in_name)
        tfp._save_predictions({out_name: out})
    INPUT_SIZE = 513
    INPUT_TENSOR_NAME = 'graph/ImageTensor:0'
    OUTPUT_TENSOR_NAME = 'graph/SemanticPredictions:0'

    width, height = image.size
    resize_ratio = 1.0 * INPUT_SIZE / max(width, height)
    target_size = (int(resize_ratio * width), int(resize_ratio * height))
    resized_image = image.convert('RGB').resize(target_size, Image.ANTIALIAS)
    input = np.asarray(resized_image)
    with tf.Session(graph=graph) as sess:
        batch_seg_map = sess.run(
            OUTPUT_TENSOR_NAME,
            feed_dict={INPUT_TENSOR_NAME: [input]})
        seg_map = batch_seg_map[0]
        tfp = TensorFlowPersistor(base_dir=base_dir, save_dir="deeplab_mobilenetv2_dm05_coco_voc_trainaug")
        input4d = np.reshape(input, [1, input.shape[0], input.shape[1], input.shape[2]])
        tfp._save_input(input4d, "graph/ImageTensor")   #TF is weird here: placeholder is [1, -1, -1, 3] but it adds extra dimension if you pass 4d in :/
        tfp._save_predictions({"graph/SemanticPredictions":seg_map})

        #Save type info
        dtype_dict = {}
        dtype_dict[INPUT_TENSOR_NAME] = str(input.dtype)
        dtype_dict["graph/SemanticPredictions"] = str(seg_map.dtype)
        tfp._save_node_dtypes(dtype_dict)


    print(seg_map)