2017-02-17 105 views
1

在keras中存在一些问题。我只是试图建立模型,并让它运行,然后调整它。所以说,我只使用99个图像和99个标签。作为参考,我使用它来给我一个连续的输出,而不仅仅是一个类标签。以下是我正在使用的代码。首先,我有一个导入所有数据的脚本。 99个图像和99个标签。Keras模型的矩阵大小错误

当我到达模型部分的拟合时,它会抛出一个错误。 “ValueError:检查模型目标时出错:期望cropping2d_1有4个维度,但获得了具有形状(99,1)的数组”。

我读了一些关于类似错误的其他线程,它似乎可能是我发送keras的数组的顺序。我玩弄了它,并得到以下。目前图像数组的形状是(99,160,320,3)。我尝试改变keras中的“input_shape”的顺序为(3,160,320)。这给了我和错误“ValueError:错误时检查模型输入:预期cropping2d_input_1有形状(无,3,160,320),但有形状(99,160,320,3)的数组”。然后,我得到了与上面相同的错误,然后重新整理了images_center数组。

我已经省略了导入语句,只是为了在这里保持简短。

对后续步骤有何想法?

#Import col 3 to get a length of the dataset 
df = pd.read_csv('/Users/user/Desktop/data/driving_log.csv',usecols=[3]) 

#import and make a matrix of the file paths and data 
f = open('/Users/user/Desktop/data/driving_log.csv') 
csv_f = csv.reader(f) 
m=[] 
for row in csv_f: 
    n=(row) 
    m.append(n) 

#Create labels data 
labels=[] 
for i in range(1,100): 
    label=(m[i][3]) 
    labels.append(label) 
list1=[] 
for i in range(len(labels)): 
    ix=float(labels[i]) 
    list1.append(ix) 
labels=list1 
labels=np.array(labels) 


#Create features data 
#Loop through file paths, combine base path with folder path then read in and append 
images_center=[] 
for i in range(1,100): 
    img=(m[i][0]) 
    img=img.lstrip() 
    path='/Users/user/Desktop/data/' 
    img=path+img 
    image=cv2.imread(img) 

    images_center.append(image) 
images_center=np.array(images_center) 
print(images_center.shape) 

# Fix error with TF and Keras 
import tensorflow as tf 
tf.python.control_flow_ops = tf 
print(images_center.shape) 


model = Sequential() 
model.add(Convolution2D(16,3,3,border_mode='valid',input_shape=(160,320,3))) 


model.compile('adam','categorical_crossentropy',['accuracy']) 
history=model.fit(images_center,labels,nb_epoch=10,validation_split=0.2) 

回答

1

您的标签(即“目标”)具有形状(99,1),因此网络应该以相同形状生成输出。尝试在末尾添加完全连接的图层,如model.add(Dense(1))