1.直接下载:http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
2.将下载下来的文件放到~/.keras/datasets/ 目录下,然后将文件名改名为cifar-10-batches-py.tar.gz ,再次运行代码的时候,则会先检查本地是否存在,存在则使用本地的该数据集,不会再进行下载。
32 x 32 | 32 filter, Size:3 x 3 | rate: 0.2 | 32 filter, Size:3 x 3 | rate: 0.2 | Size: (2 x 2) | 512个神经元+Relu激活 | rate: 0.5 | 10个神经元+softmax激活 | |
---|---|---|---|---|---|---|---|---|---|
输入层 | 卷积层① | Dropout层① | 卷积层② | Dropout层② | (最大)池化层 | Flatten 层 | 全连接层 | Dropout层 | 输出层 |
from keras.datasets import cifar10
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.optimizers import SGD
from keras.constraints import maxnorm
from keras.utils import np_utils
from keras import backend
backend.set_image_data_format('channels_first')
# 设定随机数种子
seed = 7
np.random.seed(seed)
# 导入数据
(x_train, y_train), (x_validation, y_validation) = cifar10.load_data()
x_train = x_train.astype('float32')
x_validation = x_validation.astype('float32')
x_train = x_train/255.0
x_validation = x_validation/255.0
# 进行one-hot编码
y_train = np_utils.to_categorical(y_train)
y_validation = np_utils.to_categorical(y_validation)
num_classes = y_train.shape[1]
# 定义模型创建函数
def create_model(epochs=25):
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3, 32, 32), padding='same', activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.2))
model.add(Conv2D(32, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
lrate = 0.01
decay = lrate/epochs
sgd = SGD(lr=lrate, momentum=0.9, decay=decay, nesterov=False)
# 编译模型
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
return model
epochs = 25
model = create_model(epochs)
# 训练模型及评估模型
model.fit(x=x_train, y=y_train, epochs=epochs, batch_size=32, verbose=2)
score = model.evaluate(x=x_validation, y=y_validation, verbose=0)
print('Accuracy: %.2f%%' % (score[1] * 100))
Epoch 1/25
- 590s - loss: 1.6989 - acc: 0.3823
Epoch 2/25
- 584s - loss: 1.3085 - acc: 0.5293
Epoch 3/25
- 584s - loss: 1.1180 - acc: 0.6011
Epoch 4/25
- 579s - loss: 0.9892 - acc: 0.6509
...
Epoch 23/25
- 561s - loss: 0.2258 - acc: 0.9221
Epoch 24/25
- 561s - loss: 0.2148 - acc: 0.9257
Epoch 25/25
- 558s - loss: 0.2110 - acc: 0.9271
Accuracy: 70.91%
主要按照特征图为32/64/128各两次重复构建模型
输入层:32 x 32 |
---|
↓ |
卷积层1-1: 32 filter, Size:3 x 3 |
↓ |
Dropout层1: 概率为0.2 |
↓ |
卷积层1-2: 32 filter, Size:3 x 3 |
↓ |
池化层:MaxPooling, Size = (2, 2) |
↓ |
卷积层2-1: 64 filter, Size:3 x 3 |
↓ |
Dropout层2: 概率为0.5 |
↓ |
卷积层2-2: 64 filter, Size:3 x 3 |
↓ |
池化层:MaxPooling, Size = (2, 2) |
↓ |
卷积层3-1: 128 filter, Size:3 x 3 |
↓ |
Dropout层3: 概率为0.2 |
↓ |
卷积层3-2: 128 filter, Size:3 x 3 |
↓ |
池化层:MaxPooling, Size = (2, 2) |
↓ |
Flatten 层 |
↓ |
Dropout层4: 概率为0.5 |
↓ |
全连接层: 1024神经元+Relu激活 |
↓ |
Dropout层5: 概率为0.2 |
↓ |
全连接层: 512神经元+Relu激活 |
↓ |
Dropout层:6 概率为0.5 |
↓ |
输出层:10个神经元+softmax激活 |
from keras.datasets import cifar10
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.optimizers import SGD
from keras.constraints import maxnorm
from keras.utils import np_utils
from keras import backend
backend.set_image_data_format('channels_first')
# 设定随机数种子
seed = 7
np.random.seed(seed)
# 导入数据
(x_train, y_train), (x_validation, y_validation) = cifar10.load_data()
# 将数据格式化到0~1
x_train = x_train.astype('float32')
x_validation = x_validation.astype('float32')
x_train = x_train/255.0
x_validation = x_validation/255.0
# 进行one-hot编码
y_train = np_utils.to_categorical(y_train)
y_validation = np_utils.to_categorical(y_validation)
num_classes = y_train.shape[1]
# 定义模型创建函数
def create_model(epochs=25):
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3, 32, 32), padding='same', activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.2))
model.add(Conv2D(32, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.5))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.2))
model.add(Conv2D(128, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(1024, activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
lrate = 0.01
decay = lrate/epochs
sgd = SGD(lr=lrate, momentum=0.9, decay=decay, nesterov=False)
# 编译模型
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
return model
epochs = 25
model = create_model(epochs)
# 训练模型及评估模型
model.fit(x=x_train, y=y_train, epochs=epochs, batch_size=32, verbose=2)
score = model.evaluate(x=x_validation, y=y_validation, verbose=0)
print('Accuracy: %.2f%%' % (score[1] * 100))
Epoch 1/25
- 984s - loss: 2.0213 - acc: 0.2438
Epoch 2/25
- 989s - loss: 1.6381 - acc: 0.3902
Epoch 3/25
- 977s - loss: 1.4468 - acc: 0.4665
Epoch 4/25
- 1493s - loss: 1.3066 - acc: 0.5236
Epoch 5/25
- 1044s - loss: 1.2014 - acc: 0.5657
...
Epoch 21/25
- 1046s - loss: 0.7111 - acc: 0.7487
Epoch 22/25
- 982s - loss: 0.7010 - acc: 0.7506
Epoch 23/25
- 1096s - loss: 0.6867 - acc: 0.7595
Epoch 24/25
- 1037s - loss: 0.6756 - acc: 0.7610
Epoch 25/25
- 987s - loss: 0.6701 - acc: 0.7647
Accuracy: 76.30%