我正在尝试在一组新类上使用预训练的Inception V4模型。我的想法是削减最后几步并重新训练上一层输出的1536值的softmax。
slim = tf.contrib.slim
from inception_v4 import inception_v4 as net
from inception_v4 import inception_v4_arg_scope as net_scope
from tensorflow.python.framework import graph_util
from tensorflow.python.platform import gfile
def main(argv):
with tf.Session() as sess:
with slim.arg_scope(net_scope()):
input_units = tf.placeholder(tf.float32, (None, 299, 299, 3), 'inputs')
net(input_units, num_classes=1001)
last_node = tf.get_default_graph().as_graph_def().node[-1].name
saver = tf.train.Saver()
saver.restore(sess, argv[1])
output_graph_def = graph_util.convert_variables_to_constants(
sess,
tf.get_default_graph().as_graph_def(),
[last_node]
)
with gfile.FastGFile(argv[2], 'w') as f:
f.write(output_graph_def.SerializeToString())
if __name__=='__main__':
main(sys.argv)
这将创建.pb图形文件并冻结预训练值。
之后我重新加载图表进行训练:
def add_final_training_ops(class_count, bottleneck_tensor):
with tf.name_scope('input'):
bottleneck_input = tf.placeholder_with_default(
bottleneck_tensor,
shape=[None, BOTTLENECK_TENSOR_SIZE],
name='BottleneckInputPlaceholder'
)
ground_truth_input = tf.placeholder(tf.float32,
[None, class_count],
name='GroundTruthInput')
layer_name = 'final_training_ops'
with tf.name_scope(layer_name):
with tf.name_scope('weights'):
layer_weights = tf.Variable(tf.truncated_normal([BOTTLENECK_TENSOR_SIZE, class_count], stddev=0.001), name='final_weights')
variable_summaries(layer_weights)
with tf.name_scope('biases'):
layer_biases = tf.Variable(tf.zeros([class_count]), name='final_biases')
variable_summaries(layer_biases)
with tf.name_scope('Wx_plus_b'):
logits = tf.matmul(bottleneck_input, layer_weights) + layer_biases
tf.summary.histogram('pre_activations', logits)
final_tensor = tf.nn.softmax(logits, name='final_result')
tf.summary.histogram('activations', final_tensor)
with tf.name_scope('cross_entropy'):
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
labels=ground_truth_input, logits=logits)
with tf.name_scope('total'):
cross_entropy_mean = tf.reduce_mean(cross_entropy)
tf.summary.scalar('cross_entropy', cross_entropy_mean)
with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(
cross_entropy_mean)
return (train_step, cross_entropy_mean, bottleneck_input,
ground_truth_input,
final_tensor)
代码是自定义的混合,从转移学习中获取InceptionV3教程。
培训将通过,两个相对不同的课程的准确率将是100%。
当我测试结果图时,我的准确度大约为50%,因此几乎是随机的。