利用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)