张量流1.2.1 python3.6
图像大小= 96像素
发生以下错误,无法继续处理 .
train.py
from datetime import datetime
import tensorflow as tf
import numpy as np
import model
import json
import os
import time
NUM_CLASSES = 10
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('datadir', 'data/tfrecords',
tf.app.flags.DEFINE_string('logdir', 'logdir',
"""Directory where to write event logs and checkpoint.""")
tf.app.flags.DEFINE_string('checkpoint_path', './output/model.ckpt',
"""Path to read model checkpoint.""")
tf.app.flags.DEFINE_integer('input_size', 96,
"""Size of input image""")
tf.app.flags.DEFINE_integer('max_steps', 5001,
"""Number of batches to run.""")
tf.app.flags.DEFINE_float('learning_rate', 1e-4, 'Initial learning rate.')
def inputs(batch_size, files, num_examples_per_epoch_for_train=5000):
queues = {}
for i in range(len(files)):
key = i % 10
if key not in queues:
queues[key] = []
queues[key].append(files[i])
def read_files(files):
fqueue = tf.train.string_input_producer(files)
reader = tf.TFRecordReader()
key, value = reader.read(fqueue)
features = tf.parse_single_example(value, features={
'height': tf.FixedLenFeature([], tf.int64),
'width': tf.FixedLenFeature([], tf.int64),
'depth': tf.FixedLenFeature([], tf.int64),
'label': tf.FixedLenFeature([], tf.int64),
'image_raw': tf.FixedLenFeature([], tf.string),
})
height = tf.cast(features['height'], tf.int32)
width = tf.cast(features['width'], tf.int32)
depth = tf.cast(features['depth'], tf.int32)
image_raw = tf.decode_raw(features['image_raw'], tf.uint8)
image = tf.reshape(image_raw, tf.stack([height, width, depth]))
image = tf.cast(image, tf.float32)
# distort
bounding_boxes = tf.div(tf.constant([[[8, 8, 104, 104]]], dtype=tf.float32), 112.0)
begin, size, _ = tf.image.sample_distorted_bounding_box(
tf.shape(image), bounding_boxes,
min_object_covered=(80.0*80.0)/(96.0*96.0),
aspect_ratio_range=[9.0/10.0, 10.0/9.0])
image = tf.slice(image, begin, size)
image = tf.image.resize_images(image, [FLAGS.input_size, FLAGS.input_size])
image = tf.image.random_flip_left_right(image)
image = tf.image.random_brightness(image, max_delta=0.4)
image = tf.image.random_contrast(image, lower=0.6, upper=1.4)
image = tf.image.random_hue(image, max_delta=0.04)
image = tf.image.random_saturation(image, lower=0.6, upper=1.4)
return [tf.image.per_image_standardization(image), features['label']]
min_queue_examples = num_examples_per_epoch_for_train
images, labels = tf.train.shuffle_batch_join(
[read_files(files) for files in queues.values()],
batch_size=batch_size,
capacity=min_queue_examples + 3 * batch_size,
min_after_dequeue=min_queue_examples
)
images = tf.image.resize_images(images, [FLAGS.input_size, FLAGS.input_size])
tf.summary.image('images', images)
return images, labels
def labels_json():
filepath = os.path.join(os.path.join(FLAGS.datadir, 'labels.json'))
with open(filepath, 'r') as f:
return f.read()
def restore_or_initialize(sess):
initialize_variables = []
for v in tf.global_variables():
if v in tf.trainable_variables() or "ExponentialMovingAverage" in v.name:
if tf.train.checkpoint_exists(FLAGS.checkpoint_path):
print('restore variable "%s"' % v.name)
restorer = tf.train.Saver([v])
try:
restorer.restore(sess, FLAGS.checkpoint_path)
continue
except Exception:
print('could not restore, initialize!')
initialize_variables.append(v)
sess.run(tf.variables_initializer(initialize_variables))
def main(argv=None , for_input_graph = False):
keep_prob = tf.placeholder("float")
batch_size = 128
if for_input_graph:
images = tf.placeholder(tf.float32, shape=[128, 96 , 96 , 3], name="x")
labels = tf.placeholder(tf.float32, shape=[128, 10], name="Reshape")
print ("for_input_graph = True")
else:
files = [os.path.join(FLAGS.datadir, f) for f in os.listdir(os.path.join(FLAGS.datadir)) if f.endswith('.tfrecords')]
with tf.variable_scope('inputs'):
images, labels = inputs(batch_size, files)
logits = model.inference(images, NUM_CLASSES )
losses = model.loss(logits, labels)
train_op = model.train(losses)
summary_op = tf.summary.merge_all()
saver = tf.train.Saver(tf.global_variables(), max_to_keep=21, keep_checkpoint_every_n_hours=1)
with tf.Session() as sess:
summary_writer = tf.summary.FileWriter(FLAGS.logdir, graph=sess.graph)
restore_or_initialize(sess)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
if for_input_graph:
tf.train.write_graph(sess.graph.as_graph_def(),"./","input_graph.pb", as_text=False)
print ("input_graph.pb make! OK!")
return
else:
for step in range(FLAGS.max_steps):
start_time = time.time()
_, loss_value = sess.run([train_op, losses])
duration = time.time() - start_time
assert not np.isnan(loss_value), 'Model diverged with loss = NaN'
format_str = '%s: step %d, loss = %.5f (%.3f sec/batch)'
print(format_str % (datetime.now(), step, loss_value, duration))
if step % 100 == 0:
summary_str = sess.run(summary_op)
summary_writer.add_summary(summary_str, step)
if step % 250 == 0 or (step + 1) == FLAGS.max_steps:
checkpoint_path = os.path.join(FLAGS.logdir, 'model.ckpt')
saver.save(sess, checkpoint_path, global_step=step, write_meta_graph=False)
cwd = os.getcwd()
save_path = saver.save(sess, "./output/model.ckpt")
coord.request_stop()
coord.join(threads)
print ("Finish")
if __name__ == '__main__':
main(for_input_graph = True)
main()
model.py import tensorflow as tf from tensorflow.contrib.layers.python.layers.initializers import xavier_initializer
MOVING_AVERAGE_DECAY = 0.9999
def inference(images, num_classes, reuse=False):
def _activation_summary(x):
if not reuse:
tensor_name = x.op.name
tf.summary.scalar(tensor_name + '/sparsity', tf.nn.zero_fraction(x))
inputs = tf.identity(images, name='inputs')
with tf.variable_scope('conv1', reuse=reuse) as scope:
conv1 = tf.layers.conv2d(inputs, 60, [3, 3], padding='SAME', activation=tf.nn.relu, name=scope.name)
_activation_summary(conv1)
pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool1')
with tf.variable_scope('conv2', reuse=reuse) as scope:
conv2 = tf.layers.conv2d(pool1, 90, [3, 3], padding='SAME', activation=tf.nn.relu, name=scope.name)
_activation_summary(conv2)
pool2 = tf.nn.max_pool(conv2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool2')
with tf.variable_scope('conv3', reuse=reuse) as scope:
conv3 = tf.layers.conv2d(pool2, 120, [3, 3], padding='SAME', activation=tf.nn.relu, name=scope.name)
_activation_summary(conv3)
pool3 = tf.nn.max_pool(conv3, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool3')
with tf.variable_scope('conv4', reuse=reuse) as scope:
conv3 = tf.layers.conv2d(pool3, 150, [3, 3], padding='SAME', activation=tf.nn.relu, name=scope.name)
_activation_summary(conv3)
pool4 = tf.nn.max_pool(conv3, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool4')
with tf.variable_scope('fc5', reuse=reuse) as scope:
reshape = tf.reshape(pool4, [images.get_shape()[0].value, -1])
fc5 = tf.layers.dense(reshape, 200, activation=tf.nn.relu, name=scope.name)
_activation_summary(fc5)
with tf.variable_scope('fc6', reuse=reuse) as scope:
fc6 = tf.layers.dense(fc5, 200, activation=tf.nn.relu, name=scope.name)
_activation_summary(fc6)
with tf.variable_scope('fc7', reuse=reuse) as scope:
fc7 = tf.layers.dense(fc6, num_classes, activation=None, name=scope.name)
return fc7
def loss(logits, labels):
# cross entropy
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels)
mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
tf.add_to_collection('losses', mean)
# add weight decay
wd = {
'fc5/fc5': 0.001,
'fc6/fc6': 0.001,
}
for scope, scale in wd.items():
with tf.variable_scope(scope, reuse=True):
v = tf.get_variable('kernel')
weight_decay = tf.multiply(tf.nn.l2_loss(v), scale, name='weight_loss')
tf.add_to_collection('losses', weight_decay)
return tf.add_n(tf.get_collection('losses'), name='total_loss')
def train(total_loss):
loss_averages = tf.train.ExponentialMovingAverage(0.9, name='avg')
losses = tf.get_collection('losses')
loss_averages_op = loss_averages.apply(losses + [total_loss])
for l in losses + [total_loss]:
tf.summary.scalar(l.op.name + ' (raw)', l)
# Apply gradients, and add histograms
with tf.control_dependencies([loss_averages_op]):
opt = tf.train.AdamOptimizer()
grads = opt.compute_gradients(total_loss)
apply_gradient_op = opt.apply_gradients(grads)
for var in tf.trainable_variables():
tf.summary.histogram(var.op.name, var)
for grad, var in grads:
if grad is not None:
tf.summary.histogram(var.op.name + '/gradients', grad)
# Track the moving averages of all trainable variables
variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY)
variables_averages_op = variable_averages.apply(tf.trainable_variables())
with tf.control_dependencies([apply_gradient_op, variables_averages_op]):
train_op = tf.no_op(name='train')
return train_op
tensorflow:ValueError:排名不匹配:标签排名(收到2)应该等于logits排名减去1(收到2) .
我怎样才能避免错误?
File "model.py", line 54, in loss cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels) File "C:\ProgramData\Anaconda3\envs\tensorflow36\lib\site-packages\tensorflow\python\ops\nn_ops.py", line 1686, in sparse_softmax_cross_entropy_with_logits (labels_static_shape.ndims, logits.get_shape().ndims)) ValueError: Rank mismatch: Rank of labels (received 2) should equal rank of logits minus 1 (received 2).
谢谢 .