精华内容
下载资源
问答
  • 深度学习DCCNN卷积网络框架 cifar10准确率92% cifar100准确率71% 代码请看该网址: https://github.com/titu1994/Deep-Columnar-Convolutional-Neural-Network
  • 回顾CIFAR 10和100数据集分类的最新进展 目录 ResNext 广泛的资源 预设资源 随机深度树脂 Resnet 超级融合 纸咖啡官员 随机权重平均 纸pytorch | 重生网络 纸 删除ReLU 纸 循环学习 纸 震动掉落 纸lua...
  • 有很多小伙伴们在用迁移学习在Cifar100上训练时候,其识别非常低,有的20%多,有的60%。为什么有那么低的识别呢?主要是训练策略问题,下面我们用ResNet34来测试Cifar100,看看最后的识别能达到多少。顺便说...

    有很多小伙伴们用迁移学习在Cifar100数据库上训练ResNet时候,其识别率非常低,有的20%多,有的60%多,为什么有那么低的识别率呢?主要是训练策略问题,下面我们以ResNet34为例来测试Cifar100,看看最后的识别率能达到多少。顺便说一下ResNet的命名,比如ResNet34包括33个Conv2d和1个fc层(fc层就是该网络的输出特征层),共34个可以学习的层,其它的层不具备学习功能占用资源很少;ResNet101就包含100个Conv2d和1个fc层。

    第一步、拟定迁移的层和需要调整参数的层,这里只将fc层重新学习,其余各层的权重weight固定不变。将学习速率设置大一些(初始 lr=0.01),然后保存模型,代码如下

    import torch
    import torch.nn as nn
    import torch.optim as optim
    import torchvision
    from torchvision import transforms
    from torchvision import models
    from torchvision.models.resnet import resnet34
    from torchvision.transforms.transforms import Resize
    
    transform = transforms.Compose([
    transforms.Resize(224),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225])
    ])
    #采用自带的Cifar100
    trainset = torchvision.datasets.CIFAR100(root='./data', train=True, download=True, transform=transform)
    trainloader = torch.utils.data.DataLoader(trainset, batch_size=200, shuffle=True)
    
    testset = torchvision.datasets.CIFAR100(root='./data', train=False, download=True, transform=transform)
    testloader = torch.utils.data.DataLoader(testset, batch_size=200, shuffle=False)
    net=models.resnet34(pretrained=True)
    ##迁移学习
    for param in net.parameters(): #固定参数
        print(param.names)
        param.requires_grad = False
    
    fc_inputs = net.fc.in_features #获得fc特征层的输入
    net.fc = nn.Sequential(         #重新定义特征层,根据需要可以添加自己想要的Linear层
        nn.Linear(fc_inputs, 100),  #多加几层都没关系
        nn.LogSoftmax(dim=1)
    )
    
    net = net.to('cuda')
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
    
    # Training
    def train(epoch):
        # print('\nEpoch: %d' % epoch)
        net.train()
        train_loss = 0
        correct = 0
        total = 0
        for batch_idx, (inputs, targets) in enumerate(trainloader):
            inputs, targets = inputs.to('cuda'), targets.to('cuda')
            optimizer.zero_grad()
            outputs = net(torch.squeeze(inputs, 1))
            loss = criterion(outputs, targets)
            loss.backward()
            optimizer.step()
            train_loss += loss.item()
            _, predicted = outputs.max(1)
            total += targets.size(0)
            correct += predicted.eq(targets).sum().item()
    
            print(batch_idx+1,'/', len(trainloader),'epoch: %d' % epoch, '| Loss: %.3f | Acc: %.3f%% (%d/%d)'
                % (train_loss/(batch_idx+1), 100.*correct/total, correct, total))
    
    def test(epoch):
        global best_acc
        net.eval()
        test_loss = 0
        correct = 0
        total = 0
        with torch.no_grad():
            for batch_idx, (inputs, targets) in enumerate(testloader):
                inputs, targets = inputs.to('cuda'), targets.to('cuda')
                outputs = net(torch.squeeze(inputs, 1))
                loss = criterion(outputs, targets)
                test_loss += loss.item()
                _, predicted = outputs.max(1)
                total += targets.size(0)
                correct += predicted.eq(targets).sum().item()
                print(batch_idx,'/',len(testloader),'epoch: %d'% epoch, 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
                    % (test_loss/(batch_idx+1), 100.*correct/total, correct, total))
    
    for epoch in range(100): #设置成100循环
        train(epoch)
    torch.save(net.state_dict(),'resnet34cifar100.pkl') #训练完成后保存模型,供下次继续训练使用
    print('begin  test ')
    
    for epoch in range(5): #测试5次
        test(epoch)

    上面得到的识别率不是很高估计就是60%左右 

    第二部、将保存的模型加载进来,降低学习速率继续学习(降低到 lr=0.001) ,注意代码有变化,仍然要固定前面的Conv2d层的参数,放开最后的fc层的参数反向学习功能。核心代码看下图:

    完整代码如下:

    import torch
    import torch.nn as nn
    import torch.optim as optim
    import torchvision
    from torchvision import transforms
    from torchvision import models
    from torchvision.models.resnet import resnet34
    from torchvision.transforms.transforms import Resize
    
    transform = transforms.Compose([
    transforms.Resize(224),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225]) #默认的标准化参数
    ])
    
    
    trainset = torchvision.datasets.CIFAR100(root='./data', train=True, download=True, transform=transform)
    trainloader = torch.utils.data.DataLoader(trainset, batch_size=200, shuffle=True)
    
    testset = torchvision.datasets.CIFAR100(root='./data', train=False, download=True, transform=transform)
    testloader = torch.utils.data.DataLoader(testset, batch_size=200, shuffle=False)
    
    net=models.resnet34(pretrained=False) # pretrained=False or True 不重要
    fc_inputs = net.fc.in_features # 保持与前面第一步中的代码一致
    net.fc = nn.Sequential(         #
        nn.Linear(fc_inputs, 100),  #
        nn.LogSoftmax(dim=1)
    )
    
    net.load_state_dict(torch.load('resnet34cifar100.pkl')) #装载上传训练的参数
    mydict=net.state_dict()
    #for k,v in mydict.items():    
    #    print('k===',k,'||||,v==',v)
    
    models=net.modules()
    for p in models:
        if p._get_name()!='Linear':
            print(p._get_name())
            p.requires_grad_=False
    
    net = net.to('cuda')
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) #减小 lr
    
    # Training
    def train(epoch):
        # print('\nEpoch: %d' % epoch)
        net.train()
        train_loss = 0
        correct = 0
        total = 0
        for batch_idx, (inputs, targets) in enumerate(trainloader):
            inputs, targets = inputs.to('cuda'), targets.to('cuda')
            optimizer.zero_grad()
            outputs = net(torch.squeeze(inputs, 1))
            loss = criterion(outputs, targets)
            loss.backward()
            optimizer.step()
    
            train_loss += loss.item()
            _, predicted = outputs.max(1)
            total += targets.size(0)
            correct += predicted.eq(targets).sum().item()
    
            print(batch_idx+1,'/', len(trainloader),'epoch: %d' % epoch, '| Loss: %.3f | Acc: %.3f%% (%d/%d)'
                % (train_loss/(batch_idx+1), 100.*correct/total, correct, total))
    
    def test(epoch):
        global best_acc
        net.eval()
        test_loss = 0
        correct = 0
        total = 0
        with torch.no_grad():
            for batch_idx, (inputs, targets) in enumerate(testloader):
                inputs, targets = inputs.to('cuda'), targets.to('cuda')
                outputs = net(torch.squeeze(inputs, 1))
                loss = criterion(outputs, targets)
    
                test_loss += loss.item()
                _, predicted = outputs.max(1)
                total += targets.size(0)
                correct += predicted.eq(targets).sum().item()
    
                print(batch_idx,'/',len(testloader),'epoch: %d'% epoch, 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
                    % (test_loss/(batch_idx+1), 100.*correct/total, correct, total))
    
    
    for epoch in range(30):
        train(epoch)
    torch.save(net.state_dict(),'resnet34cifar100.pkl') #训练完成后保存模型,供下次继续训练使用
    
    print('begin  test ')
    for epoch in range(5):
        test(epoch)

    第三步,如果loss没有接近0或者训练精度 没有接近100,继续第二步(lr=0.001,或者lr=0.0008)。一般最多两轮足够了,下面看看输出结果

    上图是测试10000个样本的正确识别率,最后一行是最终的识别率达到了80.06%,测试5次基本都是上了80%。

    需要说明的是,本例子中只学习了最后的fc层的参数,其余的卷积层Conv2d的参数全部固定了。如果你的设备允许,还可以开放更多的层来学习,比如抽取某层(或某些)的Conv2d和fc一起学习(将需要学习层的参数设置成requires_grad = True)。只进行了简单的学习策略就将准确率提高到了80%,如果学习更多的层,相信一定还会显著提升网络的性能。大家还有更好的方法来提升性能欢迎交流!

    展开全文
  • 环境: tensorflow 2.1 最好用GPU 模型: Resnet:把前一层的数据直接加到下一层里。...训练集上准确率:97.11%左右 验证集上准确率:90.22%左右 测试集上准确率:88.6% 训练时间在GPU上:一小时多 权重大小:21
  • 【深度学习】Cifar-10-探究不同的改进策略对分类准确率提高【深度学习】Cifar-10-探究不同的改进策略对分类准确率提高
  • 基于TensorFlow2利用ResNet18+SENet 实现cifar10分类 training准确率95.66%,test准确率90.77%
  • 老师要求使用resnet18或者resnet50将cifar10训练到精度到达95%,试过了网上其他很多方法,发现精度最高的是在预处理化的时候,图片resize到32,32,并且padding=4进行填充,并且优化器用SGD,准确率最高,但是准确率...

    目录

    前言

    因为课程需要,老师要求使用resnet18或者resnet50将cifar10训练到精度到达95%,试过了网上其他很多方法,发现精度最高的是在预处理化的时候,图片resize到32,32,并且padding=4进行填充,并且优化器用SGD,准确率最高,但是准确率最高也是到88.8%。后来和上课的同学讨论,发现先将图片resize到(224,224)再进行翻转等操作可以稳定实现超过95%的准确率。大概训练4-5个epoch就可以了。
    注:进行了迁移学习,加载了预训练模型

    代码

    废话不多说,直接上代码
    注:因为CPU训练速度太慢了,尤其是将图片拉到224,224这么大,故最好使用GPU训练。并且使用了tensorboard将损失给画了出来

    import torch
    from torch.utils.tensorboard.summary import image
    import torchvision
    import torch.nn.functional as F
    import torch.nn as nn
    import torchvision.transforms as transforms
    import torch.optim as optim
    
    
    from torch.utils.tensorboard import SummaryWriter
    myWriter = SummaryWriter('./tensorboard/log/')
    
    
    
    myTransforms = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.RandomHorizontalFlip(p=0.5),
        transforms.ToTensor(),
        transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))])
    
    
    #  load
    train_dataset = torchvision.datasets.CIFAR10(root='./cifar-10-python/', train=True, download=True, transform=myTransforms)
    train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True, num_workers=0)
    
    test_dataset = torchvision.datasets.CIFAR10(root='./cifar-10-python/', train=False, download=True, transform=myTransforms)
    test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=4, shuffle=True, num_workers=0)
    
    
    
    # 定义模型
    myModel = torchvision.models.resnet50(pretrained=True)
    # 将原来的ResNet18的最后两层全连接层拿掉,替换成一个输出单元为10的全连接层
    inchannel = myModel.fc.in_features
    myModel.fc = nn.Linear(inchannel, 10)
    
    # 损失函数及优化器
    # GPU加速
    myDevice = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    myModel = myModel.to(myDevice)
    
    
    learning_rate=0.001
    myOptimzier = optim.SGD(myModel.parameters(), lr = learning_rate, momentum=0.9)
    myLoss = torch.nn.CrossEntropyLoss()
    
    for _epoch in range(10):
        training_loss = 0.0
        for _step, input_data in enumerate(train_loader):
            image, label = input_data[0].to(myDevice), input_data[1].to(myDevice)   # GPU加速
            predict_label = myModel.forward(image)
           
            loss = myLoss(predict_label, label)
    
            myWriter.add_scalar('training loss', loss, global_step = _epoch*len(train_loader) + _step)
    
            myOptimzier.zero_grad()
            loss.backward()
            myOptimzier.step()
    
            training_loss = training_loss + loss.item()
            if _step % 10 == 0 :
                print('[iteration - %3d] training loss: %.3f' % (_epoch*len(train_loader) + _step, training_loss/10))
                training_loss = 0.0
                print()
        correct = 0
        total = 0
        #torch.save(myModel, 'Resnet50_Own.pkl') # 保存整个模型
        myModel.eval()
        for images,labels in test_loader:
            # GPU加速
            images = images.to(myDevice)
            labels = labels.to(myDevice)     
            outputs = myModel(images)   # 在非训练的时候是需要加的,没有这句代码,一些网络层的值会发生变动,不会固定
            numbers,predicted = torch.max(outputs.data,1)
            total += labels.size(0)
            correct += (predicted==labels).sum().item()
    
        print('Testing Accuracy : %.3f %%' % ( 100 * correct / total))
        myWriter.add_scalar('test_Accuracy',100 * correct / total)
    
    展开全文
  • CIFAR-100上的VGG-16 在CIFAR-100上训练的VGG网(具有batchnorm和dropout)。 您可以通过更改数据加载器类中的一行来轻松修改此代码以在CIFAR-10上进行训练。 在不增加数据的情况下达到约64%的准确性。 该数据集上...
  • CIFAR-100数据集(用作100类的图像分类) 这个数据集和CIFAR-10相比,它具有100个类,大约600张/类,每类500张训练,500张测试; 这100类又可以grouped成20个大类。 此数据集也有三个版本的...

    CIFAR-100数据集(用作100类的图像分类)



    这个数据集和CIFAR-10相比,它具有100个类,大约600张/类,每类500张训练,500张测试;


    这100类又可以grouped成20个大类。


    此数据集也有三个版本的数据可供下载: Python版本(161MB),MATLAB版本(175MB),二值版本(161MB)。




    数据集下载



    Python版本



    下载地址: CIFAR-100 python version



    MATLAB版本



    下载地址:CIFAR-100Matlab version



    二值化版本



    下载地址:CIFAR-100binary version (suitable for C programs)










    展开全文
  • pytorch在cifar100上练习 要求 这是我的实验资料 python3.6 pytorch1.6.0 + cu101 张量板2.2.2(可选) 用法 1.输入目录 $ cd pytorch-cifar100 2.数据集 我将使用来自torchvision的cifar100数据集,因为它更方便...
  • resnet在cifar10和100中精度是top1还是top5 resnext-widenet-densenet这些文章都说了在cifar10和100中的结果,但是并没有提及是top1还是top5,这些网络在imagenet和ILSVRC这些数据集上就明确说明了top1和top5精确度...
  • printoptions(threshold=np.inf) cifar10 = tf.keras.datasets.cifar10 (x_train, y_train), (x_test, y_test) = cifar10.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 class Baseline(Model): ...
  • 基于Tensorflow的Resnet程序实现(CIFAR10准确率为91.5%)

    万次阅读 多人点赞 2018-09-07 14:57:28
    在上一篇博文中我重写了Tensorflow中的CNN的实现,对于CIFAR10的测试集的准确率为85%左右。在这个实现中,用到了2个卷积层和2个全连接层。具体的模型架构如下:  为了进一步提高准确率,我们可以采用一些更先进...

           在上一篇博文中我重写了Tensorflow中的CNN的实现,对于CIFAR10的测试集的准确率为85%左右。在这个实现中,用到了2个卷积层和2个全连接层。具体的模型架构如下:

           为了进一步提高准确率,我们可以采用一些更先进的模型架构,其中一种很出名的架构就是RESNET,残差网络。这是Kaiming大神在2015年的论文"Deep Residual Learning for Image Recognition"中提到的一种网络架构,其思想是观察到一般的神经网络结构随着层数的加深,训练的误差反而会增大,因此引入了残差这个概念,把上一层的输出直接和下一层的输出相加,如下图所示。这样理论上随着网络层数的加深,引入这个结构并不会使得误差比浅层的网络更大,因为随着参数的优化,如果浅层网络已经逼近了最优值,那么之后的网络层相当于一个恒等式,即每一层的输入和输出相等,因此更深的层数不会额外增加训练误差。

           在2016年,Kaiming大神发布了另一篇论文“Identity Mappings in Deep Residual Networks”,在这个论文中对Resnet的网络结构作了进一步的改进。改进前和改进后的resnet网络结构如下图所示,按照论文的解释,改进后的结构可以在前向和后向更好的传递残差,因此能取得更好的优化效果:

           在Tensorflow的官方模型中,已经带了一个Resnet的实现,用这个模型训练,在110层的深度下,可以达到CIFAR10测试集92%左右的准确率。但是,这个代码实在是写的比较难读,做了很多辅助功能的封装,每次看代码都是要跳来跳去的看,实在是很不方便。为此我也再次改写了这个代码,按照Kaiming论文介绍的方式来进行模型的构建,在110层的网络层数下,可以达到91%左右的准确率,和官方模型的很接近。

           具体的代码分为两部分,我把构建Resnet模型的代码单独封装在一个文件中。如以下的代码,这个代码里面的_resnet_block_v1和_resnet_block_v2分别对应了上图的两种不同的resnet结构:

    import tensorflow as tf
    
    def _resnet_block_v1(inputs, filters, stride, projection, stage, blockname, TRAINING):
        # defining name basis
        conv_name_base = 'res' + str(stage) + blockname + '_branch'
        bn_name_base = 'bn' + str(stage) + blockname + '_branch'
    
        with tf.name_scope("conv_block_stage" + str(stage)):
            if projection:
                shortcut = tf.layers.conv2d(inputs, filters, (1,1), 
                                            strides=(stride, stride), 
                                            name=conv_name_base + '1', 
                                            kernel_initializer=tf.contrib.layers.variance_scaling_initializer(), 
                                            reuse=tf.AUTO_REUSE, padding='same', 
                                            data_format='channels_first')
                shortcut = tf.layers.batch_normalization(shortcut, axis=1, name=bn_name_base + '1', 
                                                         training=TRAINING, reuse=tf.AUTO_REUSE)
            else:
                shortcut = inputs
    
            outputs = tf.layers.conv2d(inputs, filters,
                                      kernel_size=(3, 3),
                                      strides=(stride, stride), 
                                      kernel_initializer=tf.contrib.layers.variance_scaling_initializer(), 
                                      name=conv_name_base+'2a', reuse=tf.AUTO_REUSE, padding='same', 
                                      data_format='channels_first')
            outputs = tf.layers.batch_normalization(outputs, axis=1, name=bn_name_base+'2a', 
                                                    training=TRAINING, reuse=tf.AUTO_REUSE)
            outputs = tf.nn.relu(outputs)
    	
            outputs = tf.layers.conv2d(outputs, filters,
                                      kernel_size=(3, 3),
                                      strides=(1, 1), 
                                      kernel_initializer=tf.contrib.layers.variance_scaling_initializer(), 
                                      name=conv_name_base+'2b', reuse=tf.AUTO_REUSE, padding='same', 
                                      data_format='channels_first')
            outputs = tf.layers.batch_normalization(outputs, axis=1, name=bn_name_base+'2b', 
                                                    training=TRAINING, reuse=tf.AUTO_REUSE)
            outputs = tf.add(shortcut, outputs)
            outputs = tf.nn.relu(outputs)								  
        return outputs
    	
    def _resnet_block_v2(inputs, filters, stride, projection, stage, blockname, TRAINING):
        # defining name basis
        conv_name_base = 'res' + str(stage) + blockname + '_branch'
        bn_name_base = 'bn' + str(stage) + blockname + '_branch'
    
        with tf.name_scope("conv_block_stage" + str(stage)):
            shortcut = inputs
            outputs = tf.layers.batch_normalization(inputs, axis=1, name=bn_name_base+'2a', 
                                                    training=TRAINING, reuse=tf.AUTO_REUSE)
            outputs = tf.nn.relu(outputs)		
            if projection:
                shortcut = tf.layers.conv2d(outputs, filters, (1,1), 
                                            strides=(stride, stride), 
                                            name=conv_name_base + '1', 
                                            kernel_initializer=tf.contrib.layers.variance_scaling_initializer(), 
                                            reuse=tf.AUTO_REUSE, padding='same', 
                                            data_format='channels_first')
                shortcut = tf.layers.batch_normalization(shortcut, axis=1, name=bn_name_base + '1', 
                                                         training=TRAINING, reuse=tf.AUTO_REUSE)
    								
            outputs = tf.layers.conv2d(outputs, filters,
                                      kernel_size=(3, 3),
                                      strides=(stride, stride), 
                                      kernel_initializer=tf.contrib.layers.variance_scaling_initializer(), 
                                      name=conv_name_base+'2a', reuse=tf.AUTO_REUSE, padding='same', 
                                      data_format='channels_first')
            
            outputs = tf.layers.batch_normalization(outputs, axis=1, name=bn_name_base+'2b', 
                                                    training=TRAINING, reuse=tf.AUTO_REUSE)
            outputs = tf.nn.relu(outputs)
            outputs = tf.layers.conv2d(outputs, filters,
                                      kernel_size=(3, 3),
                                      strides=(1, 1),
                                      kernel_initializer=tf.contrib.layers.variance_scaling_initializer(),
                                      name=conv_name_base+'2b', reuse=tf.AUTO_REUSE, padding='same', 
                                      data_format='channels_first')
    
            outputs = tf.add(shortcut, outputs)
        return outputs
    
    def inference(images, training, filters, n, ver):
        """Construct the resnet model
    
        Args:
          images: [batch*channel*height*width]
    	  training: boolean
    	  filters: integer, the filters of the first resnet stage, the next stage will have filters*2
    	  n: integer, how many resnet blocks in each stage, the total layers number is 6n+2
    	  ver: integer, can be 1 or 2, for resnet v1 or v2
        Returns:
          Tensor, model inference output
        """
        #Layer1 is a 3*3 conv layer, input channels are 3, output channels are 16
        inputs = tf.layers.conv2d(images, filters=16, kernel_size=(3, 3), strides=(1, 1), 
                                  name='conv1', reuse=tf.AUTO_REUSE, padding='same', data_format='channels_first')
    
        #no need to batch normal and activate for version 2 resnet.
        if ver==1:
            inputs = tf.layers.batch_normalization(inputs, axis=1, name='bn_conv1',
                                                   training=training, reuse=tf.AUTO_REUSE)
            inputs = tf.nn.relu(inputs)
    
        for stage in range(3):
            stage_filter = filters*(2**stage)
            for i in range(n):
                stride = 1
                projection = False
                if i==0 and stage>0:
                    stride = 2
                    projection = True
                if ver==1:
                    inputs = _resnet_block_v1(inputs, stage_filter, stride, projection, 
    				                          stage, blockname=str(i), TRAINING=training)
                else:
                    inputs = _resnet_block_v2(inputs, stage_filter, stride, projection, 
    				                          stage, blockname=str(i), TRAINING=training)
    
        #only need for version 2 resnet.
        if ver==2:
            inputs = tf.layers.batch_normalization(inputs, axis=1, name='pre_activation_final_norm', 
                                                   training=training, reuse=tf.AUTO_REUSE)
            inputs = tf.nn.relu(inputs)
    
        axes = [2, 3]
        inputs = tf.reduce_mean(inputs, axes, keep_dims=True)
        inputs = tf.identity(inputs, 'final_reduce_mean')
    
        inputs = tf.reshape(inputs, [-1, filters*(2**2)])
        inputs = tf.layers.dense(inputs=inputs, units=10, name='dense1', reuse=tf.AUTO_REUSE)
        return inputs

           另外一部分的代码就是和Cifar10的处理相关的,其中Cifar10的50000张图片中选取45000张作为训练集,另外5000张作为验证集,测试的10000张图片都作为测试集。在98层的网络深度下,测试集的准确度可以达到92%左右.

    import tensorflow as tf
    import numpy as np
    import os
    import resnet_model
    
    #Construct the filenames that include the train cifar10 images
    folderPath = 'cifar-10-batches-bin/'
    filenames = [os.path.join(folderPath, 'data_batch_%d.bin' % i) for i in xrange(1,6)]
    
    #Define the parameters of the cifar10 image
    imageWidth = 32
    imageHeight = 32
    imageDepth = 3
    label_bytes = 1
    
    #Define the train and test batch size
    batch_size = 100
    test_batch_size = 100
    valid_batch_size = 100
    
    #Calulate the per image bytes and record bytes
    image_bytes = imageWidth * imageHeight * imageDepth
    record_bytes = label_bytes + image_bytes
    
    #Construct the dataset to read the train images
    dataset = tf.data.FixedLengthRecordDataset(filenames, record_bytes)
    dataset = dataset.shuffle(50000)
    
    #Get the first 45000 records as train dataset records
    train_dataset = dataset.take(45000)
    train_dataset = train_dataset.batch(batch_size)
    train_dataset = train_dataset.repeat(300)
    iterator = train_dataset.make_initializable_iterator()
    
    #Get the remain 5000 records as valid dataset records
    valid_dataset = dataset.skip(45000)
    valid_dataset = valid_dataset.batch(valid_batch_size)
    validiterator = valid_dataset.make_initializable_iterator()
    
    #Construct the dataset to read the test images
    testfilename = os.path.join(folderPath, 'test_batch.bin')
    testdataset = tf.data.FixedLengthRecordDataset(testfilename, record_bytes)
    testdataset = testdataset.batch(test_batch_size)
    testiterator = testdataset.make_initializable_iterator()
    
    #Decode the train records from the iterator
    record = iterator.get_next()
    record_decoded_bytes = tf.decode_raw(record, tf.uint8)
    
    #Get the labels from the records
    record_labels = tf.slice(record_decoded_bytes, [0, 0], [batch_size, 1])
    record_labels = tf.cast(record_labels, tf.int32)
    
    #Get the images from the records
    record_images = tf.slice(record_decoded_bytes, [0, 1], [batch_size, image_bytes])
    record_images = tf.reshape(record_images, [batch_size, imageDepth, imageHeight, imageWidth])
    record_images = tf.transpose(record_images, [0, 2, 3, 1])
    record_images = tf.cast(record_images, tf.float32)
    
    #Decode the records from the valid iterator
    validrecord = validiterator.get_next()
    validrecord_decoded_bytes = tf.decode_raw(validrecord, tf.uint8)
    
    #Get the labels from the records
    validrecord_labels = tf.slice(validrecord_decoded_bytes, [0, 0], [valid_batch_size, 1])
    validrecord_labels = tf.cast(validrecord_labels, tf.int32)
    validrecord_labels = tf.reshape(validrecord_labels, [-1])
    
    #Get the images from the records
    validrecord_images = tf.slice(validrecord_decoded_bytes, [0, 1], [valid_batch_size, image_bytes])
    validrecord_images = tf.cast(validrecord_images, tf.float32)
    validrecord_images = tf.reshape(validrecord_images, 
                                   [valid_batch_size, imageDepth, imageHeight, imageWidth])
    validrecord_images = tf.transpose(validrecord_images, [0, 2, 3, 1])
    
    #Decode the test records from the iterator
    testrecord = testiterator.get_next()
    testrecord_decoded_bytes = tf.decode_raw(testrecord, tf.uint8)
    
    #Get the labels from the records
    testrecord_labels = tf.slice(testrecord_decoded_bytes, [0, 0], [test_batch_size, 1])
    testrecord_labels = tf.cast(testrecord_labels, tf.int32)
    testrecord_labels = tf.reshape(testrecord_labels, [-1])
    
    #Get the images from the records
    testrecord_images = tf.slice(testrecord_decoded_bytes, [0, 1], [test_batch_size, image_bytes])
    testrecord_images = tf.cast(testrecord_images, tf.float32)
    testrecord_images = tf.reshape(testrecord_images, 
                                   [test_batch_size, imageDepth, imageHeight, imageWidth])
    testrecord_images = tf.transpose(testrecord_images, [0, 2, 3, 1])
    
    #Random crop the images after pad each side with 4 pixels
    distorted_images = tf.image.resize_image_with_crop_or_pad(record_images, 
                                                              imageHeight+8, imageWidth+8)
    distorted_images = tf.random_crop(distorted_images, size = [batch_size, imageHeight, imageHeight, 3])
    
    #Unstack the images as the follow up operation are on single train image
    distorted_images = tf.unstack(distorted_images)
    for i in xrange(len(distorted_images)):
        distorted_images[i] = tf.image.random_flip_left_right(distorted_images[i])
        distorted_images[i] = tf.image.random_brightness(distorted_images[i], max_delta=63)
        distorted_images[i] = tf.image.random_contrast(distorted_images[i], lower=0.2, upper=1.8)
        distorted_images[i] = tf.image.per_image_standardization(distorted_images[i])
        
    #Stack the images
    distorted_images = tf.stack(distorted_images)
    
    #transpose to set the channel first
    distorted_images = tf.transpose(distorted_images, perm=[0, 3, 1, 2])
    
    #Unstack the images as the follow up operation are on single image
    validrecord_images = tf.unstack(validrecord_images)
    for i in xrange(len(validrecord_images)):
        validrecord_images[i] = tf.image.per_image_standardization(validrecord_images[i])
        
    #Stack the images
    validrecord_images = tf.stack(validrecord_images)
    
    #transpose to set the channel first
    validrecord_images = tf.transpose(validrecord_images, perm=[0, 3, 1, 2])
    
    #Unstack the images as the follow up operation are on single image
    testrecord_images = tf.unstack(testrecord_images)
    for i in xrange(len(testrecord_images)):
        testrecord_images[i] = tf.image.per_image_standardization(testrecord_images[i])
        
    #Stack the images
    testrecord_images = tf.stack(testrecord_images)
    
    #transpose to set the channel first
    testrecord_images = tf.transpose(testrecord_images, perm=[0, 3, 1, 2])
    
    global_step = tf.Variable(0, trainable=False)
    boundaries = [10000, 15000, 20000, 25000]
    values = [0.1, 0.05, 0.01, 0.005, 0.001]
    learning_rate = tf.train.piecewise_constant(global_step, boundaries, values)
    weight_decay = 2e-4
    filters = 16  #the first resnet block filter number
    n = 5  #the basic resnet block number, total network layers are 6n+2
    ver = 2   #the resnet block version
    
    #Get the inference logits by the model
    result = resnet_model.inference(distorted_images, True, filters, n, ver)
    
    #Calculate the cross entropy loss
    cross_entropy = tf.losses.sparse_softmax_cross_entropy(labels=record_labels, logits=result)
    cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
    
    #Add the l2 weights to the loss
    #Add weight decay to the loss.
    l2_loss = weight_decay * tf.add_n(
        # loss is computed using fp32 for numerical stability.
        [tf.nn.l2_loss(tf.cast(v, tf.float32)) for v in tf.trainable_variables()])
    tf.summary.scalar('l2_loss', l2_loss)
    loss = cross_entropy_mean + l2_loss
    
    #Define the optimizer
    optimizer = tf.train.MomentumOptimizer(learning_rate, momentum=0.9)
    
    #Relate to the batch normalization
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    with tf.control_dependencies(update_ops):
        opt_op = optimizer.minimize(loss, global_step)
    
    valid_accuracy = tf.placeholder(tf.float32)
    test_accuracy = tf.placeholder(tf.float32)
    tf.summary.scalar("valid_accuracy", valid_accuracy)
    tf.summary.scalar("test_accuracy", test_accuracy)
    tf.summary.scalar("learning_rate", learning_rate)
    
    validresult = tf.argmax(resnet_model.inference(validrecord_images, False, filters, n, ver), axis=1)
    testresult = tf.argmax(resnet_model.inference(testrecord_images, False, filters, n, ver), axis=1)
    
    #Create the session and run the graph
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    sess.run(iterator.initializer)
    
    #Merge all the summary and write
    summary_op = tf.summary.merge_all()
    train_filewriter = tf.summary.FileWriter('train/', sess.graph)
    
    step = 0
    while(True):
        try:
            lossValue, lr, _ = sess.run([loss, learning_rate, opt_op])
            if step % 100 == 0:
                print "step %i: Learning_rate: %f Loss: %f" %(step, lr, lossValue)
            if step % 1000 == 0:
                saver.save(sess, 'model/my-model', global_step=step)
                truepredictNum = 0
                sess.run([testiterator.initializer, validiterator.initializer])
                accuracy1 = 0.0
                accuracy2 = 0.0
                while(True):
                    try:
                        predictValue, testValue = sess.run([validresult, validrecord_labels])
                        truepredictNum += np.sum(predictValue==testValue)
                    except tf.errors.OutOfRangeError:
                        print "valid correct num: %i" %(truepredictNum)
                        accuracy1 = truepredictNum / 5000.0
                        break
                truepredictNum = 0
                while(True):
                    try:
                        predictValue, testValue = sess.run([testresult, testrecord_labels])
                        truepredictNum += np.sum(predictValue==testValue)
                    except tf.errors.OutOfRangeError:
                        print "test correct num: %i" %(truepredictNum)
                        accuracy2 = truepredictNum / 10000.0
                        break
                summary = sess.run(summary_op, feed_dict={valid_accuracy: accuracy1, test_accuracy: accuracy2})
                train_filewriter.add_summary(summary, step)
            step += 1
        except tf.errors.OutOfRangeError:
            break

     

    展开全文
  • CIFAR-10和CIFAR-100被标记为8000万个微小图像数据集的子集。他们由Alex Krizhevsky,Vinod Nair和Geoffrey Hinton收集。 CIFAR-10数据集 CIFAR-10数据集由10个类的60000个32x32彩色图像组成,每个类有6000个图像...
  • 基于tensorflow搭建Resnet18对Cifar10测试集准确率90%,仅用了数据增强和损失函数优化方案,简单易懂,内含源码以及训练好了的模型。继续调节参数可以进一步增高准确率,支持交流学习,勿喷
  • cifar100(20个任务)测试结果
  • 1.数据加载及预处理 # 数据预处理 将数据进行类型转换 def process(x, y): x = tf.cast(x, dtype=tf.float32) / 255. ...(x, y), (x_test, y_test) = datasets.cifar100.load_data() y = tf.squeeze(y, axi
  • 在构建深度学习过程最重要的步骤为: (1)模型 (2)损失函数 (3)优化器选择 本博客将会从上述步骤,以CIFAR10数据集为代表,利用keras展开描述整体过程;
  • cifar 10 最高正确

    千次阅读 2017-04-18 17:23:20
    http://rodrigob.github.io/are_we_there_yet/build/classification_datasets_results.html 这个网站里有MNIST数据集,cifar-10数据集,cifar-100和imagenet数据集正确排行榜
  • 卷积神经网络训练CIFAR100

    千次阅读 2021-01-17 15:57:27
    CIFAR-10数据集由10个类的60000个32x32彩色图像组成,每个类有6000个图像。有50000个训练图像和10000个测试图像。 ​ 数据集分为五个训练批次和一个测试批次,每个批次有10000个图像。测试批次包含来自每个类别的...
  • “前文我们使用libtorch实现的Resnet34网络对Cifar-10进行分类,测试集的分类准确率仅有74.95%,本文我们在前文的基础上做了一些改进,使得测试集的分类准确率达到94...
  • 2,2,2],num_class=100) 然后我们有cifar100来练练这resnet 注意:千万不要把test数据给train了,这会污染数据,没有办法评估模型的泛化能力了,在比赛中这是作弊 import tensorflow as tf from tensorflow import ...
  • 如何用Keras从头开始训练一个在CIFAR10上准确率达到89%的模型CIFAR10 是一个用于图像识别的经典数据集,包含了10个类型的图片。该数据集有60000张尺寸为 32 x 32 的彩色图片,其中50000张用于训练,10000张用于测试...
  • 此代码是在以前cifar-10的基础上对数据进行优化(随机翻转、调整光照、改变对比度、白化、使用均值或方差的分布信息、保存参数等等)来提高准确率 import tensorflow as tf import numpy as np import os import ...
  • examples_for_training,is_enhance=True) self.test=Batcher(xtest,ytest,self.num_examples_for_test,is_enhance=False) cifar10=cifar10_reader(data_path,5000,5000) #可以读入一个子集 学习策略:指数衰减+预热...
  • 目前测试在cifar10上训练的准确率至少为91%(由于时间有限,暂时没有做足够测试,只训练了30个epoch) 我的实现中用全局平均池化代替第一层全连接层,第二三层全连接用1x1卷积来代替 每组卷积之后添加BN层,并且卷积通道...
  • (梳理)用Tensorflow实现SE-ResNet(SENet ResNet ResNeXt VGG16)的数据输入,训练,预测的完整代码框架(cifar10准确率90%) balabalabala 之前的代码感觉还是太乱了,我刚开始学的时候还是用的tensorflow1.2 现在2.5都要...
  • 文章目录炼丹笔记一——基于TensorFlow的vgg16的cifar10和100超参数试错先说数据集:网络结构:源代码:先说结构的问题:关于交叉熵的问题关于动量系数的问题:关于学习关于是否图像增强记录一些原始数据吧:实验...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 7,476
精华内容 2,990
关键字:

cifar100准确率