Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for Pytorch 1.7 #126

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
My version has minor bugfixes to update it to modern python versions.

# SinGAN

[Project](https://tamarott.github.io/SinGAN.htm) | [Arxiv](https://arxiv.org/pdf/1905.01164.pdf) | [CVF](http://openaccess.thecvf.com/content_ICCV_2019/papers/Shaham_SinGAN_Learning_a_Generative_Model_From_a_Single_Natural_Image_ICCV_2019_paper.pdf)
Expand Down
2 changes: 1 addition & 1 deletion SR.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
reals_sr.append(real_)
Gs_sr.append(Gs[-1])
NoiseAmp_sr.append(NoiseAmp[-1])
z_opt = torch.full(real_.shape, 0, device=opt.device)
z_opt = torch.full(real_.shape, 0, dtype=torch.float32, device=opt.device)
m = nn.ZeroPad2d(5)
z_opt = m(z_opt)
Zs_sr.append(z_opt)
Expand Down
4 changes: 2 additions & 2 deletions SinGAN/manipulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

def generate_gif(Gs,Zs,reals,NoiseAmp,opt,alpha=0.1,beta=0.9,start_scale=2,fps=10):

in_s = torch.full(Zs[0].shape, 0, device=opt.device)
in_s = torch.full(Zs[0].shape, 0, dtype=torch.float32, device=opt.device)
images_cur = []
count = 0

Expand Down Expand Up @@ -89,7 +89,7 @@ def generate_gif(Gs,Zs,reals,NoiseAmp,opt,alpha=0.1,beta=0.9,start_scale=2,fps=1
def SinGAN_generate(Gs,Zs,reals,NoiseAmp,opt,in_s=None,scale_v=1,scale_h=1,n=0,gen_start_scale=0,num_samples=50):
#if torch.is_tensor(in_s) == False:
if in_s is None:
in_s = torch.full(reals[0].shape, 0, device=opt.device)
in_s = torch.full(reals[0].shape, 0, dtype=torch.float32, device=opt.device)
images_cur = []
for G,Z_opt,noise_amp in zip(Gs,Zs,NoiseAmp):
pad1 = ((opt.ker_size-1)*opt.num_layer)/2
Expand Down
17 changes: 9 additions & 8 deletions SinGAN/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def train_single_scale(netD,netG,reals,Gs,Zs,in_s,NoiseAmp,opt,centers=None):
alpha = opt.alpha

fixed_noise = functions.generate_noise([opt.nc_z,opt.nzx,opt.nzy],device=opt.device)
z_opt = torch.full(fixed_noise.shape, 0, device=opt.device)
z_opt = torch.full(fixed_noise.shape, 0, dtype=torch.float32, device=opt.device)
z_opt = m_noise(z_opt)

# setup optimizer
Expand Down Expand Up @@ -105,7 +105,7 @@ def train_single_scale(netD,netG,reals,Gs,Zs,in_s,NoiseAmp,opt,centers=None):
############################
# (1) Update D network: maximize D(x) + D(G(z))
###########################
for j in range(opt.Dsteps):
for j in range(opt.steps):
# train with real
netD.zero_grad()

Expand All @@ -118,10 +118,10 @@ def train_single_scale(netD,netG,reals,Gs,Zs,in_s,NoiseAmp,opt,centers=None):
# train with fake
if (j==0) & (epoch == 0):
if (Gs == []) & (opt.mode != 'SR_train'):
prev = torch.full([1,opt.nc_z,opt.nzx,opt.nzy], 0, device=opt.device)
prev = torch.full([1,opt.nc_z,opt.nzx,opt.nzy], 0, dtype=torch.float32, device=opt.device)
in_s = prev
prev = m_image(prev)
z_prev = torch.full([1,opt.nc_z,opt.nzx,opt.nzy], 0, device=opt.device)
z_prev = torch.full([1,opt.nc_z,opt.nzx,opt.nzy], 0, dtype=torch.float32, device=opt.device)
z_prev = m_noise(z_prev)
opt.noise_amp = 1
elif opt.mode == 'SR_train':
Expand Down Expand Up @@ -164,13 +164,13 @@ def train_single_scale(netD,netG,reals,Gs,Zs,in_s,NoiseAmp,opt,centers=None):
errD = errD_real + errD_fake + gradient_penalty
optimizerD.step()

errD2plot.append(errD.detach())


############################
# (2) Update G network: maximize D(G(z))
###########################

for j in range(opt.Gsteps):

netG.zero_grad()
output = netD(fake)
#D_fake_map = output.detach()
Expand All @@ -190,7 +190,8 @@ def train_single_scale(netD,netG,reals,Gs,Zs,in_s,NoiseAmp,opt,centers=None):
rec_loss = 0

optimizerG.step()


errD2plot.append(errD.detach())
errG2plot.append(errG.detach()+rec_loss)
D_real2plot.append(D_x)
D_fake2plot.append(D_G_z)
Expand Down Expand Up @@ -255,7 +256,7 @@ def draw_concat(Gs,Zs,reals,NoiseAmp,in_s,mode,m_noise,m_image,opt):
return G_z

def train_paint(opt,Gs,Zs,reals,NoiseAmp,centers,paint_inject_scale):
in_s = torch.full(reals[0].shape, 0, device=opt.device)
in_s = torch.full(reals[0].shape, 0, dtype=torch.float32, device=opt.device)
scale_num = 0
nfc_prev = 0

Expand Down
3 changes: 1 addition & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def get_arguments():
parser.add_argument('--lr_g', type=float, default=0.0005, help='learning rate, default=0.0005')
parser.add_argument('--lr_d', type=float, default=0.0005, help='learning rate, default=0.0005')
parser.add_argument('--beta1', type=float, default=0.5, help='beta1 for adam. default=0.5')
parser.add_argument('--Gsteps',type=int, help='Generator inner steps',default=3)
parser.add_argument('--Dsteps',type=int, help='Discriminator inner steps',default=3)
parser.add_argument('--steps',type=int, help='Generator / Discriminator inner steps',default=3)
parser.add_argument('--lambda_grad',type=float, help='gradient penelty weight',default=0.1)
parser.add_argument('--alpha',type=float, help='reconstruction loss weight',default=10)

Expand Down