Beispiel #1
0
    def _get_gauss_params(self):
        '''
        Returns the mean and covariance for the gaussian model on the whole
        patch (i.e., window to sample plus padding around it)
        '''

        means = np.zeros((3, self.patchSize * self.patchSize))
        covs = np.zeros((3, self.patchSize * self.patchSize,
                         self.patchSize * self.patchSize))

        path_mean = self.path_folder + '{}_means{}_indep'.format(
            "test", self.patchSize)
        path_cov = self.path_folder + '{}_covs{}_indep'.format(
            "test", self.patchSize)

        # check if  values are already precomputed and saved; otherwise do so first
        if os.path.exists(path_mean + '.npy') and os.path.exists(path_cov +
                                                                 '.npy'):

            means = np.load(path_mean + '.npy')
            covs = np.load(path_cov + '.npy')

        else:

            for c in [0, 1, 2]:

                #net = utlC.get_caffenet(self.netname)

                # get the imagenet data
                X, _, _ = utlD.get_imagenet_data()

                # get samples for fitting the distribution
                patchesMat = np.empty((0, self.patchSize * self.patchSize),
                                      dtype=np.float)
                for i in range(int(self.num_samples_fit / X.shape[0]) + 1):
                    # get a random (upper left) position of the patch
                    idx = random.sample(
                        range((self.image_dims[0] - self.patchSize) *
                              (self.image_dims[1] - self.patchSize)), 1)[0]
                    idx = np.unravel_index(
                        idx, (self.image_dims[0] - self.patchSize,
                              self.image_dims[1] - self.patchSize))
                    idx = [idx[0], idx[1]]
                    # get the patch from all the images in X, from the given channel
                    patch = X[:, c, idx[0]:idx[0] + self.patchSize,
                              idx[1]:idx[1] + self.patchSize]
                    patchesMat = np.vstack(
                        (patchesMat,
                         patch.reshape(
                             (X.shape[0], self.patchSize * self.patchSize))))

                # compute the mean and covariance of the collected samples
                means[c] = np.mean(patchesMat, axis=0)
                covs[c] = np.cov(patchesMat.T)

            # save the mean and the covariance
            np.save(path_mean, means)
            np.save(path_cov, covs)

        return means, covs
Beispiel #2
0
def save_minmax_values(netname):
    '''
    When X.npy is updated, this can be executed to also update the min/max
    values of the data (which is being used to cut off the values in the
    sampler so that we don't have overflowing values)
    '''
    #net = utlC.get_caffenet(netname)
    X, _, _ = utlD.get_imagenet_data()
    minMaxVals = np.zeros((2, 3, X.shape[-1], X.shape[-1]))
    minMaxVals[0] = np.min(X, axis=0)
    minMaxVals[1] = np.max(X, axis=0)
    path_folder = './gaussians/'
    if not os.path.exists(path_folder):
        os.makedirs(path_folder)
    np.save(path_folder + '{}_minMaxVals'.format(netname), minMaxVals)
Beispiel #3
0
# settings for sampling
sampl_style = 'conditional'  # choose: conditional / marginal
num_samples = 10
padding_size = 2  # important for conditional sampling,
# l = win_size+2*padding_size in alg 1
# (see paper)

# set the batch size - the larger, the faster computation will be
# (if caffe crashes with memory error, reduce the batch size)
batch_size = 30

# ------------------------ SET-UP ------------------------

# get the data
X_test, X_test_im, X_filenames = utlD.get_imagenet_data(net=model)

# get the label names of the 1000 ImageNet classes
classnames = utlD.get_imagenet_classnames()

if not test_indices:
    test_indices = [i for i in range(X_test.shape[0])]

# make folder for saving the results if it doesn't exist
path_results = './results/'
if not os.path.exists(path_results):
    os.makedirs(path_results)

# ------------------------ EXPERIMENTS ------------------------

# change the batch size of the network to the given value