Esempio n. 1
0
def plot_multiple_vectors_as_images(dVectors, title=None):
    """ Based on dictionary dVectors, multiple vectors are
    plotted next to each other. """

    numbVectors = len(dVectors)

    # init figure
    fig = plt.figure(figsize=(15, 4))
    grid = axes_grid1.AxesGrid(fig,
                               111,
                               nrows_ncols=(1, numbVectors),
                               axes_pad=0.55,
                               share_all=True,
                               cbar_location="right",
                               cbar_mode="each",
                               cbar_size="5%",
                               cbar_pad="2%",)
    if title is not None:
        fig.suptitle(title)

    # plot vectors from dVectors
    for idx, (key, relVal) in enumerate(dVectors.items()):
        im = grid[idx].imshow(render.vec2im(relVal), cmap='jet')
        grid[idx].set_title(key)
        grid[idx].axis('off')
        grid.cbar_axes[idx].colorbar(im)

    # finalize figure
    if title is None:
        fig.tight_layout()
    else:
        fig.tight_layout(rect=[0, 0, 0.95, .95])
Esempio n. 2
0
def unique_shapes():
    """Finds the unique shapes w.r.t. rotation. """

    # check whether calling function is appropriate
    errorMessage = "Only meant for TrianglesAndSquares dataset"
    assert settings.dataName == 'TrianglesAndSquares', errorMessage

    dirPath = os.path.dirname(os.path.realpath(__file__))
    #fileName = dirPath + r"\results_tools\uniqueShapes.pickle"
    fileName = os.path.join(dirPath, "results_tools", "uniqueShapes.pickle")
    

    try:
        return pickle.load(open(fileName, "rb"), encoding='latin1')  # the encoding was needed in order to load a Python 2.7 pickle in Python 3.6. Maybe this is not necessary anymore at a later stage.
    except (OSError, IOError):

        # load data
        X, Y = data_loader.load_data()
        imageDim = len(X['train'][0])

        uniqueShapeRotSq = np.empty((0, imageDim))
        uniqueShapeRotTr = np.empty((0, imageDim))

        for idx in range(len(X['train'])):

            x = X['train'][[idx]]
            y = Y['train'][[idx]]

            # find xMid in the middle of the image of x
            xImage = render.vec2im(x)
            halfIdx = int(math.floor(len(xImage)/2))
            xMid = xImage[halfIdx, halfIdx]

            # set shape color to 1 and background color to 0
            x[0][x[0] == xMid] = 1
            x[0][np.logical_not(x[0] == 1)] = 0

            if y[0][0] == 1:
                # a square
                considered = any(np.equal(uniqueShapeRotSq, x[0]).all(1))
                if not considered:
                    uniqueShapeRotSq = np.vstack([uniqueShapeRotSq, x[0]])
            else:
                # a triangle
                considered = any(np.equal(uniqueShapeRotTr, x[0]).all(1))
                if not considered:
                    uniqueShapeRotTr = np.vstack([uniqueShapeRotTr, x[0]])

        # sort results
        uniqueShapeRotSq = uniqueShapeRotSq[np.argsort([min(np.flatnonzero(im)) for im in uniqueShapeRotSq])]
        uniqueShapeRotTr = uniqueShapeRotTr[np.argsort([min(np.flatnonzero(im)) for im in uniqueShapeRotTr])]

        # save results
        uniqShapeRot = {'square': uniqueShapeRotSq,
                        'triangle': uniqueShapeRotTr}
        with open(fileName, 'wb') as handle:
            pickle.dump(uniqShapeRot, handle, protocol=pickle.HIGHEST_PROTOCOL)

        return uniqShapeRot
Esempio n. 3
0
def union_shapes():
    """Returns the union of all squares and triangle, respectively. """

    # check whether calling function is appropriate
    errorMessage = "Only meant for TrianglesAndSquares dataset"
    assert settings.dataName == 'TrianglesAndSquares', errorMessage

    dirPath = os.path.dirname(os.path.realpath(__file__))
    fileName = dirPath + r"\results_tools\unionShapes.pickle"

    try:
        return pickle.load(open(fileName, "rb"))
    except (OSError, IOError):

        # load data
        X, Y = data_loader.load_data()
        imageDim = len(X['train'][0])

        # init images of inner circles
        unionSq = np.zeros((imageDim,))
        unionTr = np.zeros((imageDim,))

        for idx in range(len(X['train'])):

            x = X['train'][[idx]]
            y = Y['train'][[idx]]

            # find xMid in the middle of the image of x
            xImage = render.vec2im(x)
            halfIdx = int(math.floor(len(xImage)/2))
            xMid = xImage[halfIdx, halfIdx]

            # set shape color to 1 and background color to 0
            x[0][x[0] == xMid] = 1
            x[0][np.logical_not(x[0] == 1)] = 0

            if y[0][0] == 1:
                # a square
                unionSq += x[0]
            else:
                # a triangle
                unionTr += x[0]

        # rescale result
        unionSq[unionSq > 0] = 1
        unionTr[unionTr > 0] = 1

        # save results
        res = (unionSq, unionTr)
        with open(fileName, 'wb') as handle:
            pickle.dump(res, handle, protocol=pickle.HIGHEST_PROTOCOL)

        # plot shapes
        plot_vector_as_image(unionSq)
        plot_vector_as_image(unionTr)

        return res
Esempio n. 4
0
def plot_vector_as_image(vector, title=None):
    """ Plots a vector as image with colorbar. """

    fig = plt.figure()
    ax = fig.add_subplot(111)
    if title is not None:
        ax.set_title(title)
    cax = ax.matshow(render.vec2im(vector))
    fig.colorbar(cax)
Esempio n. 5
0
    ax.scatter(train_features[1].numpy()[filter][:, 0],
               train_features[1].numpy()[filter][:, 1],
               marker=label_symbols[label_idx],
               alpha=0.3)
ax.legend()
ax.grid(True)

# inspect weights to first layer
import mpl_toolkits.axes_grid1 as axes_grid1
import render
W1 = model.layers[0].weights[0].numpy()
B = model.layers[0].weights[1].numpy()
dim = int(W1.shape[1])
W11 = W1[:, 0]
W12 = W1[:, 1]
W11Im = render.vec2im(W11)
W12Im = render.vec2im(W12)

# init figure
fig = plt.figure()
grid = axes_grid1.AxesGrid(
    fig,
    111,
    nrows_ncols=(1, dim),
    axes_pad=0.55,
    share_all=True,
    cbar_location="right",
    cbar_mode="each",
    cbar_size="5%",
    cbar_pad="2%",
)
Esempio n. 6
0
    def plot_NN_layer(self, layer_idx, nr_to_plot=None):
        """Plots of nr_to_plot nearest neighbors (NN) of a layer in a grid. """

        if nr_to_plot is None:
            nr_to_plot = self.k + 1  # plot input data and all neighbors found

        # init data
        NN = self.NN_layers[layer_idx]
        confirmation_perc_NN_pred = 100 * np.mean(
            np.argmax(NN['Y'], 1) == np.argmax(self.Y_input_predict, 1)[0])
        confirmation_perc_NN_true_label = 100 * np.mean(
            np.argmax(NN['Y'], 1) == np.argmax(self.Y_input, 1)[0])

        # init figure
        nr_of_rows = math.ceil(math.sqrt(nr_to_plot))
        fig, ax = plt.subplots(nr_of_rows,
                               nr_of_rows,
                               sharex='col',
                               sharey='row',
                               figsize=(16, 8))
        fig.suptitle(
            f'Plot of nearest neighbors (NN) in layer {NN["layer name"]} with {round(confirmation_perc_NN_pred,2)}% and {round(confirmation_perc_NN_true_label,2)}% of NN confirm with prediction and true label, resp.'
        )

        # plot input image and all nearest neighbors
        nr_NN_plotted = 0
        for i in range(nr_of_rows):
            for j in range(nr_of_rows):

                # format subplot
                ax[i, j].set_xticklabels([])
                ax[i, j].set_yticklabels([])

                if nr_NN_plotted + 1 >= nr_to_plot:
                    continue  # all intended images plotted

                if i + j == 0:
                    # plot input image
                    cax = ax[0, 0].imshow(render.vec2im(self.X_input),
                                          vmin=0,
                                          vmax=1)
                    # ax[0, 0].matshow(render.vec2im(self.X_input))
                    ax[0, 0].set_title(
                        f'Input image; label {self.Y_input}; predict {self.Y_input_predict.round(2)}',
                        fontsize=8)
                    continue

                ax[i, j].imshow(render.vec2im(NN['X'][nr_NN_plotted]),
                                vmin=0,
                                vmax=1)
                # ax[i, j].matshow(render.vec2im(NN['X'][nr_NN_plotted]))
                ax[i, j].set_title(
                    f'NN {nr_NN_plotted}; label = {NN["Y"][nr_NN_plotted]}; distance = {round(NN["distances"][nr_NN_plotted], 4)}',
                    fontsize=8)
                nr_NN_plotted += 1

        # finalize figure
        fig.subplots_adjust(right=0.8)
        # put colorbar at desire position
        cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
        fig.colorbar(cax, cax=cbar_ax)