Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ec258bf

Browse files
committed
iaa数据增强
1 parent f589296 commit ec258bf

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed
Loading

线上数据增强imgaug/list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0_51_Dresses_wearingdress_51_377_0.png 0.0885083825019613 0.20997702626503661 0.10639608854030465 0.25548294897358786 0.1156312312042364 0.30337741484701886 0.11000808013532949 0.35177791068743464 0.09992738348669587 0.3996145336199006 0.09687996708698353 0.448392221618397 0.10177439925062108 0.4969836119328583 0.11616375655808708 0.5436755064641082 0.13573235547692208 0.5884961243952668 0.15708655193759807 0.6325037489376307 0.17954538257550995 0.6759568218406773 0.20275559684721495 0.7190153369344927 0.22458856175634154 0.7627766501454629 0.24358677564804523 0.8078506022816423 0.26553019120603427 0.8514915051320606 0.2994383488738886 0.8863036343243331 0.34426656427742547 0.905014995750523 0.41143402993429656 0.904695518844796 0.47679674475761635 0.887883525513206 0.5398757487660173 0.8637623806877615 0.6020233281985486 0.8372941914961428 0.6629739545878007 0.808208130393567 0.720325805153308 0.7726321679278897 0.7703400296646182 0.7274089478049817 0.8104811153651281 0.6731986201457898 0.8405565078288442 0.6127993531805701 0.8613277419341658 0.5485699386277458 0.875740977011964 0.4825888917037134 0.8872600220237317 0.41602928768142 0.8974952857364671 0.3492604020250392 0.9057656770969534 0.28222145495554396 0.912063279411284 0.21496684182139122 0.9173093660107218 0.1476206759528635 0.0840376849952602 0.14829704252745815 0.10764852228523797 0.10126160178723195 0.1347259138418541 0.11636607916285303 0.16502725130344534 0.13154180678363625 0.1992426916146378 0.15204183027834076 0.19938282986565115 0.18226987447698745 0.1649999897849111 0.16942261252941945 0.1329895402597084 0.15864224613461037 0.10699590579236401 0.14920510008744117 0.33944351004756146 0.13965060820639383 0.4136192928298248 0.11499374581181354 0.4939853556485356 0.09573019498561715 0.5717427401363101 0.09954604144874477 0.6633368456214043 0.14362759769711037 0.5710940819903897 0.12897711717932792 0.49315269821358526 0.13357097035172594 0.4147720017692534 0.1581359607904027 0.3396150596969796 0.17961714357511768 0.2843995672888337 0.2612719675487055 0.27263165717344406 0.34170953598980125 0.2638164823524124 0.42215387192730125 0.24054912742710513 0.49059496763859833 0.2069163302497385 0.5214749260427562 0.24675530469567208 0.5337558889987578 0.288239802276739 0.5332414315834205 0.3367609000106237 0.5220229655628923 0.385994436351824 0.5147365426418672 0.11486612104471758 0.2780711920191553 0.11882146731580151 0.24821389170371339 0.1414033259307989 0.23057358234996078 0.1953716836713847 0.2375037540451752 0.23631720762372516 0.2737046245750523 0.1912219314894417 0.2878207282541187 0.1434539411856041 0.293000544464239 0.1282263081442861 0.28746154019024583 0.40344557502778505 0.26955653234505755 0.43087589790631536 0.23874246126438284 0.4691085496208159 0.22437825861336297 0.5219829990275235 0.23388991096528505 0.5729221998398274 0.25241471933021703 0.5264870871061061 0.2761134702291449 0.47569523895135984 0.2852881523355779 0.4393399290459924 0.27855921789193255 0.21814118070083682 0.6402741116958682 0.22741335306207505 0.6058980391115324 0.2642363225067011 0.6044979334875131 0.2938455238501896 0.6062975767766082 0.3308172505270986 0.6009817721953452 0.4262184558054393 0.6068850720776674 0.5220561646018567 0.6084555143092966 0.4513672385754446 0.6566068896688677 0.3733126588446326 0.6908674040598849 0.2898784940711624 0.7071479573908211 0.2553494505303674 0.6986364899320083 0.23048732869295893 0.6738099932171809 0.22610039491533734 0.6411255393566946 0.26013675194903896 0.645261756545829 0.294367004139154 0.6469429048035434 0.3939403071064331 0.6364537961313416 0.4926116177227707 0.6193356055096103 0.393806936351824 0.6364710340938807 0.2940980919235421 0.6469460970188284 0.2600028704399843 0.6452489876846889 0.16176199813268174 0.25923597263990583 0.47691051531037526 0.24798060461068253 0 0 1 0 0 0 8.832234 16.723196 1.6386904

线上数据增强imgaug/mian.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# @Time : 2020/4/14 11:32
2+
# @Author : Libuda
3+
# @FileName: mian.py
4+
# @Software: PyCharm
5+
import numpy as np
6+
import imgaug.augmenters as iaa
7+
import cv2
8+
import imageio
9+
import imgaug as ia
10+
import matplotlib.pyplot as plt
11+
12+
13+
def test1():
14+
images = np.zeros((2, 128, 128, 3), dtype=np.uint8) # two example images
15+
images[:, 64, 64, :] = 255
16+
points = [
17+
[(10.5, 20.5)], # points on first image
18+
[(50.5, 50.5), (60.5, 60.5), (70.5, 70.5)] # points on second image
19+
]
20+
21+
seq = iaa.Sequential([
22+
iaa.AdditiveGaussianNoise(scale=0.05 * 255),
23+
iaa.Affine(translate_px={"x": (1, 5)})
24+
])
25+
26+
# augment keypoints and images
27+
images_aug, points_aug = seq(images=images, keypoints=points)
28+
29+
print("Image 1 center", np.argmax(images_aug[0, 64, 64:64 + 6, 0]))
30+
print("Image 2 center", np.argmax(images_aug[1, 64, 64:64 + 6, 0]))
31+
print("Points 1", points_aug[0])
32+
print("Points 2", points_aug[1])
33+
34+
35+
def test2():
36+
image = imageio.imread("http://wx4.sinaimg.cn/large/006HcH9cgy1g2ywu5fooxj30oy0gimz1.jpg") # 读取图片数据
37+
38+
seq = iaa.Sequential([ # 定义一个sequential,把要进行的图片操作(3个操作)放在里面
39+
iaa.Affine(rotate=(-25, 25)),
40+
iaa.AdditiveGaussianNoise(scale=(10, 30)),
41+
iaa.Crop(percent=(0, 0.4))
42+
], random_order=True) # 这3个图片操作以随机顺序作用在图片上
43+
44+
images_aug = [seq.augment_image(image) for _ in range(8)] # 应用data augmentation
45+
ia.imshow(ia.draw_grid(images_aug, cols=4, rows=2)) # 显示图片操作效果
46+
47+
# for index, one in enumerate(images_aug):
48+
# cv2.imwrite(str(index) + ".jpg", one)
49+
50+
51+
def test3():
52+
file_list = "list"
53+
with open(file_list, 'r') as f:
54+
lines = f.readlines()[:1]
55+
for line in lines:
56+
line = line.strip().split()
57+
path = line[0]
58+
landmark = line[1:197]
59+
attribute = line[197:203]
60+
euler_angle = line[203:206]
61+
62+
image = imageio.imread("http://wx4.sinaimg.cn/large/006HcH9cgy1g2ywu5fooxj30oy0gimz1.jpg") # 读取图片数据
63+
# ia.imshow(image)
64+
65+
keypoints = ia.KeypointsOnImage([
66+
ia.Keypoint(x=65, y=100),
67+
ia.Keypoint(x=75, y=200),
68+
ia.Keypoint(x=100, y=100),
69+
ia.Keypoint(x=200, y=80)
70+
], shape=image.shape)
71+
72+
# landmark = np.asarray(landmark, dtype=np.float32)
73+
74+
seq = iaa.Sequential([ # 定义一个sequential,把要进行的图片操作(3个操作)放在里面
75+
iaa.Affine(rotate=(-20, 20)),
76+
# iaa.AdditiveGaussianNoise(scale=(10, 30)),
77+
# iaa.Crop(percent=(0, 0.4))
78+
], random_order=True) # 这3个图片操作以随机顺序作用在图片上
79+
80+
# augment keypoints and images
81+
images_aug, points_aug = seq(images=image, keypoints=keypoints)
82+
83+
# images_aug = [seq.augment_image(image) for _ in range(8)] # 应用data augmentation
84+
85+
# for index,one in enumerate(images_aug):
86+
# cv2.imwrite(str(index)+".jpg",one)
87+
# images_aug,points_aug = [seq(images=image, keypoints=keypoints) for _ in range(8)] # 应用data augmentation
88+
89+
ia.imshow(images_aug) # 显示图片操作效果
90+
91+
92+
def test4():
93+
# image = ia.quokka(size=(256, 256))
94+
file_list = "list"
95+
with open(file_list, 'r') as f:
96+
lines = f.readlines()[:1]
97+
landmarks = []
98+
for line in lines:
99+
line = line.strip().split()
100+
path = line[0]
101+
landmark = line[1:197]
102+
attribute = line[197:203]
103+
euler_angle = line[203:206]
104+
105+
image = imageio.imread(path) # 读取图片数据
106+
107+
h, w, _ = image.shape
108+
109+
# 定义关键点
110+
landmark = np.asarray(landmark, dtype=np.float32)
111+
112+
lad_ls = []
113+
114+
# 变成绝对位置
115+
for x, y in landmark.reshape(-1, 2) * [h, w]:
116+
lad_ls.append(ia.Keypoint(x, y))
117+
118+
# 相对位置
119+
# for x,y in landmark.reshape(-1, 2):
120+
# lad_ls.append(ia.Keypoint(x,y))
121+
122+
keypoints = ia.KeypointsOnImage(lad_ls, shape=image.shape)
123+
124+
# 定义一个变换序列
125+
seq = iaa.Sequential([
126+
iaa.Multiply((1.2, 1.5)), # 改变亮度,不影响关键点
127+
iaa.Affine(
128+
rotate=(-20, 20),
129+
# scale=(0.7, 0.9) # 旋转10度然后缩放,会影响关键点
130+
),
131+
# iaa.Fliplr(0.5),
132+
iaa.GaussianBlur(sigma=(0, 3.0)),
133+
# iaa.Crop(percent=(0, 0.4))
134+
], random_order=True)
135+
136+
# 固定变换序列,之后就可以先变换图像然后变换关键点,这样可以保证两次的变换完全相同。
137+
# 如果调用次函数,需要在每次batch的时候都调用一次,否则不同的batch执行相同的变换。
138+
seq_det = seq.to_deterministic()
139+
140+
# 转换成list或者batch来变换。由于只有一张图片, 因此用[0]来取出该图和关键点。
141+
image_aug = seq_det.augment_images([image])[0]
142+
keypoints_aug = seq_det.augment_keypoints([keypoints])[0]
143+
144+
# print coordinates before/after augmentation (see below)
145+
# use after.x_int and after.y_int to get rounded integer coordinates
146+
for i in range(len(keypoints.keypoints)):
147+
before = keypoints.keypoints[i]
148+
before = keypoints.get_coords_array() / 112
149+
after = keypoints_aug.keypoints[i]
150+
after = keypoints_aug.get_coords_array() / 112
151+
# after_new = keypoints_aug.get_coords_array()
152+
print("Keypoint %d: (%.16f, %.16f) -> (%.16f, %.16f)" % (
153+
i, before[i][0], before[i][1], after[i][0], after[i][1])
154+
)
155+
156+
# keypoints = keypoints.get_coords_array()*[h,w]
157+
# keypoints = ia.KeypointsOnImage.from_coords_array(keypoints,image.shape)
158+
#
159+
# keypoints_aug = keypoints_aug.get_coords_array()*[h,w]
160+
# keypoints_aug = ia.KeypointsOnImage.from_coords_array(keypoints_aug,image.shape)
161+
# 将关键点画在图片上。
162+
# image with keypoints before/after augmentation (shown below)
163+
image_before = keypoints.draw_on_image(image, size=1)
164+
image_after = keypoints_aug.draw_on_image(image_aug, size=1)
165+
166+
fig, axes = plt.subplots(2, 1, figsize=(20, 15))
167+
plt.subplots_adjust(left=0.2, bottom=0.2, right=0.8, top=0.8, hspace=0.3, wspace=0.0)
168+
axes[0].set_title("image before")
169+
axes[0].imshow(image_before)
170+
axes[1].set_title("image after augmentation")
171+
axes[1].imshow(image_after)
172+
173+
plt.show()
174+
175+
# 增强n次 后显示图片
176+
# images_aug = [seq.augment_image(image) for _ in range(8)] # 应用data augmentation
177+
# ia.imshow(ia.draw_grid(images_aug, cols=4, rows=2)) # 显示图片操作效果
178+
179+
180+
if __name__ == '__main__':
181+
import time
182+
183+
for i in range(10):
184+
test4()
185+
time.sleep(5)

0 commit comments

Comments
 (0)