Beispiel #1
0
 def save_png(self, save_dir, epoch):
     if torch.cuda.is_available():
         noise = self.noise.cuda()
     with torch.no_grad():
         gen_data = self.G(noise)
     gen_data = gen_data.cpu().detach().numpy()
     img_bhwc = np.transpose(gen_data, (0, 2, 3, 1))
     img_tile = plotting.img_tile(img_bhwc,
                                  aspect_ratio=1.0,
                                  border_color=1.0,
                                  stretch=True)
     img = plotting.plot_img(img_tile,
                             title='CIFAR10 samples ' + str(epoch) +
                             " epochs")
     plotting.plt.savefig(save_dir + "cifar_sample_feature_match_" +
                          str(epoch) + ".png")
Beispiel #2
0
parser.add_argument('--data_dir', type=str, default='~/temp/data')
parser.add_argument('--plot_title', type=str, default=None)
args = parser.parse_args()
print(args)

data_dir = args.data_dir

trainx, trainy = cifar10_data.load(data_dir)

ids = [[] for i in range(10)]
for i in range(trainx.shape[0]):
    if len(ids[trainy[i]]) < 10:
        ids[trainy[i]].append(i)
    if np.alltrue(np.asarray([len(_ids) >= 10 for _ids in ids])):
        break

images = np.zeros((10 * 10, 32, 32, 3), dtype='uint8')
for i in range(len(ids)):
    for j in range(len(ids[i])):
        images[10 * j + i] = trainx[ids[i][j]].transpose([1, 2, 0])
print(ids)

img_tile = plotting.img_tile(images,
                             aspect_ratio=1.0,
                             border_color=1.0,
                             stretch=True)
img = plotting.plot_img(
    img_tile, title=args.plot_title if args.plot_title != 'None' else None)
plotting.plt.savefig(args.save_dir + '/cifar10_orig_images.png')
plotting.plt.close('all')
Beispiel #3
0
        else:
            c += 1
            if c == pat: break
        # log progress to console
        print(
            "Iteration %d, time = %ds, train bits_per_dim = %.4f, test bits_per_dim = %.4f"
            % (epoch, time.time() - begin, train_loss_gen, test_loss_gen))
        sys.stdout.flush()
        if epoch % args.save_interval == -1:

            print("Generating samples...")

            # generate samples from the model
            sampled_x = sample_from_model(sess)
            img_tile = plotting.img_tile(sampled_x,
                                         aspect_ratio=1.0,
                                         border_color=1.0,
                                         stretch=True)
            img = plotting.plot_img(img_tile, title=args.data_set + ' samples')
            plotting.plt.savefig(
                os.path.join(args.save_dir,
                             '%s_sample%d.png' % (args.data_set, epoch)))
            plotting.plt.close('all')

            # save params
            saver.save(sess,
                       args.save_dir + '/params_' + args.data_set + '.ckpt')
            np.savez(args.save_dir + '/test_bpd_' + args.data_set + '.npz',
                     test_bpd=np.array(test_bpd))
print("online: ", first)
print("offline: ", testl)
Beispiel #4
0
        loss_unl += lu
        train_err += te
        train_err2 +=te2       
        e = train_batch_gen(trainx_unl2[t*args.batch_size:(t+1)*args.batch_size],lr)  # disc and gen for unlabeled data are different
        gen_loss += float(e)
        #clamp_D_fn()
    loss_lab /= nr_batches_train
    loss_unl /= nr_batches_train
    train_err /= nr_batches_train
    train_err2 /= nr_batches_train
    # test
    test_err = 0.
    for t in range(nr_batches_test):
        test_err += test_batch(testx[t*args.batch_size:(t+1)*args.batch_size],testy[t*args.batch_size:(t+1)*args.batch_size])
    test_err /= nr_batches_test

    # report
    print("Iteration %d, time = %ds, loss_lab = %.4f, loss_unl = %.4f, train err = %.4f, train err2 = %.4f,gen loss = %.4f,test err = %.4f" % (epoch, time.time()-begin, loss_lab, loss_unl, train_err,train_err2,gen_loss,test_err))
    sys.stdout.flush()

    # generate samples from the model
    sample_x = samplefun()
    img_bhwc = np.transpose(sample_x[:100,], (0, 2, 3, 1))
    img_tile = plotting.img_tile(img_bhwc, aspect_ratio=1.0, border_color=1.0, stretch=True)
    img = plotting.plot_img(img_tile, title='CIFAR10 samples')
    plotting.plt.savefig("cifar_sample_feature_match_50_pai.png")

    # save params
    np.savez('disc_params.npz', *[p.get_value() for p in disc_params])
    np.savez('gen_params.npz', *[p.get_value() for p in gen_params])
Beispiel #5
0
    test_list.append(test_err)

    # report
    print(
        "Epoch %d, time = %ds, loss_lab = %.3f, learning_rate = %.6f,train err= %.4f, test err = %.4f"
        % (epoch, time.time() - begin, loss_lab, learning_rate_var.get_value(),
           train_err, test_err))
    sys.stdout.flush()

    # generate samples from the model
    sample_y = np.int32(
        np.random.choice(num_classes, size=(args.batch_size), replace=False))
    sample_x = samplefun(sample_y)
    img_bhwc = np.transpose(sample_x[:100, ], (0, 2, 3, 1))
    img_tile = plotting.img_tile(img_bhwc,
                                 aspect_ratio=1.0,
                                 border_color=1.0,
                                 stretch=True)
    img = plotting.plot_img(img_tile, title='CIFAR-100 samples')
    plotting.plt.savefig(args.results_dir + args.dataset +
                         '/cifar100_sample.png')
    if epoch % 20 == 0:
        NNdiff = np.sum(np.sum(np.sum(np.square(
            np.expand_dims(sample_x, axis=1) - np.expand_dims(trainx, axis=0)),
                                      axis=2),
                               axis=2),
                        axis=2)
        NN = trainx[np.argmin(NNdiff, axis=1)]
        NN = np.transpose(NN[:100], (0, 2, 3, 1))
        NN_tile = plotting.img_tile(NN,
                                    aspect_ratio=1.0,
                                    border_color=1.0,