예제 #1
0
def outputTest(epoch, batch_idx, content, size):
    model.eval()
    test_loss = 0
    labels = np.array([], dtype='str').reshape(0, 1)
    index = 0

    if not args.output_tsne:
        probability = np.array([], dtype='str').reshape(0, 10)
    else:
        probability = np.array([], dtype='str').reshape(0, 2)

    for data, target in test_loader:
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data, volatile=True), Variable(target)
        output, fc1 = model(data)
        labels = np.concatenate(
            (labels, np.expand_dims(target.data.cpu().numpy(),
                                    axis=1).astype('str')),
            axis=0)
        if not args.output_tsne:
            probability = np.concatenate(
                (probability, np.exp(output.data.cpu().numpy()).astype('str')),
                axis=0)
        else:
            features_embedded = TSNE(n_components=2,
                                     perplexity=40).fit_transform(
                                         fc1.data.cpu().numpy()).astype('str')
            probability = np.concatenate(
                (probability, features_embedded.astype('str')), axis=0)

        test_loss += F.nll_loss(
            output, target, size_average=False).data[0]  # sum up batch loss
        #pred = output.data.max(1, keepdim=True)[1] # get the index of the max log-probability
        index += 1
        if size / args.test_batch_size >= index:
            break

    test_loss /= size

    epochs = np.expand_dims(np.repeat(str(epoch), size), axis=1)
    batchIds = np.expand_dims(np.repeat(str(batch_idx), size), axis=1)
    loss = np.expand_dims(np.repeat(test_loss, size), axis=1)
    path = np.expand_dims(np.array([str(i + 1) + '.png' for i in range(size)]),
                          axis=1)
    combine = np.concatenate(
        (epochs, loss, batchIds, labels, path, probability), axis=1)

    return np.concatenate((content, combine), axis=0)
예제 #2
0
# dist = cdist(feas_normalized, feas_normalized)
# dist[np.arange(569), np.arange(569)] = np.nan
# plt.matshow(dist)
# plt.colorbar()
# plt.show()

embed2 = TSNE(n_components=2).fit_transform(feas_normalized)

height = 128
width = 64
embed2 = embed2 - embed2.min(axis=0)
# np.median(np.abs(np.diff(embed2, axis=0)), axis=0)
space = 64 + 16
embed2 *= space
embed2 = embed2.astype(int)
extend = np.array([
    height,
    width,
])

shape = tuple((embed2.max(axis=0).astype(int) + extend).tolist()) + (3, )
print('res shape', shape)
res = np.ones(shape).astype(np.uint8) * 255

for ind in range(feas.shape[0]):
    img_name = feask[ind]
    img = cv2.imread(img_name)
    img2 = cvb.resize_keep_ar(img, height, width)
    if not (img2.shape[0] <= height and img2.shape[1] <= width):
        img2 = cvb.resize_keep_ar(img, width, width)