如何从Mobilenet v3的cpkt,.meta,.index和.pb文件加载模型?

时间:2020-02-18 14:22:26

标签: tensorflow mobilenet

我已经下载了检查点以及Mobilenet v3的模型。提取rar文件后,我得到了两个文件夹和另外两个文件。目录如下所示

Main Folder
     ema (folder)
        checkpoint
        model-x.data-00000-of-00001
        model-x.index
        model-x.meta
     pristine (folder)
        model.ckpt-y.data-00000-of-00001
        model.ckpt-y.index
        model.ckpt-y.meta
     .pb 
     .tflite

我尝试了很多代码,以下是其中的几个。

import tensorflow as tf
from tensorflow.python.platform import gfile
model_path = "./weights/v3-large-minimalistic_224_1.0_uint8/model.ckpt-3868848"
detection_graph = tf.Graph()
with tf.Session(graph=detection_graph) as sess:
    # Load the graph with the trained states
    loader = tf.train.import_meta_graph(model_path+'.meta')
    loader.restore(sess, model_path)

上面的代码导致以下错误

Node {{node batch_processing/distort_image/switch_case/indexed_case}} of type Case has '_lower_using_switch_merge' attr set but it does not support lowering.

我尝试了以下代码:

import tensorflow as tf
import sys
sys.path.insert(0, 'models/research/slim')
from nets.mobilenet import mobilenet_v3

tf.reset_default_graph() 

file_input = tf.placeholder(tf.string, ()) 

image = tf.image.decode_jpeg(tf.read_file('test.jpg')) 

images = tf.expand_dims(image, 0) 
images = tf.cast(images, tf.float32) / 128.  - 1 
images.set_shape((None, None, None, 3)) 
images = tf.image.resize_images(images, (224, 224)) 

model = mobilenet_v3.wrapped_partial(mobilenet_v3.mobilenet,
                           new_defaults={'scope': 'MobilenetEdgeTPU'},
                           conv_defs=mobilenet_v3.V3_LARGE_MINIMALISTIC,
                           depth_multiplier=1.0)
with tf.contrib.slim.arg_scope(mobilenet_v3.training_scope(is_training=False)): 
    logits, endpoints = model(images)

ema = tf.train.ExponentialMovingAverage(0.999)
vars = ema.variables_to_restore()
print(vars)

with tf.Session() as sess:
    tf.train.Saver(vars).restore(sess,  './weights/v3-large-minimalistic_224_1.0_uint8/saved_model.pb') 
    tf.train.Saver().save(sess, './weights/v3-large-minimalistic_224_1.0_uint8/pristine/model.ckpt')

上面的代码生成以下错误:

Unable to open table file ./weights/v3-large-minimalistic_224_1.0_uint8/saved_model.pb: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?
     [[node save/RestoreV2 (defined at <ipython-input-11-1531bbfd84bb>:29) ]]

如何将Mobilenet v3模型与检查点一起加载并将其用于我的数据?

1 个答案:

答案 0 :(得分:0)

尝试

with tf.contrib.slim.arg_scope(mobilenet_v3.training_scope(is_training=False)): 
    logits, endpoints = mobilenet_v3.large_minimalistic(images)

代替

model = mobilenet_v3.wrapped_partial(mobilenet_v3.mobilenet,
                       new_defaults={'scope': 'MobilenetEdgeTPU'},
                       conv_defs=mobilenet_v3.V3_LARGE_MINIMALISTIC,
                       depth_multiplier=1.0)
with tf.contrib.slim.arg_scope(mobilenet_v3.training_scope(is_training=False)): 
    logits, endpoints = model(images)