当前位置:   article > 正文

CIFAR-10:VGGNet实现_cifar 10 vgg 8

cifar 10 vgg 8
import tensorflow as tf
import os
import pickle
import numpy as np
CIFAR_DIR='cifar-10-python/cifar-10-batches-py'
print(os.listdir(CIFAR_DIR))
def load_data(filename):
    """read data from file"""
    with open(filename,'rb') as f:
        data=pickle.load(f,encoding='bytes')
        return data[b'data'],data[b'labels']

class CifarData:
    def __init__(self,filenames,need_shuffle):
        all_data=[]
        all_labels=[]
        for filename in filenames:
            data,labels=load_data(filename)
            all_data.append(data)
            all_labels.append(labels)
        self._data=np.vstack(all_data)
        self._data=self._data/127.5-1#归一化
        self._labels=np.hstack(all_labels)
        self._num_examples=self._data.shape[0]
        self._need_shuffle=need_shuffle
        self._indicator=0
        if self._need_shuffle:
            self._shuffle_data()
    def _shuffle_data(self):
        p=np.random.permutation(self._num_examples)
        self._data=self._data[p]
        self._labels=self._labels[p]
    def next_batch(self,batch_size):
        """return batch_size examples as a batch"""
        end_indicator=self._indicator+batch_size
        if end_indicator>self._num_examples:
            if self._need_shuffle:
                self._shuffle_data()
                self._indicator=0
                end_indicator=batch_size
            else:
                raise Exception("have no more example")
        if end_indicator>self._num_examples:
            raise Exception("batch size is larger than all example")
        batch_data=self._data[self._indicator:end_indicator]
        batch_labels=self._labels[self._indicator:end_indicator]
        self._indicator=end_indicator
        return batch_data,batch_labels
train_filenames=[os.path.join(CIFAR_DIR,'data_batch_%d'%i)for i in range(1,6)]
test_filenames=[os.path.join(CIFAR_DIR,'test_batch')]

train_data=CifarData(train_filenames,True)
test_data=CifarData(test_filenames,False)
#[None,3072]
x=tf.placeholder(tf.float32,[None,3072])
y=tf.placeholder(tf.int64,[None])
#32*32
x_image=tf.reshape(x,[-1,3,32,32])
x_images=tf.transpose(x_image,perm=[0,2,3,1])
#conv1=神经元图,fea_map,输出图像
conv1_1=tf.layers.conv2d(x_images,
                       32,
                       (3,3),
                       padding='same',
                       activation=tf.nn.relu,
                       name='conv1_1'
                       )
conv1_2=tf.layers.conv2d(conv1_1,
                       32,
                       (3,3),
                       padding='same',
                       activation=tf.nn.relu,
                       name='conv1_2'
                       )

#16*16
pooling1=tf.layers.max_pooling2d(conv1_2,
                                 (2,2),#kernel size
                                 (2,2),#stride
                                 name='pool1')

conv2_1=tf.layers.conv2d(pooling1,
                       32,
                       (3,3),
                       padding='same',
                       activation=tf.nn.relu,
                       name='conv2_1'
                       )
conv2_2=tf.layers.conv2d(conv2_1,
                       32,
                       (3,3),
                       padding='same',
                       activation=tf.nn.relu,
                       name='conv2_2'
                       )

#8*8
pooling2=tf.layers.max_pooling2d(conv2_2,
                                 (2,2),#kernel size
                                 (2,2),#stride
                                 name='pool2')

conv3_1=tf.layers.conv2d(pooling2,
                       32,
                       (3,3),
                       padding='same',
                       activation=tf.nn.relu,
                       name='conv3_1'
                       )

conv3_2=tf.layers.conv2d(pooling2,
                       32,
                       (3,3),
                       padding='same',
                       activation=tf.nn.relu,
                       name='conv3_2'
                       )
#4*4*32
pooling3=tf.layers.max_pooling2d(conv3_2,
                                 (2,2),#kernel size
                                 (2,2),#stride
                                 name='pool3')
#[None,4*4*32]
flatten=tf.layers.flatten(pooling3)
y_=tf.layers.dense(flatten,10)#输出为10是因为他是一个10分类的图

#交叉熵损失函数
loss=tf.losses.sparse_softmax_cross_entropy(labels=y,logits=y_)
#labels->one_hot
#logits->softmax

predict=tf.argmax(y_,1)
correct_prediction=tf.equal(predict,y)#equial用来比对值是否一样返回的是布尔类型
accurary=tf.reduce_mean(tf.cast(correct_prediction,tf.float64))

with tf.name_scope('train_op'):
    train_op=tf.train.AdamOptimizer(1e-3).minimize(loss)
    # train_op = tf.train.AdadeltaOptimizer(0.4).minimize(loss)

init=tf.global_variables_initializer()
batch_size=20
train_steps=100000
test_steps=100
sess = tf.InteractiveSession() #交互式
sess.run(init)
for i in range(train_steps):
    batch_data,batch_labels=train_data.next_batch(batch_size)
    loss_val,acc_val,_=sess.run([loss,accurary,train_op],feed_dict={x:batch_data,y:batch_labels})
    if (i+1) %500==0:
        print('[train] step:%d,loss:%4.5f,acc:%4.5f'%(i+1,loss_val,acc_val))
    if (i+1)%5000==0:
        test_data=CifarData(test_filenames,True)
        all_test_acc_val=[]
        for j in range(test_steps):
            test_batch_data,test_batch_labels=test_data.next_batch(batch_size)
            test_acc_val=sess.run([accurary],
                feed_dict={x:test_batch_data,y:test_batch_labels})
            all_test_acc_val.append(test_acc_val)
        test_acc=np.mean(all_test_acc_val)
        print('[test] step:%d,acc:%4.5f'%(i+1,test_acc))

  • 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
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/957519
推荐阅读
相关标签
  

闽ICP备14008679号