keras实现mobilenet_v2

数据集结构如第一篇文章(keras实现LeNet5)。

1.model.py

from keras.models import Model
from keras.layers import Input, Conv2D, GlobalAveragePooling2D, Dropout
from keras.layers import Activation, BatchNormalization, add, Reshape
from keras.layers import DepthwiseConv2D
from keras import backend as K

def relu6(x):
    return K.relu(x, max_value=6.0)

def _conv_block(inputs, filters, kernel, strides):

    channel_axis = 1 if K.image_data_format() == 'channels_first' else -1

    x = Conv2D(filters, kernel, padding='same', strides=strides)(inputs)
    x = BatchNormalization(axis=channel_axis)(x)
    return Activation(relu6)(x)


def _bottleneck(inputs, filters, kernel, t, s, r=False):

    channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
    tchannel = K.int_shape(inputs)[channel_axis] * t

    x = _conv_block(inputs, tchannel, (1, 1), (1, 1))

    x = DepthwiseConv2D(kernel, strides=(s, s), depth_multiplier=1, padding='same')(x)
    x = BatchNormalization(axis=channel_axis)(x)
    x = Activation(relu6)(x)

    x = Conv2D(filters, (1, 1), strides=(1, 1), padding='same')(x)
    x = BatchNormalization(axis=channel_axis)(x)

    if r:
        x = add([x, inputs])
    return x


def _inverted_residual_block(inputs, filters, kernel, t, strides, n):

    x = _bottleneck(inputs, filters, kernel, t, strides)

    for i in range(1, n):
        x = _bottleneck(x, filters, kernel, t, 1, True)

    return x


def MobileNetv2(input_shape, k):

    inputs = Input(shape=input_shape)
    x = _conv_block(inputs, 32, (3, 3), strides=(2, 2))

    x = _inverted_residual_block(x, 16, (3, 3), t=1, strides=1, n=1)
    x = _inverted_residual_block(x, 24, (3, 3), t=6, strides=2, n=2)
    x = _inverted_residual_block(x, 32, (3, 3), t=6, strides=2, n=3)
    x = _inverted_residual_block(x, 64, (3, 3), t=6, strides=2, n=4)
    x = _inverted_residual_block(x, 96, (3, 3), t=6, strides=1, n=3)
    x = _inverted_residual_block(x, 160, (3, 3), t=6, strides=2, n=3)
    x = _inverted_residual_block(x, 320, (3, 3), t=6, strides=1, n=1)

    x = _conv_block(x, 1280, (1, 1), strides=(1, 1))
    x = GlobalAveragePooling2D()(x)
    x = Reshape((1, 1, 1280))(x)
    x = Dropout(0.3, name='Dropout')(x)
    x = Conv2D(k, (1, 1), padding='same')(x)

    x = Activation('softmax', name='softmax')(x)
    output = Reshape((k,))(x)

    model = Model(inputs, output)

    return model


# if __name__ == '__main__':
#     MobileNetv2((224, 224, 3), 1000)

2.train.py

import os,sys
import numpy as np
import scipy
from scipy import ndimage
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
from PIL import Image
import random
import cv2
import os
import model
import keras

def one_hot(data, num_classes):
  return np.squeeze(np.eye(num_classes)[data.reshape(-1)])

def load_dataset1(path,enhance_label):
    dataset = []
    labels = []
    f1=open(path+'label-shuffle.txt','r')
    for line in f1.readlines():
        file_name=line.strip().split(' ')[0]
        label=int(line.strip().split(' ')[-1])
        # print(file_name,label)
        if enhance_label==0:
            # 1.原数据加载
            pic=cv2.imread(path+file_name,1)
            pic=cv2.resize(pic,(224,224), interpolation=cv2.INTER_CUBIC)
            # pic= pic.reshape(28, 28, 1)
            dataset.append(pic)
            labels.append(label)
        if enhance_label==1:
            # 2.数据随机增强后加载
            pic=random_enhance(path,file_name)
            dataset.append(pic)
            labels.append(label)

    dataset=np.array(dataset)
    labels=np.array(labels)
    labels=one_hot(labels, 2)
    return dataset, labels

train_path='data2/train/'
test_path='data2/test/'

enhance_label=0
train_data,train_label=load_dataset1(train_path,enhance_label)

train_data = train_data.reshape(-1, 224, 224, 3)  # normalize
print(train_data.shape,train_label.shape)


model = model.MobileNetv2((224, 224, 3), 2)
model.compile(
          optimizer=keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0),
          metrics=['accuracy'],   #评价指标
          loss='categorical_crossentropy')   #计算损失---分类交叉熵函数,  binary_crossentropy(二分类)

# 训练方法二
models = model.fit(
        train_data,
        train_label,
        batch_size=64,
        epochs=2,
        verbose=1,
        shuffle=True,
        initial_epoch=0,   #从指定的epoch开始训练,在这之前的训练时仍有用。
        validation_split=0.1   #0~1之间,用来指定训练集的一定比例数据作为验证集
        # validation_data=(test_data, test_label)   #指定的验证集,此参数将覆盖validation_spilt。
)

log_dir="model/"
model.save(log_dir+'m2.h5')   #保存最后一次迭代的模型
model.save_weights(log_dir+'m1.h5')

3.test.py

from keras.preprocessing.image import load_img
from keras.models import Sequential
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
from keras.models import load_model
import tensorflow as tf
import keras
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation, Conv2D, MaxPooling2D, Flatten
from keras.optimizers import Adam
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import cv2
import os,sys
import numpy as np
import scipy
from scipy import ndimage
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
from PIL import Image
import random
import cv2
import os


#预测结果返回0、1
def get_result(pre):
    if pre[0][0]>pre[0][1]:
        return 0
    else:
        return 1


#按标签批量预测
def get_acc(model, path):
    n=0
    total=0
    f1 = open(path + 'label-shuffle.txt', 'r')
    for line in f1.readlines():
        total += 1
        file_name=line.strip().split(' ')[0]
        label=int(line.strip().split(' ')[-1])
        # print(file_name,label)

        # # keras加载图片
        # img = load_img(path + file_name, target_size=(224, 224))
        # # img = image.img_to_array(img) / 255.0
        # img = np.expand_dims(img, axis=0)

        # opencv加载图片
        img=cv2.imread(path + file_name)
        img=cv2.resize(img,(224,224), interpolation=cv2.INTER_CUBIC)
        img = img.reshape(224, 224, 3)  # normalize
        # img = image.img_to_array(img) / 255.0
        img = np.expand_dims(img, axis=0)
        img=np.array(img)
        
        predictions = model.predict(img)
        result = get_result(predictions)
        print("pre_value:", result,predictions,'---'+str(total))

        if result==label:
            n += 1
    acc=n/total
    print("acc:",acc)
    return acc


model = ResNet50(weights=None,classes=2)

model.load_weights('model/m2.h5')

# 批量预测
path='E:/eye_dataset/test/eye/'
path2='data2/test/'
acc=get_acc(model,path2)
print("pre_acc:",acc)

你可能感兴趣的:(睁闭眼检测,深度学习,深度学习,keras,mobilenet)