Skip to content

Commit 138f2c7

Browse files
authored
Merge pull request #41 from Tensor46/develop
Develop
2 parents 540e043 + 58f6ee0 commit 138f2c7

File tree

7 files changed

+317
-148
lines changed

7 files changed

+317
-148
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ Trained on CIFAR10 (pggan-cifar10.py) -- requires more training (more gpus)!
104104
* [ReductionB](https://arxiv.org/pdf/1602.07261.pdf)
105105
* [ContextNet_Bottleneck](https://arxiv.org/pdf/1805.04554.pdf)
106106

107+
* [DropBlock](https://arxiv.org/abs/1810.12890)
107108
* [PrimaryCapsule](https://arxiv.org/pdf/1710.09829.pdf)
108109
* [RoutingCapsule](https://arxiv.org/pdf/1710.09829.pdf)
109110

core/NeuralArchitectures/convolutionalvae.py

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,36 @@
1010
import numpy as np
1111

1212

13-
class ReShape(nn.Module):
14-
def __init__(self, tensor_size):
15-
super(ReShape, self).__init__()
16-
self.tensor_size = tensor_size
17-
18-
def forward(self, tensor):
19-
return tensor.view(tensor.size(0), *self.tensor_size[1:])
20-
21-
2213
class ConvolutionalVAE(nn.Module):
23-
"""
24-
Convolutional Variational Auto Encoder
25-
26-
Parameters
27-
tensor_size :: expected size of input tensor
28-
embedding_layers :: a list of (filter_size, out_channels, strides)
29-
in each intermediate layer of the encoder.
30-
A flip is used for decoder
31-
n_latent :: length of latent vecotr Z
32-
decoder_final_activation :: tanh/sigm
33-
34-
activation, normalization, pre_nm, weight_nm, equalized, bias ::
35-
refer to core.NeuralLayers
14+
r""" Example Convolutional Variational Auto Encoder
15+
16+
Args:
17+
tensor_size: shape of tensor in BCHW
18+
(None/any integer >0, channels, height, width)
19+
embedding_layers: a list of (filter_size, out_channels, strides)
20+
in each intermediate layer of the encoder. A flip is used for
21+
decoder.
22+
n_latent: length of latent vecotr Z
23+
decoder_final_activation: tanh/sigm
24+
activation, normalization, pre_nm, weight_nm, equalized, bias:
25+
refer to core.NeuralLayers.Convolution
26+
27+
Return:
28+
encoded, mu, log_var, latent, decoded, kld, mse
3629
"""
3730
def __init__(self,
38-
tensor_size=(6, 1, 28, 28),
39-
embedding_layers=[(3, 32, 2), (3, 64, 2)],
40-
n_latent=128,
41-
decoder_final_activation="tanh",
42-
pad=True,
43-
activation="relu",
44-
normalization=None,
45-
pre_nm=False,
46-
groups=1,
47-
weight_nm=False,
48-
equalized=False,
49-
bias=False,
31+
tensor_size: tuple = (6, 1, 28, 28),
32+
embedding_layers: list = [(3, 32, 2), (3, 64, 2)],
33+
n_latent: int = 128,
34+
decoder_final_activation: str = "tanh",
35+
pad: bool = True,
36+
activation: str = "relu",
37+
normalization: str = None,
38+
pre_nm: bool = False,
39+
groups: int = 1,
40+
weight_nm: bool = False,
41+
equalized: bool = False,
42+
bias: bool = False,
5043
*args, **kwargs):
5144
super(ConvolutionalVAE, self).__init__()
5245

@@ -65,23 +58,22 @@ def __init__(self,
6558
kwargs["equalized"] = equalized
6659
# encoder with Convolution layers
6760
encoder = []
68-
_tensor_size = tensor_size
61+
t_size = tensor_size
6962
for f, c, s in embedding_layers:
70-
encoder.append(Convolution(_tensor_size, f, c, s, **kwargs))
71-
_tensor_size = encoder[-1].tensor_size
63+
encoder.append(Convolution(t_size, f, c, s, **kwargs))
64+
t_size = encoder[-1].tensor_size
7265
self.encoder = nn.Sequential(*encoder)
7366

7467
# mu and log_var to synthesize Z
75-
self.mu = Linear(_tensor_size, n_latent, "", 0., bias=bias)
76-
self.log_var = Linear(_tensor_size, n_latent, "", 0., bias=bias)
68+
self.mu = Linear(t_size, n_latent, "", 0., bias=bias)
69+
self.log_var = Linear(t_size, n_latent, "", 0., bias=bias)
7770

7871
# decoder - (Linear layer + ReShape) to generate encoder last output
7972
# shape, followed by inverse of encoder
8073
decoder = []
8174
decoder.append(Linear(self.mu.tensor_size,
82-
int(np.prod(_tensor_size[1:])),
83-
activation, 0., bias=bias))
84-
decoder.append(ReShape(_tensor_size))
75+
int(np.prod(t_size[1:])), activation, 0.,
76+
bias=bias, out_shape=t_size[1:]))
8577

8678
decoder_layers = []
8779
for i, x in enumerate(embedding_layers[::-1]):
@@ -94,10 +86,9 @@ def __init__(self,
9486
for i, (f, c, s, o) in enumerate(decoder_layers):
9587
if i == len(decoder_layers)-1:
9688
kwargs["activation"] = None
97-
decoder.append(Convolution(_tensor_size, f, c, s,
98-
transpose=True, **kwargs))
99-
decoder[-1].tensor_size = o # adjusting the output tensor size
100-
_tensor_size = decoder[-1].tensor_size
89+
decoder.append(Convolution(t_size, f, c, s, transpose=True,
90+
maintain_out_size=True, **kwargs))
91+
t_size = decoder[-1].tensor_size
10192
self.decoder = nn.Sequential(*decoder)
10293

10394
# Final normalization
@@ -123,6 +114,7 @@ def forward(self, tensor, noisy_tensor=None):
123114
mse = F.mse_loss(decoded, tensor)
124115
return encoded, mu, log_var, latent, decoded, kld, mse
125116

117+
126118
# from core.NeuralLayers import Convolution, Linear
127119
# tensor_size = (1, 1, 28, 28)
128120
# tensor = torch.rand(*tensor_size)

core/NeuralEssentials/cudamodel.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ def check_precision_device(self, inputs):
3535
self.precision = p.dtype if "p" in locals() else torch.float32
3636
if type(inputs) in [list, tuple]:
3737
if self.is_cuda:
38-
inputs = [x.type(self.precision).cuda() if self.is_cuda else
39-
x.type(self.precision) for x in inputs]
38+
inputs = [(x.type(self.precision).cuda() if self.is_cuda else
39+
x.type(self.precision)) if x.dtype != torch.long
40+
else x for x in inputs]
4041
return inputs
4142
else:
42-
inputs = inputs.type(self.precision)
43+
if not (inputs.dtype == torch.long):
44+
inputs = inputs.type(self.precision)
4345
if self.is_cuda:
4446
inputs = inputs.cuda()
4547
return inputs

0 commit comments

Comments
 (0)