omoindrot / tensorflow-triplet-loss

Implementation of triplet loss in TensorFlow
https://omoindrot.github.io/triplet-loss
MIT License
1.12k stars 284 forks source link

tf.data.dataset. Zip ((images, labels)) #44

Closed orliz closed 5 years ago

orliz commented 5 years ago

How can I use tf.data.dataset. Zip ((images, labels)) as you do when I use tfrecorder to read my data sets? What format do these two parameters need to be in?

omoindrot commented 5 years ago

If you use TFRecords, it's the same as with images loaded from jpeg.

You just need to end up with a dataset containing pairs of (image, label).

orliz commented 5 years ago

If you use TFRecords, it's the same as with images loaded from jpeg.

You just need to end up with a dataset containing pairs of (image, label). Thank you very much for your answer. This step is done.But my labeis is category interval like (0,3) ,(6,10)....(45,50,What should I do next, I really need your help.

omoindrot commented 5 years ago

If you have category intervals I'm not sure exactly what you are supposed to do.

You could say two examples are a positive pair if their intervals have a big enough intersection, or something like that. This doesn't feel like a standard case like face recognition.

orliz commented 5 years ago

If you have category intervals I'm not sure exactly what you are supposed to do.

You could say two examples are a positive pair if their intervals have a big enough intersection, or something like that. This doesn't feel like a standard case like face recognition.

Thank you for your answer.What I mean is that my label is the eight interval labels, not the integer labeis of the mnist dataset.I get an error when I run the program.

2019-08-03 21:17:22.362061: W tensorflow/core/framework/op_kernel.cc:1378] OP_REQUIRES failed at cast_op.cc:123 : Unimplemented: Cast string to float is not supported 2019-08-03 21:17:22.366516: E tensorflow/core/common_runtime/executor.cc:624] Executor failed to create kernel. Unimplemented: Cast stringto float is not supported

When I try to print a variable in a function _get_triplet_mask.I find some issues. your variable vlaue: Tensor("Cast:0", shape=(?,), dtype=int64) tttttttttttttttttttttttttttttttttttttt Tensor("Cast_1:0", shape=(?, ?), dtype=bool) Tensor("LogicalNot:0", shape=(?, ?), dtype=bool) Tensor("ExpandDims_4:0", shape=(?, ?, 1), dtype=bool) Tensor("LogicalAnd_1:0", shape=(?, ?, ?), dtype=bool) Tensor("LogicalAnd_2:0", shape=(?, ?, ?), dtype=bool) Tensor("LogicalAnd_3:0", shape=(?, ?, ?), dtype=bool)

my variable vales: Tensor("Cast:0", shape=(?, 1), dtype=int64) ttttttttttttttttttttttttttttttttttttttttt Tensor("Cast_1:0", shape=(?, ?), dtype=bool) Tensor("LogicalNot:0", shape=(?, ?), dtype=bool) Tensor("ExpandDims_4:0", shape=(?, ?, 1), dtype=bool) Tensor("LogicalAnd_1:0", shape=(?, ?, ?), dtype=bool) Tensor("LogicalAnd_2:0", shape=(?, ?, ?, 1), dtype=bool) Tensor("LogicalAnd_3:0", shape=(?, ?, ?, ?), dtype=bool)

so do you know how can i do.

omoindrot commented 5 years ago

I would guess that your labels are string (ex: the interval "[0, 8]"), so TensorFlow cannot convert these to float.

If you only have 8 intervals, you need to assign each one a unique id from 0 to 7, and then just use my code easily.

orliz commented 5 years ago

I would guess that your labels are string (ex: the interval "[0, 8]"), so TensorFlow cannot convert these to float.

If you only have 8 intervals, you need to assign each one a unique id from 0 to 7, and then just use my code easily.

All right.Thank you very much for your help.

orliz commented 5 years ago

When I got everything ready, I made the following mistake:

InvalidArgumentError (see above for traceback): Cannot batch tensors with different shapes in component 0. First element had shape [38528] and element 1 had shape [60597]. [[node IteratorGetNext (defined at C:\Users\Xiaok\Anaconda3\lib\site-packages\tensorflow_estimator\python\estimator\util.py:110) ]]

The function I'm handling the input is: def _parse_function(example_serialized):

Dense features in Example proto.

feature_map = {
    'image/encoded': tf.FixedLenFeature([], dtype=tf.string,
                                        default_value=''),
    'image/filename': tf.FixedLenFeature([], dtype=tf.string,
                                        default_value=''),

    'image/class/label': tf.FixedLenFeature([1], dtype=tf.int64,
                                            default_value=-1),
    # 'image/class/text': tf.FixedLenFeature([], dtype=tf.string,
    #                                         default_value=''),
    'image/height': tf.FixedLenFeature([1], dtype=tf.int64,
                                        default_value=-1),
    'image/width': tf.FixedLenFeature([1], dtype=tf.int64,
                                        default_value=-1),

}
features = tf.parse_single_example(example_serialized, feature_map)
label = tf.cast(features['image/class/label'], dtype=tf.int32)
image = tf.decode_raw(features['image/encoded'], out_type = tf.uint8)
image = tf.cast(image, tf.float32)
image = tf.reshape(image, [196608]) #my picture(256*256*3)
label = tf.reshape(label,[])
return image, label

def prepare_dataset(record_name,params): """ 从record_name指定的TFRecords文件,初始化一个dataset :param record_name: TFRecords文件路径 """

定义TFRecordDataset

dataset = tf.data.TFRecordDataset([record_name])
dataset = dataset.map(_parse_function)
dataset = dataset.shuffle(params.train_size)  # whole dataset into the buffer
dataset = dataset.repeat(params.num_epochs)  # repeat for multiple epochs
dataset = dataset.batch(params.batch_size)
print(dataset)
dataset = dataset.prefetch(1)  # make sure you always have one batch ready to serve
# print(dataset)
return dataset
omoindrot commented 5 years ago

This isn't related to triplet loss, maybe ask your question on stackoverflow.