我正在尝试实现一个卷积神经网络来识别面部。问题是我想训练10个班级,并且能够在考试时间预测超过10个班级(例如20个班级)。
如何在不影响旧文件识别的测试准确率的情况下做到这一点?因为我的测试精度很低,有时甚至是0。
这是我的代码。
batch_size = 16
patch_size = 5
depth = 16
num_hidden = 128
num_labels = 12
num_channels = 1
def reformat(dataset, labels):
dataset = dataset.reshape(
(-1, IMAGE_SIZE_H, IMAGE_SIZE_W, num_channels)).astype(np.float32)
labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
def accuracy(predictions, labels):
return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))
/ predictions.shape[0])
graph = tf.Graph()
with graph.as_default():
# Input data.
tf_train_dataset = tf.placeholder(
tf.float32, shape=(batch_size, IMAGE_SIZE_H, IMAGE_SIZE_W, num_channels))
print("tf_train_dataset",tf_train_dataset)
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
tf_valid_dataset = tf.constant(valid_dataset)
tf_test_dataset = tf.constant(test_dataset)
layer1_weights = tf.Variable(tf.truncated_normal(
[patch_size, patch_size, num_channels, depth], stddev=0.1))
layer1_biases = tf.Variable(tf.zeros([depth]))
layer2_weights = tf.Variable(tf.truncated_normal(
[patch_size, patch_size, depth, depth], stddev=0.1))
layer2_biases = tf.Variable(tf.constant(1.0, shape=[depth]))
layer3_weights = tf.Variable(tf.truncated_normal(
[IMAGE_SIZE_H // 16 * IMAGE_SIZE_W // 16 * depth, num_hidden], stddev=0.1))
layer3_biases = tf.Variable(tf.constant(1.0, shape=[num_hidden]))
layer4_weights = tf.Variable(tf.truncated_normal(
[num_hidden, num_labels], stddev=0.1))
layer4_biases = tf.Variable(tf.constant(1.0, shape=[num_labels]))
conv_1 = tf.nn.conv2d(data, layer1_weights, [1, 2, 2, 1], padding='SAME')
hidden_1 = tf.nn.relu(conv_1 + layer1_biases)
pool_1 = tf.nn.max_pool(hidden_1,ksize = [1,2,2,1], strides= [1,2,2,1],padding ='SAME' )
conv_2 = tf.nn.conv2d(pool_1, layer2_weights, [1, 2, 2, 1], padding='SAME')
hidden_2 = tf.nn.relu(conv_2 + layer2_biases)
pool_2 = tf.nn.max_pool(hidden_2,ksize = [1,2,2,1], strides= [1,2,2,1],padding ='SAME' )
shape = pool_2.get_shape().as_list()
reshape = tf.reshape(pool_2, [shape[0], shape[1] * shape[2] * shape[3]])
hidden_3 = tf.nn.relu(tf.matmul(reshape, layer3_weights) + layer3_biases)
return tf.matmul(hidden_3, layer4_weights) + layer4_biases
# Training computation.
logits = model(tf_train_dataset)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits,tf_train_labels))
# Optimizer.
optimizer = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
# Predictions for the training, validation, and test data.
train_prediction = tf.nn.softmax(logits)
valid_prediction = tf.nn.softmax(model(tf_valid_dataset))
test_prediction = tf.nn.softmax(model(tf_test_dataset))
num_steps = 201
with tf.Session(graph=graph) as session:
tf.initialize_all_variables().run()
print('Initialized')
for step in range(num_steps):
offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
batch_data = train_dataset[offset:(offset + batch_size), :, :, :]
batch_labels = train_labels[offset:(offset + batch_size), :]
feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}
_, l, predictions = session.run(
[optimizer, loss, train_prediction ], feed_dict=feed_dict)
if (step % 50 == 0):
print('Minibatch loss at step %d: %f' % (step, l))
print('Minibatch accuracy: %.1f%%' % accuracy(predictions, batch_labels)
print('Validation accuracy: %.1f%%' % accuracy(
valid_prediction.eval(), valid_labels))
print('Test accuracy: %.1f%%' % accuracy(test_prediction.eval(), test_labels[:,0:9]))
答案 0 :(得分:1)
不可能在一定数量的类别(10个身份)上训练一个具有交叉熵的模型,并用不同的类别进行测试(例如,10个训练身份和10个新训练身份,总共20个)
无论如何,你需要摆脱最后一个softmax图层(大小为[num_hidden, 10]
)以使用大小为[num_hidden, 20]
的新的未经训练的 softmax图层。
问题是这个新图层将被随机初始化并产生非常糟糕的结果。
在深度学习中处理未知类的一般解决方案是将每个输入数据(面)的非常好的表示构建到特征空间(大小为num_hidden
)中。这种技术称为representation learning。
想象一下,您的模型发送面部有一个很好的特征空间。理论上,身份的所有面都将被发送到一个干净的集群中的同一位置。然后,您就可以运行k-means来获取身份聚类或任何算法,而不是嵌入,其中k
是测试身份的数量(k=20
) 。
有多种方法可以获得良好的嵌入效果。您可以在softmax之前获取最后一个隐藏图层,甚至是已经训练过的模型的最后一个隐藏图层(VGGFace具有非常好的结果且可以免费获得)。
VGGFace论文中另一个有趣的想法是使用三重态损失来微调嵌入。