model_constraint = getattr(models, yamlConfig['KerasModelRetrain'])

    # Instantiate new model with added custom constraints
    if 'L1RegR' in yamlConfig:
        keras_model = model_constraint(Input(shape=X_train_val.shape[1:]),
                                       y_train_val.shape[1],
                                       l1Reg=yamlConfig['L1Reg'],
                                       l1RegR=yamlConfig['L1RegR'],
                                       h5fName=options.dropWeights)
    else:
        keras_model = model_constraint(Input(shape=X_train_val.shape[1:]),
                                       y_train_val.shape[1],
                                       l1Reg=yamlConfig['L1Reg'],
                                       h5fName=options.dropWeights)

    print_model_to_json(keras_model,
                        options.outputDir + '/' + 'KERAS_model.json')

    startlearningrate = 0.0001
    adam = Adam(lr=startlearningrate)
    keras_model.compile(optimizer=adam,
                        loss=[yamlConfig['KerasLoss']],
                        metrics=['accuracy'])

    # Load pre-trained weights!
    keras_model.load_weights(options.inputModel, by_name=True)

    callbacks = all_callbacks(stop_patience=1000,
                              lr_factor=0.5,
                              lr_patience=10,
                              lr_epsilon=0.000001,
                              lr_cooldown=2,
Exemplo n.º 2
0
            #converted_w = convert_kernel(original_w)
            converted_w = original_w
            layer.set_weights(converted_w)

    print 'Summary:'
    totalDropped = sum(
        [len(droppedPerLayer[layer.name]) for layer in model.layers])
    for layer in model.layers:
        print '%i weights dropped from %s out of %i weights' % (len(
            droppedPerLayer[layer.name]), layer.name, layer.count_params())
    print '%i total weights dropped out of %i total weights' % (
        totalDropped, model.count_params())
    print '%.1f%% compression' % (100. * totalDropped / model.count_params())
    model.save(options.outputModel)
    model.save_weights(options.outputModel.replace('.h5', '_weights.h5'))
    print_model_to_json(model, options.outputModel.replace('.h5', '.json'))

    # save binary tensor in h5 file
    h5f = h5py.File(options.outputModel.replace('.h5', '_drop_weights.h5'),
                    'w')
    for layer, binary_tensor in binaryTensorPerLayer.iteritems():
        h5f.create_dataset('%s' % layer, data=binaryTensorPerLayer[layer])
    h5f.close()

    # plot the distribution of weights
    if options.relative_weight_percentile is not None:
        your_percentile = options.relative_weight_percentile
    else:
        your_percentile = stats.percentileofscore(allWeightsArray,
                                                  relative_weight_max)
    #percentiles = [5,16,50,84,95,your_percentile]