0%

endpoint代理k8s之外的服务,让其能在k8s内访问,或可以通过ingress进行访问。

快速开始

1
2
3
4
5
6
7
8
9
#安装 ---废弃开始----直接可以在idea里面新建quarkus的项目
➜ ~ brew install quarkusio/tap/quarkus
#检查版本
➜ ~ quarkus --version
3.12.3
#更新版本
➜ ~ brew update
➜ ~ brew upgrade quarkus
#---废弃结束----直接可以在idea里面新建quarkus的项目

问题描述

  • 环境: ESXi 7.0+centos7虚拟机

在一次操作生产服务器时,发生了系统文件丢失,导致重启之后无法进入系统,但是重要哦的文件还在系统中,第一时间应该先对当前系统【创建快照】,创建快照之后就可以【查看vmdk文件】

解决方案一:导出vmdk文件进行打开(废弃)

  1. 首先需要导出vmdk文件,可以【开启ESXI服务器的ssh功能】,然后使用scp root@<ESXi_IP>:/vmfs/volumes/datastore_name/vm_name/disk.vmdk /local/path/拷贝出来,如果文件太大,可以【创建虚拟机】作为临时存放,内网相对快很多
  2. 打开vmdk文件,试了各种都不能打开,但是也许window系统下有工具能打开

解决方案二:创建虚拟机加载vmdk为第二硬盘

  1. 【创建虚拟机】,虚拟机需要自定义分区,主要修改卷组(VG)名称,修改卷组名称,主要是防止逻辑卷名称冲突

  2. 启动新的虚拟机,启动成功后,【添加硬盘】,硬盘文件选择需要打开的vmdk文件

  3. 进入虚拟机,执行下面命令

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    #检查激活状态
    #有冲突就不会激活
    [root@localhost ~]# lvscan
    inactive '/dev/centos/swap' [15.75 GiB] inherit
    inactive '/dev/centos/home' [1.93 TiB] inherit
    inactive '/dev/centos/root' [50.00 GiB] inherit
    ACTIVE '/dev/centos/swap' [2.00 GiB] inherit
    ACTIVE '/dev/centos/home' [346.99 GiB] inherit
    ACTIVE '/dev/centos/root' [50.00 GiB] inherit
    #没有冲突会自动激活
    [root@localhost ~]# lvscan
    ACTIVE '/dev/centos/swap' [15.75 GiB] inherit
    ACTIVE '/dev/centos/home' [1.93 TiB] inherit
    ACTIVE '/dev/centos/root' [50.00 GiB] inherit
    ACTIVE '/dev/os/swap' [2.00 GiB] inherit
    ACTIVE '/dev/os/home' [346.99 GiB] inherit
    ACTIVE '/dev/os/root' [50.00 GiB] inherit
    #(可选不建议)如果忘了修改卷组(VG)名称,home目录可以在虚拟机里面修改,其他目录不行
    umount /dev/centos/home #卸载
    lvrename /dev/centos/home /dev/centos/home_old #重命名
    mount /dev/centos/home_old /home
    #挂载需要打开的vmdk之后执行下面的,激活命令
    lvchange -ay /dev/centos/home
    #--可选不建议结束---------
    #挂载目录
    mount /dev/centos/home /mnt/home
    #然后就可以进行拷贝备份了

VMware ESXI常用基础操作

  • 创建快照:选择虚拟机,点击 Actions > Snapshot > Take Snapshot

  • 查看vmdk文件:在左侧菜单,点击Storage>Data_disk>对应的虚拟机服务

    1
    2
    3
    4
    #关键文件内容如下
    server.vmdk # 基础文件
    server-000001.vmdk #创建快照产生,从基础文件开始记录所有新的更改
    server-000002.vmdk #纪录新的改变
  • 开启ESXI服务器的ssh功能:在左侧菜单,点击Host>Manage>Services,里面开启ssh功能。开启后用户名和密码同网页登录esxi界面一样

  • 创建虚拟机:在左侧菜单,点击Virtual Machines>create,选择linux,centos7,在iso里面选择Storage里面的iso镜像,然后就是安装centos的步骤了。设置静态ip和ESXI服务器在同一网段中。

  • 添加硬盘:选择虚拟机,点击 edit > add hard disk 进行添加硬盘。

  • 合并快照磁盘:选择虚拟机,点击 Actions > Snapshot >Snapshot Manager> Consolidate

安装

elasticsearch安装

  1. docker.elastic.co仓库查找springboot对应的es版本,,eg:

    1
    2
    3
    4
    # springboot 2.7.18
    docker pull docker.elastic.co/elasticsearch/elasticsearch:7.17.22
    #如果上面的拉取太慢了,可以用下面的dockerhub的镜像
    docker pull elastic/elasticsearch:7.17.22
  2. 创建配置文件,执行vim /workspace/elasticsearch.yml写入如下内容

    1
    2
    3
    4
    5
    6
    cluster.name: "docker-cluster"
    network.host: 0.0.0.0
    http.port: 9200
    http.cors.enabled: true
    http.cors.allow-origin: "*"
    xpack.security.enabled: false
  3. 安装elasticsearch7.17,执行

    1
    2
    3
    4
    5
    6
    7
    mkdir /workspace/v_es_data/
    chmod -R 777 /workspace/v_es_data/
    # -m限制使用内存1g
    docker run -d -m 1g -p 9200:9200 -e "discovery.type=single-node" --name es \
    -v /workspace/v_es_data:/usr/share/elasticsearch/data \
    -v /workspace/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
    docker.elastic.co/elasticsearch/elasticsearch:7.17.22
  4. 测试,执行下面命令

    1
    2
    3
    ➜  ~ curl -X GET "127.0.0.1:9200/_cat/nodes?v=true&pretty"
    ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
    172.18.0.2 35 84 5 0.06 0.09 0.13 cdfhilmrstw * eeeedfa1ed88

kibana安装(没采用)

可视化查看工具,类似dbeaver,非必需

  1. 执行下面命令进行安装

    1
    2
    3
    docker run -d --name kibana -p 5601:5601 \
    -e "ELASTICSEARCH_HOSTS=http://127.0.0.1:9200" \
    docker.elastic.co/kibana/kibana:7.17.22
  2. 访问http://localhost:5601/

  3. 然后进行各种操作,比较复杂

本地采用es-client插件

数据同步方案

应用层同步

在应用层面进行数据同步,即在应用代码中同时写入 MySQL 和 Elasticsearch。

优点:依赖少,自定义同步逻辑。

缺点:数据一致性和可靠性方面存在一些挑战,特别是在高并发的场景下。

MySQL binlog(数据库日志)

借助于数据库日志(如 MySQL binlog)来捕获数据变化,并将其同步到 Elasticsearch。这种方法通常能够实时同步数据,并且不会对数据库性能造成太大影响。

优点:实时同步数据,不会对数据库性能造成太大影响。

缺点:性能开销,binlog 会占用额外的磁盘空间。

Debezium(重量级别)

优点:功能强大,支持复杂的配置和扩展,社区活跃。

缺点:需要 Kafka,配置相对复杂。

Maxwell(轻量级别)

Maxwell是一种开源的MySQL数据库同步工具,它可以将MySQL数据库的binlog转化为JSON格式,并将其发送到消息队列中。

需要mq和kafka等中间件

优点:轻量级,配置简单,适合快速实现。

缺点:功能相对简单,不支持复杂的场景

canal

Canal是阿里巴巴开发的一款数据库同步工具,它可以实现MySQL数据库的binlog解析和日志抓取。模拟mysql的从节点实现。

springboot接入ES

  1. 在pom.xml添加依赖

    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>

maxwell

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
version: '3.8'

services:
mysql:
image: mysql:8.0
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: maxwell
MYSQL_USER: maxwell
MYSQL_PASSWORD: maxwellpassword
ports:
- "3306:3306"
command: --server-id=1 --log-bin=mysql-bin --binlog-format=row --binlog-row-image=full
volumes:
- mysql-data:/var/lib/mysql

maxwell:
image: zendesk/maxwell:1.33.0
container_name: maxwell
environment:
- MAXWELL_MYSQL_HOST=mysql
- MAXWELL_MYSQL_USER=maxwell
- MAXWELL_MYSQL_PASSWORD=maxwellpassword
- MAXWELL_MYSQL_DATABASE=maxwell
- MAXWELL_PRODUCER=es
- MAXWELL_OUTPUT_DDL=true
- MAXWELL_ES_HOST=http://119.91.96.139:9200
depends_on:
- mysql

volumes:
mysql-data:

参考

4种数据同步到Elasticsearch方案

centos7如何安装CUDA

  1. 查看显卡型号lspci | grep -i vga,提示-bash: lspci: 未找到命令,执行yum install pciutils安装lspci命令

    1
    2
    [root@exxk ~]# lspci | grep -i vga
    01:00.0 VGA compatible controller: NVIDIA Corporation Device 25e2 (rev a1)
  2. 在输出结果中获取25e2关键字,在https://admin.pci-ids.ucw.cz/read/PC/10de/连接尾部拼接25e2,例如https://admin.pci-ids.ucw.cz/read/PC/10de/25e2,访问即可,页面回展示类似内容

    1
    2
    3
    Main -> PCI Devices -> Vendor 10de -> Device 10de:25e2

    Name: GA107BM [GeForce RTX 3050 Mobile]

    从中可以知道25e2对应的显卡是GeForce RTX 3050 Mobile

  3. 在该https://www.nvidia.cn/Download/index.aspx?lang=cn网页找到`GeForce RTX 3050 Mobile=GeForce RTX 3050 Laptop GPU该驱动,选择Linux64-bit然后下载下来 NVIDIA-Linux-x86_64-550.78.run `

  4. 拷贝到centos7上面,执行下面命令进行安装驱动

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    #屏蔽系统自带的nouveau显卡驱动(重启后生效---begin-----------
    [root@exxk ~]# lsmod | grep nouveau
    nouveau 2543616 0
    drm_ttm_helper 12288 1 nouveau
    ttm 90112 2 drm_ttm_helper,nouveau
    i2c_algo_bit 12288 1 nouveau
    mxm_wmi 12288 1 nouveau
    drm_display_helper 176128 1 nouveau
    drm_kms_helper 225280 4 drm_display_helper,nouveau
    drm 643072 6 drm_kms_helper,drm_display_helper,drm_ttm_helper,ttm,nouveau
    video 69632 2 ideapad_laptop,nouveau
    wmi 36864 5 video,wmi_bmof,ideapad_laptop,mxm_wmi,nouveau
    [root@exxk ~]# vi /lib/modprobe.d/dist-blacklist.conf
    #将nvidiafb注释掉:
    #blacklist nvidiafb
    #然后添加以下语句:
    #blacklist nouveau
    #options nouveau modeset=0
    [root@exxk ~]# mv /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.bak
    [root@exxk ~]# dracut /boot/initramfs-$(uname -r).img $(uname -r)
    [root@exxk ~]# reboot
    [root@exxk ~]# lsmod | grep nouveau
    #重启之后发现lsmod | grep nouveau命令无任何输出代表成功
    #屏蔽系统自带的nouveau显卡驱动(重启后生效---end-----------

    #安装依赖环境------------begin--------------------------
    #安装kernel-devel kernel-headers gcc
    [root@exxk ~]# yum install kernel-devel gcc -y
    [root@exxk ~]# uname -r
    3.10.0-1160.118.1.el7.x86_64
    [root@exxk ~]# yum info kernel-devel kernel-headers | grep 发布
    发布 :1160.118.1.el7
    发布 :1160.118.1.el7
    #内核版本要和安装的版本一致
    #如果不一致,检查内核是否需要升级
    [root@exxk ~]# yum check-update kernel
    #查看可用内核
    [root@exxk ~]# cat /boot/grub2/grub.cfg |grep menuentry
    #升级到和安装工具一致的内核版本
    [root@exxk ~]# grub2-set-default 'CentOS Linux (3.10.0-1160.118.1.el7.x86_64) 7 (Core)'
    [root@exxk ~]# reboot
    #安装依赖环境------------end--------------------------

    #安装显卡驱动,一致点同意即可
    [root@exxk ~]# ./NVIDIA-Linux-x86_64-550.78.run
    #查看驱动信息
    [root@exxk ~]# nvidia-smi
    Sat May 11 14:43:05 2024
    +-----------------------------------------------------------------------------------------+
    | NVIDIA-SMI 550.78 Driver Version: 550.78 CUDA Version: 12.4 |
    |-----------------------------------------+------------------------+----------------------+
    | GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
    | Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
    | | | MIG M. |
    |=========================================+========================+======================|
    | 0 NVIDIA GeForce RTX 3050 ... Off | 00000000:01:00.0 Off | N/A |
    | N/A 47C P0 10W / 60W | 1MiB / 4096MiB | 0% Default |
    | | | N/A |
    +-----------------------------------------+------------------------+----------------------+

    +-----------------------------------------------------------------------------------------+
    | Processes: |
    | GPU GI CI PID Type Process name GPU Memory |
    | ID ID Usage |
    |=========================================================================================|
    | No running processes found |
    +-----------------------------------------------------------------------------------------+
  5. 安装NVIDIA Container Toolkit,执行下面的命令(确保您已安装适用于您的 Linux 发行版的NVIDIA 驱动程序 请注意,您不需要在主机系统上安装 CUDA Toolkit,但需要安装 NVIDIA 驱动程序)

    1
    2
    3
    4
    [root@exxk ~]# curl -s -L https://nvidia.github.io/libnvidia-container/stable/rpm/nvidia-container-toolkit.repo | sudo tee /etc/yum.repos.d/nvidia-container-toolkit.repo
    [root@exxk ~]# yum-config-manager --enable nvidia-container-toolkit-experimental
    yum install -y nvidia-container-toolkit
    [root@exxk ~]# yum install -y nvidia-container-toolkit
  6. 配置containerd(针对Kubernetes),执行下面命令

    1
    2
    3
    4
    5
    [root@exxk ~]# nvidia-ctk runtime configure --runtime=containerd
    INFO[0000] Loading config from /etc/containerd/config.toml
    INFO[0000] Wrote updated config to /etc/containerd/config.toml
    INFO[0000] It is recommended that containerd daemon be restarted.
    [root@exxk ~]# systemctl restart containerd
  7. 安装 PaddlePaddle 镜像

    1
    nvidia-docker pull registry.baidubce.com/paddlepaddle/paddle:2.6.1-gpu-cuda12.0-cudnn8.9-trt8.6

新版cuda安装(废弃采用容器安装)

  1. 安装cuda 12.4.1最新版(飞浆需要12.0的版本,因此将下面的替换成12.0.1)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    wget https://developer.download.nvidia.com/compute/cuda/12.4.1/local_installers/cuda_12.4.1_550.54.15_linux.run
    sh cuda_12.4.1_550.54.15_linux.run
    ┌──────────────────────────────────────────────────────────────────────────────┐
    │ CUDA Installer │
    │ - [ ] Driver │
    │ [ ] 550.54.15 │
    │ + [X] CUDA Toolkit 12.4 │
    │ [X] CUDA Demo Suite 12.4 │
    │ [X] CUDA Documentation 12.4 │
    │ - [ ] Kernel Objects │
    │ [ ] nvidia-fs │
    │ Options │
    │ Install │
    │ │
    │ Up/Down: Move | Left/Right: Expand | 'Enter': Select | 'A': Advanced options │
    └──────────────────────────────────────────────────────────────────────────────┘
    ===========
    = Summary =
    ===========

    Driver: Not Selected
    Toolkit: Installed in /usr/local/cuda-12.4/

    Please make sure that
    - PATH includes /usr/local/cuda-12.4/bin
    - LD_LIBRARY_PATH includes /usr/local/cuda-12.4/lib64, or, add /usr/local/cuda-12.4/lib64 to /etc/ld.so.conf and run ldconfig as root

    To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-12.4/bin
    ***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 550.00 is required for CUDA 12.4 functionality to work.
    To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
    sudo <CudaInstaller>.run --silent --driver

    Logfile is /var/log/cuda-installer.log
  2. 添加环境变量,修改/etc/profile文件,执行vi /etc/profile,增加如下内容,然后执行source /etc/profile

    1
    2
    export PATH=${PATH}:/usr/local/cuda-12.4/bin
    export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/cuda-12.4/lib64
  3. 验证

    1
    2
    3
    4
    5
    6
    [root@exxk ~]# nvcc -V
    nvcc: NVIDIA (R) Cuda compiler driver
    Copyright (c) 2005-2024 NVIDIA Corporation
    Built on Thu_Mar_28_02:18:24_PDT_2024
    Cuda compilation tools, release 12.4, V12.4.131
    Build cuda_12.4.r12.4/compiler.34097967_0
  4. 卸载

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    [root@exxk ~]# cd /usr/local/cuda-12.4/bin
    [root@exxk bin]# ./cuda-uninstaller
    ┌──────────────────────────────────────────────────────────────────────────────┐
    │ CUDA Uninstaller │
    │ [X] CUDA_Toolkit_12.4 │
    │ [X] CUDA_Demo_Suite_12.4 │
    │ [X] CUDA_Documentation_12.4 │
    │ Done │
    │ │
    │ Up/Down: Move | 'Enter': Select │
    └──────────────────────────────────────────────────────────────────────────────┘
    Successfully uninstalled
    [root@exxk local]# cd /usr/local/
    [root@exxk local]# rm -rf cuda-12.4

旧版cuda废弃

  1. 下载 developer.nvidia.com页面按自己的环境选择,然后下载cuda。安装方式我选择的runfile(local),下载完成后上传到centos

  2. 执行sh cuda_7.5.18_linux.run,按ctrl+c跳过文档阅读

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    [root@exxk ~]# sh cuda_7.5.18_linux.run
    Logging to /tmp/cuda_install_22223.log
    Using more to view the EULA.
    End User License Agreement
    --------------------------
    --More--(0%)
    Do you accept the previously read EULA? (accept/decline/quit): accept
    # 非常关键,我们已经在之前安装了高版本的驱动,这个千万别装。
    Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 352.39? ((y)es/(n)o/(q)uit): n
    Install the CUDA 7.5 Toolkit? ((y)es/(n)o/(q)uit): y
    Enter Toolkit Location [ default is /usr/local/cuda-7.5 ]:
    Do you want to install a symbolic link at /usr/local/cuda? ((y)es/(n)o/(q)uit): y
    Install the CUDA 7.5 Samples? ((y)es/(n)o/(q)uit): y
    Enter CUDA Samples Location [ default is /root ]:
    Installing the CUDA Toolkit in /usr/local/cuda-7.5 ...
    Missing recommended library: libGLU.so
    Missing recommended library: libX11.so
    Missing recommended library: libXi.so
    Missing recommended library: libXmu.so

    Installing the CUDA Samples in /root ...
    Copying samples to /root/NVIDIA_CUDA-7.5_Samples now...
    Finished copying samples.

    ===========
    = Summary =
    ===========

    Driver: Not Selected
    Toolkit: Installed in /usr/local/cuda-7.5
    Samples: Installed in /root, but missing recommended libraries

    Please make sure that
    - PATH includes /usr/local/cuda-7.5/bin
    - LD_LIBRARY_PATH includes /usr/local/cuda-7.5/lib64, or, add /usr/local/cuda-7.5/lib64 to /etc/ld.so.conf and run ldconfig as root

    To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-7.5/bin
    To uninstall the NVIDIA Driver, run nvidia-uninstall

    Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-7.5/doc/pdf for detailed information on setting up CUDA.

    ***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 352.00 is required for CUDA 7.5 functionality to work.
    To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
    sudo <CudaInstaller>.run -silent -driver

    Logfile is /tmp/cuda_install_22223.log
    Signal caught, cleaning up
  3. 添加环境变量,修改/etc/profile文件,执行vi /etc/profile,增加如下内容,然后执行source /etc/profile

    1
    2
    export PATH=${PATH}:/usr/local/cuda/bin
    export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/cuda/lib64
  4. 验证环境

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    [root@exxk ~]# nvcc -V
    nvcc: NVIDIA (R) Cuda compiler driver
    Copyright (c) 2005-2015 NVIDIA Corporation
    Built on Tue_Aug_11_14:27:32_CDT_2015
    Cuda compilation tools, release 7.5, V7.5.17
    # 如果前面安装了CUDA的example,这里可以执行如下操作:
    [root@exxk ~]# cd /root/NVIDIA_CUDA-7.5_Samples/1_Utilities/deviceQuery
    [root@exxk deviceQuery]# yum install gcc gcc-c++
    [root@exxk deviceQuery]# make
    "/usr/local/cuda-7.5"/bin/nvcc -ccbin g++ -I../../common/inc -m64 -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_52,code=compute_52 -o deviceQuery.o -c deviceQuery.cpp
    "/usr/local/cuda-7.5"/bin/nvcc -ccbin g++ -m64 -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_52,code=compute_52 -o deviceQuery deviceQuery.o
    mkdir -p ../../bin/x86_64/linux/release
    cp deviceQuery ../../bin/x86_64/linux/release
    [root@exxk deviceQuery]# ./deviceQuery
    ./deviceQuery Starting...

    CUDA Device Query (Runtime API) version (CUDART static linking)

    Detected 1 CUDA Capable device(s)

    Device 0: "NVIDIA GeForce RTX 3050 Laptop GPU"
    CUDA Driver Version / Runtime Version 12.4 / 7.5
    CUDA Capability Major/Minor version number: 8.6
    Total amount of global memory: 3873 MBytes (4060872704 bytes)
    MapSMtoCores for SM 8.6 is undefined. Default to use 128 Cores/SM
    MapSMtoCores for SM 8.6 is undefined. Default to use 128 Cores/SM
    (16) Multiprocessors, (128) CUDA Cores/MP: 2048 CUDA Cores
    GPU Max Clock rate: 1500 MHz (1.50 GHz)
    Memory Clock rate: 6001 Mhz
    Memory Bus Width: 128-bit
    L2 Cache Size: 1572864 bytes
    Maximum Texture Dimension Size (x,y,z) 1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
    Maximum Layered 1D Texture Size, (num) layers 1D=(32768), 2048 layers
    Maximum Layered 2D Texture Size, (num) layers 2D=(32768, 32768), 2048 layers
    Total amount of constant memory: 65536 bytes
    Total amount of shared memory per block: 49152 bytes
    Total number of registers available per block: 65536
    Warp size: 32
    Maximum number of threads per multiprocessor: 1536
    Maximum number of threads per block: 1024
    Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
    Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535)
    Maximum memory pitch: 2147483647 bytes
    Texture alignment: 512 bytes
    Concurrent copy and kernel execution: Yes with 2 copy engine(s)
    Run time limit on kernels: No
    Integrated GPU sharing Host Memory: No
    Support host page-locked memory mapping: Yes
    Alignment requirement for Surfaces: Yes
    Device has ECC support: Disabled
    Device supports Unified Addressing (UVA): Yes
    Device PCI Domain ID / Bus ID / location ID: 0 / 1 / 0
    Compute Mode:
    < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

    deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 12.4, CUDA Runtime Version = 7.5, NumDevs = 1, Device0 = NVIDIA GeForce RTX 3050 Laptop GPU
    Result = PASS
  5. 卸载 /usr/local/cuda-7.5/bin/uninstall_cuda_7.5.pl

参考

linux服务器上查看显卡(nvidia)型号

Centos7.9安装Nvidia驱动

CentOS7安装NVIDIA显卡驱动

CentOS 7安装GPU、Cuda、Tensorflow

centos7 安装cuda toolkit

PaddleDetection是一个基于PaddlePaddle的目标检测端到端开发套件。

PP-Human 实时行人分析工具

PP-Human行人分析工具使用指南

安装使用步骤

  1. 在PyCharm欢迎页通过Get from VCS拉取github上PaddlePaddle/PaddleSeg项目。

  2. 拉取成功后,PyCharm会提示创建venv虚拟环境,点击确定即可,在PyCharm打开终端命令行前面有venv代表创建成功,如果没有,关闭终端点击Add new Interpreter(需要python3.10的环境),添加后,重新在PyCharm打开终端

    1
    2
    例如:
    (venv) ➜ PaddleSeg git:(release/2.9)
  3. 安装paddle,在PyCharm中Terminal命令行中执行pip install paddlepaddle

  4. 安装setuptools,在PyCharm中Terminal命令行中执行pip install -U pip setuptools

  5. 安装依赖,在PyCharm中Terminal命令行中执行 pip install -r requirements.txt

使用步骤

1
2
3
4
# 运行图片检测人,结果输出在output目录
python deploy/pipeline/pipeline.py --config deploy/pipeline/config/infer_cfg_pphuman.yml --image_file=demo/000000014439.jpg
# 运行视频流检测人
python deploy/pipeline/pipeline.py --config deploy/pipeline/config/examples/infer_cfg_human_attr.yml -o visual=False --rtsp rtsp://admin:123456@172.16.1.1:554/ch01.264?dev=1

检查人并输出视频

  1. 安装ffmpeg: brew install ffmpeg

  2. 下载安装rtsp推流服务bluenviron/mediamtx,在release页面选择适合的版本,我的是inter的mac,选择mediamtx_v1.8.1_darwin_amd64.tar.gz即可

  3. 解压,然后运行./mediamtx

  4. 运行视频流检测并推流python deploy/pipeline/pipeline.py --config deploy/pipeline/config/examples/infer_cfg_human_attr.yml --rtsp rtsp://admin:123456@172.1.1.1:554/ch01.264?dev=1 --pushurl rtsp://127.0.0.1:8554

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    python deploy/pipeline/pipeline.py --config deploy/pipeline/config/examples/infer_cfg_human_attr.yml --rtsp rtsp://admin:123456@172.16.80.59:554/ch01.264?dev=1 --pushurl rtsp://127.0.0.1:8554 
    ----------- Running Arguments -----------
    ATTR:
    batch_size: 8
    enable: true
    model_dir: https://bj.bcebos.com/v1/paddledet/models/pipeline/PPLCNet_x1_0_person_attribute_945_infer.zip
    DET:
    batch_size: 1
    model_dir: https://bj.bcebos.com/v1/paddledet/models/pipeline/pphuman/ppyoloe_plus_crn_t_auxhead_320_60e_pphuman.tar.gz
    MOT:
    batch_size: 1
    enable: true
    model_dir: https://bj.bcebos.com/v1/paddledet/models/pipeline/pphuman/ppyoloe_plus_crn_t_auxhead_320_60e_pphuman.tar.gz
    tracker_config: deploy/pipeline/config/tracker_config.yml
    attr_thresh: 0.5
    crop_thresh: 0.5
    visual: true
    warmup_frame: 50

    ------------------------------------------
    Multi-Object Tracking enabled
    Human Attribute Recognition enabled
    100%|██████████| 15897/15897 [00:05<00:00, 2768.11KB/s]
    DET model dir: /.cache/paddle/infer_weights/ppyoloe_plus_crn_t_auxhead_320_60e_pphuman
    MOT model dir: /.cache/paddle/infer_weights/ppyoloe_plus_crn_t_auxhead_320_60e_pphuman
    ATTR model dir: /.cache/paddle/infer_weights/PPLCNet_x1_0_person_attribute_945_infer
    ----------- Model Configuration -----------
    Model Arch: StrongBaseline
    Transform Order:
    --transform op: Resize
    --transform op: NormalizeImage
    --transform op: Permute
    --------------------------------------------
    ----------- Model Configuration -----------
    Model Arch: PPYOLOE
    Transform Order:
    --transform op: Resize
    --transform op: Permute
    --------------------------------------------
    video fps: 25, frame_count: -2562047788015215
    the result will push stream to url:rtsp://127.0.0.1:8554/ch01
    ffmpeg version 7.0 Copyright (c) 2000-2024 the FFmpeg developers
    built with Apple clang version 15.0.0 (clang-1500.1.0.2.5)
    configuration: --prefix=/usr/local/Cellar/ffmpeg/7.0_1 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags='-Wl,-ld_classic' --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libharfbuzz --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopenvino --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox
    libavutil 59. 8.100 / 59. 8.100
    libavcodec 61. 3.100 / 61. 3.100
    libavformat 61. 1.100 / 61. 1.100
    libavdevice 61. 1.100 / 61. 1.100
    libavfilter 10. 1.100 / 10. 1.100
    libswscale 8. 1.100 / 8. 1.100
    libswresample 5. 1.100 / 5. 1.100
    libpostproc 58. 1.100 / 58. 1.100
    Thread: 0; frame id: 0
    Thread: 0; trackid number: 2
    Input #0, rawvideo, from 'fd:':
    Duration: N/A, start: 0.000000, bitrate: 552960 kb/s
    Stream #0:0: Video: rawvideo (BGR[24] / 0x18524742), bgr24, 1280x720, 552960 kb/s, 25 tbr, 25 tbn
    Stream mapping:
    Stream #0:0 -> #0:0 (rawvideo (native) -> mpeg4 (native))
    Output #0, rtsp, to 'rtsp://127.0.0.1:8554/ch01':
    Metadata:
    encoder : Lavf61.1.100
    Stream #0:0: Video: mpeg4, yuv420p(progressive), 1280x720, q=2-31, 200 kb/s, 25 fps, 90k tbn
    Metadata:
    encoder : Lavc61.3.100 mpeg4
    Side data:
    cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A
    Thread: 0; frame id: 10
    Thread: 0; trackid number: 2
  5. 然后使用vlc播放rtsp://127.0.0.1:8554/ch01流即可,流的地址在上一步日志里面可以看到the result will push stream to url:rtsp://127.0.0.1:8554/ch01

  6. 默认的模型,识别速度个人电脑会卡顿,可以替换为小模型,模型地址可以在PP-Human快速开始-模型下载里面进行找

    1
    2
    3
    4
    5
    修改该deploy/pipeline/config/examples/infer_cfg_human_attr.yml文件
    替换
    model_dir: https://bj.bcebos.com/v1/paddledet/models/pipeline/mot_ppyoloe_l_36e_pipeline.zip

    model_dir: https://bj.bcebos.com/v1/paddledet/models/pipeline/pphuman/ppyoloe_plus_crn_t_auxhead_320_60e_pphuman.tar.gz

常见错误

  1. 安装依赖,执行pip install -r requirements.txt时报AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?错误,详细信息如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    (.venv) ➜  PaddleDetection git:(release/2.7) pip install -r requirements.txt
    Collecting numpy<1.24 (from -r requirements.txt (line 1))
    Using cached numpy-1.23.5.tar.gz (10.7 MB)
    Installing build dependencies ... done
    Getting requirements to build wheel ... error
    error: subprocess-exited-with-error

    × Getting requirements to build wheel did not run successfully.
    │ exit code: 1
    ╰─> [33 lines of output]
    Traceback (most recent call last):
    File "workspace/PaddleDetection/.venv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
    main()
    File "workspace/PaddleDetection/.venv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
    json_out['return_val'] = hook(**hook_input['kwargs'])
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "workspace/PaddleDetection/.venv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 112, in get_requires_for_build_wheel
    backend = _build_backend()
    ^^^^^^^^^^^^^^^^
    File "workspace/PaddleDetection/.venv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 77, in _build_backend
    obj = import_module(mod_path)
    ^^^^^^^^^^^^^^^^^^^^^^^
    File "/usr/local/Cellar/python@3.12/3.12.2_1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
    File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
    File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
    File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
    File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
    File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
    File "<frozen importlib._bootstrap_external>", line 995, in exec_module
    File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
    File "/private/var/folders/jd/0572r10n2rbcwn4tf1w6sy500000gn/T/pip-build-env-r6d8n8j4/overlay/lib/python3.12/site-packages/setuptools/__init__.py", line 16, in <module>
    import setuptools.version
    File "/private/var/folders/jd/0572r10n2rbcwn4tf1w6sy500000gn/T/pip-build-env-r6d8n8j4/overlay/lib/python3.12/site-packages/setuptools/version.py", line 1, in <module>
    import pkg_resources
    File "/private/var/folders/jd/0572r10n2rbcwn4tf1w6sy500000gn/T/pip-build-env-r6d8n8j4/overlay/lib/python3.12/site-packages/pkg_resources/__init__.py", line 2172, in <module>
    register_finder(pkgutil.ImpImporter, find_on_path)
    ^^^^^^^^^^^^^^^^^^^
    AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
    [end of output]

    note: This error originates from a subprocess, and is likely not a problem with pip.
    error: subprocess-exited-with-error

    × Getting requirements to build wheel did not run successfully.
    │ exit code: 1
    ╰─> See above for output.

    note: This error originates from a subprocess, and is likely not a problem with pip.

    原因:在docs/tutorials/INSTALL_cn.md文档里面可以查看到只支持Python 3(3.5.1+/3.6/3.7/3.8/3.9/3.10),64位版本。

    解决:执行brew install python@3.10,然后在上面安装的第二个步骤里面,切换成3.10的python环境

  2. 出现提示安装pip install numba==0.56.4的警告,安装即可

  3. 运行视频流检测人时,报错

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    python deploy/pipeline/pipeline.py --config deploy/pipeline/config/examples/infer_cfg_human_attr.yml -o visual=False --rtsp rtsp://admin:123456@172.16.1.1:554/ch01.264?dev=1 
    Traceback (most recent call last):
    File "workspace/PaddleDetection/deploy/pipeline/pipeline.py", line 1321, in <module>
    main()
    File "workspace/PaddleDetection/deploy/pipeline/pipeline.py", line 1303, in main
    cfg = merge_cfg(FLAGS) # use command params to update config
    File "workspace/PaddleDetection/deploy/pipeline/cfg_utils.py", line 212, in merge_cfg
    pred_config = merge_opt(pred_config, args_dict)
    File "workspace/PaddleDetection/deploy/pipeline/cfg_utils.py", line 202, in merge_opt
    for sub_k, sub_v in value.items():
    AttributeError: 'bool' object has no attribute 'items'

    Process finished with exit code 1

    解决:删除命令里面的-o visual=False,似乎是该参数有问题

  4. 运行视频流检测加推流的时候,报错

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    python deploy/pipeline/pipeline.py --config deploy/pipeline/config/examples/infer_cfg_human_attr.yml --rtsp rtsp://admin:hcy123456@172.16.80.229:554/ch01.264?dev=1 --pushurl rtsp://127.0.0.1:8554 
    ----------- Running Arguments -----------
    ATTR:
    batch_size: 8
    enable: true
    model_dir: https://bj.bcebos.com/v1/paddledet/models/pipeline/PPLCNet_x1_0_person_attribute_945_infer.zip
    DET:
    batch_size: 1
    model_dir: https://bj.bcebos.com/v1/paddledet/models/pipeline/mot_ppyoloe_l_36e_pipeline.zip
    MOT:
    batch_size: 1
    enable: true
    model_dir: https://bj.bcebos.com/v1/paddledet/models/pipeline/mot_ppyoloe_l_36e_pipeline.zip
    tracker_config: deploy/pipeline/config/tracker_config.yml
    attr_thresh: 0.5
    crop_thresh: 0.5
    visual: true
    warmup_frame: 50

    ------------------------------------------
    Multi-Object Tracking enabled
    Human Attribute Recognition enabled
    DET model dir: .cache/paddle/infer_weights/mot_ppyoloe_l_36e_pipeline
    MOT model dir: .cache/paddle/infer_weights/mot_ppyoloe_l_36e_pipeline
    ATTR model dir: .cache/paddle/infer_weights/PPLCNet_x1_0_person_attribute_945_infer
    ----------- Model Configuration -----------
    Model Arch: StrongBaseline
    Transform Order:
    --transform op: Resize
    --transform op: NormalizeImage
    --transform op: Permute
    --------------------------------------------
    ----------- Model Configuration -----------
    Model Arch: YOLO
    Transform Order:
    --transform op: Resize
    --transform op: Permute
    --------------------------------------------
    video fps: 25, frame_count: -2562047788015215
    the result will push stream to url:rtsp://127.0.0.1:8554/ch01
    Traceback (most recent call last):
    File "workspace/PaddleDetection/deploy/pipeline/pipeline.py", line 1321, in <module>
    main()
    File "workspace/PaddleDetection/deploy/pipeline/pipeline.py", line 1308, in main
    pipeline.run_multithreads()
    File "workspace/PaddleDetection/deploy/pipeline/pipeline.py", line 179, in run_multithreads
    self.predictor.run(self.input)
    File "workspace/PaddleDetection/deploy/pipeline/pipeline.py", line 533, in run
    self.predict_video(input, thread_idx=thread_idx)
    File "workspace/PaddleDetection/deploy/pipeline/pipeline.py", line 670, in predict_video
    pushstream.initcmd(fps, width, height)
    File "workspace/PaddleDetection/deploy/pipeline/pipe_utils.py", line 142, in initcmd
    self.pipe = sp.Popen(self.command, stdin=sp.PIPE)
    File "/usr/local/Cellar/python@3.10/3.10.14/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 971, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
    File "/usr/local/Cellar/python@3.10/3.10.14/Frameworks/Python.framework/Versions/3.10/lib/python3.10/subprocess.py", line 1863, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
    FileNotFoundError: [Errno 2] No such file or directory: 'ffmpeg'

    Process finished with exit code 1

    解决:安装ffmpeg,执行brew install ffmpeg

Agent DVR

下载安装即可

zoneminder

wsl2环境安装(Ubuntu22版本安装手册

  1. 执行下面的安装命令

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    sudo apt-get update && sudo apt upgrade -y
    sudo apt install -y software-properties-common
    sudo add-apt-repository ppa:iconnor/zoneminder-1.36
    sudo apt update
    sudo apt install -y zoneminder
    sudo a2enmod rewrite
    sudo a2enconf zoneminder
    sudo systemctl restart apache2
    sudo systemctl enable zoneminder
    sudo systemctl start zoneminder
  2. 访问http://hostname_or_ip/zm

  3. 添加监视器,查看官方使用手册

测试

两个都无法正常用下面脚本获取摄像头信息

测试脚本nvr_test.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# pip install onvif_zeep
# python nvr_test.py
from onvif import ONVIFCamera

def discover_onvif_cameras(nvr_ip, nvr_port, nvr_username, nvr_password):
# Connect to the NVR
nvr = ONVIFCamera(nvr_ip, nvr_port, nvr_username, nvr_password)

# Get the list of connected devices (cameras)
device_service = nvr.devicemgmt
cameras = device_service.GetDevices()

# Extract camera information
camera_info = []
for camera in cameras:
camera_info.append({
"name": camera.Name,
"address": camera.XAddr,
"username": camera.Username,
"password": camera.Password
})

return camera_info

# Replace these with your NVR credentials
nvr_ip = "NVR_IP_ADDRESS"
nvr_port = 80 # Default port for ONVIF
nvr_username = "USERNAME"
nvr_password = "PASSWORD"

# Discover ONVIF cameras connected to the NVR
cameras = discover_onvif_cameras(nvr_ip, nvr_port, nvr_username, nvr_password)

# Print the camera information
for camera in cameras:
print("Camera Name:", camera["name"])
print("Camera Address:", camera["address"])
print("Camera Username:", camera["username"])
print("Camera Password:", camera["password"])
print()

常见的AI深度学习框架

PaddleSeg

飞桨(PaddlePaddle)以百度多年的深度学习技术研究和业务应用为基础,集深度学习核心训练和推理框架、基础模型库、端到端开发套件、丰富的工具组件于一体,是中国首个自主研发、功能完备、开源开放的产业级深度学习平台。国内的可以优先选择paddlepaddle,这是因为百度在大力推广该框架,能提供丰富的算力支持和技术支持,且百度开源了众多模型和应用,是人工智能入门的一个好选择,且能在模型上面进行一系列魔改,强烈推荐!

pytorch

是由Facebook人工智能学院提供支持服务的,目前主要在学术研究方向领域处于领先地位,许多学术论文都是用pytorch编写的,因此使用范围更广

常见的标注工具

labelImg

目前还没用过,官方已经不维护,建议用label-studio

label-studio

还没用过,docker安装

labelme

启动有点慢

常用数据集检索网站

优先尝试:

​ ● 百度飞桨开源数据集

​ ● OpenDataLab 引领AI大模型时代的开放数据平台

​ ● kaggle开源数据集

​ ● 谷歌开源数据集

​ ● ModelScope开源数据集

​ ● TIANCHI天池开源数据集

​ ● HuggingFace开源数据集

​ ● 微软开源数据集

​ ● Roboflow Universe: Open Source Computer Vision Community

备选:

​ ● 启智开源数据集

​ ● 和鲸开源数据集

​ ● openimages开源数据集

​ ● GitHub开源数据集

​ ● AWS亚马逊开源数据集

​ ● LUGE千言开源数据集

​ ● UCI开源数据集

​ ● 计算机视觉开源数据集

​ ● Dataju聚数力开源数据集

​ ● Hyper超神经开源数据集

​ ● BAAI开源数据集

​ ● payititi帕衣提提开源数据集

注意事项:

​ 1. 检索关键词

​ 2. 数据集格式

PyCharm安装PaddleSeg

  1. 在PyCharm欢迎页通过Get from VCS拉取github上PaddlePaddle/PaddleSeg项目

  2. 拉取成功后,PyCharm会提示创建venv虚拟环境,点击确定即可,在PyCharm打开终端命令行前面有venv代表创建成功,如果没有,关闭终端点击Add new Interpreter,添加后,重新在PyCharm打开终端

    1
    2
    例如:
    (venv) ➜ PaddleSeg git:(release/2.9)
  3. 安装paddle,在PyCharm中Terminal命令行中执行pip install paddlepaddle

  4. 安装setuptools,在PyCharm中Terminal命令行中执行pip install -U pip setuptools

  5. [可选],检查paddle是否安装成功,在PyCharm中Python console执行import paddlepaddle.utils.run_check(),检查版本执行print(paddle.__version__)

  6. 安装paddleseg,在PyCharm中Terminal命令行中执行pip install paddleseg,(这里采用的直接安装发布的版本,本地编译未实验通过)

  7. 验证安装是否成功,在在PyCharm中Terminal命令行中执行sh tests/install/check_predict.sh

PaddleSeg使用

准备自定义数据集

标注工具(标注数据)

PddleSeg已支持2种标注工具:LabelMe、精灵数据标注工具

  1. 下载安装labelme

  2. 根据文档操作,进行标注数据,简单流程打开图片目录->创建多边形->框选要标注的数据->保存会在图片目录生成一个json文件,标注目录下的所有图片

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #标注前目录结构
    paddle
    |--image1.jpg
    |--image2.jpg
    |--...
    #标注后目录结构
    paddle
    |--image1.jpg
    |--image2.jpg
    |--...
    |--image1.json
    |--image2.json
    |--...
  3. 将标注的数据转换为模型训练时所需的数据格式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #python tools/data/labelme2seg.py [-h] 图片目录 输出目录
    python tools/data/labelme2seg.py /Users/x/Downloads/paddle /Users/xuanleung/Downloads/paddle_pr
    #转换后目录结构
    paddle
    paddle_pr
    |--annotations #红色背景,绿色标注的图片
    | |--image1.png
    | |--image2.png
    | |--...
    |--images
    | |--image1.jpg
    | |--image2.jpg
    | |--...
    |--class_names.txt #是数据集中所有标注类别的名称

切分数据

  1. 切分数据,对于所有原始图像和标注图像,需要按照比例划分为训练集、验证集、测试集。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #python tools/data/split_dataset_list.py <dataset_root:原始图像目录名> <images_dir_name:原始图像目录名> <labels_dir_name:标注图像目录名> ${FLAGS}
    python tools/data/split_dataset_list.py /Users/x/Downloads/paddle_pr images annotations --split 0.6 0.2 0.2 --format jpg png
    #执行后目录结构
    paddle_pr
    |--test.txt #测试集
    |--val.txt #验证集
    |--train.txt #训练集
    |--annotations #红色背景,绿色标注的图片
    | |--image1.png
    | |--image2.png
    | |--...
    |--images
    | |--image1.jpg
    | |--image2.jpg
    | |--...
    |--class_names.txt #是数据集中所有标注类别的名称
    #txt文件内容格式
    images/image1.jpg annotations/image1.png
    images/image2.jpg annotations/image2.png
    ....

    FLAGS说明:

    FLAG 含义 默认值 参数数目
    –split 训练集、验证集和测试集的切分比例 0.7 0.3 0 3
    –separator txt文件列表分隔符 “ “ 1
    –format 原始图像和标注图像的图片后缀 “jpg” “png” 2
    –postfix 按文件主名(无扩展名)是否包含指定后缀对图片和标签集进行筛选 “” “”(2个空字符) 2

准备配置文件

拷贝paddle_pr数据集目录到项目根目录,在项目下面的目录configs/quick_start/pp_liteseg_optic_disc_512x512_1k.yml,修改这两个路径配置:

1
2
3
train_dataset:
dataset_root: paddle_pr #数据集路径
train_path: paddle_pr/train.txt #数据集中用于训练的标识文件

配置文件说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
batch_size: 4  #设定batch_size的值即为迭代一次送入网络的图片数量,一般显卡显存越大,batch_size的值可以越大。如果使用多卡训练,总得batch size等于该batch size乘以卡数。
iters: 1000 #模型训练迭代的轮数

train_dataset: #训练数据设置
type: Dataset #指定加载数据集的类。数据集类的代码在`PaddleSeg/paddleseg/datasets`目录下。
dataset_root: data/optic_disc_seg #数据集路径
train_path: data/optic_disc_seg/train_list.txt #数据集中用于训练的标识文件
num_classes: 2 #指定类别个数(背景也算为一类)
mode: train #表示用于训练
transforms: #模型训练的数据预处理方式。
- type: ResizeStepScaling #将原始图像和标注图像随机缩放为0.5~2.0倍
min_scale_factor: 0.5
max_scale_factor: 2.0
scale_step_size: 0.25
- type: RandomPaddingCrop #从原始图像和标注图像中随机裁剪512x512大小
crop_size: [512, 512]
- type: RandomHorizontalFlip #对原始图像和标注图像随机进行水平反转
- type: RandomDistort #对原始图像进行亮度、对比度、饱和度随机变动,标注图像不变
brightness_range: 0.5
contrast_range: 0.5
saturation_range: 0.5
- type: Normalize #对原始图像进行归一化,标注图像保持不变

val_dataset: #验证数据设置
type: Dataset #指定加载数据集的类。数据集类的代码在`PaddleSeg/paddleseg/datasets`目录下。
dataset_root: data/optic_disc_seg #数据集路径
val_path: data/optic_disc_seg/val_list.txt #数据集中用于验证的标识文件
num_classes: 2 #指定类别个数(背景也算为一类)
mode: val #表示用于验证
transforms: #模型验证的数据预处理的方式
- type: Normalize #对原始图像进行归一化,标注图像保持不变

optimizer: #设定优化器的类型
type: SGD #采用SGD(Stochastic Gradient Descent)随机梯度下降方法为优化器
momentum: 0.9 #设置SGD的动量
weight_decay: 4.0e-5 #权值衰减,使用的目的是防止过拟合

lr_scheduler: # 学习率的相关设置
type: PolynomialDecay # 一种学习率类型。共支持12种策略
learning_rate: 0.01 # 初始学习率
power: 0.9
end_lr: 0

loss: #设定损失函数的类型
types:
- type: CrossEntropyLoss #CE损失
coef: [1, 1, 1] # PP-LiteSeg有一个主loss和两个辅助loss,coef表示权重,所以 total_loss = coef_1 * loss_1 + .... + coef_n * loss_n

model: #模型说明
type: PPLiteSeg #设定模型类别
backbone: # 设定模型的backbone,包括名字和预训练权重
type: STDC2
pretrained: https://bj.bcebos.com/paddleseg/dygraph/PP_STDCNet2.tar.gz

训练模型

使用pyCharm添加一个python run config的启动配置,script设置为tools/train.py,script parameters设置为--config configs/quick_start/pp_liteseg_optic_disc_512x512_1k.yml --save_interval 500 --do_eval --use_vdl --save_dir output,然后点击右上角的run
运行相关的日志如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/Users/x/workspace/PaddleSeg/venv/bin/python -X pycache_prefix=/Users/x/Library/Caches/JetBrains/PyCharm2023.3/cpython-cache /Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py --multiprocess --qt-support=auto --client 127.0.0.1 --port 64923 --file /Users/x/workspace/PaddleSeg/tools/train.py --config configs/quick_start/pp_liteseg_optic_disc_512x512_1k.yml --save_interval 500 --do_eval --use_vdl --save_dir output 
Connected to pydev debugger (build 233.15026.15)
2024-04-02 18:25:03 [WARNING] Add the `num_classes` in train_dataset and val_dataset config to model config. We suggest you manually set `num_classes` in model config.
2024-04-02 18:25:03 [INFO]
------------Environment Information-------------
platform: macOS-13.6.6-x86_64-i386-64bit
Python: 3.12.2 (main, Feb 6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)]
Paddle compiled with cuda: False
GCC: Apple clang version 14.0.3 (clang-1403.0.22.14.1)
PaddleSeg: 2.8.1
PaddlePaddle: 2.6.1
OpenCV: 4.5.5
------------------------------------------------
2024-04-02 18:25:03 [INFO]
---------------Config Information---------------
batch_size: 4
iters: 1000
train_dataset:
dataset_root: paddle_pr
mode: train
num_classes: 2
train_path: paddle_pr/train.txt
transforms:
- max_scale_factor: 2.0
min_scale_factor: 0.5
scale_step_size: 0.25
type: ResizeStepScaling
- crop_size:
- 512
- 512
type: RandomPaddingCrop
- type: RandomHorizontalFlip
- brightness_range: 0.5
contrast_range: 0.5
saturation_range: 0.5
type: RandomDistort
- type: Normalize
type: Dataset
val_dataset:
dataset_root: data/optic_disc_seg
mode: val
num_classes: 2
transforms:
- type: Normalize
type: Dataset
val_path: data/optic_disc_seg/val_list.txt
optimizer:
momentum: 0.9
type: SGD
weight_decay: 4.0e-05
lr_scheduler:
end_lr: 0
learning_rate: 0.01
power: 0.9
type: PolynomialDecay
loss:
coef:
- 1
- 1
- 1
types:
- type: CrossEntropyLoss
- type: CrossEntropyLoss
- type: CrossEntropyLoss
model:
backbone:
pretrained: https://bj.bcebos.com/paddleseg/dygraph/PP_STDCNet2.tar.gz
type: STDC2
num_classes: 2
type: PPLiteSeg
------------------------------------------------

2024-04-02 18:25:03 [INFO] Set device: cpu
2024-04-02 18:25:03 [INFO] Use the following config to build model
model:
backbone:
pretrained: https://bj.bcebos.com/paddleseg/dygraph/PP_STDCNet2.tar.gz
type: STDC2
num_classes: 2
type: PPLiteSeg
2024-04-02 18:25:03 [INFO] Loading pretrained model from https://bj.bcebos.com/paddleseg/dygraph/PP_STDCNet2.tar.gz
2024-04-02 18:25:04 [INFO] There are 265/265 variables loaded into STDCNet.
2024-04-02 18:25:04 [INFO] Use the following config to build train_dataset
train_dataset:
dataset_root: paddle_pr
mode: train
num_classes: 2
train_path: paddle_pr/train.txt
transforms:
- max_scale_factor: 2.0
min_scale_factor: 0.5
scale_step_size: 0.25
type: ResizeStepScaling
- crop_size:
- 512
- 512
type: RandomPaddingCrop
- type: RandomHorizontalFlip
- brightness_range: 0.5
contrast_range: 0.5
saturation_range: 0.5
type: RandomDistort
- type: Normalize
type: Dataset
2024-04-02 18:25:04 [INFO] Use the following config to build val_dataset
val_dataset:
dataset_root: data/optic_disc_seg
mode: val
num_classes: 2
transforms:
- type: Normalize
type: Dataset
val_path: data/optic_disc_seg/val_list.txt
2024-04-02 18:25:04 [INFO] If the type is SGD and momentum in optimizer config, the type is changed to Momentum.
2024-04-02 18:25:04 [INFO] Use the following config to build optimizer
optimizer:
momentum: 0.9
type: Momentum
weight_decay: 4.0e-05
2024-04-02 18:25:04 [INFO] Use the following config to build loss
loss:
coef:
- 1
- 1
- 1
types:
- type: CrossEntropyLoss
- type: CrossEntropyLoss
- type: CrossEntropyLoss
/Users/xuanleung/workspace/PaddleSeg/venv/lib/python3.12/site-packages/paddle/nn/layer/norm.py:824: UserWarning: When training, we now always track global mean and variance.
warnings.warn(
2024-04-02 18:26:25 [INFO] [TRAIN] epoch: 10, iter: 10/1000, loss: 0.8485, lr: 0.009919, batch_cost: 8.0518, reader_cost: 1.45770, ips: 0.4968 samples/sec | ETA 02:12:51
.......
2024-04-02 19:26:58 [INFO] [TRAIN] epoch: 500, iter: 500/1000, loss: 0.1028, lr: 0.005369, batch_cost: 7.3678, reader_cost: 1.30898, ips: 0.5429 samples/sec | ETA 01:01:23
2024-04-02 19:26:58 [INFO] Start evaluating (total_samples: 76, total_iters: 76)...
76/76 [==============================] - 39s 510ms/step - batch_cost: 0.5100 - reader cost: 8.5753e-04
2024-04-02 19:27:37 [INFO] [EVAL] #Images: 76 mIoU: 0.4908 Acc: 0.9816 Kappa: 0.0000 Dice: 0.4954
2024-04-02 19:27:37 [INFO] [EVAL] Class IoU:
[0.9816 0. ]
2024-04-02 19:27:37 [INFO] [EVAL] Class Precision:
[0.9816 0. ]
2024-04-02 19:27:37 [INFO] [EVAL] Class Recall:
[1. 0.]
2024-04-02 19:27:38 [INFO] [EVAL] The model with the best validation mIoU (0.4908) was saved at iter 500.
2024-04-02 19:28:51 [INFO] [TRAIN] epoch: 510, iter: 510/1000, loss: 0.2417, lr: 0.005272, batch_cost: 7.3453, reader_cost: 1.31461, ips: 0.5446 samples/sec | ETA 00:59:59
.....
2024-04-02 20:29:09 [INFO] [TRAIN] epoch: 1000, iter: 1000/1000, loss: 0.1945, lr: 0.000020, batch_cost: 7.4149, reader_cost: 1.36141, ips: 0.5395 samples/sec | ETA 00:00:00
2024-04-02 20:29:09 [INFO] Start evaluating (total_samples: 76, total_iters: 76)...
76/76 [==============================] - 38s 503ms/step - batch_cost: 0.5028 - reader cost: 7.8113e-04
2024-04-02 20:29:47 [INFO] [EVAL] #Images: 76 mIoU: 0.4908 Acc: 0.9816 Kappa: 0.0000 Dice: 0.4954
2024-04-02 20:29:47 [INFO] [EVAL] Class IoU:
[0.9816 0. ]
2024-04-02 20:29:47 [INFO] [EVAL] Class Precision:
[0.9816 0. ]
2024-04-02 20:29:47 [INFO] [EVAL] Class Recall:
[1. 0.]
2024-04-02 20:29:48 [INFO] [EVAL] The model with the best validation mIoU (0.4908) was saved at iter 500.
<class 'paddle.nn.layer.conv.Conv2D'>'s flops has been counted
<class 'paddle.nn.layer.norm.BatchNorm2D'>'s flops has been counted
<class 'paddle.nn.layer.activation.ReLU'>'s flops has been counted
<class 'paddle.nn.layer.pooling.AvgPool2D'>'s flops has been counted
<class 'paddle.nn.layer.pooling.AdaptiveAvgPool2D'>'s flops has been counted
Total Flops: 9643807616 Total Params: 12251410

Process finished with exit code 0

生成的文件:

1
2
3
4
5
6
7
8
9
output
├── iter_500 #表示在500步保存一次模型
├── model.pdparams #模型参数
└── model.pdopt #训练阶段的优化器参数
├── iter_1000 #表示在1000步保存一次模型
├── model.pdparams #模型参数
└── model.pdopt #训练阶段的优化器参数
└── best_model #精度最高的模型权重
└── model.pdparams

模型评估

拷贝paddle_pr数据集目录到项目根目录,在项目下面的目录configs/quick_start/pp_liteseg_optic_disc_512x512_1k.yml,修改这两个路径配置:

1
2
3
val_dataset:
dataset_root: paddle_pr #数据集路径
train_path: paddle_pr/val.txt #数据集中用于训练的标识文件

使用pyCharm添加一个python run config的启动配置,script设置为tools/val.py,script parameters设置为--config configs/quick_start/pp_liteseg_optic_disc_512x512_1k.yml --model_path output/iter_1000/model.pdparams,然后点击右上角的run

输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/Users/x/workspace/PaddleSeg/venv/bin/python tools/val.py --config configs/quick_start/pp_liteseg_optic_disc_512x512_1k.yml --model_path output/iter_1000/model.pdparams 
2024-04-03 11:18:24 [WARNING] Add the `num_classes` in train_dataset and val_dataset config to model config. We suggest you manually set `num_classes` in model config.
....
2024-04-03 11:18:25 [INFO] Start evaluating (total_samples: 2, total_iters: 2)...
2/2 [==============================] - 40s 20s/step - batch_cost: 20.0147 - reader cost: 0.3020
#数据解释
#Acc(准确率):指类别预测正确的像素占总像素的比例,准确率越高模型质量越好。
#mIoU(平均交并比):对每个类别数据集单独进行推理计算,计算出的预测区域和实际区域交集除以预测区域和实际区域的并集,然后将所有类别得到的结果取平均。
#Kappa系数:一个用于一致性检验的指标,可以用于衡量分类的效果。kappa系数的计算是基于混淆矩阵的,取值为-1到1之间,通常大于0。其公式如下所示,P0P_0P0为分类器的准确率,PeP_eP**e为随机分类器的准确率。Kappa系数越高模型质量越好。
2024-04-03 11:19:05 [INFO] [EVAL] #Images: 2 mIoU: 0.9224 Acc: 0.9958 Kappa: 0.9162 Dice: 0.9581
2024-04-03 11:19:05 [INFO] [EVAL] Class IoU:
[0.9957 0.8491]
2024-04-03 11:19:05 [INFO] [EVAL] Class Precision:
[0.9972 0.94 ]
2024-04-03 11:19:05 [INFO] [EVAL] Class Recall:
[0.9984 0.8977]
Process finished with exit code 0

模型预测

使用pyCharm添加一个python run config的启动配置,script设置为tools/predict.py,script parameters设置为--config configs/quick_start/pp_liteseg_optic_disc_512x512_1k.yml --model_path output/iter_1000/model.pdparams --image_path paddle_pr/images/image1.jpg --save_dir output/result,然后点击右上角的run

输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
/Users/x/workspace/PaddleSeg/venv/bin/python tools/predict.py --config configs/quick_start/pp_liteseg_optic_disc_512x512_1k.yml --model_path output/iter_1000/model.pdparams --image_path paddle_pr/images/image1.jpg --save_dir output/result 
2024-04-03 11:45:21 [WARNING] Add the `num_classes` in train_dataset and val_dataset config to model config. We suggest you manually set `num_classes` in model config.
2024-04-03 11:45:23 [INFO] Loading pretrained model from https://bj.bcebos.com/paddleseg/dygraph/PP_STDCNet2.tar.gz
2024-04-03 11:45:23 [INFO] There are 265/265 variables loaded into STDCNet.
2024-04-03 11:45:23 [INFO] The number of images: 1
2024-04-03 11:45:23 [INFO] Loading pretrained model from output/iter_1000/model.pdparams
2024-04-03 11:45:23 [INFO] There are 370/370 variables loaded into PPLiteSeg.
2024-04-03 11:45:23 [INFO] Start to predict...
1/1 [==============================] - 21s 21s/step
2024-04-03 11:45:44 [INFO] Predicted images are saved in output/result/added_prediction and output/result/pseudo_color_prediction .

Process finished with exit code 0

生成的文件:

1
2
3
4
5
6
7
8
9
10
11
output/result
|
|--added_prediction #叠加效果图
| |--image1.jpg
| |--image2.jpg
| |--...
|
|--pseudo_color_prediction #预测mask
| |--image1.jpg
| |--image2.jpg
| |--...

导出模型

使用pyCharm添加一个python run config的启动配置,script设置为tools/export.py,script parameters设置为--config configs/quick_start/pp_liteseg_optic_disc_512x512_1k.yml --model_path output/best_model/model.pdparams --save_dir output/inference_model,然后点击右上角的run

输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/Users/x/workspace/PaddleSeg/venv/bin/python tools/export.py --config configs/quick_start/pp_liteseg_optic_disc_512x512_1k.yml --model_path output/best_model/model.pdparams --save_dir output/inference_model 
2024-04-03 12:07:23 [WARNING] Add the `num_classes` in train_dataset and val_dataset config to model config. We suggest you manually set `num_classes` in model config.
2024-04-03 12:07:23 [INFO]
2024-04-03 12:07:24 [INFO] Loaded trained params successfully.
/Users/xuanleung/workspace/PaddleSeg/venv/lib/python3.12/site-packages/paddle/jit/api.py:310: UserWarning: full_graph=False is not supported in Python 3.12+. Set full_graph=True automatically
warnings.warn(
I0403 12:07:27.211655 1341450176 program_interpreter.cc:212] New Executor is Running.
2024-04-03 12:07:27 [INFO]
---------------Deploy Information---------------
Deploy:
input_shape:
- -1
- 3
- -1
- -1
model: model.pdmodel
output_dtype: int32
output_op: argmax
params: model.pdiparams
transforms:
- type: Normalize

2024-04-03 12:07:27 [INFO] The inference model is saved in output/inference_model
Process finished with exit code 0

生成的文件:

1
2
3
4
5
output/inference_model
├── deploy.yaml # 部署相关的配置文件,主要说明数据预处理方式等信息
├── model.pdmodel # 预测模型的拓扑结构文件
├── model.pdiparams # 预测模型的权重文件
└── model.pdiparams.info # 参数额外信息,一般无需关注

部署模型(Paddle Inference部署python)

使用pyCharm添加一个python run config的启动配置,script设置为deploy/python/infer.py,script parameters设置为-config output/inference_model/deploy.yaml --image_path /Users/xuanleung/Downloads/test.jpg --device cpu,然后点击右上角的run

生产的文件:output/test.png

MacOS安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#检查环境
#要求 pip 版本为 20.2.2 或更高版本
➜ ~ python3 -m pip --version
pip 24.0 from /usr/local/lib/python3.12/site-packages/pip (python 3.12)
#需要确认 python 的版本是否满足要求3.8/3.9/3.10/3.11/3.12
➜ ~ python3 --version
Python 3.12.2
#需要确认 Python 和 pip 是 64bit,并且处理器架构是 x86_64
➜ ~ python3 -c "import platform;print(platform.architecture()[0]);print(platform.machine())"
64bit
x86_64
#检查是否支持avx,结果输出avx即表示支持
➜ ~ sysctl machdep.cpu.features | grep -i avx
machdep.cpu.features: FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 CLFSH DS ACPI MMX FXSR SSE SSE2 SS HTT TM PBE SSE3 PCLMULQDQ DTES64 MON DSCPL VMX SMX EST TM2 SSSE3 FMA CX16 TPR PDCM SSE4.1 SSE4.2 x2APIC MOVBE POPCNT AES PCID XSAVE OSXSAVE SEGLIM64 TSCTMR AVX1.0 RDRAND F16C
➜ ~ sysctl machdep.cpu.leaf7_features | grep -i avx
machdep.cpu.leaf7_features: RDWRFSGS TSC_THREAD_OFFSET SGX BMI1 HLE AVX2 SMEP BMI2 ERMS INVPCID RTM FPU_CSDS MPX RDSEED ADX SMAP CLFSOPT IPT MDCLEAR TSXFA IBRS STIBP L1DF ACAPMSR SSBD

#安装unrar,官方命令brew install unrar,unrar找不到,新安装方法
➜ ~ brew install rar
==> Downloading https://www.rarlab.com/rar/rarmacos-x64-700.tar.gz
######################################################################### 100.0%
==> Installing Cask rar
==> Moving Generic Artifact 'default.sfx' to '/usr/local/lib/default.sfx'
==> Moving Generic Artifact 'rarfiles.lst' to '/usr/local/etc/rarfiles.lst'
==> Linking Binary 'rar' to '/usr/local/bin/rar'
==> Linking Binary 'unrar' to '/usr/local/bin/unrar'
🍺 rar was successfully installed!


#创建个虚拟环境目录
mkdir pytho work_python3
#Python 3创建虚拟环境
➜ paddleseg_python3 python3 -m venv .
#查看当前目录,可以发现有几个文件夹和一个pyvenv.cfg文件:
➜ paddleseg_python3 ls
bin include lib pyvenv.cfg
#继续进入bin目录
➜ paddleseg_python3 cd bin
#激活该venv环境
➜ bin source activate
#查看当前目录,里面有python3、pip3等可执行文件,实际上是链接到Python系统目录的软链接。
(paddleseg_python3) ➜ bin ls
Activate.ps1 activate.csh pip pip3.12 python3
activate activate.fish pip3 python python3.12
#下面正常安装paddlepaddle
(paddleseg_python3) ➜ bin python3 -m pip install paddlepaddle==2.6.0 -i https://mirror.baidu.com/pypi/simple
#安装依赖
(paddleseg_python3) ➜ bin pip install -U pip setuptools
#验证安装
(paddleseg_python3) ➜ bin python3
Python 3.12.2 (main, Feb 6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import paddle
>>> paddle.utils.run_check()
Running verify PaddlePaddle program ...
I0326 15:14:46.340384 1206859520 program_interpreter.cc:212] New Executor is Running.
I0326 15:14:46.356616 1206859520 interpreter_util.cc:624] Standalone Executor is Used.
PaddlePaddle works well on 1 CPU.
PaddlePaddle is installed successfully! Let's start deep learning with PaddlePaddle now.
>>> print(paddle.__version__)
2.6.0

#安装PaddleSeg https://github.com/PaddlePaddle/PaddleSeg/blob/release/2.9/docs/install_cn.md#22-%E5%AE%89%E8%A3%85paddleseg 失败

## 回退所有安装
(paddleseg_python3) ➜ PaddleSeg git:(release/2.6) deactivate
➜ PaddleSeg git:(release/2.6)

macOS docker 安装

1
2
3
4
5
6
7
mkdir paddle
cd paddle
#-v $PWD:/paddle:指定将当前路径(PWD 变量会展开为当前路径的绝对路径)挂载到容器内部的 /home/paddle 目录;
docker run -d -p 80:80 --env USER_PASSWD="123456" --name paddle -it -v $PWD:/home/paddle paddlepaddle/paddle:2.6.0-jupyter
#进入容器
docker exec -it paddle /bin/bash
#访问127.0.0.1 输入用户名/密码 jovyan/123456 进入jovyan

常见问题

  1. 安装python3 -m pip install paddlepaddle==2.6.0 -i https://mirror.baidu.com/pypi/simple提示如下错误

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    ➜  ~ python3 -m pip install paddlepaddle==2.6.0 -i https://mirror.baidu.com/pypi/simple
    error: externally-managed-environment

    × This environment is externally managed
    ╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-brew-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip.

    If you wish to install a non-brew packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

    note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
    hint: See PEP 668 for the detailed specification.

    解决:参考pip(3) install,完美解决 externally-managed-environment

    方案一:添加参数–break-system-packages,这种直接安装到系统,可能会影响系统环境。

    1
    python3 -m pip install paddlepaddle==2.6.0 -i https://mirror.baidu.com/pypi/simple --break-system-packages

    方案二:pipx,安装完成后,无法导入paddle,初步判断无法进入虚拟环境

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    # pipx会为每个安装的应用创建一个独立的虚拟环境,避免不同应用之间的依赖冲突。
    ➜ ~ brew install pipx
    #当你首次安装pipx时,运行pipx ensurepath会自动检查并修改你的环境变量设置(如需要的话),以确保你可以轻松运行pipx安装的任何程序。这个步骤通常只需要执行一次。
    ➜ ~ pipx ensurepath
    #替换官方的安装命令,使用pipx安装
    # 2.9.0找不到
    ➜ ~ pipx install paddlepaddle==2.9.0 -i https://mirror.baidu.com/pypi/simple
    Fatal error from pip prevented installation. Full pip output in file:
    /Users/xuanleung/Library/Logs/pipx/cmd_2024-03-25_15.41.49_pip_errors.log

    Some possibly relevant errors from pip install:
    ERROR: Could not find a version that satisfies the requirement paddlepaddle==2.9.0 (from versions: 2.6.0)
    ERROR: No matching distribution found for paddlepaddle==2.9.0

    Error installing paddlepaddle from spec 'paddlepaddle==2.9.0'.
    # 换成安装2.6.0
    ➜ ~ pipx install paddlepaddle==2.6.0 -i https://mirror.baidu.com/pypi/simple
    installed package paddlepaddle 2.6.0, installed using Python 3.12.2
    These apps are now globally available
    - fleetrun
    - paddle
    ⚠️ Note: '/Users/xuanleung/.local/bin' is not on your PATH environment
    variable. These apps will not be globally accessible until your PATH is
    updated. Run `pipx ensurepath` to automatically add it, or manually modify
    your PATH in your shell's config file (i.e. ~/.bashrc).
    done! ✨ 🌟 ✨
    # 再次执行更新环境变量
    ➜ ~ pipx ensurepath

    【采用】方案三:使用venv

  2. 执行import paddle提示Python 3: ImportError “No Module named Setuptools”

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    >>> import paddle
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/Users/xx/workspace/paddleseg_python3/lib/python3.12/site-packages/paddle/__init__.py", line 28, in <module>
    from .base import core # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^
    File "/Users/xx/workspace/paddleseg_python3/lib/python3.12/site-packages/paddle/base/__init__.py", line 77, in <module>
    from . import dataset
    File "/Users/xx/workspace/paddleseg_python3/lib/python3.12/site-packages/paddle/base/dataset.py", line 20, in <module>
    from ..utils import deprecated
    File "/Users/xx/workspace/paddleseg_python3/lib/python3.12/site-packages/paddle/utils/__init__.py", line 16, in <module>
    from . import ( # noqa: F401
    File "/Users/xx/workspace/paddleseg_python3/lib/python3.12/site-packages/paddle/utils/cpp_extension/__init__.py", line 15, in <module>
    from .cpp_extension import (
    File "/Users/xx/workspace/paddleseg_python3/lib/python3.12/site-packages/paddle/utils/cpp_extension/cpp_extension.py", line 21, in <module>
    import setuptools
    ModuleNotFoundError: No module named 'setuptools'
    >>> paddle.utils.run_check()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'paddle' is not defined

    解决:执行pip install -U pip setuptools

  3. 执行

    1
    2
    3
    4
    5
    6
    python tools/train.py \
    --config configs/quick_start/pp_liteseg_optic_disc_512x512_1k.yml \
    --do_eval \
    --use_vdl \
    --save_interval 500 \
    --save_dir output

    提示如下错误:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    (venv) ➜  PaddleSeg git:(release/2.9) ✗ python tools/train.py \
    --config configs/quick_start/pp_liteseg_optic_disc_512x512_1k.yml \
    --save_interval 500 \
    --do_eval \
    --use_vdl \
    --save_dir output
    Traceback (most recent call last):
    File "/Users/x/workspace/PaddleSeg/tools/train.py", line 213, in <module>
    main(args)
    File "/Users/x/workspace/PaddleSeg/tools/train.py", line 145, in main
    cfg = Config(
    ^^^^^^^
    TypeError: Config.__init__() got an unexpected keyword argument 'to_static_training'

    原因:tools/train.py文件里面缺少参数to_static_training
    解决:切换到release/2.8.1分支,再次执行命令

  4. 执行

    1
    2
    3
    4
    5
    6
    python tools/train.py \
    --config configs/quick_start/pp_liteseg_optic_disc_512x512_1k.yml \
    --do_eval \
    --use_vdl \
    --save_interval 500 \
    --save_dir output

    提示如下错误:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
      - type: CrossEntropyLoss
    - type: CrossEntropyLoss
    - type: CrossEntropyLoss
    /Users/xuanleung/workspace/PaddleSeg/venv/lib/python3.12/site-packages/paddle/nn/layer/norm.py:824: UserWarning: When training, we now always track global mean and variance.
    warnings.warn(
    Traceback (most recent call last):
    File "/Users/x/workspace/PaddleSeg/tools/train.py", line 195, in <module>
    main(args)
    File "/Users/x/workspace/PaddleSeg/tools/train.py", line 170, in main
    train(
    File "/Users/x/workspace/PaddleSeg/venv/lib/python3.12/site-packages/paddleseg/core/train.py", line 273, in train
    avg_loss_list = [l[0] / log_iters for l in avg_loss_list]
    ~^^^
    IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed

    解决:使用pyCharm添加一个python run config的启动配置,script设置为tools/train.py,script parameters设置为--config configs/quick_start/pp_liteseg_optic_disc_512x512_1k.yml --save_interval 500 --do_eval --use_vdl --save_dir output,然后点击右上角的run

tesseract

识别率较低(已卸载)

安装

mac安装执行brew install tesseract即可,安装过程可能有点慢,要下载很久

安装完成后,执行brew info tesseract,可以查看tesseract相关信息

使用

1
2
3
4
5
6
tesseract <图片名字> <输出的文本文件名(自带.txt)> [-l 语言类型] [--oem ocrenginemode] [--psm pagesegmode] [configfiles...]
#举例
tesseract test.jpg stout -l chi_sim+equ --psm 6
ls
|-test.jpg
|-stout.txt

PaddleOCR

识别率高

安装

  1. pyCharm拉取github PaddlePaddle/PaddleOCR仓库的代码,并切换到分支release/2.7

  2. 在pyCharm终端执行下面命令,注意需要venv加载成功

    1
    2
    3
    4
    5
    6
    7
    #环境所需依赖
    (venv) ➜ PaddleOCR git:(release/2.7) pip install paddlepaddle
    (venv) ➜ PaddleOCR git:(release/2.7) pip install -U pip setuptools
    #安装
    (venv) ➜ PaddleOCR git:(release/2.7) pip install "paddleocr>=2.0.1"
    #使用
    (venv) ➜ PaddleOCR git:(release/2.7) paddleocr --image_dir /Users/xuanleung/Downloads/ppocr_img/imgs/11.jpg --use_gpu false

实战场景

有一大批照片,要从照片中获取指定信息作为图片的文件名,并根据文件名生产csv表格

  1. 提前训练好模型参考:PaddleSeg基于机器学习的图像分割

  2. 采用paddleSeg(模型预测)进行关键位置的识别

    1
    2
    #pycharm 运行 
    python tools/predict.py --config configs/quick_start/pp_liteseg_optic_disc_512x512_1k.yml --model_path output/iter_1000/model.pdparams --image_path /Users/x/Downloads/桩顶标高 --save_dir /Users/x/Downloads/桩顶标高out --custom_color 0 0 0 255 255 255
  3. 裁剪图片

    1
    2
    3
    #pycharm 运行 
    python tools/crop.py --input_mask_dir /Users/x/Downloads/桩顶标高out/pseudo_color_prediction --input_orig_dir /Users/x/Downloads/桩顶标高
    Creating crop directory: /Users/x/Downloads/桩顶标高/crop_dir
  4. 执行ocr识别,会产生一个output目录,里面会产生和图片对应文件名的txt文本,内容为ocr识别的内容

    1
    2
    3
    #建议优化脚本里面的识别方式,替换成PaddleOCR识别率提高非常的多
    #在图片所在目录
    bash ocr.sh
  5. 执行关键文字提取脚本,将关键字在文本文件里面保留

    1
    2
    #在图片所在的output目录里面
    bash format.sh
  6. 替换文件名,根据文本里面的内容作为图片的文件名

    1
    2
    #在图片所在目录
    bash rename.sh
  7. 根据文件名生产csv表格

    1
    2
    #在图片所在目录
    bash csv.sh

文件附录

  • ocr.sh:批量将当前脚本所在目录的图片转换成文本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #建议优化脚本里面的识别方式,替换成PaddleOCR识别率提高非常的多
    #!/bin/bash
    # 当前脚本所在目录
    script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    # 输入文件夹路径为当前目录
    input_folder="$script_dir"
    # 输出文件夹路径为当前目录
    output_folder="$script_dir/output"
    # 创建输出文件夹
    mkdir -p "$output_folder"
    # 遍历输入文件夹中的所有图片文件
    for image_file in "$input_folder"/*.jpg "$input_folder"/*.png "$input_folder"/*.jpeg; do
    # 提取文件名(不含路径和扩展名)
    filename=$(basename "$image_file" | cut -f 1 -d '.')
    # 构建输出文本文件的路径
    output_text_path="$output_folder/$filename"
    # 执行 Tesseract 命令
    tesseract "$image_file" "$output_text_path" -l chi_sim+equ --psm 6
    echo "Processed $image_file"
    done
  • format.sh:批量将当前脚本所在目录的文本只保留关键信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #!/bin/bash
    # 获取当前脚本所在目录
    script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    # 遍历当前目录下的所有文本文件
    for file in "$script_dir"/*.txt; do
    # 检查文件是否存在
    if [ -f "$file" ]; then
    # 使用grep命令查找匹配行并提取信息
    transaction=$(grep -oE '体验[0-9]+' "$file" | cut -d'验' -f2)
    elevation=$(grep -oE '[0-9]{3}\.[0-9]{3}' "$file" | cut -d':' -f2)
    # 输出提取的信息
    echo "交易编号: $transaction"
    echo "高程: $elevation"
    # 将提取的信息写入原文件
    echo "$transaction-$elevation" > "$file"
    fi
    done
  • rename.sh:批量重命名脚本所在目录的图片,文件名根据output目录对应文件名的文本内容进行命名

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    #!/bin/bash
    # 获取当前脚本所在目录
    script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    # 图片文件夹路径
    image_folder="$script_dir"
    # 遍历图片文件夹中的所有文本文件
    for text_file in "$image_folder"/output/*.txt; do
    # 检查文件是否存在
    if [ -f "$text_file" ]; then
    # 获取文本文件名(不含路径和扩展名)
    base_name=$(basename "$text_file" .txt)
    # 获取图片文件名
    image_file="$image_folder/$base_name.jpg"
    # 检查图片文件是否存在
    if [ -f "$image_file" ]; then
    # 获取文本文件的内容作为新的图片文件名
    new_name=$(cat "$text_file")
    # 检查是否有同名文件,如果有就给新名称添加序号
    new_image_file="$image_folder/$new_name.jpg"
    counter=1
    while [ -f "$new_image_file" ]; do
    new_image_file="$image_folder/${new_name}_${counter}.jpg"
    ((counter++))
    done
    # 重命名图片文件
    mv "$image_file" "$new_image_file"
    echo "Renamed '$image_file' to '$new_image_file'"
    fi
    fi
    done
  • csv.sh:根据文件名生产表格,表格数据有两列,文件名以-进行区分列

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #!/bin/bash
    # 获取当前脚本所在目录
    script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    # 图片文件夹路径
    folder="$script_dir"
    # CSV 文件路径
    csv_file="$script_dir/output.csv"
    # 创建 CSV 文件,并写入标题行
    echo "Column1,Column2" > "$csv_file"
    # 遍历文件夹中的所有图片文件
    for file in "$folder"/*.jpg; do
    # 提取文件名中的两个部分,使用 '-' 作为分隔符
    part1=$(basename "$file" | cut -d'-' -f1)
    part2=$(basename "$file" | cut -d'-' -f2 | sed 's/\.jpg//')
    # 将两个部分写入 CSV 文件
    echo "$part1,$part2" >> "$csv_file"
    done
  • crop.py:根据模型预测的数据,进行图片裁剪

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    # 参数:input_mask_dir 掩码图片目录,黑白图片
    # 参数:input_orig_dir 原始图片目录,文件名要和掩码图片文件名一致,后缀可以不一样
    # 输出:根据掩码图片裁剪原始图片,得到原图的裁剪图片
    import argparse
    import glob
    import os
    import os.path as osp
    import cv2

    from pathlib import Path
    from paddleseg.utils import logger

    def parse_args():
    parser = argparse.ArgumentParser(description='image crop.')
    parser.add_argument('--input_mask_dir', help='input annotated directory',type=str)
    parser.add_argument('--input_orig_dir', help='input annotated directory',type=str)
    return parser.parse_args()

    def main(args):
    output_dir = osp.join(args.input_orig_dir, 'crop_dir')
    if not osp.exists(output_dir):
    os.makedirs(output_dir)
    print('Creating crop directory:', output_dir) # 在原图目录创建裁剪后输出目录
    for mask_img in glob.glob(osp.join(args.input_mask_dir, '*.png')): # 遍历input_mask_dir目录所有png的图片
    print('read mask img from:', mask_img)
    mask_img_name = Path(mask_img).stem # 获取文件名
    print('read orig img name:', args.input_orig_dir+mask_img_name+".jpg")
    # 读取原始图像和关键部位掩码图像
    original_image = cv2.imread(args.input_orig_dir+"/"+mask_img_name+".jpg")
    mask_image = cv2.imread(mask_img, cv2.IMREAD_GRAYSCALE) # 假设是单通道的灰度图像
    # 查找掩码图像中的边界
    contours, _ = cv2.findContours(mask_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    # 找到最大的边界
    max_contour = max(contours, key=cv2.contourArea)
    # 获取最大边界的边界框
    x, y, w, h = cv2.boundingRect(max_contour)
    # 从原始图像中裁剪关键部位
    key_part = original_image[y:y + h, x:x + w]
    # 保存裁剪出的关键部位
    cv2.imwrite(output_dir+"/"+mask_img_name+".jpg", key_part)
    logger.info(f'crop img is saved in {output_dir}')

    if __name__ == '__main__':
    args = parse_args()
    main(args)