File size: 2,916 Bytes
6e33b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""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("<Q", raw[:8])
    return raw[12:12 + length]


def _arr(ex, name, dtype):
    shape = ex[name.replace("data", "shape")]
    return np.frombuffer(ex[name], dtype=dtype).reshape(shape)


def load_tile(path, dtype=None):
    """Returns dict(x10,x20,x60,labels,doy,year) as numpy arrays (T leading)."""
    ex = parse_example(read_first_record(path))
    d = dtype or np.int64
    return dict(x10=_arr(ex, "x10/data", d), x20=_arr(ex, "x20/data", d),
                x60=_arr(ex, "x60/data", d), labels=_arr(ex, "labels/data", np.int64),
                doy=np.frombuffer(ex["dates/doy"], dtype=np.int64),
                year=np.frombuffer(ex["dates/year"], dtype=np.int64))