示例#1
0
def resnet_v2_50(init_weights):
    tf_compat.reset_default_graph()
    image_size = 224
    inputs = tf_compat.random_normal([1, image_size, image_size, 3])
    with slim.arg_scope(resnet_v2.resnet_arg_scope()):
        logits, _ = resnet_v2.resnet_v2_50(inputs, 1000, is_training=False)
        return tf_compat.get_default_graph()
示例#2
0
def resnet_50(inputs, is_training, num_classes):
    logits, _ = resnet_v2.resnet_v2_50(inputs,
                                       num_classes=num_classes,
                                       is_training=is_training)
    logits = tf.squeeze(logits,
                        [1, 2])  # resnet output is (N,1,1,C, remove the
    return tf.identity(logits, name='logits')
示例#3
0
    def _ConvNet2D(self, x, is_training, reuse=False):

        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            f, _ = resnet_v2.resnet_v2_50(x,
                                          num_classes=None,
                                          is_training=is_training,
                                          global_pool=False,
                                          reuse=reuse)
            print("resnet.out.shape: %s" % f.get_shape())
            with tf.variable_scope("ConvNet2D", reuse=reuse):
                f = tf.reduce_mean(f, [1, 2],
                                   name='global_avg_pooling',
                                   keep_dims=True)
                z = slim.conv2d(f,
                                4096, [1, 1],
                                padding='VALID',
                                normalizer_fn=None,
                                scope='f2zfeture')
                z = slim.conv2d(z,
                                self.z_dim, [1, 1],
                                padding='VALID',
                                normalizer_fn=None,
                                scope='z_2d')

                #g_feature = tf.squeeze(g_feature, [1, 2], name='global_spatial_squeeze')

                return tf.expand_dims(z, 1)
示例#4
0
    def load(self, **kwargs):
        session = kwargs["session"]
        assert isinstance(session, tf.Session)

        x_input = tf.placeholder(tf.float32, shape=(None, ) + self.x_shape)
        with tf.contrib.framework.arg_scope(resnet_v2.resnet_arg_scope()):
            logits, _ = resnet_v2.resnet_v2_50(x_input,
                                               self.n_class,
                                               is_training=False,
                                               reuse=tf.AUTO_REUSE)

        model_path = get_model_path('alp')
        url = 'http://download.tensorflow.org/models/adversarial_logit_pairing/imagenet64_alp025_2018_06_26.ckpt.tar.gz'
        fname = os.path.join(model_path, url.split('/')[-1])
        if not os.path.exists(fname):
            if not os.path.exists(model_path):
                os.makedirs(model_path)

            from six.moves import urllib
            urllib.request.urlretrieve(url, fname, show_progress)
            import tarfile
            t = tarfile.open(fname)
            t.extractall(model_path)
            print('Extracted model')

        saver = tf.train.Saver()
        saver.restore(session, fname.split('.tar.gz')[0])
示例#5
0
def featureExtractor(input,
                     feature_norm,
                     model='101',
                     reuse=False,
                     scope='resnet_v2_101'):
    with tf.variable_scope(scope, reuse=reuse) as scope:
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            input = scale_RGB(input)
            if '50' in model:
                net, end_points = resnet_v2.resnet_v2_50(input,
                                                         global_pool=True,
                                                         is_training=False,
                                                         reuse=reuse)
            elif '101' in model:
                _, end_points = resnet_v2.resnet_v2_101(input,
                                                        global_pool=True,
                                                        is_training=False,
                                                        reuse=reuse)
                #f = end_points['stabNet/pathFinder/featureExtractor/resnet_v2_101/block3/unit_23/bottleneck_v2/conv1'] #(18 X 32) X (18 X 32)
                f = end_points[
                    '{}/resnet_v2_101/block4/unit_2/bottleneck_v2/conv1'.
                    format(scope.name)]  #(9 X 16) X (9 X 16)
        if feature_norm:
            f = featureL2Norm(f)

    return f
示例#6
0
def main(_):
    with tf.Graph().as_default():
        url = 'https://upload.wikimedia.org/wikipedia/commons/5/5c/Tigershark3.jpg'
        image_string = urllib.urlopen(url).read()
        image = tf.image.decode_jpeg(image_string, channels=3)
        processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False)
        processed_images = tf.expand_dims(processed_image, 0)

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

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

        with tf.Session() as sess:
            init_fn(sess)
            np_image, probabilities = sess.run([image, probabilities])
            probabilities = np.reshape(probabilities, [1001])
            print(probabilities.shape)
            sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1])]
            print(sorted_inds)
        plt.figure()
        plt.imshow(np_image.astype(np.uint8))
        plt.axis('off')
        plt.show()

        names = imagenet.create_readable_names_for_imagenet_labels()

        for i in range(5):
            index = sorted_inds[i]
            # Shift the index of a class name by one.
            print('Probability %0.6f%% => [%s]' % (probabilities[index] * 100, names[index + 1]))
def PSPNet(inputs, num_classes, is_training, reuse=None):
    '''A TensorFlow implementation of PSPNet model based on 
	   https://github.com/hszhao/PSPNet/tree/master/evaluation/prototxt	   	
	
	Args:
		inputs: A 4-D tensor with dimensions [batch_size, height, width, channels]
		num_classes: Integer, the total number of categories in the dataset
		is_training : Bool, whether to updates the running means and variances during the training.
	Returns:
		A feature map with dimensions [batch_size, 1/8*height, 1/8*width, 512]

	'''
    with slim.arg_scope(resnet_utils.resnet_arg_scope()):
        net, end_points = resnet_v2.resnet_v2_50(inputs,
                                                 num_classes=None,
                                                 is_training=is_training,
                                                 global_pool=False,
                                                 output_stride=8,
                                                 reuse=reuse,
                                                 scope='resnet_v2_50')
    with tf.variable_scope("PSPNet", reuse=reuse) as sc:
        shape = tf.shape(net)[1:3]
        net = PyramidPoolingModule(net, shape=shape)
        net = slim.conv2d(net, 512, [3, 3], activation_fn=None, scope='conv5')
        net = slim.batch_norm(net, fused=True, scope='conv5_bn')
        net = tf.nn.relu(net, name='conv5_bn_relu')
    return net, end_points
def get_resnet(x_tensor, reuse, is_training, x_batch_size):
    with tf.variable_scope('resnet', reuse=reuse):
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            resnet, end_points = resnet_v2.resnet_v2_50(
                x_tensor,
                global_pool=False,
                is_training=is_training,
                reuse=reuse,
                output_stride=32)
        global_pool = tf.reduce_mean(resnet, [1, 2])
        with tf.variable_scope('fc'):
            global_pool = slim.fully_connected(global_pool,
                                               2048,
                                               scope='fc/fc_1')
            global_pool = slim.fully_connected(global_pool,
                                               1024,
                                               scope='fc/fc_2')
            global_pool = slim.fully_connected(global_pool,
                                               512,
                                               scope='fc/fc_3')
            theta = output_layer(global_pool, (grid_h + 1) * (grid_w + 1) * 2)

        with tf.name_scope('gen_theta'):
            id2_loss = tf.reduce_mean(tf.abs(theta)) * id_mul
    return theta, id2_loss, id2_loss
示例#9
0
文件: yolo_net.py 项目: nowgood/yolo
def yolonet(images, is_training=True):
    with tf.Graph().as_default():
        tf.logging.set_verbosity(tf.logging.INFO)
        output_depth = NUM_CLASS + 5 * BOX_PER_CELL
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            bottleneck, _ = resnet_v2.resnet_v2_50(images,
                                                   global_pool=False,
                                                   is_training=is_training)
            with arg_scope([layers.batch_norm], is_training=is_training):
                net = bottleneck
                net = layers_lib.conv2d(net,
                                        512, [1, 1],
                                        normalizer_fn=layers.batch_norm,
                                        scope='yolo_layer1')
                net = layers_lib.conv2d(net,
                                        512, [3, 3],
                                        normalizer_fn=layers.batch_norm,
                                        scope='yolo_layer2')
                net = layers_lib.conv2d(net,
                                        512, [3, 3],
                                        normalizer_fn=layers.batch_norm,
                                        scope='yolo_layer3')
                net = layers_lib.conv2d(net,
                                        output_depth, [1, 1],
                                        activation_fn=None,
                                        normalizer_fn=None,
                                        scope='yolo_output')

    return net
示例#10
0
 def resnet_inference(inputs, is_train=True):
     x = tf.reshape(inputs, [FLAGS.batch_size, 64, 64, 1])
     with slim.arg_scope(resnet_arg_scope(is_training=(MODE == "train"))):
         net, enpoints = resnet_v2.resnet_v2_50(
             x, num_classes=FLAGS.label_size)
     net = tf.reshape(net, [FLAGS.batch_size, FLAGS.label_size])
     return net
示例#11
0
def main(_):
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    num_classes = 1001
    itr = 30
    a = FLAGS.input_dir
    tf.logging.set_verbosity(tf.logging.INFO)

    with tf.Graph().as_default():
        # Prepare graph
        x_input = tf.placeholder(tf.float32, shape=batch_shape)
        img_resize_tensor = tf.placeholder(tf.int32, [2])
        x_input_resize = tf.image.resize_images(
            x_input,
            img_resize_tensor,
            method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)

        shape_tensor = tf.placeholder(tf.int32, [3])
        padded_input = padding_layer_iyswim(x_input_resize, shape_tensor)
        # 330 is the last value to keep 8*8 output, 362 is the last value to keep 9*9 output, stride = 32
        padded_input.set_shape(
            (FLAGS.batch_size, FLAGS.image_resize, FLAGS.image_resize, 3))
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            net, end_points = resnet_v2.resnet_v2_50(x_input,
                                                     num_classes=num_classes,
                                                     is_training=False)
        predicted_labels = tf.argmax(end_points['predictions'], 3)
        # Run computation
        saver = tf.train.Saver(slim.get_model_variables())
        session_creator = tf.train.ChiefSessionCreator(
            scaffold=tf.train.Scaffold(saver=saver),
            checkpoint_filename_with_path=FLAGS.checkpoint_path,
            master=FLAGS.master)

        with tf.train.MonitoredSession(
                session_creator=session_creator) as sess:
            with tf.gfile.Open(FLAGS.output_file, 'w') as out_file:
                for filenames, images in load_images(FLAGS.input_dir,
                                                     batch_shape):
                    if np.random.randint(0, 2, size=1) == 1:
                        images = images[:, :, ::-1, :]
                    resize_shape_ = np.random.randint(310, 331)
                    labels = sess.run(
                        predicted_labels,
                        feed_dict={
                            x_input:
                            images,
                            img_resize_tensor: [resize_shape_] * 2,
                            shape_tensor:
                            np.array([
                                random.randint(
                                    0, FLAGS.image_resize - resize_shape_),
                                random.randint(
                                    0, FLAGS.image_resize - resize_shape_),
                                FLAGS.image_resize
                            ])
                        })
                    labels = labels.flatten()
                    for filename, label in zip(filenames, labels):
                        out_file.write('{0},{1}\n'.format(filename, label))
示例#12
0
    def built_network(self, inputs, is_training, dropout_rate):

        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            net, end_points = resnet_v2.resnet_v2_50(inputs,
                                                     self.opt.num_classes,
                                                     is_training=is_training)
        net = tf.squeeze(net, axis=[1, 2])
        return net
示例#13
0
    def built_network(self, inputs1, inputs2, is_training=False):
        inputs = tf.concat([inputs1, inputs2], axis=0)
        with tf.variable_scope("Seg"):
            with tf.contrib.slim.arg_scope(
                    resnet_v2.resnet_arg_scope(
                        batch_norm_decay=_BATCH_NORM_DECAY)):
                logits, end_points = resnet_v2.resnet_v2_50(
                    inputs, is_training=is_training)

        return end_points
示例#14
0
 def build(self):
     with slim.arg_scope(resnet_v2.resnet_arg_scope()):
         logits, self.endpoints = resnet_v2.resnet_v2_50(
             self.inputs['images'],
             num_classes=self.num_classes,
             is_training=self.is_training)
     self.outputs['logits'] = tf.reshape(logits, [-1, self.num_classes])
     self.outputs['argmax'] = tf.argmax(self.outputs['logits'],
                                        axis=1,
                                        name='output/predict')
    def __init__(self, inputs, true_labels, is_train=False, num_classes=None):

        self.true_labels = true_labels
        self.NUM_CLASSES = num_classes
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            self.output, self.features = resnet_v2.resnet_v2_50(
                inputs=inputs, num_classes=None, is_training=False)
            self.classifier, _ = cnn.fc(input=self.forward_pass(),
                                        num_outputs=self.NUM_CLASSES,
                                        use_relu=False,
                                        name='classifier')
示例#16
0
def _resnet50(input_img, training=True, embedding_size=64, dropout_keep_prob=0.5, middleRepr=False):
    with tf.variable_scope('resnet', reuse=tf.AUTO_REUSE):
        logits, _ = resnet_v2.resnet_v2_50(input_img, is_training=training) # ?x1x1x2048
        
        if middleRepr:
            return tf.squeeze(logits, [1,2])

        logits = conv2d(logits, embedding_size, [1, 1], activation_fn=None,
                        normalizer_fn=None, scope='logits')
        logits = tf.squeeze(logits, [1, 2])
        return logits
 def __call__(self, x_input):
     """Constructs model and return probabilities for given input."""
     reuse = True if self.built else None
     with slim.arg_scope(resnet_v2.resnet_utils.resnet_arg_scope()):
         _, end_points = resnet_v2.resnet_v2_50(
             x_input, num_classes=self.num_classes, reuse=reuse)
     self.built = True
     output = end_points['Predictions']
     # Strip off the extra reshape op at the output
     probs = output.op.inputs[0]
     return probs
示例#18
0
def cnn_model_fn(features, labels, mode):
    """Model function for CNN."""
    # Input Layer
    # Reshape X to 4-D tensor: [batch_size, width, height, channels]
    # MNIST images are 28x28 pixels, and have one color channel
    # tf.logging.info(features)
    input_layer = tf.reshape(features, [-1, 224, 224, 1])

    net, end_points = resnet_v2.resnet_v2_50(input_layer, 33)

    net = tf.squeeze(net, axis=[1, 2])
    logits = slim.fully_connected(net,
                                  num_outputs=33,
                                  activation_fn=None,
                                  scope='train')

    slim.losses.sparse_softmax_cross_entropy(logits=logits,
                                             labels=labels,
                                             scope='Loss')
    loss = slim.losses.get_total_loss()

    predictions = {
        # Generate predictions (for PREDICT and EVAL mode)
        "classes": tf.argmax(input=logits, axis=1),
        # Add `softmax_tensor` to the graph. It is used for PREDICT and by the
        # `logging_hook`.
        "probabilities": tf.nn.softmax(logits, name="softmax_tensor")
    }

    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

    # Calculate Loss (for both TRAIN and EVAL modes)
    # tf.logging.info(labels)
    # onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=32)

    # Configure the Training Op (for TRAIN mode)
    if mode == tf.estimator.ModeKeys.TRAIN:
        optimizer = tf.train.AdamOptimizer(learning_rate=0.0001)
        train_op = slim.learning.create_train_op(loss,
                                                 optimizer,
                                                 summarize_gradients=True)
        return tf.estimator.EstimatorSpec(mode=mode,
                                          train_op=train_op,
                                          loss=loss)

    # Add evaluation metrics (for EVAL mode)
    eval_metric_ops = {
        "accuracy":
        tf.metrics.accuracy(labels=labels, predictions=predictions["classes"])
    }
    return tf.estimator.EstimatorSpec(mode=mode,
                                      loss=loss,
                                      eval_metric_ops=eval_metric_ops)
示例#19
0
def resnet50(inputs, num_classes, is_training, global_pool = True):
  with slim.arg_scope(resnet_v2.resnet_arg_scope()):
    logits, end_points = resnet_v2.resnet_v2_50(inputs,
                                                num_classes,
                                                global_pool = global_pool,
                                                reuse=tf.AUTO_REUSE)
    predictions = {
      "classes": tf.argmax(logits, axis=1),
      "probabilities": end_points["predictions"]
    }

    return logits, predictions
示例#20
0
def resnet_layers(input, output_stride=8, training=True):

    with slim.arg_scope(resnet_v2.resnet_arg_scope()):
        _, endpoint = resnet_v2.resnet_v2_50(inputs=input,
                                             output_stride=output_stride,
                                             is_training=training)

        conv_feat = tf.squeeze(endpoint['resnet_v2_50/block4'],
                               axis=0,
                               name='conv_feat')

        return conv_feat
示例#21
0
    def logits_and_labels(self, xs_ph):

        xs_ph = xs_ph * 2.0 - 1.0
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            logits, _ = resnet_v2.resnet_v2_50(xs_ph,
                                               self.n_class,
                                               is_training=False,
                                               reuse=tf.AUTO_REUSE)
        logits = tf.squeeze(logits, [1, 2])
        predicts = tf.nn.softmax(logits)
        predicted_labels = tf.argmax(predicts, 1)
        return logits, predicted_labels
示例#22
0
    def __init__(self,
                    sess,
                    inputs,
                    labels,
                    stage_of_development,
                    model_path,
                    num_of_classes,
                    min_bins=None,
                    max_bins=None): 
        #self.resnet = tf.contrib.slim
        self.batch_inputs = tf.reshape(tf.cast(inputs, tf.float32), [-1, 224, 224, 3])
        self.stage_of_development = stage_of_development
        self.model_path = model_path
        self.labels = tf.reshape(tf.cast(labels, tf.float32), [-1, 1])
        self.num_of_classes = num_of_classes

        print(stage_of_development=="training")
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=0.0001)):
            self.logits, _ = resnet_v2.resnet_v2_50(self.batch_inputs, num_classes=self.num_of_classes, is_training=stage_of_development=="training", scope='resnet_v2_50')

        # Specify where the model checkpoint is (pretrained weights).
        self.model_path = model_path

        # Restore only the layers up to "logits" (included)
        # Calling function `init_fn(sess)` will load all the pretrained weights.
        self.variables_to_restore = tf.contrib.framework.get_variables_to_restore(exclude=['resnet_v2_50/logits'])
        print("ResNet 50 - VARIABLES TO RESTORE", self.variables_to_restore)
        self.init_fn = tf.contrib.framework.assign_from_checkpoint_fn(self.model_path, self.variables_to_restore)

        # Initialization operation from scratch for the new "logits" layers
        # `get_variables` will only return the variables whose name starts with the given pattern
        self.logits_variables = tf.contrib.framework.get_variables('resnet_v2_50/logits')
        self.logits_init = tf.variables_initializer(self.logits_variables)

        # ---------------------------------------------------------------------
        # Using tf.losses, any loss is added to the tf.GraphKeys.LOSSES collection
        # We can then call the total loss easily

        self.logits = tf.reshape(tf.nn.relu(self.logits), [-1, num_of_classes])
        self.validation_logits = tf.reshape(tf.nn.relu(self.logits), [-1, num_of_classes])

        if self.num_of_classes == 1:
            self.predictions = self.logits
            self.validation_predictions = self.validation_logits
        else:
            self.predictions = tf.reshape(tf.cast(tf.argmax(self.logits, 1), tf.float32), [-1, 1])
            self.validation_predictions = tf.reshape(tf.cast(tf.argmax(self.validation_predictions, 1), tf.float32), [-1, 1])

        self.all_variables = tf.trainable_variables()
        self.variables_trained_from_scratch = self.logits_variables 
        print("ResNet - ALL VARIABLES: ", self.all_variables)
        print("ResNet - VARIABLES TRAINED FROM SCRATCH: ", self.variables_trained_from_scratch)
示例#23
0
def main(_):

    # download and conver flower_photos dataset to tfrecord
    download_dataset.maybe_download_and_extract(FLOWERS_DATA_DIR, _DATA_URL)
    convert_flowers_to_tfrecord.run(FLOWERS_DATA_DIR)

    with tf.Graph().as_default() as g:
        tf.logging.set_verbosity(tf.logging.INFO)
        dataset = flowers.get_split('train', FLOWERS_DATA_DIR)
        images, _, labels = load_batch(dataset,
                                       batch_size=BATCH_SIZE,
                                       is_training=True)

        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            logits, _ = resnet_v2.resnet_v2_50(images,
                                               num_classes=dataset.num_classes,
                                               is_training=True)
            logits = tf.squeeze(tf.convert_to_tensor(logits, tf.float32))

        one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes)
        loss = tf.losses.softmax_cross_entropy(onehot_labels=one_hot_labels,
                                               logits=logits)
        slim.losses.add_loss(loss)
        total_loss = slim.losses.get_total_loss()
        tf.summary.scalar('losses/total_loss', total_loss)

        # 梯度根性只更新最后一层
        var2training = slim.get_trainable_variables(
            scope="resnet_v2_50/logits")
        optimizer = tf.train.AdamOptimizer()
        train_op = slim.learning.create_train_op(
            total_loss, optimizer, variables_to_train=var2training)

        init_fn = get_init_fn(
            checkpoint_exclude_scopes=CHECKPOINT_EXCLUDE_SCOPES,
            checkpoint_dir=PRETRAIN_DIR)

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

        print("start training")
        final_loss = slim.learning.train(train_op,
                                         logdir=TRAIN_DIR,
                                         init_fn=init_fn,
                                         number_of_steps=NUMBER_OF_STEPS,
                                         trace_every_n_steps=500,
                                         log_every_n_steps=50,
                                         session_config=config,
                                         save_interval_secs=60)

        print('Finished training. Last batch loss %f' % final_loss)
示例#24
0
    def convolutional_model(self, num_images, images, img_indices, training):
        with tf.variable_scope('cnn/resnet', reuse=tf.AUTO_REUSE):
            bs = tf.shape(images)[0]
            imgsize = [224, 224]
            # Flatten and cast to float
            images = tf.reshape(
                images, [-1, imgsize[0], imgsize[1], 1
                         ])  # num images x img_height x img_width x channels
            images = tf.cast(images, dtype=tf.float32)
            # Run through ResNet
            logits, _ = resnet_v2.resnet_v2_50(
                images, is_training=training)  # ?x1x1x2048
            logits = conv2d(inputs=logits,
                            num_outputs=self.FLAGS.cnn_size,
                            kernel_size=[1, 1],
                            activation_fn=None,
                            normalizer_fn=None,
                            scope='logits')
            if not self.FLAGS.finetuning_cnn:
                logits = tf.stop_gradient(logits)
            logits = tf.reshape(logits, [bs, -1, self.FLAGS.cnn_size])
            # Add first 0 logits, not yet an image available
            logits_0 = tf.concat([tf.zeros_like(logits[:, :1]), logits],
                                 axis=1)

            # propagate logits to the corresponding input (input el has last used snapshot img logits)
            # bs x num_snapshots x logit_size ---> bs x max_seqlen x logit_size
            def _propagate(logits, indices):
                bs = tf.shape(logits)[0]
                # prepare indices to get the correct logits
                bs_ind = tf.range(bs)
                bs_ind = tf.tile(bs_ind[:, None], [1, self.max_seqlen])
                tog_ind = tf.stack([bs_ind, indices], axis=2)
                tog_ind = tf.reshape(tog_ind, (-1, 2))
                # gather imgs from indices
                out = tf.gather_nd(logits, tog_ind)
                return tf.reshape(out,
                                  (bs, self.max_seqlen, self.FLAGS.cnn_size))

            output_dict = {
                'last_img_logits':
                batch_gather(logits,
                             num_images),  # img logits for finished sketches
                'all_padded_img_logits': logits
            }  # img logits for snapshot images
            if self.max_seqlen:  # at every sketch point, corresponding img logits
                output_dict.update(
                    {'sketch_img_logits': _propagate(logits_0, img_indices)})

            return output_dict
def build_resnet(images_tn, num_classes, is_training):
    with slim.arg_scope(resnet_v2.resnet_arg_scope()):
        resnet_logits, end_points = resnet_v2.resnet_v2_50(
            images_tn, is_training=is_training)

    resnet_init_fn = slim.assign_from_checkpoint_fn(
        checkpoint_file, [var for var in tf.global_variables()])

    with tf.variable_scope('ClassificationPart'):
        resnet_flat = tf.layers.flatten(resnet_logits)
        dense2 = tf.layers.dense(resnet_flat, 1024, activation=tf.nn.relu)
        dropout2 = tf.layers.dropout(dense2, 0.75, training=is_training)
        logits = tf.layers.dense(dropout2, num_classes, activation=tf.nn.relu)
    return resnet_init_fn, logits
示例#26
0
 def net(self):
     #This function is finished.
     #Try to figure out what endpoints are and why they might be useful.
     #This creates your model architecture, a 50 layer resnet.
     net, end_points = resnet_v2.resnet_v2_50(
         self.inputs,
         num_classes=self.num_classes,
         is_training=self.is_train,
         global_pool=True, #check out this global pooling ;) 
         output_stride=None,
         reuse=tf.AUTO_REUSE,
         scope="resnet_v2_50"
     )
     return net, end_points
示例#27
0
def main(_):

    with tf.Graph().as_default() as g:

        tf.logging.set_verbosity(tf.logging.INFO)
        dataset = flowers.get_split(VAL, FLOWERS_DATA_DIR)
        images, images_raw, labels = load_batch(dataset,
                                                batch_size=BATCH_SIZE,
                                                height=IMAGE_SIZE,
                                                width=IMAGE_SIZE,
                                                is_training=False,
                                                shuffle=False)
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            logits, _ = resnet_v2.resnet_v2_50(images,
                                               num_classes=dataset.num_classes,
                                               is_training=False)
            logits = tf.squeeze(tf.convert_to_tensor(logits, tf.float32))

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

        with tf.Session(config=config) as sess:
            with slim.queues.QueueRunners(sess):
                writer = tf.summary.FileWriter(EVAL_DIR, g)
                step = 0
                num_iter = int(np.ceil(NUM_VALIDATION / BATCH_SIZE))
                total_sample_count = num_iter * BATCH_SIZE
                while step < 100:
                    true_count = 0

                    # sess.run(tf.local_variables_initializer())
                    checkpoint_name = tf.train.latest_checkpoint(TRAIN_DIR)
                    init_fn = slim.assign_from_checkpoint_fn(
                        os.path.join(TRAIN_DIR, checkpoint_name),
                        slim.get_model_variables())
                    init_fn(sess)

                    top_k_op = tf.nn.in_top_k(logits, labels, 1)
                    for _ in range(num_iter):
                        predictions = sess.run([top_k_op])
                        true_count += np.sum(predictions)
                    step += 1
                    precision = true_count / total_sample_count
                    tf.summary.scalar("precision", precision)
                    print('%s: accuracy = %.3f' % (datetime.now(), precision))

                    step += 1
                    time.sleep(5)
                writer.close()
def Resnet(inputs,
           scope='Resnet',
           class_number=65,
           is_training=True,
           reuse=False,
           batch_size=128,
           dropout_ratio=1.0):
    #with tf.variable_scope(scope,reuse=reuse):
    #net,end_points=resnet50v1.resnet_v1_50(inputs,is_training=is_training,scope=scope,reuse=reuse)
    with slim.arg_scope(resnet_v2.resnet_arg_scope()):
        net, end_points = resnet_v2.resnet_v2_50(inputs,
                                                 is_training=is_training,
                                                 reuse=reuse)
    net = tf.squeeze(net, [1, 2])
    return net
示例#29
0
def backend_resnet50_v2_slim(x_input):
    BATCH_SIZE, NUM_FRAMES, HEIGHT, WIDTH, NUM_CHANNELS = x_input.get_shape(
    ).as_list()

    # RESNET
    video_input = tf.reshape(
        x_input, (BATCH_SIZE * NUM_FRAMES, HEIGHT, WIDTH, NUM_CHANNELS))

    features, end_points = resnet_v2.resnet_v2_50(video_input, num_classes=512)
    # features, end_points = resnet_v2.resnet_v1_50(video_input, None)
    features = tf.reshape(
        features, (BATCH_SIZE, NUM_FRAMES, int(features.get_shape()[3])))

    print("shape after resnet is %s" % features.get_shape())

    return features
示例#30
0
 def __call__(self, x_input, return_logits=False):
     """Constructs model and return probabilities for given input."""
     reuse = True if self.built else None
     with slim.arg_scope(resnet_v2.resnet_arg_scope()):
       _, end_points = resnet_v2.resnet_v2_50(
           x_input, num_classes=self.num_classes, is_training=False,
           reuse=reuse)
     self.built = True
     self.logits = end_points['resnet_v2_50/logits']
     output = end_points['predictions']
     # Strip off the extra reshape op at the output
     self.probs = output.op.inputs[0]
     if return_logits:
         return self.logits
     else:
         return self.probs
示例#31
0
  def build(self, images):
    """Builds a ResNet50 embedder for the input images.

    It assumes that the range of the pixel values in the images tensor is
      [0,255] and should be castable to tf.uint8.

    Args:
      images: a tensor that contains the input images which has the shape of
          NxTxHxWx3 where N is the batch size, T is the maximum length of the
          sequence, H and W are the height and width of the images and C is the
          number of channels.
    Returns:
      The embedding of the input image with the shape of NxTxL where L is the
        embedding size of the output.

    Raises:
      ValueError: if the shape of the input does not agree with the expected
      shape explained in the Args section.
    """
    shape = images.get_shape().as_list()
    if len(shape) != 5:
      raise ValueError(
          'The tensor shape should have 5 elements, {} is provided'.format(
              len(shape)))
    if shape[4] != 3:
      raise ValueError('Three channels are expected for the input image')

    images = tf.cast(images, tf.uint8)
    images = tf.reshape(images,
                        [shape[0] * shape[1], shape[2], shape[3], shape[4]])
    with slim.arg_scope(resnet_v2.resnet_arg_scope()):

      def preprocess_fn(x):
        x = tf.expand_dims(x, 0)
        x = tf.image.resize_bilinear(x, [299, 299],
                                       align_corners=False)
        return(tf.squeeze(x, [0]))

      images = tf.map_fn(preprocess_fn, images, dtype=tf.float32)

      net, _ = resnet_v2.resnet_v2_50(
          images, is_training=False, global_pool=True)
      output = tf.reshape(net, [shape[0], shape[1], -1])
      return output