def my_assessor_model(mouth_nn='cnn', mouth_features_dim=512, lstm_units_1=32, dense_fc_1=128, dense_fc_2=64,
                      conv_f_1=32, conv_f_2=64, conv_f_3=128, dropout_p=0.2):

    mouth_input_shape = (MOUTH_H, MOUTH_W, MOUTH_CHANNELS)
    my_input_mouth_images = Input(shape=(TIME_STEPS, *mouth_input_shape))
    my_input_head_poses = Input(shape=(TIME_STEPS, 3))
    my_input_n_of_frames = Input(shape=(1,))
    my_input_lipreader_dense = Input(shape=(1024,))
    my_input_lipreader_softmax = Input(shape=(500,))

    if mouth_nn == 'cnn':
        mouth_feature_model = my_timedistributed_cnn_model((TIME_STEPS, *mouth_input_shape), conv_f_1, conv_f_2, conv_f_3, mouth_features_dim)
    elif mouth_nn == 'resnet18':
        mouth_feature_model = ResnetBuilder.build_resnet_18((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True)
    elif mouth_nn == 'resnet34':
        mouth_feature_model = ResnetBuilder.build_resnet_34((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True)
    elif mouth_nn == 'resnet50':
        mouth_feature_model = ResnetBuilder.build_resnet_50((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True)
    elif mouth_nn == 'resnet101':
        mouth_feature_model = ResnetBuilder.build_resnet_101((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True)
    elif mouth_nn == 'resnet152':
        mouth_feature_model = ResnetBuilder.build_resnet_152((TIME_STEPS, *mouth_input_shape), mouth_features_dim, time_distributed=True)
    elif mouth_nn == 'flatten':
        mouth_feature_model = Reshape((TIME_STEPS, -1), input_shape=(TIME_STEPS, *mouth_input_shape))

    cnn_features = mouth_feature_model(my_input_mouth_images)

    lstm_input = Concatenate()([cnn_features, my_input_head_poses])

    lstm_output = LSTM(lstm_units_1, activation='tanh', kernel_regularizer=l2(1.e-4), return_sequences=False)(lstm_input)

    concatenated_features = Concatenate()([lstm_output, my_input_n_of_frames, my_input_lipreader_dense, my_input_lipreader_softmax])

    fc1 = Dense(dense_fc_1, activation='relu', kernel_regularizer=l2(1.e-4))(concatenated_features)

    bn1 = BatchNormalization()(fc1)

    dp1 = Dropout(dropout_p)(bn1)

    fc2 = Dense(dense_fc_2, activation='relu', kernel_regularizer=l2(1.e-4))(dp1)

    bn2 = BatchNormalization()(fc2)

    dp2 = Dropout(dropout_p)(bn2)

    assessor_output = Dense(1, activation='sigmoid')(dp2)

    assessor = Model(inputs=[my_input_mouth_images, my_input_head_poses, my_input_n_of_frames, my_input_lipreader_dense, my_input_lipreader_softmax],
                     outputs=assessor_output)

    assessor.summary()

    return assessor
Пример #2
0
def get_model(model, ishape, nb_classes):
    "Return proper ResNet model"
    if model.endswith('18'):
        model = ResnetBuilder.build_resnet_18(ishape, nb_classes)
    elif model.endswith('34'):
        model = ResnetBuilder.build_resnet_34(ishape, nb_classes)
    elif model.endswith('50'):
        model = ResnetBuilder.build_resnet_50(ishape, nb_classes)
    elif model.endswith('101'):
        model = ResnetBuilder.build_resnet_101(ishape, nb_classes)
    elif model.endswith('152'):
        model = ResnetBuilder.build_resnet_152(ishape, nb_classes)
    else:
        return NotImplemented()
    return model
Пример #3
0
    def build_resnet(self, model_name, lr):
        input_shape = (128, 128, 3)
        num_outputs = 100
        model = None
        if model_name == "resnet18":
            model = ResnetBuilder.build_resnet_18(input_shape, num_outputs)
        elif model_name == "resnet34":
            model = ResnetBuilder.build_resnet_34(input_shape, num_outputs)
        elif model_name == "resnet50":
            model = ResnetBuilder.build_resnet_50(input_shape, num_outputs)
        elif model_name == "resnet50_keras":
            inp = Input((224, 224, 3))
            model = ResNet50(input_tensor=inp, weights=None, classes=100)
        else:
            raise

        # opt = SGD(lr=lr, momentum=0.9, nesterov=True)
        opt = Adam()
        model.compile(optimizer=opt,
                      loss="categorical_crossentropy",
                      metrics=['accuracy'])
        return model
    def model_confirm(self, choosed_model):
        if choosed_model == 'AlexNet':
            model = alexnet(self.config)
        elif choosed_model == 'VGG16':
            model = vgg16(self.config)
        elif choosed_model == 'VGG19':
            model = vgg19(self.config)
        elif choosed_model == 'InceptionV3':
            model = inception_v3(self.config)
        elif choosed_model == 'InceptionV4':
            model = inception_v4(self.config)
        elif choosed_model == 'InceptionV4_ResNetV1':
            model = inception_resnet_v1(self.config)
        elif choosed_model == 'InceptionV4_ResNetV2':
            model = inception_resnet_v2(self.config)
        elif choosed_model == 'ResNet18':
            model = ResnetBuilder.build_resnet_18(self.config)
        elif choosed_model == 'ResNet34':
            model = ResnetBuilder.build_resnet_34(self.config)
        elif choosed_model == 'ResNet50':
            model = ResnetBuilder.build_resnet_50(self.config)
        elif choosed_model == 'ResNet101':
            model = ResnetBuilder.build_resnet_101(self.config)
        elif choosed_model == 'ResNet152':
            model = ResnetBuilder.build_resnet_152(self.config)
        elif choosed_model == 'DenseNet121':
            model = densenet121(self.config)
        elif choosed_model == 'DenseNet169':
            model = densenet169(self.config)
        elif choosed_model == 'DenseNet201':
            model = densenet201(self.config)
        elif choosed_model == 'DenseNet264':
            model = densenet264(self.config)
        else:
            model = -1

        return model
Пример #5
0
    print('Accuracy:', (TP + TN) / float(TP + TN + FP + FN))
    print('Sensitivity:', TP / float(TP + FN))
    print('Specificity:', TN / float(TN + FP))
    return (TP + TN) / float(TP + TN + FP +
                             FN), TP / float(TP + FN), TN / float(TN + FP)


if __name__ == '__main__':
    K.set_image_data_format('channels_first')
    k_total = 10  # 交叉验证次数
    f = xlwt.Workbook()
    sheet = f.add_sheet('yourname', cell_overwrite_ok=True)
    for k_count in range(k_total):
        accuracy_max = 0
        # 搭建模型
        model = ResnetBuilder.build_resnet_34((3, 224, 224), 2)
        model_compile(model)
        # X,Y 为数据集,根据需要修改路径和预处理方式
        X, Y = process_test("./Data/test/AD", "./Data/test/NC")
        #k折交叉验证
        skf = StratifiedKFold(n_splits=5)  # 折数可改
        for cnt, (train, test) in enumerate(skf.split(X, Y)):
            # for (batch,batchlabel) in process_image("./Data/test/AD",
            #                            "./Data/test/NC",4):
            #     model.fit(batch, batchlabel, epochs=1, validation_split=0.1)
            model.fit(X[train], Y[test], epochs=100, validation_split=0.2)
            pred = model.predict(np.array(X[test]))
            accuracy, sensitivity, specificity = calculate_metric(
                Y[test], pred)
            # 在准确率较高的时候保存模型参数,也可以每次都保存
            if (accuracy_max < accuracy):
Пример #6
0
from skimage.color import rgb2gray
from skimage.transform import resize
from skimage.io import imshow, imsave

from PIL import Image

import os

import numpy as np

import matplotlib.pyplot as plt

names = ["France", "Greece", "Germany","DE - Sachsen", "Spain", "Italy 2Euro", "Luxembourg", "Italy 1Euro"]

model = ResnetBuilder.build_resnet_34((200, 200, 1), 8)

opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)

model.compile(loss='categorical_crossentropy',
			optimizer=opt,
			metrics=['accuracy'])

path = "B:/Desktop/Projekts/Euro_Pred/pred_img/"

pred_im = Image.open(path + filename)
pred_im = np.array(pred_im)

if not is_gray:
	pred_im = rgb2gray(pred_im)
Пример #7
0
def test_resnet34():
    model = ResnetBuilder.build_resnet_34((3, 224, 224), 100)
    _test_model_compile(model)
Пример #8
0
def test_resnet34():
    for ordering in DIM_ORDERING:
        K.set_image_dim_ordering(ordering)
        model = ResnetBuilder.build_resnet_34((3, 224, 224), 100)
        model.compile(loss="categorical_crossentropy", optimizer="sgd")
        assert True, "Failed to build with '{}' dim ordering".format(ordering)
Пример #9
0
    def build_resnet_34(self, shape, num_classes):
        self.model = ResnetBuilder.build_resnet_34(shape, num_classes)

        print('[+] ResNet-34 model built')
        return self.model
Пример #10
0
    def buildModel(self):

        if K.image_data_format() == 'channels_first':
            input_shape = (self.img_c, self.frames_n, self.img_w, self.img_h)
        else:
            input_shape = (self.frames_n, self.img_w, self.img_h, self.img_c)
        print(self.frames_n, self.img_w, self.img_h, self.img_c, input_shape)

        self.input_data = Input(name='input',
                                shape=input_shape,
                                dtype=np.float32)
        self.labels = Input(name='labels',
                            shape=[self.absolute_max_string_len],
                            dtype=np.float32)
        self.input_length = Input(name='input_len', shape=[1], dtype=np.int64)
        self.label_length = Input(name='label_len', shape=[1], dtype=np.int64)

        if not self.onlyRNN:
            pad = ZeroPadding3D(padding=(1, 2, 2), name='pad')(self.input_data)
            conv1 = Conv3D(64, (3, 5, 5),
                           strides=(1, 2, 2),
                           data_format='channels_last',
                           activation='relu',
                           kernel_initializer='he_normal',
                           name='conv1')(pad)
            maxpool = MaxPooling3D(pool_size=(1, 3, 3),
                                   strides=(1, 2, 2),
                                   name='max1')(conv1)

            print(maxpool.shape)

            # TODO: check if hardcoded 256 can be removed
            resnet = TimeDistributed(ResnetBuilder.build_resnet_34(
                (int(maxpool.shape[-1]), int(
                    maxpool.shape[-2]), int(maxpool.shape[-3])), 256),
                                     name='timedistresnet')(maxpool)
        else:
            resnet = TimeDistributed(Flatten())(self.input_data)
            print(resnet.shape)

        gru1 = Bidirectional(GRU(256,
                                 return_sequences=True,
                                 kernel_initializer='Orthogonal',
                                 name='gru1'),
                             merge_mode='concat')(resnet)
        gru2 = Bidirectional(GRU(256,
                                 return_sequences=True,
                                 kernel_initializer='Orthogonal',
                                 name='gru2'),
                             merge_mode='concat')(gru1)

        dense = Dense(self.output_size,
                      kernel_initializer='he_normal',
                      name='dense1')(gru2)

        self.y_pred = Activation('softmax', name='softmax')(dense)

        self.loss = CTC(
            'ctc',
            [self.y_pred, self.labels, self.input_length, self.label_length])

        self.model = Model(inputs=[
            self.input_data, self.labels, self.input_length, self.label_length
        ],
                           outputs=self.loss)

        print(self.model.summary())
Пример #11
0
def test_resnet34():
    model = ResnetBuilder.build_resnet_34((3, 224, 224), 100)
    _test_model_compile(model)