-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoordconv.py
289 lines (224 loc) · 10.7 KB
/
coordconv.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# repo: https://github.com/walsvid/CoordConv/blob/master/coordconv.py
import torch
import torch.nn as nn
import torch.nn.modules.conv as conv
class AI8XCoordConv2D:
"""
Fold data to increase the number of channels. An interlaced approach used in this folding
as explained in [1].
[1] https://arxiv.org/pdf/2203.16528.pdf
"""
def __init__(self, with_r=False):
self.with_r = with_r
def __call__(self, input_tensor):
channel_in_shape, dim_y, dim_x = input_tensor.shape
xx_ones = torch.ones([1, 1, dim_x], dtype=torch.int32)
yy_ones = torch.ones([1, 1, dim_y], dtype=torch.int32)
xx_range = torch.arange(dim_y, dtype=torch.int32)
yy_range = torch.arange(dim_x, dtype=torch.int32)
xx_range = xx_range[None, :, None]
yy_range = yy_range[None, :, None]
xx_channel = torch.matmul(xx_range, xx_ones)
yy_channel = torch.matmul(yy_range, yy_ones)
# transpose y
yy_channel = yy_channel.permute(0, 2, 1)
xx_channel = xx_channel.float() / (dim_y - 1)
yy_channel = yy_channel.float() / (dim_x - 1)
xx_channel = xx_channel * 2 - 1
yy_channel = yy_channel * 2 - 1
xx_channel = xx_channel.repeat(1, 1, 1)
yy_channel = yy_channel.repeat(1, 1, 1)
out = torch.cat([input_tensor, xx_channel, yy_channel], dim=0)
if self.with_r:
rr = torch.sqrt(torch.pow(xx_channel - 0.5, 2) + torch.pow(yy_channel - 0.5, 2))
out = torch.cat([out, rr], dim=0)
return out
class AddCoords(nn.Module):
def __init__(self, rank, with_r=False, use_cuda=True):
super(AddCoords, self).__init__()
self.rank = rank
self.with_r = with_r
self.use_cuda = use_cuda
def forward(self, input_tensor):
"""
:param input_tensor: shape (N, C_in, H, W)
:return:
"""
if self.rank == 1:
batch_size_shape, channel_in_shape, dim_x = input_tensor.shape
xx_range = torch.arange(dim_x, dtype=torch.int32)
xx_channel = xx_range[None, None, :]
xx_channel = xx_channel.float() / (dim_x - 1)
xx_channel = xx_channel * 2 - 1
xx_channel = xx_channel.repeat(batch_size_shape, 1, 1)
if torch.cuda.is_available and self.use_cuda:
input_tensor = input_tensor.cuda()
xx_channel = xx_channel.cuda()
out = torch.cat([input_tensor, xx_channel], dim=1)
if self.with_r:
rr = torch.sqrt(torch.pow(xx_channel - 0.5, 2))
out = torch.cat([out, rr], dim=1)
elif self.rank == 2:
batch_size_shape, channel_in_shape, dim_y, dim_x = input_tensor.shape
xx_ones = torch.ones([1, 1, 1, dim_x], dtype=torch.int32)
yy_ones = torch.ones([1, 1, 1, dim_y], dtype=torch.int32)
xx_range = torch.arange(dim_y, dtype=torch.int32)
yy_range = torch.arange(dim_x, dtype=torch.int32)
xx_range = xx_range[None, None, :, None]
yy_range = yy_range[None, None, :, None]
xx_channel = torch.matmul(xx_range, xx_ones)
yy_channel = torch.matmul(yy_range, yy_ones)
# transpose y
yy_channel = yy_channel.permute(0, 1, 3, 2)
xx_channel = xx_channel.float() / (dim_y - 1)
yy_channel = yy_channel.float() / (dim_x - 1)
xx_channel = xx_channel * 2 - 1
yy_channel = yy_channel * 2 - 1
xx_channel = xx_channel.repeat(batch_size_shape, 1, 1, 1)
yy_channel = yy_channel.repeat(batch_size_shape, 1, 1, 1)
if torch.cuda.is_available and self.use_cuda:
input_tensor = input_tensor.cuda()
xx_channel = xx_channel.cuda()
yy_channel = yy_channel.cuda()
out = torch.cat([input_tensor, xx_channel, yy_channel], dim=1)
if self.with_r:
rr = torch.sqrt(torch.pow(xx_channel - 0.5, 2) + torch.pow(yy_channel - 0.5, 2))
out = torch.cat([out, rr], dim=1)
elif self.rank == 3:
batch_size_shape, channel_in_shape, dim_z, dim_y, dim_x = input_tensor.shape
xx_ones = torch.ones([1, 1, 1, 1, dim_x], dtype=torch.int32)
yy_ones = torch.ones([1, 1, 1, 1, dim_y], dtype=torch.int32)
zz_ones = torch.ones([1, 1, 1, 1, dim_z], dtype=torch.int32)
xy_range = torch.arange(dim_y, dtype=torch.int32)
xy_range = xy_range[None, None, None, :, None]
yz_range = torch.arange(dim_z, dtype=torch.int32)
yz_range = yz_range[None, None, None, :, None]
zx_range = torch.arange(dim_x, dtype=torch.int32)
zx_range = zx_range[None, None, None, :, None]
xy_channel = torch.matmul(xy_range, xx_ones)
xx_channel = torch.cat([xy_channel + i for i in range(dim_z)], dim=2)
xx_channel = xx_channel.repeat(batch_size_shape, 1, 1, 1, 1)
yz_channel = torch.matmul(yz_range, yy_ones)
yz_channel = yz_channel.permute(0, 1, 3, 4, 2)
yy_channel = torch.cat([yz_channel + i for i in range(dim_x)], dim=4)
yy_channel = yy_channel.repeat(batch_size_shape, 1, 1, 1, 1)
zx_channel = torch.matmul(zx_range, zz_ones)
zx_channel = zx_channel.permute(0, 1, 4, 2, 3)
zz_channel = torch.cat([zx_channel + i for i in range(dim_y)], dim=3)
zz_channel = zz_channel.repeat(batch_size_shape, 1, 1, 1, 1)
if torch.cuda.is_available and self.use_cuda:
input_tensor = input_tensor.cuda()
xx_channel = xx_channel.cuda()
yy_channel = yy_channel.cuda()
zz_channel = zz_channel.cuda()
out = torch.cat([input_tensor, xx_channel, yy_channel, zz_channel], dim=1)
if self.with_r:
rr = torch.sqrt(torch.pow(xx_channel - 0.5, 2) +
torch.pow(yy_channel - 0.5, 2) +
torch.pow(zz_channel - 0.5, 2))
out = torch.cat([out, rr], dim=1)
else:
raise NotImplementedError
return out
class CoordConv1d(conv.Conv1d):
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
padding=0, dilation=1, groups=1, bias=True, with_r=False, use_cuda=True):
super(CoordConv1d, self).__init__(in_channels, out_channels, kernel_size,
stride, padding, dilation, groups, bias)
self.rank = 1
self.addcoords = AddCoords(self.rank, with_r, use_cuda=use_cuda)
self.conv = nn.Conv1d(in_channels + self.rank + int(with_r), out_channels,
kernel_size, stride, padding, dilation, groups, bias)
def forward(self, input_tensor):
"""
input_tensor_shape: (N, C_in,H,W)
output_tensor_shape: N,C_out,H_out,W_out)
:return: CoordConv2d Result
"""
out = self.addcoords(input_tensor)
out = self.conv(out)
return out
class CoordConv2d(conv.Conv2d):
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
padding=0, dilation=1, groups=1, bias=True, with_r=False, use_cuda=True):
super(CoordConv2d, self).__init__(in_channels, out_channels, kernel_size,
stride, padding, dilation, groups, bias)
self.rank = 2
self.addcoords = AddCoords(self.rank, with_r, use_cuda=use_cuda)
self.conv = nn.Conv2d(in_channels + self.rank + int(with_r), out_channels,
kernel_size, stride, padding, dilation, groups, bias)
def forward(self, input_tensor):
"""
input_tensor_shape: (N, C_in,H,W)
output_tensor_shape: N,C_out,H_out,W_out)
:return: CoordConv2d Result
"""
out = self.addcoords(input_tensor)
out = self.conv(out)
return out
class CoordConv3d(conv.Conv3d):
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
padding=0, dilation=1, groups=1, bias=True, with_r=False, use_cuda=True):
super(CoordConv3d, self).__init__(in_channels, out_channels, kernel_size,
stride, padding, dilation, groups, bias)
self.rank = 3
self.addcoords = AddCoords(self.rank, with_r, use_cuda=use_cuda)
self.conv = nn.Conv3d(in_channels + self.rank + int(with_r), out_channels,
kernel_size, stride, padding, dilation, groups, bias)
def forward(self, input_tensor):
"""
input_tensor_shape: (N, C_in,H,W)
output_tensor_shape: N,C_out,H_out,W_out)
:return: CoordConv2d Result
"""
out = self.addcoords(input_tensor)
out = self.conv(out)
return out
if __name__ == "__main__":
import torchvision
from torchvision import transforms, datasets
import matplotlib.pyplot as plt
import numpy as np
# Assuming data_reshape is already imported and defined elsewhere
# Initialize the transformation and the Caltech101 dataset
transform = transforms.Compose([
transforms.Resize((224, 224)), # Resize images to a common size
transforms.ToTensor() # Convert images to tensor
])
# Load Caltech101 dataset
dataset = torchvision.datasets.Caltech101(root='../data/Caltech101', download=True, transform=transform)
# Load a single image and its label
img, label = dataset[0] # Get the first image and label from the dataset
# Initialize your custom reshape class
reshaper = AI8XCoordConv2D() # Example: target to 64x64 image with 9 channels
# Apply the reshaping to the image
reshaped_img = reshaper(img)
print(img.shape)
print(reshaped_img.shape)
# reshaped_img[3]:
# - 1 -1 -1 ...
# -0.99 -0.99 -0.99 ...
# ...
# 0.99 0.99 0.99 ...
# 1 1 1 ...
# reshaped_img[4]:
# - 1 -0.99 ... 0.99 1
# - 1 -0.99 ... 0.99 1
# ...
# - 1 -0.99 ... 0.99 1
# - 1 -0.99 ... 0.99 1
# Function to display images
def show_tensor_images(original, transformed):
fig, ax = plt.subplots(1, 2, figsize=(12, 6))
# Original image
ax[0].imshow(np.transpose(original.numpy(), (1, 2, 0)))
ax[0].set_title('Original Image')
ax[0].axis('off')
# Transformed image
# Since reshaped_img may have more than 3 channels, just show the first 3 for visualization
ax[1].imshow(np.transpose(transformed.numpy()[3:6], (1, 2, 0)))
ax[1].set_title('Transformed Image')
ax[1].axis('off')
plt.show()
# Display the images
show_tensor_images(img, reshaped_img)