第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

TensorFlow:如何將圖像解碼器節(jié)點(diǎn)添加到我的圖形中?

TensorFlow:如何將圖像解碼器節(jié)點(diǎn)添加到我的圖形中?

守著一只汪 2021-12-17 16:30:28
我有一個(gè)張量流模型作為凍結(jié)圖,它接受一個(gè)圖像張量作為輸入。但是,我想向該圖中添加一個(gè)新的輸入圖像解碼器節(jié)點(diǎn),以便模型也接受 jpg 圖像的編碼字節(jié)字符串,并最終自行解碼圖像。到目前為止,我已經(jīng)嘗試過這種方法:model = './frozen_graph.pb'with tf.gfile.FastGFile(model, 'rb') as f:    # read graph    graph_def = tf.GraphDef()    graph_def.ParseFromString(f.read())    tf.import_graph_def(graph_def, name="")    g = tf.get_default_graph()    # fetch old input    old_input = g.get_tensor_by_name('image_tensor:0')    # define new input    new_input = graph_def.node.add()    new_input.name = 'encoded_image_string_tensor'    new_input.op = 'Substr'    # add new input attr    image = tf.image.decode_image(new_input, channels=3)    # link new input to old input    old_input.input = 'encoded_image_string_tensor'  #  must match with the name above上面的代碼返回這個(gè)異常:Expected string passed to parameter 'input' of op 'Substr', got name: "encoded_image_string_tensor" op: "Substr"  of type 'NodeDef' instead.我不太確定我是否可以tf.image.decode_image在圖表中使用 ,所以也許有另一種方法可以解決這個(gè)問題。有人有提示嗎?
查看完整描述

1 回答

?
守著星空守著你

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊

使用該input_map參數(shù),我成功地將一個(gè)僅解碼 jpg 圖像的新圖形映射到我的原始圖形的輸入(此處:)node.name='image_tensor:0'。只需確保重命名name_scope解碼器圖的 的(此處:)decoder。之后,您可以使用 tensorflow SavedModelBuilder 保存新的連接圖。


這是一個(gè)物體檢測(cè)網(wǎng)絡(luò)的例子:


import tensorflow as tf

from tensorflow.python.saved_model import signature_constants

from tensorflow.python.saved_model import tag_constants



# The export path contains the name and the version of the model

model = 'path/to/model.pb'

export_path = './output/dir/'


sigs = {}


with tf.gfile.FastGFile(model, 'rb') as f:

        with tf.name_scope('decoder'):

                image_str_tensor = tf.placeholder(tf.string, shape=[None], name= 'encoded_image_string_tensor')

                # The CloudML Prediction API always "feeds" the Tensorflow graph with

                # dynamic batch sizes e.g. (?,).  decode_jpeg only processes scalar

                # strings because it cannot guarantee a batch of images would have

                # the same output size.  We use tf.map_fn to give decode_jpeg a scalar

                # string from dynamic batches.

                def decode_and_resize(image_str_tensor):

                        """Decodes jpeg string, resizes it and returns a uint8 tensor."""

                        image = tf.image.decode_jpeg(image_str_tensor, channels=3)


                        # do additional image manipulation here (like resize etc...)


                        image = tf.cast(image, dtype=tf.uint8)

                        return image


                image = tf.map_fn(decode_and_resize, image_str_tensor, back_prop=False, dtype=tf.uint8)


        with tf.name_scope('net'):

                # load .pb file

                graph_def = tf.GraphDef()

                graph_def.ParseFromString(f.read())


                # concatenate decoder graph and original graph

                tf.import_graph_def(graph_def, name="", input_map={'image_tensor:0':image})

                g = tf.get_default_graph()


with tf.Session() as sess:

        # load graph into session and save to new .pb file


        # define model input

        inp = g.get_tensor_by_name('decoder/encoded_image_string_tensor:0')


        # define model outputs

        num_detections = g.get_tensor_by_name('num_detections:0')

        detection_scores = g.get_tensor_by_name('detection_scores:0')

        detection_boxes = g.get_tensor_by_name('detection_boxes:0')

        out = {'num_detections': num_detections, 'detection_scores': detection_scores, 'detection_boxes': detection_boxes}



        builder = tf.saved_model.builder.SavedModelBuilder(export_path)


        tensor_info_inputs = {

                'inputs': tf.saved_model.utils.build_tensor_info(inp)}

        tensor_info_outputs = {}

        for k, v in out.items():

                tensor_info_outputs[k] = tf.saved_model.utils.build_tensor_info(v)


        # assign detection signature for tensorflow serving

        detection_signature = (

        tf.saved_model.signature_def_utils.build_signature_def(

                inputs=tensor_info_inputs,

                outputs=tensor_info_outputs,

                method_name=signature_constants.PREDICT_METHOD_NAME))


        # "build" graph

        builder.add_meta_graph_and_variables(

                sess, [tf.saved_model.tag_constants.SERVING],

                signature_def_map={

                'detection_signature':

                        detection_signature,

                signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:

                        detection_signature,

                },

                main_op=tf.tables_initializer()

        )

        # save graph

        builder.save()

另外:如果您難以找到正確的輸入和輸出節(jié)點(diǎn),您可以運(yùn)行它來顯示圖形:


graph_op = g.get_operations()

for i in graph_op:

    print(i.node_def)


查看完整回答
反對(duì) 回復(fù) 2021-12-17
  • 1 回答
  • 0 關(guān)注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)