Tensorflow - 在经过训练的 softmax 分类模型上检测多个对象
Tensorflow - Detecting multiple objects on trained softmax classification model
我使用标准的 SVHN 裁剪数字数据集生成了一个模型,该模型分类为 10 个可能的数字,在测试集上的准确率为 89.89%。继续,我想检测图像上的多个数字。 (例如汽车牌照上的号码)我该怎么做?我是否需要重新训练我的模型来检测多个图像?
#conv1
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x, [-1,32,32,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
#conv2
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
#Densely
W_fc1 = weight_variable([8 * 8 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 8*8*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
#Dropout
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
#Readout
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
#Train
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.global_variables_initializer())
for i in range(40000):
batch = shvn_data.nextbatch(100)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={
x:batch[0], y_: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %f"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
我的代码是从这里修改的:https://www.tensorflow.org/get_started/mnist/pros. My code can be found here: https://github.com/limwenyao/ComputerVision/blob/testing/CNN_MNIST.py#L216
你会在你的网上包裹一个跨步系统。所以你用车牌拍摄你的图像并将其切割成许多较小的图像,然后 运行 在每个较小的图像上进行数字检测并记录找到的数字,最后将它们放在一起并得到你的车牌号.
这种将车牌图像切割成较小图像的过程通常也是经过训练的网络。所以你会有两个网:
- 学会剪好
- 另一个学习从每个切割子图像中读取一个数字
我使用标准的 SVHN 裁剪数字数据集生成了一个模型,该模型分类为 10 个可能的数字,在测试集上的准确率为 89.89%。继续,我想检测图像上的多个数字。 (例如汽车牌照上的号码)我该怎么做?我是否需要重新训练我的模型来检测多个图像?
#conv1
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x, [-1,32,32,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
#conv2
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
#Densely
W_fc1 = weight_variable([8 * 8 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 8*8*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
#Dropout
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
#Readout
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
#Train
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.global_variables_initializer())
for i in range(40000):
batch = shvn_data.nextbatch(100)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={
x:batch[0], y_: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %f"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
我的代码是从这里修改的:https://www.tensorflow.org/get_started/mnist/pros. My code can be found here: https://github.com/limwenyao/ComputerVision/blob/testing/CNN_MNIST.py#L216
你会在你的网上包裹一个跨步系统。所以你用车牌拍摄你的图像并将其切割成许多较小的图像,然后 运行 在每个较小的图像上进行数字检测并记录找到的数字,最后将它们放在一起并得到你的车牌号.
这种将车牌图像切割成较小图像的过程通常也是经过训练的网络。所以你会有两个网:
- 学会剪好
- 另一个学习从每个切割子图像中读取一个数字