import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D, BatchNormalization,Convolution2D,Concatenate,Input
from keras.utils import np_utils
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint
from keras.models import load_model,Model
# import sklearn
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
# load mnist from keras
from keras.datasets import mnist,fashion_mnist
# get mnist data
(X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()
# convert y label to one-hot vector
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
# display the first image in the training set
plt.imshow(X_train[0], cmap='gray')
# get CNN model keras
def get_cnn_model():
inputs = Input(shape=(28, 28, 1))
x = Conv2D(32, (3, 3), activation='relu')(inputs)
x = MaxPooling2D(pool_size=(2, 2))(x)
# x = Dropout(0.25)(x)
# x = Conv2D(32, (3,3), activation='relu')(x)
# x = MaxPooling2D(pool_size=(2, 2))(x)
x = Flatten()(x)
x = Dense(units=128, activation='relu')(x)
outputs = Dense(units=10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
return model
import scipy
import cv2
# DCT transformation for image
def dct_transform(img):
dct_img = cv2.dct(img)
return dct_img
# convert numpy to cv
def numpy_to_cv(img):
cv_img = img.astype(np.uint8)
return cv_img
# convert to CV_32F
def cv_32f(img):
cv_img = img.astype(np.float32)
return cv_img
# plot numpy
def plot_numpy(img):
plt.imshow(img, cmap='gray')
plt.show()
dct_np = dct_transform(cv_32f(numpy_to_cv(X_train[0])))
plot_numpy(dct_np)
X_train_dct = np.zeros(X_train.shape)
X_test_dct = np.zeros(X_test.shape)
for i in range(X_train.shape[0]):
X_train_dct[i] = dct_transform(cv_32f(numpy_to_cv(X_train[i])))
for i in range(X_test.shape[0]):
X_test_dct[i] = dct_transform(cv_32f(numpy_to_cv(X_test[i])))
def plot_history(history):
import seaborn as sns
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1,len(acc) + 1)
plt.figure()
plt.plot(epochs,acc,'-r',label='Training accuracy')
plt.plot(epochs, val_acc, '-b', label='Validation accuracy')
plt.xlabel('迭代次数')
plt.ylabel('准确率')
plt.rcParams['font.sans-serif']=['SimHei']#识别中文字体的设置
plt.title('Training and Validation accuracy')
sns.despine()
plt.legend()
plt.figure()
plt.plot(epochs,loss,'-r',label='Training loss')
plt.plot(epochs, val_loss, '-b', label='Validation loss')
plt.xlabel('迭代次数')
plt.ylabel('损失率')
plt.title('Training and Validation loss')
sns.despine()
plt.legend()
plt.show()
train model
model_cnn = get_cnn_model()
# compile model with categorical_crossentropy loss and adam optimizer
model_cnn.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model_cnn.summary()
Model: "model_9"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_11 (InputLayer) [(None, 28, 28, 1)] 0
conv2d_18 (Conv2D) (None, 26, 26, 32) 320
max_pooling2d_18 (MaxPoolin (None, 13, 13, 32) 0
g2D)
flatten_7 (Flatten) (None, 5408) 0
dense_20 (Dense) (None, 128) 692352
dense_21 (Dense) (None, 10) 1290
=================================================================
Total params: 693,962
Trainable params: 693,962
Non-trainable params: 0
_________________________________________________________________
# train model
history = model_cnn.fit(X_train, y_train, batch_size=512, epochs=10, verbose=1, validation_data=(X_test, y_test))
Epoch 1/10
118/118 [==============================] - 1s 10ms/step - loss: 9.2775 - accuracy: 0.7009 - val_loss: 0.7102 - val_accuracy: 0.7712
Epoch 2/10
118/118 [==============================] - 1s 6ms/step - loss: 0.5453 - accuracy: 0.8232 - val_loss: 0.4886 - val_accuracy: 0.8434
Epoch 3/10
118/118 [==============================] - 1s 6ms/step - loss: 0.3931 - accuracy: 0.8670 - val_loss: 0.4078 - val_accuracy: 0.8664
Epoch 4/10
118/118 [==============================] - 1s 6ms/step - loss: 0.3341 - accuracy: 0.8835 - val_loss: 0.3980 - val_accuracy: 0.8686
Epoch 5/10
118/118 [==============================] - 1s 6ms/step - loss: 0.2954 - accuracy: 0.8958 - val_loss: 0.3608 - val_accuracy: 0.8763
Epoch 6/10
118/118 [==============================] - 1s 6ms/step - loss: 0.2719 - accuracy: 0.9020 - val_loss: 0.3555 - val_accuracy: 0.8851
Epoch 7/10
118/118 [==============================] - 1s 6ms/step - loss: 0.2502 - accuracy: 0.9099 - val_loss: 0.3487 - val_accuracy: 0.8834
Epoch 8/10
118/118 [==============================] - 1s 6ms/step - loss: 0.2351 - accuracy: 0.9147 - val_loss: 0.3625 - val_accuracy: 0.8806
Epoch 9/10
118/118 [==============================] - 1s 6ms/step - loss: 0.2232 - accuracy: 0.9184 - val_loss: 0.3349 - val_accuracy: 0.8902
Epoch 10/10
118/118 [==============================] - 1s 6ms/step - loss: 0.2085 - accuracy: 0.9247 - val_loss: 0.3554 - val_accuracy: 0.8834
plot_history(history)
train model with dct
model_cnn_dct = get_cnn_model()
# compile model with categorical_crossentropy loss and adam optimizer
model_cnn_dct.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model_cnn_dct.summary()
Model: "model_10"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_12 (InputLayer) [(None, 28, 28, 1)] 0
conv2d_19 (Conv2D) (None, 26, 26, 32) 320
max_pooling2d_19 (MaxPoolin (None, 13, 13, 32) 0
g2D)
flatten_8 (Flatten) (None, 5408) 0
dense_22 (Dense) (None, 128) 692352
dense_23 (Dense) (None, 10) 1290
=================================================================
Total params: 693,962
Trainable params: 693,962
Non-trainable params: 0
_________________________________________________________________
# train model
history_dct = model_cnn_dct.fit(X_train_dct, y_train, batch_size=512, epochs=10, verbose=1, validation_data=(X_test_dct, y_test))
Epoch 1/10
118/118 [==============================] - 1s 8ms/step - loss: 7.7165 - accuracy: 0.6725 - val_loss: 0.9065 - val_accuracy: 0.7944
Epoch 2/10
118/118 [==============================] - 1s 7ms/step - loss: 0.7222 - accuracy: 0.8190 - val_loss: 0.7983 - val_accuracy: 0.7976
Epoch 3/10
118/118 [==============================] - 1s 7ms/step - loss: 0.6040 - accuracy: 0.8360 - val_loss: 0.5968 - val_accuracy: 0.8371
Epoch 4/10
118/118 [==============================] - 1s 7ms/step - loss: 0.5108 - accuracy: 0.8516 - val_loss: 0.5896 - val_accuracy: 0.8434
Epoch 5/10
118/118 [==============================] - 1s 7ms/step - loss: 0.4563 - accuracy: 0.8598 - val_loss: 0.5643 - val_accuracy: 0.8357
Epoch 6/10
118/118 [==============================] - 1s 7ms/step - loss: 0.3854 - accuracy: 0.8753 - val_loss: 0.4513 - val_accuracy: 0.8583
Epoch 7/10
118/118 [==============================] - 1s 7ms/step - loss: 0.3627 - accuracy: 0.8811 - val_loss: 0.4910 - val_accuracy: 0.8475
Epoch 8/10
118/118 [==============================] - 1s 7ms/step - loss: 0.3500 - accuracy: 0.8837 - val_loss: 0.4630 - val_accuracy: 0.8555
Epoch 9/10
118/118 [==============================] - 1s 7ms/step - loss: 0.3265 - accuracy: 0.8889 - val_loss: 0.5798 - val_accuracy: 0.8356
Epoch 10/10
118/118 [==============================] - 1s 7ms/step - loss: 0.2997 - accuracy: 0.8959 - val_loss: 0.4386 - val_accuracy: 0.8660
plot_history(history_dct)
bagging
# bagging model_cnn and model_cnn_dct
def get_bagging_model(model_cnn,model_cnn_dct,X,X_dct):
"""bagging two model together"""
pred_cnn = model_cnn.predict(X)
pred_cnn_dct = model_cnn_dct.predict(X_dct)
pred = (pred_cnn + pred_cnn_dct) / 2
return pred
pred_bagging = get_bagging_model(model_cnn, model_cnn_dct, X_test, X_test_dct)
pred_bagging.shape
(10000, 10)
y_test.shape
(10000, 10)
# calculate the accuracy of bagging model
from sklearn.metrics import accuracy_score
acc_bagging = accuracy_score(np.argmax(pred_bagging, axis=1), np.argmax(y_test, axis=1))
acc_bagging
0.8947
_test_dct)
```python
pred_bagging.shape
(10000, 10)
y_test.shape
(10000, 10)
# calculate the accuracy of bagging model
from sklearn.metrics import accuracy_score
acc_bagging = accuracy_score(np.argmax(pred_bagging, axis=1), np.argmax(y_test, axis=1))
acc_bagging
0.8947
结论
原图像与进行 DCT 变换后的图像分别训练模型,bagging,效果有提升