-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.py
67 lines (51 loc) · 1.86 KB
/
Test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
import overfeat
import numpy as np
import os
import json
import pickle
from scipy.misc import imresize
from scipy.ndimage import imread
from NeuralPython.Utils import Builders
def overfeatFeatures(image):
image = imresize(image, (231, 231)).astype(np.float32)
h = image.shape[0]
w = image.shape[1]
c = image.shape[2]
# numpy loads image with colors as last dimension, transpose tensor
image = image.reshape(w * h, c)
image = image.transpose()
image = image.reshape(c, h, w)
# run overfeat on the image
overfeat.fprop(image)
features = overfeat.get_output(19).copy()
return features
def pcaDimensionalityReduction(vector, pca, nFeatures):
img2 = pca.transform(vector)
return img2[:, :nFeatures]
def extractFeatures(img, pca, nFeatures):
fullVector = overfeatFeatures(img).reshape((1, 4096))
pcaVector = pcaDimensionalityReduction(fullVector, pca, nFeatures).reshape((nFeatures))
return pcaVector
def print5Best(classes, x):
sortedIndex = np.argsort(-x)
s = ""
for i in range(5):
index = sortedIndex[i]
prob = x[index]
className = classes[index][1]
s += "Predicción " + str(i + 1) + ": Raza " + className + ", con probabilidad " + str(prob) + "\n"
print s
overfeat.init('/home/juanjo/U/overfeat/data/default/net_weight_0', 0)
rootDir = "/home/juanjo/U/Búsqueda por Contenido en Imágenes y Videos/Proyecto/TestImages/"
config = json.load(open("ConfigNeuralNetwork.json"))
pca = pickle.load(open("pca.txt"))
net = Builders.buildNetwork(config)
classes = pickle.load(open("classes.pkl"))
for filename in os.listdir(rootDir):
path = os.path.join(rootDir, filename)
image = imread(path).astype(np.float32)
features = extractFeatures(image, pca, 280)
x = net.forward(features, test = True)
print filename
print5Best(classes, x)