Update.
[pytorch.git] / minidiffusion.py
index e1f6abd..65ca947 100755 (executable)
@@ -207,8 +207,10 @@ print(f'nb_parameters {sum([ p.numel() for p in model.parameters() ])}')
 ######################################################################
 # Generate
 
-def generate(size, alpha, alpha_bar, sigma, model):
+def generate(size, alpha, alpha_bar, sigma, model, train_mean, train_std):
+
     with torch.no_grad():
+
         x = torch.randn(size, device = device)
 
         for t in range(T-1, -1, -1):
@@ -267,9 +269,11 @@ if train_input.dim() == 2:
     fig = plt.figure()
     ax = fig.add_subplot(1, 1, 1)
 
+    # Nx1 -> histogram
     if train_input.size(1) == 1:
 
-        x = generate((10000, 1), alpha, alpha_bar, sigma, model)
+        x = generate((10000, 1), alpha, alpha_bar, sigma,
+                     model, train_mean, train_std)
 
         ax.set_xlim(-1.25, 1.25)
         ax.spines.right.set_visible(False)
@@ -287,9 +291,11 @@ if train_input.dim() == 2:
 
         ax.legend(frameon = False, loc = 2)
 
+    # Nx2 -> scatter plot
     elif train_input.size(1) == 2:
 
-        x = generate((1000, 2), alpha, alpha_bar, sigma, model)
+        x = generate((1000, 2), alpha, alpha_bar, sigma,
+                     model, train_mean, train_std)
 
         ax.set_xlim(-1.5, 1.5)
         ax.set_ylim(-1.5, 1.5)
@@ -315,9 +321,11 @@ if train_input.dim() == 2:
         plt.get_current_fig_manager().window.setGeometry(2, 2, 1024, 768)
         plt.show()
 
+# NxCxHxW -> image
 elif train_input.dim() == 4:
 
-    x = generate((128,) + train_input.size()[1:], alpha, alpha_bar, sigma, model)
+    x = generate((128,) + train_input.size()[1:], alpha, alpha_bar, sigma,
+                 model, train_mean, train_std)
     x = 1 - x.clamp(min = 0, max = 255) / 255
     torchvision.utils.save_image(x, f'diffusion_{args.data}.png', nrow = 16, pad_value = 0.8)