tflearn中的CNN输出回归

2024-09-29 19:30:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研制一辆自动驾驶汽车。我想用tflearn中的CNN从图片中预测转向角。问题是它只输出0.1。你觉得问题是什么?图片大小为128x128,但我尝试将其调整为28x28,以便使用mnist示例中的代码。标签的转向角在0到180之间。我也可以说,在训练中损失并没有变小。在

在培训.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')

在预测.py在

^{pr2}$

Tags: csvfromimportnumpymodelactivationlistface
2条回答

覆盖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

问题可能是由于您的输出层。它使用一个softmax激活函数,它总是产生0-1的输出。在

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

另外,如果您不想对事物进行分类,softmax函数也不是一个好的选择。在

尝试省略最后一个完全连接的层中的activation='softmax'。在

相关问题 更多 >

    热门问题