Esempio n. 1
0
def model(x, H, reuse, is_training=True):
    if H['inception'] == 1:
        with slim.arg_scope(inception_v1.inception_v1_arg_scope()):
            _, T = inception_v1.inception_v1(
                x,
                is_training=is_training,
                num_classes=1001,
                dropout_keep_prob=H['dense_dropout'],
                input_dropout=H['input_dropout'],
                spatial_squeeze=False,
                reuse=reuse)
        coarse_feat = T[H['coarse_feat']]

        # fine feat can be used to reinspect input
        early_feat = T[H['early_feat']]
        early_feat_channels = 480
    elif H['inception'] == 3:
        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            _, T = inception_v3.inception_v3(x,
                                             is_training=is_training,
                                             num_classes=1001,
                                             dropout_keep_prob=0.8,
                                             spatial_squeeze=False,
                                             reuse=reuse)
        coarse_feat = T['Mixed_5b']

        # fine feat can be used to reinspect input
        attention_lname = H.get('attention_lname', 'Mixed_3b')
        early_feat = T[attention_lname]
        early_feat_channels = 480

    return coarse_feat, early_feat, early_feat_channels
Esempio n. 2
0
    def testTrainEvalWithReuse(self):
        train_batch_size = 5
        eval_batch_size = 2
        height, width = 224, 224
        num_classes = 1000

        train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
        inception_v1.inception_v1(train_inputs, num_classes)
        eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
        logits, _ = inception_v1.inception_v1(eval_inputs,
                                              num_classes,
                                              reuse=True)
        predictions = tf.argmax(logits, 1)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            output = sess.run(predictions)
            self.assertEquals(output.shape, (eval_batch_size, ))
Esempio n. 3
0
    def testLogitsNotSqueezed(self):
        num_classes = 25
        images = tf.random_uniform([1, 224, 224, 3])
        logits, _ = inception_v1.inception_v1(images,
                                              num_classes=num_classes,
                                              spatial_squeeze=False)

        with self.test_session() as sess:
            tf.global_variables_initializer().run()
            logits_out = sess.run(logits)
            self.assertListEqual(list(logits_out.shape),
                                 [1, 1, 1, num_classes])
Esempio n. 4
0
    def testBuildPreLogitsNetwork(self):
        batch_size = 5
        height, width = 224, 224
        num_classes = None

        inputs = tf.random_uniform((batch_size, height, width, 3))
        net, end_points = inception_v1.inception_v1(inputs, num_classes)
        self.assertTrue(net.op.name.startswith('InceptionV1/Logits/AvgPool'))
        self.assertListEqual(net.get_shape().as_list(),
                             [batch_size, 1, 1, 1024])
        self.assertFalse('Logits' in end_points)
        self.assertFalse('Predictions' in end_points)
Esempio n. 5
0
    def testBuildClassificationNetwork(self):
        batch_size = 5
        height, width = 224, 224
        num_classes = 1000

        inputs = tf.random_uniform((batch_size, height, width, 3))
        logits, end_points = inception.inception_v1(inputs, num_classes)
        self.assertTrue(logits.op.name.startswith('InceptionV1/Logits'))
        self.assertListEqual(logits.get_shape().as_list(),
                             [batch_size, num_classes])
        self.assertTrue('Predictions' in end_points)
        self.assertListEqual(end_points['Predictions'].get_shape().as_list(),
                             [batch_size, num_classes])
Esempio n. 6
0
    def testUnknowBatchSize(self):
        batch_size = 1
        height, width = 224, 224
        num_classes = 1000

        inputs = tf.placeholder(tf.float32, (None, height, width, 3))
        logits, _ = inception_v1.inception_v1(inputs, num_classes)
        self.assertTrue(logits.op.name.startswith('InceptionV1/Logits'))
        self.assertListEqual(logits.get_shape().as_list(), [None, num_classes])
        images = tf.random_uniform((batch_size, height, width, 3))

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            output = sess.run(logits, {inputs: images.eval()})
            self.assertEquals(output.shape, (batch_size, num_classes))
Esempio n. 7
0
def model(x, H, reuse, is_training=True):
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        _, T = inception.inception_v1(x,
                                      is_training=is_training,
                                      num_classes=1001,
                                      spatial_squeeze=False,
                                      reuse=reuse)
    coarse_feat = T['Mixed_5b']

    # fine feat can be used to reinspect input
    attention_lname = H.get('attention_lname', 'Mixed_3b')
    early_feat = T[attention_lname]
    early_feat_channels = 480

    return coarse_feat, early_feat, early_feat_channels
Esempio n. 8
0
    def testEvaluation(self):
        batch_size = 2
        height, width = 224, 224
        num_classes = 1000

        eval_inputs = tf.random_uniform((batch_size, height, width, 3))
        logits, _ = inception_v1.inception_v1(eval_inputs,
                                              num_classes,
                                              is_training=False)
        predictions = tf.argmax(logits, 1)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            output = sess.run(predictions)
            self.assertEquals(output.shape, (batch_size, ))
Esempio n. 9
0
def model(x, H, reuse, is_training=True):
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        _, T = inception.inception_v1(x,
                                      is_training=is_training,
                                      num_classes=1001,
                                      spatial_squeeze=False,
                                      reuse=reuse)
    coarse_feat = T['Mixed_5b']

    # fine feat can be used to reinspect input
    attention_lname = H.get('attention_lname', 'Mixed_3b')
    early_feat = T[attention_lname]
    early_feat_channels = 480

    return coarse_feat, early_feat, early_feat_channels
Esempio n. 10
0
 def testUnknownImageShape(self):
     tf.reset_default_graph()
     batch_size = 2
     height, width = 224, 224
     num_classes = 1000
     input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
     with self.test_session() as sess:
         inputs = tf.placeholder(tf.float32,
                                 shape=(batch_size, None, None, 3))
         logits, end_points = inception_v1.inception_v1(inputs, num_classes)
         self.assertTrue(logits.op.name.startswith('InceptionV1/Logits'))
         self.assertListEqual(logits.get_shape().as_list(),
                              [batch_size, num_classes])
         pre_pool = end_points['Mixed_5c']
         feed_dict = {inputs: input_np}
         tf.global_variables_initializer().run()
         pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
         self.assertListEqual(list(pre_pool_out.shape),
                              [batch_size, 7, 7, 1024])
def model(x, H, reuse, is_training=True):
    if H['slim_basename'] == 'resnet_v1_101':
        with slim.arg_scope(resnet.resnet_arg_scope()):
            _, T = resnet.resnet_v1_101(x,
                                        is_training=is_training,
                                        num_classes=1000,
                                        reuse=reuse)
    elif H['slim_basename'] == 'InceptionV1':
        with slim.arg_scope(inception.inception_v1_arg_scope()):
            _, T = inception.inception_v1(x,
                                          is_training=is_training,
                                          num_classes=1001,
                                          spatial_squeeze=False,
                                          reuse=reuse)
    #print '\n'.join(map(str, [(k, v.op.outputs[0].get_shape()) for k, v in T.iteritems()]))

    coarse_feat = T[H['slim_top_lname']][:, :, :, :H['later_feat_channels']]
    assert coarse_feat.op.outputs[0].get_shape()[3] == H['later_feat_channels']

    # fine feat can be used to reinspect input
    attention_lname = H.get('slim_attention_lname', 'Mixed_3b')
    early_feat = T[attention_lname]

    return coarse_feat, early_feat
Esempio n. 12
0
num_training_sample = img_data.shape[0]
num_training_category = np.unique(class_id).shape[0]
num_valid_category = np.unique(valid_class_id).shape[0]

print('constructing model')

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)

img_place_holder = tf.placeholder(tf.float32, [None, default_image_size, default_image_size, 3])
label_place_holder = tf.placeholder("float", [None, num_training_category])
#google_net_model= GoogleNet_Model.GoogleNet_Model()
with slim.arg_scope(inception_v1.inception_v1_arg_scope()):
    net_output = inception_v1.inception_v1(img_place_holder)
    test_net_output = inception_v1.inception_v1(img_place_holder,\
            reuse = True, is_training = False) 

saver = tf.train.Saver()

with tf.variable_scope('retrieval'):
    retrieval_layer = layers.retrieval_layer(1024, embedding_dim, num_training_category)

out_layer, bottleneck = retrieval_layer.get_output(net_output,\
        alpha = args.alpha, no_norm = args.no_norm, l2_loss = args.l2_loss)
test_out_layer, test_bottleneck = retrieval_layer.get_output(test_net_output,\
        alpha = args.alpha, no_norm = args.no_norm, l2_loss = args.l2_loss)

prediction = tf.nn.softmax(out_layer)
Esempio n. 13
0
import numpy as np
from PIL import Image
import io
import os

app = Flask(__name__)
with open('real-label.json', 'r') as fopen:
    labels = json.load(fopen)
sess = tf.InteractiveSession()
X = tf.placeholder(tf.float32, [None, None, 3])
image = X / 128.0 - 1
image = tf.expand_dims(image, 0)
image = tf.image.resize_images(image, (224, 224))
with slim.arg_scope(inception_v1.inception_v1_arg_scope()):
    logits, endpoints = inception_v1.inception_v1(image,
                                                  num_classes=1001,
                                                  is_training=False)
sess.run(tf.global_variables_initializer())
var_lists = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                              scope='InceptionV1')
saver = tf.train.Saver(var_list=var_lists)
saver.restore(sess, 'inception_v1.ckpt')


@app.route('/inception', methods=['POST'])
def inception():
    img = np.array(Image.open(io.BytesIO(request.files['file'].read())))
    return json.dumps({
        'label':
        labels[str(np.argmax(sess.run(logits, feed_dict={X: img})[0]))]
    })
Esempio n. 14
0
import inception_v1
import input_data
import tensorflow.contrib.slim as slim
import tensorflow as tf 


x = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224, 3])
keep_prob = tf.placeholder(dtype=tf.float32)
logits = inception_v1.inception_v1(x, keep_prob, 5)
logits = tf.reshape(logits, [-1, 5])

exclusions = ['InceptionV1/Logits']
inception_except_logits = slim.get_variables_to_restore(exclude=exclusions)
CKPT_FILE = 'inception_v1.ckpt'
init_fn = slim.assign_from_checkpoint_fn(
	CKPT_FILE,
	inception_except_logits, ignore_missing_vars=True)


y = tf.nn.softmax(logits)
y_ = tf.placeholder(dtype=tf.float32,shape=[None, 5])
output_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='InceptionV1/Logits')
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy, var_list=output_vars)

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

flower_photos = input_data.read_data_sets('flower_photos/')

with tf.Session() as sess:
Esempio n. 15
0
                                           tf.FixedLenFeature([], tf.string)
                                       })
    img = tf.decode_raw(features["image"], tf.uint8)
    img = tf.reshape(img, [image_pixels, image_pixels, 3])
    img = tf.cast(img, tf.float32)
    label = tf.cast(features["label"], tf.int32)
    return img, label


images = tf.placeholder(tf.float32, [None, image_pixels, image_pixels, 3],
                        name="input/x_input")
labels = tf.placeholder(tf.int64, [None], name="input/y_input")

with slim.arg_scope(inception_v1_arg_scope()):
    logits, end_points = inception_v1(images,
                                      num_classes=classes,
                                      is_training=False)

correct_prediction = tf.equal(labels,
                              tf.argmax(end_points['Predictions'], 1),
                              name="correct_prediction")
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32),
                          name="accuracy")

with tf.Session() as sess:
    ckpt = tf.train.get_checkpoint_state("ckpt")
    if ckpt:
        print(ckpt.model_checkpoint_path)
        tf.train.Saver().restore(sess, ckpt.model_checkpoint_path)
    else:
        raise ValueError('The ckpt file is None.')
Esempio n. 16
0
    image_reader = tf.WholeFileReader()
    _, image_file = image_reader.read(filename_queue)

    print("Image File Read")

    image = tf.image.decode_jpeg(image_file, channels=3)
    processed_image = inception_preprocessing.preprocess_image(
        image, image_size, image_size, is_training=False)
    processed_images = tf.expand_dims(processed_image, 0)

    print("Images Processed")

    # Create the model, use the default arg scope to configure the batch norm parameters.
    with slim.arg_scope(inception_v1.inception_v1_arg_scope()):
        logits, endpoints = inception_v1.inception_v1(processed_images,
                                                      num_classes=1001,
                                                      is_training=False)
    probabilities = tf.nn.softmax(logits)

    print("Neural Network Built")

    init_fn = slim.assign_from_checkpoint_fn(
        os.path.join(checkpoints_dir, 'inception_v1.ckpt'),
        slim.get_model_variables('InceptionV1'))

    print("Checkpoint Loaded")
    with tf.Session() as sess:
        # Required to get the filename matching to run.
        tf.global_variables_initializer().run()
        tf.local_variables_initializer().run()
        coord = tf.train.Coordinator()
    num_samples=NUM_IMAGES,
    val_fraction=0.05,
    test_fraction=0.05)
num_classes = len(classes)  # should be 1000
print num_classes

with tf.device('/gpu:1'):
    x = tf.placeholder(
        tf.float32,
        shape=[None, input_data.IMAGE_WIDTH * input_data.IMAGE_HEIGHT, 3])
    y_ = tf.placeholder(tf.float32, shape=[None, num_classes])
    keep_prob = tf.placeholder(tf.float32)
    x_reshaped = tf.reshape(
        x, [-1, input_data.IMAGE_WIDTH, input_data.IMAGE_HEIGHT, 3])

    y_logit = inception_v1(x_reshaped, 1000)
    print("y_prediction's shape is:")
    print tf.shape(y_logit)

    print("label y's shape is")
    print tf.shape(y_)

    # # Training
    cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(logits=y_logit, labels=y_))
    train_step = tf.train.AdamOptimizer(0.01).minimize(cross_entropy)
    print tf.shape(tf.argmax(y_logit, 1))
    print tf.shape(tf.argmax(y_, 1))
    correct_prediction = tf.equal(tf.argmax(y_logit, 1), tf.argmax(y_, 1))

    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))