当前位置: 首页>>代码示例>>Python>>正文


Python slim.get_variables方法代码示例

本文整理汇总了Python中tensorflow.contrib.slim.get_variables方法的典型用法代码示例。如果您正苦于以下问题:Python slim.get_variables方法的具体用法?Python slim.get_variables怎么用?Python slim.get_variables使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tensorflow.contrib.slim的用法示例。


在下文中一共展示了slim.get_variables方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: add_save_vars

# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_variables [as 别名]
def add_save_vars(self, prefixes):
        """Prepares the list of variables that should be saved based on
        their name prefix.

        Args:
            prefixes: Variable name prefixes to find and save.
        """

        for pre in prefixes:
            pre_vars = slim.get_variables(pre)
            self.save_vars.update(pre_vars)

        var_list = ''
        for var in self.save_vars:
            var_list = var_list + var.name + ' '

        print ('Saving these variables: {}'.format(var_list)) 
开发者ID:kabkabm,项目名称:defensegan,代码行数:19,代码来源:base_model.py

示例2: try_load_weights

# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_variables [as 别名]
def try_load_weights(self):
    fn = None
    if self.load != "":
      fn = self.load.replace(".index", "")
    else:
      files = sorted(glob.glob(self.model_dir + self.model + "-*.index"))
      if len(files) > 0:
        fn = files[-1].replace(".index", "")

    if fn is not None:
      print("loading model from", fn)

      # vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='ReID_net')
      varlist = slim.get_variables()
      # print (np.unique(np.array([var.name.split('/')[0] for var in varlist])))
      good_list = ['conv0','conv1','fc1','fc2','outputTriplet', 'res0', 'res1', 'res10', 'res11','res12', 'res13',
                   'res14', 'res15', 'res16', 'res2', 'res3', 'res4', 'res5','res6', 'res7', 'res8', 'res9']
      varlist = [var for var in varlist if var.name.split('/')[0] in good_list]
      self.saver = tf.train.Saver(pad_step_number=True, var_list=varlist)

      self.saver.restore(self.session, fn)
      if self.model == fn.split("/")[-2]:
        self.start_epoch = int(fn.split("-")[-1])
        print("starting from epoch", self.start_epoch + 1)
    else:
      if self.load_init_savers is None:
        self.load_init_savers = [self._create_load_init_saver(x) for x in self.load_init]
      assert len(self.load_init) == len(self.load_init_savers)
      for fn, load_init_saver in zip(self.load_init, self.load_init_savers):
        if fn.endswith(".pickle"):
          print("trying to initialize model from wider-or-deeper mxnet model", fn)
          load_wider_or_deeper_mxnet_model(fn, self.session)
        else:
          print("initializing model from", fn)
          assert load_init_saver is not None
          load_init_saver.restore(self.session, fn) 
开发者ID:JonathonLuiten,项目名称:PReMVOS,代码行数:38,代码来源:Engine.py

示例3: _initialize_saver

# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_variables [as 别名]
def _initialize_saver(self, prefixes=None, force=False, max_to_keep=5):
        """Initializes the saver object.

        Args:
            prefixes: The prefixes that the saver should take care of.
            force (optional): Even if saver is set, reconstruct the saver
                object.
            max_to_keep (optional):
        """
        if self.saver is not None and not force:
            return
        else:
            if prefixes is None or not (
                type(prefixes) != list or type(prefixes) != tuple):
                raise ValueError(
                    'Prefix of variables that needs saving are not defined')

            prefixes_str = ''
            for pref in prefixes:
                prefixes_str = prefixes_str + pref + ' '

            print('[#] Initializing it with variable prefixes: {}'.format(
                prefixes_str))
            saved_vars = []
            for pref in prefixes:
                saved_vars.extend(slim.get_variables(pref))

            self.saver = tf.train.Saver(saved_vars, max_to_keep=max_to_keep) 
开发者ID:kabkabm,项目名称:defensegan,代码行数:30,代码来源:base_model.py

示例4: _build

# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_variables [as 别名]
def _build(self):
        """Builds the computation graph."""

        assert (self.batch_size % self.rec_rr) == 0, 'Batch size ' \
                                                     'should be ' \
                                                     'divisable by ' \
                                                     'random restart'
        self.test_batch_size = self.batch_size

        # Defining batch_size in input placeholders is inevitable at least
        # for now, because the z vectors are Tensorflow variables.
        self.real_data_pl = tf.placeholder(
            tf.float32, shape=[self.batch_size] + self.image_dim,
        )
        self.real_data_test_pl = tf.placeholder(
            tf.float32, shape=[self.test_batch_size] + self.image_dim,
        )

        self.input_pl_transform()
        self._build_generator_discriminator()

        self.fake_data = self.generator_fn()

        self.disc_real = self.discriminator_fn(self.real_data)

        with tf.variable_scope(tf.get_variable_scope(), reuse=True):
            sc = tf.get_variable_scope()
            sc.reuse_variables()
            self.disc_fake = self.discriminator_fn(self.fake_data)

            self.generator_vars = slim.get_variables('Generator')
            self.discriminator_vars = slim.get_variables('Discriminator')

            self.fixed_noise = tf.constant(
                np.random.normal(size=(128, self.latent_dim)).astype(
                    'float32'))
            self.fixed_noise_samples = self.generator_fn(self.fixed_noise,
                                                         is_training=False) 
开发者ID:kabkabm,项目名称:defensegan,代码行数:40,代码来源:gan.py

示例5: train

# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_variables [as 别名]
def train(image_dir, label_dir):

    # load data & gen data
    all_objs = parse_annotation(label_dir, image_dir)
    all_exams  = len(all_objs)
    np.random.shuffle(all_objs)
    train_gen = data_gen(image_dir, all_objs, BATCH_SIZE)
    train_num = int(np.ceil(all_exams/BATCH_SIZE))
    
    ### buile graph
    dimension, orientation, confidence, loss, optimizer, loss_d, loss_o, loss_c = build_model()

    ### GPU config
    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True
    sess = tf.Session(config=tfconfig)

    # create a folder for saving model
    if os.path.isdir(save_path) == False:
        os.mkdir(save_path)
    variables_to_restore = slim.get_variables()[:26] ## vgg16-conv5

    saver = tf.train.Saver(max_to_keep=100)

    #Load pretrain VGG model
    ckpt_list = tf.contrib.framework.list_variables('./vgg_16.ckpt')[1:-7]
    new_ckpt_list = []
    for name in range(1,len(ckpt_list),2):
        tf.contrib.framework.init_from_checkpoint('./vgg_16.ckpt', {ckpt_list[name-1][0]: variables_to_restore[name]})
        tf.contrib.framework.init_from_checkpoint('./vgg_16.ckpt', {ckpt_list[name][0]: variables_to_restore[name-1]})

    # Initializing the variables
    init = tf.global_variables_initializer()
    print(sess.run(init))


    # Start to train model
    for epoch in range(epochs):
        epoch_loss = np.zeros((train_num,1),dtype = float)
        tStart_epoch = time.time()
        batch_loss = 0.0
        for num_iters in tqdm(range(train_num),ascii=True,desc='Epoch '+str(epoch+1)+' : Loss:'+str(batch_loss)):
            train_img, train_label = train_gen.next()
            _,batch_loss = sess.run([optimizer,loss], feed_dict={inputs: train_img, d_label: train_label[0], o_label: train_label[1], c_label: train_label[2]})

            epoch_loss[num_iters] = batch_loss 

        # save model
        if (epoch+1) % 5 == 0:
            saver.save(sess,save_path+"model", global_step = epoch+1)

        # Print some information
        print("Epoch:", epoch+1, " done. Loss:", np.mean(epoch_loss))
        tStop_epoch = time.time()
        print("Epoch Time Cost:", round(tStop_epoch - tStart_epoch,2), "s")
        sys.stdout.flush() 
开发者ID:scutan90,项目名称:YOLO-3D-Box,代码行数:58,代码来源:bbox_estimator.py

示例6: train

# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_variables [as 别名]
def train(image_dir, box2d_loc, label_dir):

    # load data & gen data
    all_objs = parse_annotation(label_dir, image_dir)
    all_exams  = len(all_objs)
    np.random.shuffle(all_objs)
    train_gen = data_gen(image_dir, all_objs, BATCH_SIZE)
    train_num = int(np.ceil(all_exams/BATCH_SIZE))
    
    ### buile graph
    dimension, orientation, confidence, loss, optimizer, loss_d, loss_o, loss_c = build_model()

    ### GPU config
    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True
    sess = tf.Session(config=tfconfig)

    # create a folder for saving model
    if os.path.isdir(save_path) == False:
        os.mkdir(save_path)
    variables_to_restore = slim.get_variables()[:26] ## vgg16-conv5

    saver = tf.train.Saver(max_to_keep=100)

    #Load pretrain VGG model
    ckpt_list = tf.contrib.framework.list_variables('./vgg_16.ckpt')[1:-7]
    new_ckpt_list = []
    for name in range(1,len(ckpt_list),2):
        tf.contrib.framework.init_from_checkpoint('./vgg_16.ckpt', {ckpt_list[name-1][0]: variables_to_restore[name]})
        tf.contrib.framework.init_from_checkpoint('./vgg_16.ckpt', {ckpt_list[name][0]: variables_to_restore[name-1]})

    # Initializing the variables
    init = tf.global_variables_initializer()
    sess.run(init)


    # Start to train model
    for epoch in range(epochs):
        epoch_loss = np.zeros((train_num,1),dtype = float)
        tStart_epoch = time.time()
        batch_loss = 0.0
        for num_iters in tqdm(range(train_num),ascii=True,desc='Epoch '+str(epoch+1)+' : Loss:'+str(batch_loss)):
            train_img, train_label = train_gen.next()
            _,batch_loss = sess.run([optimizer,loss], feed_dict={inputs: train_img, d_label: train_label[0], o_label: train_label[1], c_label: train_label[2]})

            epoch_loss[num_iters] = batch_loss 

        # save model
        if (epoch+1) % 5 == 0:
            saver.save(sess,save_path+"model", global_step = epoch+1)

        # Print some information
        print "Epoch:", epoch+1, " done. Loss:", np.mean(epoch_loss)
        tStop_epoch = time.time()
        print "Epoch Time Cost:", round(tStop_epoch - tStart_epoch,2), "s"
        sys.stdout.flush() 
开发者ID:smallcorgi,项目名称:3D-Deepbox,代码行数:58,代码来源:main.py

示例7: build_pairwise_tower_loss

# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_variables [as 别名]
def build_pairwise_tower_loss(fvecs_i, fvecs_j, scope=None,
                              lambda_weight=LAMBDA_WEIGHT):
    """
    Builds an adversarial regressor from fvecs_j to fvecs_i.

    Args:
        fvecs_i: the target embedding (i.e. the smaller embedding)
        fvecs_j: the source embedding (i.e. the begger embedding)
        scope: scope name of the regressor.
        lambda_weight: the regularization parameter for the weights.
    Returns:
        An adversarial regressor loss function.
    """
    # build a regressor from fvecs_j to fvecs_i
    fvecs_i = flip_gradient.flip_gradient(fvecs_i)
    fvecs_j = flip_gradient.flip_gradient(fvecs_j)
    net = fvecs_j

    bias_loss = 0.0
    weight_loss = 0.0
    adversarial_loss = 0.0
    with tf.variable_scope(scope):
        for i in xrange(NUM_HIDDENS_ADVERSARIAL):
            if i < NUM_HIDDENS_ADVERSARIAL - 1:
                net = slim.fully_connected(
                    net, HIDDEN_ADVERSARIAL_SIZE, scope='fc_{}'.format(i),
                    activation_fn=tf.nn.relu)
            else:
                net = slim.fully_connected(net, fvecs_i.get_shape().as_list(
                )[-1], scope='fc_{}'.format(i), activation_fn=None)
            b = slim.get_variables(
                scope=tf.get_variable_scope().name + '/fc_{}/biases'.format(i)
            )[0]
            W = slim.get_variables(
                scope=tf.get_variable_scope().name + '/fc_{}/weights'.format(i)
            )[0]
            weight_loss += tf.reduce_mean(
                tf.square(tf.reduce_sum(W * W, axis=1) - 1)) * lambda_weight
            if b is not None:
                bias_loss += tf.maximum(
                    0.0,
                    tf.reduce_sum(b * b) - 1.0) * lambda_weight
        adversarial_loss += -tf.reduce_mean(tf.square(fvecs_i * net))
        tf.summary.scalar('adversarial loss', adversarial_loss)
        tf.summary.scalar('weight loss', weight_loss)
        tf.summary.scalar('bias loss', bias_loss)
    return adversarial_loss + weight_loss + bias_loss 
开发者ID:mop,项目名称:bier,代码行数:49,代码来源:train_bier.py

示例8: build_train_op

# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_variables [as 别名]
def build_train_op( self, global_step ):
        '''
            Builds two train ops, one for the Generator and one for the Discriminator. These can 
            be run independently any number of times, and each time will increase the global_step.

            Args:
                global_step: A Tensor to be incremented
            Returns:
                [ g_train_op, d_train_op ]
        '''
        if not self.model_built or not self.losses_built :
            raise RuntimeError( "Cannot build optimizers until 'build_model' ({0}) and 'get_losses' {1} are run".format(
                    self.model_built, self.losses_built ) )
        self.global_step = global_step
        self.global_step_copy = tf.identity( global_step, name='global_step_copy' )

        t_vars = tf.trainable_variables()
    
        # Create the optimizer train_op for the generator
        self.g_optimizer = optimize.build_optimizer( global_step=self.global_step, cfg=self.cfg )
        self.g_vars = slim.get_variables( scope='encoder', collection=tf.GraphKeys.TRAINABLE_VARIABLES )
        self.g_vars += slim.get_variables( scope='decoder', collection=tf.GraphKeys.TRAINABLE_VARIABLES )
        self.g_train_op = optimize.create_train_op( self.loss_g_total, self.g_optimizer, 
                    variables_to_train=self.g_vars, update_global_step=True )
        self.g_lnorm_op = optimize.create_train_op( self.softmax_loss, self.g_optimizer, 
                    variables_to_train=self.g_vars, update_global_step=True )


        # Create a train_op for the discriminator
        if 'discriminator_learning_args' in self.cfg: # use these
            discriminator_learning_args = self.cfg[ 'discriminator_learning_args' ]
        else:
            discriminator_learning_args = self.cfg 
        self.d_optimizer = optimize.build_optimizer( global_step=self.global_step, cfg=discriminator_learning_args )
        self.d_vars = slim.get_variables( scope='discriminator', collection=tf.GraphKeys.TRAINABLE_VARIABLES )
        self.d_vars += slim.get_variables( scope='discriminator_1', collection=tf.GraphKeys.TRAINABLE_VARIABLES )
        self.d_train_op = slim.learning.create_train_op( self.loss_d_total, self.d_optimizer, 
                    variables_to_train=self.d_vars )

        self.train_op = [ self.g_train_op, self.d_train_op, self.g_lnorm_op, self.accuracy]
        self.train_op_built = True
        return self.train_op 
开发者ID:StanfordVL,项目名称:taskonomy,代码行数:44,代码来源:encoder_decoder_cgan_softmax.py

示例9: build_train_op

# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import get_variables [as 别名]
def build_train_op( self, global_step ):
        '''
            Builds two train ops, one for the Generator and one for the Discriminator. These can 
            be run independently any number of times, and each time will increase the global_step.

            Args:
                global_step: A Tensor to be incremented
            Returns:
                [ g_train_op, d_train_op ]
        '''
        if not self.model_built or not self.losses_built :
            raise RuntimeError( "Cannot build optimizers until 'build_model' ({0}) and 'get_losses' {1} are run".format(
                    self.model_built, self.losses_built ) )
        self.global_step = global_step
        self.global_step_copy = tf.identity( global_step, name='global_step_copy' )

        t_vars = tf.trainable_variables()
    
        # Create the optimizer train_op for the generator
        self.g_optimizer = optimize.build_optimizer( global_step=self.global_step, cfg=self.cfg )
        self.g_vars = slim.get_variables( scope='encoder', collection=tf.GraphKeys.TRAINABLE_VARIABLES )
        self.g_vars += slim.get_variables( scope='decoder', collection=tf.GraphKeys.TRAINABLE_VARIABLES )
        self.g_train_op = optimize.create_train_op( self.loss_g_total, self.g_optimizer, 
                    variables_to_train=self.g_vars, update_global_step=True )

        self.g_lnorm_op = optimize.create_train_op( self.l1_loss, self.g_optimizer, 
                    variables_to_train=self.g_vars, update_global_step=True )

        # Create a train_op for the discriminator
        if 'discriminator_learning_args' in self.cfg: # use these
            discriminator_learning_args = self.cfg[ 'discriminator_learning_args' ]
        else:
            discriminator_learning_args = self.cfg 
        self.d_optimizer = optimize.build_optimizer( global_step=self.global_step, cfg=discriminator_learning_args )
        self.d_vars = slim.get_variables( scope='discriminator', collection=tf.GraphKeys.TRAINABLE_VARIABLES )
        self.d_vars += slim.get_variables( scope='discriminator_1', collection=tf.GraphKeys.TRAINABLE_VARIABLES )
        self.d_train_op = slim.learning.create_train_op( self.loss_d_total, self.d_optimizer, 
                    variables_to_train=self.d_vars )

        self.train_op = [ self.g_train_op, self.d_train_op, self.g_lnorm_op ]
        self.train_op_built = True
        return self.train_op 
开发者ID:StanfordVL,项目名称:taskonomy,代码行数:44,代码来源:encoder_decoder_cgan.py


注:本文中的tensorflow.contrib.slim.get_variables方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。