0% found this document useful (0 votes)
76 views63 pages

K Means

This document summarizes a lecture on k-means and mean-shift clustering algorithms. It begins with recaps of image segmentation, Gestalt theory of perceptual grouping, and agglomerative clustering. It then previews that k-means and mean-shift clustering will be covered. K-means aims to minimize distortion by iteratively assigning points to the closest cluster center and updating centers as cluster means. It is sensitive to initialization and choosing the number of clusters k. Mean-shift clustering seeks to find dense regions in the data.

Uploaded by

abhas kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views63 pages

K Means

This document summarizes a lecture on k-means and mean-shift clustering algorithms. It begins with recaps of image segmentation, Gestalt theory of perceptual grouping, and agglomerative clustering. It then previews that k-means and mean-shift clustering will be covered. K-means aims to minimize distortion by iteratively assigning points to the closest cluster center and updating centers as cluster means. It is sensitive to initialization and choosing the number of clusters k. Mean-shift clustering seeks to find dense regions in the data.

Uploaded by

abhas kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

Lecture 13: k-means and

mean-shift clustering
Juan Carlos Niebles
Stanford AI Lab

Professor Fei-Fei Li
Stanford Vision Lab

Fei-Fei Li Lecture 13 - 1 8-Nov-2016


Recap: Image Segmentation
• Goal: identify groups of pixels that go together

Fei-Fei Li Lecture 13 - 2 8-Nov-2016


Recap: Gestalt Theory
• Gestalt: whole or group (German: "shape, form")
– Whole is other than sum of its parts
– Relationships among parts can yield new properties/features

• Psychologists identified series of factors that predispose set of


elements to be grouped (by human visual system)
“I stand at the window and see a house, trees, sky.
Theoretically I might say there were 327 brightnesses
and nuances of colour. Do I have "327"? No. I have sky, house,
and trees.”
Max Wertheimer
(1880-1943)

Untersuchungen zur Lehre von der Gestalt,


Psychologische Forschung, Vol. 4, pp. 301-350, 1923

Fei-Fei Li Lecture 13 - 3 8-Nov-2016


Recap: Gestalt Factors

● These factors make intuitive sense, but are very difficult to translate into algorithms.

Fei-Fei Li Lecture 13 - 4 8-Nov-2016


Recap: Multistability

https://en.wikipedia.org/wiki/Spinning_Dancer

Fei-Fei Li Lecture 13 - 5 8-Nov-2016


Recap: Agglomerative clustering

Simple algorithm

● Initialization:
○ Every point is its own cluster
● Repeat:
○ Find “most similar” pair of clusters
○ Merge into a parent cluster
● Until:
○ The desired number of clusters has been reached
○ There is only one cluster

Fei-Fei Li Lecture 13 - 6 8-Nov-2016


What will we learn today?
• K-means clustering
• Mean-shift clustering

Reading material:
Forsyth & Ponce: Chapter 9.3
Comaniciu and Meer, Mean Shift: A Robust Approach toward Feature Space Analysis, PAMI 2002.
gifs: https://www.projectrhea.org

Fei-Fei Li Lecture 13 - 7 8-Nov-2016


Image Segmentation: Toy Examplewhite
pixels
3 black pixels
gray

1 2 pixels

input image
• These intensities define the three groups.
intensity
• We could label every pixel in the image according to which
of these primary intensities it is.
– i.e., segment the image based on the intensity feature.
• What if the image isn’t quite so simple?

Slide credit: Kristen Grauman

Fei-Fei Li Lecture 13 - 8 8-Nov-2016


Pixel count
Input image
Intensity

Pixel count

Input image
Intensity
Slide credit: Kristen Grauman
Fei-Fei Li Lecture 13 - 9 8-Nov-2016
Pixel count
Input image
Intensity

• Now how to determine the three main


intensities that define our groups?
• We need to cluster.

Slide credit: Kristen Grauman

Fei-Fei Li Lecture 13 - 10 8-Nov-2016


0 190 255
Intensity

3
1 2

• Goal: choose three “centers” as the representative intensities,


and label every pixel according to which of these centers it is
nearest to.

Slide credit: Kristen Grauman


• Best cluster centers are those that minimize Sum of Square
Distance (SSD) between all points and their nearest cluster
center ci:

Fei-Fei Li Lecture 13 - 11 8-Nov-2016


Objective function
● Goal: minimize the distortion in data given
clusters
- Preserve information
Cluster center Data

Slide: Derek Hoiem


Fei-Fei Li Lecture 13 - 12 8-Nov-2016
Clustering
• With this objective, it is a “chicken and egg”
problem:
– If we knew the cluster centers, we could allocate points to
groups by assigning each to its closest center.

Slide credit: Kristen Grauman


– If we knew the group memberships, we could get the
centers by computing the mean per group.

Fei-Fei Li Lecture 13 - 13 8-Nov-2016


K-means Clustering
● Initialization:
○ choose k cluster centers
● Repeat:
○ assignment step:
■ For every point find its closest center
○ update step:
■ Update every center as the mean of its points
● Until:
○ The maximum number of iterations is reached, or
○ No changes during the assignment step, or
○ The average distortion per point drops very little

[Lloyd, 1957]

Fei-Fei Li Lecture 13 - 14 8-Nov-2016


K-means Clustering te c h n ica l
note

slide credit: P. Rai

Fei-Fei Li Lecture 13 - 15 8-Nov-2016


K-means Clustering te c h n ica l
note

[1]

[1] L. Bottou and Y. Bengio. Convergence properties of the kmeans algorithm. NIPS, 1995. slide credit: P. Rai

Fei-Fei Li Lecture 13 - 16 8-Nov-2016


K-means: Initialization
● k-means is extremely sensitive to initialization
● Bad initialization can lead to:
○ poor convergence speed
○ bad overall clustering
● How to initialize?
○ randomly from data
○ try to find K “spread-out” points (k-means++)
● Safeguarding measure:
○ try multiple initializations and choose the best

Fei-Fei Li Lecture 13 - 17 8-Nov-2016


K-means: Initialization
● k-means is extremely sensitive to initialization
● Bad initialization can lead to:
○ poor convergence speed
○ bad overall clustering

Fei-Fei Li Lecture 13 - 18 8-Nov-2016


K-means++
• Can we prevent arbitrarily bad local minima?

1. Randomly choose first center.


2. Pick new center with prob. proportional to (x - ci)2
– (Contribution of x to total error)
3. Repeat until K centers.

• Expected error O(logK) (optimal)

K-means++ animation Arthur & Vassilvitskii 2007

Fei-Fei Li Lecture 13 - 19 8-Nov-2016


K-means: choosing K

slide credit: P. Rai

Fei-Fei Li Lecture 13 - 20 8-Nov-2016


K-means: choosing K

• Validation set
– Try different numbers of clusters and look at
performance
• When building dictionaries (discussed later), more
clusters typically work better

Slide: Derek Hoiem

Fei-Fei Li Lecture 13 - 21 8-Nov-2016


Distance Measure & Termination
● Choice of “distance” measure:
■ Euclidean (most commonly used)
■ Cosine
■ non-linear! (Kernel k-means)

Picture courtesy: Christof Monz (Queen Mary, Univ. of London)

● Termination:
○ The maximum number of iterations is reached
○ No changes during the assignment step (convergence)
○ The average distortion per point drops very little

Fei-Fei Li Lecture 13 - 22 8-Nov-2016


K-means: Example

K-Means Clustering Example

Fei-Fei Li Lecture 13 - 23 8-Nov-2016


How to evaluate clusters?
• Generative
– How well are points reconstructed from the clusters?
→ “Distortion”

• Discriminative
– How well do the clusters correspond to labels?
• Purity
– Note: unsupervised clustering does not aim to be
discriminative

Slide: Derek Hoiem


Fei-Fei Li Lecture 13 - 24 8-Nov-2016
Segmentation as Clustering
● Let’s just use the pixel intensities!

k=2

k=3

Slide credit: Kristen Grauman

Fei-Fei Li Lecture 13 - 25 8-Nov-2016


Feature Space
• Depending on what we choose as the feature space,
we can group pixels in different ways.

• Grouping pixels based on


intensity similarity

• Feature space: intensity value (1D)


Slide credit: Kristen Grauman

Fei-Fei Li Lecture 13 - 26 8-Nov-2016


Feature Space
• Depending on what we choose as the feature space, we can
group pixels in different ways.
R=255
• Grouping pixels based G=200
on color similarity B=250

R=245
B G=220
G B=248

R=15 R=3
R G=189 G=12
B=2 B=2

● Feature space: color value (3D)


Slide credit: Kristen Grauman

Fei-Fei Li Lecture 13 - 27 8-Nov-2016


Feature Space
• Depending on what we choose as the feature space, we can
group pixels in different ways.
• Grouping pixels based
on texture similarity
F1
F2
Filter bank of
24 filters

F24

● Feature space: filter bank responses (e.g., 24D)


Slide credit: Kristen Grauman

Fei-Fei Li Lecture 13 - 28 8-Nov-2016


K-Means Clustering Results
• K-means clustering based on intensity or color is
essentially vector quantization of the image attributes
– Clusters don’t have to be spatially coherent
Image Intensity-based clusters Color-based clusters

Image source: Forsyth & Ponce


Fei-Fei Li Lecture 13 - 29 8-Nov-2016
Smoothing Out Cluster Assignments
• Assigning a cluster label per pixel may yield outliers:

Original Labeled by cluster center’s intensity

?
• How can we ensure they
are spatially smooth? 3
1 2

Slide credit: Kristen Grauman

Fei-Fei Li Lecture 13 - 30 8-Nov-2016


Segmentation as Clustering
• Depending on what we choose as the feature space,
we can group pixels in different ways.
• Grouping pixels based on
intensity+position similarity
Intensity

X
⇒ Way to encode both similarity and proximity.
Slide credit: Kristen Grauman

Fei-Fei Li Lecture 13 - 31 8-Nov-2016


K-means clustering for superpixels
SLIC Superpixels:
● Feature space → intensity + position
○ L.a.b. color space
○ limited region (window 2*S)

● Distance metric:

● Initialization:
○ Spatial grid (grid step = S)
● Iterate over centers and not points
Achanta et al., SLIC Superpixels Compared to State-of-the-art Superpixel Methods, PAMI 2012.

Fei-Fei Li Lecture 13 - 32 8-Nov-2016


K-means clustering for superpixels

Achanta et al., SLIC Superpixels Compared to State-of-the-art Superpixel Methods, PAMI 2012.
Fei-Fei Li Lecture 13 - 33 8-Nov-2016
K-means clustering for superpixels

Achanta et al., SLIC Superpixels Compared to State-of-the-art Superpixel Methods, PAMI 2012.
Fei-Fei Li Lecture 13 - 34 8-Nov-2016
K-means clustering for superpixels

Achanta et al., SLIC Superpixels Compared to State-of-the-art Superpixel Methods, PAMI 2012.
Fei-Fei Li Lecture 13 - 35 8-Nov-2016
K-means Clustering: Limitations

[1]

[1] Dhillon et al. Kernel k-means, Spectral Clustering and Normalized Cuts. KDD, 2004. slide credit: P. Rai

Fei-Fei Li Lecture 13 - 36 8-Nov-2016


K-Means pros and cons
• Pros
• Finds cluster centers that minimize
conditional variance (good
representation of data)
• Simple and fast, Easy to implement
• Cons
• Need to choose K
• Sensitive to outliers
• Prone to local minima
• All clusters have the same
parameters (e.g., distance measure
is non-adaptive)
• *Can be slow: each iteration is
O(KNd) for N d-dimensional points
• Usage
• Unsupervised clustering
• Rarely used for pixel segmentation

Fei-Fei Li Lecture 13 - 37 8-Nov-2016


Scaling-up K-means clustering
● Assignment step is the bottleneck

● Approximate assignments
○ [AK-means, CVPR 2007], [AGM, ECCV 2012]
● Mini-batch version
○ [mbK-means, WWW 2010]
● Search from every center
○ [Ranked retrieval, WSDM 2014]
● Binarize data and centroids
○ [BK-means, CVPR 2015]
● Quantize data
○ [DRVQ, ICCV 2013], [IQ-means, ICCV 2015]

Fei-Fei Li Lecture 13 - 38 8-Nov-2016


What will we learn today?
• K-means clustering
• Mean-shift clustering

Fei-Fei Li Lecture 13 - 39 8-Nov-2016


Mean-Shift Segmentation
• An advanced and versatile technique for
clustering-based segmentation

Slide credit: Svetlana Lazebnik


D. Comaniciu and P. Meer, Mean Shift: A Robust Approach toward Feature Space Analysis, PAMI 2002.

Fei-Fei Li Lecture 13 - 40 8-Nov-2016


Mean-Shift Algorithm

Slide credit: Steve Seitz


• Iterative Mode Search
1. Initialize random seed, and window W
2. Calculate center of gravity (the “mean”) of W =
3. Shift the search window to the mean
4. Repeat Step 2 until convergence

[Fukunaga & Hostetler, 1975]


Fei-Fei Li Lecture 13 - 41 8-Nov-2016
Mean-Shift Region of
interest

Center of
mass

Mean Shift
vector
Slide by Y. Ukrainitz & B. Sarel

Fei-Fei Li Lecture 13 - 42 8-Nov-2016


Mean-Shift Region of
interest

Center of
mass

Mean Shift
vector
Slide by Y. Ukrainitz & B. Sarel

Fei-Fei Li Lecture 13 - 43 8-Nov-2016


Mean-Shift Region of
interest

Center of
mass

Mean Shift
vector
Slide by Y. Ukrainitz & B. Sarel

Fei-Fei Li Lecture 13 - 44 8-Nov-2016


Mean-Shift Region of
interest

Center of
mass

Mean Shift
vector
Slide by Y. Ukrainitz & B. Sarel

Fei-Fei Li Lecture 13 - 45 8-Nov-2016


Mean-Shift Region of
interest

Center of
mass

Mean Shift
vector
Slide by Y. Ukrainitz & B. Sarel

Fei-Fei Li Lecture 13 - 46 8-Nov-2016


Mean-Shift Region of
interest

Center of
mass

Mean Shift
vector
Slide by Y. Ukrainitz & B. Sarel

Fei-Fei Li Lecture 13 - 47 8-Nov-2016


Mean-Shift Region of
interest

Center of
mass

Slide by Y. Ukrainitz & B. Sarel

Fei-Fei Li Lecture 13 - 48 8-Nov-2016


Real Modality Analysis

Slide by Y. Ukrainitz & B. Sarel


Tessellate the space with windows Run the procedure in parallel
Fei-Fei Li Lecture 13 - 49 8-Nov-2016
Real Modality Analysis

Slide by Y. Ukrainitz & B. Sarel


The blue data points were traversed by the windows towards the mode.
Fei-Fei Li Lecture 13 - 50 8-Nov-2016
Mean-Shift Clustering
• Cluster: all data points in the attraction basin of a
mode
• Attraction basin: the region for which all trajectories
lead to the same mode

Slide by Y. Ukrainitz & B. Sarel

Fei-Fei Li Lecture 13 - 51 8-Nov-2016


Mean-Shift Clustering/Segmentation
• Find features (color, gradients, texture, etc)
• Initialize windows at individual pixel locations
• Perform mean shift for each window until convergence
• Merge windows that end up near the same “peak” or mode

Slide credit: Svetlana Lazebnik


Fei-Fei Li Lecture 13 - 52 8-Nov-2016
Mean-Shift Segmentation Results

Slide credit: Svetlana Lazebnik


Fei-Fei Li Lecture 13 - 53 8-Nov-2016
More Results

Slide credit: Svetlana Lazebnik


Fei-Fei Li Lecture 13 - 54 8-Nov-2016
More Results

Fei-Fei Li Lecture 13 - 55 8-Nov-2016


Problem: Computational Complexity

Slide credit: Bastian Leibe


• Need to shift many windows…
• Many computations will be redundant.

Fei-Fei Li Lecture 13 - 56 8-Nov-2016


Speedups: Basin of Attraction

Slide credit: Bastian Leibe


1. Assign all points within radius r of end point to the mode.

Fei-Fei Li Lecture 13 - 57 8-Nov-2016


Speedups

Slide credit: Bastian Leibe


● Assign all points within radius r/c of the search path to the mode
Fei-Fei Li Lecture 13 - 58 8-Nov-2016
n i cal
tech e
not

Mean-shift
Algorithm
Comaniciu & Meer, 2002

Fei-Fei Li Lecture 13 - 59 8-Nov-2016


n i cal
tech e
not

Mean-shift
Algorithm
Comaniciu & Meer, 2002

Fei-Fei Li Lecture 13 - 60 8-Nov-2016


Summary Mean-Shift
• Pros
– General, application-independent tool
– Model-free, does not assume any prior shape (spherical,
elliptical, etc.) on data clusters
– Just a single parameter (window size h)
• h has a physical meaning (unlike k-means)
– Finds variable number of modes
– Robust to outliers
• Cons

Slide credit: Svetlana Lazebnik


– Output depends on window size
– Window size (bandwidth) selection is not trivial
– Computationally (relatively) expensive (~2s/image)
– Does not scale well with dimension of feature space

Fei-Fei Li Lecture 13 - 61 8-Nov-2016


Medoid-Shift & Quick-Shift

• Quick-Shift:
- does not need the gradient or quadratic lower bound
- only one step has to be computed for each point: simply moves each
point to the nearest neighbor for which there is an increment of the
density
- there is no need for a stopping/merging heuristic
- the data space X may be non-Euclidean

[Vedaldi and Soatto, 2008]

Fei-Fei Li Lecture 13 - 62 8-Nov-2016


What have we learned today
• K-means clustering
• Mean-shift clustering

IPython Notebook for SLIC and Quickshift

Fei-Fei Li Lecture 13 - 63 8-Nov-2016

You might also like