cifar10-图像识别相关代码

import urllib
import os
import tarfile
import numpy as np
import pickle as pk
import tensorflow as tf

tf.compat.v1.disable_eager_execution() 
#下载、解压CIFAR数据集
url='https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz'
file_path='/Users/mac/Downloads/TensorFlow/09图像识别问题:卷积神经网络与应用/data/cifar-10-python.tar.gz'
#如果目标文件不存在,则从指定url下载该文件
if not os.path.isfile(file_path):      
    urllib.request.urlretrieve(url,file_path)
#如果目录下不存在文件,则解压
if not os.path.exists('/Users/mac/Downloads/TensorFlow/09图像识别问题:卷积神经网络与应用/data/cifar-10-batches-py'):     
    tfile=tarfile.open('/Users/mac/Downloads/TensorFlow/09图像识别问题:卷积神经网络与应用/data/cifar-10-python.tar.gz','r:gz')
    tfile.extractall('/Users/mac/Downloads/TensorFlow/09图像识别问题:卷积神经网络与应用/data')
    
#载入数据
def load_batch(file):                                            #读取一个批次的数据
    with open(file,'rb') as f:
        data_dict=pk.load(f,encoding='bytes')
        images=data_dict[b'data']
        labels=data_dict[b'labels']
        
        #将一维图片数据调整为四维数组
        images=images.reshape(10000,3,32,32)
        #将(10000,3,32,32)调整参数数组维度为(10000,32,32,3)
        images=images.transpose(0,2,3,1)                         
        
        labels=np.array(labels)
        
        return images,labels
    
def load_data(data_dir):
    images_train=[]
    labels_train=[]
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值