取消Flask内部对象的属性错误

时间:2017-07-18 12:45:05

标签: python flask pickle attributeerror

我正在使用脚本train_et.py创建一个pickle对象然后我尝试在我的烧瓶服务器的函数中加载对象但是我在创建pickle对象时使用了自定义类的问题" ItemSelector&# 34;和" TextStats"。我试图根据这个http://stefaanlippens.net/python-pickling-and-dealing-with-attributeerror-module-object-has-no-attribute-thing.html和其他帖子解决它,但我无法弄明白。如果我使用不使用自定义类的不同pickle对象,它可以工作。任何想法如何解决这一问题?代码和错误如下:

这是产生pickle对象的脚本:

train_et.py

        import pandas as pd
        import numpy as np
        from sklearn.feature_extraction.text import TfidfVectorizer
        from sklearn.pipeline import Pipeline
        import pickle
        from sklearn.externals import joblib
        from sklearn.pipeline import FeatureUnion
        from sklearn.feature_extraction import DictVectorizer
        from sklearn.ensemble import ExtraTreesClassifier

        from sklearn.base import BaseEstimator, TransformerMixin

    class ItemSelector(BaseEstimator, TransformerMixin):

        def __init__(self, column):
            self.column = column

        def fit(self, X, y=None, **fit_params):
            return self

        def transform(self, X):
            return (X[self.column])



    class TextStats(BaseEstimator, TransformerMixin):
        """Extract features from each document for DictVectorizer"""
        def fit(self, x, y=None):
            return self

        def transform(self, posts):
            return [{'REPORT_M': text} for text in posts]



     def train():
            data = joblib.load('data_df.pkl')

            # train and predict
            classifier = Pipeline([
                        ('union', FeatureUnion([

                                ('text', Pipeline([
                                    ('selector', ItemSelector(column='TEXT')),
                                    ('tfidf_vec', TfidfVectorizer(max_df=0.8
                                ])),

                                ('category', Pipeline([
                                    ('selector', ItemSelector(column='CATEGORY')),
                                    ('stats', TextStats()),
                                    ('vect', DictVectorizer())
                                ]))

                        ])),
                        ('clf', ExtraTreesClassifier(n_estimators=30, max_depth=300, min_samples_split=6, class_weight='balanced'))])

            classifier.fit(data, data.y)
            joblib.dump(classifier, 'et20000.pkl')

      if __name__ == '__main__':
          train()

然后在我的典型烧瓶项目结构中,我执行run.py

server
├── run.py
├── flask
├── app
│   ├── load.py
│   ├── __init.py__
│   ├── train_et.py
│   ├── views.py
│   ├── pipeline_classes.py
│   ├── ml
│   │   ├── et20000.pkl

__ init.py __

from flask import Flask

app = Flask(__name__)
app.config.from_object('config')

from app import views

run.py

from app import app
if __name__ == '__main__':
   app.run(debug=True)

views.py

from app import app
from flask import render_template
from .load import load

@app.before_first_request
def load_classifier():
    print("data loading")
    global loaded
    loaded = load()
    print("data loaded")

load.py

import pickle
import pandas as pd
from app import train_et
from app.train_et import ItemSelector, TextStats

def load():
    clf_ = pd.read_pickle('app/ml/et20000.pkl')
return(clf)

我收到以下错误:

builtins.AttributeError
AttributeError: module '__main__' has no attribute 'ItemSelector'
使用Traceback

func()
File "/home/q423446/server/app/views.py", line 19, in load_classifier
loaded = load()
File "/home/q423446/server/app/load.py", line 10, in load
clf_ = pd.read_pickle('app/ml/et20000.pkl')
File "/usr/local/lib/python3.5/dist-packages/pandas/io/pickle.py", line 68, in read_pickle
return try_read(path, encoding='latin1')
File "/usr/local/lib/python3.5/dist-packages/pandas/io/pickle.py", line 62, in try_read
return pc.load(fh, encoding=encoding, compat=True)
File "/usr/local/lib/python3.5/dist-packages/pandas/compat/pickle_compat.py", line 117, in load
return up.load()
File "/usr/lib/python3.5/pickle.py", line 1039, in load
dispatch[key[0]](self)
File "/usr/lib/python3.5/pickle.py", line 1334, in load_global
klass = self.find_class(module, name)
File "/usr/lib/python3.5/pickle.py", line 1388, in find_class
return getattr(sys.modules[module], name)

1 个答案:

答案 0 :(得分:1)

问题是我用joblib.dump创建了pickle对象,但用pd.read_pickle加载它。