示例#1
0
def inference(filename):
    layers = 50
    
    img = load_image(filename)
    print(img.shape)
    
    sess = tf.Session()
    
    new_saver = tf.train.import_meta_graph(meta_fn(layers))
    new_saver.restore(sess, checkpoint_fn(layers))
    
    graph = tf.get_default_graph()
    prob_tensor = graph.get_tensor_by_name("prob:0")
    images = graph.get_tensor_by_name("images:0")
    for op in graph.get_operations():
        print op.name

    #init = tf.initialize_all_variables()
    #sess.run(init)
    print "graph restored"
    
    batch = img.reshape((1, 224, 224, 3))
    
    feed_dict = {images: batch}
    
    prob = sess.run(prob_tensor, feed_dict=feed_dict)

    return print_prob(prob[0])
示例#2
0
def resnet(dataDir, layers):
    from convert import print_prob, load_image, checkpoint_fn, meta_fn
    import tensorflow as tf
    import cv2

    sess = tf.Session()

    new_saver = tf.train.import_meta_graph('./models/' + meta_fn(layers))
    new_saver.restore(sess, './models/' + checkpoint_fn(layers))

    graph = tf.get_default_graph()
    prob_tensor_1 = graph.get_tensor_by_name("prob:0")
    prob_tensor_2 = graph.get_tensor_by_name("avg_pool:0")
    images = graph.get_tensor_by_name("images:0")
    for op in graph.get_operations():
        print(op.name)

    print("graph restored")

    start_time = time.time()

    EMB1, EMB2 = None, None
    image_names = os.listdir(dataDir)
    image_names = image_names[:]
    images_list = np.zeros(shape=(len(image_names), cfg.EMB_IMAGE_HEIGHT,
                                  cfg.EMB_IMAGE_WIDTH, 3))
    for i, img_name in enumerate(image_names):
        if i == 1:
            start_time = time.time()
        print(
            str(i + 1).rjust(4) + '/' + str(len(image_names)) + ' - ' +
            img_name)
        img = load_image(os.path.join(dataDir, img_name))

        image = cv2.imread(os.path.join(dataDir, img_name))
        image = cv2.resize(image, (cfg.EMB_IMAGE_HEIGHT, cfg.EMB_IMAGE_WIDTH),
                           interpolation=cv2.INTER_CUBIC)
        images_list[i] = image

        batch = img.reshape((1, 224, 224, 3))

        feed_dict = {images: batch}
        prob = sess.run([prob_tensor_1, prob_tensor_2], feed_dict=feed_dict)

        embedding = prob[0].flatten()
        if EMB1 is None:
            EMB1 = np.zeros((len(image_names), len(embedding)),
                            dtype='float32')
        EMB1[i] = embedding

        embedding = prob[1].flatten()
        if EMB2 is None:
            EMB2 = np.zeros((len(image_names), len(embedding)),
                            dtype='float32')
        EMB2[i] = embedding

    print("--- ResNet time: %s seconds ---" % (time.time() - start_time))

    return sess, images_list, image_names, EMB1, EMB2
示例#3
0
FLAGS = tf.app.flags.FLAGS

tf.app.flags.DEFINE_string('resnet_model_base_dir', '/opt/storage/models/resnet',
                           """Directory where to write trained models and checkpoints.""")

tf.app.flags.DEFINE_string('resnet_model_name', 'ResNet-',
                           """Model name.""")

layers = 20

img = load_image("data/cat.jpg")

sess = tf.Session()

saver = tf.train.import_meta_graph(meta_fn(layers))
saver.restore(sess, checkpoint_fn(layers))

graph = tf.get_default_graph()
prob_tensor = graph.get_tensor_by_name("prob:0")
images = graph.get_tensor_by_name("images:0")
for op in graph.get_operations():
    print op.name

#init = tf.initialize_all_variables()
#sess.run(init)
print "graph restored"

batch = img.reshape((1, 224, 224, 3))

feed_dict = {images: batch}
示例#4
0
from convert import print_prob, load_image, checkpoint_fn, meta_fn
import tensorflow as tf

layers = 50

img = load_image("data/cat.jpg")

sess = tf.Session()

new_saver = tf.train.import_meta_graph(meta_fn(layers))
new_saver.restore(sess, checkpoint_fn(layers))

graph = tf.get_default_graph()
prob_tensor = graph.get_tensor_by_name("prob:0")
images = graph.get_tensor_by_name("images:0")
for op in graph.get_operations():
    print op.name

#init = tf.initialize_all_variables()
#sess.run(init)
print "graph restored"

batch = img.reshape((1, 224, 224, 3))

feed_dict = {images: batch}

prob = sess.run(prob_tensor, feed_dict=feed_dict)

print_prob(prob[0])
示例#5
0
from convert import print_prob, load_image, checkpoint_fn, meta_fn
import tensorflow as tf

root = "/scratch/lyc/ai_challenger_scene"

layers = 50

img = load_image(os.path.join(root, "data/cat.jpg"))

sess = tf.Session()

model_dir = os.path.join(root, "model")
new_saver = tf.train.import_meta_graph(os.path.join(model_dir,
                                                    meta_fn(layers)))
new_saver.restore(sess, os.path.join(model_dir, checkpoint_fn(layers)))

graph = tf.get_default_graph()
prob_tensor = graph.get_tensor_by_name("prob:0")
images = graph.get_tensor_by_name("images:0")
sub_tensor = graph.get_tensor_by_name("sub:0")

file_path = os.path.join(root, "graph_op_in_forward.txt")
print("writing graph op to file {}".format(file_path))
f = open(file_path, "w+")
for op in graph.get_operations():
    f.write(op.name + "\n")
f.close()

file_path = os.path.join(root, "graph_variables_forward.txt")
print("writing graph variables to file {}".format(file_path))
示例#6
0
import sys
sys.path.insert(0, '/pkgs/tensorflow-gpu-0.9.0')
from convert import print_prob, load_image, checkpoint_fn, meta_fn
import tensorflow as tf

NLAYERS = 152


sess = tf.Session()

new_saver = tf.train.import_meta_graph(meta_fn(NLAYERS))
new_saver.restore(sess, checkpoint_fn(NLAYERS))

graph = tf.get_default_graph()
prob_tensor = graph.get_tensor_by_name("prob:0")
images = graph.get_tensor_by_name("images:0")
# for op in graph.get_operations():
#     print op.name

#init = tf.initialize_all_variables()
# sess.run(init)
print "graph restored"


def load_image(fname):
    import cv2
    image = cv2.imread(fname).astype('float32') / 255
    short_edge = min(image.shape[:2])
    yy = int((image.shape[0] - short_edge) / 2)
    xx = int((image.shape[1] - short_edge) / 2)
    image = image[yy:yy + short_edge, xx:xx + short_edge]
示例#7
0
import sys
sys.path.insert(0, '/pkgs/tensorflow-gpu-0.9.0')
from convert import print_prob, load_image, checkpoint_fn, meta_fn
import tensorflow as tf

NLAYERS = 152

sess = tf.Session()

new_saver = tf.train.import_meta_graph(meta_fn(NLAYERS))
new_saver.restore(sess, checkpoint_fn(NLAYERS))

graph = tf.get_default_graph()
# for op in graph.get_operations():
#     print op.name

print 'graph restored'


def load_image(fname):
    import cv2
    image = cv2.imread(fname).astype('float32') / 255
    short_edge = min(image.shape[:2])
    yy = int((image.shape[0] - short_edge) / 2)
    xx = int((image.shape[1] - short_edge) / 2)
    image = image[yy:yy + short_edge, xx:xx + short_edge]
    image = cv2.resize(image, (224, 224))
    image = image[:, :, [2, 1, 0]]
    return image