Beispiel #1
0
 def __init__(self, z_dim=8, c_dim=1, scale=10.0, net_size=32):
     self.cppn = CPPN(z_dim=z_dim,
                      c_dim=c_dim,
                      scale=scale,
                      net_size=net_size)
     self.z = self.generate_z(
     )  # saves most recent z here, in case we find a nice image and want the z-vec
Beispiel #2
0
def main():
    checkDirectory('./many_models/model_images/')
    checkDirectory('./many_models/models/')

    # Variables for different images
    x_dim = 1440
    y_dim = 1080

    color_channels = 1

    # Edit these to make specific types of models
    zs = list(range(3, 4))
    neurons = list(range(15, 20))
    layers = list(range(3, 5))

    tests = [0, 1, 2, 3, 4, 5]  # the different types of networks
    scale = 24

    for z_dim in zs:
        for neurons_per_layer in neurons:
            for number_of_layers in layers:
                for t in tests:
                    # Make a new CPPN
                    interpolations_per_image = 1
                    cppn = CPPN(x_dim,
                                y_dim,
                                z_dim,
                                scale,
                                neurons_per_layer,
                                number_of_layers,
                                color_channels,
                                interpolations_per_image,
                                test=t)
                    cppn.neural_net(True)

                    dict_key = makeKey(z_dim, neurons_per_layer,
                                       number_of_layers, t)

                    # Save the model
                    cppn.save_model(model_name=dict_key,
                                    model_dir='many_models/models',
                                    save_outfile=True)

                    z = np.random.uniform(-1.0, 1.0,
                                          size=(z_dim)).astype(np.float32)

                    # Save a test image
                    filename = "./many_models/model_images/%s.png" % (dict_key)
                    cppn.save_png(z, filename, save=True)

                    cppn.close()

                print('num/l', number_of_layers, 'neur/l', neurons_per_layer,
                      'z', z_dim, 'all_tests completed')
def main(x_dim, y_dim, z_dim, scale, neurons_per_layer, number_of_layers,
         color_channels, number_of_stills, interpolations_per_image, file_name):

    print('Initializing CPPN...')
    cppn = CPPN(x_dim, y_dim, z_dim, scale, neurons_per_layer, number_of_layers,
                color_channels, interpolations_per_image)
    cppn.neural_net(True)

    print('Making latent(z) vectors')
    # Make a list of random latent vectors
    zs = [] # list of latent vectors
    for i in range(number_of_stills):
        zs.append(np.random.uniform(-1.0, 1.0, size=(z_dim)).astype(np.float32))

    # Save to a video file
    print('Making Video!')
    cppn.save_mp4(zs, file_name)

    print('Done! The video is at %s' % file_name)
def main(model_name, function, outfiles=None, file_name=None):

    # Load a saved CPPN and set variables
    meta_data = getMetaData(model_name)

    (x_dim, y_dim, z_dim, scale, neurons_per_layer, number_of_layers,
     color_channels, test) = meta_data
    interpolations_per_image = 24

    # Initialize CPPN with parameters #########################################
    print('Initializing CPPN...')
    cppn = CPPN(x_dim,
                y_dim,
                z_dim,
                scale,
                neurons_per_layer,
                number_of_layers,
                color_channels,
                interpolations_per_image,
                test=test)
    cppn.neural_net(True)
    cppn.load_model(model_name=model_name)
    ###########################################################################

    if (function == 'make_random_video'):
        if (file_name != None):
            make_random_video(cppn, file_name, z_dim, number_of_stills=5)
        else:
            print('Please specify a file name. Visit the readme.')
    elif (function == 'make_gui_video'):
        if (outfiles != None and file_name != None):
            outfiles = outfiles.split(',')
            make_gui_video(cppn, outfiles, file_name)
        else:
            print(
                'Please specify an outfile and a file name. Visit the readme.')
    else:
        print('Please enter a valid function. Visit the readme.')
Beispiel #5
0
 def __init__(self, z_dim = 8, c_dim = 1, scale = 10.0, net_size = 32):
   self.cppn = CPPN(z_dim = z_dim, c_dim = c_dim, scale = scale, net_size = net_size)
   self.z = self.generate_z() # saves most recent z here, in case we find a nice image and want the z-vec
Beispiel #6
0
class Sampler():
  def __init__(self, z_dim = 8, c_dim = 1, scale = 10.0, net_size = 32):
    self.cppn = CPPN(z_dim = z_dim, c_dim = c_dim, scale = scale, net_size = net_size)
    self.z = self.generate_z() # saves most recent z here, in case we find a nice image and want the z-vec
  def reinit(self):
    self.cppn.reinit()
  def generate_z(self):
    z = np.random.uniform(-1.0, 1.0, size=(1, self.cppn.z_dim)).astype(np.float32)
    return z
  def generate(self, z=None, x_dim=1080, y_dim=1060, scale = 10.0):
    if z is None:
      z = self.generate_z()
    else:
      z = np.reshape(z, (1, self.cppn.z_dim))
    self.z = z
    return self.cppn.generate(z, x_dim, y_dim, scale)[0]
  def show_image(self, image_data):
    '''
    image_data is a tensor, in [height width depth]
    image_data is NOT the PIL.Image class
    '''
    plt.subplot(1, 1, 1)
    y_dim = image_data.shape[0]
    x_dim = image_data.shape[1]
    c_dim = self.cppn.c_dim
    if c_dim > 1:
      plt.imshow(image_data, interpolation='nearest')
    else:
      plt.imshow(image_data.reshape(y_dim, x_dim), cmap='Greys', interpolation='nearest')
    plt.axis('off')
    plt.show()
  def save_png(self, image_data, filename):
    img_data = np.array(1-image_data)
    y_dim = image_data.shape[0]
    x_dim = image_data.shape[1]
    c_dim = self.cppn.c_dim
    if c_dim > 1:
      img_data = np.array(img_data.reshape((y_dim, x_dim, c_dim))*255.0, dtype=np.uint8)
    else:
      img_data = np.array(img_data.reshape((y_dim, x_dim))*255.0, dtype=np.uint8)
    im = Image.fromarray(img_data)
    im.save(filename)
  def to_image(self, image_data):
    # convert to PIL.Image format from np array (0, 1)
    img_data = np.array(1-image_data)
    y_dim = image_data.shape[0]
    x_dim = image_data.shape[1]
    c_dim = self.cppn.c_dim
    if c_dim > 1:
      img_data = np.array(img_data.reshape((y_dim, x_dim, c_dim))*255.0, dtype=np.uint8)
    else:
      img_data = np.array(img_data.reshape((y_dim, x_dim))*255.0, dtype=np.uint8)
    im = Image.fromarray(img_data)
    return im
  def save_anim_gif(self, z1, z2, filename, n_frame = 10, duration1 = 0.5, \
                    duration2 = 1.0, duration = 0.1, x_dim = 512, y_dim = 512, scale = 10.0, reverse = True):
    '''
    this saves an animated gif from two latent states z1 and z2
    n_frame: number of states in between z1 and z2 morphing effect, exclusive of z1 and z2
    duration1, duration2, control how long z1 and z2 are shown.  duration controls frame speed, in seconds
    '''
    delta_z = (z2-z1) / (n_frame+1)
    total_frames = n_frame + 2
    images = []
    for i in range(total_frames):
      z = z1 + delta_z*float(i)
      images.append(self.to_image(self.generate(z, x_dim, y_dim, scale)))
      print "processing image ", i
    durations = [duration1]+[duration]*n_frame+[duration2]
    if reverse == True: # go backwards in time back to the first state
      revImages = list(images)
      revImages.reverse()
      revImages = revImages[1:]
      images = images+revImages
      durations = durations + [duration]*n_frame + [duration1]
    print "writing gif file..."
    writeGif(filename, images, duration = durations)
Beispiel #7
0
class Sampler():
    def __init__(self, z_dim=8, c_dim=1, scale=10.0, net_size=32):
        self.cppn = CPPN(z_dim=z_dim,
                         c_dim=c_dim,
                         scale=scale,
                         net_size=net_size)
        self.z = self.generate_z(
        )  # saves most recent z here, in case we find a nice image and want the z-vec

    def reinit(self):
        self.cppn.reinit()

    def generate_z(self):
        z = np.random.uniform(-1.0, 1.0,
                              size=(1, self.cppn.z_dim)).astype(np.float32)
        return z

    def generate(self, z=None, x_dim=1080, y_dim=1060, scale=10.0):
        if z is None:
            z = self.generate_z()
        else:
            z = np.reshape(z, (1, self.cppn.z_dim))
        self.z = z
        return self.cppn.generate(z, x_dim, y_dim, scale)[0]

    def show_image(self, image_data):
        '''
    image_data is a tensor, in [height width depth]
    image_data is NOT the PIL.Image class
    '''
        plt.subplot(1, 1, 1)
        y_dim = image_data.shape[0]
        x_dim = image_data.shape[1]
        c_dim = self.cppn.c_dim
        if c_dim > 1:
            plt.imshow(image_data, interpolation='nearest')
        else:
            plt.imshow(image_data.reshape(y_dim, x_dim),
                       cmap='Greys',
                       interpolation='nearest')
        plt.axis('off')
        plt.show()

    def save_png(self, image_data, filename):
        img_data = np.array(1 - image_data)
        y_dim = image_data.shape[0]
        x_dim = image_data.shape[1]
        c_dim = self.cppn.c_dim
        if c_dim > 1:
            img_data = np.array(img_data.reshape(
                (y_dim, x_dim, c_dim)) * 255.0,
                                dtype=np.uint8)
        else:
            img_data = np.array(img_data.reshape((y_dim, x_dim)) * 255.0,
                                dtype=np.uint8)
        im = Image.fromarray(img_data)
        im.save(filename)

    def to_image(self, image_data):
        # convert to PIL.Image format from np array (0, 1)
        img_data = np.array(1 - image_data)
        y_dim = image_data.shape[0]
        x_dim = image_data.shape[1]
        c_dim = self.cppn.c_dim
        if c_dim > 1:
            img_data = np.array(img_data.reshape(
                (y_dim, x_dim, c_dim)) * 255.0,
                                dtype=np.uint8)
        else:
            img_data = np.array(img_data.reshape((y_dim, x_dim)) * 255.0,
                                dtype=np.uint8)
        im = Image.fromarray(img_data)
        return im

    def to_anim_images(self, z1, z2, n_frame = 10, duration1 = 0.5, \
                      duration2 = 1.0, duration = 0.1, x_dim = 512, y_dim = 512, scale = 10.0, reverse = True):
        '''
    this saves an animated gif from two latent states z1 and z2
    n_frame: number of states in between z1 and z2 morphing effect, exclusive of z1 and z2
    duration1, duration2, control how long z1 and z2 are shown.  duration controls frame speed, in seconds
    '''
        delta_z = (z2 - z1) / (n_frame + 1)
        total_frames = n_frame + 2
        images = []
        for i in range(total_frames):
            z = z1 + delta_z * float(i)
            images.append(self.to_image(self.generate(z, x_dim, y_dim, scale)))
            print("processing image ", i)
        durations = [duration1] + [duration] * n_frame + [duration2]
        if reverse == True:  # go backwards in time back to the first state
            revImages = list(images)
            revImages.reverse()
            revImages = revImages[1:]
            images = images + revImages
            durations = durations + [duration] * n_frame + [duration1]
        return images, durations
Beispiel #8
0
parser.add_argument('--frames',
                    type=int,
                    default=2,
                    help='number of frames in an animation for the gif')

opt = parser.parse_args()
print(opt)

mnist = MNIST()

mnist_train = mnist.train_loader

if opt.cuda:
    cuda_gpu = torch.device('cuda:0')
    cppn = CPPN(x_dim=opt.x_dim,
                y_dim=opt.y_dim,
                scale=opt.scale,
                cuda_device=cuda_gpu)
    cppn.cuda()
    cppn.load_state_dict(torch.load(opt.model))
else:
    cppn = CPPN(x_dim=opt.x_dim, y_dim=opt.y_dim, scale=opt.scale)
    cppn.load_state_dict(torch.load(opt.model, map_location='cpu'))

cppn.eval()

enc = []
lab = []
for idx, (im, label) in enumerate(mnist_train):
    with torch.no_grad():
        if opt.cuda:
            im = im.cuda()
Beispiel #9
0
from model import CPPN, CPPNParameters

if __name__ == "__main__":
    params = CPPNParameters()
    model = CPPN(params)
    #model.save_gif("test.gif", 50)
    model.save_image("test.png")
Beispiel #10
0
class Sampler():
    def __init__(self, z_dim=8, c_dim=1, scale=10.0, net_size=32):
        self.cppn = CPPN(z_dim=z_dim,
                         c_dim=c_dim,
                         scale=scale,
                         net_size=net_size)
        self.z = self.generate_z(
        )  # saves most recent z here, in case we find a nice image and want the z-vec

    def reinit(self):
        self.cppn.reinit()

    def generate_z(self):
        z = np.random.uniform(-1.0, 1.0,
                              size=(1, self.cppn.z_dim)).astype(np.float32)
        return z

    def generate(self, z=None, x_dim=1080, y_dim=1060, scale=10.0):
        if z is None:
            z = self.generate_z()
        else:
            z = np.reshape(z, (1, self.cppn.z_dim))
        self.z = z
        return self.cppn.generate(z, x_dim, y_dim, scale)[0]

    def show_image(self, image_data):
        '''
    image_data is a tensor, in [height width depth]
    image_data is NOT the PIL.Image class
    '''
        plt.subplot(1, 1, 1)
        y_dim = image_data.shape[0]
        x_dim = image_data.shape[1]
        c_dim = self.cppn.c_dim
        if c_dim > 1:
            plt.imshow(image_data, interpolation='nearest')
        else:
            plt.imshow(image_data.reshape(y_dim, x_dim),
                       cmap='Greys',
                       interpolation='nearest')
        plt.axis('off')
        plt.show()

    def save_png(self, image_data, filename):
        img_data = np.array(1 - image_data)
        y_dim = image_data.shape[0]
        x_dim = image_data.shape[1]
        c_dim = self.cppn.c_dim
        if c_dim > 1:
            img_data = np.array(img_data.reshape(
                (y_dim, x_dim, c_dim)) * 255.0,
                                dtype=np.uint8)
        else:
            img_data = np.array(img_data.reshape((y_dim, x_dim)) * 255.0,
                                dtype=np.uint8)
        im = Image.fromarray(img_data)
        im.save(filename)

    def to_image(self, image_data):
        # convert to PIL.Image format from np array (0, 1)
        img_data = np.array(1 - image_data)
        y_dim = image_data.shape[0]
        x_dim = image_data.shape[1]
        c_dim = self.cppn.c_dim
        if c_dim > 1:
            img_data = np.array(img_data.reshape(
                (y_dim, x_dim, c_dim)) * 255.0,
                                dtype=np.uint8)
        else:
            img_data = np.array(img_data.reshape((y_dim, x_dim)) * 255.0,
                                dtype=np.uint8)
        im = Image.fromarray(img_data)
        return im
    def save_anim_gif(self, z1, z2, filename, n_frame = 240, duration1 = 0.5, \
                      duration2 = 1.0, duration = 0.1, x_dim = 1920, y_dim = 1080, scale = 10.0, reverse = True):
        '''
    this saves an animated gif from two latent states z1 and z2
    n_frame: number of states in between z1 and z2 morphing effect, exclusive of z1 and z2
    duration1, duration2, control how long z1 and z2 are shown.  duration controls frame speed, in seconds
    '''
        delta_z = (z2 - z1) / (n_frame + 1)
        total_frames = n_frame + 2
        images = []
        for i in range(total_frames):
            z = z1 + delta_z * float(i)
            images.append(self.to_image(self.generate(z, x_dim, y_dim, scale)))
            print "processing image ", i
        durations = [duration1] + [duration] * n_frame + [duration2]
        if reverse == True:  # go backwards in time back to the first state
            revImages = list(images)
            revImages.reverse()
            revImages = revImages[1:]
            images = images + revImages
            durations = durations + [duration] * n_frame + [duration1]
        print "writing gif file..."
        writeGif(filename, images, duration=durations)

    def save_anim_mp4(self,
                      filename,
                      n_frame=120,
                      x_dim=1920,
                      y_dim=1080,
                      scale=10.0,
                      reverse=True):
        z1 = self.generate_z()
        z2 = self.generate_z()
        path_folder = 'output/%s' % filename
        if not os.path.exists(path_folder):
            print 'creating path: %s' % path_folder
            os.makedirs(path_folder)

        delta_z = (z2 - z1) / (n_frame + 1)
        total_frames = n_frame + 2
        for i in range(total_frames):
            z = z1 + delta_z * float(i)
            img = self.to_image(self.generate(z, x_dim, y_dim, scale))
            img.save('%s/%s-%04d.png' % (path_folder, filename, i))
            print "processing image %d/%d" % (i, n_frame)
        os.system(
            'ffmpeg -i %s/%s-%%04d.png -c:v libx264 -crf 0 -preset veryslow -framerate 30 %s/%s.mp4'
            % (path_folder, filename, path_folder, filename))
        if (reverse):
            os.system(
                'ffmpeg -i %s/%s.mp4 -filter_complex "[0:v]reverse,fifo[r];[0:v][r] concat=n=2:v=1 [v]" -map "[v]" %s/%s-looped.mp4'
                % (path_folder, filename, path_folder, filename))

    def save_anim_mp4_2(self,
                        filename,
                        zs=[],
                        n_frame=360,
                        x_dim=1920,
                        y_dim=1080,
                        scale=10.0,
                        count=0):
        path_folder = 'output/%s' % filename
        if not os.path.exists(path_folder):
            print 'creating path: %s' % path_folder
            os.makedirs(path_folder)

        if (count <= 0):
            zs.append(zs[0])  # make it a full loop, return to first frame!
            first_z = zs[0]
            formatted_zs = map((lambda x: "%.3f" % x[0, 0]), zs)
            print "%d vectors: %s" % (len(zs), ", ".join(formatted_zs))
            print "%d images total" % (len(zs) * n_frame)
            print "---"

        if (len(zs) <= 1):
            z = zs[0]
            img = self.to_image(self.generate(z, x_dim, y_dim, scale))
            img.save('%s/%s-%04d.png' % (path_folder, filename, count))
            print ">> %d : %.3f" % (count, z[0, 0])
            print "---"
            print "%d images rendered" % count
            print "---"
            os.system(
                'ffmpeg -i %s/%s-%%04d.png -c:v libx264 -crf 0 -preset veryslow -framerate 30 %s/%s.mp4'
                % (path_folder, filename, path_folder, filename))
            return

        z1 = zs.pop(0)
        z2 = zs[0]

        print ">> (%.3f to %.3f) step #%d" % (z1[0, 0], z2[0, 0], len(zs))

        delta_z = (z2 - z1) / (n_frame + 1)
        total_frames = n_frame + 1

        for i in range(total_frames):
            z = z1 + delta_z * float(i)
            image_number = i + count
            img = self.to_image(self.generate(z, x_dim, y_dim, scale))
            img.save('%s/%s-%04d.png' % (path_folder, filename, image_number))

            z_output = ", ".join(str(x) for x in z[0].tolist())
            print ">> %d : %.3f" % (image_number, z[0, 0])
        self.save_anim_mp4_2(filename, zs, n_frame, x_dim, y_dim, scale,
                             image_number + 1)

    def save_anim_mp4_loop(self,
                           filename,
                           zs,
                           n_frame=360,
                           x_dim=1920,
                           y_dim=1080,
                           scale=10.0,
                           count=0):
        path_folder = 'output/%s' % filename
        if not os.path.exists(path_folder):
            print 'creating path: %s' % path_folder
            os.makedirs(path_folder)

        if (isinstance(zs, (int, long))):
            zs = self.generate_zs(zs)

        if (count <= 0):
            formatted_zs = map((lambda x: "%.3f" % x[0, 0]), zs)
            print "%d vectors: %s" % (len(zs), ", ".join(formatted_zs))
            print "%d images total" % (len(zs) * n_frame)
            print "---"
            zs.append(zs[0])  # make it a full loop, return to first frame!
            zs.append(
                zs[1])  # make it smoothly loop (knows next frame is coming)

        if (len(zs) <= 2):
            # z = zs[0]
            # img = self.to_image(self.generate(z, x_dim, y_dim, scale))
            # img.save('%s/%s-%04d.png' % (path_folder, filename, count))
            # print ">> %d : %.3f" % (count, z[0,0])
            print "---"
            print "%d images rendered" % count
            print "---"
            os.system(
                'ffmpeg -i %s/%s-%%04d.png -c:v libx264 -pix_fmt yuv420p -crf 0 -preset veryslow -framerate 30 %s/%s.mp4'
                % (path_folder, filename, path_folder, filename))
            # os.system('ffmpeg -i %s/%s.mp4 -filter "minterpolate='mi_mode=mci:mc_mode=aobmc:vsbmc=1:fps=90" %s/%s-interpolated-90fps.mp4')
            # os.system('docker cp %s/%s.mp4 boring_hamilton:/tmp/%s-docker.mp4')
            # os.system('docker run ')
            return

        z1 = zs.pop(0)
        z2 = zs[0]
        z3 = zs[1]

        print ">> (%.3f to %.3f) with %d steps left" % (z1[0, 0], z2[0, 0],
                                                        len(zs) - 2)

        total_frames = n_frame + 1
        for i in range(total_frames):
            percent_complete = float(i) / total_frames
            p = (math.asin(percent_complete * 2 - 1) + math.pi / 2) / math.pi
            delta_z1 = (z2 - z1) / (n_frame + 1)
            delta_z2 = (z3 - z2) / (n_frame + 1)
            delta_z = (p * delta_z2) + ((1 - p) * delta_z1)

            z = z1 + delta_z1 * float(i)
            image_number = i + count
            img = self.to_image(self.generate(z, x_dim, y_dim, scale))
            img.save('%s/%s-%04d.png' % (path_folder, filename, image_number))

            z_output = ", ".join(str(x) for x in z[0].tolist())
            print ">> #%d \tz = %.3f \t%.1f%% \t%.4f delta \t%0.4f" % (
                image_number, z[0, 0], percent_complete * 100, delta_z[0,
                                                                       0], p)
        self.save_anim_mp4_loop(filename, zs, n_frame, x_dim, y_dim, scale,
                                image_number + 1)

    def generate_zs(self, num):
        return map(lambda x: self.generate_z(), range(num))
Beispiel #11
0
        input_vecs[:, len(z) + 2] = torch.Tensor(r).view(-1)

        return input_vecs

    def __getitem__(self, idx):
        return self.input_vecs[idx]

    def __len__(self):
        return self.img_size**2


if __name__ == "__main__":
    print("Device used:", device)

    z = torch.rand(z_dim) * scale
    cppn = CPPN(z_dim=z_dim, model_size=model_size).to(device)

    dataset = PixelDataSet(img_size, z)
    dataloader = DataLoader(dataset, batch_size=batch_size)

    image = torch.zeros(img_size**2)

    with torch.no_grad():
        for i, batch in tqdm(enumerate(dataloader)):
            out = cppn(batch.to(device))
            del batch
            image[i * batch_size:(i + 1) * batch_size] = out.view(-1)

    image = image.numpy()
    image = image.reshape(img_size, img_size)
Beispiel #12
0
    def __init__(self,
                 x_dim=1280,
                 y_dim=720,
                 z_dim=5,
                 scale=16,
                 neurons_per_layer=6,
                 number_of_layers=8,
                 color_channels=1,
                 number_of_stills=5,
                 interpolations_per_image=24,
                 file_name='./p.png',
                 model_name=None,
                 outfile=None,
                 model_dir=None,
                 master=None):

        if (model_name == None or outfile == None or model_dir == None):
            print('Supply a model name and outfile!')
            exit(0)

        checkpoint_path = os.path.join(model_dir, model_name)
        outfile_name = checkpoint_path + 'meta_data'
        with open(outfile_name, 'rb') as fp:  # 'outfile' can be renamed
            data = pickle.load(fp)

        x_dim = data[0]
        y_dim = data[1]
        z_dim = data[2]
        scale = data[3]
        neurons_per_layer = data[4]
        number_of_layers = data[5]
        color_channels = data[6]
        test = data[7]

        self.cppn = CPPN(x_dim,
                         y_dim,
                         z_dim,
                         scale,
                         neurons_per_layer,
                         number_of_layers,
                         color_channels,
                         interpolations_per_image,
                         test=test)
        self.cppn.neural_net(True)

        self.cppn.load_model(model_name=model_name, model_dir=model_dir)

        self.outfile = outfile

        self.z_vector = [0] * self.cppn.z_dim

        # parameters that you want to send through the Frame class.
        Frame.__init__(self, master)

        #reference to the master widget, which is the tk window
        self.master = master

        self.varHolders = []
        self.saved_data = []
        self.scaler = DoubleVar()

        self.save_image_name = 'image_name.png'

        #with that, we want to then run init_window, which doesn't yet exist
        self.init_window()
Beispiel #13
0
class Window(Frame):

    # Define settings upon initialization. Here you can specify
    def __init__(self,
                 x_dim=1280,
                 y_dim=720,
                 z_dim=5,
                 scale=16,
                 neurons_per_layer=6,
                 number_of_layers=8,
                 color_channels=1,
                 number_of_stills=5,
                 interpolations_per_image=24,
                 file_name='./p.png',
                 model_name=None,
                 outfile=None,
                 model_dir=None,
                 master=None):

        if (model_name == None or outfile == None or model_dir == None):
            print('Supply a model name and outfile!')
            exit(0)

        checkpoint_path = os.path.join(model_dir, model_name)
        outfile_name = checkpoint_path + 'meta_data'
        with open(outfile_name, 'rb') as fp:  # 'outfile' can be renamed
            data = pickle.load(fp)

        x_dim = data[0]
        y_dim = data[1]
        z_dim = data[2]
        scale = data[3]
        neurons_per_layer = data[4]
        number_of_layers = data[5]
        color_channels = data[6]
        test = data[7]

        self.cppn = CPPN(x_dim,
                         y_dim,
                         z_dim,
                         scale,
                         neurons_per_layer,
                         number_of_layers,
                         color_channels,
                         interpolations_per_image,
                         test=test)
        self.cppn.neural_net(True)

        self.cppn.load_model(model_name=model_name, model_dir=model_dir)

        self.outfile = outfile

        self.z_vector = [0] * self.cppn.z_dim

        # parameters that you want to send through the Frame class.
        Frame.__init__(self, master)

        #reference to the master widget, which is the tk window
        self.master = master

        self.varHolders = []
        self.saved_data = []
        self.scaler = DoubleVar()

        self.save_image_name = 'image_name.png'

        #with that, we want to then run init_window, which doesn't yet exist
        self.init_window()

    #Creation of init_window
    def init_window(self):

        # changing the title of our master widget
        self.master.title("GUI")

        # allowing the widget to take the full space of the root window
        self.pack(fill=BOTH, expand=1)

        for i in range(self.cppn.z_dim):
            m = DoubleVar()
            w = Scale(self.master,
                      from_=-2,
                      to=2,
                      variable=m,
                      resolution=.01,
                      command=self.updateValue,
                      orient=HORIZONTAL)
            w.pack(side=LEFT)
            self.varHolders.append(m)

        w = Scale(self.master,
                  from_=1,
                  to=48,
                  variable=self.scaler,
                  resolution=.1,
                  command=self.updateValue,
                  orient=HORIZONTAL)
        w.pack(side=LEFT)

        # creating a button instance
        saveDataButton = Button(self.master,
                                text="save data",
                                command=self.saveData)
        saveDataButton.pack()

        pickleButton = Button(self.master,
                              text="pickle data",
                              command=self.pickleData)
        pickleButton.pack()

    def pickleData(self):
        print('Data pickled!')
        with open(self.outfile, 'wb') as f:  # can change 'outfile'
            pickle.dump(self.saved_data, f)

    def saveData(self):
        print('saved ', len(self.saved_data))
        zs = []
        for value in self.varHolders:
            zs.append(value.get())
        zs.append(self.scaler.get())
        self.saved_data.append(zs)

    def updateValue(self, event):
        for index, value in enumerate(self.varHolders):
            self.z_vector[index] = value.get()
        z = np.array(self.z_vector)
        self.cppn.scale = self.scaler.get()

        # if save set to false, just sets cppn.curImage to image
        self.cppn.save_png(z, self.save_image_name, save=False)
        self.showImg()

    def showImg(self):
        load = self.cppn.curImage
        resized = load.resize((800, 600), Image.ANTIALIAS)
        render = ImageTk.PhotoImage(load)

        # labels can be text or images
        img = Label(self, image=render)
        img.image = render
        img.place(x=0, y=0)
Beispiel #14
0
                    default=4,
                    help='number of cpu threads to use during\
 batch generation')

opt = parser.parse_args()

print(opt)

mnist = MNIST()

train_mnist = mnist.train_loader
test = mnist.test_loader

if opt.cuda:
    cuda_gpu = torch.device('cuda:0')
    model = CPPN(cuda_device=cuda_gpu)
    model.cuda()
else:
    model = CPPN()

le = 0
ld = 0
lg = 0

indices_train = np.arange(60000)

model.train()
for epoch in range(opt.n_epochs):
    print("STARTING EPOCH {}".format(epoch))
    for idx, (im, _) in enumerate(train_mnist):
        model.optimizer_discriminator.zero_grad()