return (img_as_tensor, single_image_label) def __len__(self): return len(self.data.index) img_transform = transforms.Compose([transforms.ToTensor()]) dataset = CustomDatasetFromCSV( '/home/17mcmi06/harsha/project/draw_tel_unbalanced/UHTelPCC.csv', 32, 32, transform=img_transform) train_loader = DataLoader(dataset, batch_size=batch_size, shuffle=True) model = DrawModel(T, A, B, z_size, N, dec_size, enc_size) optimizer = optim.Adam(model.parameters(), lr=learning_rate, betas=(beta1, 0.999)) if USE_CUDA: model.cuda() def generate_image(count): x = model.generate(batch_size) return x def save_example_image():
from config import * from utility import Variable, save_image, xrecons_grid import torch.nn.utils import matplotlib.pyplot as plt torch.set_default_tensor_type('torch.FloatTensor') train_loader = torch.utils.data.DataLoader(datasets.MNIST( '../data', train=True, download=True, transform=transforms.Compose([transforms.ToTensor()])), batch_size=batch_size, shuffle=False) model = DrawModel(T, A, B, z_size, N, dec_size, enc_size, use_att) optimizer = optim.Adam(model.parameters(), lr=learning_rate, betas=(beta1, 0.999)) if USE_CUDA: model.cuda() def train(): avg_loss = 0 count = 0 for epoch in range(epoch_num): for data, _ in train_loader: bs = data.size()[0] data = Variable(data).view(bs, -1)
from draw_model import DrawModel from config import * #from torchvision.utils import save_image from utility import * import torch.nn.utils import numpy as np import pandas as pd torch.set_default_tensor_type('torch.FloatTensor') model = DrawModel(T,A,B,z_size,N,dec_size,enc_size) if USE_CUDA: model.cuda() state_dict = torch.load('save/weights_15600.tar') model.load_state_dict(state_dict) def save_imag(x,i): for t in range(T): img = xrecons_grid(x[t],B,A) plt.matshow(img, cmap=plt.cm.gray) imgname = 'gen_image/i_%d_%s_%d.png' % (i,'gen', t) # you can merge using imagemagick, i.e. convert -delay 10 -loop 0 *.png mnist.gif plt.savefig(imgname) print(imgname) def generate(epoch): x,_ = model.generate(batch_size) return x #save_imag(x,epoch)
import torch.utils from draw_model import DrawModel from config import * from utility import Variable,save_image,xrecons_grid import torch.nn.utils import matplotlib.pyplot as plt torch.set_default_tensor_type('torch.FloatTensor') train_loader = torch.utils.data.DataLoader( datasets.MNIST('data/', train=True, download=True, transform=transforms.Compose([ transforms.ToTensor()])), batch_size=batch_size, shuffle=False) model = DrawModel(T,A,B,z_size,N,dec_size,enc_size) optimizer = optim.Adam(model.parameters(),lr=learning_rate,betas=(beta1,0.999)) if USE_CUDA: model.cuda() def train(): avg_loss = 0 count = 0 for epoch in range(epoch_num): for data,_ in train_loader: bs = data.size()[0] data = Variable(data).view(bs, -1) optimizer.zero_grad() loss = model.loss(data) avg_loss += loss.cpu().data.numpy()