2017-02-17 71 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))