欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

表计数字数字识别 自己写个ResNet8吧

程序员文章站 2022-07-13 11:28:13
...

写的尽量规范了~

ipynb格式的,注意自己分段

keras=2.1.2

数据集地址:https://download.csdn.net/download/Andrwin/12344471

1.Import

#!/usr/bin/env python
# encoding: utf-8
from keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, AveragePooling2D, MaxPooling2D,Dropout, GlobalMaxPooling2D
from keras.applications.imagenet_utils import preprocess_input
from keras.preprocessing.image import ImageDataGenerator,img_to_array
from keras.utils.vis_utils import model_to_dot
from keras.initializers import glorot_uniform
from keras.utils.data_utils import get_file
from keras.models import Model, load_model
from keras.utils import to_categorical,layer_utils,plot_model
from keras.preprocessing import image
from keras.optimizers import Adam
from matplotlib.pyplot import imshow
from keras.callbacks import ModelCheckpoint,EarlyStopping
from keras.callbacks import ReduceLROnPlateau
from IPython.display import SVG
import matplotlib.pylab as plt
from imutils import paths
import keras.backend as K
from sklearn.model_selection import train_test_split
from keras import layers
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.ERROR)
import scipy.misc
import numpy as np
import random
import keras
import sys
import cv2
import os
import warnings
warnings.filterwarnings("ignore")
K.set_image_data_format('channels_last')
K.set_learning_phase(1)
keras.__version__

2.Hyper-Parameters

Lr = 0.002
# Lr = .0002
Epoch = 200
ImgSize = 64
Verbose = 1
ClassNum = 10
BatchSize = 64
Channel = 3
train_split_ratio = 0.1

path_tr = "/home/nvidia/SVHN/dataset_2"
path_mo = "/home/nvidia/SVHN/weight/r1_64_45rio_best_vloss_0.13931_vacc_0.98020.hdf5"
path_va = '/home/nvidia/SVHN/valdata/'

# train/test/re-train
mode = "test"

save_model_image = True

3.Model

def load_data_for_train(path,norm_size,class_num):
    data = []
    label = []
    image_paths = sorted(list(paths.list_images(path)))
    random.seed(188)
    random.shuffle(image_paths)
    for each_path in image_paths:
        image = cv2.imread(each_path)
        image = cv2.resize(image,(norm_size,norm_size),interpolation=cv2.INTER_CUBIC)
        image = img_to_array(image)
        data.append(image)
        maker = int(each_path.split(os.path.sep)[-2])
        label.append(maker)
    data = np.array(data,dtype="float")/255.0
    label = np.array(label)
    label = to_categorical(label,num_classes=class_num)
    train_data, test_data, train_label, test_label = train_test_split(data, 
                                                                      label, 
                                                                      test_size = train_split_ratio, 
                                                                      random_state = 1)
    return train_data, test_data, train_label, test_label

def load_data_for_test(path,norm_size,class_num):
    data = []
    label = []
    image_paths = sorted(list(paths.list_images(path)))
    random.seed(188)
    random.shuffle(image_paths)
    for each_path in image_paths:
        image = cv2.imread(each_path)
        image = cv2.resize(image,(norm_size,norm_size),interpolation=cv2.INTER_CUBIC)
        image = img_to_array(image)
        data.append(image)
        maker = int(each_path.split(os.path.sep)[-2])
        label.append(maker)
    data = np.array(data,dtype="float")/255.0
    label = np.array(label)
    label = to_categorical(label,num_classes=class_num)
    return data, label

def identify_block(X,f,filters,stage,block):
    conv_name_base = 'res' +str(stage) +block +'_branch'
    bn_name_base = 'bn' + str(stage) + block +  '_branch'
    F1, F2, F3 = filters
    X_shortcut = X 
    X = Conv2D(filters=F1,kernel_size=(1,1),strides=(1,1),padding='valid',name = conv_name_base+'2a',
               kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name= bn_name_base+'2a')(X)
    X = Activation('relu')(X)
    X = Conv2D(filters=F2, kernel_size=(f,f),strides=(1,1),padding='same',name= conv_name_base+'2b',
              kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3,name= bn_name_base+'2b')(X) 
    X = Add()([X,X_shortcut])
    X = Activation('relu')(X)
    return X

def convolutional_block(X,f,filters,stage,block,s=2):
    conv_name_base = 'res' +str(stage) +block +'_branch'
    bn_name_base = 'bn' + str(stage) + block +  '_branch'
    F1,F2,F3 = filters
    X_shortcut = X 
    X = Conv2D(filters=F1,kernel_size=(1,1),strides=(s,s),padding='valid',name = conv_name_base+'2a',kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3,name=bn_name_base+'2a')(X)
    X = Activation('relu')(X)
    X = Conv2D(filters=F2,kernel_size=(f,f),strides=(1,1),padding='same',name = conv_name_base+'2b',kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3,name= bn_name_base+'2b')(X)
    X_shortcut = Conv2D(filters=F3,kernel_size=(1,1),strides=(s,s),padding='valid',name = conv_name_base+'1',
                       kernel_initializer=glorot_uniform(seed=0))(X_shortcut)
    X_shortcut = BatchNormalization(axis=3,name = bn_name_base+'1')(X_shortcut)
    X = Add()([X,X_shortcut])
    X = Activation('relu')(X)
    return X 

def ResNet8(input_shape,classes):
    X_input = Input(input_shape)
    X = Conv2D(filters = 32,kernel_size=(7,7),strides=(2,2),name='conv1',kernel_initializer=glorot_uniform(seed=0))(X_input)
    X = BatchNormalization(axis=3, name='bn_conv1')(X)
    X = Activation('relu')(X)
    X = MaxPooling2D(pool_size=(3,3),strides=(2,2))(X)
    X = convolutional_block(X,f=3,filters=[32,32,32],stage=2,block='a',s=1)
    X = identify_block(X, f=3, filters=[32,32,64], stage=1, block="b")
    X = convolutional_block(X, f=3, filters=[64,64,64], stage=3, block="c", s=2)
    X = Conv2D(filters = 128,kernel_size=(3,3),strides=(1,1),name='conv1_last',kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name='bn_conv1_last')(X)
    X = Activation('relu')(X)
    X = AveragePooling2D(pool_size=(2,2),padding='same')(X)
    X = Flatten()(X)
#     X = Dropout(0.6)(X)
#     X = Dense(512,activation = "relu")(X)
#     X = Dropout(0.8)(X)
    X = Dense(classes,activation="softmax",name='fc'+str(classes),kernel_initializer=glorot_uniform(seed=0))(X)
#     X = Dropout(0.2)(X)
    model = Model(inputs = X_input,output = X, name= 'ResNet8')
    return model

def train(dataGen, model,train_x,train_y,test_x,test_y):
    opt = Adam(lr=Lr, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
    earlyStopping = EarlyStopping(monitor='val_loss',patience = 10,verbose=1,mode='auto')
    checkpointer = ModelCheckpoint(filepath="weight/weight_{epoch:02d}_loss_{loss:.5f}_acc_{acc:.5f}_vloss_{val_loss:.5f}_vacc_{val_acc:.5f}.hdf5",
                                   verbose=Verbose,save_best_only=True)
    reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=10, mode='auto')
    model.compile(loss="categorical_crossentropy",
                  optimizer=opt,metrics=["accuracy"])
    _history = model.fit_generator(dataGen.flow(train_x,train_y,batch_size=BatchSize),
                                    validation_data=(test_x,test_y),
                                    steps_per_epoch=len(train_x)//BatchSize,
                                    epochs=Epoch,
                                    verbose=Verbose,
                                    shuffle = True,
                                    callbacks = [checkpointer,reduce_lr])
    plt.style.use("ggplot")
    plt.figure()
    plt.plot(np.arange(0,Epoch),_history.history["loss"],label ="train_loss")
    plt.plot(np.arange(0,Epoch),_history.history["val_loss"],label="val_loss")
    plt.plot(np.arange(0,Epoch),_history.history["acc"],label="train_acc")
    plt.plot(np.arange(0,Epoch),_history.history["val_acc"],label="val_acc")
    plt.title("loss and accuracy")
    plt.xlabel("epoch")
    plt.ylabel("loss/acc")
    plt.legend(loc="best")
    plt.savefig("result.png")
    plt.show()

4.Training and Evaluate

if mode=="train":
#     train_x, train_y = load_data(path_tr, ImgSize, ClassNum)
#     print("Find :")
#     print("Train data : "+str(len(train_x)))
#     test_x, test_y = load_data(path_te, ImgSize, ClassNum)
#     print("Test data : "+str(len(test_x)))
    
    train_data, test_data, train_label, test_label = load_data_for_train(path_tr, ImgSize, ClassNum)
    train_x = train_data
    train_y = train_label
    test_x = test_data 
    test_y = test_label
    print("Find :")
    print("Train data : "+str(len(train_x)))
    print("Test data : "+str(len(test_x)))
    model = ResNet8(input_shape=(ImgSize,ImgSize,Channel),classes=ClassNum)
    if save_model_image:
        plot_model(model, to_file='./model2.png', show_shapes=True)
#     model.summary()
    dataGen = ImageDataGenerator(rotation_range=45,
                                 width_shift_range=0,
                                 height_shift_range=0,
                                 shear_range=0.1,
                                 zoom_range=0,
                                 horizontal_flip=False,
                                 fill_mode="nearest")
else:
    pass
if mode=="train":
    print("Start training ...")
    train(dataGen,model,train_x,train_y,test_x,test_y)
else:
    pass
if mode=="train":
    pass
else:
    if mode == "retrain":
        model = load_model(path_mo)
        train_data, test_data, train_label, test_label = load_data_for_train(path_tr, ImgSize, ClassNum)
        train_x = train_data
        train_y = train_label
        test_x = test_data 
        test_y = test_label
        dataGen = ImageDataGenerator(rotation_range=45,
                                     width_shift_range=0,
                                     height_shift_range=0,
                                     shear_range=0.1,
                                     zoom_range=0,
                                     horizontal_flip=False,
                                     fill_mode="nearest")
        train(dataGen,model,train_x,train_y,test_x,test_y)
    else:
        model = load_model(path_mo)

5.prediction

if mode=="train":
    pass
else:
    if mode=="test":
        path = path_va
        dirs = os.listdir(path)
        err = 0
        tru = 0
        i = 1
        for files in dirs:
            img = cv2.imread(path+files)
            image = cv2.resize(img,(ImgSize,ImgSize),interpolation=cv2.INTER_CUBIC)
            image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
            if image.mean() <= 200:
                #超好用的提升亮度
                image = np.uint8(np.clip((1.5 * image + 30), 0, 255))
                image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)          
            image = image.reshape((1,ImgSize,ImgSize,3))
            _str = files.split('.', -1)[0]
            label = _str.split('_', -1)[1]
            data = np.array(image,dtype="float")/255.0
            y_m = model.predict(data)
            p = np.argmax(y_m,axis=1)# axis=1取行最大值索引
            if p[0] == int(label):
                tru = tru + 1
            else:
                print(str(i)+". "+str(p[0])+" ! "+str(label)+" @ "+files)
                err = err + 1
                i = i + 1
        res = round((1 - err/len(dirs)),5) * 100
        print("Total "+ str(len(dirs))+" images : " + str(res) +"%" )
    else:
        pass