Translate

Showing posts with label deep learning. Show all posts
Showing posts with label deep learning. Show all posts

What is huggingface?

1. 一个开源的AI社区,提供模型,数据集,一些工具等

2. Spaces 应用:通过网页端展示自己模型的输入输出和别的交互

3. Gradio:给任意python函数加上UI页面的库,用于页面展示

4. 几个重要的库:Transformer / Datasets / Evaluate / Accelerate / Optimum / Diffusers ...

    Datasets:

            安装:pip install datasets

            导入:from datasets import load_dataset

            使用:dataset = load_dataset('imdb')

            查看数据集大小:print(len(dataset['train']))

                                    print(len(dataset['test']))

            第一次使用数据集的时候,如果数据集还未下载,会自动下载并保存在

                    ~/.cache/huggingface/datasets

            如果想控制下载的目录:dataset = load_dataset('imdb', cache_dir="./imdb")

            * huggingface 下载的图像数据集的格式 并不是.jpg的可视化格式

    
    Accelerate: 一个帮助模型加速的库

            是 Hugging Face 开源的一个方便将 PyTorch 模型迁移到 GPU/multi-GPUs/TPU/fp16/bf16 模式下训练的小巧工具
            只需要增加几行代码就可以在任何分布式配置中运行相同的PyTorch代码

            安装:pip install git+https://github.com/huggingface/accelerate

            配置:accelerate config

            检查配置是否正常:accelerate env


             * 以下为简单的配置方法,Accelerate将自动利用可用gpu的最大数量,并设置混合精度模式

                            python -c "from accelerate.utils import write_basic_config;                                            write_basic_config(mixed_precision='fp16')"


            用accelerate 执行脚本:accelerate launch {my_script.py}

            





    ref: https://huggingface.co/datasets/HuggingFace-CN-community/translation/blob/main/eat_accelerate_in_30_minites.md
        https://juejin.cn/post/7232091653065015355






Some commands for YOLOv5

1. train yolov5 using multi-gpu:

python -m torch.distributed.run --nproc_per_node 8 train.py --batch 64 --epochs 400 
--data ./data/fall.yaml --weights yolov5m.pt --device 0,1,2,3,4,5,6,7


2. yolov5 convert pytorch to onnx:

python export.py --weights yolov5m.pt --include onnx --dynamic

3. yolov5 convert onnx to trt:

/usr/src/tensorrt/bin/trtexec --onnx=./fall_det/1/0620.onnx --saveEngine=./fall_det/1/model.plan --workspace=8192 --explicitBatch --fp16 --verbose --dumpOutput --minShapes='images':1x3x640x640 --optShapes='images':10x3x640x640  --maxShapes='images':10x3x640x640





The commands to convert onnx model to trt model

For static batchsize:

/usr/src/tensorrt/bin/trtexec --onnx=model.onnx --saveEngine=model.engine --explicitBatch --fp16 --verbose --workspace=8192 --dumpOutput


For dynamic batchsize:

/usr/src/tensorrt/bin/trtexec --onnx=model.onnx --saveEngine=model.engine --explicitBatch --fp16 --verbose --dumpOutput --minShapes='input':1x3x640x640 --optShapes='input':6x3x640x640  --maxShapes='input':12x3x640x640 --exportTimes=trace.json --dumpProfile --exportProfile=prof.json 


Euclidean distance VS Cosine distance

Before we talk about euclidean distance and cosine distance, let's see what is Norm firstly.

    1. What is Norm?

        The distance from the origin is called Norm of $\vec{A}$.
        To calculate Norm of $\vec{A}$, there are several methods:
            1) Euclidean distance
                $$d=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}$$
                Norm is the distance from the origin, so it equals to $\sqrt{x_2^2+y_2^2}$
            
            Vector Norm using Euclidean distance is called L2-Norm.

            2) Manhattan distance
                $$d=|x_2-x_1|+|y_2-y_1|$$
                Norm equals to $|x_2|+|y_2|$
            
            Vector Norm using Manhattan distance is called L1-Norm.

    2. Euclidean distance
        
        If $\vec{A}$ = $(x_1,y_1)$, $\vec{B}$ = $(x_2,y_2)$,
        The Euclidean distance between $\vec{A}$ and $\vec{B}$ should be:
            $$d=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}$$
        Smaller, closer.

    3. Cosine similarity

$$\cos\theta=\frac{\vec{A}\cdot\vec{B}}{||\vec{A}||\cdot||\vec{B}||}=\frac{\sum_{i=1}^{n}x_iy_i}{\sqrt{\sum_{i=1}^{n}x_i^2}\cdot\sqrt{\sum_{i=1}^{n}y_i^2}}$$

        The cosine value means the angle between two vectors, $\cos\theta\in[-1,1]$
        More close to 1, more similar

    4. Cosine distance

        Cosine distance = $1-\cos\theta$. So, cosine distance $\in[0,2]$
        Smaller, closer.

* What is Normalization:
        $$\vec{y} = \frac{\vec{x}}{||\vec{x}||}$$
        $\vec{y}$ is $\vec{x}$ after normalization. All the value in $\vec{y} \in [-1,1]$. Also, $||\vec{y}||=1$.

    5. The relationship between Euclidean distance and Cosine distance:

        After normalization, $||\vec{A}||=1$, $||\vec{B}||=1$
        Cdist($\vec{A}$, $\vec{B}$) = $1-\vec{A}\cdot\vec{B}$       
        Edist($\vec{A}$, $\vec{B}$) = $\sqrt{||\vec{A}-\vec{B}||^2}$ = $\sqrt{||\vec{A}||^2+||\vec{B}||^2-2\vec{A}\vec{B}}$ = $\sqrt{2}\cdot\sqrt{1-\vec{A}\vec{B}}$
        Which means after normalization, Euclidean distance has the same monotonicity with Cosine distance.

    6. In python 
       
    Use cosine distance get topk vectors:
        f = f / np.linalg.norm(f, axis=-1. keepdims=True)
        sim = (features * f).sum(axis=1)
        topk_idx = np.argpartition(-sim, tuple(range(K)))[:K]
        topk_val = sim[topk_idx].tolist()