Tensorflow不会识别一个热编码标签

时间:2018-04-20 13:33:32

标签: python tensorflow machine-learning neural-network deep-learning

我正在使用10K灰度图像训练我的CNN,这些图像被调整为50x50px,使用一个热编码器进行6个等级。当我训练模型时,我将模型从疯狂的高损失下降到大约190(我可以得到的最低值)和大约16%的可怕准确度。当我使用10个测试图像进​​行预测时,我会收到10个数字的序列,例如通常相同,如[2 2 2 2 2 2 2 2 2]或[5 5 5 5 5 5 5 5 5 5],但情况并非总是如此,有时它可能只是一堆随机数。

这就是我编码的方式:

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)

这是我正在谈论的一个例子。在模型训练完毕后,我会提供10个第一类图像,并且大部分时间都会收到5个图像。模型是否应该返回类似[1,0,0,0,0,0,0,0,0,0]的东西,因为那是什么训练?

enter image description here

如果您想仔细查看代码,请使用jupyter笔记本 https://www.floydhub.com/arse123/projects/cnn-1/8/code/train_edge.ipynb

1 个答案:

答案 0 :(得分:1)

这是因为correct_pred = tf.argmax(pred, 1)而发生的,它给了你在softmax之后概率最高的班级。您可以使用predicted = sess.run(pred, feed_dict={x: test_images[0:10]})。现在,您将获得给定图像的每个类的概率。例如,您的6个类可能得到[.1,.05,。05,.6,.1,.1]。你不会得到[0,0,0,1,0,0]。现在,argmax会给你一个对应于.6的索引。

另外,添加:

pred_ = tf.nn.softmax(pred)

predicted = sess.run(pred_, feed_dict={x: test_images[0:10]})