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

Add CPU option (fix issue #23) #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 15 additions & 8 deletions code/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,21 +357,24 @@ def sampling(self, split_dir):
else:
netG = G_NET()
netG.apply(weights_init)
netG.cuda()
if cfg.CUDA:
netG.cuda()
netG.eval()
#
text_encoder = RNN_ENCODER(self.n_words, nhidden=cfg.TEXT.EMBEDDING_DIM)
state_dict = \
torch.load(cfg.TRAIN.NET_E, map_location=lambda storage, loc: storage)
text_encoder.load_state_dict(state_dict)
print('Load text encoder from:', cfg.TRAIN.NET_E)
text_encoder = text_encoder.cuda()
if cfg.CUDA:
text_encoder = text_encoder.cuda()
text_encoder.eval()

batch_size = self.batch_size
nz = cfg.GAN.Z_DIM
noise = Variable(torch.FloatTensor(batch_size, nz), volatile=True)
noise = noise.cuda()
if cfg.CUDA:
noise = noise.cuda()

model_dir = cfg.TRAIN.NET_G
state_dict = \
Expand Down Expand Up @@ -440,7 +443,8 @@ def gen_example(self, data_dic):
torch.load(cfg.TRAIN.NET_E, map_location=lambda storage, loc: storage)
text_encoder.load_state_dict(state_dict)
print('Load text encoder from:', cfg.TRAIN.NET_E)
text_encoder = text_encoder.cuda()
if cfg.CUDA:
text_encoder = text_encoder.cuda()
text_encoder.eval()

# the path to save generated images
Expand All @@ -454,7 +458,8 @@ def gen_example(self, data_dic):
torch.load(model_dir, map_location=lambda storage, loc: storage)
netG.load_state_dict(state_dict)
print('Load G from: ', model_dir)
netG.cuda()
if cfg.CUDA:
netG.cuda()
netG.eval()
for key in data_dic:
save_dir = '%s/%s' % (s_tmp, key)
Expand All @@ -466,11 +471,13 @@ def gen_example(self, data_dic):
captions = Variable(torch.from_numpy(captions), volatile=True)
cap_lens = Variable(torch.from_numpy(cap_lens), volatile=True)

captions = captions.cuda()
cap_lens = cap_lens.cuda()
if cfg.CUDA:
captions = captions.cuda()
cap_lens = cap_lens.cuda()
for i in range(1): # 16
noise = Variable(torch.FloatTensor(batch_size, nz), volatile=True)
noise = noise.cuda()
if cfg.CUDA:
noise = noise.cuda()
#######################################################
# (1) Extract text embeddings
######################################################
Expand Down