Error When Checking Input: Expected Flatten_input To Have 3 Dimensions, But Got Array With Shape (none, 100, 100, 1)
Using TensorFlow/Keras, I want to classify pictures into two classes, selfie and non-selfie. I have gathered samples into two filesystem folders, one for each category. I implement
Solution 1:
1) The images have one channel so this must be reflected in the input shape argument:
keras.layers.Flatten(input_shape=(100, 100, 1))
2) To load the files with tf.data
API, you need to first fetch the image filenames and their corresponding labels:
image_paths, lbls = ["selfies-data/1", "selfies-data/2"], [0., 1.]
labels = []
file_names = []
for d, l in zip(image_paths, lbls):
# get the list all the images file names
name = [os.path.join(d,f) for f in os.listdir(d)]
file_names.extend(name)
labels.extend([l] * len(name))
file_names = tf.convert_to_tensor(file_names, dtype=tf.string)
labels = tf.convert_to_tensor(labels)
dataset = tf.data.Dataset.from_tensor_slices((file_names, labels))
# the rest is the same
You may also need to expand the dimension of labels
to make it have a shape of (?, 1)
(instead of (?,)
). To do so, you can put the following line in map_fn
function:
labels = tf.expand_dims(labels, axis=-1)
3) If you have two classes, then why the last layer has 10 units? It is a binary classification problem, so make the last layer have one unit with sigmoid
activation. Finally, change the loss to binary_crossentropy
:
# ...
keras.layers.Dense(1, activation=tf.nn.sigmoid)
])
model.compile(optimizer=tf.train.AdamOptimizer(),
loss='binary_crossentropy',
metrics=['accuracy'])
Post a Comment for "Error When Checking Input: Expected Flatten_input To Have 3 Dimensions, But Got Array With Shape (none, 100, 100, 1)"