Ejemplo n.º 1
0
                                                            volatile=True)

            # forward (recontruction)
            outputs = model.forward(inputs)

            # forward (sampling)
            samples = []
            for k in range(num_samples):
                indices = np.random.choice(num_dists, args.batch)
                sample = model.forward(inputs,
                                       mean=to_var(means[indices]),
                                       log_var=to_var(log_vars[indices]))
                samples.append(sample)

            # visualize
            outputs = visualize(inputs, outputs)
            targets = visualize(inputs, targets)
            samples = [visualize(inputs, sample) for sample in samples]
            inputs = visualize(inputs)

            # logger
            logger.image_summary('{0}-inputs'.format(split), inputs, step)
            logger.image_summary('{0}-outputs'.format(split),
                                 zip(inputs, outputs), step)
            logger.image_summary('{0}-targets'.format(split),
                                 zip(inputs, targets), step)

            for k, sample in enumerate(samples):
                logger.image_summary('{0}-samples-{1}'.format(split, k + 1),
                                     zip(inputs, sample), step)
Ejemplo n.º 2
0
def analyze_reprs(max_dims=16, threshold=.5, bound=8., step=.2):
    reprs_path = os.path.join('exp', args.exp, 'reprs')
    mkdir(reprs_path, clean=True)

    images_path = os.path.join(reprs_path, 'images')
    mkdir(images_path, clean=True)

    # statistics
    x, ym, yv = [], [], []
    for k in range(means.shape[1]):
        x.extend([k, k])
        ym.extend([np.min(means[:, k]), np.max(means[:, k])])
        yv.extend([np.min(log_vars[:, k]), np.max(log_vars[:, k])])

    plt.figure()
    plt.bar(x, ym, .5, color='b')
    plt.xlabel('dimension')
    plt.ylabel('mean')
    plt.savefig(os.path.join(images_path, 'means.png'), bbox_inches='tight')

    plt.figure()
    plt.bar(x, yv, .5, color='b')
    plt.xlabel('dimension')
    plt.ylabel('log(var)')
    plt.savefig(os.path.join(images_path, 'vars.png'), bbox_inches='tight')

    # dimensions
    values = np.arange(-bound, bound + step, step)

    magnitudes = np.max(np.abs(means), axis=0)
    indices = np.argsort(-magnitudes)

    dimensions = [k for k in indices if magnitudes[k] > threshold][:max_dims]
    print('==> dominated dimensions = {0}'.format(dimensions))

    for split in ['train', 'test']:
        inputs, targets = iter(loaders[split]).next()
        inputs, targets = to_var(inputs, volatile=True), to_var(targets,
                                                                volatile=True)

        outputs, z = model.forward(inputs, returns='z')

        for dim in tqdm(dimensions):
            repr = to_np(z).copy()

            samples = []
            for val in tqdm(values, leave=False):
                repr[:, dim] = val
                sample = model.forward(inputs, z=to_var(repr, volatile=True))
                samples.append(visualize(inputs, sample))

            for k in range(args.batch):
                images = [sample[k] for sample in samples]
                image_path = os.path.join(
                    images_path, '{0}-{1}-{2}.gif'.format(split, k, dim))
                save_images(images,
                            image_path,
                            duration=.1,
                            channel_first=True)

        inputs = visualize(inputs)
        for k in range(args.batch):
            image_path = os.path.join(images_path,
                                      '{0}-{1}.png'.format(split, k))
            save_image(inputs[k], image_path, channel_first=True)

    # visualization
    with open(os.path.join(reprs_path, 'index.html'), 'w') as fp:
        print('<h3>statistics</h3>', file=fp)
        print('<img src="{0}">'.format(os.path.join('images', 'means.png')),
              file=fp)
        print('<img src="{0}">'.format(os.path.join('images', 'vars.png')),
              file=fp)

        print('<h3>inputs</h3>', file=fp)
        print('<table border="1" style="table-layout: fixed;">', file=fp)
        for split in ['train', 'test']:
            print('<tr>', file=fp)
            for k in range(args.batch):
                image_path = os.path.join('images',
                                          '{0}-{1}.png'.format(split, k))
                print(
                    '<td halign="center" style="word-wrap: break-word;" valign="top">',
                    file=fp)
                print(
                    '<img src="{0}" style="width:128px;">'.format(image_path),
                    file=fp)
                print('</td>', file=fp)
            print('</tr>', file=fp)
        print('</table>', file=fp)

        for dim in dimensions:
            print('<h3>dimension [{0}]</h3>'.format(dim), file=fp)
            print('<table border="1" style="table-layout: fixed;">', file=fp)
            for split in ['train', 'test']:
                print('<tr>', file=fp)
                for k in range(args.batch):
                    image_path = os.path.join(
                        'images', '{0}-{1}-{2}.gif'.format(split, k, dim))
                    print(
                        '<td halign="center" style="word-wrap: break-word;" valign="top">',
                        file=fp)
                    print('<img src="{0}" style="width:128px;">'.format(
                        image_path),
                          file=fp)
                    print('</td>', file=fp)
                print('</tr>', file=fp)
            print('</table>', file=fp)
Ejemplo n.º 3
0
        print('==> snapshot "{0}" loaded (epoch {1})'.format(
            args.resume, epoch))
    else:
        raise FileNotFoundError('no snapshot found at "{0}"'.format(
            args.resume))

    model.train(False)
    for k in trange(args.samples // args.batch):
        outputs = model.forward(batch_size=args.batch)
        outputs = outputs.data.cpu().numpy()

        for i, output in enumerate(outputs):
            index = k * args.batch + i
            scipy.misc.imsave(
                os.path.join(demo_path, '{:08d}.png'.format(index)),
                visualize(output))
            np.save(os.path.join(demo_path, '{:08d}.npy'.format(index)),
                    output)

    # visualization
    with open(os.path.join(demo_path, 'index.html'), 'w') as fp:
        print('<table border="1" style="table-layout: fixed;">', file=fp)
        for k in range(0, args.samples, 8):
            print('<tr>', file=fp)
            for index in range(k, k + 8):
                print(
                    '<td halign="center" style="word-wrap: break-word;" valign="top">',
                    file=fp)
                print('<img src="{0}" style="width:128px;">'.format(
                    '{:08d}.png'.format(index)),
                      file=fp)