当前位置: 首页>>代码示例>>Python>>正文


Python Preprocessor.set_tfidf方法代码示例

本文整理汇总了Python中preprocessor.Preprocessor.set_tfidf方法的典型用法代码示例。如果您正苦于以下问题:Python Preprocessor.set_tfidf方法的具体用法?Python Preprocessor.set_tfidf怎么用?Python Preprocessor.set_tfidf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在preprocessor.Preprocessor的用法示例。


在下文中一共展示了Preprocessor.set_tfidf方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ImageServer

# 需要导入模块: from preprocessor import Preprocessor [as 别名]
# 或者: from preprocessor.Preprocessor import set_tfidf [as 别名]
class ImageServer(SocketServer.ThreadingTCPServer):
    """ Generates a caption from an image """

    def __init__(self, server_address, RequestHandlerClass, args):
        SocketServer.ThreadingTCPServer.__init__(self, server_address, RequestHandlerClass)

        self.options_file = args.options
        self.model = args.model

        print "Loading options..."
        self._load_options()
        self._load_capgen()
        if "tex_dim" in self.options:
            print "Loading text preprocessor..."
            self._load_text_preprocessor(args)

        print "Loading dictionary..."
        self._load_worddict()
        print "Loading CNN..."
        self._load_cnn()
        print "Loading model..."
        self._build()

        self._update_status(5)
        print "All done!"

    def _update_status(self, status):
        """
        0: not running
        1: loading_options
        2: loading dictionary
        3: loading CNN
        4: building model
        5: done
        6: loading preprocessor
        """
        json.dump({"status": status}, open("{}_runningstatus.json".format(self.model), "w"))

    def _load_options(self):
        """ Loads the options of the caption model """
        self._update_status(1)

        with open(self.options_file, "rb") as f:
            self.options = pkl.load(f)

    def _load_capgen(self):
        """ Loads the caption generator version dependent on whether to use textual context or not """
        if "tex_dim" in self.options:
            import capgen_text as capgen

            self.capgen = capgen
        else:
            import capgen

            self.capgen = capgen

    def _load_worddict(self):
        """ Load the worddict """
        self._update_status(2)

        self.dictionary = dict()
        for kk, vv in self.options["dictionary"].iteritems():
            self.dictionary[vv] = kk
        self.dictionary[0] = "<eos>"
        self.dictionary[1] = "UNK"

    def _load_text_preprocessor(self, args):
        """ Load the preprocessor for the context """
        self._update_status(6)
        self.text_preprocessor = Preprocessor()

        # Load preprocessors based on type of model
        ## TF-IDF:
        if "tfidf" in self.options["preproc_type"]:
            print "Loading TF-IDF model..."
            with open(args.tfidfmodel, "rb") as f_tfidf, open(args.svdmodel, "rb") as f_svd:
                tfidf_model = pkl.load(f_tfidf)
                svd_model = pkl.load(f_svd) if "with_svd" in self.options["preproc_params"] else None
                self.text_preprocessor.set_tfidf(tfidf_model, svd_model)
        ## Word2Vec:
        if "w2v" in self.options["preproc_type"]:
            print "Loading Word2Vec model..."
            w2v_model = Word2Vec.load_word2vec_format(args.w2vmodel, binary=True)
            self.text_preprocessor.set_w2v(w2v_model)
        ## Raw:
        if "raw" in self.options["preproc_type"]:
            print "Loading counter model..."
            with open(args.rawmodel, "rb") as f_raw:
                raw_model = pkl.load(args.rawmodel)
                self.text_preprocessor.set_raw(raw_model)

    def resize_image(self, image, resize=256, crop=224):
        """ Resizes and crops the  image """
        # From author Kelvin Xu:
        # https://github.com/kelvinxu/arctic-captions/blob/master/alpha_visualization.ipynb
        width, height = image.size

        if width > height:
            width = (width * resize) / height
            height = resize
#.........这里部分代码省略.........
开发者ID:flipvrijn,项目名称:Dedicon-Thesis,代码行数:103,代码来源:model_server.py


注:本文中的preprocessor.Preprocessor.set_tfidf方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。