Example #1
0
def mLeNetDefault_Features_Dense(input_shape,
                                 num_classes,
                                 steps_per_epoch,
                                 epochs,
                                 use_cache=False,
                                 dataset='dataset1'):
    model_name = 'LeNetDefault_Features_Dense'
    helpers.createFoldersForModel(model_name, dataset)
    print("===================== " + model_name +
          " model ====================")

    losses = {
        'classifierOutput': classifierLossFunction,
        'featuresOutput': featuresLossFunction
    }
    metrics = {
        'classifierOutput': classifierAccuracy,
        'featuresOutput': featuresAccuracy
    }
    weights = {'classifierOutput': 1.0, 'featuresOutput': 1.0}

    if existsModelCache(model_name, dataset) and use_cache:
        model = loadModel(model_name, dataset)
        model.compile(loss=losses,
                      metrics=metrics,
                      optimizer='rmsprop',
                      run_eagerly=True,
                      loss_weights=weights)
        evaluateModel(model, dataset, input_shape, steps_per_epoch, epochs,
                      True)
        return model

    model = getDefaultModel(input_shape)
    dense1 = Dense(120)(model.layers[-1].output)
    dense2 = Dense(84)(dense1)
    dense3 = Dense(units=64, activation="relu")(dense2)
    classifierOutput = Dense(num_classes,
                             activation='softmax',
                             name='classifierOutput')(dense3)
    featuresOutput = Flatten(name='featuresOutput')(model.layers[-1].output)
    # define new model
    model = Model(inputs=model.inputs,
                  outputs=[classifierOutput, featuresOutput])

    model.compile(loss=losses,
                  metrics=metrics,
                  optimizer='rmsprop',
                  run_eagerly=True,
                  loss_weights=weights)
    model.run_eagerly = True
    [history, time_taken] = fitModel(model, dataset, input_shape,
                                     steps_per_epoch, epochs, True)

    if use_cache:
        saveModel(model, model_name, dataset)

    createAndSaveCurvesFeatures(history, model_name, dataset)

    return model
Example #2
0
def mLeNetDefault(input_shape,
                  num_classes,
                  steps_per_epoch,
                  epochs,
                  use_cache=False,
                  dataset='dataset1'):
    model_name = 'LeNetDefault'
    helpers.createFoldersForModel(model_name, dataset)
    print("===================== " + model_name +
          " model ====================")
    if existsModelCache(model_name, dataset) and use_cache:
        model = loadModel(model_name, dataset)
        model.compile(loss='categorical_crossentropy',
                      optimizer='rmsprop',
                      metrics=['accuracy'])
        evaluateModel(model, dataset, input_shape, steps_per_epoch, epochs)
        return model

    model = getDefaultModel(input_shape)
    model.add(Dense(120))
    model.add(Dense(84))
    model.add(Dense(num_classes, activation='softmax'))
    # model.add(Activation('softmax'))
    model.compile(loss='categorical_crossentropy',
                  optimizer='rmsprop',
                  metrics=['accuracy'])

    [history, time_taken] = fitModel(model, dataset, input_shape,
                                     steps_per_epoch, epochs, False)

    if use_cache:
        saveModel(model, model_name, dataset)

    createAndSaveCurves(history, model_name, dataset)

    return model