我有一个用于图像分割的Tensorflow预训练模型,该模型接收6个波段作为输入,我想将模型的输入大小更改为接收4个波段,以便我可以使用自己的数据集进行重新训练,但仍然无法做到这一点,不确定这是否可能?
我尝试通过名称获取输入节点并使用它进行更改,但import_graph_def
没有成功,似乎是在尝试替换时要求尊重尺寸。
graph = tf.get_default_graph()
tf_new_input = tf.placeholder(shape=(4, 256, 256), dtype='float32', name='new_input')
tf.import_graph_def(graph_def, input_map={"ImageInputLayer": tf_new_input})
但是我收到以下错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimensions must be equal, but are 4 and 6 for 'import/ImageInputLayer_Sub' (op: 'Sub') with input shapes: [4,256,256], [6,256,256]
您必须将4通道占位符输入转换为6通道输入,并且输入图像形状应与6通道模型所期望的相同。您可以使用任何操作,但是conv2d
在将其输入到现有模型之前,这是一个易于执行的操作。这就是你的做法。
with tf.Graph().as_default() as old_graph:
# You have to load your 6 channel input graph here
saver.restore(tf.get_default_session(), <<save_path>>)
# Assuming that input node is named as 'input_node' and
# final node is named as 'softmax_node'
with tf.Graph().as_default() as new_graph:
tf_new_input = tf.placeholder(shape=(None, 256, 256, 4), dtype='float32')
# Map 4 channeled input to 6 channel and
# image input shape should be same as expected by old model.
new_node = tf.nn.conv2d(tf_new_input, (3, 3, 4, 6), strides=1, padding='SAME')
# If you want to obtain output node so that you can further perform operations.
softmax_node = tf.import_graph_def(old_graph, input_map={'input_node:0': new_node},
return_elements=['softmax_node:0'])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句