Пример #1
0
def load_model():
    """
    加载本地模型
    :return:
    """
    model = CNN3()
    model.load_weights('./models/cnn3_best_weights.h5')
    return model
Пример #2
0
def load_model():
    """
    加载本地模型
    :return:
    """
    from model import CNN3
    model = CNN3()
    model.load_weights('cnn3_best_weights.h5')
    return model
def load_cnn_model():
    """
    载入CNN模型
    :return:
    """
    from model import CNN3
    model = CNN3()
    model.load_weights('./models/cnn3_best_weights.h5')
    return model
Пример #4
0
def predict_no_gen():
    """
    无增广预测
    :return:
    """
    from model import CNN3
    from data import Jaffe
    import numpy as np
    model = CNN3()
    model.load_weights('../models/cnn3_best_weights.h5')
    expression, x_test, y_test = Jaffe().gen_train()
    pred = model.predict(x_test)
    pred = np.argmax(pred, axis=1)
    print(np.sum(pred.reshape(-1) == y_test.reshape(-1)))
Пример #5
0
def predict_data_gen():
    """
    有增广预测
    :return:
    """
    from model import CNN3
    from data import Jaffe
    import numpy as np
    model = CNN3()
    model.load_weights('../models/cnn3_best_weights.h5')
    expression, x_test, y_test = Jaffe().gen_train()
    pred = []
    x_test = np.squeeze(x_test, axis=-1)
    for index in range(x_test.shape[0]):
        faces = generate_faces(x_test[index])
        results = model.predict(faces)
        result_sum = np.sum(results, axis=0)
        label_index = np.argmax(result_sum, axis=0)
        pred.append(label_index)
    pred = np.array(pred)
    print(np.sum(pred.reshape(-1) == y_test.reshape(-1)))
Пример #6
0
def plot_feature_map():
    from model import CNN3
    model = CNN3()
    model.load_weights('../models/cnn3_best_weights.h5')

    def get_feature_map(model, layer_index, channels, input_img):
        from tensorflow.keras import backend as K
        layer = K.function([model.layers[0].input], [model.layers[layer_index].output])
        feature_map = layer([input_img])[0]
        import matplotlib.pyplot as plt
        plt.figure(figsize=(20, 8))
        for i in range(channels):
            img = feature_map[:, :, :, i]
            plt.subplot(4, 8, i + 1)
            plt.imshow(img[0], cmap='gray')
        plt.savefig('rst.png')
        plt.show()
        import cv2
        img = cv2.cvtColor(cv2.imread('../data/demo.jpg'), cv2.cv2.COLOR_BGR2GRAY)
        img.shape = (1, 48, 48, 1)
        get_feature_map(model, 4, 32, img)
Пример #7
0
def get_feature_map(model, layer_index, channels, input_img=None):
    """
    可视化每个卷积层学到的特征图
    :param model:
    :param layer_index:
    :param channels:
    :param input_img:
    :return:
    """
    if not input_img:
        input_img = load_test_image('../data/demo.jpg')
        input_img.shape = (1, 48, 48, 1)
    from keras import backend as K
    layer = K.function([model.layers[0].input], [model.layers[layer_index+1].output])
    feature_map = layer([input_img])[0]
    import matplotlib.pyplot as plt
    plt.figure(figsize=(20, 8))
    for i in range(channels):
        img = feature_map[:, :, :, i]
        plt.subplot(4, 8, i+1)
        plt.imshow(img[0], cmap='gray')
    plt.show()


if __name__ == '__main__':
    from model import CNN3
    model = CNN3()
    model.load_weights('../models/cnn3_best_weights.h5')
    get_feature_map(model, 1, 32)

Пример #8
0
if opt.dataset == "fer2013":
    expressions, x_train, y_train = Fer2013().gen_train()
    _, x_valid, y_valid = Fer2013().gen_valid()
    _, x_test, y_test = Fer2013().gen_test()
    # target编码
    y_train = to_categorical(y_train).reshape(y_train.shape[0], -1)
    y_valid = to_categorical(y_valid).reshape(y_valid.shape[0], -1)
    # 为了统一几个数据集,必须增加一列为0的
    y_train = np.hstack((y_train, np.zeros((y_train.shape[0], 1))))
    y_valid = np.hstack((y_valid, np.zeros((y_valid.shape[0], 1))))
    print(
        "load fer2013 dataset successfully, it has {} train images and {} valid iamges"
        .format(y_train.shape[0], y_valid.shape[0]))

    model = CNN3(input_shape=(48, 48, 1), n_classes=8)
    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    callback = [
        #     EarlyStopping(monitor='val_loss', patience=50, verbose=True),
        #     ReduceLROnPlateau(monitor='lr', factor=0.1, patience=20, verbose=True),
        ModelCheckpoint('../models/cnn2_best_weights.h5',
                        monitor='val_acc',
                        verbose=True,
                        save_best_only=True,
                        save_weights_only=True)
    ]

    train_generator = ImageDataGenerator(rotation_range=10,
Пример #9
0
def load_cnn_model():

    from model import CNN3
    model = CNN3()
    model.load_weights('../models/cnn3_best_weights.h5')
    return model