Skip to content

Commit 111d63d

Browse files
committed
Pushing the docs to dev/ for branch: master, commit f107e53a9ba43f4eac9bbbfa136797e802d21ed1
1 parent d48cbec commit 111d63d

File tree

1,088 files changed

+3644
-3678
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,088 files changed

+3644
-3678
lines changed
-385 Bytes
Binary file not shown.
-387 Bytes
Binary file not shown.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [],
10+
"source": [
11+
"%matplotlib inline"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"\n# Segmenting the picture of greek coins in regions\n\n\nThis example uses `spectral_clustering` on a graph created from\nvoxel-to-voxel difference on an image to break this image into multiple\npartly-homogeneous regions.\n\nThis procedure (spectral clustering on an image) is an efficient\napproximate solution for finding normalized graph cuts.\n\nThere are two options to assign labels:\n\n* with 'kmeans' spectral clustering will cluster samples in the embedding space\n using a kmeans algorithm\n* whereas 'discrete' will iteratively search for the closest partition\n space to the embedding space.\n\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {
25+
"collapsed": false
26+
},
27+
"outputs": [],
28+
"source": [
29+
"print(__doc__)\n\n# Author: Gael Varoquaux <[email protected]>, Brian Cheung\n# License: BSD 3 clause\n\nimport time\n\nimport numpy as np\nfrom scipy.ndimage.filters import gaussian_filter\nimport matplotlib.pyplot as plt\nfrom skimage.data import coins\nfrom skimage.transform import rescale\n\nfrom sklearn.feature_extraction import image\nfrom sklearn.cluster import spectral_clustering\n\n\n# load the coins as a numpy array\norig_coins = coins()\n\n# Resize it to 20% of the original size to speed up the processing\n# Applying a Gaussian filter for smoothing prior to down-scaling\n# reduces aliasing artifacts.\nsmoothened_coins = gaussian_filter(orig_coins, sigma=2)\nrescaled_coins = rescale(smoothened_coins, 0.2, mode=\"reflect\")\n\n# Convert the image into a graph with the value of the gradient on the\n# edges.\ngraph = image.img_to_graph(rescaled_coins)\n\n# Take a decreasing function of the gradient: an exponential\n# The smaller beta is, the more independent the segmentation is of the\n# actual image. For beta=1, the segmentation is close to a voronoi\nbeta = 10\neps = 1e-6\ngraph.data = np.exp(-beta * graph.data / graph.data.std()) + eps\n\n# Apply spectral clustering (this step goes much faster if you have pyamg\n# installed)\nN_REGIONS = 25"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"Visualize the resulting regions\n\n"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {
43+
"collapsed": false
44+
},
45+
"outputs": [],
46+
"source": [
47+
"for assign_labels in ('kmeans', 'discretize'):\n t0 = time.time()\n labels = spectral_clustering(graph, n_clusters=N_REGIONS,\n assign_labels=assign_labels, random_state=42)\n t1 = time.time()\n labels = labels.reshape(rescaled_coins.shape)\n\n plt.figure(figsize=(5, 5))\n plt.imshow(rescaled_coins, cmap=plt.cm.gray)\n for l in range(N_REGIONS):\n plt.contour(labels == l,\n colors=[plt.cm.spectral(l / float(N_REGIONS))])\n plt.xticks(())\n plt.yticks(())\n title = 'Spectral clustering: %s, %.2fs' % (assign_labels, (t1 - t0))\n print(title)\n plt.title(title)\nplt.show()"
48+
]
49+
}
50+
],
51+
"metadata": {
52+
"kernelspec": {
53+
"display_name": "Python 3",
54+
"language": "python",
55+
"name": "python3"
56+
},
57+
"language_info": {
58+
"codemirror_mode": {
59+
"name": "ipython",
60+
"version": 3
61+
},
62+
"file_extension": ".py",
63+
"mimetype": "text/x-python",
64+
"name": "python",
65+
"nbconvert_exporter": "python",
66+
"pygments_lexer": "ipython3",
67+
"version": "3.6.4"
68+
}
69+
},
70+
"nbformat": 4,
71+
"nbformat_minor": 0
72+
}

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
2-
===================================================
3-
Segmenting the picture of a raccoon face in regions
4-
===================================================
2+
================================================
3+
Segmenting the picture of greek coins 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
@@ -25,37 +25,32 @@
2525
import time
2626

2727
import numpy as np
28-
import scipy as sp
2928
from scipy.ndimage.filters import gaussian_filter
3029
import matplotlib.pyplot as plt
31-
from skimage import img_as_float
30+
from skimage.data import coins
3231
from skimage.transform import rescale
3332

3433
from sklearn.feature_extraction import image
3534
from sklearn.cluster import spectral_clustering
3635

3736

38-
# load the raccoon face as a numpy array
39-
try: # SciPy >= 0.16 have face in misc
40-
from scipy.misc import face
41-
orig_face = img_as_float(face(gray=True))
42-
except ImportError:
43-
orig_face = img_as_float(sp.face(gray=True))
37+
# load the coins as a numpy array
38+
orig_coins = coins()
4439

45-
# Resize it to 10% of the original size to speed up the processing
40+
# Resize it to 20% of the original size to speed up the processing
4641
# Applying a Gaussian filter for smoothing prior to down-scaling
4742
# reduces aliasing artifacts.
48-
smoothened_face = gaussian_filter(orig_face, sigma=4.5)
49-
rescaled_face = rescale(smoothened_face, 0.1, mode="reflect")
43+
smoothened_coins = gaussian_filter(orig_coins, sigma=2)
44+
rescaled_coins = rescale(smoothened_coins, 0.2, mode="reflect")
5045

5146
# Convert the image into a graph with the value of the gradient on the
5247
# edges.
53-
graph = image.img_to_graph(rescaled_face)
48+
graph = image.img_to_graph(rescaled_coins)
5449

5550
# Take a decreasing function of the gradient: an exponential
5651
# The smaller beta is, the more independent the segmentation is of the
5752
# actual image. For beta=1, the segmentation is close to a voronoi
58-
beta = 5
53+
beta = 10
5954
eps = 1e-6
6055
graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps
6156

@@ -71,10 +66,10 @@
7166
labels = spectral_clustering(graph, n_clusters=N_REGIONS,
7267
assign_labels=assign_labels, random_state=42)
7368
t1 = time.time()
74-
labels = labels.reshape(rescaled_face.shape)
69+
labels = labels.reshape(rescaled_coins.shape)
7570

7671
plt.figure(figsize=(5, 5))
77-
plt.imshow(rescaled_face, cmap=plt.cm.gray)
72+
plt.imshow(rescaled_coins, cmap=plt.cm.gray)
7873
for l in range(N_REGIONS):
7974
plt.contour(labels == l,
8075
colors=[plt.cm.spectral(l / float(N_REGIONS))])
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [],
10+
"source": [
11+
"%matplotlib inline"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"\n# A demo of structured Ward hierarchical clustering on an image of coins\n\n\nCompute the segmentation of a 2D image with Ward hierarchical\nclustering. The clustering is spatially constrained in order\nfor each segmented region to be in one piece.\n\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {
25+
"collapsed": false
26+
},
27+
"outputs": [],
28+
"source": [
29+
"# Author : Vincent Michel, 2010\n# Alexandre Gramfort, 2011\n# License: BSD 3 clause\n\nprint(__doc__)\n\nimport time as time\n\nimport numpy as np\nfrom scipy.ndimage.filters import gaussian_filter\n\nimport matplotlib.pyplot as plt\n\nfrom skimage.data import coins\nfrom skimage.transform import rescale\n\nfrom sklearn.feature_extraction.image import grid_to_graph\nfrom sklearn.cluster import AgglomerativeClustering\n\n\n# #############################################################################\n# Generate data\norig_coins = coins()\n\n# Resize it to 20% of the original size to speed up the processing\n# Applying a Gaussian filter for smoothing prior to down-scaling\n# reduces aliasing artifacts.\nsmoothened_coins = gaussian_filter(orig_coins, sigma=2)\nrescaled_coins = rescale(smoothened_coins, 0.2, mode=\"reflect\")\n\nX = np.reshape(rescaled_coins, (-1, 1))\n\n# #############################################################################\n# Define the structure A of the data. Pixels connected to their neighbors.\nconnectivity = grid_to_graph(*rescaled_coins.shape)\n\n# #############################################################################\n# Compute clustering\nprint(\"Compute structured hierarchical clustering...\")\nst = time.time()\nn_clusters = 27 # number of regions\nward = AgglomerativeClustering(n_clusters=n_clusters, linkage='ward',\n connectivity=connectivity)\nward.fit(X)\nlabel = np.reshape(ward.labels_, rescaled_coins.shape)\nprint(\"Elapsed time: \", time.time() - st)\nprint(\"Number of pixels: \", label.size)\nprint(\"Number of clusters: \", np.unique(label).size)\n\n# #############################################################################\n# Plot the results on an image\nplt.figure(figsize=(5, 5))\nplt.imshow(rescaled_coins, cmap=plt.cm.gray)\nfor l in range(n_clusters):\n plt.contour(label == l,\n colors=[plt.cm.spectral(l / float(n_clusters)), ])\nplt.xticks(())\nplt.yticks(())\nplt.show()"
30+
]
31+
}
32+
],
33+
"metadata": {
34+
"kernelspec": {
35+
"display_name": "Python 3",
36+
"language": "python",
37+
"name": "python3"
38+
},
39+
"language_info": {
40+
"codemirror_mode": {
41+
"name": "ipython",
42+
"version": 3
43+
},
44+
"file_extension": ".py",
45+
"mimetype": "text/x-python",
46+
"name": "python",
47+
"nbconvert_exporter": "python",
48+
"pygments_lexer": "ipython3",
49+
"version": "3.6.4"
50+
}
51+
},
52+
"nbformat": 4,
53+
"nbformat_minor": 0
54+
}

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
2-
=========================================================================
3-
A demo of structured Ward hierarchical clustering on a raccoon face image
4-
=========================================================================
2+
======================================================================
3+
A demo of structured Ward hierarchical clustering on an image of coins
4+
======================================================================
55
66
Compute the segmentation of a 2D image with Ward hierarchical
77
clustering. The clustering is spatially constrained in order
@@ -17,12 +17,11 @@
1717
import time as time
1818

1919
import numpy as np
20-
import scipy as sp
2120
from scipy.ndimage.filters import gaussian_filter
2221

2322
import matplotlib.pyplot as plt
2423

25-
from skimage import img_as_float
24+
from skimage.data import coins
2625
from skimage.transform import rescale
2726

2827
from sklearn.feature_extraction.image import grid_to_graph
@@ -31,41 +30,37 @@
3130

3231
# #############################################################################
3332
# Generate data
34-
try: # SciPy >= 0.16 have face in misc
35-
from scipy.misc import face
36-
orig_face = img_as_float(face(gray=True))
37-
except ImportError:
38-
orig_face = img_as_float(sp.face(gray=True))
33+
orig_coins = coins()
3934

40-
# Resize it to 10% of the original size to speed up the processing
35+
# Resize it to 20% of the original size to speed up the processing
4136
# Applying a Gaussian filter for smoothing prior to down-scaling
4237
# reduces aliasing artifacts.
43-
smoothened_face = gaussian_filter(orig_face, sigma=4.5)
44-
rescaled_face = rescale(smoothened_face, 0.1, mode="reflect")
38+
smoothened_coins = gaussian_filter(orig_coins, sigma=2)
39+
rescaled_coins = rescale(smoothened_coins, 0.2, mode="reflect")
4540

46-
X = np.reshape(rescaled_face, (-1, 1))
41+
X = np.reshape(rescaled_coins, (-1, 1))
4742

4843
# #############################################################################
4944
# Define the structure A of the data. Pixels connected to their neighbors.
50-
connectivity = grid_to_graph(*rescaled_face.shape)
45+
connectivity = grid_to_graph(*rescaled_coins.shape)
5146

5247
# #############################################################################
5348
# Compute clustering
5449
print("Compute structured hierarchical clustering...")
5550
st = time.time()
56-
n_clusters = 15 # number of regions
51+
n_clusters = 27 # number of regions
5752
ward = AgglomerativeClustering(n_clusters=n_clusters, linkage='ward',
5853
connectivity=connectivity)
5954
ward.fit(X)
60-
label = np.reshape(ward.labels_, rescaled_face.shape)
55+
label = np.reshape(ward.labels_, rescaled_coins.shape)
6156
print("Elapsed time: ", time.time() - st)
6257
print("Number of pixels: ", label.size)
6358
print("Number of clusters: ", np.unique(label).size)
6459

6560
# #############################################################################
6661
# Plot the results on an image
6762
plt.figure(figsize=(5, 5))
68-
plt.imshow(rescaled_face, cmap=plt.cm.gray)
63+
plt.imshow(rescaled_coins, cmap=plt.cm.gray)
6964
for l in range(n_clusters):
7065
plt.contour(label == l,
7166
colors=[plt.cm.spectral(l / float(n_clusters)), ])

dev/_downloads/plot_face_segmentation.ipynb

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)