"""Dependency-free reader for the MTLCC (data_IJGI18) *.tfrecord.gz tiles. Each file holds ONE tf.Example whose features are: x10/data (T,48,48,4) x20/data (T,24,24,6) x60/data (T,8,8,3) raw bytes labels/data (T,48,48) dates/doy, dates/year raw bytes (+ */shape int64) Parsed with a tiny protobuf wire decoder so DataLoader workers do not import TensorFlow. """ import gzip import struct import numpy as np def _varint(b, p): r = s = 0 while True: c = b[p]; p += 1 r |= (c & 0x7F) << s if c < 0x80: return r, p s += 7 def _fields(b): p, n = 0, len(b) while p < n: key, p = _varint(b, p) f, w = key >> 3, key & 7 if w == 0: v, p = _varint(b, p) elif w == 2: l, p = _varint(b, p); v = b[p:p + l]; p += l elif w == 1: v = b[p:p + 8]; p += 8 elif w == 5: v = b[p:p + 4]; p += 4 else: raise ValueError("bad wire type") yield f, v def parse_example(buf): out = {} for f, features in _fields(buf): # Example.features = 1 if f != 1: continue for f2, entry in _fields(features): # Features.feature (map) = 1 key = val = None for f3, v in _fields(entry): if f3 == 1: key = bytes(v).decode() elif f3 == 2: val = v for kind, lst in _fields(val): # Feature: 1 bytes,2 float,3 int64 if kind == 1: out[key] = [bytes(v) for _, v in _fields(lst)][0] elif kind == 3: ints = [] for _, v in _fields(lst): if isinstance(v, int): ints.append(v) else: # packed varints p = 0 while p < len(v): x, p = _varint(v, p); ints.append(x) out[key] = ints return out def read_first_record(path): with gzip.open(path, "rb") as f: raw = f.read() (length,) = struct.unpack("