精华内容
下载资源
问答
  • whether...or的用法
    千次阅读
    2018-04-23 08:31:44

    whether意为“是否;不管……(还是……)”,表示迟疑或两个可能性之间的选择。whether有以下三种常见表达:

    (1) whether to do sth. 意为“是否……”

    (2) whether ... or ... 意为“是……,还是……呢?”

    (3) whether ... or not 意为“无论……,不管……”

    Example: There were times when I wondered whether or not we would get there.

    我曾几度怀疑我们能否到达那里。

    Example: I didn’t know whether to believe him or not.

    我不确定该不该相信他。

    Example: She was uncertain whether to stay or leave.

    她不确定自己是去是留。

    Example: Whether or not we’re successful, we can be sure that we did our best.

    不管能否成功,我们真的已经尽力了。

    转载于:https://blog.51cto.com/ciscoexpert/2106652

    更多相关内容
  • requests.request方法使用

    千次阅读 2018-09-21 19:18:35
    使用requests.request发送请求,实际是使用requests.sessions.Session().request(),自动管理cookie 以下是源码: def request(method, url, **kwargs): """Constructs and sends a :class:`Request &...

    使用requests.request发送请求,实际是使用requests.sessions.Session().request(),自动管理cookie
    以下是源码:

    def request(method, url, **kwargs):
        """Constructs and sends a :class:`Request <Request>`.
    
        :param method: method for the new :class:`Request` object.
        :param url: URL for the new :class:`Request` object.
        :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
        :param data: (optional) Dictionary or list of tuples ``[(key, value)]`` (will be form-encoded), bytes, or file-like object to send in the body of the :class:`Request`.
        :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
        :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
        :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
        :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
            ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
            or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
            defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
            to add for the file.
        :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
        :param timeout: (optional) How many seconds to wait for the server to send data
            before giving up, as a float, or a :ref:`(connect timeout, read
            timeout) <timeouts>` tuple.
        :type timeout: float or tuple
        :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
        :type allow_redirects: bool
        :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
        :param verify: (optional) Either a boolean, in which case it controls whether we verify
                the server's TLS certificate, or a string, in which case it must be a path
                to a CA bundle to use. Defaults to ``True``.
        :param stream: (optional) if ``False``, the response content will be immediately downloaded.
        :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
        :return: :class:`Response <Response>` object
        :rtype: requests.Response
    
        Usage::
    
          >>> import requests
          >>> req = requests.request('GET', 'http://httpbin.org/get')
          <Response [200]>
        """
    
        # By using the 'with' statement we are sure the session is closed, thus we
        # avoid leaving sockets open which can trigger a ResourceWarning in some
        # cases, and look like a memory leak in others.
        with sessions.Session() as session:
            return session.request(method=method, url=url, **kwargs)
    

    以下是例子:

    import requests
    
    url = "https://sec-m.ctrip.com/restapi/soa2/10131/json/ViewSpotSearchV1QOC"
    
    querystring = {"Host":"sec-m.ctrip.com","Connection":"keep-alive","Pragma":"no-cache","Cache-Control":"no-cache","Accept":"application/json","cookieOrigin":"https://m.ctrip.com","Origin":"https://m.ctrip.com","User-Agent":"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3557.0 Mobile Safari/537.36","Content-Type":"application/json","Referer":"https://m.ctrip.com/webapp/ticket/dest/dt-上海-2/s-tickets?urltype=dt&name=上海&pagefrom=newhp_ticketIndex&searchtype=city","Accept-Encoding":"gzip, deflate, br","Accept-Language":"zh-CN,zh;q=0.9","Cookie":"_abtest_userid=50c2487b-ccaf-433f-8d0d-d29890c461ee; _ga=GA1.2.709875315.1537525410; _gid=GA1.2.16745294.1537525410; _RF1=13.67.65.168; _RSG=mf4NcnN1i67HEWHPvSsT_8; _RDG=281eecb0a9929920551fa1397940a1cdb6; _RGUID=71ac057c-665c-4a70-8fdd-d9ce59460391; MKT_Pagesource=H5; Union=OUID=&AllianceID=4897&SID=353693&SourceID=55551825&AppID=&Expires=1538130214937; _fpacid=09031077211452549306; GUID=09031077211452549306; _gat=1; Mkt_UnionRecord=[{\"aid\":\"4897\",\"timestamp\":1537525492688}]; _jzqco=|||||1.451842881.1537525415149.1537525481240.1537525492725.1537525481240.1537525492725.0.0.0.3.3; _bfa=1.1537525408919.53y3e6.1.1537525408919.1537525500093.1.5.214424"}
    
    payload = "{\"head\":{\"cid\":\"09031077211452549306\",\"ctok\":\"\",\"cver\":\"1.0\",\"lang\":\"01\",\"sid\":\"55551825\",\"syscode\":\"09\",\"auth\":null,\"extension\":[{\"name\":\"networkstatus\",\"value\":\"None\"},{\"name\":\"protocal\",\"value\":\"https\"}]},\"ver\":\"7.13.3\",\"keyword\":\"\",\"radiusmax\":null,\"distids\":\"5_2\",\"smode\":6,\"sort\":1,\"pidx\":1,\"isintion\":true,\"psize\":20,\"isneedf\":true,\"reltype\":1,\"spara\":\"\",\"filters\":[],\"excepts\":[],\"contentType\":\"json\"}"
    headers = {
        'cache-control': "no-cache",
        'postman-token': "449b7d4e-7a9c-38c7-4d1a-a7f814c2dcbd"
        }
    
    response = requests.request("POST", url, data=payload, headers=headers, params=querystring)
    # response = requests.sessions.Session().request("POST", url, data=payload, headers=headers, params=querystring, verify=False)
    
    
    print(response.text)
    
    展开全文
  • or (key in rv.__dict__): raise KeyError( "Attempt to overwrite %r in LogRecord" % key) rv.__dict__[key] = extra[key] return rv 调用 _logRecordFactory 初始化一个日志记录实例, _...

    本次分析一下Logger.info的流程

    1. Logger.info源码:

        def info(self, msg, *args, **kwargs):
            """
            Log 'msg % args' with severity 'INFO'.
    
            To pass exception information, use the keyword argument exc_info with
            a true value, e.g.
    
            logger.info("Houston, we have a %s", "interesting problem", exc_info=1)
            """
            if self.isEnabledFor(INFO):
                self._log(INFO, msg, args, **kwargs)
    

    注释中反应了可以通过 msg和不定参数args来进行日志的格式化。
    真实的调用为:_log方法:

    2. Logger._log方法:

        def _log(self, level, msg, args, exc_info=None, extra=None, stack_info=False):
            """
            Low-level logging routine which creates a LogRecord and then calls
            all the handlers of this logger to handle the record.
            """
            sinfo = None
            if _srcfile:
                #IronPython doesn't track Python frames, so findCaller raises an
                #exception on some versions of IronPython. We trap it here so that
                #IronPython can use logging.
                try:
                    fn, lno, func, sinfo = self.findCaller(stack_info)
                except ValueError: # pragma: no cover
                    fn, lno, func = "(unknown file)", 0, "(unknown function)"
            else: # pragma: no cover
                fn, lno, func = "(unknown file)", 0, "(unknown function)"
            if exc_info:
                if isinstance(exc_info, BaseException):
                    exc_info = (type(exc_info), exc_info, exc_info.__traceback__)
                elif not isinstance(exc_info, tuple):
                    exc_info = sys.exc_info()
            record = self.makeRecord(self.name, level, fn, lno, msg, args,
                                     exc_info, func, extra, sinfo)
            self.handle(record)
    

    最后两行:
    1. 生成日志记录:
    record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra, sinfo)
    2. 处理日志记录
    self.handle(record)

    2 生成日志记录:

        def makeRecord(self, name, level, fn, lno, msg, args, exc_info,
                       func=None, extra=None, sinfo=None):
            """
            A factory method which can be overridden in subclasses to create
            specialized LogRecords.
            """
            rv = _logRecordFactory(name, level, fn, lno, msg, args, exc_info, func,
                                 sinfo)
            if extra is not None:
                for key in extra:
                    if (key in ["message", "asctime"]) or (key in rv.__dict__):
                        raise KeyError("Attempt to overwrite %r in LogRecord" % key)
                    rv.__dict__[key] = extra[key]
            return rv

    调用_logRecordFactory初始化一个日志记录实例,_logRecordFactory其实就是LogRecord类,初始化时,可能包含logger的name, level、调用的函数、行号、日志字符串、模板参数、堆栈信息等。
    再看extra信息,extra到底有何用?现在从代码中可以看到,只是更新到生成的日志记录实例的__dict__中去.猜测:肯定会在生成最终的日志字符串的时候会用到。继续往下看。

    3 处理日志记录self.handle(record):

    Logger继承自Filterer,

        def handle(self, record):
            """
            Call the handlers for the specified record.
    
            This method is used for unpickled records received from a socket, as
            well as those created locally. Logger-level filtering is applied.
            """
            if (not self.disabled) and self.filter(record):
                self.callHandlers(record)

    3.1 if语句中有一self.filter(record)的判断,看函数名,是来筛选是否要继续处理消息的,其核心源码如下:

        def filter(self, record):
            """
            Determine if a record is loggable by consulting all the filters.
    
            The default is to allow the record to be logged; any filter can veto
            this and the record is then dropped. Returns a zero value if a record
            is to be dropped, else non-zero.
    
            .. versionchanged:: 3.2
    
               Allow filters to be just callables.
            """
            rv = True
            for f in self.filters:
                if hasattr(f, 'filter'):
                    result = f.filter(record)
                else:
                    result = f(record) # assume callable - will raise if not
                if not result:
                    rv = False
                    break
            return rv
    

    可以看到, 如果在handler中的filter中如果有返回为False或空,则会屏蔽对应的record,返回True或部位空的值,则会将record放行。那么我们就可以自定义自己的filter。

    3.2 让Logger中所有的handles去处理record:

        def callHandlers(self, record):
            """
            Pass a record to all relevant handlers.
    
            Loop through all handlers for this logger and its parents in the
            logger hierarchy. If no handler was found, output a one-off error
            message to sys.stderr. Stop searching up the hierarchy whenever a
            logger with the "propagate" attribute set to zero is found - that
            will be the last logger whose handlers are called.
            """
            c = self
            found = 0
            while c:
                for hdlr in c.handlers:
                    found = found + 1
                    if record.levelno >= hdlr.level:
                        hdlr.handle(record)
                if not c.propagate:
                    c = None    #break out
                else:
                    c = c.parent
            if (found == 0):
                if lastResort:
                    if record.levelno >= lastResort.level:
                        lastResort.handle(record)
                elif raiseExceptions and not self.manager.emittedNoHandlerWarning:
                    sys.stderr.write("No handlers could be found for logger"
                                     " \"%s\"\n" % self.name)
                    self.manager.emittedNoHandlerWarning = True
    

    代码中会去循环调用当前logger的所有handlers去处理record,for循环部分,之后,如果当前的loggerpropagate的值为False或空,则不向logger父logger传递,即向上传递。

    4. Handler 中的 handler(record) 部分:

        def handle(self, record):
            """
            Conditionally emit the specified logging record.
    
            Emission depends on filters which may have been added to the handler.
            Wrap the actual emission of the record with acquisition/release of
            the I/O thread lock. Returns whether the filter passed the record for
            emission.
            """
            rv = self.filter(record)
            if rv:
                self.acquire()
                try:
                    self.emit(record)
                finally:
                    self.release()
            return rv

    可以看到, Handler在处理record时, 会去加锁,然后调用self.emit(record)方法去处理。

    4.1 emit(record)

        def emit(self, record):
            """
            Do whatever it takes to actually log the specified logging record.
    
            This version is intended to be implemented by subclasses and so
            raises a NotImplementedError.
            """
            raise NotImplementedError('emit must be implemented '
                                      'by Handler subclasses')

    看到需要由子类去实现,以StreamHandler为例子:

        def emit(self, record):
            """
            Emit a record.
    
            If a formatter is specified, it is used to format the record.
            The record is then written to the stream with a trailing newline.  If
            exception information is present, it is formatted using
            traceback.print_exception and appended to the stream.  If the stream
            has an 'encoding' attribute, it is used to determine how to do the
            output to the stream.
            """
            try:
                msg = self.format(record)
                stream = self.stream
                stream.write(msg)
                stream.write(self.terminator)
                self.flush()
            except Exception:
                self.handleError(record)
    

    4.2 Handler.format(record):

        def format(self, record):
            """
            Format the specified record.
    
            If a formatter is set, use it. Otherwise, use the default formatter
            for the module.
            """
            if self.formatter:
                fmt = self.formatter
            else:
                fmt = _defaultFormatter
            return fmt.format(record)
    

    如果handler有自定义的formatter就用自定义的,如果没有则用默认的Formatter的实例, 初始化元源码为:

        def __init__(self, fmt=None, datefmt=None, style='%'):
            """
            Initialize the formatter with specified format strings.
    
            Initialize the formatter either with the specified format string, or a
            default as described above. Allow for specialized date formatting with
            the optional datefmt argument (if omitted, you get the ISO8601 format).
    
            Use a style parameter of '%', '{' or '$' to specify that you want to
            use one of %-formatting, :meth:`str.format` (``{}``) formatting or
            :class:`string.Template` formatting in your format string.
    
            .. versionchanged:: 3.2
               Added the ``style`` parameter.
            """
            if style not in _STYLES:
                raise ValueError('Style must be one of: %s' % ','.join(
                                 _STYLES.keys()))
            self._style = _STYLES[style][0](fmt)
            self._fmt = self._style._fmt
            self.datefmt = datefmt
    
    

    有三个参数:
    * fmt: 格式化模板
    * datefmt: 时间格式化参数
    * style: 日志格式化的样式。

    style有三种:

    _STYLES = {
        '%': (PercentStyle, BASIC_FORMAT),
        '{': (StrFormatStyle, '{levelname}:{name}:{message}'),
        '$': (StringTemplateStyle, '${levelname}:${name}:${message}'),
    

    可以看出对应到:% 操作符的格式化, format方法的格式化以及Template的格式化。
    Formatter的format方法源码为:

        def format(self, record):
            """
            Format the specified record as text.
            The record's attribute dictionary is used as the operand to a
            string formatting operation which yields the returned string.
            Before formatting the dictionary, a couple of preparatory steps
            are carried out. The message attribute of the record is computed
            using LogRecord.getMessage(). If the formatting string uses the
            time (as determined by a call to usesTime(), formatTime() is
            called to format the event time. If there is exception information,
            it is formatted using formatException() and appended to the message.
            """
            record.message = record.getMessage()
            if self.usesTime():
                record.asctime = self.formatTime(record, self.datefmt)
            s = self.formatMessage(record)
            if record.exc_info:
                # Cache the traceback text to avoid converting it multiple times
                # (it's constant anyway)
                if not record.exc_text:
                    record.exc_text = self.formatException(record.exc_info)
            if record.exc_text:
                if s[-1:] != "\n":
                    s = s + "\n"
                s = s + record.exc_text
            if record.stack_info:
                if s[-1:] != "\n":
                    s = s + "\n"
                s = s + self.formatStack(record.stack_info)

    看到会调用record.getMessage(),这里仅仅是获取我们需要的日志信息。
    之后会调用s = self.formatMessage(record):

        def formatMessage(self, record):
            return self._style.format(record)

    其实是调用了当前style的format方法,以%这一类型为例PercentStyle

    class PercentStyle(object):
    
        default_format = '%(message)s'
        asctime_format = '%(asctime)s'
        asctime_search = '%(asctime)'
    
        def __init__(self, fmt):
            self._fmt = fmt or self.default_format
    
        def usesTime(self):
            return self._fmt.find(self.asctime_search) >= 0
    
        def format(self, record):
            return self._fmt % record.__dict__

    从其中的format方法可以看出,是针对record__dict__属性中的所有参数进行格式化,这下,就清楚了之前的extra参数是干嘛用的了:可以在formatter中加入自己自定义的一些参数,如固定的用户信息等等。

    之后,将最终的message flush到对应的Stream里面去就行了,就是整个流程:

    官方文档中的处理流程

    请大家多多指点。

    展开全文
  • 文件将点(纬度/经度)和海岸分辨率作为输入向量,并返回这些点是在陆地还是海洋上的指标向量。 如果需要,也可以制作地理图。 主要代码是在 2011 年 2 月 Brett Shoelson...这段代码还没有放入函数文件中供公众使用
  • YOLOX源码之demo.py逐行讲解

    千次阅读 2022-02-18 15:06:11
    首先进入make_parser()方法,就是熟悉一下参数解析器: def make_parser(): parser = argparse.ArgumentParser("YOLOX Demo!") """argparse.ArgumentParser是创建一个参数解析器,用以解析命令行的参数""" ""...

    demo.py就是一个推理的脚本,告诉我们如何从头到尾的推理图像。

    首先进入make_parser()方法,就是熟悉一下参数解析器:

    def make_parser():
        parser = argparse.ArgumentParser("YOLOX Demo!")  """argparse.ArgumentParser是创建一个参数解析器,用以解析命令行的参数"""
        """parser.add_argument是往参数解析器里添加参数。该方法第一个参数为需要添加参数的简称,后面也可以跟该参数的全名。default参数代表默认值,help参数代表提示,type参数代表该参数的类型"""
        parser.add_argument("demo", default="image", help="demo type, eg. image, video and webcam")"""试验的输入是图像、视频or摄像头"""
        parser.add_argument("-expn", "--experiment-name", type=str, default=None)"""试验名称"""
        parser.add_argument("-n", "--name", type=str, default=None, help="model name")"""模型的类型,有s、x、m、l等"""
        parser.add_argument("--path", default="./assets/dog.jpg", help="path to images or video")"""输入验证或者测试图片及视频的路径"""
        parser.add_argument("--camid", type=int, default=0, help="webcam demo camera id")"""调用摄像头的索引"""
        parser.add_argument("--save_result", action="store_true", help="whether to save the inference result of image/video",)"""需不需要保存结果"""
    
        # exp file
        parser.add_argument("-f", "--exp_file", default=None, type=str, help="pls input your expriment description file",)"""试验描述文件"""
        parser.add_argument("-c", "--ckpt", default=None, type=str, help="ckpt for eval")"""权重的地址"""
        parser.add_argument("--device", default="cpu", type=str, help="device to run our model, can either be cpu or gpu",)"""实验设备"""
        parser.add_argument("--conf", default=None, type=float, help="test conf")"""置信度阈值"""
        parser.add_argument("--nms", default=None, type=float, help="test nms threshold")"""非极大值抑制阈值"""
        parser.add_argument("--tsize", default=None, type=int, help="test img size")"""输入网络图片尺寸的大小,默认为32的倍数。因为YOLOX最大要进行32倍的降采样"""
        parser.add_argument("--fp16", dest="fp16", default=False, action="store_true", help="Adopting mix precision evaluating.",)"""混合精度训练。一般情况都用fp32。fp16会减小计算量,加快训练速度"""
        parser.add_argument("--fuse", dest="fuse", default=False, action="store_true", help="Fuse conv and bn for testing.",)"""模型融合高级技巧。将conv层和bn层融合,加快推理速度"""
        parser.add_argument("--trt", dest="trt", default=False, action="store_true", help="Using TensorRT model for testing.",)"""用trt模式加快推理速度"""
        return parser

    当我们运行demo.py的时候,首先会执行下面两行代码:

        args = make_parser().parse_args()"""解析参数"""
        exp = get_exp(args.exp_file, args.name)

    我们定位get_exp()方法,在yolox\exp\build.py文件夹中,首先会执行断言函数,如果两个参数都为空,则会报错,计算机不知道我们要干什么。接着执行get_exp_by_name()方法:

    def get_exp(exp_file, exp_name):
        """
        get Exp object by file or name. If exp_file and exp_name
        are both provided, get Exp by exp_file.
    
        Args:
            exp_file (str): file path of experiment.
            exp_name (str): name of experiment. "yolo-s",
        """
        assert (exp_file is not None or exp_name is not None), "plz provide exp file or exp name."
        if exp_file is not None:
            return get_exp_by_file(exp_file)
        else:
            return get_exp_by_name(exp_name)

    我们进入当前脚本的get_exp_by_name()方法:

    def get_exp_by_name(exp_name):
        import yolox
    
        yolox_path = os.path.dirname(os.path.dirname(yolox.__file__))"""获取yolox的根目录"""
        filedict = {
            "yolox-s": "yolox_s.py",
            "yolox-m": "yolox_m.py",
            "yolox-l": "yolox_l.py",
            "yolox-x": "yolox_x.py",
            "yolox-tiny": "yolox_tiny.py",
            "yolox-nano": "nano.py",
            "yolov3": "yolov3.py",}
        filename = filedict[exp_name]"""模型名称对应的py文件"""
        exp_path = os.path.join(yolox_path, "exps", "default", filename)"""路径拼接:根目录/exps/default/"""
        return get_exp_by_file(exp_path)

    然后执行get_exp_by_file()方法:

    def get_exp_by_file(exp_file):
        try:"""异常处理函数"""
            """下面全部代码的作用就是import的一个yolox-m.py"""
            sys.path.append(os.path.dirname(exp_file))"""sys.path保存路径"""
            current_exp = importlib.import_module(os.path.basename(exp_file).split(".")[0])
            exp = current_exp.Exp()"""yolox-m.py中的Exp类"""
        except Exception:
            raise ImportError("{} doesn't contains class named 'Exp'".format(exp_file))
        return exp

    我们进入到exps\default\yolox_s.py,其父类的位置为yolox\exp\yolox_base.py。

    import os
    from yolox.exp import Exp as MyExp
    
    class Exp(MyExp):
        def __init__(self):
            super(Exp, self).__init__()
            self.depth = 0.33"""调整模型深度"""
            self.width = 0.50"""调整模型宽度"""
            """os.path.realpath(__file__)代表获取当前模块的绝对路径"""
            self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(".")[0]"""值为yolox_s"""

    接着我们执行demo.py的main()方法,整个过程十分简单,最重要的就是其内部的Predictor()方法:

    def main(exp, args):
        if not args.experiment_name:"""如果没有输入试验名称,就以exp.exp_name(如:yolox_s)为实验名称"""
            args.experiment_name = exp.exp_name
    
        file_name = os.path.join(exp.output_dir, args.experiment_name)"""保存训练结果的位置"""
        os.makedirs(file_name, exist_ok=True)"""路径不存在,则创建路径"""
    
        if args.save_result:
            vis_folder = os.path.join(file_name, "vis_res")"""再来一个子路径,也是用来保存结果信息"""
            os.makedirs(vis_folder, exist_ok=True)
    
        if args.trt:"""不管"""
            args.device = "gpu"
    
        logger.info("Args: {}".format(args))"""记载日志"""
    
        if args.conf is not None:
            exp.test_conf = args.conf
        if args.nms is not None:
            exp.nmsthre = args.nms
        if args.tsize is not None:
            exp.test_size = (args.tsize, args.tsize)
    
        model = exp.get_model()"""模型加载"""
        logger.info("Model Summary: {}".format(get_model_info(model, exp.test_size)))
    
        if args.device == "gpu":"""如果设备是gpu,还得cuda一下"""
            model.cuda()
        model.eval()"""测试模式,不会改变任何权重"""
    
        if not args.trt:
            if args.ckpt is None:
                ckpt_file = os.path.join(file_name, "best_ckpt.pth")
            else:
                ckpt_file = args.ckpt
            logger.info("loading checkpoint")
            ckpt = torch.load(ckpt_file, map_location="cpu")"""将权重文件加载进内存"""
            # load the model state dict
            model.load_state_dict(ckpt["model"])"""将内存的权重值加载到模型"""
            logger.info("loaded checkpoint done.")
    
        if args.fuse:
            logger.info("\tFusing model...")
            model = fuse_model(model)
    
        if args.trt:
            assert not args.fuse, "TensorRT model is not support model fusing!"
            trt_file = os.path.join(file_name, "model_trt.pth")
            assert os.path.exists(
                trt_file
            ), "TensorRT model is not found!\n Run python3 tools/trt.py first!"
            model.head.decode_in_inference = False
            decoder = model.head.decode_outputs
            logger.info("Using TensorRT to inference")
        else:
            trt_file = None
            decoder = None
    
        predictor = Predictor(model, exp, COCO_CLASSES, trt_file, decoder, args.device)

    我们进入Predictor()方法,只需好好理解以下两个方法,其他的不用管:

        def inference(self, img):"""推理出检测框"""
            img_info = {"id": 0}
            if isinstance(img, str):"""判断输入是否为字符串"""
                img_info["file_name"] = os.path.basename(img)
                img = cv2.imread(img)"""读图"""
            else:
                img_info["file_name"] = None
    
            height, width = img.shape[:2]"""得到图像的宽高"""
            img_info["height"] = height
            img_info["width"] = width
            img_info["raw_img"] = img
    
            img, ratio = preproc(img, self.test_size, self.rgb_means, self.std)"""改变图像的shape为我们规定的shape(等比例缩放、其他地方填充灰色、并将图像归一化)"""
            img_info["ratio"] = ratio"""获取图像的缩放比例"""
            img = torch.from_numpy(img).unsqueeze(0)"""将数组形式转为张量,并扩充维度以满足运算要求"""
            if self.device == "gpu":
                img = img.cuda()"""如果为gpu,还得将图像cuda一下"""
    
            with torch.no_grad():"""测试过程不进行梯度运算"""
                t0 = time.time()
                outputs = self.model(img)"""output一般在几千个左右"""
                if self.decoder is not None:"""高级训练技巧,不用管"""
                    outputs = self.decoder(outputs, dtype=outputs.type())
                outputs = postprocess(outputs, self.num_classes, self.confthre, self.nmsthre)"""非极大值抑制"""
                logger.info("Infer time: {:.4f}s".format(time.time() - t0))
            return outputs, img_info
    
        def visual(self, output, img_info, cls_conf=0.35):"""可视化检测框"""
            ratio = img_info["ratio"]"""获取缩放比例"""
            img = img_info["raw_img"]"""获取原始图像"""
            if output is None:"""如果没有检测框"""
                return img
            output = output.cpu()"""cpu一下"""
    
            bboxes = output[:, 0:4]"""获取预测框"""
    
            # preprocessing: resize
            bboxes /= ratio"""将预测框还原成原图大小"""
    
            cls = output[:, 6]"""获取类别"""
            scores = output[:, 4] * output[:, 5]"""获取置信度"""
    
            vis_res = vis(img, bboxes, scores, cls, cls_conf, self.cls_names)"""画图,率除掉置信度阈值以下的框"""
            return vis_res
    展开全文
  • 文章目录写在前面os.path常用方法使用说明参考文档 写在前面 在python代码中,处理文件经常涉及到路径的操作,os.path提供了路径操作的多种方法。 通过 pydoc os.path 可以查看到 Help on module posixpath in os:...
  • Whether it’s a game view, UI, a camera, or a texture— NatCorder can record it. - Custom Resolutions. Record with resolutions as high as Full HD (1920x1080) and even higher on devices that support ...
  • spring.messages.use-code-as-default-message=false # Whether to use the message code as the default message instead of throwing a "NoSuchMessageException". Recommended during development only. # ...
  • java8List.sort()排序常用方法

    千次阅读 2022-03-29 18:24:14
    对于一些普通的数据类型(比如 String, Integer…),它们默认实现了Comparable 接口,实现了 compareTo 方法,我们可以直接使用。 而对于一些自定义类,它们可能在不同情况下需要实现不同的比较策略,我们可以使用...
  • python中numpy.zeros(np.zeros)的使用方法

    千次阅读 2020-12-23 07:06:16
    python 的 python中numpy.zeros(np.zeros)的使用方法翻译:用法:zeros(shape, dtype=float, order='C')返回:返回来一个给定形状和类型的用0填充的数组;参数:shape:形状dtype:数据类型,可选参数,默认numpy....
  • Prometheus+Springboot2.x实用实战——Timer(一)之@Timed初探@TimedTimer主要参数value()extraTags()description()@Timed的用法Timed的引用TimeAspectWebMvcMetricsFilter @Timed 在io.micrometer.core....
  • 方法hybr使用MINPACK中实现的Powell混合方法的修改[1] Method *lm* solves the system of nonlinear equations in a least squares sense using a modification of the Levenberg-Marquardt algorithm as ...
  • sklearn:sklearn.feature_selection的SelectFromModel函数的简介、使用方法之详细攻略 目录 SelectFromModel函数的简介 1、使用SelectFromModel和LassoCV进行特征选择 2、L1-based feature selection 3、...
  • WKWebView 如何支持window.open方法

    千次阅读 2018-12-01 19:09:49
    window.open是js新开页面的一种方法,本质上是为了在PC上支持直接新开页面,但是移动端也是支持该方法的,只是处理方式上并没有让它扮演新开窗口的任务,通过window.open打开的页面链接其实还是在当前webview内进行...
  • rayaun.github.io:笨拙的

    2021-05-15 10:57:11
    笨拙的 用法使用开曼主题,请执行以下操作: 将以下内容添加到您网站的_config.yml : theme : jekyll-theme-cayman ... show_downloads : ["true" or "false" to indicate whether to provide a do
  • inflater.inflate()参数详解

    万次阅读 多人点赞 2019-08-04 11:49:59
    前言 今天在lint项目的代码时,又遇到了inflate方法的参数问题,之前看过相关的文章,又没有记录下来,导致时间长了就忘记了。...用法 LayoutInflater.from(RecylerActivity.this).inflate(R.layout.my_text_view...
  • 关于python:什么是__main__.py?

    千次阅读 2021-03-17 16:21:33
    A module can discover whether or not it is running in the main scope by checking its own __name__, which allows a common idiom for conditionally executing code in a module when it is run as a script ...
  • 关于os.popen你可能不知道的

    千次阅读 2021-03-17 01:59:43
    一前言使用python执行一些命令或调用第三方工具是比较常见的情况,实现的方法也很多,如os.system(),os.popen()、subprocess模块中的函数等等,本文主要介绍一下os.popen(),可能隐藏着你不曾接触的知识点,同时会...
  • ML之sklearn:sklearn.linear_mode中的LogisticRegression函数的简介、使用方法之详细攻略 目录 sklearn.linear_mode中的LogisticRegression函数的简介、使用方法 sklearn.linear_mode中的...
  • 【Request类型】 Request类型定义在request.go文件中,用于设置一个http请求来发送给服务端。 // A Request represents an ...// or to be sent by a client. // // The field semantics differ slightly betw...
  • # 而且,使用realpath()可以保证Sphinx将使用与matplotlib所看到的相同的路径 # (在奇怪的符号链接的情况下)。 _basedir = os . path . realpath ( _basedir ) _fullpath = os . path . join ( _...
  • Axios.delete 两种传参方式

    千次阅读 2020-07-23 11:31:15
    Axios中文网特意说明data 传参方式只适用于POST,PATCH 和PUT,所以一直以为delete 传参也只能用params 传参,结果github API ...其实delete 两种传参方式都支持, 用法如下: // url 参数传递, DELETE /api/book/12?ke.
  • python 中使用matplotlib.pyplot 绘图,其中plt.axes()的用法 import matplotlib.pyplot as plt 后,可以在命令行中输入指令: plt.axes? 来查看axes函数的用法,其返回的文档如下: Signature: plt.axes(arg=None, ...
  • 概要 Fake account creator with options of exporting to DB or ... it is not intended to be a substitute for professional advice, whether medical, legal, or otherwise 动机 - 安装 - API参考 jsoup-1.7.3.
  • tf定义了tf.app.flags,用于支持接受命令行传递参数,相当于接受argv。 import tensorflow as tf #第一个是参数名称,第二个参数是默认值,第三个是参数描述 tf.app.flags.DEFINE_string('str_name', 'def_v_1',...
  • 处理数据时经常需要从数组中随机抽取元素,这...因此经过反复实验,我较为详细的总结出了他的用法,并给出了较为详细的使用代码例子。 官方解释:https://docs.scipy.org/doc/numpy/reference/generated/numpy.ra...
  • 变量被使用前,需要通过会话(session)运行其初始化方法完成初始化赋值 sess.run(tf.global_variables_initializer) . #!/usr/bin/python2.7 # -*- coding:utf-8 -*- """ @author: tz_zs """ import tensorflow ...
  • R语言|函数|t.test()

    万次阅读 2018-11-03 17:03:55
    the specified hypothesized value of the mean or mean difference depending on whether it was a one-sample test or a two-sample test. alternative a character string ...
  • 在vue项目中 使用driver.js来进行页面分步引导

    万次阅读 热门讨论 2019-05-22 20:11:08
    高度可定制 – 可在任何地方使用,可覆盖 界面友好 – 可通过按键控制 轻量级 – gzip 压缩后只有约4kb 在所有主流浏览器中保持一致的行为 免费用于个人和商业用途 安装 无论你喜欢哪种方式,你都可以使用 ...
  • Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 84,103
精华内容 33,641
热门标签
关键字:

whether...or的用法