def label(data_path, number_of_labels=1, equal_weights=True):
    """ 
    Create annotation files for directories of pictures. 
    Number of labels returned can be adjusted.
    Equal weighting of predictions.
    """
    net = DecafNet()

    entities = os.listdir(data_path)
    for entity in entities:
        labels = []
        pictures = os.listdir(os.path.join(data_path,entity))
        for picture in pictures:
            imarray = numpy.array(Image.open(os.path.join(data_path,entity,picture)))
            scores = net.classify(imarray)
            predictions = net.top_k_prediction(scores,number_of_labels) # Format:([confidence],[labels])
            labels.extend(predictions[1])
        
        if equal_weights:
            prediction = max(set(labels),key=labels.count)
        elif confidence_weights:
            pass

        with open("label.txt","w") as opened_file:
            opened_file.write(prediction)
Exemple #2
0
#!/usr/bin/python

import sys

sys.path.append("decaf")
from decaf.scripts.imagenet import DecafNet
import cStringIO as StringIO
import Image
import numpy as np
from time import time as tic

net = DecafNet('imagenet.decafnet.epoch90', 'imagenet.decafnet.meta')

start_time = tic()

#img = np.asarray(Image.open("cat.jpg"))
img_content = open("cat.jpg").read()
print len(img_content)
stream = StringIO.StringIO(img_content)
img = np.asarray(Image.open(stream))

scores = net.classify(img)
print net.top_k_prediction(scores, 5)
#scores = net.classify(img, center_only=True)

end_time = tic()
print "diff_time: %f" % (end_time - start_time)