数学建模社区-数学中国
标题:
深度学习和目标检测系列教程 8-300:目标检测常见的标注工具LabelImg和将xml文件提...
[打印本页]
作者:
杨利霞
时间:
2021-7-14 15:18
标题:
深度学习和目标检测系列教程 8-300:目标检测常见的标注工具LabelImg和将xml文件提...
9 ]8 A8 q2 L8 h3 r" L8 b. J0 h
深度学习和目标检测系列教程 8-300:目标检测常见的标注工具LabelImg和将xml文件提取图像信息
6 |7 o6 i" |3 `$ a! e6 }
图像标注主要用于创建数据集进行图片的标注。本篇博客将推荐一款非常实用的图片标注工具LabelImg,重点介绍其安装使用过程。如果想简单点,请直接下载打包版(下载地址见结尾),无需编译,直接打开即可!
4 y( }6 q: Y( U% I: r2 k
3 X- o- u& X1 k$ ~
+ t7 U L5 k" r/ r0 U4 x1 K
感谢原作者对Github的贡献,博主发现软件已经更新,可以关注最新版本。这个工具是一个用 Python 和 Qt 编写的完整的图形界面。最有意思的是,它的标注信息可以直接转换成XML文件,这和PASCAL VOC和ImageNet使用的XML是一样的。
: x* g5 G" ?" T+ W! |% V
' q6 d8 f2 p5 W' m( J
- F5 r( P) u; J4 r1 T! c
附注。作者在5月份更新了代码,现在最新版本号是1.3.0,博主亲测,源码在Windows 10和Ubuntu 16.04上正常运行。
) E7 i7 |. r1 B7 q* `4 p+ I
{3 R0 E/ p2 [4 `9 N
( C0 P" M* C; x
具体的安装查看Github教程:https://github.com/wkentaro/labelme/#installation
8 w. ^' z% Q, l U* F
3 f8 S4 q! X) U) ~* l7 ?0 S
6 U! b; X4 ~. i+ {. H( S) F
在原作者的github下载源码:https://github.com/tzutalin/labelImg
! \$ c9 I! T1 C: @
。解压名为labelImg-master的文件夹,进入当前目录的命令行窗口,输入如下语句依次打开软件。
( u9 O( v; u, r/ O
% T! c- U% ^0 O* }9 p6 J" t: W
! |6 F1 b7 B" N# I1 t1 E
python labelImg.py
( J& z3 b' P X; T. f q; t! Y, w5 t
1
0 ]2 g2 `: [$ L3 g- }0 M
% `8 n+ b2 A0 _6 a$ C: F; J
0 M% I" o7 V: j) W( O, m
) y4 U2 t, Q, r; J8 y
7 e" V Y" k5 Y+ E* c a" ]0 p
具体使用
* G! p3 ]& v+ a* z
修改默认的XML文件保存位置,使用快捷键“Ctrl+R”,更改为自定义位置,这里的路径一定不能包含中文,否则不会保存。
2 }9 r" j; b' A t g
4 L5 C4 k! X, K
# `4 B, I! G- s1 `5 v+ G1 ?0 o& o
使用notepad++打开源文件夹中的data/predefined_classes.txt,修改默认分类,如person、car、motorcycle这三个分类。
0 Z2 U' O7 R/ S- s1 E0 B* E
- |# p+ O- }" r4 r% U
; ?+ ?8 a5 W9 H- g4 I: n5 B* U' ?+ }2 x
“打开目录”打开图片文件夹,选择第一张图片开始标注,用“创建矩形框”或“Ctrl+N”启动框,点击结束框,双击选择类别。完成一张图片点击“保存”保存后,XML文件已经保存到本地了。单击“下一张图片”转到下一张图片。
0 I! n: N* r# q& i4 g
# K" y9 y; o _' t7 j; q0 F; Z8 @5 ~
" h+ r& S7 F% G* t/ h5 z
贴标过程可以随时返回修改,保存的文件会覆盖上一个。
' d _. u. S3 K: M2 O
( ? e# L0 q- n" `- b8 ]6 o9 ^
2 I; [2 C1 ]) P3 |/ O
完成注解后,打开XML文件,发现和PASCAL VOC格式一样。
, C: V# L7 \3 m! k
) d/ q7 z; u% ^
* ^* |8 C7 d! N. P1 H: _2 H
将xml文件提取图像信息
0 W* r% h0 |2 Z" j# w& _
下面列举如何将xml文件提取图像信息,图片保存到image文件夹,xml保存标注内容。图片和标注的文件名字一样的。
! Y/ a3 B2 {" \) u5 i6 D1 Q
$ m+ y# ^0 m9 |1 a( _2 y) L
. L1 v) C, y9 n) i9 U
) Z4 L @2 {% W2 q# v. x0 P
' P4 L- K* b0 E
下面是images图片中的一个。
, T. c3 v9 e- W Q
5 }0 q" I* n, y# J! E( [
6 P& w; n& p2 M5 U' e
下面是对应的xml文件。
! Y" _' X6 A/ |' H1 @7 j
( P& M8 |4 }! c) b% ^
, R6 _$ U) h8 f2 c1 V+ \- l
<annotation>
5 {4 _! v7 ^! f" @0 H. X: z
<folder>train</folder>
! s, f2 w: P: q% H8 T$ H
<filename>apple_30.jpg</filename>
% p9 j/ i4 d" C* ?& S
<path>C:\tensorflow1\models\research\object_detection\images\train\apple_30.jpg</path>
" M0 w8 o: L6 ]
<source>
2 n( {/ T/ S! ^7 B& s v
<database>Unknown</database>
! h J G/ c B
</source>
$ r2 @% O) Z5 q# I
<size>
% {5 H2 e& e. |$ B5 O4 C9 e: C* Z% R
<width>800</width>
1 ]9 m |$ U$ Z: J
<height>800</height>
, g. ?: w8 }+ A! h$ \3 j1 [4 |
<depth>3</depth>
1 O* k& ^# M7 F
</size>
, O5 C& C& I+ K! a/ A1 b9 B
<segmented>0</segmented>
/ v) A* K; [# Y1 ]% c% a) o( ?7 \
<object>
' C: o. e& h. X$ p) y3 N
<name>apple</name>
+ Q. H% z6 |6 P1 Q; p3 H6 x
<pose>Unspecified</pose>
% Q4 a' C( F D1 r' s& @% C% |- o
<truncated>0</truncated>
6 x. d- x: w; C1 Z" g6 ]+ Z1 t$ h! b
<difficult>0</difficult>
' U! m) \ \; v8 l3 P
<bndbox>
/ O( _, r; G+ [" Y( U u
<xmin>254</xmin>
: {9 X# X' E; x. {5 i5 C. P6 M
<ymin>163</ymin>
. f% x' O6 l9 t6 n. V! ^2 S% S
<xmax>582</xmax>
: z" V; E, J! j1 H9 S! N5 z
<ymax>487</ymax>
\( Z7 O& M9 Y7 Y- G7 n# U
</bndbox>
3 m( N7 O' g/ G5 X
</object>
& G/ o# z1 R3 j4 h
<object>
7 ~" F6 f1 \; F( v
<name>apple</name>
1 N5 |* _( C( T2 Z% v2 E* W
<pose>Unspecified</pose>
* u9 X( ~/ j/ w* W# q9 z' w8 R
<truncated>0</truncated>
; O" Z' ^! X3 ^: I
<difficult>0</difficult>
6 J7 G. O0 K( j# I& p: Z0 B8 k, G
<bndbox>
! u9 t4 K! f- f I$ J* ?1 R
<xmin>217</xmin>
7 [7 ]% ?- ^7 b
<ymin>448</ymin>
3 [ a0 L; h) R/ C, c6 F
<xmax>535</xmax>
" V: c8 o+ Q( V
<ymax>713</ymax>
?8 a% I9 V$ T# V+ I7 {' f9 S
</bndbox>
4 w5 f1 G- U9 b, R9 A0 a
</object>
& Z' M+ S5 x7 Y$ Y% \" V
<object>
9 `, G' B0 i& l7 O* K
<name>apple</name>
5 c8 [4 @4 S W0 G. g) q) N) N5 V1 n `
<pose>Unspecified</pose>
' G3 @) R3 J z1 V/ u
<truncated>1</truncated>
2 C, w, k! g# V0 ]
<difficult>0</difficult>
' f( d- ~+ D ~4 T$ D/ L1 L
<bndbox>
/ P. |" r4 _% ]) r& ]( R( d
<xmin>603</xmin>
& v0 v4 d- p8 D3 ]7 l
<ymin>470</ymin>
; p& e, P4 n" q9 ~) _0 m. k5 r
<xmax>800</xmax>
- G- i0 I. | z9 }$ J% r$ v7 ?, ~, Z
<ymax>716</ymax>
, E2 R& u/ d; \/ j3 A
</bndbox>
6 {# _# `3 }4 A
</object>
* Q# I" [/ R9 P/ h4 G% d
<object>
k& u& f/ c! l: I/ @# ]: h
<name>apple</name>
- f: s* k' |5 u" Q
<pose>Unspecified</pose>
; D& ^; ]7 A/ n3 x3 J7 W
<truncated>0</truncated>
- v* W) d* p" W1 Z1 G
<difficult>0</difficult>
* D/ m T- z$ t, j. \4 W
<bndbox>
I* o+ I* P- Q5 }
<xmin>468</xmin>
u- K$ b2 x( c
<ymin>179</ymin>
, w8 X" h: e; w1 @2 R; g
<xmax>727</xmax>
" P ?/ q# a6 n4 i1 l
<ymax>467</ymax>
, I' B* R0 ?5 g7 J! w! K$ v% S
</bndbox>
* `. }' A/ l0 E9 e4 K- j7 U2 x
</object>
$ C6 Y$ c1 m- O3 L, h4 @
<object>
" u- s J4 c; H$ v6 s4 ~
<name>apple</name>
1 A$ @ C2 V; }* Q
<pose>Unspecified</pose>
0 y2 f' E( f! L. M1 Q1 n$ L/ ]
<truncated>1</truncated>
# D) X9 ^9 ~0 Q; k4 i: ~* G
<difficult>0</difficult>
$ k6 C" ~2 t' D
<bndbox>
* A( X( t2 e- R, \% f/ n) k$ D6 P
<xmin>1</xmin>
! g$ E1 e% L! W- W; P6 w2 K
<ymin>63</ymin>
: f$ x/ N7 s8 D; S ~
<xmax>308</xmax>
' c' t5 v: _3 y
<ymax>414</ymax>
1 Q& Z C; `9 H! {
</bndbox>
9 G9 G5 T! V; q1 \
</object>
; `# t" ~. o$ d5 \% A# g
</annotation>
7 S, N) J7 s% o* H
1
: \ q& l' i7 S# r
2
' c1 b/ S8 P7 n: [9 I
3
5 N$ N3 p+ ]+ R) I. k4 ~6 r' t3 ]
4
' f! D6 I" H! K9 Y; E
5
+ s7 j" |) t) c. v3 K: `' P
6
+ \5 \: r& }+ D1 x. Z
7
2 H" P% E: W9 u: [
8
B7 o- v. L4 ~( ?( p/ D ~# t: E
9
6 T( }& ]" F, J0 c. [8 ?3 a
10
2 A8 H9 T) C% ]
11
. n E9 _; U; C( R
12
/ f S7 K& ~6 a
13
4 ]8 ]) ~9 w) y8 H/ H0 N% E; W
14
! {/ g3 E0 I/ }; c3 @0 D
15
/ a1 y. f( S& h, H
16
; {' H! s) e& p- ]# K4 J
17
% c; {5 Y; P3 h. j) f. C, T+ s" g
18
# o( |) ] j9 D* C, X
19
8 |7 D% |" A4 P& N/ B, K
20
, h+ N# P8 {, a* L5 B5 K! c& ~
21
( O" t: j+ @, e/ B! z
22
3 p+ l4 L) x$ X7 L3 e5 L, ^) d
23
* K0 \, ]8 n" S4 B" ^) s% o
24
# _, Q& k, X! }
25
) R9 s$ b9 b# h& q+ P! n- ^
26
5 F+ W' v- B, g s( W
27
( T; }- C0 Z2 v Q9 i4 l
28
2 F7 B1 _, U9 n( j
29
0 y0 U4 y/ A$ M R/ V$ A
30
& M) O: Q1 z% {; y
31
& g8 A9 L6 B, J# O
32
, `) V- j3 A# e% s# m6 N& l0 n+ k
33
6 J d7 W ?% G
34
$ ]' h1 N* I$ d: i
35
2 N u+ b- u" c1 m
36
: @/ h( f& l8 T7 T
37
; T: I; N0 h ?# v0 y" D
38
. ]4 E% _9 D. M E' f
39
2 ~6 F( ^5 u- }% I. y: C
40
+ H' @4 Q$ }8 X. J
41
! k/ t" Y6 C) ?* v% a
42
+ Y1 O( g6 E9 S. y2 j
43
; \" r$ {, _1 {( O9 ]5 @
44
" J/ H/ S0 u: A* ?7 _4 T- I
45
. K" P+ @, p- B- }$ h1 `: r7 O& U$ \
46
1 l3 E( @8 ~3 U# A! j6 [! G* t) o
47
* R+ _9 k! ?7 F; u
48
# p$ G! E# \$ S
49
+ d& {7 }- ^5 e3 _5 T6 S2 r( `! T
50
. w; G; d* P1 ~# e, g! H
51
! e4 T$ y# ]+ m# n
52
4 M/ j8 S9 |+ ^: J* U; A
53
" c! R: q6 Z5 b, ?+ J( m
54
+ K' b+ s, l/ J7 H3 b
55
' M; \1 U1 N+ l, g
56
" T3 j: w. |: e( r# d0 J8 P
57
( `9 v4 F5 _3 w& J2 K9 o% O+ J. c
58
% s/ q% X; C6 O( S, l, J' O
59
& i$ K/ \" a( P. h1 a \4 b
60
$ G9 c% |# s+ R8 v1 r8 s- y6 L
61
) A. q/ l8 g( v
62
; Z3 Q4 d8 q( D6 Q
63
% i/ p9 [0 |1 C, ?' }
64
0 F( C0 Z5 Q1 P% Z# z( M
65
" d! g6 s8 p2 n: [) ?2 r* S
66
% f( [3 E3 M8 E: B4 m/ e7 [
67
, w3 ~+ W C( k5 Z
68
* w7 B# M, o8 u7 m6 l
69
! o* c& [+ A1 O4 v. Z
70
$ k. b' ~, S& J9 g
71
( R3 p+ Y& F% i4 u
72
, u- A% V5 o! Y. @2 B$ ]# \. c' X0 N9 M
73
i1 T7 u0 D) V0 P8 C
74
4 Q& [- [9 v/ o K9 s( T! D* O
将xml文件提取图像信息,主要使用xml和opencv,基于torch提取,代码比较凌乱。
& ]" r1 q% S6 ~" m+ L2 o2 i
; @! G" z* j) y4 M, k! |* O, P
& Y* Y2 M) \$ G1 Y- Q
import os
% L/ G0 ~ B5 Z1 ?
import numpy as np
) [2 M0 E, Y# ]5 R4 a, Z
import cv2
1 T' Z( ?7 v) b1 O
import torch
+ Z- @8 Z0 Z+ c( d4 [9 r
import matplotlib.patches as patches
' ^' f6 J$ Y( a1 G" ~: v. C( W
import albumentations as A
+ D6 g% q# L: w" a: N7 M
from albumentations.pytorch.transforms import ToTensorV2
. Q! f6 X* O: H
from matplotlib import pyplot as plt
- \3 @1 w6 m9 G7 Y4 V
from torch.utils.data import Dataset
# P' {1 }2 n) z: a
from xml.etree import ElementTree as et
4 y0 P. t$ w& }2 a* u# o) W9 L
from torchvision import transforms as torchtrans
* `: V N# `6 h/ q2 h$ d
- m+ V, s/ U0 A: T, H- m
, w, u7 f* N2 J. f# R% a
# defining the files directory and testing directory
- v0 _; y' `5 p9 U' e
train_image_dir = 'train/train/image'
7 E3 [8 }1 k# u/ k* H
train_xml_dir = 'train/train/xml'
0 G- R: K5 a' z7 f" Q
# test_image_dir = 'test/test/image'
% B0 f) A) N( _' s% u* u. B7 |+ R% A( H
# test_xml_dir = 'test/test/xml'
# c1 ?7 L5 L" _% ^, Y: z& g
1 d0 C9 W* A1 p1 Q" G2 s! O
; c2 `3 f- ]$ F2 o" V4 t- n
class FruitImagesDataset(Dataset):
|, R# D' r9 `" ` m, N
- C% z6 l( s+ G+ ?
7 |. e2 X |: k9 u- s3 [
def __init__(self, image_dir, xml_dir, width, height, transforms=None):
& G- R. K1 i3 U! ~% V
self.transforms = transforms
: }) w& _8 x* V! G# F8 Y; j
self.image_dir = image_dir
& q& R9 t9 I( J; y( }/ _
self.xml_dir = xml_dir
& C3 ~1 Y: g5 I
self.height = height
/ i$ r8 I( B) z$ [" h3 A Z
self.width = width
) J0 I' b; k8 M+ D; R. |4 r
/ x. |% K D4 R0 |5 F; U; D. o
' e; k( g) K$ C' @
# sorting the images for consistency
( r3 H7 @6 O0 q# h* I" |: x
# To get images, the extension of the filename is checked to be jpg
# w9 U3 c. U3 n7 s2 h/ r
self.imgs = [image for image in os.listdir(self.image_dir)
5 j1 J( w) G! s' `( Z# _2 N6 ?9 O
if image[-4:] == '.jpg']
- d- d' c8 |4 q- |
self.xmls = [xml for xml in os.listdir(self.xml_dir)
: E: b% N- z- n1 g5 w$ k9 Y
if xml[-4:] == '.xml']
- p" y9 }* J! c E- F
) l7 w6 V5 T& N! U) H* O
9 S$ ~ d: n+ q
# classes: 0 index is reserved for background
6 i) j* R: F" R
self.classes = ['apple', 'banana', 'orange']
! n, X$ Y" t4 v w; e! j. R
" y1 ~: x: \& ?( `
/ q3 I5 j5 b6 u! o$ ^7 _# G3 B; n3 u
def __getitem__(self, idx):
, v' G9 }: q8 Y) v% J2 [
/ D2 W5 t2 H: s/ N C& h9 D
# E, K& L* E7 J, H& |( M
img_name = self.imgs[idx]
1 E8 f0 G5 @& Q4 K& \
image_path = os.path.join(self.image_dir, img_name)
, o9 Y) g$ Q: e7 Z& Y
6 m; z* ~ @ y' v- o8 [ s7 [" j
0 H) |6 m: P3 ]1 _! r+ h; Z: w
# reading the images and converting them to correct size and color
. [/ [0 C' w; D/ t$ S: F
img = cv2.imread(image_path)
$ I' r4 M' J( |2 H7 i
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32)
1 n! ]$ `) n( s4 @3 k8 i" M
img_res = cv2.resize(img_rgb, (self.width, self.height), cv2.INTER_AREA)
5 k1 ~6 L j& K; h
# diving by 255
* a( ]9 C5 J) M& A" b
img_res /= 255.0
/ O1 `; P+ r; g, E" T
" Y' v$ I z( Q
7 x" q/ z" M' U& @' M+ W; D
# annotation file
3 J9 O2 l$ S0 j: H
annot_filename = img_name[:-4] + '.xml'
7 Z1 a1 N: i) z+ m2 n9 {3 y( m$ [0 p
annot_file_path = os.path.join(self.xml_dir, annot_filename)
1 w7 ~. `/ v: W0 I- K
?7 Q! @2 O" ]
) S3 S5 I- f o
boxes = []
* u! w9 k8 |2 h
labels = []
; x7 o9 ^- X: O% ^! b, b
tree = et.parse(annot_file_path)
! o4 ^1 i! b2 p( c) r& x
root = tree.getroot()
' c& ~/ X7 H) ]5 ~9 P! c
% j( }( m. h, x! s; M3 D
$ t- t& f& }1 Q5 E3 |% Q* `
# cv2 image gives size as height x width
, ?% s# m* C P5 C
wt = img.shape[1]
& v) K) I1 M4 M5 o+ @* h! G) k
ht = img.shape[0]
4 q" h5 O: t, t4 S2 J( J1 Q2 \4 t
' f; Q5 ]+ g, A" T% [
, S; I1 \& G# W( J& I6 q2 t1 E' R
# box coordinates for xml files are extracted and corrected for image size given
$ _+ D3 z1 z0 r) k, O, i# u
for member in root.findall('object'):
- `: B+ i2 i/ ^
labels.append(self.classes.index(member.find('name').text))
$ `3 X# m a4 w6 b- U
1 s, P2 R0 `+ E+ r" J
2 x* G. W- N8 _7 r1 k1 |" g
# bounding box
k$ ^$ w. G# {! p# p+ C
xmin = int(member.find('bndbox').find('xmin').text)
/ `, {+ Z9 z/ Q5 |: f& P6 S1 y
xmax = int(member.find('bndbox').find('xmax').text)
^1 ?( z5 x7 N0 d8 n; U( i
0 @5 q" w7 C& t9 f5 k3 R1 y
1 ~- j) |) l5 L
ymin = int(member.find('bndbox').find('ymin').text)
3 A8 S2 V9 x) d) f* \! y `6 b* i8 e
ymax = int(member.find('bndbox').find('ymax').text)
5 L' L5 Y+ p+ C- P3 l) P. s5 b+ B2 Z
; W8 w: w/ x( q5 g
T/ T" \8 m0 a, |" l. Z5 _
xmin_corr = (xmin / wt) * self.width
0 i4 L* p* z3 V. ]3 x q) H
xmax_corr = (xmax / wt) * self.width
6 M. K- R( I" k7 D. a$ c
ymin_corr = (ymin / ht) * self.height
( Z$ h0 z+ i3 V, @) J
ymax_corr = (ymax / ht) * self.height
/ s9 O. ^3 r# w# ^/ P7 I
boxes.append([xmin_corr, ymin_corr, xmax_corr, ymax_corr])
1 w7 o' [6 b+ Z7 B" d
& Y! O* X6 i! f: G1 q
8 v# y. f; i8 t
# convert boxes into a torch.Tensor
3 b0 S8 X! B7 ~* e7 u- z
boxes = torch.as_tensor(boxes, dtype=torch.float32)
# t0 P" D; R$ M+ Q+ w- J
: i3 U( @: ]" A1 Y$ L
[" p: I0 D e8 k
# getting the areas of the boxes
/ T+ R& H" j( J+ r3 Z$ y
area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
7 N+ ?& Q% C5 T
2 K2 L2 a9 P* \
) G3 e7 ]! b6 u. d. i% ^
# suppose all instances are not crowd
" V4 g' \7 ]" }4 ?% z
iscrowd = torch.zeros((boxes.shape[0],), dtype=torch.int64)
9 W6 V% T' N! E L* V: Z) R
( g2 _# q: K) y/ v; D
J6 o: j+ M& ?! R! f& J
labels = torch.as_tensor(labels, dtype=torch.int64)
* [: H! i* {/ a, t h% R! O
* \1 T. ?" }. a. T, I- D5 M" n
/ K; O* Z: E1 P( T; y
target = {}
: u1 _4 J5 {& _( l1 A( h# ]
target["boxes"] = boxes
% ]! p! G5 A! \3 V) T, K! o( u# ^
target["labels"] = labels
J0 k7 `: O9 r: z/ ^9 M7 r
target["area"] = area
9 v3 |; ]3 E; m$ _- l7 Z2 \: m
target["iscrowd"] = iscrowd
; g4 a) E. f% g& s
# image_id
1 ?) d$ Q) ]8 Z, ^: x" K
image_id = torch.tensor([idx])
7 x# f3 j6 H" @% x
target["image_id"] = image_id
; y: w8 g. ^, ]/ s
3 c* E6 U# g: {" _& W( k
) l( a2 b. b4 H2 w. g4 W* n0 |- E
if self.transforms:
: @6 e \7 M p0 ^& i
sample = self.transforms(image=img_res,
# K2 R4 j' ]0 X/ M9 M
bboxes=target['boxes'],
. c; S4 D) M0 o) M& Z5 Q% r4 m5 U
labels=labels)
+ e' K' n7 h6 n
, K. J5 k3 B0 b, j3 d, E
4 t! W# d; f4 q( V) G& @% y5 F/ v
img_res = sample['image']
9 P" U( t Y% E9 y
target['boxes'] = torch.Tensor(sample['bboxes'])
; I m- u% c, y( }( w8 T
) M: p5 m7 A- V* c9 Q, O
+ l; Z3 f# w0 u R, E
return img_res, target
4 F d- C$ r4 x, d; o
: M6 N; C; @" f: s0 n6 B
5 |" K' Z, v2 W3 [# B/ z) j$ a0 f
def __len__(self):
9 j: H7 V$ n8 w( D7 s* [
return len(self.imgs)
& I0 }9 l" `5 H3 ?& f; t
: L1 e6 E- u/ K% y9 H5 ? x
+ j Y; e+ z+ y: p/ O
# function to convert a torchtensor back to PIL image
7 W. C ^/ b5 }' X5 q1 D
def torch_to_pil(img):
* _: E' c9 \/ s% `/ b6 [0 f
return torchtrans.ToPILImage()(img).convert('RGB')
?' B& b/ V5 e+ R1 d# a' s
: [+ r# l2 H9 i8 V/ }, ]8 q6 r
L+ T- Y! Q" H# C
0 v8 O C; }) B! j+ {' S- C% {
s& }' j% j2 l/ G
def plot_img_bbox(img, target):
& d" O+ U& F7 ?# n# H
# plot the image and bboxes
, P8 g' w/ H1 f/ P* }
fig, a = plt.subplots(1, 1)
5 K2 K3 T9 d" K
fig.set_size_inches(5, 5)
' q) \3 p! t( c3 ?
a.imshow(img)
: p/ n- M2 O: B2 f/ _& i/ N6 }
for box in (target['boxes']):
9 i0 q" q" O B0 y) D" e
x, y, width, height = box[0], box[1], box[2] - box[0], box[3] - box[1]
) ?0 T2 q0 B+ @2 R3 u* d B
rect = patches.Rectangle((x, y),
( @6 p% v( V2 T7 m4 U) H" V
width, height,
2 \7 X! k2 C9 F7 e1 P- ?9 c. ?. o
linewidth=2,
1 H+ G, w7 M6 _- p, T
edgecolor='r',
' h6 m; F# ~6 e+ f
facecolor='none')
( }1 @& c+ }, Q# g/ M4 @
) g: S8 ? v; b3 \& d! x: L+ k) Q
$ J6 L) F* j8 S% L m
# Draw the bounding box on top of the image
( c0 a# J9 _ C$ S: U
a.add_patch(rect)
# t* [8 a: I& u7 b" B# e
plt.show()
6 b* `& L1 W. [2 K3 q
7 ?; B/ @) a" m4 o( ?) l- I
" a# n. _) t/ o( b3 b
; n+ v+ l, r( Y9 f) s% t1 l
" V( J; ~$ L- `
def get_transform(train):
1 V" P5 f4 t2 ]- T7 b( S7 F, y9 u
if train:
3 j% m* ~, b. K5 k/ P! L$ {7 e
return A.Compose([
4 g* `1 ?0 H! Z2 Z' n1 @
A.HorizontalFlip(0.5),
5 j, {1 w! j# R6 X& y
# ToTensorV2 converts image to pytorch tensor without div by 255
% U& s5 z6 Q$ x/ x# b
ToTensorV2(p=1.0)
& u+ X U0 @5 z- R' {9 o6 ]1 S# z
], bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']})
5 ^5 t# A! S$ h2 @- G) v
else:
: N. |$ Z2 D, E0 A1 N7 H8 c
return A.Compose([
0 R/ v0 P5 O7 \; K9 a0 R% q
ToTensorV2(p=1.0)
& G1 `+ C0 l7 ?6 _6 t: N/ R9 @3 \# G
], bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']})
) g2 F! a3 F q7 T, w+ @
6 L% X( D& Q7 f- O5 y% `( w' C" @1 f
$ }/ Z8 B' ^' N* K$ S
* L% B' Y7 C: S, U9 {% U ]
+ u, b, R. p( f9 k5 X. x; G( T
' h- m* ]' R+ Z* _0 Y+ |% ^
' }, N- C9 L; m) r) M, o
dataset = FruitImagesDataset(train_image_dir,train_xml_dir, 480, 480, transforms= get_transform(train=True))
& j$ O* w0 M! z! h E- Z, w7 O
0 D4 O4 N) k2 y7 ?4 t; `# {
) s( `$ V: Q' c' z
print(len(dataset))
, h U/ ~& R( S& _( o0 j( {& o
# getting the image and target for a test index. Feel free to change the index.
3 V; i) S/ K2 h9 ]& F4 T1 |
img, target = dataset[29]
3 A/ f0 X( y) G7 a. e* r
print(img.shape, '\n', target)
" m9 ~. W( E8 U8 i7 T. m* N z
plot_img_bbox(torch_to_pil(img), target)
9 c4 O4 h' q4 H3 K, P$ D
1
5 ], c( k, s% q3 y4 A$ O3 w: L
2
* g4 n$ [ Z M/ A4 b' N
3
! ]) m+ j9 f, u
4
" C5 c. j( C/ l0 X% {( P
5
9 o; p q5 x( g/ T
6
" A. G0 W3 i# t# t; E) g8 x
7
2 t" \$ F0 G1 W6 c1 u
8
& p* @# u ~3 Z* `% a7 v0 j
9
7 U" Q# k9 G. k4 i6 r, c
10
F! q# ?2 I7 |) ?+ P' V5 i
11
/ G" s( P: l; X% s
12
) S6 r" n# B* R0 V3 c- s
13
7 [, \! E8 J5 V# H F& H# Z
14
' Y" x3 L0 c ?; p0 r
15
/ x3 U S& j8 C. ]5 ]
16
1 u/ e7 @: E7 S t7 f
17
@# l1 F0 j$ \( R1 ~3 ^/ D
18
0 n& y( h Y, v h
19
8 n6 x: a4 f2 d8 {' g: h/ Z
20
& Q5 }8 A1 s) V( d! X
21
, d3 N. J( N+ R3 a+ P: D
22
2 D# H: ]8 [/ |9 L% b% |
23
7 y0 o4 t/ T* c$ B9 ]! {2 H9 z
24
+ e0 V3 L- N4 V4 Y4 {
25
+ z( W: O9 F/ @ ^( \* s
26
$ s; n3 r& `/ R' F7 W
27
7 m% y3 D$ ? l" v* L1 J
28
% I+ R- v- S0 k1 ~( u
29
& l2 b' v, G+ g$ V' i/ j H* N
30
( x: t3 ~3 i2 ^2 K% u, p; }
31
& B0 M, V) t" h9 h6 w4 F6 l
32
& M, x% N" o- Y$ t2 i. Q+ N, B5 x% D
33
, t N7 t* n" `! _. s9 k3 _. O& T
34
5 a: E0 @/ h+ N `# p2 n
35
% i1 [1 _9 L8 L
36
: X# e3 h$ f( w1 w$ I
37
! R& Q8 P- j: y# D1 O4 w$ `, K3 v8 _
38
- z T: ]% V. O7 S3 s7 I; W
39
1 t9 n6 I6 [1 c. ]! t, s# W6 z+ @
40
) P) A' [$ U4 f7 Z; \/ i* {
41
2 r. o, x1 x X% }
42
0 s0 n! J" n) U0 m% V9 F8 x3 s
43
- V) U% T. N. q5 R
44
7 _% D* w# D: a
45
5 E* _% O- }# A! P: ~6 b' K
46
9 r# C T$ K5 D' w
47
9 X* S7 T( u. F/ y _
48
, e6 K9 `! X1 F3 q6 n5 w
49
4 v; @& |, g4 q+ G* h& @5 }
50
% o4 }3 m/ W" K9 \6 j, C2 v
51
, L: h" }! y5 [" A& s2 |9 R5 J) Z
52
4 W5 O9 S% V2 o8 h5 x; F
53
]# W7 t! D. p. g
54
$ T7 w: g, b: r# q) f+ D
55
' f0 `6 T* C9 Z$ }4 o3 C8 h- g
56
# d2 t k' g8 h
57
' g4 ~9 u% I2 `: C" k v9 s' a) ]
58
$ C, T3 S, S- s' k" V
59
, W4 o5 K+ V; p, i
60
0 I/ h, r" p4 u. n- R( @6 ?
61
7 K2 S. L" ]+ B( y0 O) V3 a; _. d
62
* m0 V% l( R N/ O/ C& W @" i
63
2 Y; Y" X" B, n3 D) y7 @
64
7 r# ` c$ T# N: ?4 M
65
7 }/ E }4 e+ G! w3 `
66
8 c4 K3 M" O3 a0 l' Y
67
. n6 a6 H8 \1 |3 i5 l" |) n
68
6 R$ }; L/ n! K& R' w* s; `+ T: g- ^
69
0 @3 j i2 X8 f3 b
70
9 L6 F: @# k9 x; a/ s3 C
71
+ b+ Z0 u2 T( g5 q" I; [3 E3 H
72
1 O3 T% U: _$ L0 T: i" y* C
73
) ^8 @/ j7 p# c& v. G# V, {: b
74
0 g* k4 y+ `7 M2 }1 W( j
75
. l3 p5 p5 i* S" r# V+ W
76
3 ^* |; b5 n) h) e/ o, d/ i) l& m$ M
77
, |8 {: h; Q$ g2 T1 g+ }* D
78
( Q M+ g- F6 M5 S1 y7 h, K
79
5 X3 ~# x4 C2 s3 q ?6 z3 `
80
0 {: u5 g/ ` a) \* {( o( S" W
81
# \6 ~/ L: P+ ^8 E( m$ V) o( R1 t- K, b
82
) r- V3 J) ~2 d# J7 b" Z
83
9 a2 s: K: i$ t( {, y- I" Z" p
84
- H6 q; ^/ n" i3 {8 Z
85
# @3 L$ k! x% z$ j& ^
86
8 ?, P6 O$ u7 [( i2 T% h
87
. V( n& s+ r& r& j( r$ \' d9 [5 Z* r
88
! r7 i8 e+ X7 I) B% e9 s9 @
89
3 Y2 k7 u; s8 e1 O% k! Y
90
+ i. Y/ t& ~& ~' Z2 P4 n3 D
91
. _: p5 r6 c5 }4 r) A6 ~; f [
92
* P6 n- y O, K% |
93
1 L$ j1 [$ ~& @& Q! Z; b, _/ J# x
94
2 {& m3 U: B1 J
95
/ s7 A! t1 z1 l, Q1 {1 M: }1 r$ u
96
3 Q; A$ i. m* S! L& ^8 X
97
" i( J! Z0 n5 d+ Q( { i9 L
98
) c. q: Q. c$ E. w1 y( {2 [
99
2 s0 m7 D! U) ^; \ H( z8 T
100
1 l' \: f3 O% p- W
101
: K5 D; U4 L7 ~$ }8 \/ F
102
( |& ^5 x2 K) A3 g% {
103
# }' g* V# }6 ]9 y
104
5 f% b9 l! G& X/ f
105
6 I9 ?3 M- h# x+ h9 g$ J7 E8 X
106
, O- d" [2 z4 B |# O( |
107
* i" J- E" I8 y. K& y
108
7 W" W- k# T! ?# T- o# A k; K
109
5 n% f! N& p9 P9 U+ ?7 Q
110
, {7 R; @3 U# q9 F* {
111
, h( e# m% B ]/ P& l
112
% j' s! w+ a2 G" _" _
113
3 K( u1 s/ e- Z+ A
114
: F7 \; \( X# m
115
* G; m+ d, l3 U, r8 n
116
* |! e, C" h/ W4 ?
117
* a! K" I0 Y% o
118
! }4 o: R3 m; u# O0 S0 L
119
+ k k& U5 t0 E( P5 f
120
T6 u# r! T) p4 D8 f
121
2 w# ~) F1 ^3 {7 t& _
122
: y+ m7 N3 ~ F0 c
123
: s. H" Z) g: e; t
124
) F4 i6 }$ ]0 j) Z; V* R: l
125
. n$ N; |) }+ c# j G% f+ C$ o
126
9 t0 i5 Q' R6 }* x# N
127
: C: d# @; r$ v1 I4 }+ X
128
' L& e2 c0 L1 g f# l# V
129
2 ~" ?/ [# o. v E* S/ @% }# b
130
7 o4 w& U% Y* n8 b
131
9 f' K2 _) K2 n1 \
132
3 @( q+ [9 b8 ` d( J
133
5 ~' F: n. b9 W: G; |
134
( h2 t: ~5 q3 D8 ?0 S1 z! L# j
135
1 j- k, R0 z& ~+ |5 r: j
136
. Y* n0 x+ F2 ^: R5 r
137
# H3 k1 U1 G3 t& {# F% O2 P
138
+ Z3 O; g; F" f* p5 S
139
9 P8 Z2 b4 `3 {# f+ C* {2 x
140
: g- ?7 q6 ]0 V- w0 J
141
3 @0 O- s/ u- i* g' f7 N- f) a9 w
142
4 ]3 c$ u! g) b* O/ \
143
5 H( X1 V' C1 s( a/ v3 Z
144
/ [+ \% u7 W. c: `4 ~
145
2 j' V \. ` H( @' N* ~
146
& U) ?9 Y* `2 a! ~% _2 B# O$ t
147
5 M* r0 C6 [9 [* o8 n
148
7 G" a* e, ^! b
149
8 D+ \% q: B6 {; _: V1 Q* _
150
# |" x7 w0 j8 F7 L
151
# ^( x- l* l5 E& ?" ]* m k
152
) K: e5 k4 w$ L8 m2 _7 W
153
+ r6 l- [9 c! a
154
9 S; X7 A- I H; [! J
155
9 n& @1 k" Q# R7 K! b$ s; k9 G
156
. B0 c9 F4 ^3 Y# i0 Q. M: _5 Y
输出如下:
% j" t" Z% N5 e! F8 |) ]7 K& q' ^
( J) @. t8 d& c1 y
; o' i' a0 ~% L* Z; P8 g& {) ^
torch.Size([3, 480, 480])
& X4 y& f' y9 Z! U4 }8 d x4 f# Y/ a
{'boxes': tensor([[130.8000, 97.8000, 327.6000, 292.2000],
. s4 P' v) ^' Q, j* \+ h
[159.0000, 268.8000, 349.8000, 427.8000],
6 [3 K% g. Z1 O
[ 0.0000, 282.0000, 118.2000, 429.6000],
# B/ D2 i) ~5 |" r: [# r3 v
[ 43.8000, 107.4000, 199.2000, 280.2000],
7 b% i z" Z# U4 D! r7 n
[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])}
2 T' P# O$ a/ v5 k6 B
1
& W( o0 |- k2 T, M
2
2 X. W& i% S" w" r2 _+ A* j
3
( V% Q. P: }0 W w
4
% ^* F2 [$ y8 w( k
5
A9 k0 v- k2 x# o, n: Q% z+ K: s- |
6
: f, m0 D4 q& R) F4 ^5 S& u; w
* @# G! K! a% G y/ [
4 X, G$ d& W5 D& b3 i* N
( ]2 D* X3 I: g
$ x2 z! M) l3 d0 E3 l$ q/ y. s' T) R& m
下载地址
~$ V: \) C. ?2 i8 S& J. y
链接:https://pan.baidu.com/s/1QZDgeYTHyAlD2xhtJqZ-Yw
, O( N# e7 O7 B5 j
提取码:srjn
- |9 E4 }. A1 P
————————————————
6 E; W" q/ M$ Y8 n* x4 \0 F
版权声明:本文为CSDN博主「刘润森!」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
m- F" Z$ R3 U W) C
原文链接:https://blog.csdn.net/weixin_44510615/article/details/118496273
3 D T, Y6 z9 J6 N [1 d! Z2 `; K
3 |& i, Q# @& n" l; d; V9 ]
! }0 k$ m# ~) z F- O+ N
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5