utilities common to cifar10 and cifar100 datasets import pickle as cpickle def loadbatchfpath labelkeylabels with openfpath rb as f d cpickle loadf encodingbytes decode utf8 ddecoded for k v in d items ddecodedk decodeutf8 v d ddecoded data ddata labels dlabelkey data data reshapedata shape0 3 32 32 return data labels internal utility for parsing cifar data args fpath path the file to parse label_key key for label data in the retrieve dictionary returns a tuple data labels decode utf8
import _pickle as cPickle


def load_batch(fpath, label_key="labels"):
    
    with open(fpath, "rb") as f:
        d = cPickle.load(f, encoding="bytes")
        
        d_decoded = {}
        for k, v in d.items():
            d_decoded[k.decode("utf8")] = v
        d = d_decoded
    data = d["data"]
    labels = d[label_key]

    data = data.reshape(data.shape[0], 3, 32, 32)
    return data, labels