Skip to content

Commit d8eb307

Browse files
committed
DOc Add documentation to scripts
1 parent 28b8273 commit d8eb307

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

ch10/figure18.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
#
66
# It is made available under the MIT License
77

8+
from __future__ import print_function
89
import mahotas as mh
910
from sklearn import cross_validation
1011
from sklearn.linear_model.logistic import LogisticRegression
11-
from mpltools import style
1212
from matplotlib import pyplot as plt
1313
import numpy as np
1414
from glob import glob
1515

1616
basedir = 'AnimTransDistr'
17+
print('This script will test classification of the AnimTransDistr dataset')
1718

1819

1920
def features_for(images):
@@ -39,6 +40,7 @@ def features_labels(groups):
3940
'Trans',
4041
]
4142

43+
print('Computing whole-image texture features...')
4244
features = []
4345
labels = []
4446
for ci, cl in enumerate(classes):
@@ -68,12 +70,14 @@ def features_labels(groups):
6870
labels.extend([ci for _ in curimages])
6971
labels = np.array(labels)
7072

73+
print('Computing SURF descriptors...')
7174
alldescriptors = []
7275
for im in images:
7376
im = mh.imread(im, as_grey=1)
7477
im = im.astype(np.uint8)
7578

76-
#alldescriptors.append(surf.dense(im, spacing=max(im.shape)//32))
79+
# To use dense sampling, you can try the following line:
80+
# alldescriptors.append(surf.dense(im, spacing=max(im.shape)//32))
7781
alldescriptors.append(surf.surf(im, descriptor_only=True))
7882

7983
print('Descriptors done')
@@ -82,7 +86,7 @@ def features_labels(groups):
8286

8387
concatenated = np.concatenate(alldescriptors)
8488
concatenated = concatenated[::64]
85-
print('k-meaning...')
89+
print('Clustering with K-means...')
8690
km.fit(concatenated)
8791
features = []
8892
for d in alldescriptors:
@@ -97,22 +101,21 @@ def features_labels(groups):
97101
print('Accuracy (5 fold x-val) with Log. Reg [SURF features]: %s%%' % (
98102
0.1 * round(1000 * scoreSURFlr.mean())))
99103

100-
print('combined...')
104+
print('Attemping classification with all features combined...')
101105
allfeatures = np.hstack([features, tfeatures])
102106
scoreSURFplr = cross_validation.cross_val_score(
103107
LogisticRegression(), allfeatures, labels, cv=5).mean()
104108

105109
print('Accuracy (5 fold x-val) with Log. Reg [All features]: %s%%' % (
106110
0.1 * round(1000 * scoreSURFplr.mean())))
107111

108-
style.use('ggplot')
109112
plt.plot([0, 1, 2], 100 *
110113
np.array([scores0.mean(), scoreSURFlr, scoreSURFplr]), 'k-', lw=8)
111114
plt.plot(
112115
[0, 1, 2], 100 * np.array([scores0.mean(), scoreSURFlr, scoreSURFplr]),
113116
'o', mec='#cccccc', mew=12, mfc='white')
114117
plt.xlim(-.5, 2.5)
115118
plt.ylim(scores0.mean() * 90., scoreSURFplr * 110)
116-
plt.xticks([0, 1, 2], ["baseline", "SURF", "combined"])
119+
plt.xticks([0, 1, 2], ["Texture", "SURF", "combined"])
117120
plt.ylabel('Accuracy (%)')
118121
plt.savefig('../1400OS_10_18+.png')

ch10/simple_classification.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def features_for(im):
3535
sobels = []
3636
labels = []
3737

38+
print('This script will test (with cross-validation) classification of the simple 3 class dataset')
3839
print('Computing features...')
3940
# Use glob to get all the images
4041
images = glob('{}/*.jpg'.format(basedir))

0 commit comments

Comments
 (0)