def generate_interpolation(self,
                            out_name,
                            zsample1=None,
                            zsample2=None,
                            deterministic=True,
                            mode='row',
                            figsize=(10, 10),
                            cmap='gray'):
     """
     Generated an image showing the decoded interpolation between two samples
       from the prior.
     out_name: output image, which is an image grid showing the
       interpolations.
     zsample1: latent vector of size (latent_dim,). If this is `None`, then this
       will be automatically sampled from the model's pre-specified prior.
     zsample2: latent vector of size (latent_dim,). If this is `None`, then this
       will be automatically sampled from the model's pre-specified prior.
     mode: if 'row', produce a row of interpolations. If 'matrix',
       produce a matrix of interpolations.
     cmap: cmap to use with matplotlib
     returns: an output image at filename `out_name`.
     """
     import grid
     assert mode in ['row', 'matrix']
     fn = self.z_fn if not deterministic else self.z_fn_det
     # TODO: currently does not work with non-greyscale images
     if zsample1 == None:
         zsample1 = self.sampler(1, self.latent_dim)[0]
     if zsample2 == None:
         zsample2 = self.sampler(1, self.latent_dim)[1]
     if mode == 'row':
         grid = np.zeros((1, 6, self.in_shp, self.in_shp,
                          1 if self.is_a_grayscale else 3),
                         dtype=zsample1.dtype)
     else:
         grid = np.zeros((5, 5, self.in_shp, self.in_shp,
                          1 if self.is_a_grayscale else 3),
                         dtype=zsample1.dtype)
     ctr = 0
     if mode == 'row':
         coefs = [0.0, 0.1, 0.3, 0.6, 0.9, 1.0]
     else:
         coefs = np.linspace(0, 1, 25).astype(zsample1.dtype)
     if mode == 'row':
         for a in coefs:
             tmp = fn((1 - a) * zsample1[np.newaxis] +
                      a * zsample2[np.newaxis])
             grid[0][ctr] = convert_to_rgb(tmp[0],
                                           is_grayscale=self.is_a_grayscale)
             ctr += 1
     else:
         for y in range(5):
             for x in range(5):
                 a = coefs[ctr]
                 tmp = fn((1 - a) * zsample1[np.newaxis] +
                          a * zsample2[np.newaxis])
                 grid[y][x] = convert_to_rgb(
                     tmp[0], is_grayscale=self.is_a_grayscale)
                 ctr += 1
     grid.write_image_grid(out_name, grid, figsize=figsize, cmap=cmap)
 def generate_gz(self,
                 num_examples,
                 batch_size,
                 out_dir,
                 deterministic=True):
     """
     Generate DCGAN samples g(z).
     num_examples: number of images to generate
     batch_size: batch size
     out_dir: output folder to dump the images.
     deterministic:
     returns:
     """
     if not os.path.exists(out_dir):
         os.makedirs(out_dir)
     from skimage.io import imsave
     fn = self.z_fn if not deterministic else self.z_fn_det
     z = floatX(self.sampler(num_examples, self.latent_dim))
     ctr = 0
     for b in range(num_examples // batch_size):
         out = fn(z[b * batch_size:(b + 1) * batch_size])
         for i in range(out.shape[0]):
             out_processed = convert_to_rgb(
                 out[i], is_grayscale=self.is_a_grayscale)
             imsave(fname="%s/%i.png" % (out_dir, ctr), arr=out_processed)
             ctr += 1
 def generate_atob(self,
                   itr,
                   num_batches,
                   out_dir,
                   dont_predict=False,
                   deterministic=True):
     """
     Generate pix2pix samples.
     itr: iterator to use
     num_batches:
     out_dir:
     dont_predict: if `True`, rather than map from A -> B, just output
       the B ground truth rather than predicting it. This can be useful
       for outputting images that visualise the [A,B] pairs outputted
       by the iterator `itr`.
     deterministic: do we do a deterministic forward pass through the
       pix2pix model?
     """
     fn = self.gen_fn if not deterministic else self.gen_fn_det
     if not os.path.exists(out_dir):
         os.makedirs(out_dir)
     from skimage.io import imsave
     ctr = 0
     for n in range(num_batches):
         this_x, this_y = itr.next()
         if dont_predict:
             pred_y = this_y
         else:
             pred_y = fn(this_x)
         for i in range(pred_y.shape[0]):
             this_x_processed = convert_to_rgb(
                 this_x[i], is_grayscale=self.is_a_grayscale)
             pred_y_processed = convert_to_rgb(
                 pred_y[i], is_grayscale=self.is_b_grayscale)
             imsave(fname="%s/%i.a.png" % (out_dir, ctr),
                    arr=this_x_processed)
             imsave(fname="%s/%i.b.png" % (out_dir, ctr),
                    arr=pred_y_processed)
             ctr += 1
def slow_cycle(strip, args, colors=[], wait_ms=100):
    minval, maxval = 1, 2
    multipler = 1
    if args.win95 or args.slow:
        multipler = 10 if not args.slow else 3
    else:
        multipler = 1
    steps = 10 * multipler if args.steps == STEPS_DEFA else args.steps
    delta = float(maxval - minval) / steps
    print('  Val       R    G    B')
    for i in range(steps + 1):
        val = minval + i * delta
        r, g, b = cc.convert_to_rgb(minval, maxval, val, colors)
        print('{:.3f} -> ({:3d}, {:3d}, {:3d})'.format(val, r, g, b))
        colorWipe(strip, Color(r, g, b), wait_ms)
 def generate_interpolation_clip(self,
                                 num_samples,
                                 batch_size,
                                 out_dir,
                                 deterministic=True,
                                 min_max_norm=False,
                                 concat=False):
     """
     Generate frames corresponding to a long interpolation between
       z1, z2, ..., zn.
     num_samples: number of samples of z to interpolate between
     batch_size:
     out_dir:
     deterministic:
     min_max_norm:
     concat: if `True`, save the (a,b) pairs as single side-by-side images, otherwise
       save the a's and b's separately.
     """
     from skimage.io import imsave
     if not os.path.exists(out_dir):
         os.makedirs(out_dir)
     fn = self.z_fn if not deterministic else self.z_fn_det
     fn_atob = self.gen_fn if not deterministic else self.gen_fn_det
     zs = floatX(self.sampler(num_samples, self.latent_dim))
     coefs = np.linspace(0, 1, 25).astype(zs.dtype)
     # generate interp tuples
     tps = [(zs[i], zs[i + 1]) for i in range(zs.shape[0] - 1)]
     all_tps = []
     for i in range(len(tps)):
         tp = tps[i]
         # generate the interps
         for a in coefs:
             all_tps.append((1 - a) * tp[0] + a * tp[1])
     all_tps = np.asarray(all_tps, dtype=zs.dtype)
     #import pdb
     # pdb.set_trace()
     ctr = 0
     for b in range(all_tps.shape[0] // batch_size):
         z_batch = all_tps[b * batch_size:(b + 1) * batch_size]
         z_out = fn(z_batch)
         p2p_out = fn_atob(z_out)
         for i in range(z_out.shape[0]):
             this_a_img = z_out[i]
             this_b_img = p2p_out[i]
             if min_max_norm:
                 this_a_img = (this_a_img - np.min(this_a_img)) / \
                     (np.max(this_a_img) - np.min(this_a_img))
             this_a_img = convert_to_rgb(this_a_img,
                                         is_grayscale=self.is_a_grayscale)
             this_b_img = convert_to_rgb(this_b_img,
                                         is_grayscale=self.is_b_grayscale)
             d = '%04d' % ctr
             if concat:
                 full_img = np.zeros((self.in_shp, self.in_shp * 2, 3),
                                     dtype=zs.dtype)
                 full_img[0:self.in_shp, 0:self.in_shp, :] = this_a_img
                 full_img[0:self.in_shp, self.in_shp::, :] = this_b_img
                 imsave(arr=full_img,
                        fname="%s/concat_%s.png" % (out_dir, d))
             else:
                 imsave(arr=this_a_img, fname="%s/a_%s.png" % (out_dir, d))
                 imsave(arr=this_b_img, fname="%s/b_%s.png" % (out_dir, d))
             ctr += 1