CNN Code
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import csv
import math
import numpy as np
# 读取 train.csv
# 划分 70% 为 train set, 30% 为 test set
def loaddata():
train_x = []
train_y = []
n_row = 0
path = r'E:\李宏毅\机器学习\keras\train.csv'
text = open(path, 'r')
row = csv.reader(text)
for r in row:
temp = []
if n_row != 0: # 第一行(索引 0),是列标签,不用读取
train_y.append(r[0])
temp = [float(b) for b in r[1].split(' ')]
train_x.append(temp)
n_row = n_row + 1
train_x = np.array(train_x)
train_y = np.array(train_y)
len = math.floor(train_x.shape[0]*0.7)
x_train = train_x[0:len]
y_train = train_y[0:len]
x_test = train_x[len:]
y_test = train_y[len:]
x_train = x_train.reshape(x_train.shape[0], 48, 48, 1)
x_test = x_test.reshape(x_test.shape[0], 48, 48, 1)
# print(x_train.shape) # (20096, 48, 48, 1)
# print(y_train.shape) # (20096,)
# print(x_test.shape) # (8613, 48, 48, 1)
# print(y_test.shape) # (8613,)
return (x_train, y_train), (x_test, y_test)
def Model():
batch_size = 128
num_classes = 7
epochs = 10
img_rows, img_cols = 48, 48
input_shape = (img_rows, img_cols, 1)
(x_train, y_train), (x_test, y_test) = loaddata()
# 归一化
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(64, (3, 3), activation = 'relu', input_shape = input_shape))
model.add(MaxPooling2D(pool_size = (3, 3)))
model.add(Conv2D(128, (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (3, 3)))
model.add(Flatten())
model.add(Dense(256, activation = 'relu'))
model.add(Dropout(0.3))
model.add(Dense(256, activation = 'relu'))
model.add(Dropout(0.3))
model.add(Dense(num_classes, activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adadelta', metrics = ['accuracy'])
model.fit(x_train, y_train, batch_size = batch_size, epochs = epochs,verbose = 1, validation_data = (x_test, y_test))
score = model.evaluate(x_test, y_test, verbose = 0)
print('Test Loss: ', score[0])
print('Test accuracy: ', score[1])
if __name__ == '__main__':
Model()
Test Loss: 1.21152299923
Test accuracy: 0.53860443516
DNN Code
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import csv
import math
import numpy as np
# 读取 train.csv
# 划分 70% 为 train set, 30% 为 test set
def loaddata():
train_x = []
train_y = []
n_row = 0
path = r'E:\李宏毅\机器学习\keras\train.csv'
text = open(path, 'r')
row = csv.reader(text)
for r in row:
temp = []
if n_row != 0: # 第一行(索引 0),是列标签,不用读取
train_y.append(r[0])
temp = [float(b) for b in r[1].split(' ')]
train_x.append(temp)
n_row = n_row + 1
train_x = np.array(train_x)
train_y = np.array(train_y)
len = math.floor(train_x.shape[0]*0.7)
x_train = train_x[0:len]
y_train = train_y[0:len]
x_test = train_x[len:]
y_test = train_y[len:]
x_train = x_train.reshape(x_train.shape[0], 48*48)
x_test = x_test.reshape(x_test.shape[0], 48*48)
print(x_train.shape) # (20096, 2304)
print(y_train.shape) # (20096,)
print(x_test.shape) # (8613, 2304)
print(y_test.shape) # (8613,)
return (x_train, y_train), (x_test, y_test)
def Model():
batch_size = 128
num_classes = 7
epochs = 10
img_rows, img_cols = 48, 48
input_shape = (img_rows*img_cols,)
(x_train, y_train), (x_test, y_test) = loaddata()
# 归一化
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Dense(256, activation = 'relu', input_shape = input_shape))
model.add(Dropout(0.3))
model.add(Dense(256, activation = 'relu'))
model.add(Dropout(0.3))
model.add(Dense(256, activation = 'relu'))
model.add(Dropout(0.3))
model.add(Dense(256, activation = 'relu'))
model.add(Dropout(0.3))
model.add(Dense(num_classes, activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adadelta', metrics = ['accuracy'])
model.fit(x_train, y_train, batch_size = batch_size, epochs = epochs,verbose = 1, validation_data = (x_test, y_test))
score = model.evaluate(x_test, y_test, verbose = 0)
print('Test Loss: ', score[0])
print('Test accuracy: ', score[1])
if __name__ == '__main__':
Model()
Test Loss: 1.69847433028
Test accuracy: 0.311157552537
当层数变少时,准确率提高(代码如下)
model = Sequential()
model.add(Dense(256, activation = 'relu', input_shape = input_shape))
model.add(Dropout(0.3))
model.add(Dense(256, activation = 'relu'))
model.add(Dropout(0.3))
model.add(Dense(num_classes, activation = 'softmax'))
Test Loss: 1.67239785668
Test accuracy: 0.361082085223
小结
- 比较 CNN ,DNN 的 accuracy , 可见CNN的准确率更加高
- 当二者作为第一层是,注意输入数据格式的不同,也就是 input_shape 不同,具体看代码
参考资料
- 投影片
- 作业网址
- kaggle
- How convolutional neural networks see the world