利用tensorflow/slim将训练的cifar10分类网络输出为带权重的模型,报错:
ERROR: Assign requires shapes of both tensors to match. lhs shape= [1001] rhs shape= [10]
solution:
slim默认分类网络为ImageNet,因此类别数为1000+1(背景),如果用cifar10训练,需要将类别数修改为10。
在export_inference_graph.py中添加几行代码,修改模型的类别数目即可。蓝色代码为新添加的代码。
tf.app.flags.DEFINE_integer(
'output_size', 10, 'whether to use cifar10 or not, if use ImageNet, set to 1001.')FLAGS = tf.app.flags.FLAGS
def main(_):
if not FLAGS.output_file:
raise ValueError('You must supply the path to save to with --output_file')
tf.logging.set_verbosity(tf.logging.INFO)
with tf.Graph().as_default() as graph:
dataset = dataset_factory.get_dataset(FLAGS.dataset_name, 'train',
FLAGS.dataset_dir)
dataset.num_classes = FLAGS.output_size
network_fn = nets_factory.get_network_fn(
FLAGS.model_name,
num_classes=(dataset.num_classes - FLAGS.labels_offset),
is_training=FLAGS.is_training)
附录:
从ckpt输出为模型官网链接:
- https://github.com/tensorflow/models/tree/master/research/slim#exporting-the-inference-graph(在models/research/slim文件夹下输出)
- https://github.com/tensorflow/models/tree/master/research/slim#freezing-the-exported-graph (在tensorflow文件夹下bazel)