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

Small fixes to the UI and to keep up to date with pytorch changes. #12

Open
wants to merge 4 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
8 changes: 4 additions & 4 deletions ACGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def train(self):
C_fake_loss = self.CE_loss(C_fake, torch.max(y_vec_, 1)[1])

D_loss = D_real_loss + C_real_loss + D_fake_loss + C_fake_loss
self.train_hist['D_loss'].append(D_loss.data[0])
self.train_hist['D_loss'].append(D_loss.data.item())

D_loss.backward()
self.D_optimizer.step()
Expand All @@ -198,14 +198,14 @@ def train(self):
C_fake_loss = self.CE_loss(C_fake, torch.max(y_vec_, 1)[1])

G_loss += C_fake_loss
self.train_hist['G_loss'].append(G_loss.data[0])
self.train_hist['G_loss'].append(G_loss.data.item())

G_loss.backward()
self.G_optimizer.step()

if ((iter + 1) % 100) == 0:
print("Epoch: [%2d] [%4d/%4d] D_loss: %.8f, G_loss: %.8f" %
((epoch + 1), (iter + 1), len(self.data_X) // self.batch_size, D_loss.data[0], G_loss.data[0]))
((epoch + 1), (iter + 1), len(self.data_X) // self.batch_size, D_loss.data.item(), G_loss.data.item()))

self.train_hist['per_epoch_time'].append(time.time() - epoch_start_time)
self.visualize_results((epoch+1))
Expand Down Expand Up @@ -270,4 +270,4 @@ def load(self):
save_dir = os.path.join(self.save_dir, self.dataset, self.model_name)

self.G.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_G.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
14 changes: 7 additions & 7 deletions BEGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def train(self):
D_fake_err = torch.mean(torch.abs(D_fake - G_))

D_loss = D_real_err - self.k * D_fake_err
self.train_hist['D_loss'].append(D_loss.data[0])
self.train_hist['D_loss'].append(D_loss.data.item())

D_loss.backward()
self.D_optimizer.step()
Expand All @@ -201,7 +201,7 @@ def train(self):
D_fake_err = torch.mean(torch.abs(D_fake - G_))

G_loss = D_fake_err
self.train_hist['G_loss'].append(G_loss.data[0])
self.train_hist['G_loss'].append(G_loss.data.item())

G_loss.backward()
self.G_optimizer.step()
Expand All @@ -211,15 +211,15 @@ def train(self):

# operation for updating k
temp_k = self.k + self.lambda_ * (self.gamma * D_real_err - D_fake_err)
temp_k = temp_k.data[0]
temp_k = temp_k.data.item()

# self.k = temp_k.data[0]
# self.k = temp_k.data.item()
self.k = min(max(temp_k, 0), 1)
self.M = temp_M.data[0]
self.M = temp_M.data.item()

if ((iter + 1) % 100) == 0:
print("Epoch: [%2d] [%4d/%4d] D_loss: %.8f, G_loss: %.8f, M: %.8f, k: %.8f" %
((epoch + 1), (iter + 1), self.data_loader.dataset.__len__() // self.batch_size, D_loss.data[0], G_loss.data[0], self.M, self.k))
((epoch + 1), (iter + 1), self.data_loader.dataset.__len__() // self.batch_size, D_loss.data.item(), G_loss.data.item(), self.M, self.k))

self.train_hist['per_epoch_time'].append(time.time() - epoch_start_time)
self.visualize_results((epoch+1))
Expand Down Expand Up @@ -279,4 +279,4 @@ def load(self):
save_dir = os.path.join(self.save_dir, self.dataset, self.model_name)

self.G.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_G.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
8 changes: 4 additions & 4 deletions CGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def train(self):
D_fake_loss = self.BCE_loss(D_fake, self.y_fake_)

D_loss = D_real_loss + D_fake_loss
self.train_hist['D_loss'].append(D_loss.data[0])
self.train_hist['D_loss'].append(D_loss.data.item())

D_loss.backward()
self.D_optimizer.step()
Expand All @@ -189,14 +189,14 @@ def train(self):
G_ = self.G(z_, y_vec_)
D_fake = self.D(G_, y_fill_)
G_loss = self.BCE_loss(D_fake, self.y_real_)
self.train_hist['G_loss'].append(G_loss.data[0])
self.train_hist['G_loss'].append(G_loss.data.item())

G_loss.backward()
self.G_optimizer.step()

if ((iter + 1) % 100) == 0:
print("Epoch: [%2d] [%4d/%4d] D_loss: %.8f, G_loss: %.8f" %
((epoch + 1), (iter + 1), len(self.data_X) // self.batch_size, D_loss.data[0], G_loss.data[0]))
((epoch + 1), (iter + 1), len(self.data_X) // self.batch_size, D_loss.data.item(), G_loss.data.item()))

self.train_hist['per_epoch_time'].append(time.time() - epoch_start_time)
self.visualize_results((epoch+1))
Expand Down Expand Up @@ -261,4 +261,4 @@ def load(self):
save_dir = os.path.join(self.save_dir, self.dataset, self.model_name)

self.G.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_G.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
8 changes: 4 additions & 4 deletions DRAGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def train(self):
gradient_penalty = self.lambda_ * ((gradients.view(gradients.size()[0], -1).norm(2, 1) - 1) ** 2).mean()

D_loss = D_real_loss + D_fake_loss + gradient_penalty
self.train_hist['D_loss'].append(D_loss.data[0])
self.train_hist['D_loss'].append(D_loss.data.item())
D_loss.backward()
self.D_optimizer.step()

Expand All @@ -211,14 +211,14 @@ def train(self):
D_fake = self.D(G_)

G_loss = self.BCE_loss(D_fake, self.y_real_)
self.train_hist['G_loss'].append(G_loss.data[0])
self.train_hist['G_loss'].append(G_loss.data.item())

G_loss.backward()
self.G_optimizer.step()

if ((iter + 1) % 100) == 0:
print("Epoch: [%2d] [%4d/%4d] D_loss: %.8f, G_loss: %.8f" %
((epoch + 1), (iter + 1), self.data_loader.dataset.__len__() // self.batch_size, D_loss.data[0], G_loss.data[0]))
((epoch + 1), (iter + 1), self.data_loader.dataset.__len__() // self.batch_size, D_loss.data.item(), G_loss.data.item()))

self.train_hist['per_epoch_time'].append(time.time() - epoch_start_time)
self.visualize_results((epoch+1))
Expand Down Expand Up @@ -277,4 +277,4 @@ def load(self):
save_dir = os.path.join(self.save_dir, self.dataset, self.model_name)

self.G.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_G.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
8 changes: 4 additions & 4 deletions EBGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def train(self):
D_loss = D_real_err + (self.margin - D_fake_err)
else:
D_loss = D_real_err
self.train_hist['D_loss'].append(D_loss.data[0])
self.train_hist['D_loss'].append(D_loss.data.item())

D_loss.backward()
self.D_optimizer.step()
Expand All @@ -203,14 +203,14 @@ def train(self):
D_fake, D_fake_code = self.D(G_)
D_fake_err = self.MSE_loss(D_fake, G_.detach())
G_loss = D_fake_err + self.pt_loss_weight * self.pullaway_loss(D_fake_code)
self.train_hist['G_loss'].append(G_loss.data[0])
self.train_hist['G_loss'].append(G_loss.data.item())

G_loss.backward()
self.G_optimizer.step()

if ((iter + 1) % 100) == 0:
print("Epoch: [%2d] [%4d/%4d] D_loss: %.8f, G_loss: %.8f" %
((epoch + 1), (iter + 1), self.data_loader.dataset.__len__() // self.batch_size, D_loss.data[0], G_loss.data[0]))
((epoch + 1), (iter + 1), self.data_loader.dataset.__len__() // self.batch_size, D_loss.data.item(), G_loss.data.item()))

self.train_hist['per_epoch_time'].append(time.time() - epoch_start_time)
self.visualize_results((epoch+1))
Expand Down Expand Up @@ -290,4 +290,4 @@ def load(self):
save_dir = os.path.join(self.save_dir, self.dataset, self.model_name)

self.G.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_G.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
8 changes: 4 additions & 4 deletions GAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def train(self):
D_fake_loss = self.BCE_loss(D_fake, self.y_fake_)

D_loss = D_real_loss + D_fake_loss
self.train_hist['D_loss'].append(D_loss.data[0])
self.train_hist['D_loss'].append(D_loss.data.item())

D_loss.backward()
self.D_optimizer.step()
Expand All @@ -190,14 +190,14 @@ def train(self):
G_ = self.G(z_)
D_fake = self.D(G_)
G_loss = self.BCE_loss(D_fake, self.y_real_)
self.train_hist['G_loss'].append(G_loss.data[0])
self.train_hist['G_loss'].append(G_loss.data.item())

G_loss.backward()
self.G_optimizer.step()

if ((iter + 1) % 100) == 0:
print("Epoch: [%2d] [%4d/%4d] D_loss: %.8f, G_loss: %.8f" %
((epoch + 1), (iter + 1), self.data_loader.dataset.__len__() // self.batch_size, D_loss.data[0], G_loss.data[0]))
((epoch + 1), (iter + 1), self.data_loader.dataset.__len__() // self.batch_size, D_loss.data.item(), G_loss.data.item()))

self.train_hist['per_epoch_time'].append(time.time() - epoch_start_time)
self.visualize_results((epoch+1))
Expand Down Expand Up @@ -257,4 +257,4 @@ def load(self):
save_dir = os.path.join(self.save_dir, self.dataset, self.model_name)

self.G.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_G.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
8 changes: 4 additions & 4 deletions LSGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def train(self):
D_fake_loss = self.MSE_loss(D_fake, self.y_fake_)

D_loss = D_real_loss + D_fake_loss
self.train_hist['D_loss'].append(D_loss.data[0])
self.train_hist['D_loss'].append(D_loss.data.item())

D_loss.backward()
self.D_optimizer.step()
Expand All @@ -190,14 +190,14 @@ def train(self):
G_ = self.G(z_)
D_fake = self.D(G_)
G_loss = self.MSE_loss(D_fake, self.y_real_)
self.train_hist['G_loss'].append(G_loss.data[0])
self.train_hist['G_loss'].append(G_loss.data.item())

G_loss.backward()
self.G_optimizer.step()

if ((iter + 1) % 100) == 0:
print("Epoch: [%2d] [%4d/%4d] D_loss: %.8f, G_loss: %.8f" %
((epoch + 1), (iter + 1), self.data_loader.dataset.__len__() // self.batch_size, D_loss.data[0], G_loss.data[0]))
((epoch + 1), (iter + 1), self.data_loader.dataset.__len__() // self.batch_size, D_loss.data.item(), G_loss.data.item()))

self.train_hist['per_epoch_time'].append(time.time() - epoch_start_time)
self.visualize_results((epoch+1))
Expand Down Expand Up @@ -257,4 +257,4 @@ def load(self):
save_dir = os.path.join(self.save_dir, self.dataset, self.model_name)

self.G.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_G.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
8 changes: 4 additions & 4 deletions WGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,16 @@ def train(self):
G_ = self.G(z_)
D_fake = self.D(G_)
G_loss = -torch.mean(D_fake)
self.train_hist['G_loss'].append(G_loss.data[0])
self.train_hist['G_loss'].append(G_loss.data.item())

G_loss.backward()
self.G_optimizer.step()

self.train_hist['D_loss'].append(D_loss.data[0])
self.train_hist['D_loss'].append(D_loss.data.item())

if ((iter + 1) % 100) == 0:
print("Epoch: [%2d] [%4d/%4d] D_loss: %.8f, G_loss: %.8f" %
((epoch + 1), (iter + 1), self.data_loader.dataset.__len__() // self.batch_size, D_loss.data[0], G_loss.data[0]))
((epoch + 1), (iter + 1), self.data_loader.dataset.__len__() // self.batch_size, D_loss.data.item(), G_loss.data.item()))

self.train_hist['per_epoch_time'].append(time.time() - epoch_start_time)
self.visualize_results((epoch+1))
Expand Down Expand Up @@ -262,4 +262,4 @@ def load(self):
save_dir = os.path.join(self.save_dir, self.dataset, self.model_name)

self.G.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_G.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
8 changes: 4 additions & 4 deletions WGAN_GP.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,16 @@ def train(self):
G_ = self.G(z_)
D_fake = self.D(G_)
G_loss = -torch.mean(D_fake)
self.train_hist['G_loss'].append(G_loss.data[0])
self.train_hist['G_loss'].append(G_loss.data.item())

G_loss.backward()
self.G_optimizer.step()

self.train_hist['D_loss'].append(D_loss.data[0])
self.train_hist['D_loss'].append(D_loss.data.item())

if ((iter + 1) % 100) == 0:
print("Epoch: [%2d] [%4d/%4d] D_loss: %.8f, G_loss: %.8f" %
((epoch + 1), (iter + 1), self.data_loader.dataset.__len__() // self.batch_size, D_loss.data[0], G_loss.data[0]))
((epoch + 1), (iter + 1), self.data_loader.dataset.__len__() // self.batch_size, D_loss.data.item(), G_loss.data.item()))

self.train_hist['per_epoch_time'].append(time.time() - epoch_start_time)
self.visualize_results((epoch+1))
Expand Down Expand Up @@ -276,4 +276,4 @@ def load(self):
save_dir = os.path.join(self.save_dir, self.dataset, self.model_name)

self.G.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_G.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
self.D.load_state_dict(torch.load(os.path.join(save_dir, self.model_name + '_D.pkl')))
12 changes: 6 additions & 6 deletions infoGAN.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def forward(self, input):
x = self.conv(input)
x = x.view(-1, 128 * (self.input_height // 4) * (self.input_width // 4))
x = self.fc(x)
a = F.sigmoid(x[:, self.output_dim])
a = F.sigmoid(x[:, :self.output_dim])
b = x[:, self.output_dim:self.output_dim + self.len_continuous_code]
c = x[:, self.output_dim + self.len_continuous_code:]

Expand Down Expand Up @@ -221,7 +221,7 @@ def train(self):
D_fake_loss = self.BCE_loss(D_fake, self.y_fake_)

D_loss = D_real_loss + D_fake_loss
self.train_hist['D_loss'].append(D_loss.data[0])
self.train_hist['D_loss'].append(D_loss.data.item())

D_loss.backward(retain_graph=True)
self.D_optimizer.step()
Expand All @@ -233,7 +233,7 @@ def train(self):
D_fake, D_cont, D_disc = self.D(G_)

G_loss = self.BCE_loss(D_fake, self.y_real_)
self.train_hist['G_loss'].append(G_loss.data[0])
self.train_hist['G_loss'].append(G_loss.data.item())

G_loss.backward(retain_graph=True)
self.G_optimizer.step()
Expand All @@ -242,15 +242,15 @@ def train(self):
disc_loss = self.CE_loss(D_disc, torch.max(y_disc_, 1)[1])
cont_loss = self.MSE_loss(D_cont, y_cont_)
info_loss = disc_loss + cont_loss
self.train_hist['info_loss'].append(info_loss.data[0])
self.train_hist['info_loss'].append(info_loss.data.item())

info_loss.backward()
self.info_optimizer.step()


if ((iter + 1) % 100) == 0:
print("Epoch: [%2d] [%4d/%4d] D_loss: %.8f, G_loss: %.8f, info_loss: %.8f" %
((epoch + 1), (iter + 1), len(self.data_X) // self.batch_size, D_loss.data[0], G_loss.data[0], info_loss.data[0]))
((epoch + 1), (iter + 1), len(self.data_X) // self.batch_size, D_loss.data.item(), G_loss.data.item(), info_loss.data.item()))

self.train_hist['per_epoch_time'].append(time.time() - epoch_start_time)
self.visualize_results((epoch+1))
Expand Down Expand Up @@ -333,4 +333,4 @@ def loss_plot(self, hist, path='Train_hist.png', model_name=''):

path = os.path.join(path, model_name + '_loss.png')

plt.savefig(path)
plt.savefig(path)
21 changes: 20 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from EBGAN import EBGAN
from BEGAN import BEGAN

import torch

"""parsing and configuration"""
def parse_args():
desc = "Pytorch implementation of GAN collections"
Expand All @@ -32,7 +34,7 @@ def parse_args():
parser.add_argument('--lrD', type=float, default=0.0002)
parser.add_argument('--beta1', type=float, default=0.5)
parser.add_argument('--beta2', type=float, default=0.999)
parser.add_argument('--gpu_mode', type=bool, default=True)
parser.add_argument('--gpu_mode', type=str2bool, default=True)

return check_args(parser.parse_args())

Expand Down Expand Up @@ -62,8 +64,25 @@ def check_args(args):
except:
print('batch size must be larger than or equal to one')

# --gpu_mode
if args.gpu_mode:
try:
assert torch.cuda.is_available()
except:
print('CUDA is not available. Use --gpu_mode False')
raise

return args

def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')


"""main"""
def main():
# parse arguments
Expand Down