1import torch
2from huggingface_hub import hf_hub_download
3from torch import nn
4
5# Generator Code
6
7from huggingface_hub import hf_hub_download
8from torch import nn
9from torchvision.utils import save_image
10class Generator(nn.Module):
11 def __init__(self, ngpu=1, ngf=128, nz=128, nc =1):
12 super(Generator, self).__init__()
13 self.ngpu = ngpu
14 self.main = nn.Sequential(
15 nn.ConvTranspose2d(nz, ngf * 16, 4, 1, 0, bias=False),
16 nn.BatchNorm2d(ngf*16),
17 nn.LeakyReLU(0.2, inplace=True),
18 nn.ConvTranspose2d(ngf*16, ngf*8, 4, 2, 1, bias=False),
19 nn.BatchNorm2d(ngf * 8),
20 nn.LeakyReLU(0.2, inplace=True),
21 nn.ConvTranspose2d(ngf*8, ngf*4, 4, 2, 1, bias=False),
22 nn.BatchNorm2d(ngf*4),
23 nn.ReLU(True),
24 nn.ConvTranspose2d(ngf*4, ngf*2, 4, 2, 1, bias=False),
25 nn.BatchNorm2d(ngf * 2),
26 nn.ReLU(True),
27 nn.ConvTranspose2d(ngf*2,ngf, 4, 2, 1, bias=False),
28 nn.BatchNorm2d(ngf),
29 nn.ReLU(True),
30 nn.ConvTranspose2d(ngf,nc, 4, 2, 1, bias=False),
31 nn.Tanh()
32 )
33 def forward(self, input):
34 return self.main(input)
35model = Generator()
36weights_path = hf_hub_download('oohtmeel/ct-gan-gen', 'lung_ct_generator_gan.pth')
37model.load_state_dict(torch.load(weights_path, map_location=torch.device('cpu')))
38
39out = model(torch.randn(128, 128, 1, 1))
40save_image(out, "ct_scans.png", normalize=True)
41
42