Python Tensorflow不是';t重新编码一个热编码标签
我正在训练我的CNN在10K灰度图像上,这些图像的大小被调整为50x50px,有6个类使用一个热编码器。当我训练模型时,我得到的模型从一个疯狂的高损失降到了大约190(我能得到的最低值),并且有大约16%的可怕的准确率。当我使用10张测试图像进行预测时,我会收到一个由10个数字组成的序列,这些数字通常与[2 2 2 2 2 2 2 2 2]或[5 5 5 5 5 5 5]相同,但情况并非总是如此,有时它可能只是一组随机数 我是这样编码的:Python Tensorflow不是';t重新编码一个热编码标签,python,tensorflow,machine-learning,neural-network,deep-learning,Python,Tensorflow,Machine Learning,Neural Network,Deep Learning,我正在训练我的CNN在10K灰度图像上,这些图像的大小被调整为50x50px,有6个类使用一个热编码器。当我训练模型时,我得到的模型从一个疯狂的高损失降到了大约190(我能得到的最低值),并且有大约16%的可怕的准确率。当我使用10张测试图像进行预测时,我会收到一个由10个数字组成的序列,这些数字通常与[2 2 2 2 2 2 2 2 2]或[5 5 5 5 5 5 5]相同,但情况并非总是如此,有时它可能只是一组随机数 我是这样编码的: def load_data(TRAINING_DIR):
def load_data(TRAINING_DIR):
images = []
labels = []
directories = [d for d in os.listdir(TRAINING_DIR)
if os.path.isdir(os.path.join(TRAINING_DIR, d))]
# Need to sort these because
# floyd hum jumbled up the order
directories = sorted(directories, key=int)
# Traverse through each directory and make a list
# of files names if they end in the PNG format
for d in directories:
label_directory = os.path.join(TRAINING_DIR, d)
file_names = [os.path.join(label_directory, f)
for f in os.listdir(label_directory)
if f.endswith(".png")]
#Traverse through each file, add the image data
# and label to the 2 lists
for f in file_names:
images.append(skimage.data.imread(f))
labels.append(int(d))
return images, labels
images, labels = load_data(TRAINING_DIR)
images = np.array(images, object)
labels = np.array(labels, object)
# Convert labels into a one hot vector
labels = pd.get_dummies(labels)
print('imported...')
然后,当我训练模型时,它看起来实际上是在训练,因为损失在减少。但是,当我对模型进行推断时,预测的标签与热编码标签的格式完全不同?这是我的课程
def train_network(x):
pred = convolutional_network(x)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y, logits = pred))
train_op = tf.train.AdamOptimizer(learning_rate=0.085).minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer()) # Initialize all the variables
saver = tf.train.Saver()
time_full_start = time.clock()
print("RUNNING SESSION...")
for epoch in range(num_epochs):
epoch_loss = 0
time_epoch_start = time.clock()
i = 0
while i < len(images):
start = i
end = i+ batch_size
train_batch_x = images[start:end]
train_batch_y = labels[start:end]
print('Training...')
op , loss_value = sess.run([train_op, loss], feed_dict={x: train_batch_x, y: train_batch_y})
epoch_loss += loss_value
i += batch_size
print('Epoch : ', epoch+1, ' of ', num_epochs, ' - Loss for epoch: ', epoch_loss)
def系列网络(x):
pred=卷积_网络(x)
损失=tf.reduce\u mean(tf.nn.softmax\u cross\u entropy\u与logits(labels=y,logits=pred))
训练op=tf.train.AdamOptimizer(学习率=0.085)。最小化(损失)
使用tf.Session()作为sess:
run(tf.global_variables_initializer())#初始化所有变量
saver=tf.train.saver()
time\u full\u start=time.clock()
打印(“正在运行会话…”)
对于范围内的历元(num_历元):
历元损失=0
time\u epoch\u start=time.clock()
i=0
而我这是因为
correct\u pred=tf.argmax(pred,1)
,它为您提供了仅次于softmax的最高概率的类。您可以使用predicted=sess.run(pred,feed\u dict={x:test\u images[0:10]})
。现在,您将获得给定图像的每个类的概率,例如,您可能会得到6个类的[.1、.05、.05、.6、.1、.1]。您将无法获得[0,0,0,1,0,0]。现在,argmax将为您提供与.6对应的索引
此外,加上:
pred=tf.nn.softmax(pred)
我照你说的做了,但结果甚至不清晰,它们的浮点数太小,无法读取。我该如何着手解决这个问题?(图像中的结果仅针对一个图像)我添加了所需的代码。此外,浮点值非常大。你需要softmax在预测时对它们进行规范化。好吧,我认为我们取得了一些进展,但它每次对相同图像预测不同(且不正确)的一个热值,我需要在训练和重新训练模型时使用softmax函数吗?你已经在这里使用过它:loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=pred))。我不能说你为什么在没有详细说明的情况下得到错误的结果。
predicted = sess.run(pred_, feed_dict={x: test_images[0:10]})