首页 文章

CNN输出在tflearn中回归

提问于
浏览
2

我正在开一辆自动驾驶汽车 . 我想在tflearn中使用CNN预测照片的转向角度 . 问题是它只输出0.1 . 您认为这个问题是什么?图片是128x128,但我试图将它们调整为28x28,所以我可以使用mnist示例中的代码 . 标签的转向角度在0到180之间 . 我还可以说在训练时损失不会变小 .

Training.py

import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import tflearn.datasets.mnist as mnist
import numpy
from scipy import misc
import csv

nrOfFiles = 0
csv_list = []

with open('/Users/gustavoskarsson/Desktop/car/csvfile.csv', 'r') as f:
    reader = csv.reader(f)
    csv_list = list(reader)

nrOfFiles = len(csv_list)

pics = []
face = misc.face()
for i in range(0, nrOfFiles):
    face = misc.imread('/Users/gustavoskarsson/Desktop/car/pics/' + str(i) + '.jpg')
    face = misc.imresize(face[:,:,0], (28, 28))
    pics.append(face)

X = numpy.array(pics)


steer = []
throt = []
for i in range(0, nrOfFiles):
    steer.append(csv_list[i][1])
    throt.append(csv_list[i][2])

#y__ = numpy.array([steer, throt])
Y = numpy.array(steer)
Y = Y.reshape(-1, 1)
#Strunta i gasen till att börja med.


convnet = input_data(shape=[None, 28, 28, 1], name='input')

convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 1, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=0.01, loss='mean_square', name='targets')

model = tflearn.DNN(convnet)
model.fit(X, Y, n_epoch=6, batch_size=10, show_metric=True)
model.save('mod.model')

Predict.py

import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import tflearn.datasets.mnist as mnist
import numpy
from scipy import misc


convnet = input_data(shape=[None, 28, 28, 1], name='input')
                           #[none, 28,  28, 1]

convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)

convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 1, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=0.01, loss='mean_square', name='targets')

model = tflearn.DNN(convnet)
model.load('mod.model')

#load test image
face = misc.face()
pics = []
for i in range(0, 3):
    face = misc.imread('/Users/gustavoskarsson/Desktop/car/pics/' + str(i) + '.jpg')
    face = misc.imresize(face[:,:,0], (28, 28))
    pics.append(face) 

test_x = numpy.array(pics)
test_x = test_x.reshape([-1, 28, 28, 1])
print(model.predict([test_x[0]]))

2 回答

  • 1

    问题可能是由于您的输出层 . 它使用softmax激活功能,始终产生0-1的输出 .

    如果你看一下softmax function definition,你会发现它取决于你图层的每个输出节点 . 由于您只有一个输出节点,因此您应始终返回1,因为您要将输出除以其自己的值 . 如果您想了解有关softmax图层的更多信息,请查看Michael Nielsen's great free book on Neural Networks .

    如果您不尝试对事物进行分类,softmax函数也不是一个好的选择 .

    尝试在上次完全连接的图层中省略 activation='softmax' .

  • 1

    您将覆盖convent变量,该变量具有每个层的卷积网络 . 您还应该在每一层中进行采样 . 你的代码应该是这样的:

    x = tf.reshape(x, shape=[-1, 28, 28, 1])
    
        # Convolution Layer with 32 filters and a kernel size of 5
        conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu)
    
        # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
        conv1 = tf.layers.max_pooling2d(conv1, 2, 2)
    
        # Convolution Layer with 64 filters and a kernel size of 3
        conv2 = tf.layers.conv2d(conv1, 64, 3, activation=tf.nn.relu)
    
        # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
        conv2 = tf.layers.max_pooling2d(conv2, 2, 2)
    

    你也可以看到here

相关问题