TensorFlow加载cifar10数据集

TensorFlow加载cifar10数据集加载cifar10数据集cifar10_dir=’C:/Users/1/.keras/datasets/cifar-10-batches-py'(train_images,train_labels),(test_images,test_labels)=load_data(cifar10_dir)注意:在官网下好cifar10数据集后将其解压成下面形式load_local_cifar10.pyfrom__future__importabsolute_importfrom_

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

加载cifar10数据

cifar10_dir = 'C:/Users/1/.keras/datasets/cifar-10-batches-py'
(train_images, train_labels), (test_images, test_labels) = load_data(cifar10_dir)

注意:在官网下好cifar10数据集后将其解压成下面形式
在这里插入图片描述

load_local_cifar10.py

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import sys

import numpy as np
from six.moves import cPickle
from tensorflow.keras import backend as K


def load_batch(fpath, label_key='labels'):
    """Internal utility for parsing CIFAR data. # Arguments fpath: path the file to parse. label_key: key for label data in the retrieve dictionary. # Returns A tuple `(data, labels)`. """
    with open(fpath, 'rb') as f:
        if sys.version_info < (3,):
            d = cPickle.load(f)
        else:
            d = cPickle.load(f, encoding='bytes')
            # decode utf8
            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


def load_data(ROOT):
    """Loads CIFAR10 dataset. # Returns Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. """
    # dirname = 'cifar-10-batches-py'
    # origin = 'https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz'
    # path = get_file(dirname, origin=origin, untar=True)
    path = ROOT

    num_train_samples = 50000

    x_train = np.empty((num_train_samples, 3, 32, 32), dtype='uint8')
    y_train = np.empty((num_train_samples,), dtype='uint8')

    for i in range(1, 6):
        fpath = os.path.join(path, 'data_batch_' + str(i))
        (x_train[(i - 1) * 10000: i * 10000, :, :, :],
         y_train[(i - 1) * 10000: i * 10000]) = load_batch(fpath)

    fpath = os.path.join(path, 'test_batch')
    x_test, y_test = load_batch(fpath)

    y_train = np.reshape(y_train, (len(y_train), 1))
    y_test = np.reshape(y_test, (len(y_test), 1))

    if K.image_data_format() == 'channels_last':
        x_train = x_train.transpose(0, 2, 3, 1)
        x_test = x_test.transpose(0, 2, 3, 1)

    return (x_train, y_train), (x_test, y_test)

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

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

(0)
上一篇 2022年6月22日 上午10:16
下一篇 2022年6月22日 上午10:16


相关推荐

  • CentOS7查看和关闭防火墙

    CentOS7.0默认使用的是firewall作为防火墙查看防火墙状态firewall-cmd–state停止firewallsystemctlstopfirewalld.service禁止firewall开机启动systemctldisablefirewalld.service转自:CentOS6和CentOS7防火墙的关闭关闭se…

    2022年4月4日
    45
  • LVS实现负载均衡「建议收藏」

    LVS实现负载均衡「建议收藏」一、LVS1、LVS是什么?LVS(LinuxVirtualServer)即Linux虚拟服务器,是由章文嵩博士主导的开源负载均衡项目,目前LVS已经被集成到Linux内核模块中。终端互联网用户从外部访问公司的外部负载均衡服务器,终端用户的Web请求会发送给LVS调度器,调度器根据自己预设的算法决定将该请求发送给后端的某台Web服务器,比如,轮询算法可以将外部的请求平均分…

    2022年7月23日
    12
  • java 单例模式 —饿汉式懒汉式

    java 单例模式 —饿汉式懒汉式目录单例设计模式饿汉式懒汉式饿汉式vs懒汉式结语单例设计模式所谓单例设计模式,就是采取一定的方法在整个软件系统中,对某个类只能存在一个对象实例1、单例类只能有一个实例。2、单例类必须自己创建自己的唯一实例。3、单例类必须给所有其他对象提供这一实例。饿汉式饿汉式:在程序启动或单例模式类被加载的时候,单例模式实例就已经被创建。上例子!packagecom.happy.demo;publicclassSingleton…

    2022年7月25日
    10
  • 数据结构(C语言版本)

    数据结构(C语言版本)数据结构 C 语言版本 第 1 章绪论 1 常用的数据结构类型 集合 线性 树形 图状 2 数据结构 逻辑结构 数据元素之间的关系 存储结构 数据结构在计算机中的表示 存储结构分为 顺序存储结构和链式存储结构 3 算法是对特定问题求解步骤的一种描述 算法具有如下特性 有穷性 确定性 可行性 输入 输出 4 算法的度量 时间复杂度 空间复杂度

    2026年3月19日
    2
  • 将腾讯元宝设置成写材料的助手

    将腾讯元宝设置成写材料的助手

    2026年3月12日
    1
  • redhat忘记root密码的解决办法_grub修改root密码

    redhat忘记root密码的解决办法_grub修改root密码转于lee的http://hi.baidu.com/maozilee/item/12a62a76f371df2bd7a89c5dRedFlagLinux忘记root密码解决办法Linux忘记root密码解决办法(进入Linux单用户系统修复模式)1.用RedFlag标准安装盘启动系统见http://blog.sina.com.cn/s/blog_8e5b82670101

    2022年8月20日
    9

发表回复

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

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