Exemple #1
0
def save_top(data, column, columnuid, layeruid, save_path):
    t = column.fwd(data)
    top_shape = column.top_shape()
    if len(top_shape) == 4:
        im = w2i.tile_imgs(t)
        #       im = Image.fromarray(dp.denormalize(c[0,:]).astype(np.uint8).squeeze(),mode='L')
        im.save(save_path + 'col' + str(columnuid) + '_level' + str(layeruid) +
                '_top.png')
def save_column_means(dp, columns, immap):
    #generate mean image for each column
    means = np.zeros([N_COLUMNS] + list(dp.shape()[1:]))
    for i, (col, keys) in enumerate(immap['col2key'].iteritems()):
        mb = dp.get_mb_by_keys(keys)
        x = np.mean(mb[0], axis=0)
        means[i, :, :, :] = x
    im = w2i.tile_imgs(means)
    im.save(IMG_DIR + "mean_imgs.png")
def save_top(dp, columns, immap):
    for n, column in columns.iteritems():
        mapped_samples, _, _ = get_mapped_batch(dp, n, immap)
        if len(mapped_samples) == 0:
            return
        mapped_batch = np.zeros(dp.shape())
        mapped_batch[0:len(mapped_samples), :] = mapped_samples
        t = column.fwd(mapped_batch)
        top_shape = column.top_shape()
        if len(top_shape) == 4:
            im = w2i.tile_imgs(t)
            #       im = Image.fromarray(dp.denormalize(c[0,:]).astype(np.uint8).squeeze(),mode='L')
            im.save(IMG_DIR + 'col' + str(n) + '_level' +
                    str(layer_number + 1) + '_top.png')
        elif len(top_shape) == 2:
            t = t.reshape([1] + top_shape + [1])
            im = w2i.tile_imgs(t)
            im.save(IMG_DIR + 'col' + str(n) + '_level' +
                    str(layer_number + 1) + '_top.png')
        else:
            print("Top not saved.  Dimenstions {}".format(top_shape))
Exemple #4
0
def save_recon(data, column, columnuid, layeruid, save_path):
    #Reconstruction of a batch
    d, r = column.fwd_back(data)
    print(np.max(d))
    print(np.max(r))
    s = list(d.shape)
    s[0] = s[0] * 2
    d_r_array = np.empty(s, dtype=d.dtype)
    d_r_array[0::2, :, :, :] = d
    d_r_array[1::2, :, :, :] = r
    im = w2i.tile_imgs(d_r_array)
    im.save(save_path + 'col' + str(columnuid) + '_img_recon_level' +
            str(layeruid) + '.png')
def save_recon(dp, columns, immap):
    for n, column in columns.iteritems():
        #Reconstruct mapped examples
        mapped_samples, _, _ = get_mapped_batch(dp, n, immap)
        if len(mapped_samples) == dp.shape()[0]:
            d, r = column.fwd_back(mapped_samples)
            s = list(d.shape)
            s[0] = s[0] * 2
            d_r_array = np.empty(s, dtype=d.dtype)
            d_r_array[0::2, :, :, :] = d
            d_r_array[1::2, :, :, :] = r
            im = w2i.tile_imgs(d_r_array)
            im.save(IMG_DIR + 'col' + str(n) + '_mapped_recon_level' +
                    str(layer_number + 1) + '.png')
Exemple #6
0
def save_injection(column, columnuid, layeruid, save_path):
    #Reconstruction of an injected filter
    top_shape = column.top_shape()
    a = np.zeros(top_shape)
    input_shape = column.bottom_shape()
    imgs = np.zeros([top_shape[-1]] + list(input_shape[1:]))
    for channel in range(top_shape[-1]):
        b = a.copy()
        if len(top_shape) == 4:
            b[0, top_shape[1] / 2, top_shape[2] / 2, channel] = 1
        elif len(top_shape) == 2:
            b[0, channel] = 1
        c = column.inject(b)
        imgs[channel, :, :, :] = c[0, :, :, :]
    im = w2i.tile_imgs(imgs, normalize=True)
    #       im = Image.fromarray(dp.denormalize(c[0,:]).astype(np.uint8).squeeze(),mode='L')
    im.save(save_path + 'col' + str(columnuid) + '_level' + str(layeruid) +
            '_inject.png')
def save_injection(dp, columns, immap):
    for n, column in columns.iteritems():
        #Reconstruct an injected value
        top_shape = column.top_shape()
        a = np.zeros(top_shape)
        input_shape = dp.shape()
        imgs = np.zeros([top_shape[-1]] + list(input_shape[1:]))
        for channel in range(top_shape[-1]):
            b = a.copy()
            if len(top_shape) == 4:
                b[0, top_shape[1] / 2, top_shape[2] / 2, channel] = 1
            elif len(top_shape) == 2:
                b[0, channel] = 1
            c = column.inject(b)
            imgs[channel, :, :, :] = c[0, :, :, :]
        im = w2i.tile_imgs(imgs, normalize=True)
        #       im = Image.fromarray(dp.denormalize(c[0,:]).astype(np.uint8).squeeze(),mode='L')
        im.save(IMG_DIR + 'col' + str(n) + '_level' + str(layer_number + 1) +
                '_decode.png')
Exemple #8
0
if __name__ == '__main__':
    cifarpath = sys.argv[1]
    with open(cifarpath, 'rb') as fi:
      datadict = cPickle.load(fi)
    data = datadict['data']
    data = np.reshape(data,[len(data), 3,32,32])
    original_shape = data.shape
    print("Original Data Shape {}".format(original_shape))
    a,b,dog_data = dog(data,1,0.5)
    print("Dog Data Shape {}".format(dog_data.shape))
    appended_mat = np.append(
                             np.append(data[0:2], 
                                       a[0:2],
                                       axis=0
                                       ),
                             np.append(b[0:2],
                                       dog_data[0:2], 
                                       axis =0
                                       ),
                             axis=0
                             )
    print(appended_mat.shape)
    comparison_mat = np.transpose(appended_mat,[0,2,3,1])
    comp = w2i.tile_imgs(comparison_mat)
    plt.imshow(comp)
    plt.show()
    newdatadict = {'data':dog_data,
                   'labels':datadict['labels']}
    with open("dog_cifar_data", 'w') as fo:
      cPickle.dump(newdatadict, fo)
    
def save_exemplars(dp, columns, immap):
    for i in range(len(columns)):
        sample, l, k = get_mapped_batch(dp, i, immap)
        if k != None and len(k) > 0:
            im = w2i.tile_imgs(sample)
            im.save(IMG_DIR + "col" + str(i) + "_exemplars.png")
Exemple #10
0
    return np.dot(inputs, ZCAMatrix)  #Data whitening


def zca_whitening_sk(inputs):
    pca = PCA(whiten=True)
    transformed = pca.fit_transform(inputs)
    pca.whiten = False
    zca = pca.inverse_transform(transformed)
    return zca


if __name__ == '__main__':
    cifarpath = sys.argv[1]
    with open(cifarpath, 'rb') as fi:
        datadict = cPickle.load(fi)
    data = datadict['data']
    original_shape = data.shape
    print("Original Data Shape {}".format(original_shape))
    data = np.reshape(data, [len(data), -1])
    white_data = zca_whitening(data)
    comp = w2i.tile_imgs(
        np.transpose(
            np.reshape(np.append(data[0, :], white_data[0, :], axis=0),
                       [2, 3, 32, 32])), [0, 2, 3, 1])
    plt.imshow(comp)
    plt.show()
    white_data = np.reshape(white_data, original_shape)
    newdatadict = {'data': white_data, 'labels': datadict['labels']}
    with open("zca_cifar_data", 'w') as fo:
        cPickle.dump(newdatadict, fo)