Skip to main content
 首页 » 编程设计

python之Tensorflow 没有重新识别一个热编码标签

2025年01月19日26bonelee

我在 10K 灰度图像上训练我的 CNN,这些图像被调整为 50x50px,使用一个热编码器有 6 个类。当我训练模型时,我得到的模型从异常高的损失下降到大约 190(我能得到的最低)和大约 16% 的可怕精度。当我使用 10 个测试图像进​​行预测时,我会收到 10 个数字的序列,这些数字通常与 [2 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...') 

然后,当我训练模型时,它似乎实际上在训练,因为损失正在减少。但是当我对模型进行推理时,预测的标签与热编码的格式完全不同吗?这是我的 session 。

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] 的东西,因为那是它训练的对象吗?

如果你想仔细查看代码,这里是 jupyter notebook https://www.floydhub.com/arse123/projects/cnn-1/8/code/train_edge.ipynb

请您参考如下方法:

它的发生是因为 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]})