Skip to content

Commit

Permalink
sync remote changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeng committed Sep 3, 2021
1 parent eef3976 commit 484b690
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
1 change: 0 additions & 1 deletion cgan_earth/code_files/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def forward(self, noise, labels, Training=True, ratio=2):
for layer in self.gen:
x = layer(x)
# upsample to give output spatial size (img_length, img_length)
print(torch.sigmoid(self.final_conv(x)).shape)
return torch.sigmoid(self.final_conv(x))


Expand Down
7 changes: 5 additions & 2 deletions cgan_earth/code_files/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,10 @@ def change_noise(label, original_noise, netG, n_classes, z_dim=64, lf=4, device=

lbl = test_label.repeat(1, 1, lf, max_len).to(device)
imgs = np.array([])
noise = original_noise
if method == 'combined':
noise, bool_tensor = vary_noise(original_noise, value/3, ratio=0.5)
else:
noise = original_noise
step = 0.0
if step_size >= 1:
num_img = 1
Expand Down Expand Up @@ -412,7 +415,7 @@ def change_noise(label, original_noise, netG, n_classes, z_dim=64, lf=4, device=
elif method == 'sub':
noise = torch.add(noise, value)
elif method == 'combined':
noise = vary_noise(noise, value/2, ratio=0.5)
noise, bool_tensor = vary_noise(noise, value/2, ratio=0.5, boolean=bool_tensor)
return imgs, noise, netG

def animate(path, imgs, fps=24):
Expand Down
22 changes: 13 additions & 9 deletions cgan_earth/code_files/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@ def replace_noise(original_noise, z_dim, lf, ratio, device):
new_noise[:, :, :, -1] = torch.randn(1, z_dim, lf, device=device)
return new_noise

def vary_noise(original_noise, value, ratio):
def vary_noise(original_noise, value, ratio, boolean=None):
new_noise = torch.zeros_like(original_noise)
for idx0 in range(original_noise.shape[0]):
for idx1 in range(original_noise.shape[1]):
old = original_noise[idx0][idx1].clone().flatten()
old[torch.randint(len(old), (int(len(old)*ratio),))] = torch.add(old[torch.randint(len(old), (int(len(old)*ratio),))], value)
new_noise[idx0][idx1] = old.reshape(original_noise.shape[-2], original_noise.shape[-1])
boolean = torch.eq(original_noise, new_noise)
new_noise[boolean] = torch.sub(new_noise[boolean], value)
return new_noise
if boolean == None:
for idx0 in range(original_noise.shape[0]):
for idx1 in range(original_noise.shape[1]):
old = original_noise[idx0][idx1].clone().flatten()
old[torch.randint(len(old), (int(len(old)*ratio),))] = torch.add(old[torch.randint(len(old), (int(len(old)*ratio),))], value)
new_noise[idx0][idx1] = old.reshape(original_noise.shape[-2], original_noise.shape[-1])
boolean = torch.eq(original_noise, new_noise)
new_noise[boolean] = torch.sub(new_noise[boolean], value)
else:
new_noise[~boolean] = torch.add(original_noise[~boolean], value)
new_noise[boolean] = torch.sub(original_noise[boolean], value)
return new_noise, boolean

def uniform_transit(label1_channel, label2_channel, cur_label, l_step_size):
'''
Expand Down
4 changes: 2 additions & 2 deletions cgan_earth/make_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
sea_lbl = [3]
snow_lbl = [4]
lf = 16
ratio = 2
ratio = 4

# test1: forest, then transit to sea, then roll in sea
# the speed currently must start with the lowest possible speed to make sure the noise has right dimension
imgs1, noise, netG = roll_video(proj_path, sea_lbl, netG(z_dim+n_classes, img_length), n_classes, z_dim, lf=lf, device=device, ratio=ratio, n_clips=5, step_size=0.25)
imgs2, noise, netG = transit_video(sea_lbl, forest_lbl, n_classes, noise, netG, lf=lf, ratio=ratio, device=device, step_size=0.25, z_step_size=0.1, l_step_size=0.2, transit_mode='uniform')
imgs3, noise, netG = roll_video(proj_path, forest_lbl, netG, n_classes, z_dim, lf=lf, device=device, ratio=ratio, n_clips=15, step_size=0.25, original_noise=noise)
imgs4, noise, netG = change_noise(forest_lbl, noise, netG, n_classes, z_dim, lf=lf, device=device, ratio=ratio, n_clips=30, step_size=0.25, value=0.02, method='combined')
imgs4, noise, netG = change_noise(forest_lbl, noise, netG, n_classes, z_dim, lf=lf, device=device, ratio=ratio, n_clips=30, step_size=0.25, value=0.00001, method='combined')

# concatenante the imgs together and make video
imgs = np.vstack((imgs1, imgs2))
Expand Down

0 comments on commit 484b690

Please sign in to comment.