2. 机器学习实战之 CNN训练CIFAR10数据集

CIFAR10 数据集介绍

CIFAR10数据集与MNIST都是入门级数据集, 该数据集共有60000张彩色图像,这些图像是32*32,分为10个类,每类6000张图。 这里面有50000张用于训练,构成了5个训练批,每一批10000张图;另外10000用于测试

数据加载

import tensorflow as tf
from keras.datasets import cifar10
import matplotlib.pyplot as plt 
from keras.utils import to_categorical

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print(type(x_train))
print(x_train.size)
print(x_train.shape)
print(x_train[0,:,:,0])

print(x_test.shape)
print(y_train)

<class 'numpy.ndarray'>
153600000
(50000, 32, 32, 3)
[[ 59  43  50 ... 158 152 148]
 [ 16   0  18 ... 123 119 122]
 [ 25  16  49 ... 118 120 109]
 ...
 [208 201 198 ... 160  56  53]
 [180 173 186 ... 184  97  83]
 [177 168 179 ... 216 151 123]]
(10000, 32, 32, 3)
[[6]
 [9]
 [9]
 ...
 [9]
 [1]
 [1]]

数据可视化

plt.imshow(x_train[1000,:,:,:])

2. 机器学习实战之 CNN训练CIFAR10数据集_第1张图片
可以统计label的分布 看是否要数据增广(但是这个数据也很干净 所以可以先忽略)

归一化与One hot

y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test,10)
x_train = x_train.astype('float32')
x_train /= 255.0

模型建立与训练

import keras
from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, Activation,Flatten, Dropout, Dense
from keras.losses import categorical_crossentropy
from keras.optimizers import Adadelta

model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',
                 input_shape=x_train.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
 
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
 
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
model.summary()

# initiate RMSprop optimizer
# 均方根反向传播(RMSprop,root mean square prop)优化
opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
 
# Let's train the model using RMSprop
# 使用均方根反向传播(RMSprop)训练模型
model.compile(loss='categorical_crossentropy',
              optimizer=opt,
              metrics=['accuracy'])
batch_size = 32 #每个批次样本记录数
num_classes =10
epochs = 100 #100个周期

model.fit(x_train, y_train,
              batch_size=batch_size,
              epochs=epochs,
              validation_data=(x_test, y_test),
              shuffle=True)

# Score trained model.
# 评估训练的模型
scores = model.evaluate(x_test, y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])

模型参考 https://www.cnblogs.com/neopenx/p/4480701.html

你可能感兴趣的:(机器学习)