1# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Utilities common to CIFAR10 and CIFAR100 datasets.
16"""
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21import sys
22
23from six.moves import cPickle
24
25
26def load_batch(fpath, label_key='labels'):
27  """Internal utility for parsing CIFAR data.
28
29  Args:
30      fpath: path the file to parse.
31      label_key: key for label data in the retrieve
32          dictionary.
33
34  Returns:
35      A tuple `(data, labels)`.
36  """
37  with open(fpath, 'rb') as f:
38    if sys.version_info < (3,):
39      d = cPickle.load(f)
40    else:
41      d = cPickle.load(f, encoding='bytes')
42      # decode utf8
43      d_decoded = {}
44      for k, v in d.items():
45        d_decoded[k.decode('utf8')] = v
46      d = d_decoded
47  data = d['data']
48  labels = d[label_key]
49
50  data = data.reshape(data.shape[0], 3, 32, 32)
51  return data, labels
52