问题
I have a CNN model that is trained using a set of 120 pictures. The images are converted in TFR record and labeled with this method
def write_records_file(dataset, record_location):
"""
dataset : dict(list)
Dictionary with each key being a label for the list of image filenames of its value.
record_location : str
Location to store the TFRecord output.
"""
writer = None
# Enumerating the dataset because the current index is used to breakup the files if they get over 100
current_index = 0
for breed, images_filenames in dataset.items():
for image_filename in images_filenames:
if current_index % 100 == 0:
if writer:
writer.close()
record_filename = "{record_location}-{current_index}.tfrecords".format(
record_location=record_location,
current_index=current_index)
writer = tf.python_io.TFRecordWriter(record_filename)
current_index += 1
image_file = tf.read_file(image_filename)
image = tf.image.decode_jpeg(image_file)
grayscale_image = tf.image.rgb_to_grayscale(image)
resized_image = tf.image.resize_images(grayscale_image, 250, 151)
image_bytes = sess.run(tf.cast(resized_image, tf.uint8)).tobytes()
image_label = breed.encode("utf-8")
example = tf.train.Example(features=tf.train.Features(feature={
'label': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_label])),
'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image_bytes]))
}))
writer.write(example.SerializeToString())
write_records_file(testing_dataset, "./output/testing-images/testing-image")
write_records_file(training_dataset, "./output/training-images/training-image")
The whole model+training script ends with train_prediction = tf.nn.softmax(final_fully_connected)
and I get 2 .tfr files as output (training and test).
Now suppose you have a picture and want to know what is the more similar picture of the 120 pic sample to identify it. How I have to proceed?
train_prediction tensor has this format shape=(3, 120), dtype=float32120 is the total numbers of categories
In the book that I'm reading unfortunately there isn't any indication and the chapter end with this trained model that I don't know how to use in a real application, and searching in internet there are many similar sample that end at same point.
回答1:
I didn't understand your question. By 120 samples, do you mean there are 120 classes with few examples images per class? In that case, this is classification problem where you train a model for classification with input as image and output as probability that the input will belong to one of the 120 classes (+ extra class that it doesnt belong to any of those). In this case, fully connected layer is fed into softmax function that outputs this probability for 120 classes which is essentially vector of length 120. But for this, you need multiple training examples per class.
In case you just have 120 images and you need to know if another test image similarity with one of these images, you can just take output vector of layer before fully connected layer for those 120 images for a neural network that is already trained on a larger sample of images (like inception model) and then just similarity of the test image vector with those 120 vectors.
I have hosted a simple to use demo using above technique here. Check if works out for you otherwise refine the problem statement more.
来源:https://stackoverflow.com/questions/41618096/how-to-use-a-trained-cnn-model-for-object-identification-in-tensorflow