【小白学习keras教程】六、基于CIFAR-10数据集训练CNN-RNN神经网络模型

@Author:Runsen

@[toc]

Load Dataset

import numpy as np

from sklearn.metrics import accuracy_score
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical

(X_train, y_train), (X_test, y_test) = cifar10.load_data()
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

1.CNN-RNN

  • 先执行卷积和池运算,然后执行循环运算
  • 类似于图像字幕的结构
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, LSTM, Input, Activation, Reshape, concatenate
from tensorflow.keras import optimizers

model = Sequential()
model.add(Conv2D(input_shape = (X_train.shape[1], X_train.shape[2], X_train.shape[3]), filters = 50, kernel_size = (3,3), strides = (1,1), padding = 'same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2,2)))
print(model.output_shape)

(None, 16, 16, 50)

model.add(Reshape(target_shape = (16*16, 50)))
model.add(LSTM(50, return_sequences = False))
model.add(Dense(10))
model.add(Activation('softmax'))
adam = optimizers.Adam(lr = 0.001)
model.compile(loss = 'categorical_crossentropy', optimizer = adam, metrics = ['accuracy'])
print(model.summary())

model.fit(X_train, y_train, epochs = 10, batch_size = 10, verbose = 0)
results = model.evaluate(X_test, y_test)
print('Test Accuracy: ', results[1])

在这里插入图片描述

results = model.evaluate(X_test, y_test)
print('Test Accuracy: ', results[1])

Test Accuracy: 0.6473000049591064

2.CNN-RNN-2

  • 独立执行卷积池运算和循环运算,并将其结果汇总
  • 类似于视觉问答的结构

input_layer = Input(shape = (X_train.shape[1], X_train.shape[2], X_train.shape[3]))
conv_layer = Conv2D(filters = 50, kernel_size = (3,3), strides = (1,1), padding = 'same')(input_layer)
activation_layer = Activation('relu')(conv_layer)
pooling_layer = MaxPooling2D(pool_size = (2,2), padding = 'same')(activation_layer)
flatten = Flatten()(pooling_layer)
dense_layer_1 = Dense(100)(flatten)
reshape = Reshape(target_shape = (X_train.shape[1]*X_train.shape[2], X_train.shape[3]))(input_layer)
lstm_layer = LSTM(50, return_sequences = False)(reshape)
dense_layer_2 = Dense(100)(lstm_layer)
merged_layer = concatenate([dense_layer_1, dense_layer_2])
output_layer = Dense(10, activation = 'softmax')(merged_layer)
model = Model(inputs = input_layer, outputs = output_layer)
adam = optimizers.Adam(lr = 0.001)
model.compile(loss = 'categorical_crossentropy', optimizer = adam, metrics = ['accuracy'])
print(model.summary())

model.fit(X_train, y_train, epochs = 10, batch_size = 100, verbose = 1)

results = model.evaluate(X_test, y_test)
print('Test Accuracy: ', results[1])

Test Accuracy: 0.48030000925064087

(完)