首页 > 解决方案 > 如何从 Mobilenet v3 的 cpkt、.meta、.index 和 .pb 文件中加载模型?

问题描述

我已经下载了用于 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 模型以及检查点并将其用于我的数据?

标签: tensorflowmobilenet

解决方案


尝试这个

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)

推荐阅读