예제 #1
0
def classify(data):
    if not os.path.isfile('model.joblib'):
        model.trainModel()

    clf = load('model.joblib')
    # stores output for file
    output = []
    ## goes through and gets the entities and size
    ## needed for the prediction
    for i in range(len(data)):
        entities = []
        total_length = []
        predict_array = []
        ## creates array of predictions for each sentence
        ## in a topic in each paragraph
        for j in range(len(data[i])):
            doc = nlp(data[i][j])
            entities.append((len(doc.ents)))
            total_length.append((len(data[i][j])))
            predictions = list(zip(total_length, entities))
            predict_array = clf.predict(predictions)

        output.append("-" + data[i][0])
        output.append("\n")
        ## goes through and indents how the sentences are
        ## based on the calculated prediction
        for y in range(1, len(predict_array)):
            if (predict_array[y] == 1 and y != 0):
                output.append("\t" + "\t" + "-" + data[i][y])
                output.append("\n")
            elif (predict_array[y] == 0 and y != 1):
                output.append("\t" + "-" + data[i][y])
                output.append("\n")
    output.append("\n")

    # returns output for file
    return output
예제 #2
0
파일: eval.py 프로젝트: jingsliu/NLP_HW
                            map_location=lambda storage, loc: storage)

    model = getattr(m, args.modelName)(model_paras, emb)
    model.load_state_dict(model0.state_dict())

    print(model)

    opt = optim.Adam(model.params, lr=args.lr)

    train_paras = {
        'n_iter':
        args.n_iter,
        'log_interval': [args.logInterval, 1000],
        'flg_cuda':
        args.flg_cuda,
        'lr_decay': [
            args.lr, args.lr_decay_rate, args.lr_decay3, 1e-5,
            args.lr_decay_type
        ],
        'flgSave':
        args.flgSave,
        'savePath':
        args.savePath,
        'n_batch':
        args.n_batch
    }

    m = m.trainModel(train_paras, None, test_loader, model, opt)
    m._test(0)
    m._savePrediction()
예제 #3
0
    print("Loading model")
    if flg_cuda:
        model0 = torch.load(args.modelPath + '_model.pt')
    else:
        model0 = torch.load(args.modelPath + '_model.pt',
                            map_location=lambda storage, loc: storage)

    if args.modelName == 'AttRNNseq2seq':
        model.load_state_dict(model0)
    else:
        model.load_state_dict(model0.state_dict())

    print(model)
    if flg_cuda:
        model = model.cuda()

    opt = optim.Adam(model.params, lr=args.lr)

    train_paras = {
        'n_iter': args.n_iter,
        'log_interval': [args.logInterval, 1000],
        'lr_decay': [args.lr, args.lr_decay_rate, args.lr_decay3, 1e-5],
        'n_batch': args.n_batch,
        'randSeed': args.randSeed
    }

    m = m.trainModel(train_paras, None, test_loader, model, opt,
                     dict_target['id2token'])
    m._test(0)
예제 #4
0
from sklearn import preprocessing
import matplotlib.pyplot as plt
import matplotlib.image as matimage
import matplotlib.cm as cm
import cv2
from string import ascii_lowercase, ascii_uppercase
import pprint
import heapq
import json
from flask import send_file
import time

app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
model = trainModel()


@app.route("/")
@cross_origin()
def helloWorld():
    return "Works!!"


@app.route("/predict", methods=['POST'])
@cross_origin()
def predict():
    image, filename = createBinaryMatrix(request.json['points'],
                                         request.json['width'],
                                         request.json['height'])
    #image = normalize(image)
from config import img_height, img_width

from audioToSpecgram import createSpecgramFromAudio, createSlicesFromSpecgram
from util import createDatasetFromSlices

from model import trainModel

parser = argparse.ArgumentParser()
parser.add_argument(
    "mode",
    help="Create Dataset - create, Train CNN - train, Test CNN - test",
    nargs="+",
    choices=["create", "train", "test"])

args = parser.parse_args()

print("CNN Config")
print("Validation Ratio : {0}".format(validation_ratio))
print("Test Ratio : {0}".format(test_ratio))
print("Specgram Time : {0}".format(spec_time))
print("Slices Per Genre : {0}".format(per_genre))

if "create" in args.mode:
    createDatasetFromSlices()

if "train" in args.mode:
    trainModel()

if "test" in args.mode:
    trainModel()
예제 #6
0
# If user wants to train the model
if (trainingBool == 1):
    print("Training model on train MFCCs...")
    nEpochs = 10

    # load mfcc correctly
    model = generateModel()

    tf.keras.utils.plot_model(model, to_file='model.png', show_shapes=True)

    model.compile(loss='binary_crossentropy',
                  optimizer=keras.optimizers.Adam(lr=0.0001),
                  metrics=['accuracy'])

    trainModel(nEpochs, gameNames, model)

if (trainingBool == 0):
    print("Loading pre-existing model...")
    model = generateModel()
    model.load_weights('model_weights.h5')

if (SHOW_ARCH == 1):
    keras2ascii(model)

# PREDICTION
#
if (audioTestFile):
    X_test = generatePrediction(audioTestFile)
    print('retrieved %s test MFCCs' % (X_test.shape[0]))
    # Prediction using trained model