Example #1
0
def plot(W):
    """
    Plot basis vectors.
    
    :param W: Basis matrix of the fitted factorization model.
    :type W: `numpy.matrix`
    """
    set_cmap('gray')
    blank = new("L", (225 + 6, 280 + 6))
    for i in xrange(5):
        for j in xrange(5):
            basis = np.array(W[:, 5 * i + j])[:, 0].reshape((56, 46))
            basis = basis / np.max(basis) * 255
            basis = 255 - basis
            ima = fromarray(basis)
            expand(ima, border = 1, fill = 'black')
            blank.paste(ima.copy(), (j * 46 + j, i * 56 + i))
    imshow(blank)
    savefig("orl_faces.png")
def plot(W):
    """
    Plot basis vectors.
    
    :param W: Basis matrix of the fitted factorization model.
    :type W: `numpy.matrix`
    """
    set_cmap('gray')
    blank = new("L", (225 + 6, 280 + 6))
    for i in range(5):
        for j in range(5):
            basis = np.array(W[:, 5 * i + j])[:, 0].reshape((56, 46))
            basis = basis / np.max(basis) * 255
            basis = 255 - basis
            ima = fromarray(basis)
            expand(ima, border=1, fill='black')
            blank.paste(ima.copy(), (j * 46 + j, i * 56 + i))
    imshow(blank)
    savefig("orl_faces.png")
Example #3
0
def plot(W):
    """
    Plot basis vectors.
    
    :param W: Basis matrix of the fitted factorization model.
    :type W: `numpy.matrix`
    """
    set_cmap('gray')
    blank = new("L", (133 + 6, 133 + 6))
    for i in xrange(7):
        for j in xrange(7):
            basis = np.array(W[:, 7 * i + j])[:, 0].reshape((19, 19))
            basis = basis / np.max(basis) * 255
            basis = 255 - basis
            ima = fromarray(basis)
            ima = ima.rotate(180)
            expand(ima, border=1, fill='black')
            blank.paste(ima.copy(), (j * 19 + j, i * 19 + i))
    imshow(blank)
    savefig("cbcl_faces.png")
Example #4
0
def plot(W):
    """
    Plot basis vectors.
    
    :param W: Basis matrix of the fitted factorization model.
    :type W: `numpy.matrix`
    """
    set_cmap('gray')
    blank = new("L", (133 + 6, 133 + 6))
    for i in range(7):
        for j in range(7):
            basis = np.array(W[:, 7 * i + j])[:, 0].reshape((19, 19))
            basis = basis / np.max(basis) * 255
            basis = 255 - basis
            ima = fromarray(basis)
            ima = ima.rotate(180)
            expand(ima, border=1, fill='black')
            blank.paste(ima.copy(), (j * 19 + j, i * 19 + i))
    imshow(blank)
    savefig("cbcl_faces.png")
Example #5
0
def normalize(image):
    cropped = image.crop(image.getbbox())
    padded = Image.new('RGBA', (max(cropped.size), max(cropped.size)))
    size = max(cropped.size)
    offset = (int((size - cropped.width) / 2), int(
        (size - cropped.height) / 2))
    padded.paste(cropped, offset)

    normalized = expand(padded, int(padded.height / 10))
    normalized.convert('LA')
    normalized.thumbnail((28, 28), Image.ANTIALIAS)
    return normalized
Example #6
0
def plot_basis(U, ncol, nrow, size=32):
    """
    Plots all basis images
    - U     : the basis matrix
    - ncol  : # cols in the canvas
    - nrow  : # rows in the canvas
    - size  : the height/width of each basis image (assuming it's a square)
    """
    # sns.heatmap(U)
    # plt.show()
    # return
    plt.set_cmap("gray")
    canvas = Image.new(
        "L", (ncol * size + ncol + 1, nrow * size + nrow + 1))  # (w, h)
    for i in range(nrow):
        for j in range(ncol):
            basis = U[:, i * ncol + j].reshape((size, size))
            basis = basis / basis.max() * 255
            img = expand(Image.fromarray(basis), border=1, fill=255)
            canvas.paste(img.copy(), (j * size + j, i * size + i))
    plt.imshow(canvas)
    plt.show()