Пример #1
0
def route(db, json_data_s, operation, feature_type, algorithm=None):
    print("function: route")
    print("operation => " + operation)
    print(json_data_s)
    data = json.loads(json_data_s)
    
    init_data(data)
            
    # train, predict, testではalgorithmに対応するモジュールをimportする
    if operation in {'train','predict','test'}:
        mod = __import__(algorithm+'.classifier', fromlist=['.'])

# train
    if operation=='train':
        return mod.__dict__[operation](db,feature_type,data)        

# clear_classifier
    elif operation=='clear_classifier':
        return mongointerface.clear_classifier(db, feature_type, data, algorithm)

# clear_samples
    elif operation=='clear_samples':
        return mongointerface.clear_samples(db, feature_type, data)

# get samples / sample detail
    elif operation=='get_samples':
        return mongointerface.get_samples(db,feature_type,data)
    elif operation=='get_sample_detail':
        return mongointerface.get_sample_detail(db,feature_type,data)

# band
    elif operation=='band':
        return mongointerface.band(db, feature_type, data)

# disband
    elif operation=='disband':
        return mongointerface.disband(db, feature_type, data)

# evaluate
    elif operation=='evaluate':
        return mongointerface.evaluate(db, feature_type, data, algorithm)

    # operations using sample
    else:        
        if data.has_key('id'):
            if (operation != "add"):
                sample = db[feature_type].find_one({'_id':data['id']})
                if sample:
                    for k,v in sample.items():                    
                        data[k] = v
        # generate sample ID automatically (can be collapse if several samples add at once)
        else:            
            data['id'] = "sample_" +  "%012d" % db[feature_type].find().count()

        sample = Sample(data)
        if not sample.ft:
            return error_json("sample must contain 'feature'")                           

# add
        if operation == 'add':
            return mongointerface.add(db,feature_type,sample)

# predict
        elif operation == 'predict':
            return mod.__dict__[operation](db, feature_type, sample, data)
                        
# unknown operations (error)
    return error_json('Error: unknown operation %s.' % operation)
Пример #2
0
        def wrapper(db,feature_type, sample, data):
            # サンプルのグループを取ってくるだけ
            # Sample … (ft,_id,type,cls,group,likelihood,weight)
            #print "function: predict"
            
            clf_id = generate_clf_id(algorithm,feature_type,data) 

            # 予測部
            collection = db["classifiers"]            
            
            try:
                record = collection.find_one({'_id':clf_id})
                if record == None:
                    return error_json("No classifier was found.")


                print "load %s..."%record['clf']
                clf = load_model(record['clf'])
                print "done"
            except:
                print sys.exc_info()[0]
                return error_json(sys.exc_info()[1])
                

            if record.has_key('pca'):
                print "load %s..."%record['pca']
                pca = load_model(record['pca'])
                print "done"
                feature = sample.ft
                sample.ft = pca.transform(sample.ft)
                
            # sparseな学習をしても通常のベクトルを入力として良い
            # (逆にsparseなベクトルを入れると出力のlikelihoodもsparseになる)
            #if record.has_key('sparse'):
            #    sample.ft = lil_matrix(sample.ft).tocsr()

            # algorithmに応じた処理(func)を行う
            likelihood_list = func(clf,sample)
            

            if record.has_key('pca'):
                sample.ft = feature

            likelihood_dict = {}
            for i,l in enumerate(likelihood_list):
                if not record['class_id2name'].has_key(str(i)):
                    # 本来,ここは通らないはず.稀にclass_id2nameに登録されていないクラスID (カテゴリ数と一致を返す.scikit-learnの仕様??要調査
                    continue
                key = record['class_id2name'][str(i)]
                likelihood_dict[key] = l
                        
            # 予測結果をデータベースへ追加
            sample.likelihood[clf_id] = likelihood_dict

            collections = db[feature_type]
            if collections.find_one(sample._id):
                collections.update_one({"_id":sample._id},{"$set":{'likelihood':sample.likelihood}})
                sub_result = {'update::%s'%sample._id}
            else:
                sub_result = mongointerface.add(db,feature_type,sample)
            
            result = success_json()
            result['event'] = {'_id':"predict::"+clf_id+"::"+str(sample._id), 'sub_event':sub_result}
            result['result'] = {'id':sample._id, 'likelihood':likelihood_dict}            
            if sample.ground_truth:
                result['result']['ground_truth'] = sample.ground_truth
            
            return result