Example #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)
    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)