QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3429|回复: 0
打印 上一主题 下一主题

深度学习和目标检测系列教程 8-300:目标检测常见的标注工具LabelImg和将xml文件提...

[复制链接]
字体大小: 正常 放大
杨利霞        

5273

主题

82

听众

17万

积分

  • TA的每日心情
    开心
    2021-8-11 17:59
  • 签到天数: 17 天

    [LV.4]偶尔看看III

    网络挑战赛参赛者

    网络挑战赛参赛者

    自我介绍
    本人女,毕业于内蒙古科技大学,担任文职专业,毕业专业英语。

    群组2018美赛大象算法课程

    群组2018美赛护航培训课程

    群组2019年 数学中国站长建

    群组2019年数据分析师课程

    群组2018年大象老师国赛优

    跳转到指定楼层
    1#
    发表于 2021-7-14 15:18 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta

    : R8 V+ ]7 Q* Y+ B) k! f; t深度学习和目标检测系列教程 8-300:目标检测常见的标注工具LabelImg和将xml文件提取图像信息
    ( o( C* C* K; q0 A! v0 `7 \1 O图像标注主要用于创建数据集进行图片的标注。本篇博客将推荐一款非常实用的图片标注工具LabelImg,重点介绍其安装使用过程。如果想简单点,请直接下载打包版(下载地址见结尾),无需编译,直接打开即可!
    & T- ]: Q3 k  a' c5 r1 k
    % |8 G/ J" v( `: ~& ^1 b

    % ]3 I( y/ L5 t感谢原作者对Github的贡献,博主发现软件已经更新,可以关注最新版本。这个工具是一个用 Python 和 Qt 编写的完整的图形界面。最有意思的是,它的标注信息可以直接转换成XML文件,这和PASCAL VOC和ImageNet使用的XML是一样的。
    $ f7 h8 S' S- K8 n
    7 ^1 X  V& i/ T7 u& e- a0 y8 m# @

    4 `/ Y- p5 @. a1 t% k% w* ?附注。作者在5月份更新了代码,现在最新版本号是1.3.0,博主亲测,源码在Windows 10和Ubuntu 16.04上正常运行。0 j9 S8 l! z* O& f3 M2 e+ m: N! y$ H) ]
      q6 j9 _2 D4 s$ Q/ |

    $ q9 T  ]# Q/ f) @1 l- \3 C3 \0 f具体的安装查看Github教程:https://github.com/wkentaro/labelme/#installation
    $ L; S4 E  P  Z$ F8 u) H% L: K
    ( @4 g# S. n1 B4 t+ H
    2 i5 R' x( z$ y, m4 D
    在原作者的github下载源码:https://github.com/tzutalin/labelImg' A( C' t4 D9 J* }) s& `$ D3 h8 l
    。解压名为labelImg-master的文件夹,进入当前目录的命令行窗口,输入如下语句依次打开软件。- w3 h" b3 R( _# P7 Y/ a% w$ \

    - y3 V7 E- \2 P) n# D0 F7 y

    $ _" b# h9 v: h7 j3 ?$ C; Gpython labelImg.py$ @2 C4 n* c+ a
    1
    , g; p" Q! [9 `+ h" s! G
    5 m, ~% b( |4 J" ^- n
    4 P2 k  M, g3 V; _0 Z3 S5 R
    - L8 f' P8 {7 e, D) w$ c
    ; s. \' ]. _) ?
    具体使用( L) ^& M7 h* @+ G
    修改默认的XML文件保存位置,使用快捷键“Ctrl+R”,更改为自定义位置,这里的路径一定不能包含中文,否则不会保存。
    " f' r( h$ S. k" g
    7 W* r* _* H3 L0 U
    8 u7 Y- ?- d4 l* r8 Y8 m. y$ s
    使用notepad++打开源文件夹中的data/predefined_classes.txt,修改默认分类,如person、car、motorcycle这三个分类。% R& ?+ g- `' \
    - m+ J, P0 \1 g9 Y. S

    " W$ N) {  ~* K; t: g“打开目录”打开图片文件夹,选择第一张图片开始标注,用“创建矩形框”或“Ctrl+N”启动框,点击结束框,双击选择类别。完成一张图片点击“保存”保存后,XML文件已经保存到本地了。单击“下一张图片”转到下一张图片。
    6 [; T8 v* W  k5 n4 h3 u
    & {  P: f  v. c* V

    # E6 E+ a' j# m. i7 n; m贴标过程可以随时返回修改,保存的文件会覆盖上一个。/ ]5 X& E# V) A/ S' I# c9 b

    1 r4 A- B' m, [' Q: {

    4 z. p( Q/ T- o+ h/ A9 ]完成注解后,打开XML文件,发现和PASCAL VOC格式一样。: P" i* g) a# B7 v2 L) C. T: M) \( }0 i
    2 I: q5 U  N/ n: Z
    " S* d/ r0 o; e$ x; N: [* T
    将xml文件提取图像信息) V8 l- f7 q$ h, T/ g/ F
    下面列举如何将xml文件提取图像信息,图片保存到image文件夹,xml保存标注内容。图片和标注的文件名字一样的。5 S7 T# D: f  Y& J! [
    6 `6 E! ?, u) u* n% L2 I. H

    * \3 ^/ o9 m0 t- f0 H3 O. i8 a3 f0 F2 Y( X( ?' ~0 _; T! ~

    ' J& P. Y7 b; x% o- H下面是images图片中的一个。
      h/ {6 T6 a. P2 k( h; F8 P# `3 J$ n7 d  [% d0 Q
    8 U6 Y% o- b3 e( a' K- W
    下面是对应的xml文件。
    * g1 E, ]3 U; F" J; O, M
    $ H( R3 C5 ^2 l5 ^/ i: u& c
    " V3 Q6 |5 |+ ^, f4 H! s/ K
    <annotation>
    5 I# B0 r/ M7 u: a$ F* Z        <folder>train</folder>9 [" B$ O1 |- ]$ r4 e. v3 p
            <filename>apple_30.jpg</filename>! R* n$ A3 |7 w6 x
            <path>C:\tensorflow1\models\research\object_detection\images\train\apple_30.jpg</path>. {# ^) v- u& g+ r8 S) z& M/ @7 O
            <source>
    % y" o3 H+ [; \7 S9 U                <database>Unknown</database>
    ; C% T8 L" `3 W/ F; D' n( z        </source>( [! t* y5 ~+ v0 A, p% v" {
            <size>
    0 s! j. f+ T7 v6 r6 D                <width>800</width>
    4 b  e( a: |2 \2 o- a                <height>800</height>
    : D4 z. R0 }4 C/ {5 z9 v5 m5 g. S                <depth>3</depth>6 s# G* C" Y* w; f$ ^. n
            </size>
    ; C8 l" ]! _1 d7 k7 k        <segmented>0</segmented>% [: q, Y% N% o4 Y
            <object>
    9 E1 {! N4 ~. i) R' I8 t' y" v1 `" Q                <name>apple</name>0 _( B. f0 C# J2 r! }+ s
                    <pose>Unspecified</pose>- w9 H3 N9 h( [' [2 W9 o* r" q( K
                    <truncated>0</truncated>: A& H! e, {, i8 Y1 G
                    <difficult>0</difficult>5 O- i0 X8 b! h" m2 `
                    <bndbox>4 ?9 p- [5 R* I0 i: f- P
                            <xmin>254</xmin># [9 a8 }7 V9 v$ E+ R
                            <ymin>163</ymin>& B/ o7 }* G6 |) G4 W" q
                            <xmax>582</xmax>
    2 z7 ?$ _  V+ {4 W  R" A1 s! u' M                        <ymax>487</ymax>
    # \+ Q* W  z9 A; R                </bndbox>
    - r# v$ i0 k0 i+ s+ E& ]  w        </object>- z  g5 T4 F+ A& Y  I( @
            <object>
    ; _9 d) y8 c1 _9 p0 x                <name>apple</name>7 V% C# }4 m. B$ d
                    <pose>Unspecified</pose>
    8 C* n4 }# k. Q, F! E4 y1 Q                <truncated>0</truncated>
    ' Q. C# ~- {0 _; l. \                <difficult>0</difficult>
      x) R: [+ \2 A                <bndbox>
    ; C: `: A3 d3 f) W( I                        <xmin>217</xmin>! I$ b2 g% f& ~! j4 d/ l& G( k) h( J
                            <ymin>448</ymin>' t( N- b+ C. a! b& j: Q
                            <xmax>535</xmax>
    3 Q# s/ w/ b8 M" s$ n7 k! u: V                        <ymax>713</ymax>) c. |# F; v8 s. ?9 R, ]" e
                    </bndbox>
    ) ]6 W: o% b" E# s8 V$ @$ m* t1 H+ C        </object># T) p# O# Y' N5 I6 [
            <object>
    " G" y: F6 A$ _5 Z' V                <name>apple</name>4 L0 M& ]8 f" V/ I' x
                    <pose>Unspecified</pose>
    & [9 x3 A7 c( |- C( H7 R                <truncated>1</truncated>* X( D3 G8 ^- ]
                    <difficult>0</difficult>
    ! Q; A1 ?- X) _! Q6 E' q                <bndbox>3 N4 [( I; r. G$ Y5 i( H3 m
                            <xmin>603</xmin>/ D8 K8 }5 m9 ^
                            <ymin>470</ymin>4 O3 z+ ^( _" D. r0 L* m
                            <xmax>800</xmax>
    8 r" f* {6 j) W                        <ymax>716</ymax>+ c& {7 n$ R: S0 [
                    </bndbox>) o; @8 T" a: R5 P6 Z
            </object>. N; Y( \2 G) w* a) d
            <object>/ x4 P3 u+ Q" F- k9 E* K+ i
                    <name>apple</name>
    0 X* ~0 Z1 k. L6 ]( m                <pose>Unspecified</pose>
    $ W( @2 y; I4 {6 e; G6 F, s                <truncated>0</truncated>+ A! n9 t$ ^8 q6 {
                    <difficult>0</difficult>6 ~  J# Q  R- ?
                    <bndbox>
    6 E7 J8 t& h% K$ u" t9 T8 N( H                        <xmin>468</xmin>+ B7 W* O; x; o* H1 \
                            <ymin>179</ymin>: \+ m3 Z. T) P- y
                            <xmax>727</xmax>
    2 I0 v9 G: ]5 z  |8 R. H, b" U                        <ymax>467</ymax>3 F: R" b9 U2 G) d- }
                    </bndbox>
    1 J: p! ~  S9 [& q, n        </object>  B7 C) f( A& g7 b* D$ |" @: |
            <object>
    4 |9 q3 m  f3 z, V* ?                <name>apple</name>
    ' y; J  _9 n3 y7 \, j                <pose>Unspecified</pose>
    + C: u( h' d+ k                <truncated>1</truncated>. J# i0 I* F) _! x$ X" O  c
                    <difficult>0</difficult>
    8 y5 M8 s' q* \# a                <bndbox>
    8 i6 \; e6 G  n+ B7 L                        <xmin>1</xmin>
    . B6 x4 z3 G0 j" I2 o                        <ymin>63</ymin>
    * }3 E$ n6 I( F! Y, T1 z                        <xmax>308</xmax>* d8 I$ Z1 B3 Y* a: b- z
                            <ymax>414</ymax>
    $ ]5 \3 M2 A! G, ]                </bndbox>! X. ]4 N9 q' n* J6 R
            </object>& c. V* i$ K$ ?1 i
    </annotation>
    ! q5 S8 w! P7 l5 w; f8 u; p15 t# _4 m6 L$ i
    29 B7 D# l+ ~) V" C
    3- n8 N) I3 v: l  ?- ^: t
    4
    ' m! \0 w: }" A* a) I55 U! K# z' C; Z) w
    6
    . m) O* T3 r5 @7 H& h" c/ c1 P# o7
    2 Q- g: `2 A7 w0 ]- N( H( g8' C# r5 t- A3 A% M5 d6 _( p
    9+ x! T9 Q% Y) L) D
    10; l$ h2 e2 _/ n- _) G& I
    116 v; @+ G: y, U) }) z0 Z
    123 h+ u! W8 e3 |& f7 Y0 B! H9 l
    13
    : V0 {; e# O( Q: K; V! v+ M, W14- R' N4 C# ?2 U4 F+ P  r+ v
    15
    - Z8 f" x; t" \# f) J162 h$ _& l+ Q9 u( ?; F& ^9 m
    17
    - R5 {5 p* K6 T  Q8 v( L& g% Z18
    & a" E0 p4 P1 G' e4 x7 ^7 T& @3 M19. b0 ?3 r& ]0 I/ y7 o3 E' z% F
    20
    ; y) R9 V* N+ S5 j$ M" E3 a21# I8 O# r) j. A9 r" p) d7 _
    22- q0 u' H7 X$ \! T) ~
    23+ Q! U% x: x/ j
    24
    5 c' V8 m. D, j1 w1 ]; K' V25; `5 K* U2 K7 v, u; T0 _! T8 g
    26. D4 ^' v3 U6 Z( ^+ _  f
    27( @6 q; b- B' r& Y1 h
    28
    ) R% f) W! x7 k% l( L5 J. o/ `& r29
    : `2 g+ A3 b! ]( Q9 L30# c5 t2 }8 `+ \
    31" \' L* o: E8 L7 n/ P3 B
    322 p7 u: m/ D9 r. c( N# T
    33( @- x3 x! j4 B" W# B4 c. c
    346 p6 X3 [9 s* E/ r% }
    35  v4 X. o0 q% c6 W* g5 O
    36
    4 u* M5 Z8 n$ I1 p371 A8 G' q6 U& i
    38) Z+ b5 H+ q$ t  U& b1 x5 y' h/ I" s
    39
    0 j/ z# k/ w9 Q6 [1 l- S9 e40
    1 l) o2 f4 N: m( F: _: c0 E41
    * P1 q% t& z; U42
      _" P- K. s  d* H) E9 J437 |7 @) B; S: B0 D
    44
    7 ^9 s* T0 ~) `% J, T: d2 Y45: C6 D$ \- a7 u' F5 L
    46/ g1 n8 m( u4 M7 y* L
    47
    ! G9 k8 D7 _- k0 g  h48
    " u2 I* Y$ E# Y( j1 c49
    ' Z7 x, R6 l- n3 [506 {6 M; p# H9 g; i7 E3 w: X9 c
    51% x' O, J6 ^: n1 F
    52! ~7 R% m+ L. @! F3 _
    53
    + w9 g8 X) ^& g% d) \; Z: g54
    ' }/ ~2 l0 s* Z* C9 U+ w) K6 K551 Z/ k5 H& ~+ j; N
    56
    5 M: A' U$ @) p, @, h7 z0 R# p57  J$ I$ A' e9 G/ _
    58
    9 b$ v2 Q" E; W  l6 d; e59) k9 n" V$ h" c+ t
    60
    4 l2 h0 z' t  V+ B4 U" l61) R: {6 P) i, C/ h" ]) n- J5 j! j
    62
    # b( f2 q4 r+ C0 |633 Y/ y: }* h& ^3 Y  x2 a5 A$ b
    64, J1 Q0 d' ~- \3 }. p
    65% u' Z" w! n1 f. t
    66) ]" {5 B6 m8 Y4 D
    67
    # s5 S. V/ I- E2 C683 w) e7 F% m+ v( b+ [- u; n
    698 B: \: O4 v  j' o
    70- Q# [/ T8 }. W% p$ H) p' Z9 q& U
    718 ^' N8 s" k. e8 r
    72
    + i* Z& y/ R6 J; v73
    7 N4 c& T) w# o6 S, w7 Z: n74" y) w' I* {! R6 l: M5 \
    将xml文件提取图像信息,主要使用xml和opencv,基于torch提取,代码比较凌乱。# H* F6 j8 T# Q( T& L! {  N0 h% o: L

    : L" \( d; b. v( @1 T1 c% Q4 C- v  P' n
    2 Q, q  B* z# N; g* b
    import os) u3 J" R8 b/ _3 `  A2 R
    import numpy as np
    2 g8 N  S9 ?) C2 }import cv2
    0 l2 P( q! ^& }' gimport torch& s( m" k9 Z/ s" D3 l
    import matplotlib.patches as patches
    " S( r7 ]* n8 |. y. K/ ]% d% [import albumentations as A
    5 q6 `# Y3 _+ l; Hfrom albumentations.pytorch.transforms import ToTensorV2
    ' [0 C( J, h1 H9 a' A4 Zfrom matplotlib import pyplot as plt
    * i* N4 c5 Z. ?! M! c# N% Vfrom torch.utils.data import Dataset
    1 D  K1 e& }2 D! e8 Ifrom xml.etree import ElementTree as et4 O6 v8 ?: {1 o, N5 d
    from torchvision import transforms as torchtrans
    9 T2 G) q& b$ k3 I: Y$ x1 z6 V
    % K9 ~4 j6 `: L$ R8 M4 I( y
    5 @3 _0 ~4 a8 L) R  R5 T' S
    # defining the files directory and testing directory
    ' V. w9 r6 C* F% {- Utrain_image_dir = 'train/train/image', `0 w$ }8 R( \! m/ ^* a* S
    train_xml_dir = 'train/train/xml'$ q3 g' c" h# e% v
    # test_image_dir = 'test/test/image'
    2 V- X' ~5 K: b1 k, `: T2 @# test_xml_dir = 'test/test/xml'* q2 M" A, B) v. K0 K; @- y( L  P

    5 L' \0 `7 ~& f1 k8 ?. R" T

    % ]: j. x7 Q; i5 {class FruitImagesDataset(Dataset):
    & q6 R+ ~8 j: |, K# @; ]7 U, J$ O6 s  Q* O, ]9 c3 V4 N$ ]& d

    , E# I9 ~+ o3 l8 x    def __init__(self, image_dir, xml_dir, width, height, transforms=None):
    7 F8 m0 |' @5 `) W/ t0 X        self.transforms = transforms
    - I/ c$ N4 z6 r  r7 N: A' T        self.image_dir = image_dir7 X4 k  x/ V1 _
            self.xml_dir = xml_dir% d# v; j  c5 X6 c- r% D! f
            self.height = height
    5 l9 I! U. Q# \        self.width = width' L, g: k: d( @. w7 g0 C

    / I% c) d/ |# x8 Z. E  Q# `8 y
    3 I" q* S# D8 W3 E' H
            # sorting the images for consistency- F/ I! D8 H6 G
            # To get images, the extension of the filename is checked to be jpg
    ( b' b3 O" Z( T8 i        self.imgs = [image for image in os.listdir(self.image_dir)
    5 d' b  u- M4 S; _                     if image[-4:] == '.jpg']
    4 \( s+ m5 ~6 r+ R; ]- y! P# P        self.xmls = [xml for xml in os.listdir(self.xml_dir)' {2 y* b# X7 S$ y5 g
                         if xml[-4:] == '.xml']7 F/ |6 N" U' F# K2 G$ k: h

    $ X& N" q# F4 b  @% I2 i7 ^7 i! h6 o

    : y4 Z- D- m  k) p3 H        # classes: 0 index is reserved for background
    : q' ^7 H+ z' ~% Y3 [7 b9 q        self.classes = ['apple', 'banana', 'orange']
    $ x2 N5 X) F$ l5 t0 o: j- m1 k4 m- K6 R6 P
    1 d- C1 ?) h7 ?+ ?+ M
        def __getitem__(self, idx):3 U7 Q  D" \: ?: l& o" ^
    % w) B" P5 ~9 T0 e3 e! F; A

    " u& j& r) s9 P( W$ k        img_name = self.imgs[idx]7 ^; C- C& @2 q- q/ H! W
            image_path = os.path.join(self.image_dir, img_name)4 \6 [. H$ B1 @! U. e) V, o* O' z

    : U) K* ~8 G  A% R% g, }8 J

    5 H, r2 R# B( X+ k# ?) I        # reading the images and converting them to correct size and color$ F+ ~" q. h7 L4 A! C3 F9 P; j+ d
            img = cv2.imread(image_path)
    : ~0 A7 Q7 S; U8 @        img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32)" B+ n" H+ x( C. L' ~
            img_res = cv2.resize(img_rgb, (self.width, self.height), cv2.INTER_AREA)
    ( r6 P9 [* f; B' Q$ {0 t5 K        # diving by 255( R- G$ g& R) N# l+ X
            img_res /= 255.0
    " \/ b: `4 j. Y) J$ o% p  c$ i; h0 w1 O# ?- @, y2 n; w

    . H2 S% B5 F( e( R% O( E$ L        # annotation file
    6 a2 R' N/ `6 c* a2 P2 a7 N        annot_filename = img_name[:-4] + '.xml'
    ) R2 r" [& i: ^- ^$ M        annot_file_path = os.path.join(self.xml_dir, annot_filename)* M6 c; a, |- H5 f

    4 }: ]. C& \( D5 x: p* R

    6 A: n5 t$ |8 [0 V4 Q: K' f2 t        boxes = []
    2 A% n; c3 h8 o        labels = []
    5 r# l/ T/ S4 |& g! v        tree = et.parse(annot_file_path)# ]4 m- C; r5 C1 l+ l/ w* V
            root = tree.getroot()4 o- T; u% A; L% B; V; l; j
    + }7 K* ^  E  V& w0 e! G" k( \
    6 `+ r. q2 m- `2 d9 M  F) G
            # cv2 image gives size as height x width
    ; S0 ?8 M: X5 H# |9 o/ P, K        wt = img.shape[1]
    3 w# Y( C8 G$ J" [) o1 g        ht = img.shape[0]% \* T2 g) V7 R7 _9 n- N  S7 e

    & U; s: z- t; a- S/ k0 Y

    6 g4 P9 B! O" L# ?        # box coordinates for xml files are extracted and corrected for image size given
    9 h/ D: i  e6 G4 }        for member in root.findall('object'):( }2 c8 Y4 E5 M- ]; A2 s6 e
                labels.append(self.classes.index(member.find('name').text))/ |+ j5 F" D; ]

    $ f% t# p" Y2 p2 u

    8 v( y& N! `! {4 n2 ~- v" {% h            # bounding box
    ( B  v4 o+ T9 T& L3 b            xmin = int(member.find('bndbox').find('xmin').text)" C3 I7 a& K4 C  M* U* P
                xmax = int(member.find('bndbox').find('xmax').text)8 D8 Z& [, |5 V& T. h' b

    ; i+ \/ [4 q  K7 M9 l
    ( |( I# i5 U% i9 x, p- j
                ymin = int(member.find('bndbox').find('ymin').text)6 V8 u1 r1 f( G4 ?
                ymax = int(member.find('bndbox').find('ymax').text)" `1 d0 \! h" [8 a# ~& T

    / p; n* S: {  g

    ; Y& f6 b5 Y+ |$ H3 ~6 y. O/ U            xmin_corr = (xmin / wt) * self.width; b& Q& _, p9 a7 I
                xmax_corr = (xmax / wt) * self.width
    / s7 c: W1 T9 j: L6 p            ymin_corr = (ymin / ht) * self.height% ]: I$ w# d: [
                ymax_corr = (ymax / ht) * self.height
    2 X2 C+ B+ z% v: k2 v1 G4 c$ x            boxes.append([xmin_corr, ymin_corr, xmax_corr, ymax_corr])
    * [' M+ x; y% I9 S2 l9 v
    , z0 C: T% d5 @, U
    ( n, t- ~  Y. b9 p" g
            # convert boxes into a torch.Tensor
    . e1 M: N4 T* V; f/ u        boxes = torch.as_tensor(boxes, dtype=torch.float32)
    1 Q( L9 c) |1 T1 ~; L
    # |( x) d4 X3 D/ H7 t4 |

    $ ?  S6 @9 R2 r# s9 u        # getting the areas of the boxes7 r# l" l# a% t0 o$ Z
            area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
    8 T" f/ [( C: E# ^1 W' B
    4 W  D7 M- b0 k. o

    * {! G5 J# Q0 @        # suppose all instances are not crowd
    6 M7 x0 s$ a" w( f8 U        iscrowd = torch.zeros((boxes.shape[0],), dtype=torch.int64)# l6 h* t% L  \7 I  E
    9 M# Z  I0 Z1 n6 v
    2 w4 n; d- D$ d9 ?
            labels = torch.as_tensor(labels, dtype=torch.int64)
    7 l" r9 Y2 h( W8 c8 c2 g" N" u
    / Z& g) B( [" z- G* D1 v
      `1 `) t  v) I. O! I' G
            target = {}
    $ u+ D; ?+ M1 W2 T5 V+ N        target["boxes"] = boxes
    5 Y9 O* M. [  I$ @- B        target["labels"] = labels
    5 C( _$ S& X8 q) Q3 O! Z        target["area"] = area1 f+ h3 I! G6 P& g& j' L% R! U
            target["iscrowd"] = iscrowd9 A6 q1 i9 |  u& h( t0 H- K6 D
            # image_id
    , [+ A% @, k! R0 ]        image_id = torch.tensor([idx])
    8 T( [  c! Q0 |. o" j$ {        target["image_id"] = image_id& Y  c3 C' j- E9 K8 `  B, Z1 |: ]9 R

    & T. o  i$ u/ ^* x+ ~; x6 U5 C
    7 r" K* y4 E1 a4 H
            if self.transforms:3 q( V& x! D! ^) Z" ^3 k
                sample = self.transforms(image=img_res,, X4 U, r- w) @! V; E- k9 y
                                         bboxes=target['boxes'],
    ( `9 ?+ i% r. F2 `                                     labels=labels)
    8 C5 r* y5 C* v- j4 b  Q8 c. v6 d' G" ^, G/ X( O$ ~' V

    : m, R7 V6 H& l* N) P0 W( h            img_res = sample['image']
    6 K  S& M/ {9 Z; z& o0 J; g            target['boxes'] = torch.Tensor(sample['bboxes']). v/ {0 K' s: W& `4 B0 W
    ) V+ K0 k% N% ]  T! A! I
    0 p$ \3 D) x# d6 T9 K. j
            return img_res, target
    : O# |/ ^& r/ }9 t8 {
    ( u6 `: M/ \+ q  F$ k7 u
    0 `) H" A& y2 e5 z5 P
        def __len__(self):
    1 v! t; ?- M: E3 f+ k        return len(self.imgs)
    * K  N( d5 \2 K
    2 {9 j* b5 c4 p+ q8 {

    , o1 \# w. H9 {$ l# function to convert a torchtensor back to PIL image
    0 A" Y6 F: f! \0 p: X: sdef torch_to_pil(img):8 U: X* d4 r2 {6 L. l8 J
        return torchtrans.ToPILImage()(img).convert('RGB')' X9 f9 [# r! ?  R

    6 c5 ^3 l. M: G

    % b. k. C9 n8 K- ]6 K1 o2 [: z1 ?4 j( f2 O* {1 P7 a1 `: u/ p

    * X4 b" }2 H, g* h6 v: u/ idef plot_img_bbox(img, target):: q8 p7 ~1 r# Y9 h5 o5 f
        # plot the image and bboxes0 F  z' k  H. e
        fig, a = plt.subplots(1, 1); e" f8 C! u3 @6 q3 n: P
        fig.set_size_inches(5, 5)$ ~4 J5 D) z) {
        a.imshow(img)
    7 B% N. Y8 ?* W: Y9 v/ W6 q& v    for box in (target['boxes']):& h* v' U% E5 O; c
            x, y, width, height = box[0], box[1], box[2] - box[0], box[3] - box[1]
    $ p( S1 X" W7 C) G+ S        rect = patches.Rectangle((x, y),
    + m( V7 a! U# S+ s5 m7 r# k                                 width, height,8 @2 V3 Q- t" l: Z" v) ?9 \
                                     linewidth=2,
    $ H% ]+ z, B4 B9 o' a% e4 H                                 edgecolor='r',+ ?$ }" Q7 K5 i) U- {3 I3 r
                                     facecolor='none')
    # U2 c* ]' z- }: Q
    : m" L' z/ N3 _

    4 V, |) q! Y6 @6 d' h        # Draw the bounding box on top of the image4 D# j: j8 y$ l  z7 @( f7 U6 a- ?
            a.add_patch(rect)
    # M  R' ?. o: Y% B( E. |( |9 `    plt.show(), n) B8 ~2 s+ h, P- ^# j
    * i- S" U  P& G
    * P( Z% f5 _# j3 v: D; A
    2 x3 N# t& d' p( o' `7 T
    " W) n+ M* Y( @  b: a* A/ @
    def get_transform(train):' p* }) [! B( w# y/ Q8 [8 h4 b
        if train:. O; E, c; t3 X2 }
            return A.Compose([, O/ V2 W8 G2 p. e) u) P6 ?6 M
                A.HorizontalFlip(0.5),. {- q7 I2 U& k1 s8 F/ U- I  v
                # ToTensorV2 converts image to pytorch tensor without div by 255% M; G  {5 ]' K) S/ [  ]4 m
                ToTensorV2(p=1.0)" C- {2 N% ]6 N$ H2 ~0 F& a
            ], bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']})
      y# `* S6 X( V- U9 ~, j    else:9 E# m( k5 p0 p
            return A.Compose([
      S2 T9 E' l; `/ d            ToTensorV2(p=1.0). u" R# g) `) J4 ^6 a3 Y0 h
            ], bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']})" X* x2 K8 h. V5 S* B+ }3 `

    ; q( s! c3 Z' @9 Z

    * H# N& c0 I9 }! k4 A! K4 L) l; `2 d. F2 S0 l, J. d0 b
    0 ^3 S" |9 T- ?+ X& @
    ! L. X- h; _  l

    7 L  o" q4 u/ S$ t' J9 Ldataset = FruitImagesDataset(train_image_dir,train_xml_dir, 480, 480, transforms= get_transform(train=True))0 Z0 x7 ]- I8 i9 D; \: b1 [
    1 s4 u% j: l) G' A2 r

    ' c7 Q; {; P' ~* ]print(len(dataset))
    ' w+ b1 q) Q! j! _% U0 x. O! N: \# getting the image and target for a test index.  Feel free to change the index.
    2 Z3 D4 d0 @, ?3 _( _$ z: m  Timg, target = dataset[29]
    ( ?4 c1 l5 Y! ^: O+ e7 x; c$ Uprint(img.shape, '\n', target)( V* _& ~: I: w
    plot_img_bbox(torch_to_pil(img), target)
    8 N, c1 t4 o* |7 G4 N) b1
    9 _# q9 w0 G" L2 a! ^7 F2
    ; p- Z% s+ x9 ~+ ^2 d# P38 \; B- N9 m& n" P
    4
    7 W3 B& C- ^8 B8 z" G2 i5
    $ V7 A) K" L! q1 S6) o) d- @- Y$ b% t) e- z
    7+ u4 a0 D; O! H5 A# ^; E% I7 _
    84 P0 r* C. y6 E' S# i$ f
    93 t" S- K- I* j5 {' [1 a
    10' ~& s" j& F% I' H# F# d9 }
    11
    4 }- j& U* ~6 ?6 P' r4 _12
    3 t9 R+ L* H1 ^" S137 m8 X" _2 }! X/ g
    14
    5 E8 @- X' E6 V0 H2 P$ ]: }0 C2 [& K15
    3 M. g1 c0 ^+ s* z0 f16
      ^+ k5 N' c9 w7 `2 [174 d4 c# B7 `7 s8 Y* F- P
    18) t) k7 l8 s0 V: x6 p) k+ l
    199 ^6 ]+ r. |  ], z( K# Z) F9 q
    20
    3 b) b+ a: x) T5 v6 @' J( z21; s& W* X) l4 |1 Z# ?7 O
    22( o; D* v5 L+ J
    23/ _" }* i3 A* k% l/ m5 N
    24
    7 x1 G! m6 l+ k$ g) j* ~! e255 S8 t' G  M% i; p( S
    26
    # K' P; u( y- N. m& w; ?27
    / ?* R+ Z2 Z% d) H  \$ |28, p! d- \- ~$ H( S1 p
    29
    ! |3 v2 ?! G+ e* }% p30
    2 n$ F4 G7 Q% G: i3 r) k% S* B2 C31
    7 m+ H5 b. a/ I" Y32' d& ~( R2 a8 @0 h1 S/ K" g
    339 q0 Z% M5 `* e* |, s
    34
    9 B: Y' |, g' b/ x6 r" l+ a354 E" j9 x% g: T$ h, C7 ?
    36
    , g$ w8 ?" D1 s6 n37
    9 X- Q% W8 B+ Y1 N* _' u38
    6 u# o4 B1 d& C6 e3 B2 ~39
    * x6 L- J+ q, z* n$ `1 p/ [402 y, D9 h' j8 i0 O2 }3 u
    41
    6 R2 S% E4 H2 A& p' E/ w2 Y- R) o0 h42
    : E. h) @1 r( s9 F43- P% e9 r4 ?$ m* Z! q* B4 D% ]
    44) E/ Z1 x% T3 W- W; w5 {* d
    45
    ! v3 @! R+ `3 W& V/ m0 c" I; _% m9 Z$ W46
    $ A7 v+ e4 \, T47! [3 t; {  e: U! }7 c8 K
    48
    # i" k+ R7 k6 K' C49
    7 _. Y8 P: ^: Z9 D# S: r) x50& Q* l' |/ Y  u
    51
    " c9 O+ M/ G, M2 b" Q7 O529 J( b2 H" c, [" q5 e/ X
    53
    - O! D4 c; \$ T8 R547 [. f$ m5 v0 [8 r6 Z: \
    559 T  W5 m* |7 a2 [
    56
    0 H- u+ a6 ]$ i" r. ~: s4 V' {4 u57' H  z: K  ^) k& N
    58
    : @4 H  ~, e+ l1 x2 Q! S0 J59) s4 U4 |  A8 `5 G, Q
    60
    # A5 X5 w8 x; C0 o0 J+ H618 ]7 o( M" t& J8 m! f& _7 N
    62, R. {$ q5 z; h; W# _4 q2 ~
    63
    ; \0 C( S; e9 l5 ?3 k  M4 y8 l: t648 b2 g6 o6 g+ F* U0 \" Y
    652 n2 E( ]0 D2 L0 a' m; V
    66, ~/ g: b0 Q! L7 A( n7 m1 k/ A
    671 ~% N" ]7 I$ V. d* e' G
    68
    ' v; `' m& y- d6 {4 m69* p5 t% T/ }9 e1 e$ ^8 M
    707 t* a/ P" B+ l
    714 d4 |1 y* E- `9 ]! M, C& ~
    72
    ; ?& B7 }! S9 d3 M& W4 f2 }73
    7 G, X; I* c! E# b  U1 f" d74
    9 m" |% m2 X7 d3 ~: K; _( s4 {% s75
    3 |% a8 B* }: I76+ ]6 V  W. R; v# b: e$ n, ^
    779 I: h! p" F& L, }( z3 m
    78
    ' J- l5 Y  X/ D5 W7 \79" L" k: i% q0 ~( O" t
    80
    9 H0 f+ v( x. X* o81
    - ~% f# L/ ]$ E5 M) W82
    $ ~* r/ t" P. [& _- H; B2 \3 l832 z9 i0 E, v) U9 o* \
    84
    - l. |, b0 [( g1 X85
    ; D) Y' R2 h0 D, ^8 @6 H& c- Y86
    ! w1 T- n' h" |7 Z( Y/ [9 B8 b878 a: c+ ^4 Y' ~8 V) {0 Q/ A3 y
    88+ e. D  w" X0 i! h9 E5 g
    89
    : h; X; w. @3 T+ Q1 n90
    2 @/ y2 c. Q2 A1 }' M% w91
    4 O( c+ X, }+ |/ J92
    7 K" ]- ^+ X# \9 A2 u. l93( ]+ b% q5 u2 _' b2 P: P/ _
    94
      h1 c% g! A% m. d' R# i# l95
    4 u3 J; O! ~: f) r7 F96
    7 K2 ]; V% {* x97# p/ Z% {, ^7 e0 i  d8 F, R
    98
    ) O- i! N' b) V$ @* A. c99. h+ A8 A1 R4 _
    1000 [" k. Q5 y$ p0 W* Q+ ^% C( q
    101
    ! F+ Y* A, g3 v; {4 q1021 D8 v/ R* h: ]8 _, I
    103
    / {) N/ i, s! W5 Q104
    : n' p) A) Z+ c1 C% h105/ w% F$ ^1 D" o; }& q* s$ O
    106
    ! H+ t' e' F- p5 w107. B* q- f( ~' Q/ g' @% P
    108& |! ^) k6 P( P! n& d9 H% T8 a1 W
    109: U& I0 Q- Q3 p7 i* z3 m" ?
    110
    " T6 g2 s6 `$ {111
    4 c9 u6 g& n: z4 s5 Q% U9 N9 e6 R1123 f% B3 y) X" {- c
    1130 l: M$ ~1 j0 i
    1149 R' b# k3 I* o4 \# i( b/ f
    115* d: {4 H2 ], _4 G9 I
    116
      z5 w$ M9 H, d7 A$ ~& B7 |7 h117( N  |, Q% D/ r  }( D) Q* E5 r* \
    118
    ' E* g. k, J. T1 t1 G* P119
    % Z$ v  @6 f, j' g$ f120
    % f0 K2 q7 c: [) G' b4 h6 }( a1216 R+ V) _% T5 P0 X5 B
    122
    % y, i) F! C8 E123- J; V2 {( P2 J' D  D( Z
    124
    # j5 ^1 e9 V% }8 S: w9 w4 o7 ~+ k7 u125
    - V1 F; C/ i1 D& J126
    6 Q( Z+ C0 \3 Y( \1275 O$ [# m% z2 E, a
    128% |* ~% Q. @3 [( L2 X/ r  c- O
    129) \9 s$ Q+ C6 w" d
    130/ I0 M/ ?5 [( F. }" o+ G" q
    1316 X7 m0 ^0 ]5 I, S
    132. R6 s+ }& R; r4 d; K% U, G
    1333 s, w6 x$ n2 S4 Y, |0 U
    134
    + \0 I9 R6 }5 K# I135
    1 X3 c- U9 x( ]136, m7 m0 O: e4 d9 A+ q( z; N
    137$ l9 J# s+ R5 G
    138
    2 b7 X% B7 V% J9 g0 ?  o139$ `% ^0 L" w5 E
    140
    ! i0 {; n. X. ?9 m# b( F141
    2 e6 ^4 g4 T$ U; a2 e142% `4 u7 C" D' n0 ]$ {6 l1 B* ]: p; g
    143( \9 S6 l  s" }' ^# }
    1444 ^0 u' c1 D% d6 ~! K
    145
    2 G/ I% g% E! M. x( ~, d2 v146
    / q5 [0 P; ]7 Y147, D# [9 s& Z8 u$ g
    148) q+ W" ~  ~% B; h7 K6 Z5 a* v
    149
    ! W$ K# A. @- E+ H+ @$ V- V150; U# v, {" N* w7 b$ j
    151
    2 x% L+ Y! H- S+ ]7 }. a" `152
    ) Y0 ]( r! S9 a/ C3 }153/ b0 I0 ]6 H1 E7 O+ H1 Q, v
    1541 A* x' |  T& P& M
    155: q7 o8 p, d' Y; z
    156' g5 R+ `* E  o5 S- n, p  W& a
    输出如下:
    ' u4 ?! B$ P. w* h, c% S; q( H7 E8 a& n& |3 E2 K( u) N' k( a  g; }
    4 I% J2 ^* Y( A8 a- V3 M; R
    torch.Size([3, 480, 480])
    / a7 k# M7 P: B9 @ {'boxes': tensor([[130.8000,  97.8000, 327.6000, 292.2000],
    % Y1 m# t7 I. T9 |) K        [159.0000, 268.8000, 349.8000, 427.8000],# n; ^( Y- Z$ q, e4 K
            [  0.0000, 282.0000, 118.2000, 429.6000],
    - A$ P: G2 y( e8 U- ^4 @3 q        [ 43.8000, 107.4000, 199.2000, 280.2000],
    0 J4 \4 S  \$ I. N% V4 x        [295.2000,  37.8000, 479.4000, 248.4000]]), 'labels': tensor([0, 0, 0, 0, 0]), 'area': tensor([38257.9258, 30337.2012, 17446.3223, 26853.1270, 38792.5195]), 'iscrowd': tensor([0, 0, 0, 0, 0]), 'image_id': tensor([29])}
    0 {  M! y% K/ [; a1
    " g/ w- J& \7 q- x! h1 N2
    4 ]6 B5 i* }' Z/ j3 @6 V; ?0 f3
    9 E& v: Y# _, J( M4, r( E4 O- ^4 H
    50 Z' l" Q) f5 u& ~$ s; B, m" J
    6, Z1 I$ F1 {4 i

    ! _% I; G" D& H# Y, \
    6 |7 [. m5 z9 x

    1 `  J2 c5 b4 @

    3 }% @9 k- d) F. w1 I+ E下载地址( a) I$ \9 h# X! z
    链接:https://pan.baidu.com/s/1QZDgeYTHyAlD2xhtJqZ-Yw' F7 R) A$ L: P3 U
    提取码:srjn
    7 n' v0 D, M. {. h! T) _+ b————————————————" Y$ u- x9 j* _7 x
    版权声明:本文为CSDN博主「刘润森!」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。# M7 C) V5 Q( x; j3 F. G0 g9 h
    原文链接:https://blog.csdn.net/weixin_44510615/article/details/118496273! o$ E4 k9 }! Z& i( A8 D9 y
    0 u$ q/ i1 A  E* }
    - ?) m- o8 y. H. v9 c2 O
    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2026-4-16 04:56 , Processed in 4.041776 second(s), 50 queries .

    回顶部