[英]I have no idea what's wrong with my TensorFlow program
如果有人能給我指點我需要解決的問題,我將不勝感激。 我是 TensorFlow 的新手,我不知道出了什么問題。
編碼:
from PIL import ImageFont, Image, ImageDraw
import tensorflow as tf
import random
import numpy as np
x = tf.placeholder(tf.float32, [None,48,48,1])
y = tf.placeholder(tf.float32, [None,26])
def initW(shape):
return tf.Variable(tf.random_normal(shape, stddev=0.01))
w1 = initW([5,5,1,32])
w2 = initW([5,5,32,64])
w3 = initW([64*12*12,26])
keep_prob = tf.placeholder(tf.float32)
saver = tf.train.Saver()
def model(x,w1,w2,w3,keep_prob):
l1c = tf.nn.relu(tf.nn.conv2d(x, w1, strides = [1,1,1,1], padding = "SAME"))
l1p = tf.nn.max_pool(l1c, ksize = [1,2,2,1], strides = [1,2,2,1], padding = "SAME")
l2c = tf.nn.relu(tf.nn.conv2d(l1p, w2, strides = [1,1,1,1], padding = "SAME"))
l2p = tf.nn.max_pool(l2c, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
l2r = tf.reshape(l2p,[-1,64*12*12])
l2d = tf.nn.dropout(l2r, keep_prob)
l3f = tf.matmul(l2d,w3)
return l3f
model = model(x,w1,w2,w3,keep_prob)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y, logits = model))
op = tf.train.AdamOptimizer(0.000002).minimize(cost)
def imgData(char):
img = Image.new("L", (48, 48), color=255)
f = ImageFont.truetype("Arial.ttf", 50)
ImageDraw.Draw(img).text((25 - f.getsize("W")[0] / 2, 0), "W", font=f)
return np.reshape(img.getdata(),[48,48,1])
def oneHot(C,reshape=False):
arr = [0] * 26
arr[charToInt(C)] = 1
if reshape:
return np.reshape(arr,[1,26])
else:
return arr
def charToInt(C):
return ord(C) - ord("A")
def accuracy():
alphabet = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
testSet = np.array([imgData(C) for C in alphabet])
testLabels = np.array([oneHot(C) for C in alphabet])
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(model,1),tf.argmax(y,1)),tf.float32))
print (sess.run(accuracy,feed_dict={x:testSet,y:testLabels,keep_prob:1.0}))
def predict(char):
probabilities = sess.run(model, feed_dict= {x:[imgData(char)], keep_prob: 0.5})[0]
for n in range(26):
print (chr(ord("A") + n) + ": " + "{0:.2f}".format(probabilities[n]))
with tf.Session() as sess:
tf.global_variables_initializer().run()
alphabet = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
random.shuffle(alphabet)
train = True
if train:
for n in range(20):
for C in alphabet:
sess.run(op, feed_dict = {
x: [imgData(C)],
y: oneHot(C,reshape=True),
keep_prob: 0.5
})
print("cost: " + str(sess.run(cost,feed_dict={
x:[imgData(C)],
y:oneHot(C,reshape=True),
keep_prob: 1.0
})))
saver.save(sess,"./model")
else:
saver.restore(sess,"./model")
accuracy()
# predict("B")
這是一個簡單的卷積神經網絡程序,它獲取大寫字母的圖像並輸出預測的字符。 x
是imgData(char)
生成的 48x48 圖像的輸入張量, y
是表示大寫字符的輸出。
cnn 模型位於model(x,w1,w2,w3,keep_prob)
。 盡管嘗試了各種學習率和時期的數字,成本總是收斂到 3.3 左右。
accuracy()
使用測試集打印出模型的測量精度。 它總是說 0.0384615。 我希望predict()
顯示給定圖像成為各種字符的概率,但現在它給出了各種奇怪的數字。
非常感謝。
您的imgData
方法有一個錯字:如果我理解正確,您應該寫出char
而不是W
(請參閱ImageDraw.draw.text 的文檔)。 否則,您的金色標簽將始終是W
的字符。 這就是為什么您總是得到 0.0384 的准確度,即 1/26,即您只學會識別 26 個字符中的一個(這是預期的,因為您從未為W
以外的其他字符提供標簽)。
def imgData(char):
img = Image.new("L", (48, 48), color=255)
f = ImageFont.truetype("/tmp/arial.ttf", 50)
ImageDraw.Draw(img).text((25 - f.getsize("W")[0] / 2, 0), char, font=f)
return np.reshape(img.getdata(),[48,48,1])
我還將您的學習率提高了 10 倍。 然后模型在大約 10 次迭代中過度擬合:
0, cost: 4.96586 accu 0.0769231
1, cost: 5.01186 accu 0.115385
2, cost: 3.9491 accu 0.230769
3, cost: 2.56 accu 0.384615
4, cost: 1.99173 accu 0.423077
5, cost: 2.51248 accu 0.576923
6, cost: 2.19388 accu 0.730769
7, cost: 3.79979 accu 0.769231
8, cost: 2.25762 accu 0.807692
9, cost: 1.27227 accu 0.884615
10, cost: 0.964877 accu 0.884615
11, cost: 2.15709 accu 0.923077
12, cost: 0.482007 accu 0.961538
13, cost: 0.733133 accu 1.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.