import os
import keras
from keras.preprocessing import image
from keras.applications.imagenet_utils import decode_predictions, preprocess_input
from keras.models import Model
model = keras.applications.VGG16(weights='imagenet', include_top=True)
feat_extractor = Model(inputs=model.input, outputs=model.get_layer("fc2").output)
import numpy as np
import matplotlib.pyplot as plt
def load_image(path):
img = image.load_img(path, target_size=model.input_shape[1:3])
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
return img, x
images_path = 'D:/code/python/data/101_ObjectCategories'
image_extensions = ['.jpg', '.png', '.jpeg'] # case-insensitive (upper/lower doesn't matter)
max_num_images = 10000
import random
images = [os.path.join(dp, f) for dp, dn, filenames in os.walk(images_path) for f in filenames if os.path.splitext(f)[1].lower() in image_extensions]
if max_num_images < len(images):
images = [images[i] for i in sorted(random.sample(xrange(len(images)), max_num_images))]
print("keeping %d images to analyze" % len(images))
import time
tic = time.clock()
features = []
for i, image_path in enumerate(images):
if i % 500 == 0:
toc = time.clock()
elap = toc-tic;
print("analyzing image %d / %d. Time: %4.4f seconds." % (i, len(images), elap))
tic = time.clock()
img, x = load_image(image_path);
feat = feat_extractor.predict(x)[0]
features.append(feat)
print('finished extracting features for %d images' % len(images))
from sklearn.decomposition import PCA
features = np.array(features)
pca = PCA(n_components=300)
pca.fit(features)
pca_features = pca.transform(features)
import pickle
pickle.dump([images, pca_features, pca], open('./features_caltech101.p', 'wb'))
images, pca_features, pca = pickle.load(open('./features_caltech101.p', 'rb'))
for img, f in list(zip(images, pca_features))[0:5]:
print("image: %s, features: %0.2f,%0.2f,%0.2f,%0.2f... "%(img, f[0], f[1], f[2], f[3]))
num_images_to_plot = 1000
if len(images) > num_images_to_plot:
sort_order = sorted(random.sample(range(len(images)), num_images_to_plot))
images = [images[i] for i in sort_order]
pca_features = [pca_features[i] for i in sort_order]
X = np.array(pca_features)
tsne = TSNE(n_components=2, learning_rate=150, perplexity=30, angle=0.2, verbose=2).fit_transform(X)
tx, ty = tsne[:,0], tsne[:,1]
tx = (tx-np.min(tx)) / (np.max(tx) - np.min(tx))
ty = (ty-np.min(ty)) / (np.max(ty) - np.min(ty))
width = 4000
height = 3000
max_dim = 100
full_image = Image.new('RGBA', (width, height))
for img, x, y in zip(images, tx, ty):
tile = Image.open(img)
rs = max(1, tile.width/max_dim, tile.height/max_dim)
tile = tile.resize((int(tile.width/rs), int(tile.height/rs)), Image.ANTIALIAS)
full_image.paste(tile, (int((width-max_dim)*x), int((height-max_dim)*y)), mask=tile.convert('RGBA'))
matplotlib.pyplot.figure(figsize = (16,12))
imshow(full_image)
matplotlib.pyplot.show()
full_image.save("./example-tSNE-caltech101.png")
效果如下图: