def main():
    # Parse command line args
    if len(sys.argv) < 2:
        print 'USAGE: validate-treshold.py project_file model_name best_epoch_number [models_dir]'
        sys.exit(1)
    project_file = sys.argv[1]
    model_name = sys.argv[2]
    best_epoch_number = int(sys.argv[3])
    if len(sys.argv) >= 5:
        models_dir = sys.argv[4]
    else:
        models_dir = '.'

    # Load the project file and the model
    project = Project(project_file)
    model_filename = os.path.join(models_dir, model_name + '.model')
    model = get_model_from_name(project, model_name)
    # Load the weight corresponding to the right epoch
    load_weights(model, model_name, model_filename, best_epoch_number)

    # Generate validation data
    validation_dataset = project.get_validation_dataset()
    validation_genconfig = project.get_validation_genconfig() #using the same validation dataset for threshold fine tuning
    input_data = validation_dataset.generate_input_data(validation_genconfig)
    output_data = validation_dataset.generate_ground_truth_data(validation_genconfig)


    validate_threshold(model,input_data,output_data)
def main():
    # Parse command line args
    if len(sys.argv) < 2:
        print 'USAGE: validate.py project_file.json [models_dir]'
        sys.exit(1)
    project_file = sys.argv[1]
    if len(sys.argv) >= 3:
        models_dir = sys.argv[2]
    else:
        models_dir = '.'
    # Check that the models directory exists
    if not os.path.exists(models_dir):
        raise IOError('The models directory does not exist.')
    # Load project
    project = Project(project_file)
    # Generate validation data
    validation_dataset = project.get_validation_dataset()
    validation_genconfig = project.get_validation_genconfig()
    input_data = validation_dataset.generate_input_data(validation_genconfig)
    output_data = validation_dataset.generate_ground_truth_data(validation_genconfig)
    print str(input_data.shape[0]) + ' validation samples were generated.'
    print 'Each sample contains ' + str(input_data.shape[1]) + ' pixels.'
    # Validate each model
    for i in range(project.get_models_count()):
    #TODO : implement a validation method that doesn't need training to be finished, so that validation error could be visualized while the app is still running
        model = project.get_model(i)
        model_name = project.get_model_name(i)
        print 'Validating model ' + model_name + '...'
        model_filename = os.path.join(models_dir, model_name + '.model')
        validate(model, model_filename, model_name, input_data, output_data)
    # Show all plots
    plt.show()