加载本地cifar10 数据集

加载本地cifar10 数据集defload_CIFAR10(ROOT):”””loadallofcifar”””xs=[]ys=[]forbinrange(1,6):f=os.path.join(ROOT,’data_batch_%d’%(b,))X,Y=load_CIFAR_batch(f)xs.append(X)y…

大家好,又见面了,我是你们的朋友全栈君。

由于我们使用官方的导入cifar10数据集方法不成功,在知道cifar10数据集的本地路径的情况下,可以通过以下方法进行导入:

import tensorflow as tf
import numpy as np
import math
from six.moves import cPickle as pickle
import os
import platform
from subprocess import check_output
classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
def load_pickle(f):
    version = platform.python_version_tuple()
    if version[0] == '2':
        return  pickle.load(f)
    elif version[0] == '3':
        return  pickle.load(f, encoding='latin1')
    raise ValueError("invalid python version: {}".format(version))

def load_CIFAR_batch(filename):
    """ load single batch of cifar """
    with open(filename, 'rb') as f:
        datadict = load_pickle(f)
        X = datadict['data']
        Y = datadict['labels']
        X = X.reshape(10000,3072)
        Y = np.array(Y)
        return X, Y

def load_CIFAR10(ROOT):
    """ load all of cifar """
    xs = []
    ys = []
    for b in range(1,6):
        f = os.path.join(ROOT, 'data_batch_%d' % (b, ))
        X, Y = load_CIFAR_batch(f)
        xs.append(X)
        ys.append(Y)
    Xtr = np.concatenate(xs)
    Ytr = np.concatenate(ys)
    del X, Y
    Xte, Yte = load_CIFAR_batch(os.path.join(ROOT, 'test_batch'))
    return Xtr, Ytr, Xte, Yte
    
def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=10000):
    # Load the raw CIFAR-10 data
    cifar10_dir = '../input/cifar-10-batches-py/'
    X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)

    # Subsample the data
    mask = range(num_training, num_training + num_validation)
    X_val = X_train[mask]
    y_val = y_train[mask]
    mask = range(num_training)
    X_train = X_train[mask]
    y_train = y_train[mask]
    mask = range(num_test)
    X_test = X_test[mask]
    y_test = y_test[mask]

    x_train = X_train.astype('float32')
    x_test = X_test.astype('float32')

    x_train /= 255
    x_test /= 255

    return x_train, y_train, X_val, y_val, x_test, y_test


# Invoke the above function to get our data.
x_train, y_train, x_val, y_val, x_test, y_test = get_CIFAR10_data()


print('Train data shape: ', x_train.shape)
print('Train labels shape: ', y_train.shape)
print('Validation data shape: ', x_val.shape)
print('Validation labels shape: ', y_val.shape)
print('Test data shape: ', x_test.shape)
print('Test labels shape: ', y_test.shape)

参考:

  1. data_utils;
  2. stackoverflow;
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/152110.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • TaskScheduler_taskset -p

    TaskScheduler_taskset -p目录1、DAGScheduler与TaskScheduler2、TaskScheduler与SchedulerBackend3、任务调度过程总结1、DAGScheduler与TaskSchedulerDAGScheduler面向我们整个Job划分出了Stage,划分了Stage是从后往前划分的,执行的时候是从前往后,每个Stage内部有一系列任务,Stage里面的任务是并…

    2022年10月11日
    0
  • HTTP状态码——302「建议收藏」

    理解302表示临时性重定向。访问一个Url时,被重定向到另一个url上。常用于页面跳转。与301的区别301是指永久性的移动,302是暂时性的,即以后还可能有变化其它重定向方式在响应头中加入Location参数。浏览器接受到带有location头的响应时,就会跳转到相应的地址。…

    2022年4月18日
    82
  • python一维插值scipy.interpolate.interp1d

    python一维插值scipy.interpolate.interp1dSciPy的interpolate模块提供了许多对数据进行插值运算的函数,范围涵盖简单的一维插值到复杂多维插值求解。当样本数据变化归因于一个独立的变量时,就使用一维插值;反之样本数据归因于多个独立变量时,使用多维插值。classscipy.interpolate.interp1d(x,y,kind=’linear’,axis=-1,copy=True,bounds_…

    2022年6月8日
    97
  • IE7\IE6 图片上传预览

    IE7\IE6 图片上传预览

    2021年7月28日
    54
  • qlogic官网_zabbix和nagios

    qlogic官网_zabbix和nagios引用http://hi.baidu.com/zeorliu/blog/item/be188aca2ce3ab8fc9176858.html2009-06-1716:26http://asmboy001.blog.51cto.com/340398/111496CactiNagiosSquid三个工具的一些区别cacti是

    2022年10月5日
    0
  • c语言List头文件和应用

    c语言List头文件和应用util_list.h#ifndef__UTIL_LIST__#define__UTIL_LIST__/*双链节点*/typedefstructlist_node{ list_node*prev; list_node*next;}LIST_NODE;/*单链节点*/typedefstructslist_node{ slist_node*ne

    2022年7月12日
    46

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号