Before I start I would like to point out that I am fairly new to this subject therefore I still have a lot to learn and if it is not much to ask for, I would like a clear answer so I can really grasp the idea behind.
So my problem as it is presented in the question is about how can I feed a label that is essentially a tuple with several values in it to my fit
function so that I can train my model. I've tried to transform it into a numpy array and then feed it to my model using asarray
function.
label = np.asarray(label)
but it gives me an error essentially saying this:
ValueError: Input arrays should have the same number of samples as target arrays. Found 1 input samples and 6 target samples.
Which makes sense since the tuple is made by 6 values and after transforming it into a numpy array i get 6 elements in it, so when i pass the image and the label which now has 6 elements, to fit
function it arises this error because im essentially passing 6 labels for just one image right? So my question is, how can I feed the label, with those 6 features which represent different parts of the image that i want the model to be able to recognise, to the fit function so that the model can be trained based on that label which has 6 features in it?
Background:
So I am working with Convolutional Neural Networks ( Conv2D
) and I am trying to build a model that can identify American car plates. The images that I have, have only license plates with 6 number/characters and thats what goes into the labels. I have a parseImgFunction
that receives the photo and returns return (image_mat,label)
. This label has 6 elements in it (each representing one characters/number of the plate) and is a tuple. Basically I want to use this label in fit
like its shown below so that for each image I have a label with 6 features each representing a part of the plate. Also the image that I fed to the model was already reshaped.
history = model.fit(image, label, epochs=1, steps_per_epoch=100)
Thanks in advance!
Edit:
Sorry for not giving you the necessary code. Here is the following code that i am using:
dataset = tf.data.TFRecordDataset('american_car_plates.tfrecords')
feature_description = {'first': tf.io.FixedLenFeature([], tf.int64),
'second': tf.io.FixedLenFeature([], tf.int64),
'third': tf.io.FixedLenFeature([], tf.int64),
'forth': tf.io.FixedLenFeature([], tf.int64),
'fifth': tf.io.FixedLenFeature([], tf.int64),
'sixth': tf.io.FixedLenFeature([], tf.int64),
'raw': tf.io.FixedLenFeature([], tf.string),
}
def parseImgFunction(proto):
aux = tf.io.parse_single_example(proto, feature_description)
raw = aux['raw']
first = aux['first']
second = aux['second']
third = aux['third']
forth = aux['forth']
fifth = aux['fifth']
sixth = aux['sixth']
full_label = (first, second, third, forth, fifth, sixth)
label = full_label
image = tf.io.decode_jpeg(raw, channels=1)
image = tf.cast(image, dtype=tf.float32)
image_mat = 1 / 255 * image
return (image_mat,label)
mapped_images = dataset.map(parseImgFunction)
it = iter(mapped_images)
image_mat, label = next(it)
im = tf.squeeze(image_mat).numpy()
im = im.reshape([-1, 620, 420, 1])
label = np.asarray(label)
input = Input(shape=(620, 420, 1))
conv1 = Conv2D(16, kernel_size=(3, 3), activation='relu')(input)
max1 = MaxPooling2D((2, 2))(conv1)
drop1 = Dropout(0.2)(max1)
conv2 = Conv2D(24, kernel_size=(3, 3), activation='relu')(drop1)
max2 = MaxPooling2D((2, 2))(conv2)
drop2 = Dropout(0.2)(max2)
flat1 = Flatten()(drop2)
dense1 = Dense(128, activation='relu')(flat1)
drop3 = Dropout(0.2)(dense1)
out = Dense(1, activation='relu')(drop3)
model = Model(input, out)
print(model.summary())
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(im, label, epochs=1, steps_per_epoch=100)
Versions:
Keras-2.3.1 Tensorflow-2.0.0 Python-3.7
Although I do not have code (please update the post with the code), it seems that, when you feed data to your model, your classes are not separated in samples, as provided in this comment: Value error: Input arrays should have the same number of samples as target arrays. Found 1600 input samples and 6400 target samples .
Ensure the right preprocessing is done in order to solve your problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.