示例#1
0
def augmented_train_mobilenet_with_data_generator(model_path):
    """
    To train a pre-trained model.
    :param model_path: pretrained model path.
    :return:
    """

    # STEPS_PER_EPOCH * BATCH_SIZE = DATA_SIZE
    EPOCH = 5
    DATA_SIZE = 100
    BATCH_SIZE = 20
    STEPS_PER_EPOCH = DATA_SIZE / BATCH_SIZE

    CLASS_NUM = 2

    INPUT_SHAPE = (224, 224)
    MODEL_PATH_EACH_EPOCH = '../model/gender/gender.mobilenet.augment.{epoch:02d}-{loss:.2f}.model'

    FINAL_MODEL_PATH = "../models/gender/gender.mobilenet.augment.final.model"

    checkpoint = keras.callbacks.ModelCheckpoint(MODEL_PATH_EACH_EPOCH,
                                                 monitor='loss',
                                                 verbose=1)

    model = keras.models.load_model(model_path,
                                    custom_objects={
                                        'relu6': relu6,
                                        'DepthwiseConv2D': DepthwiseConv2D
                                    })

    X_test, Y_test = dataset.load_data_from_config(
        "../data/gender/gender_test.txt", input_shape=INPUT_SHAPE)

    encoder = LabelEncoder()
    encoder.fit(["F", "M"])
    encoded_Y_test = encoder.transform(Y_test).astype(np.int32)

    ohr_Y_test = np_utils.to_categorical(encoded_Y_test, CLASS_NUM)

    generator = dataset.data_generator("../data/gender/gender_train.txt",
                                       encoder,
                                       CLASS_NUM,
                                       input_shape=INPUT_SHAPE,
                                       batch_size=BATCH_SIZE)

    mobilenet = MobileNet(model)

    mobilenet.run_generator(generator,
                            loss="categorical_crossentropy",
                            optimizer="sgd",
                            epoch=EPOCH,
                            steps_per_epoch=STEPS_PER_EPOCH,
                            val_data=(X_test, ohr_Y_test),
                            augment=True,
                            callbacks=[checkpoint])

    mobilenet.save(FINAL_MODEL_PATH)
示例#2
0
def train_mobilenet_with_data_generator():

    # STEPS_PER_EPOCH * BATCH_SIZE = DATA_SIZE
    EPOCH = 5
    DATA_SIZE = 100
    BATCH_SIZE = 20
    STEPS_PER_EPOCH = DATA_SIZE / BATCH_SIZE

    CLASS_NUM = 2

    INPUT_SHAPE = (224, 224)
    MODEL_PATH_EACH_EPOCH = '../model/gender/gender.mobilenet.{epoch:02d}-{loss:.2f}.model'
    FINAL_MODEL_PATH = '../models/gender/gender.mobilenet.final.model'

    checkpoint = keras.callbacks.ModelCheckpoint(MODEL_PATH_EACH_EPOCH,
                                                 monitor='loss',
                                                 verbose=1)
    tensorboard = keras.callbacks.TensorBoard(log_dir='../logs',
                                              histogram_freq=0,
                                              batch_size=8,
                                              write_graph=True,
                                              write_grads=False,
                                              write_images=False,
                                              embeddings_freq=0,
                                              embeddings_layer_names=None,
                                              embeddings_metadata=None)

    X_test, Y_test = dataset.load_data_from_config(
        "../data/gender/gender_test.txt", input_shape=INPUT_SHAPE)

    encoder = LabelEncoder()
    encoder.fit(["F", "M"])
    encoded_Y_test = encoder.transform(Y_test).astype(np.int32)

    ohr_Y_test = np_utils.to_categorical(encoded_Y_test, CLASS_NUM)

    generator = dataset.data_generator("../data/gender/gender_train.txt",
                                       encoder,
                                       2,
                                       input_shape=INPUT_SHAPE,
                                       batch_size=BATCH_SIZE)

    net = MobileNet()
    net.build_network(include_top=True,
                      input_shape=(224, 224, 3),
                      output_shape=CLASS_NUM)
    net.run_generator(generator,
                      loss="categorical_crossentropy",
                      optimizer="sgd",
                      epoch=EPOCH,
                      steps_per_epoch=STEPS_PER_EPOCH,
                      val_data=(X_test, ohr_Y_test),
                      callbacks=[checkpoint, tensorboard])

    net.save(FINAL_MODEL_PATH)
示例#3
0
def run_hybrid_xception():

    genders = dataset.load_labels("../data/gender_label.txt")
    births = dataset.load_labels("../data/age_label.txt")
    x_test, y_test = dataset.load_data_from_config("../data/hybrid_test.txt",
                                                   input_shape=(299, 299))

    gender_y_test = [y.split("_")[0] for y in y_test]
    birth_y_test = [y.split("_")[1] for y in y_test]

    gender_encoder = LabelEncoder()
    gender_encoder.fit(genders)
    encoded_gender_y_test = gender_encoder.transform(gender_y_test).astype(
        np.int32)
    one_hot_gender_y_test = np_utils.to_categorical(
        encoded_gender_y_test,
        np.max(encoded_gender_y_test) + 1)

    birth_encoder = LabelEncoder()
    birth_encoder.fit(births)
    encoded_birth_y_test = birth_encoder.transform(birth_y_test).astype(
        np.int32)
    one_hot_birth_y_test = np_utils.to_categorical(
        encoded_birth_y_test,
        np.max(encoded_birth_y_test) + 1)

    hybrid_y_test = []
    for i in range(len(one_hot_gender_y_test)):
        hybrid_y_test.append(
            np.append(one_hot_gender_y_test[i], one_hot_birth_y_test[i]))
    hybrid_y_test = np.asarray(hybrid_y_test)

    # ohr_Y_train = np_utils.to_categorical(encoded_age_Y_train, np.max(encoded_age_Y_train) + 1)
    # ohr_Y_test = np_utils.to_categorical(encoded_Y_test, np.max(encoded_Y_test) + 1)

    network = Xception()

    network.build_network(include_top=True,
                          input_shape=(299, 299, 3),
                          output_shape=hybrid_y_test[0].__len__())

    network.run_generator(
        dataset.data_hybrid_generator("../data/hybrid_train.txt",
                                      gender_encoder, birth_encoder,
                                      genders.__len__(), births.__len__(), 1),
        loss="categorical_crossentropy",
        optimizer="sgd",
        steps_per_epoch=8,
        val_data=(x_test, hybrid_y_test))  # (X_test, ohr_Y_test)
示例#4
0
def train_mobilenet_with_data_generator(batch_size, input_shape):

    class_num = birth_year.__len__()

    filepath = '../models/birth/birth.mobilenet.{epoch:02d}-{loss:.2f}.model'
    checkpoint = keras.callbacks.ModelCheckpoint(filepath,
                                                 monitor='loss',
                                                 verbose=1)
    tensorboard = keras.callbacks.TensorBoard(log_dir='../logs',
                                              histogram_freq=0,
                                              batch_size=batch_size,
                                              write_graph=True,
                                              write_grads=False,
                                              write_images=False,
                                              embeddings_freq=0,
                                              embeddings_layer_names=None,
                                              embeddings_metadata=None)

    X_test, Y_test = dataset.load_data_from_config(
        "../data/birth/birth_test.txt")

    encoder = LabelEncoder()
    encoder.fit(birth_year)
    encoded_Y_test = encoder.transform(Y_test).astype(np.int32)

    ohr_Y_test = np_utils.to_categorical(encoded_Y_test, class_num)

    generator = dataset.data_generator("../data/birth/birth_train.txt",
                                       encoder,
                                       class_num,
                                       batch_size=batch_size)

    net = MobileNet()
    net.build_network(include_top=True,
                      input_shape=(224, 224, 3),
                      output_shape=class_num)
    net.run_generator(generator,
                      loss="categorical_crossentropy",
                      optimizer="sgd",
                      epoch=1,
                      steps_per_epoch=10,
                      val_data=(X_test, ohr_Y_test),
                      callbacks=[checkpoint, tensorboard])

    net.save("../model/birth/birth.mobilenet.h5")
示例#5
0
def train_mobilenet_with_data_generator():

    filepath = '../emotion/emotion.mobilenet.{epoch:02d}-{loss:.2f}.model'
    checkpoint = keras.callbacks.ModelCheckpoint(filepath,
                                                 monitor='loss',
                                                 verbose=1)
    tensorboard = keras.callbacks.TensorBoard(log_dir='../logs',
                                              histogram_freq=0,
                                              batch_size=8,
                                              write_graph=True,
                                              write_grads=False,
                                              write_images=False,
                                              embeddings_freq=0,
                                              embeddings_layer_names=None,
                                              embeddings_metadata=None)

    X_test, Y_test = dataset.load_data_from_config(
        "../data/emotion/emotion_test.txt", input_shape=(224, 224))

    encoder = LabelEncoder()
    encoder.fit(["0", "1", "2", "3", "4", "5", "6"])
    encoded_Y_test = encoder.transform(Y_test).astype(np.int32)

    ohr_Y_test = np_utils.to_categorical(encoded_Y_test, 7)

    generator = dataset.data_generator("../data/emotion/emotion_train.txt",
                                       encoder,
                                       7,
                                       input_shape=(224, 224),
                                       batch_size=20)

    net = MobileNet()
    net.build_network(include_top=True,
                      input_shape=(224, 224, 3),
                      output_shape=7)
    net.run_generator(generator,
                      loss="categorical_crossentropy",
                      optimizer="sgd",
                      epoch=2,
                      steps_per_epoch=10,
                      val_data=(X_test, ohr_Y_test),
                      callbacks=[checkpoint, tensorboard])

    net.save("../models/emotion/emotion.mobilenet.final.model")