Pokémon Recognition

Collecting Data

Pikachu
  • Pikachu — because it’s the most known pokemon in the world
Tangela
Charizard
  • Charizard — because it’s a dragon, and dragons are cool
Gengar
  • Gengar — `cause he looks like he’s gonna whoop yo’ ass!

Preparing the data

from PIL import Image, ImageOps
...
img = Image.open(pokemon_path).convert('RGB')
img = ImageOps.fit(img, (200, 200), Image.ANTIALIAS, (0.5, 0.5))
img = ImageOps.grayscale(img)

What’s next?

Charizard (collapsed)
pokemon = get_pokemon()
X = pokemon.data
y = pokemon.target
kf = KFold(len(y), n_folds=4, shuffle=True)
for train_index, test_index in kf:
X_train = np.array([X[i] for i in train_index])
X_test = np.array([X[i] for i in test_index])
y_train = np.array([y[i] for i in train_index])
y_test = np.array([y[i] for i in test_index])
param_grid = {
'kernel': ['rbf', 'linear'],
'C': [1e3, 5e3, 1e4, 5e4, 1e5],
'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1],
}
clf = GridSearchCV(SVC(class_weight='balanced'), param_grid)
clf = clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
print classification_report(y_test, y_pred, target_names=pokemon.target_names)
print confusion_matrix(y_test, y_pred, labels=range(n_classes))
...
param_grid = {
'n_neighbors': list(xrange(1, 15)),
}
clf = GridSearchCV(KNeighborsClassifier(), param_grid)
clf = clf.fit(X_train, y_train)
...

PCA

n_components = .8
pca = PCA(n_components=n_components, whiten=True).fit(X_train)
X_train_pca = pca.transform(X_train)
X_test_pca = pca.transform(X_test)
Scary eigenpokemon
Top row: Reconstructed Gengar (18 PC and 38 PC) Bottom row: Reconstructed Charizard (18 PC and 38 PC)
param_grid = {
'kernel': ['rbf', 'linear'],
'C': [1e3, 5e3, 1e4, 5e4, 1e5],
'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1],
}
clf = GridSearchCV(SVC(class_weight='balanced'), param_grid)
clf = clf.fit(X_train_pca, y_train)
y_pred = clf.predict(X_test_pca)

--

--

Every day matters

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store