SmilingWolf commited on
Commit
6387a69
·
verified ·
1 Parent(s): b9c7907

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +71 -0
README.md ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ size_categories:
3
+ - 1M<n<10M
4
+ ---
5
+
6
+ E621 TFRecords to train classifiers and other stuff with my codebases.
7
+
8
+ TFRecord serialization/deserialization code:
9
+
10
+ ```python
11
+ NUM_CLASSES = 8783
12
+
13
+
14
+ # Function to convert value to bytes_list
15
+ def _bytes_feature(value):
16
+ if isinstance(value, type(tf.constant(0))):
17
+ value = value.numpy()
18
+ elif isinstance(value, str):
19
+ value = value.encode()
20
+ return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
21
+
22
+
23
+ # Function to convert bool/enum/int/uint to int64_list
24
+ def _int64_feature(value):
25
+ int64_list = tf.train.Int64List(value=tf.reshape(value, (-1,)))
26
+ return tf.train.Feature(int64_list=int64_list)
27
+
28
+
29
+ # Function to create a tf.train.Example message
30
+ def serialize_example(image_id, image_bytes, label_indexes, tag_string):
31
+ feature = {
32
+ "image_id": _int64_feature(image_id),
33
+ "image_bytes": _bytes_feature(image_bytes),
34
+ "label_indexes": _int64_feature(label_indexes),
35
+ "tag_string": _bytes_feature(tag_string),
36
+ }
37
+ example_proto = tf.train.Example(features=tf.train.Features(feature=feature))
38
+ return example_proto.SerializeToString()
39
+
40
+
41
+ # Function to deserialize a single tf.train.Example message
42
+ def deserialize_example(example_proto):
43
+ feature_description = {
44
+ "image_id": tf.io.FixedLenFeature([], tf.int64),
45
+ "image_bytes": tf.io.FixedLenFeature([], tf.string),
46
+ "label_indexes": tf.io.VarLenFeature(tf.int64),
47
+ "tag_string": tf.io.FixedLenFeature([], tf.string),
48
+ }
49
+
50
+ # Parse the input 'tf.train.Example' proto using the dictionary above.
51
+ parsed_example = tf.io.parse_single_example(example_proto, feature_description)
52
+ image_tensor = tf.io.decode_jpeg(parsed_example["image_bytes"], channels=3)
53
+
54
+ # We only stored label indexes in the TFRecords to save space
55
+ # Emulate MultiLabelBinarizer to get a tensor of 0s and 1s
56
+ label_indexes = tf.sparse.to_dense(
57
+ parsed_example["label_indexes"],
58
+ default_value=0,
59
+ )
60
+ one_hots = tf.one_hot(label_indexes, NUM_CLASSES)
61
+ labels = tf.reduce_max(one_hots, axis=0)
62
+ labels = tf.cast(labels, tf.float32)
63
+
64
+ sample = {
65
+ "image_ids": parsed_example["image_id"],
66
+ "images": image_tensor,
67
+ "labels": labels,
68
+ "tags": parsed_example["tag_string"],
69
+ }
70
+ return sample
71
+ ```