Skip to content

Commit 1fefe4c

Browse files
committed
Pushing the docs for revision for branch: master, commit c761ba7661872fa029f07e5a1c6327612843af46
1 parent aa61026 commit 1fefe4c

File tree

1,847 files changed

+34712
-23842
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,847 files changed

+34712
-23842
lines changed

dev/.buildinfo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Sphinx build info version 1
22
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
3-
config: ca7bd685c3cd43d3277e97e8df531e55
3+
config: 62d02c61361f099325d4a13d622765cb
44
tags: 645f666f9bcd5a90fca523b33c5a78b7

dev/_downloads/plot_lena_compress.py renamed to dev/_downloads/plot_face_compress.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
Vector Quantization Example
77
=========================================================
88
9-
The classic image processing example, Lena, an 8-bit grayscale
10-
bit-depth, 512 x 512 sized image, is used here to illustrate
11-
how `k`-means is used for vector quantization.
9+
Face, a 1024 x 768 size image of a raccoon face,
10+
is used here to illustrate how `k`-means is
11+
used for vector quantization.
1212
1313
"""
1414
print(__doc__)
@@ -23,45 +23,52 @@
2323
import matplotlib.pyplot as plt
2424

2525
from sklearn import cluster
26+
from sklearn.utils.testing import SkipTest
27+
from sklearn.utils.fixes import sp_version
2628

27-
n_clusters = 5
28-
np.random.seed(0)
29+
if sp_version < (0, 12):
30+
raise SkipTest("Skipping because SciPy version earlier than 0.12.0 and "
31+
"thus does not include the scipy.misc.face() image.")
2932

3033
try:
31-
lena = sp.lena()
34+
face = sp.face(gray=True)
3235
except AttributeError:
33-
# Newer versions of scipy have lena in misc
36+
# Newer versions of scipy have face in misc
3437
from scipy import misc
35-
lena = misc.lena()
36-
X = lena.reshape((-1, 1)) # We need an (n_sample, n_feature) array
38+
face = misc.face(gray=True)
39+
40+
n_clusters = 5
41+
np.random.seed(0)
42+
43+
X = face.reshape((-1, 1)) # We need an (n_sample, n_feature) array
3744
k_means = cluster.KMeans(n_clusters=n_clusters, n_init=4)
3845
k_means.fit(X)
3946
values = k_means.cluster_centers_.squeeze()
4047
labels = k_means.labels_
4148

4249
# create an array from labels and values
43-
lena_compressed = np.choose(labels, values)
44-
lena_compressed.shape = lena.shape
50+
face_compressed = np.choose(labels, values)
51+
face_compressed.shape = face.shape
4552

46-
vmin = lena.min()
47-
vmax = lena.max()
53+
vmin = face.min()
54+
vmax = face.max()
4855

49-
# original lena
56+
# original face
5057
plt.figure(1, figsize=(3, 2.2))
51-
plt.imshow(lena, cmap=plt.cm.gray, vmin=vmin, vmax=256)
58+
plt.imshow(face, cmap=plt.cm.gray, vmin=vmin, vmax=256)
5259

53-
# compressed lena
60+
# compressed face
5461
plt.figure(2, figsize=(3, 2.2))
55-
plt.imshow(lena_compressed, cmap=plt.cm.gray, vmin=vmin, vmax=vmax)
62+
plt.imshow(face_compressed, cmap=plt.cm.gray, vmin=vmin, vmax=vmax)
5663

57-
# equal bins lena
64+
# equal bins face
5865
regular_values = np.linspace(0, 256, n_clusters + 1)
59-
regular_labels = np.searchsorted(regular_values, lena) - 1
66+
regular_labels = np.searchsorted(regular_values, face) - 1
6067
regular_values = .5 * (regular_values[1:] + regular_values[:-1]) # mean
61-
regular_lena = np.choose(regular_labels.ravel(), regular_values)
62-
regular_lena.shape = lena.shape
68+
regular_face = np.choose(regular_labels.ravel(), regular_values, mode="clip")
69+
regular_face.shape = face.shape
6370
plt.figure(3, figsize=(3, 2.2))
64-
plt.imshow(regular_lena, cmap=plt.cm.gray, vmin=vmin, vmax=vmax)
71+
plt.imshow(regular_face, cmap=plt.cm.gray, vmin=vmin, vmax=vmax)
6572

6673
# histogram
6774
plt.figure(4, figsize=(3, 2.2))

dev/_downloads/plot_lena_segmentation.py renamed to dev/_downloads/plot_face_segmentation.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
2-
=========================================
3-
Segmenting the picture of Lena in regions
4-
=========================================
2+
===================================================
3+
Segmenting the picture of a raccoon face in regions
4+
===================================================
55
66
This example uses :ref:`spectral_clustering` on a graph created from
77
voxel-to-voxel difference on an image to break this image into multiple
@@ -30,45 +30,58 @@
3030

3131
from sklearn.feature_extraction import image
3232
from sklearn.cluster import spectral_clustering
33+
from sklearn.utils.testing import SkipTest
34+
from sklearn.utils.fixes import sp_version
3335

34-
lena = sp.misc.lena()
35-
# Downsample the image by a factor of 4
36-
lena = lena[::2, ::2] + lena[1::2, ::2] + lena[::2, 1::2] + lena[1::2, 1::2]
37-
lena = lena[::2, ::2] + lena[1::2, ::2] + lena[::2, 1::2] + lena[1::2, 1::2]
36+
if sp_version < (0, 12):
37+
raise SkipTest("Skipping because SciPy version earlier than 0.12.0 and "
38+
"thus does not include the scipy.misc.face() image.")
39+
40+
41+
# load the raccoon face as a numpy array
42+
try:
43+
face = sp.face(gray=True)
44+
except AttributeError:
45+
# Newer versions of scipy have face in misc
46+
from scipy import misc
47+
face = misc.face(gray=True)
48+
49+
# Resize it to 10% of the original size to speed up the processing
50+
face = sp.misc.imresize(face, 0.10) / 255.
3851

3952
# Convert the image into a graph with the value of the gradient on the
4053
# edges.
41-
graph = image.img_to_graph(lena)
54+
graph = image.img_to_graph(face)
4255

4356
# Take a decreasing function of the gradient: an exponential
4457
# The smaller beta is, the more independent the segmentation is of the
4558
# actual image. For beta=1, the segmentation is close to a voronoi
4659
beta = 5
4760
eps = 1e-6
48-
graph.data = np.exp(-beta * graph.data / lena.std()) + eps
61+
graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps
4962

5063
# Apply spectral clustering (this step goes much faster if you have pyamg
5164
# installed)
52-
N_REGIONS = 11
65+
N_REGIONS = 25
5366

54-
###############################################################################
67+
#############################################################################
5568
# Visualize the resulting regions
5669

5770
for assign_labels in ('kmeans', 'discretize'):
5871
t0 = time.time()
5972
labels = spectral_clustering(graph, n_clusters=N_REGIONS,
60-
assign_labels=assign_labels,
61-
random_state=1)
73+
assign_labels=assign_labels, random_state=1)
6274
t1 = time.time()
63-
labels = labels.reshape(lena.shape)
75+
labels = labels.reshape(face.shape)
6476

6577
plt.figure(figsize=(5, 5))
66-
plt.imshow(lena, cmap=plt.cm.gray)
78+
plt.imshow(face, cmap=plt.cm.gray)
6779
for l in range(N_REGIONS):
6880
plt.contour(labels == l, contours=1,
69-
colors=[plt.cm.spectral(l / float(N_REGIONS)), ])
81+
colors=[plt.cm.spectral(l / float(N_REGIONS))])
7082
plt.xticks(())
7183
plt.yticks(())
72-
plt.title('Spectral clustering: %s, %.2fs' % (assign_labels, (t1 - t0)))
73-
84+
title = 'Spectral clustering: %s, %.2fs' % (assign_labels, (t1 - t0))
85+
print(title)
86+
plt.title(title)
7487
plt.show()

dev/_downloads/plot_lena_ward_segmentation.py renamed to dev/_downloads/plot_face_ward_segmentation.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
2-
===============================================================
3-
A demo of structured Ward hierarchical clustering on Lena image
4-
===============================================================
2+
=========================================================================
3+
A demo of structured Ward hierarchical clustering on a raccoon face image
4+
=========================================================================
55
66
Compute the segmentation of a 2D image with Ward hierarchical
77
clustering. The clustering is spatially constrained in order
@@ -15,39 +15,57 @@
1515
print(__doc__)
1616

1717
import time as time
18+
1819
import numpy as np
1920
import scipy as sp
21+
2022
import matplotlib.pyplot as plt
23+
2124
from sklearn.feature_extraction.image import grid_to_graph
2225
from sklearn.cluster import AgglomerativeClustering
26+
from sklearn.utils.testing import SkipTest
27+
from sklearn.utils.fixes import sp_version
28+
29+
if sp_version < (0, 12):
30+
raise SkipTest("Skipping because SciPy version earlier than 0.12.0 and "
31+
"thus does not include the scipy.misc.face() image.")
32+
2333

2434
###############################################################################
2535
# Generate data
26-
lena = sp.misc.lena()
27-
# Downsample the image by a factor of 4
28-
lena = lena[::2, ::2] + lena[1::2, ::2] + lena[::2, 1::2] + lena[1::2, 1::2]
29-
X = np.reshape(lena, (-1, 1))
36+
try:
37+
face = sp.face(gray=True)
38+
except AttributeError:
39+
# Newer versions of scipy have face in misc
40+
from scipy import misc
41+
face = misc.face(gray=True)
42+
43+
# Resize it to 10% of the original size to speed up the processing
44+
face = sp.misc.imresize(face, 0.10) / 255.
45+
46+
X = np.reshape(face, (-1, 1))
3047

3148
###############################################################################
3249
# Define the structure A of the data. Pixels connected to their neighbors.
33-
connectivity = grid_to_graph(*lena.shape)
50+
connectivity = grid_to_graph(*face.shape)
3451

3552
###############################################################################
3653
# Compute clustering
3754
print("Compute structured hierarchical clustering...")
3855
st = time.time()
3956
n_clusters = 15 # number of regions
40-
ward = AgglomerativeClustering(n_clusters=n_clusters,
41-
linkage='ward', connectivity=connectivity).fit(X)
42-
label = np.reshape(ward.labels_, lena.shape)
57+
ward = AgglomerativeClustering(n_clusters=n_clusters, linkage='ward',
58+
connectivity=connectivity)
59+
ward.fit(X)
60+
label = np.reshape(ward.labels_, face.shape)
4361
print("Elapsed time: ", time.time() - st)
4462
print("Number of pixels: ", label.size)
4563
print("Number of clusters: ", np.unique(label).size)
4664

4765
###############################################################################
4866
# Plot the results on an image
4967
plt.figure(figsize=(5, 5))
50-
plt.imshow(lena, cmap=plt.cm.gray)
68+
plt.imshow(face, cmap=plt.cm.gray)
5169
for l in range(n_clusters):
5270
plt.contour(label == l, contours=1,
5371
colors=[plt.cm.spectral(l / float(n_clusters)), ])

dev/_downloads/plot_image_denoising.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
=========================================
55
66
An example comparing the effect of reconstructing noisy fragments
7-
of the Lena image using firstly online :ref:`DictionaryLearning` and
7+
of a raccoon face image using firstly online :ref:`DictionaryLearning` and
88
various transform methods.
99
1010
The dictionary is fitted on the distorted left half of the image, and
@@ -37,32 +37,45 @@
3737

3838
import matplotlib.pyplot as plt
3939
import numpy as np
40-
41-
from scipy.misc import lena
40+
import scipy as sp
4241

4342
from sklearn.decomposition import MiniBatchDictionaryLearning
4443
from sklearn.feature_extraction.image import extract_patches_2d
4544
from sklearn.feature_extraction.image import reconstruct_from_patches_2d
45+
from sklearn.utils.testing import SkipTest
46+
from sklearn.utils.fixes import sp_version
47+
48+
if sp_version < (0, 12):
49+
raise SkipTest("Skipping because SciPy version earlier than 0.12.0 and "
50+
"thus does not include the scipy.misc.face() image.")
4651

4752
###############################################################################
48-
# Load Lena image and extract patches
49-
lena = lena() / 256.0
53+
try:
54+
from scipy import misc
55+
face = misc.face(gray=True)
56+
except AttributeError:
57+
# Old versions of scipy have face in the top level package
58+
face = sp.face(gray=True)
59+
60+
# Convert from uint8 representation with values between 0 and 255 to
61+
# a floating point representation with values between 0 and 1.
62+
face = face / 255
5063

5164
# downsample for higher speed
52-
lena = lena[::2, ::2] + lena[1::2, ::2] + lena[::2, 1::2] + lena[1::2, 1::2]
53-
lena /= 4.0
54-
height, width = lena.shape
65+
face = face[::2, ::2] + face[1::2, ::2] + face[::2, 1::2] + face[1::2, 1::2]
66+
face /= 4.0
67+
height, width = face.shape
5568

5669
# Distort the right half of the image
5770
print('Distorting image...')
58-
distorted = lena.copy()
59-
distorted[:, height // 2:] += 0.075 * np.random.randn(width, height // 2)
71+
distorted = face.copy()
72+
distorted[:, width // 2:] += 0.075 * np.random.randn(height, width // 2)
6073

6174
# Extract all reference patches from the left half of the image
6275
print('Extracting reference patches...')
6376
t0 = time()
6477
patch_size = (7, 7)
65-
data = extract_patches_2d(distorted[:, :height // 2], patch_size)
78+
data = extract_patches_2d(distorted[:, :width // 2], patch_size)
6679
data = data.reshape(data.shape[0], -1)
6780
data -= np.mean(data, axis=0)
6881
data /= np.std(data, axis=0)
@@ -85,7 +98,7 @@
8598
interpolation='nearest')
8699
plt.xticks(())
87100
plt.yticks(())
88-
plt.suptitle('Dictionary learned from Lena patches\n' +
101+
plt.suptitle('Dictionary learned from face patches\n' +
89102
'Train time %.1fs on %d patches' % (dt, len(data)),
90103
fontsize=16)
91104
plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)
@@ -99,7 +112,8 @@ def show_with_diff(image, reference, title):
99112
plt.figure(figsize=(5, 3.3))
100113
plt.subplot(1, 2, 1)
101114
plt.title('Image')
102-
plt.imshow(image, vmin=0, vmax=1, cmap=plt.cm.gray, interpolation='nearest')
115+
plt.imshow(image, vmin=0, vmax=1, cmap=plt.cm.gray,
116+
interpolation='nearest')
103117
plt.xticks(())
104118
plt.yticks(())
105119
plt.subplot(1, 2, 2)
@@ -113,14 +127,14 @@ def show_with_diff(image, reference, title):
113127
plt.suptitle(title, size=16)
114128
plt.subplots_adjust(0.02, 0.02, 0.98, 0.79, 0.02, 0.2)
115129

116-
show_with_diff(distorted, lena, 'Distorted image')
130+
show_with_diff(distorted, face, 'Distorted image')
117131

118132
###############################################################################
119133
# Extract noisy patches and reconstruct them using the dictionary
120134

121135
print('Extracting noisy patches... ')
122136
t0 = time()
123-
data = extract_patches_2d(distorted[:, height // 2:], patch_size)
137+
data = extract_patches_2d(distorted[:, width // 2:], patch_size)
124138
data = data.reshape(data.shape[0], -1)
125139
intercept = np.mean(data, axis=0)
126140
data -= intercept
@@ -138,7 +152,7 @@ def show_with_diff(image, reference, title):
138152
reconstructions = {}
139153
for title, transform_algorithm, kwargs in transform_algorithms:
140154
print(title + '...')
141-
reconstructions[title] = lena.copy()
155+
reconstructions[title] = face.copy()
142156
t0 = time()
143157
dico.set_params(transform_algorithm=transform_algorithm, **kwargs)
144158
code = dico.transform(data)
@@ -149,11 +163,11 @@ def show_with_diff(image, reference, title):
149163
if transform_algorithm == 'threshold':
150164
patches -= patches.min()
151165
patches /= patches.max()
152-
reconstructions[title][:, height // 2:] = reconstruct_from_patches_2d(
153-
patches, (width, height // 2))
166+
reconstructions[title][:, width // 2:] = reconstruct_from_patches_2d(
167+
patches, (height, width // 2))
154168
dt = time() - t0
155169
print('done in %.2fs.' % dt)
156-
show_with_diff(reconstructions[title], lena,
170+
show_with_diff(reconstructions[title], face,
157171
title + ' (time: %.1fs)' % dt)
158172

159173
plt.show()
458 Bytes
458 Bytes
1.5 KB
1.5 KB
453 Bytes

0 commit comments

Comments
 (0)