def main():
    X = generate_dataset(shape="blobs")
    D = pairwise_distances(X)  # euclidean distance as distance metric
    A = gaussian_kernel(D, is_sym=True)  # Gaussian distance as affinity metric

    # K-MEANS
    clusters, _ = apply_kmeans(X)
    plot_clustering_result(X,
                           A,
                           clusters,
                           clustering_name="K means clustering")

    # DBSCAN
    clusters, noise = apply_dbscan(X, D)
    plot_clustering_result(X,
                           A,
                           clusters,
                           noise,
                           clustering_name="DBSCAN clustering")

    # EIGENVECTOR BASED CLUSTERING
    A_eigen = gaussian_kernel(
        D, mult=0.05, is_sym=True)  # Gaussian distance as affinity metric
    clusters, noise = apply_eigenvector_based(X, A_eigen)
    plot_clustering_result(X,
                           A_eigen,
                           clusters,
                           noise,
                           clustering_name="Eigenvector based clustering")
Example #2
0
def hybridImage(im1, im2, cutoff_low, cutoff_high):
    '''
    Inputs:
        im1:    RGB (height x width x 3) or a grayscale (height x width) image
                as a numpy array.
        im2:    RGB (height x width x 3) or a grayscale (height x width) image
                as a numpy array.
        cutoff_low: standard deviation for the low-pass filter
        cutoff_high: standard deviation for the high-pass filter
        
    Output:
        Return the combination of both images, one filtered with a low-pass filter
        and the other with a high-pass filter.
    '''    
    # For a low-pass filter, Oliva et al. suggest using a standard 2D Gaussian filter.

    # For a high-pass filter, use the impulse filter minus the Gaussian filter, 
    # which can be computed by subtracting the Gaussian-filtered image from the original.
    '''
    high_Gaussian_filter = utils.gaussian_kernel(cutoff_high, cutoff_high*5)
    signal_filter = signal.unit_impulse(high_Gaussian_filter)
    high_filter = signal_filter - high_Gaussian_filter
    '''
    Gaussian_filter1 = utils.gaussian_kernel(cutoff_low, cutoff_low*4)
    display_frequency_image(Gaussian_filter1)
    plt.axis('off')
    plt.savefig('./image/FFT_LOW')     

    # Gaussian_filter2 = utils.gaussian_kernel(cutoff_high, cutoff_high*4)
    # filter_impulse = signal.unit_impulse(Gaussian_filter2.shape, 'mid')
    # Gaussian_filter22 = filter_impulse - Gaussian_filter2
    # display_frequency_image(Gaussian_filter22)
    # plt.axis('off')
    # plt.savefig('./image/FFT_HIGH')

    Gaussian_filter2 = utils.gaussian_kernel(cutoff_high, cutoff_high*4)
    plt.figure()
    plt.imshow(Gaussian_filter2)
    plt.axis('off')
    plt.savefig('./image/FFT_HIGH')

    im1_lowpass = cv2.filter2D(im1,-1,Gaussian_filter1) #filter_image(im1,Gaussian_filter1)
    im2_highpass = im2 - cv2.filter2D(im2,-1,Gaussian_filter2) #filter_image(im2,Gaussian_filter2)
    
    plt.figure()
    plt.imshow(im1_lowpass, cmap='gray')
    plt.axis('off')
    plt.savefig('./image/dog_filter_low.jpg')
    
    plt.figure()
    plt.imshow(im2_highpass, cmap='gray')
    plt.axis('off')
    plt.savefig('./image/me_filter_high.jpg')
    result = (im1_lowpass + im2_highpass) / 2
    return result
Example #3
0
    def generate_visual_css(self, rawcss, closeness, return_all=False):
        """ generate_visual_css(rawcss, closeness)
        Generate a 1D signal that can be plotted to depict the CSS by taking
        column maximums. Further checks for close interest points and nicely
        smoothes them with weighted moving average
        """

        flat_signal = np.amax(rawcss, axis=0)

        # minor smoothing via moving averages
        window = closeness
        weights = gaussian_kernel(window, 0, window, False)  # gaussian weights
        sig = np.convolve(flat_signal, weights)[window - 1:-(window - 1)]

        maxs = []

        # get maximas
        w = sig.size

        for i in range(1, w - 1):
            if sig[i - 1] < sig[i] and sig[i] > sig[i + 1]:
                maxs.append([i, sig[i]])

        if return_all:
            return sig, maxs
        else:
            return sig
Example #4
0
 def get_kernel(self, data, a):
     K = self.K[a]
     kt = np.empty((data.shape[0], self.X.shape[0]))
     for i in range(data.shape[0]):
         for j in range(self.X.shape[0]):
             kt[i, j] = gaussian_kernel(data[i], self.X[j], K)
     return kt
Example #5
0
def main_boilerplate():
    # Load image and kernel
    img = sm.imresize(sm.face(), 4.0, interp='bicubic')
    x = utils.rgb2ycc(img.astype(np.float32) / 255.)[:, :, 0]
    k = utils.gaussian_kernel(2, 3.5)
    noise = np.random.normal(0., 0.01, x.shape).astype(np.float32)
    return img, x, k, noise
 def compute_cov(self, K):
     """
     :param K: float, parameter of the Gaussian kernel
     :return: np.array, covariance matrix for training
     """
     cov = np.eye(self.n)
     for i in range(self.n):
         for j in range(i):
             cov_ij = gaussian_kernel(self.X[i], self.X[j], K)
             cov[i, j], cov[j, i] = cov_ij, cov_ij
     return cov
 def predict_single_pref(self, r, s, f_map, M):
     """
     :param r: np.array, vector of dimension d
     :param s: np.array, vector of dimension d
     :param f_map: np.array, Maximum a posteriori for n vectors in R^d
     :param M: np.array, covariance matrix computed at the maximum a posteriori
     :return: float, probability that r is preferred over s
     """
     sigma_t = np.array([[1., gaussian_kernel(r, s, self.K)],
                         [gaussian_kernel(r, s, self.K), 1.]])
     kt = np.zeros((self.n, 2))
     kt[:,
        0] = [gaussian_kernel(r, self.X[i], self.K) for i in range(self.n)]
     kt[:,
        1] = [gaussian_kernel(s, self.X[i], self.K) for i in range(self.n)]
     mu_star = np.dot(kt.T, np.dot(self.inv_cov, f_map))
     s_star = sigma_t - np.dot(np.dot(kt.T, M), kt)
     new_sigma = np.sqrt(2 * self.sigma**2 + s_star[0, 0] + s_star[1, 1] -
                         2 * s_star[0, 1])
     return norm.cdf((mu_star[0] - mu_star[1]) / new_sigma, loc=0, scale=1)
 def predict(self, test_set, f_map, get_proba=False):
     """
     :param test_set: tuple of length 2, - an array for the test X from which preferences are drawn
                                         - a list of tuples (i,j) for the new preferences to predict, where i is
                                           preferred to j (helps for the score)
     :param f_map: np.array, Maximum a Posteriori obtained with the GP method
     :return: tuple, - float, score of the prediction
                     - np.array, array of probabilities P(i preferred over j)
     """
     X = test_set[0]
     pref = test_set[1]
     score = 0
     proba_pref = []
     self.compute_Hessian_S(f_map)
     if not get_proba:
         for p in pref:
             kt = np.zeros((self.n, 2))
             kt[:, 0] = [
                 gaussian_kernel(X[p[0]], self.X[i], self.K)
                 for i in range(self.n)
             ]
             kt[:, 1] = [
                 gaussian_kernel(X[p[1]], self.X[i], self.K)
                 for i in range(self.n)
             ]
             mu_star = np.dot(kt.T, np.dot(self.inv_cov, f_map))
             if (mu_star[0] - mu_star[1]) > 0:
                 score += 1
         return score / len(pref), None
     else:
         try:
             M = np.linalg.inv(self.cov + np.linalg.inv(self.nu))
             for p in pref:
                 proba = self.predict_single_pref(X[p[0]], X[p[1]], f_map,
                                                  M)
                 proba_pref.append(proba)
                 if proba > 0.5:
                     score += 1
             return score / len(pref), proba_pref
         except np.linalg.LinAlgError:
             return np.nan, np.nan
Example #9
0
 def predict_movie(self, model, instances, data, MAP):
     """
     :param model: Instance_Learning instance
     :param instances: Training dataset (features of movies already watched)
     :param data: Test dataset (feature of movies not watched)
     :param MAP: Maximum a posteriori estimate on watched movies
     :return: indice of the best instance in the test dataset
     """
     beta = np.dot(model.inv_cov, MAP)
     kt = np.array([[
         gaussian_kernel(data[i], instances[j], self.K)
         for j in range(instances.shape[0])
     ] for i in range(data.shape[0])])
     return int(np.argmax(np.dot(kt, beta)))
    def compute_off_diagonal_blocks(self):
        """
        Compute the off-diagonal blocks. We use the blocks of W calculated previously.
        :return:
        """

        dim_subsample = 3 * self.rank_k  # Get the size of the subsample matrix
        for s in range(self.number_clusters_c):
            for t in range(self.number_clusters_c):
                if s != t:

                    df_s = self.df.loc[self.df['clusters'] == s].iloc[:, :-1]
                    df_t = self.df.loc[self.df['clusters'] == t].iloc[:, :-1]

                    number_col_s = df_s.shape[0]
                    number_row_t = df_t.shape[0]

                    index_col_s = random.randint(0, number_col_s - dim_subsample)
                    index_row_t = random.randint(0, number_row_t - dim_subsample)

                    # We have the coordinates of the submatrix

                    sub_matrix = np.zeros((dim_subsample, dim_subsample))

                    for i in range(index_row_t, index_row_t + dim_subsample):
                        for j in range(index_col_s, index_col_s + dim_subsample):
                            sub_matrix[i - index_row_t, j - index_col_s] = gaussian_kernel(self.gamma, df_s.iloc[j],
                                                                                           df_t.iloc[i])

                    # Then we can extract W_nu_s and W_nu_t

                    W_s = self.blocks_of_W[s]
                    W_nu_s = W_s[index_col_s:index_col_s + dim_subsample, :]

                    W_t = self.blocks_of_W[t]
                    W_nu_t = W_t[index_row_t:index_row_t + dim_subsample, :]

                    # And finally we compute the dot product (i.e solve Least Squares) to get L(s,t)

                    L_s_t = inv(np.transpose(W_nu_s).dot(W_nu_s)).dot(np.transpose(W_nu_s)).dot(sub_matrix).dot(
                        W_nu_t).dot(
                        inv(np.transpose(W_nu_t).dot(W_nu_t)))

                    self.blocks_of_L[str(s) + '_' + str(t)] = L_s_t

                    # We have now all the blocks of L and W

        print("Computation of off-diagonal blocks done.")
Example #11
0
def load_train_data():
    """
    Fills train_images array with cutouts.
    """
    global train_images

    for k in range(0, train_length):
        if labels[k] == 1:
            train_im = np.load(ones_path + file_list1[k])
        elif labels[k] == 0:
            train_im = np.load(zeros_path + file_list1[k])
        else:
            print('There is something wrong')
            train_im = 0

        # filter and bin the cutout, scale it to [0, 1]
        train_im = ndimage.convolve(train_im, gaussian_kernel(5, 5))
        train_images[k] = pre_process_cutout(train_im, im_size, bin_size)
def PyrBlending(source, target, mask):
    """
    Blend source image onto target. Source is the foreground
    Source, target, and mask have the same height and width
    Source.shape == target.shape
    Number of layers for pyramid is fixed at 6
    Input:
    - source: 2D or 3D source image matrix
    - target: 2D or 3D target image matrix
    - mask: binary matrix with 1 indicating ROI
    
    Output:
    - blended image 
    """
    num_layers = 6
    sH, sW = source.shape[:2]
    tH, tW = target.shape[:2]
    mH, mW = mask.shape[:2]
    kernel = utils.gaussian_kernel((9, 9), 2)
    res_pyr = []

    s_gPyr, s_lPyr = ComputePyr(source, num_layers)
    t_gPyr, t_lPyr = ComputePyr(target, num_layers)
    m_gPyr, _ = ComputePyr(mask, num_layers)

    for i in range(num_layers):
        s = s_lPyr[i]
        t = t_lPyr[i]
        m = m_gPyr[i]
        combined = m * s + (1 - m) * t
        res_pyr.append(combined)

    res = res_pyr.pop()
    while len(res_pyr) > 0:
        layer = res_pyr.pop()
        h, w = layer.shape[:2]
        res = utils.resample(res, (h, w))
        res = conv.conv2(res, kernel, 'reflect')
        res += layer

    return res
Example #13
0
def template_matching(data_dir,
                      template_dir,
                      pyramid_depth=4,
                      rotations=None,
                      gaussian=None):
    if rotations is None:
        rotations = [x for x in range(0, 360, 30)]

    if gaussian is None:
        gaussian = utils.gaussian_kernel(5, 5, 15)

    image_names = utils.get_files(data_dir, extension='.png')
    for image_name in image_names:
        image = cv2.imread(data_dir + image_name + '.png',
                           cv2.IMREAD_UNCHANGED)
        image_filtered = tm.pre_process_image(image)

        # Create a Gaussian pyramid of given depth for each image
        pyramid = tm.create_gaussian_pyramid(image_filtered, gaussian,
                                             pyramid_depth)

        # Create directory for class
        image_class = tm.get_class_name(image_name)
        class_dir = template_dir + image_class + '/'
        utils.create_directory(class_dir)
        # Path(class_dir).mkdir(parents=True, exist_ok=True)

        # Rotate each scaled image
        for scale_index, scaled_image in enumerate(pyramid):
            for angle in rotations:
                rotated_image = tm.rotate_image(scaled_image, angle)

                # Save image to png file
                file_name = image_class + "-level" + str(
                    scale_index) + "-rotation" + str(angle) + ".png"
                cv2.imwrite(class_dir + file_name, rotated_image)

    return True
Example #14
0
def optimise_parameters():
    pyramid_levels = [3, 4, 5]
    # pyramid_levels = [x for x in range(3,6)]
    rotations = [[x for x in range(0, 360, rot)] for rot in range(20, 35, 5)]
    gaussian_parameters = [[5, 5, 15]]

    # Create results directory
    utils.create_directory(config.RESULTS_DIR)

    for level in pyramid_levels:
        for rots in rotations:
            for g_n, gaussian in enumerate(gaussian_parameters):
                step_size = rots[1] - rots[0]
                row, col, dev = gaussian
                g = utils.gaussian_kernel(row, col, dev)
                utils.delete_directory(config.TEMPLATE_OUTPUT_DIR)
                print(
                    'training rotation {} level {} gaussian {}-{}-{}'.format(
                        step_size, level, row, col, dev), rots, level)

                start = time.time()

                template_matching(config.TRAINING_DIR,
                                  config.TEMPLATE_OUTPUT_DIR, level, rots, g)
                new_dir = config.RESULTS_DIR + 'level{}-rot{}-g-{}-{}-{}/'.format(
                    level, step_size, row, col, dev)
                utils.create_directory(new_dir)
                print('testing', rots, level)
                images = test_template_matching(config.TESTING_DIR,
                                                config.TEMPLATE_OUTPUT_DIR)
                end = time.time()
                time_elapsed = end - start
                utils.write_to_file(new_dir + 'time.txt', time_elapsed)

                for idx, im in enumerate(images):
                    cv2.imwrite(new_dir + '{}.png'.format(idx), im)

    return True
def ComputePyr(img, num_layers):
    """
    Given image and number of pyramid layers,
    computes Gaussian and Laplacian Pyramid of the image
    Input:
    - img: 2D or 3D image matrix
    - num_layers: int indicating the number of layers of the pyramids

    Output:
    - (Gaussian Pyramid, Laplacian Pyramid)
    """
    # only accept 2D and 3D images
    if np.ndim(img) == 2:
        H, W = img.shape
    elif np.ndim(img) == 3:
        H, W, _ = img.shape
    else:
        raise ValueError("input image has invalid dimension %s" % (img.shape))

    kernel = utils.gaussian_kernel((9, 9), 2)
    gPyr = gaussian_pyr(img, (H, W), num_layers, kernel)
    lPyr = laplacian_pyr(gPyr, kernel)
    return (gPyr, lPyr)
Example #16
0
def build_graphs(batch, size, blur_radius, blur_scale, center_crop, scale,
                 flip, rotation, crops, brightness, contrast, gaussian_noise,
                 uniform_noise):
    """
    Tensorflow graph to load and transform images.

    batch: number of images to process in batch
    size: target width/height
    blur_radius: mask/weight blur radius
    blur_scale: gaussian blur scale factor for mask/weight images
    center_crop: source images center crop percentage
    scale: scale factor
    flip: flip mode
    rotation: rotation in degree
    crops: number of variation to create
    brightness: random brightness delta
    contrast: random contrast delta
    gaussian_noise: gaussian noise standard deviation
    uniform_noise: uniform noise magnitude

    return tensorflow graph pipeline
    """

    items = list()
    for i in range(batch):
        with tf.name_scope("batch%d" % i):
            # inputs
            name = tf.placeholder(tf.string, name="name")
            width = tf.placeholder(tf.int32, name="width")
            height = tf.placeholder(tf.int32, name="height")
            orientation = tf.placeholder(tf.int32, name="orientation")
            rgbpath = tf.placeholder(tf.string, name="rgbpath")
            maskpath = tf.placeholder(tf.string, name="maskpath")
            weightpath = tf.placeholder(tf.string, name="weightpath")

            # read images
            rgb = tf.image.decode_image(tf.read_file(rgbpath),
                                        channels=3,
                                        name="decode_rgb")
            mask = tf.image.decode_image(tf.read_file(maskpath),
                                         channels=1,
                                         name="decode_mask")
            weight = tf.image.decode_image(tf.read_file(weightpath),
                                           channels=1,
                                           name="decode_weight")
            rgb = tf.image.convert_image_dtype(rgb,
                                               tf.float32,
                                               name="convert_rgb")
            mask = tf.image.convert_image_dtype(mask,
                                                tf.float32,
                                                name="convert_mask")
            weight = tf.image.convert_image_dtype(weight,
                                                  tf.float32,
                                                  name="convert_weight")
            rgb = tf.reshape(rgb, [height, width, 3], name="reshape_rgb")
            mask = tf.reshape(mask, [height, width, 1], name="reshape_mask")
            weight = tf.reshape(weight, [height, width, 1],
                                name="reshape_weight")

            # blur mask/weight
            if blur_radius > 0:
                with tf.name_scope("blur"):
                    kernel = tf.constant(gaussian_kernel(blur_radius),
                                         name="kernel")
                    blur_in = tf.stack([mask, weight], name="stack")
                    if blur_scale < 1.0:
                        height2 = tf.to_double(height) * blur_scale
                        width2 = tf.to_double(width) * blur_scale
                        blur_in = tf.image.resize_bilinear(
                            blur_in,
                            tf.to_int32([height2, width2]),
                            name="downscale")
                    blur_out = tf.nn.conv2d(input=blur_in,
                                            filter=kernel,
                                            strides=[1, 1, 1, 1],
                                            padding="SAME",
                                            name="conv2d")
                    if blur_scale < 1.0:
                        blur_out = tf.image.resize_bilinear(blur_out,
                                                            [height, width],
                                                            name="upscale")
                    mask, weight = tf.unstack(blur_out, name="unstack")

            # normalize mask/weight
            mask /= tf.maximum(1.0, tf.reduce_max(mask))
            weight /= tf.maximum(1.0, tf.reduce_max(weight))

            # preprocess image
            image = tf.concat([rgb, mask, weight], axis=2, name="concat")
            image = tf.image.central_crop(image, center_crop)
            with tf.name_scope("flip"):
                image = build_flip_graph(image, flip)
            with tf.name_scope("rotatation"):
                image = build_rotate_graph(image, rotation)
            with tf.name_scope("scale"):
                image = build_scale_graph(image, scale, size)

            # generate variations
            with tf.name_scope("variations"):
                variations = build_variation_graphs(image, size, crops,
                                                    brightness, contrast,
                                                    gaussian_noise,
                                                    uniform_noise)
            items.append({
                "name":
                name,
                "width":
                width,
                "height":
                height,
                "orientation":
                orientation,
                "transform":
                tf.constant("%.02f_%s_%.01f" % (scale, flip, rotation),
                            tf.string),
                "rgbpath":
                rgbpath,
                "maskpath":
                maskpath,
                "weightpath":
                weightpath,
                "variations":
                variations,
            })
    return items
Example #17
0
utils.plot_decision_boundary(scaleX,y,svm,'x1','x2',['neg','pos'])
plt.savefig('fig2.pdf')

############################################################################
#  Part  3: Training SVM with a kernel                                     #
#  We train an SVM with an RBF kernel on the data set and the plot the     #
#  learned decision boundary                                               #
############################################################################

# test your Gaussian kernel implementation

x1 = np.array([1,2,1])
x2 = np.array([0,4,-1])
sigma = 2

print "Guassian kernel value (should be around 0.324652) = ", utils.gaussian_kernel(x1,x2,sigma)

# load ex4data2.mat

X,y = utils.load_mat('data/ex4data2.mat')

# visualize the data

utils.plot_twoclass_data(X,y,'', '',['neg','pos'])
plt.savefig('fig3.pdf')

# convert X to kernel form with the kernel function

sigma = 0.02

# compute the kernel (slow!)
def run_attack(args):
    input_folder = os.path.join(args.input_path, 'images/')
    adv_img_save_folder = os.path.join(args.result_path, 'adv_images/')
    if not os.path.exists(adv_img_save_folder):
        os.makedirs(adv_img_save_folder)

    # Dataset, dev50.csv is the label file
    data_set = MyDataset(csv_path=os.path.join(args.input_path,
                                               args.label_file),
                         path=input_folder)
    data_loader = DataLoader(dataset=data_set,
                             batch_size=args.batch_size,
                             shuffle=False,
                             num_workers=2)

    device = torch.device("cuda:0")
    source_models = load_models(args.source_models,
                                device)  # load model, maybe several models

    seed_num = 0  # set seed
    random.seed(seed_num)
    np.random.seed(seed_num)
    torch.manual_seed(seed_num)
    torch.backends.cudnn.deterministic = True

    # gaussian_kernel: filter high frequency information of images
    gaussian_smoothing = gaussian_kernel(device,
                                         kernel_size=5,
                                         sigma=1,
                                         channels=3)

    print('Start attack......')
    for i, data in enumerate(data_loader, 0):
        start_t = time.time()
        X, labels, filenames = data
        X = X.to(device)
        labels = labels.to(device)

        # the noise
        delta = torch.zeros_like(X, requires_grad=True).to(device)
        X = gaussian_smoothing(
            X)  # filter high frequency information of images

        for t in range(args.iterations):
            g_temp = []
            for tt in range(len(liner_interval)):
                if args.input_diversity:  # use Input Diversity
                    X_adv = X + delta
                    X_adv = input_diversity(X_adv)
                    # images interpolated to 224*224, adaptive standard networks and reduce computation time
                    X_adv = F.interpolate(X_adv, (224, 224),
                                          mode='bilinear',
                                          align_corners=False)
                else:
                    c = liner_interval[tt]
                    X_adv = X + c * delta
                    X_adv = F.interpolate(X_adv, (224, 224),
                                          mode='bilinear',
                                          align_corners=False)
                # get ensemble logits
                ensemble_logits = get_logits(X_adv, source_models)
                loss = -nn.CrossEntropyLoss()(ensemble_logits, labels)
                loss.backward()

                grad = delta.grad.clone()
                # TI: smooth the gradient
                grad = F.conv2d(grad,
                                TI_kernel(),
                                bias=None,
                                stride=1,
                                padding=(2, 2),
                                groups=3)
                g_temp.append(grad)

            # calculate the mean and cancel out the noise, retained the effective noise
            g = 0.0
            for j in range(len(liner_interval)):
                g += g_temp[j]
            g = g / float(len(liner_interval))
            delta.grad.zero_()

            delta.data = delta.data - args.alpha * torch.sign(g)
            delta.data = delta.data.clamp(-args.epsilon / 255.,
                                          args.epsilon / 255.)
            delta.data = ((X + delta.data).clamp(0.0, 1.0)) - X

        save_imgs(X + delta, adv_img_save_folder, filenames)  # save adv images
        end_t = time.time()
        print('Attack batch: {}/{};   Time spent(seconds): {:.2f}'.format(
            i, len(data_loader), end_t - start_t))
Example #19
0
svm = LinearSVM_twoclass()
svm.theta = np.zeros((X.shape[1],))

'''
    first select the best gaussian kernelized svm
'''

Cvals = [0.01,0.03,0.1,0.3,10,30]
sigma_vals = [0.01,0.03,0.1,0.3,10,30]
learning_rates = [1e-4, 1e-3, 1e-2, 1e-1, 1]
iterations = [100 ,1000, 10000]

best_acc = 0
for sigma_val in sigma_vals:
    K = np.array([utils.gaussian_kernel(x1,x2,sigma_val) for x1 in X_train for x2 in X_train]).reshape(X_train.shape[0],X_train.shape[0])
    scaler = preprocessing.StandardScaler().fit(K)
    scaleK = scaler.transform(K)
    KK = np.hstack([np.ones((scaleK.shape[0],1)),scaleK])
    Kval = np.array([utils.gaussian_kernel(x1,x2,sigma_val) for x1 in X_val for x2 in X_train]).reshape(X_val.shape[0],X_train.shape[0])
    scaleKval = scaler.transform(Kval)
    KKval = np.hstack([np.ones((scaleKval.shape[0],1)),scaleKval])
    for Cval in Cvals:
        for learning_rate in learning_rates:
            for iteration in iterations:
                svm = LinearSVM_twoclass()
                svm.theta = np.zeros((KK.shape[1],))
                svm.train(KK,y_train,learning_rate=learning_rate,C=Cval,num_iters=iteration,verbose=True)
                y_pred = svm.predict(KKval)
                acc = np.mean(y_pred == y_val)
                if acc > best_acc:
Example #20
0
 def test_gaussian_distance(self):
     sigma = 2
     x1 = np.array([1,2,1])
     x2 = np.array([0,4,-1])
     dist = gaussian_kernel(x1,x2,sigma)
     self.assertAlmostEqual(dist, 0.324652, places=6)
Example #21
0
def closest_mode_filter(I, sigma, freq=1., neighborhood_size=9, display_histograms=False):
    """Apply closest mode filtering on image I

    Parameters
    ----------
    I: numpy.ndarray
        Image to filter (Height x Width x Channels)

    sigma: float
        Standard deviation of the gaussian kernel

    freq: float
        Sampling rate over the 256 values for intensity (RGB)

    neighborhood_size: int or iterable
        If int, the neighborhood is a square,
        otherwise it is a rectangle

    display_histograms: bool, default=False
        Display the histogram and histogram derivative of the image

    Returns
    -------
    I_closest: numpy.ndarray
        Filtered image (Height x Width x Channels)
    """
    message = "Closest mode filtering"
    print(message)
    print("-" * len(message))
    if I.shape[2] > 1:  # multi channel image
        if check_if_rgb(I):
            I = rgb_to_hsv(I/255)  # convert to HSV
        else:
            raise ValueError("Input image is not RGB")
    I_closest = np.zeros(I.shape)
    if I_closest.shape[2] > 1:
        I_closest[:, :, 0] = I[:, :, 0]
        I_closest[:, :, 1] = I[:, :, 1]
        canal_to_filter = I[:, :, 2]
    else:
        canal_to_filter = I[:, :, 0]

    canal_to_filter = canal_to_filter*255
    canal_to_filter = canal_to_filter.astype(int)
    
    print("Computing lookup tables...")
    # Compute lookup table
    samples = np.arange(int(freq * 256))
    lookup_K = {i: K(i - samples, sigma) for i in range(256)}
    lookup_K_prime = {i: -K_prime(i - samples, sigma) for i in range(256)}

    # Build the spatial kernel
    W = gaussian_kernel(neighborhood_size)
    print("Mapping the image...")

    histograms = np.zeros((canal_to_filter.shape[0],
                           canal_to_filter.shape[1],
                           256))
    for x in range(canal_to_filter.shape[0]):
        for y in range(canal_to_filter.shape[1]):
            histograms[x, y] = lookup_K[canal_to_filter[x, y]]

    histogram_derivatives = np.zeros((canal_to_filter.shape[0],
                           canal_to_filter.shape[1],
                           256))
    for x in range(canal_to_filter.shape[0]):
        for y in range(canal_to_filter.shape[1]):
            histogram_derivatives[x, y] = lookup_K_prime[canal_to_filter[x, y]]

    for i in range(256):
        histograms[:, :, i] = fftconvolve(histograms[:, :, i], W, mode="same")
        histogram_derivatives[:, :, i] = fftconvolve(histogram_derivatives[:, :, i], W, mode="same")

    total_histogram = np.sum(histograms, axis=(0, 1))
    total_histogram_derivative = np.sum(histogram_derivatives, axis=(0, 1))

    if display_histograms:
        f = plt.figure()
        plt.subplot(311)
        plt.plot(make_hist(canal_to_filter).keys(), make_hist(canal_to_filter).values())
        plt.title("histogram")
        plt.subplot(312)
        plt.plot(total_histogram)
        plt.title("smoothed histogram")
        plt.subplot(313)
        plt.plot(total_histogram_derivative)
        plt.plot([0]*256)
        plt.title("histogram derivative")
        plt.show()

    print("Finding closest mode...")
    filtered_canal = np.zeros(canal_to_filter.shape)
    for x in range(filtered_canal.shape[0]):
        for y in range(filtered_canal.shape[1]):
            closest_intensity = find_closest_mode(canal_to_filter[x, y],
                                                  total_histogram_derivative)
            filtered_canal[x, y] = closest_intensity

    if I_closest.shape[2] > 1:
        I_closest[:, :, 2] = filtered_canal / 255
    else:
        I_closest[:, :, 0]  = filtered_canal / 255

    I_closest = hsv_to_rgb(I_closest) * 255
    I_closest = I_closest.astype(np.uint8)

    return I_closest
Example #22
0
svm = LinearSVM_twoclass()
svm.theta = np.zeros((X.shape[1], ))
'''
    first select the best gaussian kernelized svm
'''

Cvals = [0.01, 0.03, 0.1, 0.3, 10, 30]
sigma_vals = [0.01, 0.03, 0.1, 0.3, 10, 30]
learning_rates = [1e-4, 1e-3, 1e-2, 1e-1, 1]
iterations = [100, 1000, 10000]

best_acc = 0
for sigma_val in sigma_vals:
    K = np.array([
        utils.gaussian_kernel(x1, x2, sigma_val) for x1 in X_train
        for x2 in X_train
    ]).reshape(X_train.shape[0], X_train.shape[0])
    scaler = preprocessing.StandardScaler().fit(K)
    scaleK = scaler.transform(K)
    KK = np.hstack([np.ones((scaleK.shape[0], 1)), scaleK])
    Kval = np.array([
        utils.gaussian_kernel(x1, x2, sigma_val) for x1 in X_val
        for x2 in X_train
    ]).reshape(X_val.shape[0], X_train.shape[0])
    scaleKval = scaler.transform(Kval)
    KKval = np.hstack([np.ones((scaleKval.shape[0], 1)), scaleKval])
    for Cval in Cvals:
        for learning_rate in learning_rates:
            for iteration in iterations:
                svm = LinearSVM_twoclass()
Example #23
0
utils.plot_decision_boundary(scaleX, y, svm, 'x1', 'x2', ['neg', 'pos'])
plt.savefig('fig2.pdf')

############################################################################
#  Part  3: Training SVM with a kernel                                     #
#  We train an SVM with an RBF kernel on the data set and the plot the     #
#  learned decision boundary                                               #
############################################################################

# test your Gaussian kernel implementation

x1 = np.array([1, 2, 1])
x2 = np.array([0, 4, -1])
sigma = 2

print "Guassian kernel value (should be around 0.324652) = ", utils.gaussian_kernel(
    x1, x2, sigma)

# load ex4data2.mat

X, y = utils.load_mat('data/ex4data2.mat')

# visualize the data

utils.plot_twoclass_data(X, y, '', '', ['neg', 'pos'])
plt.savefig('fig3.pdf')

# convert X to kernel form with the kernel function

sigma = 0.02

# compute the kernel (slow!)
Example #24
0
svm = LinearSVM_twoclass()
svm.theta = np.zeros((X.shape[1],))


Cvals = [0.01,0.03,0.1,0.3,1,3,10,30]
sigma_vals = [0.01,0.03,0.1,0.3,1,3,10,30]

best_C = 0.01
best_sigma = 0.01

best_acc = 0;
for sigma in sigma_vals:
  print "Calculating K"
  sys.stdout.flush()
  K = np.array([utils.gaussian_kernel(x1,x2,sigma) for x1 in X for x2 in X]).reshape(X.shape[0],X.shape[0])
  scaler = preprocessing.StandardScaler().fit(K)
  scaleK = scaler.transform(K)
  KK = np.vstack([np.ones((scaleK.shape[0],)),scaleK]).T
  
  Kval = np.array([utils.gaussian_kernel(x1,x2,sigma) for x1 in Xval for x2 in X]).reshape(Xval.shape[0], X.shape[0])
  scaleKval = scaler.transform(Kval)
  KKval = np.vstack([np.ones((scaleKval.shape[0],)),scaleKval.T]).T
  print "Done!"
  for C in Cvals:
    print "sigma=", sigma, ", C=", C
    sys.stdout.flush()
    svm.theta = np.zeros((KK.shape[1],))
    svm.train(KK,yy,learning_rate=1e-4,C=C,num_iters=2000)
    
    y_pred = svm.predict(KKval)