Beispiel #1
0
def load_model():
    print("**************Please wait....Loading weights*********************")
    global ludwig_model
    model_definition = get_model_definition()

    ludwig_model = LudwigModel(model_definition)

    train_model()

    ludwig_model.load(MODEL_PATH)
    print("weights loaded")
Beispiel #2
0
    def predict(self, mode='predict', ignore_columns=[]):
        predict_dataframe, model_definition = self._create_ludwig_dataframe(
            mode)
        model_definition = self.transaction.hmd['ludwig_data'][
            'model_definition']

        model = LudwigModel.load(
            self.transaction.lmd['ludwig_data']['ludwig_save_path'])

        if self.transaction.lmd['model_order_by'] is None:
            timeseries_cols = []
        else:
            timeseries_cols = list(
                map(lambda x: x[0], self.transaction.lmd['model_order_by']))

        if len(timeseries_cols) > 0:
            predict_dataframe, model_definition = self._translate_df_to_timeseries_format(
                predict_dataframe, model_definition, timeseries_cols)

        for ignore_col in ignore_columns:
            try:
                predict_dataframe[ignore_col] = [None] * len(
                    predict_dataframe[ignore_col])
            except:
                for date_appendage in ['_year', '_month', '_day']:
                    predict_dataframe[ignore_col + date_appendage] = [
                        None
                    ] * len(predict_dataframe[ignore_col + date_appendage])

        with disable_ludwig_output():
            model = LudwigModel.load(
                self.transaction.lmd['ludwig_data']['ludwig_save_path'])
            predictions = model.predict(data_df=predict_dataframe)

        for col_name in predictions:
            col_name_normalized = col_name.replace('_predictions', '')
            predictions = predictions.rename(
                columns={col_name: col_name_normalized})

        return predictions
Beispiel #3
0
def initialize(config):
    pth_mdl = os.path.join( config['pth_mdls'], config['model_to_load'], "model")
    if not os.path.isdir(pth_mdl):
        raise Exception("Could not find the model specified in the models directory: {}".format(pth_mdl))


    global MODL, MMTA, FEAS_IN, FEAS_OUT
    # load a model
    st = time.time()
    print("...loading model from {}".format(pth_mdl))
    MODL = LudwigModel.load(pth_mdl)
    with open(os.path.join(pth_mdl,"train_set_metadata.json")) as f: MMTA = json.load(f)
    print("...loaded model in {:.2f}s".format(time.time()-st))

    FEAS_IN = [fea for fea in MODL.model.hyperparameters['input_features']]
    FEAS_OUT = [fea for fea in MODL.model.hyperparameters['output_features']]
    input_features_desc, output_features_desc = "",""
    for fea in FEAS_IN:
        if fea['type'] == "image":
            input_features_desc += "\t{}\t({}: {})\n".format(fea['name'],fea['type'],"({}, {}) {}".format(fea['width'],fea['height'],chan_count_to_mode(fea['num_channels'])))
        else: input_features_desc += "\t{}\t({})\n".format(fea['name'],fea['type'])
    for fea in FEAS_OUT:
        if fea['type'] == "category":
            fea['meta'] = MMTA[fea['name']]
            output_features_desc += "\t{}\t(category with {} classes)\n".format(fea['name'],fea['num_classes'])
            output_features_desc += "\t\t\t{}\n".format(", ".join(fea['meta']['idx2str']))
        else:
            output_features_desc += "\t{}\t({})\n".format(fea['name'],fea['type'])

    #print(output_features)
    console_msg = """#################### FRESH EYES ####################
I've just loaded a saved model from {0}
To check that the server is working, go to http://localhost:{1}/
It looks like the model I loaded requires the following inputs to make a prediction:
{2}
Make note of these names, as these particular fields will be required by API calls.
It looks like the following values will result from a prediction.
{3}
Make note of these as well, as these fields will be returned to API calls.
Invoke Cntl+C to stop the server
#################### FRESH EYES ####################
"""
    print(console_msg.format(pth_mdl,config['port_num'],input_features_desc, output_features_desc))
Beispiel #4
0
def startjob(
        csv_file_path=r'C:\Users\57855\Desktop\2%test.csv',  #训练文件的路径
        model_file=r'C:\Users\57855\Desktop\2%.yaml',  #模型配置文件路径
        test_file=r'C:\Users\57855\Desktop\2%test_data.csv'):  #结果输出路径
    #Lugwig教程上的代码
    with open(model_file, encoding='utf-8', mode='r') as file:
        model_definition = yaml.load(file.read())
        print(model_definition)
        ludwig_model = LudwigModel(model_definition)
        train_stats = ludwig_model.train(csv_file_path,
                                         logging_level=logging_DEBUG)
        print(train_stats)
        predictions = ludwig_model.predict(test_file,
                                           logging_level=logging_DEBUG)
        print(predictions)
        ludwig_model.close()
Beispiel #5
0
from flask import Flask, request, jsonify  # loading in Flask
from ludwig import LudwigModel  # loading in Ludwig
import pandas as pd  # loading pandas for reading csv

# creating a Flask application
app = Flask(__name__)

# Load the model
model = LudwigModel.load('model')


# creating predict url and only allowing post requests.
@app.route('/predict', methods=['POST'])
def predict():
    # Get data from Post request
    data = request.get_json()
    # Make prediction
    df = pd.DataFrame([str(data['text'])], columns=['content'])
    print(df.head())
    # making predictions
    pred = model.predict(data_df=df)
    print(pred)
    # returning the predictions as json
    return jsonify(pred['airline_sentiment_predictions'][0])


if __name__ == '__main__':
    app.run(port=3000, debug=True)
Beispiel #6
0
from tensorflow.contrib.training.python.training import hparam

from ludwig import LudwigModel

model_definition = {
    "input_features": [{
        "name": "doc_text",
        "type": "text"
    }],
    "output_features": [{
        "name": "class",
        "type": "category"
    }]
}

ludwig_model = LudwigModel(model_definition)
train_stats = ludwig_model.train(
    data_csv="gs://skyl-dev-ml/playground/ludwig/train.csv")

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument(
        '--train-files',
        nargs='+',
        help='Training file local or GCS',
        default=
        'gs://skyl-dev-ml/aashishdahiya/flowers_aashishdahiya_testV3_20181205_134432/preproc/train*'
    )

    parser.add_argument(
Beispiel #7
0
			"type": "sequence",
			"encoder": "rnn",
			"cell_type": "lstm",
			"bidirectional": true,
			"num_layers": 2,
			"reduce_output": null
		}
	],
	"output_features": [
		{
			"name": "intent",
			"type": "category",
			"reduce_input": "sum",
			"num_fc_layers": 1,
			"fc_size": 64
		}
	]
}"""

model_definition = json.loads(data)

ludwig_model = LudwigModel(model_definition)
loaded_ludwig_model = ludwig_model
train_stats = ludwig_model.train(data_csv='train.csv')

ludwig_model.save(MODEL_PATH)
loaded_ludwig_model.load(MODEL_PATH)
predictions = loaded_ludwig_model.predict(data_csv='test.csv')

print(predictions)
Beispiel #8
0
    def train(self):
        training_dataframe, model_definition = self._create_ludwig_dataframe(
            'train')
        if self.transaction.lmd['model_order_by'] is None:
            timeseries_cols = []
        else:
            timeseries_cols = list(
                map(lambda x: x[0], self.transaction.lmd['model_order_by']))

        if len(timeseries_cols) > 0:
            training_dataframe, model_definition = self._translate_df_to_timeseries_format(
                training_dataframe, model_definition, timeseries_cols, 'train')

        with disable_ludwig_output():

            model = LudwigModel(model_definition)

            # <---- Ludwig currently broken, since mode can't be initialized without train_set_metadata and train_set_metadata can't be obtained without running train... see this issue for any updates on the matter: https://github.com/uber/ludwig/issues/295
            #model.initialize_model(train_set_metadata={})
            #train_stats = model.train_online(data_df=training_dataframe) # ??Where to add model_name?? ----> model_name=self.transaction.lmd['name']

            if self.transaction.lmd['rebuild_model'] is True:
                train_stats = model.train(
                    data_df=training_dataframe,
                    model_name=self.transaction.lmd['name'],
                    skip_save_model=True)
            else:
                model = LudwigModel.load(
                    self.transaction.lmd['ludwig_data']['ludwig_save_path'])
                train_stats = model.train(
                    data_df=training_dataframe,
                    model_name=self.transaction.lmd['name'],
                    skip_save_model=True)
                #,model_load_path=self.transaction.lmd['ludwig_data']['ludwig_save_path'])

            for k in train_stats['train']:
                if k not in self.transaction.lmd['model_accuracy']['train']:
                    self.transaction.lmd['model_accuracy']['train'][k] = []
                    self.transaction.lmd['model_accuracy']['test'][k] = []
                elif k is not 'combined':
                    # We should be adding the accuracy here but we only have it for combined, so, for now use that, will only affect multi-output scenarios anyway
                    pass
                else:
                    self.transaction.lmd['model_accuracy']['train'][k].extend(
                        train_stats['train'][k]['accuracy'])
                    self.transaction.lmd['model_accuracy']['test'][k].extend(
                        train_stats['test'][k]['accuracy'])
                '''
                @ TRAIN ONLINE BIT That's not working
                model = LudwigModel.load(self.transaction.lmd['ludwig_data']['ludwig_save_path'])
                for i in range(0,100):
                    train_stats = model.train_online(data_df=training_dataframe)
                    # The resulting train_stats are "None"... wonderful -_-
                '''

        ludwig_model_savepath = Config.LOCALSTORE_PATH.rstrip(
            'local_jsondb_store') + self.transaction.lmd['name']

        model.save(ludwig_model_savepath)
        model.close()

        self.transaction.lmd['ludwig_data'] = {
            'ludwig_save_path': ludwig_model_savepath
        }
        self.transaction.hmd['ludwig_data'] = {
            'model_definition': model_definition
        }
from ludwig import LudwigModel
import pandas as pd

df = pd.read_csv('Tweets.csv')
print(df.head())

model_definition = {
    'input_features':[
        {'name':'text', 'type':'text'},
    ],
    'output_features': [
        {'name': 'airline_sentiment', 'type': 'category'}
    ]
}

print('creating model')
model = LudwigModel(model_definition)
print('training model')
train_stats = model.train(data_df=df)
model.close()
Beispiel #10
0
from flask import Flask, request, jsonify
from ludwig import LudwigModel
import pandas as pd

app = Flask(__name__)

#load the model
model = LudwigModel.load('./model')


@app.rout('/predict', method=['POST'])
def predict():
    #get POST request data
    data = request.get_json()
    #Make prediction
    df = pd.DataFrame([str(data['text'])], columns=['content'])
    print(df.head())
    pred = model.predict(data_df=df)
    print(pred)
    return jsonify(pred['airline_sentiment_predictions'][0])


if __name__ == '__main__':
    app.run(port=4000, debug=True)
test_features = transform(test_df)

add_missing_dummy_columns(test_features, model_columns)
test_features = test_features[model_columns]

test_prediction = pd.DataFrame(model.predict(test_features),
                               columns=['prediction'])
submission = Ids.merge(test_prediction,
                       how='left',
                       left_index=True,
                       right_index=True)
submission.to_csv('submission.csv')

# Try with ludwig
# train a model
#load a model
model = LudwigModel.load(
    "/Users/geoffrey.kip/Projects/ludwig/ludwig_models/results/experiment_run_1/model"
)

# obtain predictions
ludwig_predictions = model.predict(test_df)

#evaluate predictions
preds = np.where(predictions_probs[:, 1] >= 0.5, 1, 0)
print(accuracy_score(Y_test, preds))
print(confusion_matrix(Y_test, preds))
print(classification_report(Y_test, preds))

model.close()
Beispiel #12
0
        'name': 'oldpeak',
        'type': 'numerical',
        'encoder': 'rnn'
    }, {
        'name': 'slope',
        'type': 'category',
        'encoder': 'rnn'
    }, {
        'name': 'ca',
        'type': 'category',
        'encoder': 'rnn'
    }, {
        'name': 'thal',
        'type': 'category',
        'encoder': 'rnn'
    }],
    'output_features': [{
        'name': 'target',
        'type': 'binary'
    }],
    'training': {
        'epochs': 10
    }
}
model = LudwigModel(model_definition)
train_stats = model.train(data)

# obtain predictions
predictions = model.predict(data)

model.close()
# import json
from ludwig import LudwigModel
import pandas as pd

# load the csv file
csv_file = pd.read_csv('out.csv')

model = LudwigModel.load(
    "/home/jesse/Documents/Machine Learning/Disease Prediction System/Hepatitis/results/_run_4/model"
)
predict = model.predict(csv_file)

# convert prediit to json file
predict.to_json(r'./prediit.json')
Beispiel #14
0
        },
        {
            'name': 'pe',
            'type': 'category'
        },
        {
            'name': 'ane',
            'type': 'category'
        },
    ],
    'output_features': [{
        'name': 'classification',
        'type': 'binary'
    }]
}

print('creating model')
model = LudwigModel(model_definition)
print('training model')
train_stats = model.train(data_df=train_df)

#Run predictions
predictions = model.predict(data_df=X_test)
predictions["classification_predictions"] = np.where(
    predictions.iloc[:, 0] == True, 1, 0)

print(accuracy_score(Y_test, predictions.iloc[:, 0]))
print(confusion_matrix(Y_test, predictions.iloc[:, 0]))
print(classification_report(Y_test, predictions.iloc[:, 0]))

model.close()