ResNet18的实现还是相对比较简单的,一共就18次卷积,从数据加载到最后完成验证不到80行
import tensorflow as tf
import pandas as pd
import numpy as np
from tensorflow.keras import layers
from sklearn.metrics import classification_report
import warnings
warnings.filterwarnings('ignore')
#数据导入
train_data = pd.read_csv('sign_train.csv')
test_data = pd.read_csv('sign_test.csv')
train_x = train_data.drop('label',axis=1).values
train_y = train_data['label'].values
test_x = test_data.drop('label',axis=1).values
test_y = test_data['label'].values
#数据归一化
train_x = train_x / 255
test_x = test_x / 255
train_x = train_x.reshape((-1, 28, 28, 1))
test_x = test_x.reshape((-1, 28, 28, 1))
#模型建立
def conv2d_bn(inpt, filters=64, kernel_size=(3,3), strides=1, padding='same'):
'''卷积、归一化和relu三合一'''
x = layers.Conv2D(filters=filters, kernel_size=kernel_size, strides=strides, padding=padding)(inpt)
x = layers.BatchNormalization()(x)
x = tf.keras.layers.Activation('relu')(x)
return x
def basic_bottle(inpt, filters=64, kernel_size=(3,3), strides=1, padding='same', if_baisc=False):
'''18中的4个basic_bottle'''
x = conv2d_bn(inpt, filters=filters, kernel_size=kernel_size, strides=strides, padding=padding)
x = conv2d_bn(x, filters=filters)
if if_baisc==True:
temp = conv2d_bn(inpt, filters=filters, kernel_size=(1,1), strides=2, padding='same')
outt = layers.add([x, temp])
else:
outt = layers.add([x, inpt])
return outt
def resnet18(class_nums):
'''主模型'''
inpt = layers.Input(shape=(28,28,1))
#layer 1
x = conv2d_bn(inpt, filters=64, kernel_size=(7,7), strides=2, padding='valid')
x = layers.MaxPool2D(pool_size=(3,3), strides=2)(x)
#layer 2
x = basic_bottle(x, filters=64, kernel_size=(3,3), strides=1, padding='same', if_baisc=False)
x = basic_bottle(x, filters=64, kernel_size=(3,3), strides=1, padding='same', if_baisc=False)
#layer 3
x = basic_bottle(x, filters=128, kernel_size=(3, 3), strides=2, padding='same', if_baisc=True)
x = basic_bottle(x, filters=128, kernel_size=(3, 3), strides=1, padding='same', if_baisc=False)
# layer 4
x = basic_bottle(x, filters=256, kernel_size=(3, 3), strides=2, padding='same', if_baisc=True)
x = basic_bottle(x, filters=256, kernel_size=(3, 3), strides=1, padding='same', if_baisc=False)
# layer 5
x = basic_bottle(x, filters=512, kernel_size=(3, 3), strides=2, padding='same', if_baisc=True)
x = basic_bottle(x, filters=512, kernel_size=(3, 3), strides=1, padding='same', if_baisc=False)
#GlobalAveragePool
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(class_nums, activation='softmax')(x)
model = tf.keras.Model(inputs=inpt, outputs=x)
return model
#开始训练
class_num = len(set(train_y))+1
model = resnet18(class_num)
model.summary()
model.compile(optimizer=tf.keras.optimizers.Adam(),loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy'])
model.fit(train_x, train_y, batch_size=64, epochs=10, validation_split=0.2) #epochs可以调的大一点,如果你机器好的话
#验证结果
pred_y = model.predict(test_x)
last_pred = []
for i in range(pred_y.shape[0]):
last_pred.append(pred_y[i].argmax())
last_pred = np.array(last_pred)
print(classification_report(test_y, last_pred))