ERROR: Assign requires shapes of both tensors to match. lhs shape= [1001] rhs shape= [10]

利用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輸出爲模型官網鏈接:

  1. https://github.com/tensorflow/models/tree/master/research/slim#exporting-the-inference-graph(在models/research/slim文件夾下輸出)
  2. https://github.com/tensorflow/models/tree/master/research/slim#freezing-the-exported-graph (在tensorflow文件夾下bazel)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章