-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpaper_mtl.py
executable file
·234 lines (211 loc) · 8.95 KB
/
paper_mtl.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import math
import os
from copy import copy
import matplotlib.pyplot as plt
import numpy as np
import torch
from torch.nn import functional as F
import pdb
def pad(img, pos):
'''
:param img:
:param bk_image:
:return: pasted img
paste 2d img rasterized from mesh onto background imgs
'''
i_h, i_w = img.shape[2:]
h_pad_len = 416 - i_h
w_pad_len = 416 - i_w
paste_imgs = []
# paste
# h_pos = rd.randint(0, h_pad_len)
h_pos = int(h_pad_len * 0.75)
w_pos = int(w_pad_len * 0.5)
# w_pos = rd.randint(0, w_pad_len)
h_top = h_pad_len
h_bottom = 0
w_top = int(pos)
w_bottom = w_pad_len - w_top
# h_top = int((img_size - i_h) / 2) if (img_size - i_h) % 2 == 0 else int((img_size - i_h) / 2) + 1
# h_bottom = int((img_size - i_h) / 2)
# w_top = int((img_size - i_w) / 2) if (img_size - i_h) % 2 == 0 else int((img_size - i_h) / 2) + 1
# w_bottom = int((img_size - i_w) / 2)
# TODO:padiing img
dim = (w_top, w_bottom, h_top, h_bottom)
img = F.pad(img, dim, 'constant', value=0.)
return img
plt.yticks([])
plt.xticks([])
plt.axis('off')
# texture_aug = torch.zeros()
def augment(img, number = 4):
# pdb.set_trace()
img = img.unsqueeze(0)
i_h, i_w = img.shape[2:]
size = torch.linspace(0.4, 1, 5)[0:]
aug_imgs = []
for scale in size:
rots = torch.linspace(-20, 20, number)
# print(rots)
poses = torch.linspace(416, 0, number + 2)
print(poses)
board = torch.zeros(1, 3, 416, 416)
for pi, (rot, pos) in enumerate(zip(rots, poses[2:])):
print(poses[2:])
# print(rot, pos)
image = img.clone()
pallete = torch.zeros(image.shape)
channel = pi % 3
pallete[:, channel, int(i_h * 0.6):, :] = 1.
color_aug = torch.where(image != 0. , pallete, image)
image = torch.where(color_aug != 0., color_aug, image)
image = F.interpolate(image, (int(scale * i_h), int(scale * i_w)), mode='bilinear')
angle = float(rot) * math.pi / 180
theta = torch.tensor([
[math.cos(angle), math.sin(-angle), 0],
[math.sin(angle), math.cos(angle), 0]
], dtype=torch.float)
# blank = torch.ones(target.shape)
grid = F.affine_grid(theta.unsqueeze(0), image.size())
output = F.grid_sample(image, grid)
output = pad(output, pos)
board = torch.where(output == 0., board, output)
plt.imshow(board[0].numpy().transpose(1, 2, 0))
plt.savefig('/home/zhouge/Documents/aug{}.pdf'.format(scale), bbox_inches='tight')
aug_imgs.append(board)
# def logo_augment(logo):
# logo
cur_dir = os.path.dirname(os.path.realpath(__file__))
image_file = os.path.join(cur_dir, 'image/mtl')
# for file_name in os.listdir(image_file):
# file_path = os.path.join(image_file, file_name)
#
# target, logo, syn = torch.load(file_path)
# np_blank = np.ones(target.shape, dtype=np.float32)
# # target = np.where(target == 0., np_blank, target)
# # plt.imshow(target[0].transpose(1,2,0))
# # plt.savefig('/home/zhouge/Documents/target.pdf', bbox_inches = 'tight')
# # logo = np.where(logo == 0., np_blank, logo)
# # plt.imshow(logo[0].transpose(1,2,0))
# # plt.savefig('/home/zhouge/Documents/logo.pdf', bbox_inches = 'tight')
# # transform = transforms.ToTensor()
# # image = transform(image)
# blank = torch.ones(target.shape)
#
# target = torch.from_numpy(target).squeeze(0)
#
# logo = torch.from_numpy(logo).squeeze(0)
# syn = torch.from_numpy(syn).squeeze(0)
# noise = torch.FloatTensor(logo.size()).uniform_(-1, 1)
# target = torch.where(target == 0., blank, target).squeeze(0)
# syn = torch.where(syn == 0., blank, syn).squeeze(0)
# noise_image = torch.add(noise, logo)
# noise_image = torch.where(logo == 0., logo, noise_image).squeeze(0)
# logo = torch.where(logo == 0., blank, logo).squeeze(0)
# noise_image = torch.where(noise_image == 0., blank, noise_image).squeeze(0)
# # plt.imshow(target.numpy().transpose(1,2,0))
# # plt.savefig('/home/zhouge/Documents/target.pdf', bbox_inches = 'tight')
# # plt.imshow(logo.numpy().transpose(1,2,0))
# # plt.savefig('/home/zhouge/Documents/logo.pdf', bbox_inches = 'tight')
# # image = torch.where(logo == 0., target, logo)
#
# # augment(image)
#
# # aug_img = copy(image)
# # aug_img[2, 150:, :] = 1.
# # plt.imshow(aug_img.numpy().transpose(1, 2, 0))
# # plt.savefig('/home/zhouge/Documents/aug_img.pdf', bbox_inches = 'tight')
# # image = torch.where(image == 0., blank, image.unsqueeze(0)).squeeze(0)
# # plt.imshow(image.numpy().transpose(1,2,0))
# # plt.savefig('/home/zhouge/Documents/merge.pdf', bbox_inches = 'tight')
# angle = -30 * math.pi / 180
# theta = torch.tensor([
# [math.cos(angle), math.sin(-angle), 0],
# [math.sin(angle), math.cos(angle), 0]
# ], dtype=torch.float)
# # blank = torch.ones(target.shape)
# grid = F.affine_grid(theta.unsqueeze(0), syn.unsqueeze(0).size())
# output = F.grid_sample(syn.unsqueeze(0), grid)
#
# output = torch.where(output == 0., blank, output)
# new_img_torch = output[0]
# # print(new_img_torch)
# plt.imshow(new_img_torch.numpy().transpose(1, 2, 0))
# plt.savefig('/home/zhouge/Documents/{}_{}.pdf'.format(file_name, 'aug'), bbox_inches='tight')
# plt.imshow(syn.numpy().transpose(1, 2, 0))
# plt.savefig('/home/zhouge/Documents/{}_{}.png'.format(file_name, 'syn'), bbox_inches='tight')
# plt.imshow(target.numpy().transpose(1, 2, 0))
# plt.savefig('/home/zhouge/Documents/{}_{}.png'.format(file_name, 'target'), bbox_inches='tight')
# plt.imshow(logo.numpy().transpose(1, 2, 0))
# plt.savefig('/home/zhouge/Documents/{}_{}.png'.format(file_name, 'logo'), bbox_inches='tight')
# plt.imshow(noise_image.numpy().transpose(1, 2, 0))
# plt.savefig('/home/zhouge/Documents/{}_{}.png'.format(file_name, 'noise_logo'), bbox_inches='tight')
# # plt.savefig('/home/zhouge/Documents/aug_img.jpg', bbox_inches='tight')
# # plt.show()
img_dir = '/home/zhouge/Downloads/park_adv/selection'
def image_process(imgdir):
import cv2
count = 0
# for i in range(1, 3 + 1):
# for j in range(2, 6 + 1):
# count += 1
# file_name = '{}_{}.png'.format(j, i)
# print(file_name)
# print(i * j - i + j)
# file_path = os.path.join(imgdir, file_name)
# img = cv2.imread(file_path)
# # plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# # plt.savefig(img_dir + '/{}_{}'.format(j, i) + '.pdf', bbox_inches='tight')
# plt.subplot(3, 5, count)
# plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# plt.axis('off')
# plt.subplots_adjust(left=0.125, right=0.9, bottom=0.1, top=0.9, wspace=0.2, hspace=0.2)
# plt.savefig(img_dir + '/mtl.pdf', bbox_inches='tight')
for file in os.listdir(imgdir):
filename = file.split('.')[0]
file_path = os.path.join(imgdir, file)
# print(file_path)
# file_name = '{}_{}.png'.format(j, i)
# print(file_name)
# print(i * j - i + j)
# file_path = os.path.join(imgdir, file_name)
img = cv2.imread(file_path)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.savefig(imgdir + '/' + filename + '.pdf', bbox_inches='tight')
# image_process(img_dir)
mtl_dir = '/home/zhouge/Documents/3dmesh'
def prepare_mtl(datadir):
import numpy as np
import cv2
mtldir = datadir + '/mtl'
count = 0
# for i in range(1, 3 + 1):
# for j in range(2, 6 + 1):
# count += 1
# file_name = '{}_{}.png'.format(j, i)
# print(file_name)
# print(i * j - i + j)
# file_path = os.path.join(imgdir, file_name)
# img = cv2.imread(file_path)
# # plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# # plt.savefig(img_dir + '/{}_{}'.format(j, i) + '.pdf', bbox_inches='tight')
# plt.subplot(3, 5, count)
# plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# plt.axis('off')
# plt.subplots_adjust(left=0.125, right=0.9, bottom=0.1, top=0.9, wspace=0.2, hspace=0.2)
# plt.savefig(img_dir + '/mtl.pdf', bbox_inches='tight')
blank = np.ones((256, 256, 3), dtype=np.uint8) * 255
for file in os.listdir(mtldir):
filename = file.split('.')[0]
file_path = os.path.join(mtldir, file)
print(file_path)
# file_name = '{}_{}.png'.format(j, i)
# print(file_name)
# print(i * j - i + j)
# file_path = os.path.join(imgdir, file_name)
img = cv2.imread(file_path)
print(img.dtype)
img = np.where(img == 0, blank, img)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.savefig(datadir + '/pdf/' + filename + '.png', bbox_inches='tight')
prepare_mtl(mtl_dir)