affinelayer / pix2pix-tensorflow

Tensorflow port of Image-to-Image Translation with Conditional Adversarial Nets https://phillipi.github.io/pix2pix/
MIT License
5.07k stars 1.3k forks source link

Example to run pb file #106

Open groot-1313 opened 6 years ago

groot-1313 commented 6 years ago

I have used the exported model to produce the .pb file for deployment. I am unsure of how to specify the input. Below is the script I am using,

import tensorflow as tf
import argparse 
import numpy as np

def load_graph(frozen_graph_filename):
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

with tf.Graph().as_default() as graph:
    tf.import_graph_def(graph_def, name="prefix")
    return graph

def main():
     parser = argparse.ArgumentParser()
     parser.add_argument("--frozen_model_filename",  
     default="frozen_model.pb", type=str, help="Frozen model file to import")
     args = parser.parse_args()

     graph = load_graph(args.frozen_model_filename) 

     name_img = graph.get_tensor_by_name('prefix/batch:0')
     input_img = graph.get_tensor_by_name('prefix/batch:1')
     target_img = graph.get_tensor_by_name('prefix/batch:2')
     y = graph.get_tensor_by_name('prefix/encode_images/output_pngs/TensorArrayUnstack/TensorArrayScatter/TensorArrayScatterV3:0')

     with tf.Session(graph=graph) as sess:
          y_out = sess.run(y, feed_dict={name_img: "1.png", input_img: np.zeros((512, 512, 3)), target_img: np.zeros((512, 512, 3))})
main()

But I get the following error:

InvalidArgumentError (see above for traceback): The first dimension of paddings must be the rank of inputs[4,2] [512,512,3] [[Node: prefix/generator/encoder_1/conv/Pad = Pad[T=DT_FLOAT, Tpaddings=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_prefix/batch_1_1, prefix/generator/encoder_1/conv/Pad/paddings)]]

  1. How do I go about specifying the inputs?
  2. Is there a different input layer I could use instead of prefix/batch which only requires an input of the test image file?
groot-1313 commented 6 years ago

EDIT: Using the following script.

import tensorflow as tf
import argparse 
import numpy as np
import cv2

def load_graph(frozen_graph_filename):
     with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
         graph_def = tf.GraphDef()
         graph_def.ParseFromString(f.read())

     with tf.Graph().as_default() as graph:
         tf.import_graph_def(graph_def, name="prefix")
     return graph

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--frozen_model_filename", default="Models_pix2pix/exported_files/frozen_model.pb", type=str, help="Frozen model file to import")
    args = parser.parse_args()

    graph = load_graph(args.frozen_model_filename)
    img = cv2.imread("1.png", -1)
    img_in = img[:, :int((img.shape[1])/2), :]
    img_in = cv2.resize(img_in, (512, 512))
    img_in = np.reshape(img_in, (1, 512, 512, 3))

    for op in graph.get_operations():
        print(op.name)

    input_img = graph.get_tensor_by_name('prefix/batch:1')
    y = graph.get_tensor_by_name('prefix/encode_images/output_pngs/while/EncodePng:0')

    with tf.Session(graph=graph) as sess:
        y_out = sess.run(y, feed_dict={ input_img: img_in})

    with open("output.png", "wb") as f:
        f.write(y_out)

main()

The output image in "output.png" is not exactly the output that I expected. I feel there is some kind of processing missing.

Below is the output of print(op.name). If there is a different tensor that will result in the processed, and png encoded image, please point it out because I have tried out different combinations to no avail.

... prefix/convert_inputs/convert_image/Mul/y prefix/convert_inputs/convert_image/Mul prefix/convert_inputs/convert_image/min prefix/convert_inputs/convert_image/Maximum prefix/convert_inputs/convert_image/max prefix/convert_inputs/convert_image/Minimum prefix/convert_inputs/convert_image prefix/convert_outputs/convert_image/Mul/y prefix/convert_outputs/convert_image/Mul prefix/convert_outputs/convert_image/min prefix/convert_outputs/convert_image/Maximum prefix/convert_outputs/convert_image/max prefix/convert_outputs/convert_image/Minimum prefix/convert_outputs/convert_image prefix/encode_images/input_pngs/Shape prefix/encode_images/input_pngs/strided_slice/stack prefix/encode_images/input_pngs/strided_slice/stack_1 prefix/encode_images/input_pngs/strided_slice/stack_2 prefix/encode_images/input_pngs/strided_slice prefix/encode_images/input_pngs/TensorArray prefix/encode_images/input_pngs/TensorArrayUnstack/Shape prefix/encode_images/input_pngs/TensorArrayUnstack/strided_slice/stack prefix/encode_images/input_pngs/TensorArrayUnstack/strided_slice/stack_1 prefix/encode_images/input_pngs/TensorArrayUnstack/strided_slice/stack_2 prefix/encode_images/input_pngs/TensorArrayUnstack/strided_slice prefix/encode_images/input_pngs/TensorArrayUnstack/range/start prefix/encode_images/input_pngs/TensorArrayUnstack/range/delta prefix/encode_images/input_pngs/TensorArrayUnstack/range prefix/encode_images/input_pngs/TensorArrayUnstack/TensorArrayScatter/TensorArrayScatterV3 prefix/encode_images/input_pngs/Const prefix/encode_images/input_pngs/TensorArray_1 prefix/encode_images/input_pngs/while/Enter prefix/encode_images/input_pngs/while/Enter_1 prefix/encode_images/input_pngs/while/Merge prefix/encode_images/input_pngs/while/Merge_1 prefix/encode_images/input_pngs/while/Less/Enter prefix/encode_images/input_pngs/while/Less prefix/encode_images/input_pngs/while/LoopCond prefix/encode_images/input_pngs/while/Switch prefix/encode_images/input_pngs/while/Switch_1 prefix/encode_images/input_pngs/while/Identity prefix/encode_images/input_pngs/while/Identity_1 prefix/encode_images/input_pngs/while/TensorArrayReadV3/Enter prefix/encode_images/input_pngs/while/TensorArrayReadV3/Enter_1 prefix/encode_images/input_pngs/while/TensorArrayReadV3 prefix/encode_images/input_pngs/while/EncodePng prefix/encode_images/input_pngs/while/TensorArrayWrite/TensorArrayWriteV3/Enter prefix/encode_images/input_pngs/while/TensorArrayWrite/TensorArrayWriteV3 prefix/encode_images/input_pngs/while/add/y prefix/encode_images/input_pngs/while/add prefix/encode_images/input_pngs/while/NextIteration prefix/encode_images/input_pngs/while/NextIteration_1 prefix/encode_images/input_pngs/while/Exit_1 prefix/encode_images/input_pngs/TensorArrayStack/TensorArraySizeV3 prefix/encode_images/input_pngs/TensorArrayStack/range/start prefix/encode_images/input_pngs/TensorArrayStack/range/delta prefix/encode_images/input_pngs/TensorArrayStack/range prefix/encode_images/input_pngs/TensorArrayStack/TensorArrayGatherV3 prefix/encode_images/output_pngs/Shape prefix/encode_images/output_pngs/strided_slice/stack prefix/encode_images/output_pngs/strided_slice/stack_1 prefix/encode_images/output_pngs/strided_slice/stack_2 prefix/encode_images/output_pngs/strided_slice prefix/encode_images/output_pngs/TensorArray prefix/encode_images/output_pngs/TensorArrayUnstack/Shape prefix/encode_images/output_pngs/TensorArrayUnstack/strided_slice/stack prefix/encode_images/output_pngs/TensorArrayUnstack/strided_slice/stack_1 prefix/encode_images/output_pngs/TensorArrayUnstack/strided_slice/stack_2 prefix/encode_images/output_pngs/TensorArrayUnstack/strided_slice prefix/encode_images/output_pngs/TensorArrayUnstack/range/start prefix/encode_images/output_pngs/TensorArrayUnstack/range/delta prefix/encode_images/output_pngs/TensorArrayUnstack/range prefix/encode_images/output_pngs/TensorArrayUnstack/TensorArrayScatter/TensorArrayScatterV3 prefix/encode_images/output_pngs/Const prefix/encode_images/output_pngs/TensorArray_1 prefix/encode_images/output_pngs/while/Enter prefix/encode_images/output_pngs/while/Enter_1 prefix/encode_images/output_pngs/while/Merge prefix/encode_images/output_pngs/while/Merge_1 prefix/encode_images/output_pngs/while/Less/Enter prefix/encode_images/output_pngs/while/Less prefix/encode_images/output_pngs/while/LoopCond prefix/encode_images/output_pngs/while/Switch prefix/encode_images/output_pngs/while/Switch_1 prefix/encode_images/output_pngs/while/Identity prefix/encode_images/output_pngs/while/Identity_1 prefix/encode_images/output_pngs/while/TensorArrayReadV3/Enter prefix/encode_images/output_pngs/while/TensorArrayReadV3/Enter_1 prefix/encode_images/output_pngs/while/TensorArrayReadV3 prefix/encode_images/output_pngs/while/EncodePng prefix/encode_images/output_pngs/while/TensorArrayWrite/TensorArrayWriteV3/Enter prefix/encode_images/output_pngs/while/TensorArrayWrite/TensorArrayWriteV3 prefix/encode_images/output_pngs/while/add/y prefix/encode_images/output_pngs/while/add prefix/encode_images/output_pngs/while/NextIteration prefix/encode_images/output_pngs/while/NextIteration_1 prefix/encode_images/output_pngs/while/Exit_1 prefix/encode_images/output_pngs/TensorArrayStack/TensorArraySizeV3 prefix/encode_images/output_pngs/TensorArrayStack/range/start prefix/encode_images/output_pngs/TensorArrayStack/range/delta prefix/encode_images/output_pngs/TensorArrayStack/range prefix/encode_images/output_pngs/TensorArrayStack/TensorArrayGatherV3

Any help will be very much appreciated.