Jake3991
Jake3991

Reputation: 101

Matrix Size Error with Keras Model

Having some issues with keras. I am just trying to build the model and get it running and then tune it in. So that being said I am only using 99 images and 99 labels. For reference I am using it to give me a continuous output not just a class label. Below is the code I am using. First I have a script that imports all the data. 99 images and 99 labels.

When I get to the fitting of the model part it tosses me an error. "ValueError: Error when checking model target: expected cropping2d_1 to have 4 dimensions, but got array with shape (99, 1)".

I read some other threads about similar errors and it seems it could be the order of the array I am sending keras. I played around with it and got the following. Currently the shape of the images array is (99,160,320,3). I tried changing the order of the "input_shape" in keras to (3,160,320). This gave me and error "ValueError: Error when checking model input: expected cropping2d_input_1 to have shape (None, 3, 160, 320) but got array with shape (99, 160, 320, 3)". I then reshaped the images_center array accordingly at got the same error as above.

I've left out the import statements just to keep it short here.

Any thoughts on next steps?

#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)

Upvotes: 1

Views: 448

Answers (1)

hulleywood
hulleywood

Reputation: 11

Your labels (i.e. the "target") are of shape (99, 1) so the network should produce an output in the same shape. Try adding a fully connected layer at the end, like model.add(Dense(1)).

Upvotes: 1

Related Questions