forked from MasterprojectRK/Hi-cGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecords.py
55 lines (50 loc) · 2.17 KB
/
records.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import tensorflow as tf
import numpy as np
#parse serialized input to tensors
def parse_function(example_proto, descriptionDict):
featDict = dict()
targetDict = dict()
contents = [key for key in descriptionDict]
dtypes = [descriptionDict[key]["dtype"] for key in contents]
shapes = [descriptionDict[key]["shape"] for key in contents]
features = {key: tf.io.FixedLenFeature((), tf.string) for key in contents}
parsed_features = tf.io.parse_single_example(example_proto, features)
for name, dtype, shape in zip(contents, dtypes, shapes):
if name.startswith("out_"):
targetDict[name] = tf.reshape( tf.io.decode_raw(parsed_features[name], dtype), shape)
else:
featDict[name] = tf.reshape( tf.io.decode_raw(parsed_features[name], dtype), shape)
retList = []
if len(featDict) > 0:
retList.append(featDict)
if len(targetDict) > 0:
retList.append(targetDict)
return tuple(retList)
# helper functions from tensorflow TFRecord docs
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
#write tfRecord to disk
def writeTFRecord(pFilename: str, pRecordDict: dict):
for key in pRecordDict:
if not isinstance(pRecordDict[key], np.ndarray):
return
batches = set()
for key in pRecordDict:
batches.add(pRecordDict[key].shape[0])
if len(batches) > 1:
msg = "Batch sizes are not equal"
raise ValueError(msg)
with tf.io.TFRecordWriter(pFilename, options="GZIP") as writer:
for i in range(list(batches)[0]):
feature = dict()
for key in pRecordDict:
feature[key] = _bytes_feature( pRecordDict[key][i].flatten().tostring() )
example = tf.train.Example(features=tf.train.Features(feature=feature))
writer.write(example.SerializeToString())
def mirror_function(tensor1, tensor2):
t1 = tf.reverse(tensor1, axis=[0])
t2 = tf.transpose(tensor2, perm=(1,0,2))
t2 = tf.image.rot90(t2, 2)
return {"factorData": t1}, {"out_matrixData": t2}