Conda、pip、Python、Latex、Linux零碎技巧

Conda、pip

  • conda create -n B --clone A 克隆A环境为B
  • opencv安装,请参考pypi官网说明
  • mmcv-full使用pip安装时编译出错,需安装当前虚拟环境pytorch对应的cuda的NVIDIA官网版本,并确认系统环境变量CUDA_PATH指向该版本(安装程序会自动设置,一般不需要手动修改)
  • conda命令行换盘符d:,在任意路径下输入目标盘符加冒号,即可更换到目标盘的根目录下

Python

  • with关键字:代替try-finally,用于处理文件,释放资源

  • for line in f: print line :通过迭代器访问文件每一行。

  • shape

    • (3,1)二维数组,三行一列
    • (3,)一维数组,长度位3
  • pandas.DataFrame.values不如使用pandas.DataFrame.to_numpy,DataFrame转numpy

  • Python 类内方法

    class Test():
        a = 20
        _b = 21
        # 类外无法访问,类内可以通过类名或对象(self等)访问
        __c = 22
    
        def __init__(self):
            # 类内类外都无法通过类名访问,类内可通过对象(self等)访问
            self.aa = 10
            self._bb = 11
            # 类外无法访问
            self.__cc = 12
    
        @classmethod
        def cls_method(cls):
            print("class")
    
        # 参数不需要加 self
        @staticmethod
        def sta_method():
            print("static")
    
        # 类调用需要显式传对象参数
        def obj_method(self):
            print("object")
    
        # 类调用需要显式传对象参数
        def _pri_method(self):
            print("protect")
    
        # 类外无法访问,类内可以用类名或对象(self等)访问
        def __pri_method(self):
            print("private2")
    
        # 通过类名或对象(self等)调用所有函数
        def in_method(self):
            print("in==>")
            test_in = Test()
    
            Test.cls_method()
            Test.sta_method()
            Test.obj_method(test_in)
            Test._pri_method(test_in)
            Test.__pri_method(test_in)
            print(Test.a)
            print(Test._b)
            print(Test.__c)
            # print(Test.aa)  # 错误
            # print(Test._bb)  # 错误
            # print(Test.__cc)  # 错误
    
            test_in.cls_method()
            test_in.sta_method()
            test_in.obj_method()
            test_in._pri_method()
            test_in.__pri_method()
            print(test_in.a)
            print(test_in._b)
            print(test_in.__c)
            print(test_in.aa)
            print(test_in._bb)
            print(test_in.__cc)
    
            self.cls_method()
            self.sta_method()
            self.obj_method()
            self._pri_method()
            self.__pri_method()
            print(self.a)
            print(self._b)
            print(self.__c)
            print(self.aa)
            print(self._bb)
            print(self.__cc)
            print("<==in")
    
    
    test = Test()
    
    Test.cls_method()
    Test.sta_method()
    Test.obj_method(test)
    Test._pri_method(test)  # 警告
    # Test.__pri_method()  # 错误
    print(Test.a)
    print(Test._b)
    # print(Test.__c)  # 错误
    # print(Test.aa)  # 错误
    # print(Test._bb)  # 错误
    # print(Test.__cc)  # 错误
    
    Test.in_method(test)
    
    test.cls_method()
    test.sta_method()
    test.obj_method()
    test._pri_method()  # 警告
    # test.__pri_method()  # 错误
    print(test.a)
    print(test._b)
    # print(test.__c) # 错误
    print(test.aa)
    print(test._bb)
    # print(test.__cc)# 错误
    
    test.in_method()
    
  • 按最后一列groupby

    # 先排序
    train_draw = train_draw[train_draw[:, -1].argsort()]
    # 后分割
    train_draw = np.array(np.split(train_draw, np.unique(train_draw[:, -1], return_index=True)[1][1:]))
    
  • 函数静态类型定义,:后表示形参类型,->后表示返回类型

    def fun(a: int) -> int:
        return a + 1
    
  • linux切割路径字符串os.path.dirname(path).split('/')[-1]/the/input/path得到path

  • windows切割路径字符串os.path.dirname(path).split('\\')[-1]\the\input\path得到path

  • 预训练模型下载缓慢可以将以下代码

    model = torch.hub.load('facebookresearch/detr', 'detr_resnet50', pretrained=True)
    model.eval()
    

    替换为如下形式,二者等价,需要手动git克隆仓库并参照hubconf.py文件中的函数,下载并加载权重

    model = torch.hub.load('./detr', 'detr_resnet50', pretrained=False,source='local')
    model.load_state_dict(torch.load('detr-r50-e632da11.pth')["model"])
    model.eval()
    
  • PIL报错

    Traceback (most recent call last):
    File "clip_inference.py", line 4, in <module>
        import clip
    File "D:\anaconda3\envs\changeclip\lib\site-packages\clip\__init__.py", line 1, in <module>
        from .clip import *
    File "D:\anaconda3\envs\changeclip\lib\site-packages\clip\clip.py", line 9, in <module>
        from PIL import Image
    File "D:\anaconda3\envs\changeclip\lib\site-packages\PIL\Image.py", line 100, in <module>
        from . import _imaging as core
    ImportError: DLL load failed while importing _imaging: 找不到指定的模块
    

    解决方法:重新安装pillow,使用pip而不是conda防止影响其他包

    pip uninstall pillow
    pip install pillow
    

Latex

  • 拖式计算,对齐等号

    & 对齐标志
    \\ 换行(必须有换行,否则对齐无效)
    $$
    \begin{aligned}
    CPU时间&=(CPU执行周期数+存储器停顿周期数)*时钟周期时间\\
    &=(CPU执行周期数+访存次数*不命中率*不命中开销)*时钟周期时间\\
    &=IC*(CPI_{execution}+每条指令平均访存次数*不命中率*不命中开销)*时钟周期时间
    \end{aligned}
    $$
    

    $$
    \begin{aligned}
    CPU时间&=(CPU执行周期数+存储器停顿周期数)时钟周期时间\
    &=(CPU执行周期数+访存次数
    不命中率不命中开销)时钟周期时间\
    &=IC
    (CPI_{execution}+每条指令平均访存次数
    不命中率*不命中开销)*时钟周期时间
    \end{aligned}
    $$

  • acmart class下的tex包的baselinestretch问题

    \let\savedbaselinestretch\baselinestretch
    \usepackage[UTF8]{ctex}
    \let\baselinestretch\savedbaselinestretch
    
  • 中文支持

    • 添加\usepackage[UTF-8]{ctex}
    • 将编译器改为XeLaTex

Linux

  • 根据进程号查看命令运行文件的路径pwdx PID