Reputation: 131
I have a Tensorflow with the following layers model, I believe that the problem lies with the input shape of the first layer. all the images that i am using are colored and has 492x702 resolution.
# Initialize empty lists
images = np.array([])
labels = np.array([])
# read and convert each image to an array
# then append it to the images list and append the result label to the label list
for index, folder in enumerate(os.listdir(path)):
print("Folder Index = " + str(index))
if folder == ".DS_Store":
continue
for i, image in enumerate(os.listdir(path + '/' + folder)):
print("Image Index = " + str(i))
image_path = path + '/' + folder + '/' + image
img = plt.imread(image_path)
image_array = tf.keras.preprocessing.image.img_to_array(img)
images = np.append(images, image_array)
labels = np.append(labels, folder)
training_images = images
training_images = training_images / 255.0
# model structure
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation=tf.nn.relu, input_shape=(702, 492, 1)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(64, (3, 3), activation=tf.nn.relu),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(100, activation=tf.nn.softmax)
])
# compile the mode
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy'])
model.summary()
# train the model
model.fit(training_images, labels, epochs=3)
Error:
ValueError: Data cardinality is ambiguous:
x sizes: 725306400
y sizes: 700
Make sure all arrays contain the same number of samples.
Am I doing something wrong?
Upvotes: 0
Views: 247
Reputation: 489
Pay attention on images.shape
. After applying np.append()
you get a flat array instead of 3d array. So as a result you pass a flat array to NN input which expects an object having (702, 492, 1)
shape
Upvotes: 1