当前位置:   article > 正文

【tensorflow】Resnet+CIFAR10图像分类_cifar10数据集tensorflow1 resnet

cifar10数据集tensorflow1 resnet
  • Resnet的主要结构:
    在这里插入图片描述左图是何大神提出的原始版本,右图是第二年大神论文改进后的版本;

  • 代码:
    加载数据集和处理:

import tensorflow as tf
import numpy as np
import math
import timeit
import matplotlib.pyplot as plt
%matplotlib inline
from cs231n.data_utils import load_CIFAR10

def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=10000):
    """
    Load the CIFAR-10 dataset from disk and perform preprocessing to prepare
    it for the two-layer neural net classifier. These are the same steps as
    we used for the SVM, but condensed to a single function.  
    """
    # Load the raw CIFAR-10 data
    cifar10_dir = 'cs231n/datasets/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]

    # Normalize the data: subtract the mean image
    mean_image = np.mean(X_train, axis=0)
    X_train -= mean_image
    X_val -= mean_image
    X_test -= mean_image

    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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

模型代码:

# Feel free to play with this cell
tf.reset_default_graph()

X = tf.placeholder(tf.float32, [None, 32, 32, 3])
y = tf.placeholder(tf.int64, [None])
is_training = tf.placeholder(tf.bool)
filters = 16 #first resnet block filter number
ver = 2 #resnet version
n = 5 #the basic resnet block number,total networks layers are 6n+2

#two resnet structure
def _resnet_block_v1(inputs,filters,stride,projection,stage,blockname,is_training):
    #define name
    conv_name_base = 'result'+str(stage)+blockname+'_branch'
    bn_name_base = 'bn'+str(stage)+blockname+'_branch'
    
    with tf.name_scope('conv_name_stage'+str(stage)):
        shortcut = inputs
        if projection:
            shortcut = tf.layers.conv2d(shortcut,filters,(1,1),(stride,stride),name=conv_name_base+'1',padding='same')
            shortcut = tf.layers.batch_normalization(shortcut,name=bn_name_base+'1',training=is_training)
        output = tf.layers.conv2d(inputs, filters,(3,3),strides=(stride,stride),name=conv_name_base+'2a',padding='same')
        output = tf.layers.batch_normalization(output,training=is_training,name=bn_name_base+'2a')
        output = tf.nn.relu(output)
        
        output = tf.layers.conv2d(output,filters,(3,3),strides=(1,1),name= conv_name_base+'2b',padding='same')
        output = tf.layers.batch_normalization(output,training=is_training,name=bn_name_base+'2b')
        output = tf.add(shortcut,output)
        
        output = tf.nn.relu(output)
    
    return output

def _resnet_block_v2(inputs,filters,stride,projection,stage,blockname,is_training):
    conv_name_base = 'result'+str(stage)+blockname+'_branch'
    bn_name_base = 'bn'+str(stage)+blockname+'_branch'
    
    with tf.name_scope('conv_name_stage'+str(stage)):
        shortcut = inputs
        #for dimension same(shortcut+output) and unsampling
        if projection:
            shortcut = tf.layers.conv2d(shortcut,filters,(1,1),(stride,stride),name=conv_name_base+'1',padding='same')
            shortcut = tf.layers.batch_normalization(shortcut,name=bn_name_base+'1',training=is_training)
        output = tf.layers.batch_normalization(inputs,training=is_training,name=bn_name_base+'2a')
        output = tf.nn.relu(output)
        output = tf.layers.conv2d(output,filters,(3,3),strides=(stride,stride),name=conv_name_base+'2a',padding='same')
        output = tf.layers.batch_normalization(output,training=is_training,name=bn_name_base+'2b')
        
        output = tf.nn.relu(output)
        output = tf.layers.conv2d(output,filters,(3,3),(1,1),name=conv_name_base+'2b',padding='same')
        
        output = tf.add(shortcut,output)
    return output

def resnet_model(X,y,is_training,filters,n,ver):
    y_out = tf.layers.conv2d(X,16,(3,3),(1,1),name = 'conv_1',padding='same')
    
    if ver == 1:
        y_out = tf.layers.batch_normalization(y_out,name='bn_conv1',training=is_training)
        y_out = tf.nn.relu(y_out)
    
    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:
                y_out = _resnet_block_v1(y_out,stage_filter,stride,projection,stage,blockname=str(i),is_training=is_training)
            else:
                y_out = _resnet_block_v2(y_out,stage_filter,stride,projection,stage,blockname=str(i),is_training=is_training)
                
                
    if ver == 2:
        y_out = tf.layers.batch_normalization(y_out,name='pre_activation_final_norm',training=is_training)
        y_out = tf.nn.relu(y_out)
        
    axes = [2,3]
    y_out = tf.reduce_mean(y_out,axes,keep_dims=True)
    y_out = tf.identity(y_out,'finl_reduce_mean')
    
    flatten =  tf.contrib.layers.flatten(y_out)
    y_out = tf.layers.dense(flatten,10)
    
    return y_out

print('my model')
y_out = resnet_model(X,y,is_training,filters,n,ver)
print('come on')
mean_loss = None
optimizer = None

print('loss')
total_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels= tf.one_hot(y,10),logits=y_out))
#loss = tf.losses.sparse_softmax_cross_entropy( labels = y, logits = y_out)


mean_loss = tf.reduce_mean(total_loss)
optimizer = tf.train.RMSPropOptimizer(1e-3)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 结果:
    在这里插入图片描述
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/92761
推荐阅读
相关标签
  

闽ICP备14008679号