Esempio n. 1
0
def make_model(X,
               class_names,
               nb_layers=4,
               try_checkpoint=True,
               weights_file='weights.hdf5',
               quiet=False,
               missing_weights_fatal=False):
    ''' In the following, the reason we hang on to & return serial_model,
         is because Keras can't save parallel models, but according to fchollet
         the serial & parallel versions will always share the same weights
         (Strange but true!)
    '''

    gpu_count = get_available_gpus()

    if gpu_count <= 1:
        serial_model = MyCNN_Keras2(X,
                                    nb_classes=len(class_names),
                                    nb_layers=nb_layers)
    else:
        with tf.device("/cpu:0"):
            serial_model = MyCNN_Keras2(X,
                                        nb_classes=len(class_names),
                                        nb_layers=nb_layers)

    # Initialize weights using checkpoint if it exists.
    if (try_checkpoint):
        print("Looking for previous weights...")
        if (isfile(weights_file)):
            print('Weights file detected. Loading from ', weights_file)
            loaded_model = load_model(
                weights_file
            )  # strip any previous parallel part, to be added back in later
            serial_model.set_weights(loaded_model.get_weights()
                                     )  # assign weights based on checkpoint
        else:
            if (missing_weights_fatal):
                print("Need weights file to continue.  Aborting")
                assert (not missing_weights_fatal)
            else:
                print('No weights file detected, so starting from scratch.')

    if (gpu_count >= 2):
        print(" Parallel run on", gpu_count, "GPUs")
        model = make_parallel(serial_model, gpu_count=gpu_count)
    else:
        model = serial_model

    opt = Adam(lr=0.00001)  #
    loss = 'categorical_crossentropy'
    metrics = ['accuracy']
    model.compile(loss=loss, optimizer=opt, metrics=metrics)
    serial_model.compile(loss=loss, optimizer=opt, metrics=metrics)

    if (not quiet):
        print("Summary of serial model (duplicated across", gpu_count,
              "GPUs):")
        serial_model.summary()  # print out the model layers

    return model, serial_model
Esempio n. 2
0
def setup_model(X, class_names, nb_layers=4, try_checkpoint=True,
    weights_file='weights.hdf5', quiet=False, missing_weights_fatal=False, multi_tag=False):
    ''' In the following, the reason we hang on to & return serial_model,
         is because Keras can't save parallel models, but according to fchollet
         the serial & parallel versions will always share the same weights
         (Strange but true!)
    '''

    # Here's where one might 'swap out' different neural network 'model' choices
    serial_model = MyCNN_Keras2(X.shape, nb_classes=len(class_names), nb_layers=nb_layers)
    #serial_model = old_model(X.shape, nb_classes=len(class_names), nb_layers=nb_layers)
    #serial_model = imageModels(X, nb_classes=len(class_names))

    # don't bother with freezing layers, at least with the hope of trianing on a laptop. doesn't speed up by more than a factor of 2.
    # serial_model = freeze_layers(serial_model, train_last = 3)

    # Initialize weights using checkpoint if it exists.
    if (try_checkpoint):
        print("Looking for previous weights...")
        if ( isfile(weights_file) ):
            print ('Weights file detected. Loading from ',weights_file)
            loaded_model = load_model(weights_file)   # strip any previous parallel part, to be added back in later
            serial_model.set_weights( loaded_model.get_weights() )   # assign weights based on checkpoint
        else:
            if (missing_weights_fatal):
                print("Need weights file to continue.  Aborting")
                assert(not missing_weights_fatal)
            else:
                print('No weights file detected, so starting from scratch.')


    opt = 'adadelta' # Adam(lr = 0.00001)  # So far, adadelta seems to work the best of things I've tried
    #opt = 'adam'
    metrics = ['accuracy']

    if (multi_tag):     # multi_tag means more than one class can be 'chosen' at a time; default is 'only one'
        loss = 'binary_crossentropy'
    else:
        loss = 'categorical_crossentropy'

    serial_model.compile(loss=loss, optimizer=opt, metrics=metrics)

    # Multi-GPU "parallel" capability
    gpu_count = get_available_gpus()
    if (gpu_count >= 2):
        print(" Parallel run on",gpu_count,"GPUs")
        model = make_parallel(serial_model, gpu_count=gpu_count)
        model.compile(loss=loss, optimizer=opt, metrics=metrics)
    else:
        model = serial_model

    if (not quiet):
        print("Summary of serial model (duplicated across",gpu_count,"GPUs):")
        serial_model.summary()  # print out the model layers

    return model, serial_model   # fchollet says to hang on to the serial model for checkpointing