请看下面的github页,我在那里做了这些。如果上面的链接失败,请按照kgeorge.github.io上的线索,查看笔记本tf_cifar.ipynb。我尝试使用小步骤加载cifar-10数据。请查找函数load_and_preprocess_input
该代码中的以下函数接受数据作为(nsamples,32x32x3)float32的np数组,并将其标记为nsamples int32的np数组,并预处理tensorflow训练要使用的数据。image_depth=3
image_height=32
image_width=32
#data = (nsamples, 32x32x3) float32
#labels = (nsamples) int32
def prepare_input(data=None, labels=None):
global image_height, image_width, image_depth
assert(data.shape[1] == image_height * image_width * image_depth)
assert(data.shape[0] == labels.shape[0])
#do mean normaization across all samples
mu = np.mean(data, axis=0)
mu = mu.reshape(1,-1)
sigma = np.std(data, axis=0)
sigma = sigma.reshape(1, -1)
data = data - mu
data = data / sigma
is_nan = np.isnan(data)
is_inf = np.isinf(data)
if np.any(is_nan) or np.any(is_inf):
print('data is not well-formed : is_nan {n}, is_inf: {i}'.format(n= np.any(is_nan), i=np.any(is_inf)))
#data is transformed from (no_of_samples, 3072) to (no_of_samples , image_height, image_width, image_depth)
#make sure the type of the data is no.float32
data = data.reshape([-1,image_depth, image_height, image_width])
data = data.transpose([0, 2, 3, 1])
data = data.astype(np.float32)
return data, labels