defimshow(tensor, title=None): image = tensor.cpu().clone() # we clone the tensor to not do changes on it image = image.squeeze(0) # remove the fake batch dimension image = unloader(image) plt.imshow(image) if title isnotNone: plt.title(title) plt.pause(0.001) # pause a bit so that plots are updated
直接保存tensor格式图片
1 2 3 4 5 6 7 8 9 10
defsave_image(tensor, **para): dir = 'results' image = tensor.cpu().clone() # we clone the tensor to not do changes on it image = image.squeeze(0) # remove the fake batch dimension image = unloader(image) ifnot osp.exists(dir): os.makedirs(dir) image.save('results_{}/s{}-c{}-l{}-e{}-sl{:4f}-cl{:4f}.jpg' .format(num, para['style_weight'], para['content_weight'], para['lr'], para['epoch'], para['style_loss'], para['content_loss']))