【问题标题】:Tensorflow Image Classifier Accuracy Fails to ChangeTensorFlow 图像分类器精度无法更改
【发布时间】:2018-10-12 16:30:25
【问题描述】:

我是张量流的新手。我正在为图像分类创建一个简单的全连接神经网络。图片为 (-1, 224, 224, 3),标签为 (-1, 2)。但是,我的代码的结果是准确性根本没有提高;它保持在 47% 并且不会改变——即使改变了学习率、优化器和不同的测试集。任何帮助都将不胜感激!谢谢!

import matplotlib.pyplot as plt 
from util.MacOSFile import MacOSFile
import numpy as np
import _pickle as pickle
import tensorflow as tf

def pickle_load(file_path):
    with open(file_path, "rb") as f:
        return pickle.load(MacOSFile(f))

###hyperparameters###
batch_size = 32
iterations = 10

###loading training data start###
data = pickle_load('training.pickle')
x_train = []
y_train = []

for features, labels in data:
    x_train.append(features)
    y_train.append(labels)

x_train = np.array(x_train)
y_train = np.array(y_train)

###################################

###loading test data start###
data = pickle_load('testing.pickle')
x_test = []
y_test = []

for features, labels in data:
    x_test.append(features)
    y_test.append(labels)

x_test = np.array(x_test)
y_test = np.array(y_test)

###################################


###neural network###

x_s = tf.placeholder(tf.float32, [None, 224, 224, 3])
y_s = tf.placeholder(tf.float32, [None, 2])
x_image = tf.reshape(x_s, [-1, 150528])

W_1 = tf.Variable(tf.truncated_normal([150528, 8224]))
b_1 = tf.Variable(tf.zeros([8224]))
h_fc1 = tf.nn.relu(tf.matmul(x_image, W_1) + b_1)

W_2 = tf.Variable(tf.truncated_normal([8224, 1028]))
b_2 = tf.Variable(tf.zeros([1028]))
h_fc2 = tf.nn.relu(tf.matmul(h_fc1, W_2) + b_2)

W_3 = tf.Variable(tf.truncated_normal([1028, 2]))
b_3 = tf.Variable(tf.zeros([2]))
prediction = tf.nn.softmax(tf.matmul(h_fc2, W_3) + b_3)

cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_s, logits=prediction)
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy)
init = tf.global_variables_initializer()

###neural network end###


with tf.Session() as sess:
    sess.run(init)

    train_sample_size = len(data) #how many data points?
    max_batches_in_data = int(train_sample_size/batch_size) #max number of batches possible; 623 

    for iteration in range(iterations):
            print('Iteration ', iteration)
            epoch = int(iteration/max_batches_in_data)
            start_idx = (iteration-epoch*max_batches_in_data)*batch_size
            end_idx = (iteration+1 - epoch*max_batches_in_data)*batch_size
            mini_x_train = x_train[start_idx: end_idx] 
            mini_y_train = y_train[start_idx: end_idx]

            ##actual training is here
            sess.run(train_step, feed_dict={x_s: mini_x_train, y_s: mini_y_train})

            #test accuracy#
            y_pre = sess.run(prediction, feed_dict={x_s: x_train[:100]})
            correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(y_train[:100], 1))
            accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
            result = sess.run(accuracy, feed_dict={x_s: x_train[:100], y_s: y_train[:100]})
            print("Result: {0}".format(result))

【问题讨论】:

  • 你的学习率设置为零,这实际上意味着训练根本不会做任何事情。
  • 嗨,Matias,0 是为调试设置的,我忘记更改了。但是当我将学习率改回 0.1 时,网络仍然无法学习。全程准确率52.99%

标签: python tensorflow machine-learning computer-vision


【解决方案1】:

我做了一些观察,首先你的代码有点过时了,你不必手动设置全连接层,有一些东西:dense layers。 如果加载图像,为什么不使用卷积层呢? 我还建议adam optimizer 将参数保留为默认值。我希望我能有所帮助:)

【讨论】:

  • 嗨 T. Kelher!感谢您的回答。将我的全连接层切换到 Tensorflow 的密集层似乎可以解决问题。很奇怪,它不适用于我的初始代码。
  • 嗨,你肯定知道密集层与全连接层完全相同。顺便说一句,你还在你的网络中应用了两次 Softmax
  • 我又试了一次。事实证明,问题出在我使用的 relu 激活函数上——由于某种原因,relu “死了”,因为它一直输出零,因此网络没有优化。切换到leaky_relu 或 tanh 为我完成了这项工作!
猜你喜欢
  • 2017-05-30
  • 2018-07-26
  • 2017-10-15
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
  • 2016-05-18
  • 2020-08-05
  • 2018-01-18
相关资源
最近更新 更多