Skip to content

Commit 5efbb23

Browse files
authored
Merge pull request #31 from ckkelvinchan/numpy
Change math to numpy
2 parents 3b10260 + 50fd90a commit 5efbb23

16 files changed

+81
-92
lines changed

apps/stylegan_projector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
Ref: https://github.com/rosinality/stylegan2-pytorch/blob/master/projector.py # noqa
66
"""
77
import argparse
8-
import math
98
import os
109

1110
import mmcv
11+
import numpy as np
1212
import torch
1313
import torch.nn.functional as F
1414
from mmcv import Config
@@ -124,7 +124,7 @@ def noise_normalize_(noises):
124124

125125
def get_lr(t, initial_lr, rampdown=0.25, rampup=0.05):
126126
lr_ramp = min(1, (1 - t) / rampdown)
127-
lr_ramp = 0.5 - 0.5 * math.cos(lr_ramp * math.pi)
127+
lr_ramp = 0.5 - 0.5 * np.cos(lr_ramp * np.pi)
128128
lr_ramp = lr_ramp * min(1, t / rampup)
129129
return initial_lr * lr_ramp
130130

mmgen/datasets/samplers/distributed_sampler.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import division
2-
import math
32

3+
import numpy as np
44
import torch
55
from torch.utils.data import DistributedSampler as _DistributedSampler
66

@@ -25,8 +25,10 @@ def __init__(self,
2525
self.samples_per_gpu = samples_per_gpu
2626
# fix the bug of the official implementation
2727
self.num_samples_per_replica = int(
28-
math.ceil(
29-
len(self.dataset) * 1.0 / self.num_replicas / samples_per_gpu))
28+
int(
29+
np.ceil(
30+
len(self.dataset) * 1.0 / self.num_replicas /
31+
samples_per_gpu)))
3032
self.num_samples = self.num_samples_per_replica * self.samples_per_gpu
3133
self.total_size = self.num_samples * self.num_replicas
3234

@@ -43,9 +45,10 @@ def update_sampler(self, dataset, samples_per_gpu=None):
4345
self.samples_per_gpu = samples_per_gpu
4446
# fix the bug of the official implementation
4547
self.num_samples_per_replica = int(
46-
math.ceil(
47-
len(self.dataset) * 1.0 / self.num_replicas /
48-
self.samples_per_gpu))
48+
int(
49+
np.ceil(
50+
len(self.dataset) * 1.0 / self.num_replicas /
51+
self.samples_per_gpu)))
4952
self.num_samples = self.num_samples_per_replica * self.samples_per_gpu
5053
self.total_size = self.num_samples * self.num_replicas
5154

mmgen/datasets/singan_dataset.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import math
2-
31
import mmcv
2+
import numpy as np
43
import torch
54
from torch.utils.data import Dataset
65

@@ -21,33 +20,36 @@ def create_real_pyramid(real, min_size, max_size, scale_factor_init):
2120
scale_factor_init (float): The initial scale factor.
2221
"""
2322

24-
num_scales = math.ceil(
25-
math.log(
26-
math.pow(min_size / min(real.shape[0], real.shape[1]), 1),
27-
scale_factor_init)) + 1
23+
num_scales = int(
24+
np.ceil(
25+
np.log(
26+
np.power(min_size / min(real.shape[0], real.shape[1]), 1),
27+
scale_factor_init))) + 1
2828

29-
scale2stop = math.ceil(
30-
math.log(
31-
min([max_size, max([real.shape[0], real.shape[1]])]) /
32-
max([real.shape[0], real.shape[1]]), scale_factor_init))
29+
scale2stop = int(
30+
np.ceil(
31+
np.log(
32+
min([max_size, max([real.shape[0], real.shape[1]])]) /
33+
max([real.shape[0], real.shape[1]]), scale_factor_init)))
3334

3435
stop_scale = num_scales - scale2stop
3536

3637
scale1 = min(max_size / max([real.shape[0], real.shape[1]]), 1)
3738
real_max = mmcv.imrescale(real, scale1)
38-
scale_factor = math.pow(
39+
scale_factor = np.power(
3940
min_size / (min(real_max.shape[0], real_max.shape[1])),
4041
1 / (stop_scale))
4142

42-
scale2stop = math.ceil(
43-
math.log(
44-
min([max_size, max([real.shape[0], real.shape[1]])]) /
45-
max([real.shape[0], real.shape[1]]), scale_factor_init))
43+
scale2stop = int(
44+
np.ceil(
45+
np.log(
46+
min([max_size, max([real.shape[0], real.shape[1]])]) /
47+
max([real.shape[0], real.shape[1]]), scale_factor_init)))
4648
stop_scale = num_scales - scale2stop
4749

4850
reals = []
4951
for i in range(stop_scale + 1):
50-
scale = math.pow(scale_factor, stop_scale - i)
52+
scale = np.power(scale_factor, stop_scale - i)
5153
curr_real = mmcv.imrescale(real, scale)
5254
reals.append(curr_real)
5355

mmgen/models/architectures/dcgan/generator_discriminator.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import math
2-
1+
import numpy as np
32
import torch
43
import torch.nn as nn
54
from mmcv.cnn import ConvModule, normal_init
@@ -74,7 +73,7 @@ def __init__(self,
7473
self.noise_size = noise_size
7574

7675
# the number of times for upsampling
77-
self.num_upsamples = int(math.log2(output_scale // input_scale))
76+
self.num_upsamples = int(np.log2(output_scale // input_scale))
7877

7978
# output 4x4 feature map
8079
self.noise2feat = ConvModule(
@@ -244,7 +243,7 @@ def __init__(self,
244243
self.base_channels = base_channels
245244

246245
# the number of times for downsampling
247-
self.num_downsamples = int(math.log2(input_scale // output_scale))
246+
self.num_downsamples = int(np.log2(input_scale // output_scale))
248247

249248
# build up downsampling backbone (excluding the output layer)
250249
downsamples = []

mmgen/models/architectures/lsgan/generator_discriminator.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import math
2-
1+
import numpy as np
32
import torch
43
import torch.nn as nn
54
from mmcv.cnn import ConvModule
@@ -74,7 +73,7 @@ def __init__(self,
7473
'act', build_activation_layer(default_act_cfg))
7574

7675
# the number of times for upsampling
77-
self.num_upsamples = int(math.log2(output_scale // input_scale)) - 2
76+
self.num_upsamples = int(np.log2(output_scale // input_scale)) - 2
7877

7978
# build up convolution backbone (excluding the output layer)
8079
self.conv_blocks = nn.ModuleList()
@@ -254,7 +253,7 @@ def __init__(self,
254253
act_cfg=default_act_cfg))
255254

256255
# the number of times for downsampling
257-
self.num_downsamples = int(math.log2(input_scale // output_scale)) - 1
256+
self.num_downsamples = int(np.log2(input_scale // output_scale)) - 1
258257

259258
# build up downsampling backbone (excluding the output layer)
260259
curr_channels = base_channels

mmgen/models/architectures/positional_encoding.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import math
2-
1+
import numpy as np
32
import torch
43
import torch.nn as nn
54

@@ -68,9 +67,9 @@ def get_embedding(num_embeddings,
6867
# there is a little difference from the original paper.
6968
half_dim = embedding_dim // 2
7069
if not div_half_dim:
71-
emb = math.log(10000) / (half_dim - 1)
70+
emb = np.log(10000) / (half_dim - 1)
7271
else:
73-
emb = math.log(1e4) / half_dim
72+
emb = np.log(1e4) / half_dim
7473
# compute exp(-log10000 / d * i)
7574
emb = torch.exp(torch.arange(half_dim, dtype=torch.float) * -emb)
7675
emb = torch.arange(

mmgen/models/architectures/singan/generator_discriminator.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import math
21
from functools import partial
32

3+
import numpy as np
44
import torch
55
import torch.nn as nn
66
import torch.nn.functional as F
@@ -65,9 +65,10 @@ def __init__(self,
6565
F.interpolate, mode='bicubic', align_corners=True)
6666

6767
for scale in range(num_scales + 1):
68-
base_ch = min(base_channels * pow(2, math.floor(scale / 4)), 128)
68+
base_ch = min(base_channels * pow(2, int(np.floor(scale / 4))),
69+
128)
6970
min_feat_ch = min(
70-
min_feat_channels * pow(2, math.floor(scale / 4)), 128)
71+
min_feat_channels * pow(2, int(np.floor(scale / 4))), 128)
7172

7273
self.blocks.append(
7374
GeneratorBlock(
@@ -218,9 +219,10 @@ def __init__(self,
218219
super().__init__()
219220
self.blocks = nn.ModuleList()
220221
for scale in range(num_scales + 1):
221-
base_ch = min(base_channels * pow(2, math.floor(scale / 4)), 128)
222+
base_ch = min(base_channels * pow(2, int(np.floor(scale / 4))),
223+
128)
222224
min_feat_ch = min(
223-
min_feat_channels * pow(2, math.floor(scale / 4)), 128)
225+
min_feat_channels * pow(2, int(np.floor(scale / 4))), 128)
224226
self.blocks.append(
225227
DiscriminatorBlock(
226228
in_channels=in_channels,

mmgen/models/architectures/singan/positional_encoding.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
mentioned in the paper: Positional Encoding as Spatial Inductive Bias in GANs.
55
More details can be found in: https://arxiv.org/pdf/2012.05217.pdf
66
"""
7-
import math
87
from functools import partial
98

109
import mmcv
10+
import numpy as np
1111
import torch
1212
import torch.nn as nn
1313
import torch.nn.functional as F
@@ -97,9 +97,10 @@ def __init__(self,
9797
F.interpolate, mode='bicubic', align_corners=True)
9898

9999
for scale in range(num_scales + 1):
100-
base_ch = min(base_channels * pow(2, math.floor(scale / 4)), 128)
100+
base_ch = min(base_channels * pow(2, int(np.floor(scale / 4))),
101+
128)
101102
min_feat_ch = min(
102-
min_feat_channels * pow(2, math.floor(scale / 4)), 128)
103+
min_feat_channels * pow(2, int(np.floor(scale / 4))), 128)
103104

104105
if scale == 0:
105106
in_ch = (

mmgen/models/architectures/stylegan/generator_discriminator_v1.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import math
21
import random
32

43
import mmcv
4+
import numpy as np
55
import torch
66
import torch.nn as nn
77
import torch.nn.functional as F
@@ -90,7 +90,7 @@ def __init__(self,
9090
}
9191

9292
# generator backbone (8x8 --> higher resolutions)
93-
self.log_size = int(math.log2(self.out_size))
93+
self.log_size = int(np.log2(self.out_size))
9494

9595
self.convs = nn.ModuleList()
9696
self.to_rgbs = nn.ModuleList()
@@ -332,7 +332,7 @@ def forward(self,
332332
latent = torch.cat([latent, latent2], 1)
333333

334334
curr_log_size = self.log_size if curr_scale < 0 else int(
335-
math.log2(curr_scale))
335+
np.log2(curr_scale))
336336
step = curr_log_size - 2
337337

338338
_index = 0
@@ -408,7 +408,7 @@ def __init__(self,
408408
1024: 16,
409409
}
410410

411-
log_size = int(math.log2(in_size))
411+
log_size = int(np.log2(in_size))
412412
self.log_size = log_size
413413
in_channels = channels[in_size]
414414

@@ -495,7 +495,7 @@ def forward(self, input, transition_weight=1., curr_scale=-1):
495495
torch.Tensor: Predict score for the input image.
496496
"""
497497
curr_log_size = self.log_size if curr_scale < 0 else int(
498-
math.log2(curr_scale))
498+
np.log2(curr_scale))
499499
step = curr_log_size - 2
500500
for i in range(step, -1, -1):
501501
index = self.n_layer - i - 1

mmgen/models/architectures/stylegan/generator_discriminator_v2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import math
21
import random
32

43
import mmcv
4+
import numpy as np
55
import torch
66
import torch.nn as nn
77
from mmcv.runner.checkpoint import _load_checkpoint_with_prefix
@@ -152,7 +152,7 @@ def __init__(self,
152152
fp16_enabled=fp16_enabled)
153153

154154
# generator backbone (8x8 --> higher resolutions)
155-
self.log_size = int(math.log2(self.out_size))
155+
self.log_size = int(np.log2(self.out_size))
156156

157157
self.convs = nn.ModuleList()
158158
self.upsamples = nn.ModuleList()
@@ -534,7 +534,7 @@ def __init__(self,
534534
1024: 16 * channel_multiplier,
535535
}
536536

537-
log_size = int(math.log2(in_size))
537+
log_size = int(np.log2(in_size))
538538

539539
in_channels = channels[in_size]
540540

0 commit comments

Comments
 (0)