Commit 536c95c0 authored by kiegeland's avatar kiegeland
Browse files

added vector_from_image_multilabel.py

parent a464e8ba
Loading
Loading
Loading
Loading
+50 −0
Original line number Diff line number Diff line
from argparse import ArgumentParser
import tensorflow as tf
import numpy as np
import pickle


def main():
    parser = ArgumentParser(
        description=''
    )
    parser.add_argument(
        '-g', '--graph', required=True, help='path to graph'
        )
    parser.add_argument(
        '-i', '--image', required=True, help='path to image')
    parser.add_argument(
        '-l', '--label', required=True, help='path to labels')
    
    args = parser.parse_args()
    graph = args.graph
    img = args.image
    interface = []
    
    with tf.gfile.FastGFile(graph, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        _ = tf.import_graph_def(graph_def, name='')

    image_data = tf.gfile.FastGFile(img, 'rb').read()
    
    label_lines = [line.rstrip() for line 
                    in tf.gfile.GFile(args.label)]
    with tf.Session() as sess:
        # Feed the image_data as input to the graph and get first prediction
        feature_tensor = sess.graph.get_tensor_by_name('pool_3/_reshape:0')
        
        results = sess.run(feature_tensor, \
                {'DecodeJpeg/contents:0': image_data})[0]
        
        d = dict()
        d['file_name'] = img
        d['vector'] = np.squeeze(results)
        interface.append(d)

    output_file = 'single_picture_multilabel.pickle'
    with open(output_file, 'wb') as handle:
        pickle.dump(interface, handle, protocol=pickle.HIGHEST_PROTOCOL)
    
if __name__ == '__main__':
    main()