Esempio n. 1
0
def start_kmeans(args):
    imlist = imtools.get_imlist(args.data_dir)

    imnbr = len(imlist)  # get the number of images
    print("The number of images is %d" % imnbr)

    # Create matrix to store all flattened images
    immatrix = np.array([np.array(cv2.imread(imname)).flatten() for imname in imlist], 'f')

    # PCA reduce dimension
    V, S, immean = pca.pca(immatrix)

    immatrix = np.array([np.array(cv2.imread(im)).flatten() for im in imlist])
    immatrix_src = np.array([np.array(cv2.imread(im)) for im in imlist])

    # project on the 40 first PCs
    immean = immean.flatten()
    projected = np.array([dot(V[:40], immatrix[i] - immean) for i in range(imnbr)])

    # k-means
    projected = whiten(projected)
    centroids, distortion = kmeans(projected, args.num_class)
    code, distance = vq(projected, centroids)
   
    output_path = os.path.join(args.output_dir, 'kmeans_result') 
    if not os.path.exists(output_path):
        os.mkdir(output_path)
    # plot clusters
    for k in range(args.num_class):
        ind = where(code == k)[0]
        print("class:", k, len(ind))
        os.mkdir(os.path.join(output_path, str(k)))
        i = rdm.randint(0,len(ind))
        cv2.imwrite(os.path.join(os.path.join(output_path, str(k)), str(i) + '.jpg'), immatrix_src[ind[i]])
Esempio n. 2
0
def save_vis(filename_list, number_list, pred_pca, output_hcluster, output_PCA):

    imlist = filename_list
    imnbr = len(imlist)

    # Load images, run PCA.
    immatrix = array(pred_pca)
    V, S, immean = pca.pca(immatrix)

    # Project on 2 PCs.
    projected = array([dot(V[[0, 1]], immatrix[i] - immean) for i in range(imnbr)])

    # height and width
    h, w = 1200, 1200
    
    # create a new image with a white background
    img = Image.new('RGB', (w, h), (255, 255, 255))
    draw = ImageDraw.Draw(img)

    # draw axis
    draw.line((0, h/2, w, h/2), fill=(255, 0, 0))
    draw.line((w/2, 0, w/2, h), fill=(255, 0, 0))

    # scale coordinates to fit
    scale = abs(projected).max(0)
    scaled = floor(array([(p/scale) * (w/2 - 20, h/2 - 20) + (w/2, h/2)
                          for p in projected])).astype(int)

    # paste thumbnail of each image
    for i in range(imnbr):
        nodeim = Image.open(imlist[i])
        nodeim.thumbnail((25, 25))
        ns = nodeim.size
        box = (scaled[i][0] - ns[0] // 2, scaled[i][1] - ns[1] // 2,
             scaled[i][0] + ns[0] // 2 + 1, scaled[i][1] + ns[1] // 2 + 1)
        img.paste(nodeim, box)

    tree = hcluster.hcluster(projected)
    hcluster.draw_dendrogram(tree,imlist,filename=output_hcluster)

    for i, num in enumerate(number_list):
        if num < 8:
            color1 = mod(370*num,255)
            color2 = mod(170*num,255)
            color3 = mod(270*num,255)
            draw.text((scaled[i][0],scaled[i][1]),str(num),fill = (color1,color2,color3))
        else:
            draw.text((scaled[i][0],scaled[i][1]),str(num),fill = (255,0,0))

    figure()
    imshow(img)
    axis('off')
    img.save(output_PCA)
    show()
Esempio n. 3
0
def inference(mnist):

    x = tf.placeholder(dtype=tf.float32, shape=[None, 784])
    x_image = tf.reshape(x, [-1, 28, 28, 1])

    global_step = tf.Variable(0, trainable=False)

    W_conv1 = weight_variable([3, 3, 1, 128])
    b_conv1 = bias_variable([128])
    h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
    h_pool1 = max_pool_2x2(h_conv1)

    W_conv2 = weight_variable([3, 3, 128, 100])
    b_conv2 = bias_variable([100])
    h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
    h_pool2 = max_pool_2x2(h_conv2)

    # W_conv3 = weight_variable([3, 3, 128, 64])
    # b_conv3 = bias_variable([64])
    # h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3)
    # h_pool3 = max_pool_2x2(h_conv3)

    # W_conv4 = weight_variable([5, 5, 64, 32])
    # b_conv4 = bias_variable([32])
    # h_conv4 = tf.nn.relu(conv2d(h_pool3, W_conv4) + b_conv4)
    # h_pool4 = max_pool_2x2(h_conv4)

    h_pool4_flat = tf.reshape(h_pool2, [-1, 7*7*100])

    # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.4)
    # config = tf.ConfigProto(gpu_options=gpu_options)
    # config.gpu_options.allow_growth = True

    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        xs = mnist.test.images
        ys = mnist.test.labels
        extract_features = sess.run(h_pool4_flat, feed_dict={x: xs})
        print(extract_features.shape)
        V, S, immean = pca.pca(extract_features)
        immean = immean.flatten()
        imnbr = 10000
        projected = array([dot(V[:32], extract_features[i] - immean) for i in range(imnbr)])
        centroids, distortion = kmeans(projected, 20)

        code, distance = vq(projected, centroids)

        tools.calculate_acc(code, ys, 10, 20)

    return extract_features
def main(args):
    print(args)
    # 根据指定总帧数和间隔抽取帧
    frames, frame_ids = get_frames(args.input, args.frame_num, args.frame_interval, args.max_size)
    print(len(frames), len(frame_ids), frames[0].shape)

    immatrix = np.array([x.flatten() for x in frames]).astype(np.float32)
    print(immatrix.shape)
    # PCA降维
    V, S, immean = pca.pca(immatrix)
    print(V.shape, S.shape, immean.shape)

    imnbr = len(frames)
    projected = np.array([np.dot(V[:40],immatrix[i]-immean) for i in range(imnbr)])


    # k-means
    projected = whiten(projected)
    print(projected.shape, type(projected))
    centroids,distortion = kmeans(projected, args.key_frame_num)
    code,distance = vq(projected,centroids)
    print(code.shape, distance.shape)
    print(code)
    print(distance)
    # 使用Kmeans进行聚类
    # kmeans = KMeans(n_clusters=args.key_frame_num, random_state=0).fit(np.array(frames))

    centers = []
    clusters = [[] for i in range(args.key_frame_num)]
    ids = [[] for i in range(args.key_frame_num)]
    for i in range(len(frames)):
        clusters[code[i]].append(distance[i])
        ids[code[i]].append(i)
    print(ids)
    print(clusters)
    for i in range(args.key_frame_num):
        index = np.argsort(clusters[i])
        j = index[0]
        print(index)
        index = ids[i][j]
        print(index)
        centers.append((frames[index], index))
    
    if not os.path.isdir(args.out_dir):
        os.makedirs(args.out_dir)
    basename = os.path.basename(args.input)
    for i, (frame, index) in enumerate(centers):
        cv2.imwrite(os.path.join(args.out_dir, basename + '_%d_%d.jpg' % (index,i)), frame)
 # -*- coding: utf-8 -*-
from PCV.tools import imtools, pca
from PIL import Image, ImageDraw
from pylab import *

imlist = imtools.get_imlist('../data/selectedfontimages/a_selected_thumbs')
imnbr = len(imlist)

# Load images, run PCA.
immatrix = array([array(Image.open(im)).flatten() for im in imlist], 'f')
V, S, immean = pca.pca(immatrix)

# Project on 2 PCs.
projected = array([dot(V[[0, 1]], immatrix[i] - immean) for i in range(imnbr)])  # P131 Fig6-3左图
#projected = array([dot(V[[1, 2]], immatrix[i] - immean) for i in range(imnbr)])  # P131 Fig6-3右图

# height and width
h, w = 1200, 1200

# create a new image with a white background
img = Image.new('RGB', (w, h), (255, 255, 255))
draw = ImageDraw.Draw(img)

# draw axis
draw.line((0, h/2, w, h/2), fill=(255, 0, 0))
draw.line((w/2, 0, w/2, h), fill=(255, 0, 0))

# scale coordinates to fit
scale = abs(projected).max(0)
scaled = floor(array([(p/scale) * (w/2 - 20, h/2 - 20) + (w/2, h/2)
                      for p in projected])).astype(int)
Esempio n. 6
0
# -*- coding: utf-8 -*-
from PCV.tools import imtools, pca
from PIL import Image, ImageDraw
from pylab import *
from scipy.cluster.vq import *

imlist = imtools.get_imlist('tmp/test/')
imnbr = len(imlist)

# Load images, run PCA.
immatrix = array([array(Image.open(im)).flatten() for im in imlist], 'f')
V, S, immean = pca.pca(immatrix)

projected = array([dot(V[[0, 1]], immatrix[i] - immean) for i in range(imnbr)])

n = len(projected)
# compute distance matrix
S = array([[sqrt(sum((projected[i] - projected[j])**2)) for i in range(n)]
           for j in range(n)], 'f')
# create Laplacian matrix
rowsum = sum(S, axis=0)
D = diag(1 / sqrt(rowsum))
I = identity(n)
L = I - dot(D, dot(S, D))
# compute eigenvectors of L
U, sigma, V = linalg.svd(L)
k = 20
# create feature vector from k first eigenvectors
# by stacking eigenvectors as columns
features = array(V[:k]).T
# k-means
Esempio n. 7
0
def PCA_Kmeans(train_path,result_path):
    # Uses sparse pca codepath.
    # imlist = imtools.get_imlist('C:/Users/Zqc/Desktop/PCV-book-data/data/selectedfontimages/a_selected_thumbs/')

    # falter_image("D:/datebase/test")

    imlist = imtools.get_imlist(train_path)
    # print(imlist)
    # 获取图像列表和他们的尺寸
    im = array(Image.open(imlist[0]))  # open one image to get the size
    m, n = im.shape[:2]  # get the size of the images
    imnbr = len(imlist)  # get the number of images
    print("The number of images is %d" % imnbr)

    # Create matrix to store all flattened images
    immatrix = array([array(Image.open(imname)).flatten() for imname in imlist], 'f')

    # PCA降维
    V, S, immean = pca.pca(immatrix)

    # 保存均值和主成分
    # f = open('./a_pca_modes.pkl', 'wb')
    # f = open('C:/Users/Zqc/Desktop/PCV-book-data/data/selectedfontimages/a_pca_modes.pkl', 'wb')

    f = open(train_path + "\\a_pca_modes.pkl", 'wb')

    pickle.dump(immean, f)
    pickle.dump(V, f)
    f.close()

    # get list of images
    # imlist = imtools.get_imlist('C:/Users/Zqc/Desktop/PCV-book-data/data/selectedfontimages/a_selected_thumbs/')
    print("go")
    imlist = imtools.get_imlist(train_path)
    imnbr = len(imlist)
    # load model file
    # with open('C:/Users/Zqc/Desktop/PCV-book-data/data/selectedfontimages/a_pca_modes.pkl','rb') as f:
    with open(train_path + "a_pca_modes.pkl", 'rb') as f:
        immean = pickle.load(f)
        V = pickle.load(f)
    # create matrix to store all flattened images
    immatrix = array([array(Image.open(im)).flatten() for im in imlist], 'f')

    # project on the 40 first PCs
    immean = immean.flatten()

    projected = array([dot(V[:30], immatrix[i] - immean) for i in range(imnbr)])
    print("PCA")

    # k-means
    projected = whiten(projected)
    centroids, distortion = kmeans(projected, 370)
    code, distance = vq(projected, centroids)

    filepath = result_path
    # plot clusters
    for k in range(370):
        tempath = filepath + str(k)
        os.makedirs(tempath)
        ind = where(code == k)[0]
        for i in range(len(ind)):
            io.imsave(tempath + "\\" + str(i) + ".png", immatrix[ind[i]].reshape((100, 100)).astype(np.uint8),
                      cmap="gray")
Esempio n. 8
0
print 'training data is:', features.shape, len(labels)

# read test data
####################
test_features,test_labels = read_gesture_features_labels('../data/hand_gesture/test/')
print 'test data is:', test_features.shape, len(test_labels)

classnames = unique(labels)
nbr_classes = len(classnames)


if False:
    # reduce dimensions with PCA
    from PCV.tools import pca

    V,S,m = pca.pca(features)

    # keep most important dimensions
    V = V[:50]
    features = array([dot(V,f-m) for f in features])
    test_features = array([dot(V,f-m) for f in test_features])


if True:
    # test kNN
    k = 1
    knn_classifier = knn.KnnClassifier(labels,features)
    res = array([knn_classifier.classify(test_features[i],k) for i in range(len(test_labels))]) # TODO kan goras battre


if False:
print 'training data is:', features.shape, len(labels)

# read test data
####################
test_features, test_labels = read_gesture_features_labels(
    '../data/hand_gesture/test/')
print 'test data is:', test_features.shape, len(test_labels)

classnames = unique(labels)
nbr_classes = len(classnames)

if False:
    # reduce dimensions with PCA
    from PCV.tools import pca

    V, S, m = pca.pca(features)

    # keep most important dimensions
    V = V[:50]
    features = array([dot(V, f - m) for f in features])
    test_features = array([dot(V, f - m) for f in test_features])

if True:
    # test kNN
    k = 1
    knn_classifier = knn.KnnClassifier(labels, features)
    res = array([
        knn_classifier.classify(test_features[i], k)
        for i in range(len(test_labels))
    ])  # TODO kan goras battre
Esempio n. 10
0
            else:
                temp = "%d: None" % (j)
            str += temp + "| "
        summ += max_count
        if step % 1000 == 0:
            print("in cluster %d:" % i)
            print(str + "|| MAX PERCENT: %d: %g of %d" % (f, ma, len(part)))
    print("TOTAL ACC %g" % (summ/len(label)))


mnist = input_data.read_data_sets("mnist_data", one_hot=False)

xs = mnist.test.images
ys = mnist.test.labels

V, S, immean = pca.pca(xs)

f = open('./mnist_test_pca_modes.pkl', 'wb')
pickle.dump(immean, f)
pickle.dump(V, f)
f.close()

immean = immean.flatten()
imnbr = 10000
projected = array([dot(V[:32], xs[i]-immean) for i in range(imnbr)])


# k-means
projected = whiten(projected)

centroids, distortion = kmeans(projected, 15)