def test(self, test_samples, test_labels, *, data_iterator):
        if self.saver is None:
            self.define_model()
        if self.writer is None:
            self.writer = tf.summary.FileWriter('./board', tf.get_default_graph())

        print('Before session')
        with tf.Session(graph=tf.get_default_graph()) as session:
            self.saver.restore(session, self.save_path)
            ### 测试
            accuracies = []
            confusionMatrices = []
            for i, samples, labels in data_iterator(test_samples, test_labels, chunkSize=self.test_batch_size):
                result = session.run(
                    self.test_prediction,
                    feed_dict={self.tf_test_samples: samples}
                )
                # self.writer.add_summary(summary, i)
                accuracy, cm = self.accuracy(result, labels, need_confusion_matrix=True)
                accuracies.append(accuracy)
                confusionMatrices.append(cm)
                print('Test Accuracy: %.1f%%' % accuracy)
            print(' Average  Accuracy:', np.average(accuracies))
            print('Standard Deviation:', np.std(accuracies))
            self.print_confusion_matrix(np.add.reduce(confusionMatrices))
Exemplo n.º 2
0
	def __init__(self, model_dir, files, use_gpu=False):
		import tensorflow as tf
		from glob import glob
		self.files = files
		
		cluster_file = model_dir+'clusters.npy'
		self.n_clusters = getNCluster(cluster_file)
		self._load_cluster(cluster_file)
		
		if not use_gpu:
			d = tf.device('/cpu:0')
		data = tf.placeholder(tf.uint8, shape=(None,None,3))
		w_data = tf.expand_dims(tf.cast(data,tf.float32),0) - tf.constant([123,117,103],dtype=tf.float32,shape=(1,1,3))
		vgg = vgg16(w_data, n_out=self.n_clusters)
		avg_vgg = tf.reduce_mean(tf.reduce_mean(vgg,1),1)
		pred = tf.argmax(avg_vgg,dimension=1)
		self.vgg, self.pred, self.data = vgg, pred, data
		if not use_gpu:
			del d
		
		saver = tf.train.Saver(tf.all_variables())
		tf.get_default_graph().finalize()
		
		self.sess = tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.3)))
		snap = youngest([f for f in glob(os.path.join(model_dir, 'final.ckpt*')) + glob(os.path.join(model_dir, 'snap.ckpt*')) if '.meta' not in f])
		saver.restore(self.sess, snap)
    def train(self, train_samples, train_labels, *, data_iterator, iteration_steps):
        self.writer = tf.summary.FileWriter('./board', tf.get_default_graph())
        with tf.Session(graph=tf.get_default_graph()) as session:
            tf.initialize_all_variables().run()

            ### 训练
            print('Start Training')
            # batch 1000
            for i, samples, labels in data_iterator(train_samples, train_labels, iteration_steps=iteration_steps,
                                                    chunkSize=self.train_batch_size):
                _, l, predictions, summary = session.run(
                    [self.optimizer, self.loss, self.train_prediction, self.merged_train_summary],
                    feed_dict={self.tf_train_samples: samples, self.tf_train_labels: labels}
                )
                self.writer.add_summary(summary, i)
                # labels is True Labels
                accuracy, _ = self.accuracy(predictions, labels)
                if i % 50 == 0:
                    print('Minibatch loss at step %d: %f' % (i, l))
                    print('Minibatch accuracy: %.1f%%' % accuracy)
            ###

            # 检查要存放的路径值否存在。这里假定只有一层路径。
            import os
            if os.path.isdir(self.save_path.split('/')[0]):
                save_path = self.saver.save(session, self.save_path)
                print("Model saved in file: %s" % save_path)
            else:
                os.makedirs(self.save_path.split('/')[0])
                save_path = self.saver.save(session, self.save_path)
                print("Model saved in file: %s" % save_path)
Exemplo n.º 4
0
def build_reparam_loss_and_gradients(inference, var_list):
  """Build loss function. Its automatic differentiation
  is a stochastic gradient of

  $-\\text{ELBO} =
      -\mathbb{E}_{q(z; \lambda)} [ \log p(x, z) - \log q(z; \lambda) ]$

  based on the reparameterization trick (Kingma and Welling, 2014).

  Computed by sampling from $q(z;\lambda)$ and evaluating the
  expectation using Monte Carlo sampling.
  """
  p_log_prob = [0.0] * inference.n_samples
  q_log_prob = [0.0] * inference.n_samples
  base_scope = tf.get_default_graph().unique_name("inference") + '/'
  for s in range(inference.n_samples):
    # Form dictionary in order to replace conditioning on prior or
    # observed variable with conditioning on a specific value.
    scope = base_scope + tf.get_default_graph().unique_name("sample")
    dict_swap = {}
    for x, qx in six.iteritems(inference.data):
      if isinstance(x, RandomVariable):
        if isinstance(qx, RandomVariable):
          qx_copy = copy(qx, scope=scope)
          dict_swap[x] = qx_copy.value()
        else:
          dict_swap[x] = qx

    for z, qz in six.iteritems(inference.latent_vars):
      # Copy q(z) to obtain new set of posterior samples.
      qz_copy = copy(qz, scope=scope)
      dict_swap[z] = qz_copy.value()
      q_log_prob[s] += tf.reduce_sum(
          inference.scale.get(z, 1.0) * qz_copy.log_prob(dict_swap[z]))

    for z in six.iterkeys(inference.latent_vars):
      z_copy = copy(z, dict_swap, scope=scope)
      p_log_prob[s] += tf.reduce_sum(
          inference.scale.get(z, 1.0) * z_copy.log_prob(dict_swap[z]))

    for x in six.iterkeys(inference.data):
      if isinstance(x, RandomVariable):
        x_copy = copy(x, dict_swap, scope=scope)
        p_log_prob[s] += tf.reduce_sum(
            inference.scale.get(x, 1.0) * x_copy.log_prob(dict_swap[x]))

  p_log_prob = tf.reduce_mean(p_log_prob)
  q_log_prob = tf.reduce_mean(q_log_prob)

  if inference.logging:
    tf.summary.scalar("loss/p_log_prob", p_log_prob,
                      collections=[inference._summary_key])
    tf.summary.scalar("loss/q_log_prob", q_log_prob,
                      collections=[inference._summary_key])

  loss = -(p_log_prob - q_log_prob)

  grads = tf.gradients(loss, var_list)
  grads_and_vars = list(zip(grads, var_list))
  return loss, grads_and_vars
Exemplo n.º 5
0
def restore_and_run():
    sess=tf.Session()    
    #First let's load meta graph and restore weights
    saver = tf.train.import_meta_graph('my_test_model-1000.meta')
    saver.restore(sess,tf.train.latest_checkpoint('./'))


    # Access saved Variables directly
    b1_value = sess.run('bias:0')
    print(b1_value)
    # This will print 2, which is the value of bias that we saved

    # cannot directly get the w4, since it depends on w1 and w2
    # w1 and w2 need to be input
    #w4_value = sess.run('op_to_restore:0')


    # Now, let's access and create placeholders variables and
    # create feed-dict to feed new data
    graph = tf.get_default_graph()
    w1 = graph.get_tensor_by_name("w1:0")
    w2 = graph.get_tensor_by_name("w2:0")
    feed_dict ={w1:13.0,w2:17.0}

    #Now, access the op that you want to run. 
    w4 = graph.get_tensor_by_name("op_to_restore:0")

    w4_value = sess.run(w4,feed_dict)
    print(w4_value)
    #This will print 60 which is calculated 

    # see all operator tensor name
    graph.get_operations()
    [op.outputs for op in tf.get_default_graph().get_operations()]
    def predict(self, test_X, isDBN):
        """Predict the labels for the test set.

        Parameters
        ----------

        test_X : array_like, shape (n_samples, n_features)
            Test data.

        Returns
        -------

        array_like, shape (n_samples,) : predicted labels.
        """
        with self.tf_graph.as_default():
	    self.tf_saver = tf.train.import_meta_graph(self.model_path+'.meta')
            with tf.Session() as self.tf_session:
                self.tf_saver.restore(self.tf_session, self.model_path)
		self.input_data = tf.get_default_graph().get_tensor_by_name('x-input:0')
		self.keep_prob = tf.get_default_graph().get_tensor_by_name('keep-probs:0')
		self.next_train = tf.get_default_graph().get_tensor_by_name('encode-9/dropout/mul:0')
		self.mod_y = tf.get_default_graph().get_tensor_by_name('linear/output-y:0')
                feed = {
                    self.input_data: test_X,
                    self.keep_prob: 1
                }
                if isDBN == True:
                    return self.next_train.eval(feed), self.mod_y.eval(feed)
                return self.mod_y.eval(feed)
    def _save_tf_model(self):
        ckpt_dir = '/opt/ml/output/data/checkpoint'
        model_dir = '/opt/ml/model'

        # Re-Initialize from the checkpoint so that you will have the latest models up.
        tf.train.init_from_checkpoint(ckpt_dir,
                                      {'main_level/agent/online/network_0/': 'main_level/agent/online/network_0'})
        tf.train.init_from_checkpoint(ckpt_dir,
                                      {'main_level/agent/online/network_1/': 'main_level/agent/online/network_1'})

        # Create a new session with a new tf graph.
        sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
        sess.run(tf.global_variables_initializer())  # initialize the checkpoint.

        # This is the node that will accept the input.
        input_nodes = tf.get_default_graph().get_tensor_by_name('main_level/agent/main/online/' + \
                                                                'network_0/observation/observation:0')
        # This is the node that will produce the output.
        output_nodes = tf.get_default_graph().get_operation_by_name('main_level/agent/main/online/' + \
                                                                    'network_1/ppo_head_0/policy_mean/BiasAdd')
        # Save the model as a servable model.
        tf.saved_model.simple_save(session=sess,
                                   export_dir='model',
                                   inputs={"observation": input_nodes},
                                   outputs={"policy": output_nodes.outputs[0]})
        # Move to the appropriate folder. 
        shutil.move('model/', model_dir + '/model/tf-model/00000001/')
        # SageMaker will pick it up and upload to the right path.
        print("Success")
Exemplo n.º 8
0
    def __init__(self, hyperparams):

        tf.reset_default_graph()

        #Model hyperparameters
        self.learning_rate = hyperparams['learning_rate']
        self.encoder_act_func = tf.nn.elu #tf.nn.softplus #tf.tanh
        self.decoder_act_func = tf.tanh
        self.encoder_net = hyperparams['encoder_net']
        self.decoder_net = hyperparams['decoder_net']
        self.z_size = hyperparams['z_size']  #Z
        self.x_size = hyperparams['x_size']  #X
        self.rs = 0
        self.n_W_particles = hyperparams['n_W_particles']  #S
        self.n_z_particles = hyperparams['n_z_particles']  #P

        #Placeholders - Inputs/Targets
        self.x = tf.placeholder(tf.float32, [None, self.x_size])
        self.batch_size = tf.shape(self.x)[0]   #B
        self.batch_frac = tf.placeholder(tf.float32, None)
        

        #Define endocer and decoder
        with tf.variable_scope("encoder"):
            encoder = NN(self.encoder_net, self.encoder_act_func, self.batch_size)

        with tf.variable_scope("decoder"):
            decoder = BNN(self.decoder_net, self.decoder_act_func, self.batch_size)
        

        #Objective
        log_probs = self.log_probs(self.x, encoder, decoder)

        self.elbo = self.objective(*log_probs)
        # self.iwae_elbo = self.iwae_objective(*log_probs)

        self.iwae_elbo_test = self.iwae_objective_test(*log_probs)

        # Minimize negative ELBO
        self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, 
                                                epsilon=1e-02).minimize(-self.elbo)


        # for var in tf.global_variables():
        #     print var


        # FOR INSPECING MODEL

        # self.decoder_means = decoder.W_means
        # self.decoder_logvars = decoder.W_logvars

        self.recons, self.priors = self.get_x_samples(self.x, encoder, decoder)



        #Finalize Initilization
        self.init_vars = tf.global_variables_initializer()
        self.saver = tf.train.Saver()
        tf.get_default_graph().finalize()
Exemplo n.º 9
0
    def __init__(self, hyperparams):

        #Model hyperparameters
        self.learning_rate = hyperparams['learning_rate']
        self.encoder_act_func = tf.nn.elu #tf.nn.softplus #tf.tanh
        self.decoder_act_func = tf.tanh
        self.encoder_net = hyperparams['encoder_net']
        self.decoder_net = hyperparams['decoder_net']
        self.z_size = hyperparams['z_size']  #Z
        self.x_size = hyperparams['x_size']  #X
        self.rs = 0
        self.n_W_particles = hyperparams['n_W_particles']  #S

        #Placeholders - Inputs/Targets
        self.x = tf.placeholder(tf.float32, [None, self.x_size])
        self.batch_size = tf.placeholder(tf.int32, None)   #B
        self.n_z_particles = tf.placeholder(tf.int32, None)  #P
        self.batch_frac = tf.placeholder(tf.float32, None)
        
        #Objective
        self.elbo = self.objective(self.x)

        # Minimize negative ELBO
        self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, 
                                                epsilon=1e-02).minimize(-self.elbo)

        #Finalize Initilization
        self.init_vars = tf.global_variables_initializer()
        self.saver = tf.train.Saver()
        tf.get_default_graph().finalize()
        self.sess = tf.Session()
Exemplo n.º 10
0
def inference(input, batch_size, num_segments, lstm_keep_prob=0.5, conv_keep_prob=1.0, train_conv123=False, train_conv45=False, train_fc67=False):
    # input size is [num_segments, batch_size, 224, 224, num_length*3/2]
    fc6_per_step = []
    with tf.variable_scope("conv"):
        for time_step in range(num_segments):
            if time_step > 0: tf.get_variable_scope().reuse_variables()
            fc8 = vgg16.inference(input[time_step, :, :, :, :], conv_keep_prob, train_conv123, train_conv45, train_fc67, False)
            fc7 = tf.get_default_graph().get_tensor_by_name("conv/fc7/fc7:0")
            fc6 = tf.get_default_graph().get_tensor_by_name("conv/fc6/fc6:0")
            # output is [batch_size*num_segments, 4096]
            fc6_per_step.append(fc6)

    with tf.variable_scope("lstm"):
        hidden_size = 512
        lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(hidden_size, forget_bias=1.0, state_is_tuple=True)
        lstm_cell = tf.nn.rnn_cell.DropoutWrapper(lstm_cell, input_keep_prob=lstm_keep_prob, output_keep_prob=lstm_keep_prob)
        cell = lstm_cell
        _initial_state = cell.zero_state(batch_size, tf.float32)

        outputs = []
        state = _initial_state
        for time_step in range(num_segments):
            if time_step > 0: tf.get_variable_scope().reuse_variables()
            (cell_output, state) = cell(fc6_per_step[time_step], state)
            outputs.append(cell_output)
        final_state = state
        lstm_params = [var for var in tf.all_variables() if var.name.startswith("lstm")]
        for var in lstm_params:
            tf.add_to_collection("params", var)
    logits = layers.fc(tf.concat(0, outputs, 'concat'), 101, relu=False, name='cls')

    return logits
Exemplo n.º 11
0
def main(args):
  
    images, cout_per_image, nrof_samples = load_and_align_data(args.image_files,args.image_size, args.margin, args.gpu_memory_fraction)
    with tf.Graph().as_default():

       with tf.Session() as sess:
      
            # Load the model
                facenet.load_model(args.model)
            # Get input and output tensors
                images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
                embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
                phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")

            # Run forward pass to calculate embeddings
                feed_dict = { images_placeholder: images , phase_train_placeholder:False}
                emb = sess.run(embeddings, feed_dict=feed_dict)
                classifier_filename_exp = os.path.expanduser(args.classifier_filename)
                with open(classifier_filename_exp, 'rb') as infile:
                    (model, class_names) = pickle.load(infile)
                print('Loaded classifier model from file "%s"\n' % classifier_filename_exp)
                predictions = model.predict_proba(emb)
                best_class_indices = np.argmax(predictions, axis=1)
                best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices]
                k=0     
	    #print predictions       
                for i in range(nrof_samples):
                    print("\npeople in image %s :" %(args.image_files[i]))
                    for j in range(cout_per_image[i]):
                        print('%s: %.3f' % (class_names[best_class_indices[k]], best_class_probabilities[k]))
                        k+=1
Exemplo n.º 12
0
def main(args):
    align = align_dlib.AlignDlib(os.path.expanduser(args.dlib_face_predictor))
    image_paths = [args.image1, args.image2]
    landmarkIndices = align_dlib.AlignDlib.OUTER_EYES_AND_NOSE

    with tf.Graph().as_default():

        with tf.Session() as sess:
      
            # Load the model
            print('Loading model "%s"' % args.model_file)
            facenet.load_model(args.model_file)
    
            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            image_size = int(images_placeholder.get_shape()[1])

            # Run forward pass to calculate embeddings
            images = load_and_align_data(image_paths, image_size, align, landmarkIndices)
            feed_dict = { images_placeholder: images, phase_train_placeholder: False }
            emb = sess.run(embeddings, feed_dict=feed_dict)
            dist = np.sqrt(np.mean(np.square(np.subtract(emb[0,:], emb[1,:]))))
            print('Distance between the embeddings: %3.6f' % dist)
Exemplo n.º 13
0
 def test_input_pipeline(self):
     Xs, Ys = dsu.tiny_imagenet_load()
     n_batches = 0
     batch_size = 10
     with tf.Graph().as_default(), tf.Session() as sess:
         batch_generator = dsu.create_input_pipeline(
             Xs[:100],
             batch_size=batch_size,
             n_epochs=1,
             shape=(64, 64, 3),
             crop_shape=(64, 64, 3))
         init_op = tf.group(tf.global_variables_initializer(),
                            tf.local_variables_initializer())
         sess.run(init_op)
         coord = tf.train.Coordinator()
         tf.get_default_graph().finalize()
         threads = tf.train.start_queue_runners(sess=sess, coord=coord)
         try:
             while not coord.should_stop():
                 batch = sess.run(batch_generator)
                 assert (batch.shape == (batch_size, 64, 64, 3))
                 n_batches += 1
         except tf.errors.OutOfRangeError:
             pass
         finally:
             coord.request_stop()
         coord.join(threads)
     assert (n_batches == 10)
Exemplo n.º 14
0
def test_distribution_creation_global_graph():
    # Distribution creation doesn't modify the global graph
    before = tf.get_default_graph().as_graph_def()
    with tp.Model():
        tp.Parameter()
    after = tf.get_default_graph().as_graph_def()
    assert before == after
Exemplo n.º 15
0
 def central_step():
     # restore v1, slots
     op5 = tf.group(*[ tf.assign(w,v) for w,v in zip(restored_vars, tmp_vars)])
     with tf.get_default_graph().control_dependencies([op5]):
         back =  tf.group(*[tf.assign_sub(v, -self._lr_t*grad) for grad,v in grads_and_vars])
         with tf.get_default_graph().control_dependencies([back]):
             return tf.gradients(self.gan.trainer.d_loss, d_vars) + tf.gradients(self.gan.trainer.g_loss, g_vars)
Exemplo n.º 16
0
    def _extractFeatures(self, alignedFrames):
        start = time.time()

        if len(alignedFrames) <= 0:
            return []

        with self.session.graph.as_default():
            with self.session.as_default():
                # Get input and output tensors
                images_placeholder = tf.get_default_graph().get_tensor_by_name("resnet/input:0")
                embeddings = tf.get_default_graph().get_tensor_by_name("resnet/embeddings:0")
                #phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("resnet/phase_train:0")

                #image_size = images_placeholder.get_shape()[1]
                #embedding_size = embeddings.get_shape()[1]

                frames = [f for (i, j, f) in alignedFrames]

                imgs = np.array(frames, copy=False)
                feed_dict = { images_placeholder: imgs } #phase_train_placeholder: False }
                emb = self.session.run(embeddings, feed_dict=feed_dict)
                #print emb.shape
                features = [(i, j, e) for (i, j, f), e in zip(alignedFrames, emb)]

        processTime = time.time() - start
        print 'feature extraction for', len(alignedFrames), 'took', processTime, 's, ~', processTime/len(alignedFrames)
        return features
Exemplo n.º 17
0
 def getOp(self, short_name):
   if short_name == 'FC':
     return tf.get_default_graph().get_operation_by_name('FC/MatMul')
   tokens = short_name.split('/')
   name = ('resnet_v1/block1/' + tokens[0] + '/bottleneck_v1/' + tokens[1] +
           '/Conv2D')
   return tf.get_default_graph().get_operation_by_name(name)
Exemplo n.º 18
0
def add_check_numerics_ops():
    """Connect a `check_numerics` to every floating point tensor.
    `check_numerics` operations themselves are added for each `half`, `float`,
    or `double` tensor in the graph. For all ops in the graph, the
    `check_numerics` op for all of its (`half`, `float`, or `double`) inputs
    is guaranteed to run before the `check_numerics` op on any of its outputs.
    Returns:
      A `group` op depending on all `check_numerics` ops added.

    Based on `tf.add_check_numerics_ops`; modified to work around problem with
    variables in different "frames" (triggered by attempt to merge nodes
    from inside and outside the while loop of an RNN).
    """
    check_op = []
    # This code relies on the ordering of ops in get_operations().
    # The producer of a tensor always comes before that tensor's consumer in
    # this list. This is true because get_operations() returns ops in the order
    # added, and an op can only be added after its inputs are added.
    for op in tf.get_default_graph().get_operations():
        if op.name and any(re.search(pattern, op.name) for pattern in NO_MONITOR):
            continue
        for output in op.outputs:
            if output.dtype in [tf.float16, tf.float32, tf.float64] and \
                    output.op._get_control_flow_context() == \
                    tf.get_default_graph()._get_control_flow_context():
                message = op.name + ":" + str(output.value_index)
                with tf.control_dependencies(check_op):
                    check_op = [tf.check_numerics(output, message=message)]
    return tf.group(*check_op)
Exemplo n.º 19
0
def eval_network(sess, summary_writer, dataset, correct_prediction, epoch,
                 mode, make_summary=False):
    correct_sum = 0
    total_test = 0
    if mode == 'test' and make_summary:
        training_summary = tf.get_default_graph().get_tensor_by_name("training_accuracy:0")
        loss_summary = tf.get_default_graph().get_tensor_by_name("loss:0")
    for i in range(dataset.labels.shape[0] / 1000):
        feed_dict = {x: dataset.images[i * 1000:(i + 1) * 1000],
                     y_: dataset.labels[i * 1000:(i + 1) * 1000],
                     # keep_prob: 1.0
                     }

        if mode == 'test' and make_summary:
            [test_correct, train_summ, loss_summ] = sess.run([correct_prediction,
                                                              training_summary,
                                                              loss_summary],
                                                             feed_dict=feed_dict)
            summary_writer.add_summary(train_summ, epoch)
            summary_writer.add_summary(loss_summ, epoch)
        else:
            test_correct = correct_prediction.eval(feed_dict=feed_dict)
        correct_sum += sum(test_correct)
        total_test += len(test_correct)
    return float(correct_sum) / total_test
Exemplo n.º 20
0
  def testFromDifferentScope(self):
    sub = functools.partial(re.sub, r'^[^/]+/', 'agent/')
    restore_initializers = {
        'w': initializers.restore_initializer(_checkpoint(), 'w', sub),
        'b': initializers.restore_initializer(_checkpoint(), 'b', sub)
    }

    with tf.variable_scope('some_random_scope'):
      c = convnet.ConvNet2D(
          output_channels=(16, 32),
          kernel_shapes=(8, 4),
          strides=(4, 2),
          paddings=[conv.VALID],
          activation=tf.nn.relu,
          activate_final=True,
          initializers=restore_initializers)

    inputs = tf.constant(1 / 255.0, shape=[1, 86, 86, 3])
    outputs = c(inputs)
    init = tf.global_variables_initializer()
    tf.get_default_graph().finalize()
    with self.test_session() as session:
      session.run(init)
      o = session.run(outputs)

    self.assertAllClose(
        np.linalg.norm(o), _TWO_CONV_LAYERS_RELU, atol=_TOLERANCE)
Exemplo n.º 21
0
  def testScopeRestore(self):
    c1 = conv.Conv2D(
        16,
        8,
        4,
        name='conv_2d_0',
        padding=conv.VALID,
        initializers={
            'w':
                initializers.restore_initializer(
                    _checkpoint(), 'w', scope='agent/conv_net_2d/conv_2d_0'),
            'b':
                initializers.restore_initializer(
                    _checkpoint(), 'b', scope='agent/conv_net_2d/conv_2d_0')
        })

    inputs = tf.constant(1 / 255.0, shape=[1, 86, 86, 3])
    outputs = c1(inputs)
    init = tf.global_variables_initializer()
    tf.get_default_graph().finalize()
    with self.test_session() as session:
      session.run(init)
      o = session.run(outputs)

    self.assertAllClose(np.linalg.norm(o), _ONE_CONV_LAYER, atol=_TOLERANCE)
def initialize_session(acoustic_checkpoint, hparams):
  """Initializes a transcription session."""
  with tf.Graph().as_default():
    examples = tf.placeholder(tf.string, [None])

    hparams.batch_size = 1

    batch, iterator = data.provide_batch(
        batch_size=1,
        examples=examples,
        hparams=hparams,
        is_training=False,
        truncated_length=0)

    model.get_model(batch, hparams, is_training=False)

    session = tf.Session()
    saver = tf.train.Saver()
    saver.restore(session, acoustic_checkpoint)

    onset_probs_flat = tf.get_default_graph().get_tensor_by_name(
        'onsets/onset_probs_flat:0')
    frame_probs_flat = tf.get_default_graph().get_tensor_by_name(
        'frame_probs_flat:0')
    velocity_values_flat = tf.get_default_graph().get_tensor_by_name(
        'velocity/velocity_values_flat:0')

    return TranscriptionSession(
        session=session,
        examples=examples,
        iterator=iterator,
        onset_probs_flat=onset_probs_flat,
        frame_probs_flat=frame_probs_flat,
        velocity_values_flat=velocity_values_flat,
        hparams=hparams)
Exemplo n.º 23
0
  def testMoreMultipleRestore(self):
    restore_initializers = {
        'w': initializers.restore_initializer(_checkpoint(), 'w'),
        'b': initializers.restore_initializer(_checkpoint(), 'b')
    }

    with tf.variable_scope('agent'):
      c = convnet.ConvNet2D(
          output_channels=(16, 32),
          kernel_shapes=(8, 4),
          strides=(4, 2),
          paddings=[conv.VALID],
          activation=tf.nn.relu,
          activate_final=True,
          initializers=restore_initializers)

    inputs = tf.constant(1 / 255.0, shape=[1, 86, 86, 3])
    outputs = c(inputs)
    init = tf.global_variables_initializer()
    tf.get_default_graph().finalize()
    with self.test_session() as session:
      session.run(init)
      o = session.run(outputs)

    self.assertAllClose(
        np.linalg.norm(o), _TWO_CONV_LAYERS_RELU, atol=_TOLERANCE)
Exemplo n.º 24
0
    def __init__(self, hyperparams):

        tf.reset_default_graph()

        #Model hyperparameters
        self.learning_rate = hyperparams['learning_rate']

        self.z_size = hyperparams['z_size']  #Z
        self.x_size = hyperparams['x_size']  #X
        self.rs = 0
        # self.n_W_particles = hyperparams['n_W_particles']  #S
        # self.n_W_particles = tf.placeholder(tf.int32, None)  #S
        self.n_z_particles = hyperparams['n_z_particles']  #P
        # self.n_z_particles = tf.placeholder(tf.int32, None)  #P
        self.n_transitions = hyperparams['leapfrog_steps'] #this is leapfrog steps, whereas T=1 like the paper


        # self.encoder_act_func = tf.nn.elu #tf.nn.softplus #tf.tanh
        # self.decoder_act_func = tf.tanh
        # self.encoder_net = hyperparams['encoder_net']
        # self.decoder_net = hyperparams['decoder_net']

        #Placeholders - Inputs/Targets
        self.x = tf.placeholder(tf.float32, [None, self.x_size])

        # self.batch_frac = tf.placeholder(tf.float32, None)
        self.batch_size = tf.shape(self.x)[0]   #B

        #Define networks q_zlx, q_vlxz, r_vlxz, p_xlz

        # q(z|x)
        net1 = NN([self.x_size, 300, 300, self.z_size*2], tf.nn.elu)
        # q(v|x,z) 
        net2 = NN([self.x_size+self.z_size, 300, 300, self.z_size*2], tf.nn.elu)
        # r(v|x,z)
        net3 = NN([self.x_size+self.z_size, 300, 300, self.z_size*2], tf.nn.elu)
        # p(x|z)
        net4 = NN([self.z_size, 300, 300, self.x_size], tf.tanh)
        

        #Objective
        log_probs_list = self.log_probs(self.x, net1, net2, net3, net4)

        self.elbo = self.objective(*log_probs_list)
        self.iwae_elbo = self.iwae_objective(*log_probs_list)


        # Minimize negative ELBO
        self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, 
                                                epsilon=1e-02).minimize(-self.elbo)


        # for var in tf.global_variables():
        #     print var


        #Finalize Initilization
        self.init_vars = tf.global_variables_initializer()
        self.saver = tf.train.Saver()
        tf.get_default_graph().finalize()
def main():
    with open(a.input_file, "rb") as f:
        input_data = f.read()

    input_instance = dict(input=base64.urlsafe_b64encode(input_data).decode("ascii"), key="0")
    input_instance = json.loads(json.dumps(input_instance))

    with tf.Session() as sess:
        saver = tf.train.import_meta_graph(a.model_dir + "/export.meta")
        saver.restore(sess, a.model_dir + "/export")
        input_vars = json.loads(tf.get_collection("inputs")[0])
        output_vars = json.loads(tf.get_collection("outputs")[0])
        input = tf.get_default_graph().get_tensor_by_name(input_vars["input"])
        output = tf.get_default_graph().get_tensor_by_name(output_vars["output"])

        input_value = np.array(input_instance["input"])
        output_value = sess.run(output, feed_dict={input: np.expand_dims(input_value, axis=0)})[0]

    output_instance = dict(output=output_value.decode("ascii"), key="0")

    b64data = output_instance["output"]
    b64data += "=" * (-len(b64data) % 4)
    output_data = base64.urlsafe_b64decode(b64data.encode("ascii"))

    with open(a.output_file, "wb") as f:
        f.write(output_data)
    def images_to_vectors(self, inpath, outjson_path, modelpath):
        results = dict()

        with tf.Graph().as_default():
            with tf.Session() as sess:
                src.facenet.load_model(modelpath)
                # Get input and output tensors
                images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
                embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
                phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")

                image_paths = self.get_image_paths(inpath)
                for image_path in image_paths:
                    # 获取图片中的人脸数
                    img = misc.imread(os.path.expanduser(image_path), mode='RGB')
                    images = self.image_array_align_data(img,image_path)
                    #判断是否检测出人脸 检测不出 就跳出此循环
                    if images.shape[0] == 1 : continue
                    feed_dict = {images_placeholder: images, phase_train_placeholder: False}

                    emb_array = sess.run(embeddings, feed_dict=feed_dict)

                    filename_base, file_extension = os.path.splitext(image_path)
                    for j in range(0, len(emb_array)):
                        results[filename_base + "_" + str(j)] = emb_array[j].tolist()
                        face_mysql_instant = face_mysql.face_mysql()
                        face_mysql_instant.insert_facejson(filename_base + "_" + str(j),
                                                           ",".join(str(li) for li in emb_array[j].tolist()))

        # All done, save for later!
        json.dump(results, open(outjson_path, "w"))
Exemplo n.º 27
0
def main(args):
  
    with tf.Graph().as_default():

        with tf.Session() as sess:
            
            # Read the file containing the pairs used for testing
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))

            # Get the paths for the corresponding images
            paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)
            
            # Load the model
            print('Loading model "%s"' % args.model_file)
            facenet.load_model(args.model_file)
            
            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")

            tpr, fpr, accuracy, val, val_std, far = lfw.validate(sess, 
                paths, actual_issame, args.seed, 60, 
                images_placeholder, phase_train_placeholder, embeddings, nrof_folds=args.lfw_nrof_folds)
            print('Accuracy: %1.3f+-%1.3f' % (np.mean(accuracy), np.std(accuracy)))
            print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far))
            
            facenet.plot_roc(fpr, tpr, 'NN4')
def gradient_memory_measure_mb():
  """Evaluates gradient, prints peak memory in MBs."""
  global sess
  
  start_time0 = time.perf_counter()
  loss = create_loss()

  if DUMP_GRAPHDEF:
    open('graphdef.txt', 'w').write(str(tf.get_default_graph().as_graph_def()))

  # use block_layer1, block_layer2, block_layer3 as checkpoint nodes
  g = tf.get_default_graph()
  ops = g.get_operations()
  for op in ge.filter_ops_from_regex(ops, "block_layer"):
    tf.add_to_collection("checkpoints", op.outputs[0])

  start_time = time.perf_counter()
  grads = tf.gradients(loss, tf.trainable_variables())
  
  start_time = time.perf_counter()
  sess = create_session()
  start_time = time.perf_counter()
  sessrun(tf.global_variables_initializer())
  start_time = time.perf_counter()
  sessrun(grads)
  start_time = time.perf_counter()
  sessrun(grads)

  mem_use = mem_util.peak_memory(run_metadata)['/gpu:0']/1e6
  
  print("Memory used: %.2f MB "%(mem_use))
  total_time = time.perf_counter()-start_time0
  print("Total time: %.2f sec"%(total_time))
  assert total_time < 100
  return mem_use
Exemplo n.º 29
0
def restore_and_test():
    data_dir = "./data"
    mnist = input_data.read_data_sets(data_dir, one_hot=True)

    sess=tf.Session()    
    #First let's load meta graph and restore weights
    saver = tf.train.import_meta_graph('mnist_softmax-1000.meta')
    saver.restore(sess,tf.train.latest_checkpoint('./'))

    [op.outputs for op in tf.get_default_graph().get_operations()]

    graph = tf.get_default_graph()
    x = graph.get_tensor_by_name("x:0")
    y = graph.get_tensor_by_name("y/add:0")
    y_ = graph.get_tensor_by_name("y_:0")

    W = graph.get_tensor_by_name("W:0")
    W_value = sess.run(W)
    # you can see the values of W
    # the most import value in neuron network
    # the main difference with simple example.
    np.unique(W_value,return_counts=True)
    # 
    # (array([-0.66475219, -0.66456056, -0.65921789, ...,  0.63828367,
    #          0.69256693,  0.72933155], dtype=float32),
    #  array([1, 1, 1, ..., 1, 1, 1]))


    # Test trained model
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    print(sess.run(accuracy, feed_dict={x: mnist.test.images,
                                      y_: mnist.test.labels}))
Exemplo n.º 30
0
Arquivo: eval.py Projeto: caomw/geoloc
def main():
	from argparse import ArgumentParser
	from glob import glob
	from time import time
	import random
	import tensorflow as tf

	parser = ArgumentParser()
	parser.add_argument('--batch_size', type=int, default=32, help='batch size')
	parser.add_argument('-n', type=int, default=None, help='Number of images to evaluate on')
	parser.add_argument('--file-list', type=str, default='/fastdata/finder/streetview_test.txt', help='path to the streetview test file')
	parser.add_argument('--file-base-dir', type=str, default='/fastdata/finder/streetview/', help='directory of the test images')
	parser.add_argument('train_dir', help='training directory')
	args = parser.parse_args()

	cluster_file = args.train_dir+'clusters.npy'
	files = [os.path.join(args.file_base_dir,l.strip()) for l in open(args.file_list,'r')]

	random.shuffle(files)
	if args.n:
		files = files[:args.n]
	args.n = len(files)
	
	# Setup the graph
	data,gt = glocData(files, cluster_file, batch_size=args.batch_size)
	n_clusters = getNCluster(cluster_file)

	vgg = vgg16(data, n_out=n_clusters)
	avg_vgg = tf.reduce_mean(tf.reduce_mean(vgg,1),1)
	
	pred = tf.argmax(avg_vgg,dimension=1)

	# Initialize ops
	saver = tf.train.Saver(tf.all_variables())
	tf.get_default_graph().finalize()

	with tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.3))) as sess:
		# Initialize stuff
		tf.train.start_queue_runners(sess=sess)
		snap = youngest([f for f in glob(os.path.join(args.train_dir, 'final.ckpt*')) + glob(os.path.join(args.train_dir, 'snap.ckpt*')) if '.meta' not in f])
		saver.restore(sess, snap)
		
		# Eval
		top1_acc, top5_acc = [], []
		id = []
		for it in range(0,args.n,args.batch_size):
			t0 = time()
			gt_value, pred_value, score = sess.run([gt, pred, avg_vgg])
			t1 = time()
			
			for g, p, s in zip(gt_value, pred_value, score):
				top1_acc.append( g==p )
				ss = sorted( s )
				top5_acc.append( ss[-5] <= s[g] )
				id.append( np.where(s[g] == ss[::-1])[0] )
			if it % (10*args.batch_size) == 0:
				print('%8d, top1 = %0.2f    top5 = %0.2f  (%0.1f im/sec)'%(it, np.mean(top1_acc), np.mean(top5_acc), args.batch_size / (t1-t0)))
		print('%8d, top1 = %0.2f    top5 = %0.2f  (%0.1f im/sec)'%(args.n, np.mean(top1_acc), np.mean(top5_acc), args.batch_size / (t1-t0)))
		print( [np.mean(np.array(id) <= r) for r in range(100)] )
    # Symbols
    x = tf.placeholder("float", shape=[None, x_size])
    y = tf.placeholder("float", shape=[None, y_size])

    sess = tf.Session()
    #init = tf.global_variables_initializer()
    saver = tf.train.import_meta_graph(
        '/home/vkvalappil/Data/workspace/pythonScripts/MLP/mlp_bias_cor_' +
        str(h_size) + 'N/mlp_bias_cor_' + str(h_size) + 'N.meta')
    saver.restore(
        sess,
        tf.train.latest_checkpoint(
            '/home/vkvalappil/Data/workspace/pythonScripts/MLP/mlp_bias_cor_' +
            str(h_size) + 'N/'))

    graph = tf.get_default_graph()
    w1 = graph.get_tensor_by_name("W1:0")
    w2 = graph.get_tensor_by_name("W2:0")
    w3 = graph.get_tensor_by_name("W3:0")

    b1 = graph.get_tensor_by_name("b1:0")
    b2 = graph.get_tensor_by_name("b2:0")
    b3 = graph.get_tensor_by_name("b3:0")

    hidden_out = tf.nn.tanh(tf.add(tf.matmul(x, w1), b1))

    hidden_out2 = tf.nn.tanh(tf.add(tf.matmul(hidden_out, w2), b2))

    y_ = tf.add(tf.matmul(hidden_out2, w3), b3)

    #y_ = tf.nn.tanh(tf.add(tf.matmul(hidden_out2, w3), b3))
Exemplo n.º 32
0
def train(resume, visualize):
    np.random.seed(cfg.random_seed)
    dataset, train_imdb = get_dataset()
    do_val = len(cfg.train.val_imdb) > 0

    class_weights = class_equal_weights(train_imdb)
    (preloaded_batch, enqueue_op, enqueue_placeholders,
     q_size) = setup_preloading(Gnet.get_batch_spec(train_imdb['num_classes']))
    reg = tf.contrib.layers.l2_regularizer(cfg.train.weight_decay)
    net = Gnet(num_classes=train_imdb['num_classes'],
               batch=preloaded_batch,
               weight_reg=reg,
               class_weights=class_weights)
    lr_gen = LearningRate()
    # reg_ops = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
    # reg_op = tf.reduce_mean(reg_ops)
    # optimized_loss = net.loss + reg_op
    optimized_loss = tf.contrib.losses.get_total_loss()
    learning_rate, train_op = get_optimizer(optimized_loss,
                                            net.trainable_variables)

    val_net = val_imdb = None
    if do_val:
        val_imdb = imdb.get_imdb(cfg.train.val_imdb, is_training=False)
        val_net = Gnet(num_classes=val_imdb['num_classes'], reuse=True)

    with tf.name_scope('summaries'):
        tf.summary.scalar('loss', optimized_loss)
        tf.summary.scalar('data_loss', net.loss)
        tf.summary.scalar('data_loss_normed', net.loss_normed)
        tf.summary.scalar('data_loss_unnormed', net.loss_unnormed)
        tf.summary.scalar('lr', learning_rate)
        tf.summary.scalar('q_size', q_size)
        if cfg.train.histograms:
            tf.summary.histogram('roi_feats', net.roifeats)
            tf.summary.histogram('det_imfeats', net.det_imfeats)
            tf.summary.histogram('pw_feats', net.pw_feats)
            for i, blockout in enumerate(net.block_feats):
                tf.summary.histogram('block{:02d}'.format(i + 1), blockout)
        merge_summaries_op = tf.summary.merge_all()

    with tf.name_scope('averaging'):
        ema = tf.train.ExponentialMovingAverage(decay=0.7)
        maintain_averages_op = ema.apply(
            [net.loss_normed, net.loss_unnormed, optimized_loss])
        # update moving averages after every loss evaluation
        with tf.control_dependencies([train_op]):
            train_op = tf.group(maintain_averages_op)
        smoothed_loss_normed = ema.average(net.loss_normed)
        smoothed_loss_unnormed = ema.average(net.loss_unnormed)
        smoothed_optimized_loss = ema.average(optimized_loss)

    restorer = ckpt = None
    if resume:
        ckpt = tf.train.get_checkpoint_state('./')
        restorer = tf.train.Saver()
    elif cfg.gnet.imfeats:
        variables_to_restore = slim.get_variables_to_restore(
            include=["resnet_v1"])
        variables_to_exclude = \
            slim.get_variables_by_suffix('Adam_1', scope='resnet_v1') + \
            slim.get_variables_by_suffix('Adam', scope='resnet_v1') + \
            slim.get_variables_by_suffix('Momentum', scope='resnet_v1')
        restorer = tf.train.Saver(
            list(set(variables_to_restore) - set(variables_to_exclude)))

    saver = tf.train.Saver(max_to_keep=None)
    model_manager = ModelManager()
    config = tf.ConfigProto()
    with tf.Session(config=config) as sess:
        train_writer = tf.summary.FileWriter(cfg.log_dir, sess.graph)
        tf.global_variables_initializer().run()
        tf.local_variables_initializer().run()
        coord = start_preloading(sess, enqueue_op, dataset,
                                 enqueue_placeholders)

        start_iter = 1
        if resume:
            restorer.restore(sess, ckpt.model_checkpoint_path)
            tensor = tf.get_default_graph().get_tensor_by_name("global_step:0")
            start_iter = sess.run(tensor + 1)
        elif cfg.gnet.imfeats:
            restorer.restore(sess, cfg.train.pretrained_model)

        for it in range(start_iter, cfg.train.num_iter + 1):
            if coord.should_stop():
                break

            if visualize:
                # don't do actual training, just visualize data
                visualize_detections(sess, it, learning_rate, lr_gen, net,
                                     train_op)
                continue

            (_, val_total_loss, val_loss_normed, val_loss_unnormed,
             summary) = sess.run([
                 train_op, smoothed_optimized_loss, smoothed_loss_normed,
                 smoothed_loss_unnormed, merge_summaries_op
             ],
                                 feed_dict={learning_rate: lr_gen.get_lr(it)})
            train_writer.add_summary(summary, it)

            if it % cfg.train.display_iter == 0:
                print(('{}  iter {:6d}   lr {:8g}   opt loss {:8g}     '
                       'data loss normalized {:8g}   '
                       'unnormalized {:8g}').format(datetime.now(), it,
                                                    lr_gen.get_lr(it),
                                                    val_total_loss,
                                                    val_loss_normed,
                                                    val_loss_unnormed))

            if do_val and it % cfg.train.val_iter == 0:
                print('{}  starting validation'.format(datetime.now()))
                val_map, mc_ap, pc_ap = val_run(sess, val_net, val_imdb)
                print(('{}  iter {:6d}   validation pass:   mAP {:5.1f}   '
                       'multiclass AP {:5.1f}').format(datetime.now(), it,
                                                       val_map, mc_ap))

                save_path = saver.save(sess, net.name, global_step=it)
                print('wrote model to {}'.format(save_path))
                # dump_debug_info(sess, net, it)
                model_manager.add(it, val_map, save_path)
                model_manager.print_summary()
                model_manager.write_link_to_best('./gnet_best')

            elif it % cfg.train.save_iter == 0 or it == cfg.train.num_iter:
                save_path = saver.save(sess, net.name, global_step=it)
                print('wrote model to {}'.format(save_path))
                # dump_debug_info(sess, net, it)

        coord.request_stop()
        coord.join()
    print('training finished')
    if do_val:
        print('summary of validation performance')
        model_manager.print_summary()
Exemplo n.º 33
0
from Chapter02 import hy_param

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

# Pointing the model checkpoint
checkpoint_file = tf.train.latest_checkpoint(
    os.path.join(hy_param.checkpoint_dir, 'checkpoints'))
saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))

# Loading test data
test_data = np.array([mnist.test.images[6]])

# Loading input variable from the model
input_x = tf.get_default_graph().get_operation_by_name("input_x").outputs[0]

# Loading Prediction operation
prediction = tf.get_default_graph().get_operation_by_name(
    "prediction").outputs[0]

with tf.Session() as sess:
    # Restoring the model from the checkpoint
    saver.restore(sess, checkpoint_file)

    # Executing the model to make predictions
    data = sess.run(prediction, feed_dict={input_x: test_data})

    print("Predicted digit: ", data.argmax())

# Display the feed image
            'Human 2',
            'Human 3',
            'Human 4',
            'Lee Yam Keng',
            'Human 6',
            'Mohammed Alzaidi',
            'Human 8',
            'Human 9',
            'Human 10'
            ]    #train human names

        print('Loading feature extraction model')
        modeldir = 'path/to/saved_model.pb'
        facenet.load_model(modeldir)

        images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
        embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
        phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
        embedding_size = embeddings.get_shape()[1]

        classifier_filename = 'save_classifier/my_classifier.pkl'
        classifier_filename_exp = os.path.expanduser(classifier_filename)
        with open(classifier_filename_exp, 'rb') as infile:
            (model, class_names) = pickle.load(infile)
            print('load classifier file-> %s' % classifier_filename_exp)

        video_capture = cv2.VideoCapture(1)
        c = 0

        #video writer
        # fourcc = cv2.CV_FOURCC(*'XVID')
Exemplo n.º 35
0
loss = tf.reduce_mean(tf.square(tf.subtract(model.y_, model.y))) + tf.add_n([tf.nn.l2_loss(v) for v in train_vars]) * L2NormConst
accuracy = 100 - loss 
train_step = tf.train.AdamOptimizer(1e-4).minimize(loss)
sess.run(tf.initialize_all_variables())

# create a summary to monitor cost tensor
tf.summary.scalar("loss", loss)
tf.summary.scalar("accuracy", accuracy)
# merge all summaries into a single op
merged_summary_op =  tf.summary.merge_all()

saver = tf.train.Saver(write_version = saver_pb2.SaverDef.V2)

# op to write logs to Tensorboard
logs_path = './logs'
summary_writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph())
# 30 epochs is a good number. You can in or decrease baed on your computation power.
epochs = 30
batch_size = 32

# Training with those values takes about an hour on a GTX 1070 with CUDA 10.0
for epoch in range(epochs):
  for i in range(int(driving_data.num_images/batch_size)):
    xs, ys = driving_data.LoadTrainBatch(batch_size)
    train_step.run(feed_dict={model.x: xs, model.y_: ys, model.keep_prob: 0.8})
    if i % 10 == 0:
      xs, ys = driving_data.LoadValBatch(batch_size)
      # The Step value is calculated by: epoch * batchsize + i
      loss_value = loss.eval(feed_dict={model.x:xs, model.y_: ys, model.keep_prob: 1.0})
      print("Epoch: %d, Step: %d, Loss: %g" % (epoch, epoch * batch_size + i, loss_value))
Exemplo n.º 36
0
def main():
    """Create the model and start the training."""
    args = get_arguments()

    os.environ['CUDA_DEVIDE_ORDER'] = "PCI_BUS_ID"
    os.environ['CUDA_VISIBLE_DEVICES'] = args.GPU

    h, w = map(int, args.input_size.split(','))
    input_size = (h, w)

    tf.set_random_seed(args.random_seed)

    # Create queue coordinator.
    coord = tf.train.Coordinator()

    # Load reader.
    with tf.name_scope("create_inputs"):
        reader = ImageReader(args.data_dir, args.data_list, input_size,
                             args.random_scale, args.random_mirror,
                             args.ignore_label, IMG_MEAN, coord)
        image_batch, label_batch = reader.dequeue(args.batch_size)
        image_batch075 = tf.image.resize_images(
            image_batch, [int(h * 0.75), int(w * 0.75)])
        image_batch05 = tf.image.resize_images(
            image_batch, [int(h * 0.5), int(w * 0.5)])

    # Create network.
    with tf.variable_scope('', reuse=False):
        net = DeepLabResNetModel_34({'data': image_batch},
                                    is_training=args.is_training,
                                    num_classes=args.num_classes)
    with tf.variable_scope('', reuse=True):
        net075 = DeepLabResNetModel_34({'data': image_batch075},
                                       is_training=args.is_training,
                                       num_classes=args.num_classes)
    with tf.variable_scope('', reuse=True):
        net05 = DeepLabResNetModel_34({'data': image_batch05},
                                      is_training=args.is_training,
                                      num_classes=args.num_classes)
    # For a small batch size, it is better to keep
    # the statistics of the BN layers (running means and variances)
    # frozen, and to not update the values provided by the pre-trained model.
    # If is_training=True, the statistics will be updated during the training.
    # Note that is_training=False still updates BN parameters gamma (scale) and beta (offset)
    # if they are presented in var_list of the optimiser definition.

    # Predictions.
    raw_output100 = net.layers['fc1_voc12']
    raw_output075 = net075.layers['fc1_voc12']
    raw_output05 = net05.layers['fc1_voc12']
    raw_output = tf.reduce_max(tf.stack([
        raw_output100,
        tf.image.resize_images(raw_output075,
                               tf.shape(raw_output100)[1:3, ]),
        tf.image.resize_images(raw_output05,
                               tf.shape(raw_output100)[1:3, ])
    ]),
                               axis=0)
    # Which variables to load. Running means and variances are not trainable,
    # thus all_variables() should be restored.
    restore_var = [
        v for v in tf.global_variables()
        if 'fc' not in v.name or not args.not_restore_last
    ]
    all_trainable = [
        v for v in tf.trainable_variables()
        if 'beta' not in v.name and 'gamma' not in v.name
    ]
    fc_trainable = [v for v in all_trainable if 'fc' in v.name]
    conv_trainable = [v for v in all_trainable
                      if 'fc' not in v.name]  # lr * 1.0
    fc_w_trainable = [v for v in fc_trainable
                      if 'weights' in v.name]  # lr * 10.0
    fc_b_trainable = [v for v in fc_trainable
                      if 'biases' in v.name]  # lr * 20.0
    assert (len(all_trainable) == len(fc_trainable) + len(conv_trainable))
    assert (len(fc_trainable) == len(fc_w_trainable) + len(fc_b_trainable))

    # Predictions: ignoring all predictions with labels greater or equal than n_classes
    raw_prediction = tf.reshape(raw_output, [-1, args.num_classes])
    raw_prediction100 = tf.reshape(raw_output100, [-1, args.num_classes])
    raw_prediction075 = tf.reshape(raw_output075, [-1, args.num_classes])
    raw_prediction05 = tf.reshape(raw_output05, [-1, args.num_classes])

    label_proc = prepare_label(label_batch,
                               tf.stack(raw_output.get_shape()[1:3]),
                               num_classes=args.num_classes,
                               one_hot=False)  # [batch_size, h, w]
    label_proc075 = prepare_label(label_batch,
                                  tf.stack(raw_output075.get_shape()[1:3]),
                                  num_classes=args.num_classes,
                                  one_hot=False)
    label_proc05 = prepare_label(label_batch,
                                 tf.stack(raw_output05.get_shape()[1:3]),
                                 num_classes=args.num_classes,
                                 one_hot=False)

    raw_gt = tf.reshape(label_proc, [
        -1,
    ])
    raw_gt075 = tf.reshape(label_proc075, [
        -1,
    ])
    raw_gt05 = tf.reshape(label_proc05, [
        -1,
    ])

    indices = tf.squeeze(tf.where(tf.less_equal(raw_gt, args.num_classes - 1)),
                         1)
    indices075 = tf.squeeze(
        tf.where(tf.less_equal(raw_gt075, args.num_classes - 1)), 1)
    indices05 = tf.squeeze(
        tf.where(tf.less_equal(raw_gt05, args.num_classes - 1)), 1)

    gt = tf.cast(tf.gather(raw_gt, indices), tf.int32)
    gt075 = tf.cast(tf.gather(raw_gt075, indices075), tf.int32)
    gt05 = tf.cast(tf.gather(raw_gt05, indices05), tf.int32)

    prediction = tf.gather(raw_prediction, indices)
    prediction100 = tf.gather(raw_prediction100, indices)
    prediction075 = tf.gather(raw_prediction075, indices075)
    prediction05 = tf.gather(raw_prediction05, indices05)

    # Pixel-wise softmax loss.
    loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=prediction,
                                                          labels=gt)
    loss100 = tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=prediction100, labels=gt)
    loss075 = tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=prediction075, labels=gt075)
    loss05 = tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=prediction05, labels=gt05)
    l2_losses = [
        args.weight_decay * tf.nn.l2_loss(v) for v in tf.trainable_variables()
        if 'weights' in v.name
    ]
    reduced_loss = tf.reduce_mean(loss) + tf.reduce_mean(
        loss100) + tf.reduce_mean(loss075) + tf.reduce_mean(loss05) + tf.add_n(
            l2_losses)
    tf.summary.scalar('loss', reduced_loss)

    # Processed predictions: for visualisation.
    raw_output_up = tf.image.resize_bilinear(raw_output,
                                             tf.shape(image_batch)[1:3, ])
    raw_output_up = tf.argmax(raw_output_up, dimension=3)
    pred = tf.expand_dims(raw_output_up, dim=3)

    # Image summary.
    images_summary = tf.py_func(inv_preprocess,
                                [image_batch, args.save_num_images, IMG_MEAN],
                                tf.uint8)
    labels_summary = tf.py_func(
        decode_labels, [label_batch, args.save_num_images, args.num_classes],
        tf.uint8)
    preds_summary = tf.py_func(decode_labels,
                               [pred, args.save_num_images, args.num_classes],
                               tf.uint8)

    tf.summary.image(
        'images',
        tf.concat(axis=2,
                  values=[images_summary, labels_summary, preds_summary]),
        max_outputs=args.save_num_images)  # Concatenate row-wise.

    # Define loss and optimisation parameters.
    base_lr = tf.constant(args.learning_rate)
    step_ph = tf.placeholder(dtype=tf.float32, shape=())
    learning_rate = tf.scalar_mul(
        base_lr, tf.pow((1 - step_ph / args.num_steps), args.power))
    tf.summary.scalar('learning_rate', learning_rate)

    opt_conv = tf.train.AdamOptimizer(learning_rate)
    opt_fc_w = tf.train.AdamOptimizer(learning_rate)
    opt_fc_b = tf.train.AdamOptimizer(learning_rate)

    # Define a variable to accumulate gradients.
    accum_grads = [
        tf.Variable(tf.zeros_like(v.initialized_value()), trainable=False)
        for v in conv_trainable + fc_w_trainable + fc_b_trainable
    ]

    # Define an operation to clear the accumulated gradients for next batch.
    zero_op = [v.assign(tf.zeros_like(v)) for v in accum_grads]

    # Compute gradients.
    grads = tf.gradients(reduced_loss,
                         conv_trainable + fc_w_trainable + fc_b_trainable)

    # Accumulate and normalise the gradients.
    accum_grads_op = [
        accum_grads[i].assign_add(grad / args.grad_update_every)
        for i, grad in enumerate(grads)
    ]

    grads_conv = accum_grads[:len(conv_trainable)]
    grads_fc_w = accum_grads[len(conv_trainable):(len(conv_trainable) +
                                                  len(fc_w_trainable))]
    grads_fc_b = accum_grads[(len(conv_trainable) + len(fc_w_trainable)):]

    # Apply the gradients.
    train_op_conv = opt_conv.apply_gradients(zip(grads_conv, conv_trainable))
    train_op_fc_w = opt_fc_w.apply_gradients(zip(grads_fc_w, fc_w_trainable))
    train_op_fc_b = opt_fc_b.apply_gradients(zip(grads_fc_b, fc_b_trainable))

    train_op = tf.group(train_op_conv, train_op_fc_w, train_op_fc_b)

    merged = tf.summary.merge_all()
    summary_writer = tf.summary.FileWriter(args.snapshot_dir,
                                           graph=tf.get_default_graph())

    # Set up tf session and initialize variables.
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    init = tf.global_variables_initializer()

    sess.run(init)

    # Saver for storing checkpoints of the model.
    saver = tf.train.Saver(var_list=tf.global_variables(), max_to_keep=10)

    # Load variables if the checkpoint is provided.
    if args.restore_from is not None:
        loader = tf.train.Saver(var_list=restore_var)
        load(loader, sess, args.restore_from)

    # Start queue threads.
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    # Iterate over training steps.
    for step in range(args.num_steps):
        start_time = time.time()
        feed_dict = {step_ph: step}
        loss_value = 0

        # Clear the accumulated gradients.
        sess.run(zero_op, feed_dict=feed_dict)

        # Accumulate gradients.
        for i in range(args.grad_update_every):
            _, l_val = sess.run([accum_grads_op, reduced_loss],
                                feed_dict=feed_dict)
            loss_value += l_val

        # Normalise the loss.
        loss_value /= args.grad_update_every

        # Apply gradients.
        if step % args.save_pred_every == 0:
            images, labels, summary, _ = sess.run(
                [image_batch, label_batch, merged, train_op],
                feed_dict=feed_dict)
            summary_writer.add_summary(summary, step)
            save(saver, sess, args.snapshot_dir, step)
        else:
            sess.run(train_op, feed_dict=feed_dict)

        duration = time.time() - start_time
        print('step {:d} \t loss = {:.3f}, ({:.3f} sec/step)'.format(
            step, loss_value, duration))
    coord.request_stop()
    coord.join(threads)
Exemplo n.º 37
0
import itertools

import tensorflow.keras.backend as K
import numpy as np
import pytest
import tensorflow as tf
from tensorflow.keras.layers import Input

from flowket.machines.ensemble import make_2d_obc_invariants, make_pbc_invariants

from .simple_models import real_values_2d_model, real_values_1d_model

DEFAULT_TF_GRAPH = tf.get_default_graph()


def transform_sample(sample, num_of_rotations, flip):
    if num_of_rotations > 0:
        sample = np.rot90(sample, k=num_of_rotations, axes=(1, 2))
    if flip:
        sample = np.flip(sample, axis=2)
    return sample


def roll_sample(sample, roll_for_axis):
    return np.roll(sample, roll_for_axis, tuple(range(1, len(roll_for_axis))))


@pytest.mark.parametrize('model_builder, batch_size', [
    (real_values_2d_model, 100),
])
def test_make_2d_obc_invariants(model_builder, batch_size):
Exemplo n.º 38
0
def approximator_deep(*args):
    data = tf.stack(args, axis = 1, name = 'data_stacked')
    data = tf.squeeze(data)
#    print(tf.cast(tf.keras.backend.ndim(data) == 1, tf.bool), 
#          type(tf.cast(tf.keras.backend.ndim(data)1, tf.bool)))
#    test = tf.cond(pred = tf.cast(tf.keras.backend.ndim(data) == 1, tf.bool), 
#                   true_fn = lambda: True, false_fn = lambda: False)
    
#    print(type(tf.cast(tf.keras.backend.ndim(data) == 1, tf.bool)))
#    data = tf.cond(pred = tf.equal(tf.keras.backend.ndim(data), 1), 
#                   true_fn = lambda: tf.expand_dims(data, axis=1), false_fn = lambda: data)
    if tf.keras.backend.ndim(data) == 1:
        data = tf.expand_dims(data, axis=1)
    with tf.name_scope("UA_fit"):   
        w1 = tf.get_default_graph().get_tensor_by_name("w1:0")
        b1 = tf.get_default_graph().get_tensor_by_name("b1:0")
        w2 = tf.get_default_graph().get_tensor_by_name("w2:0")
        b2 = tf.get_default_graph().get_tensor_by_name("b2:0")
        w3 = tf.get_default_graph().get_tensor_by_name("w3:0")
        b3 = tf.get_default_graph().get_tensor_by_name("b3:0")
        w4 = tf.get_default_graph().get_tensor_by_name("w4:0")
        b4 = tf.get_default_graph().get_tensor_by_name("b4:0")
        w5 = tf.get_default_graph().get_tensor_by_name("w5:0")
        b5 = tf.get_default_graph().get_tensor_by_name("b5:0")        
        w_out = tf.get_default_graph().get_tensor_by_name("w_out:0")
        
        ws1 = tf.matmul(data, w1) + b1
        a1 = tf.nn.sigmoid(ws1) 

        ws2 = tf.matmul(a1, w2) + b2
        a2 = tf.nn.sigmoid(ws2)

        ws3 = tf.matmul(a2, w3) + b3
        a3 = tf.nn.sigmoid(ws3)


        ws4 = tf.matmul(a3, w4) + b4
        a4 = tf.nn.sigmoid(ws4)

        ws5 = tf.matmul(a4, w5) + b5
        a5 = tf.nn.sigmoid(ws5)

        ws_out = tf.matmul(a5, w_out)
    return ws_out            
def train(hparams,
          event_dir=None,
          model_dir=None,
          restore_agent=True,
          epoch=0):
    """Train."""
    with tf.name_scope("rl_train"):
        train_summary_op, _, initialization = define_train(hparams)
        if event_dir:
            summary_writer = tf.summary.FileWriter(
                event_dir, graph=tf.get_default_graph(), flush_secs=60)
        if model_dir:
            model_saver = tf.train.Saver(
                tf.global_variables(".*network_parameters.*"))
        else:
            summary_writer = None
            model_saver = None

        # TODO(piotrmilos): This should be refactored, possibly with
        # handlers for each type of env
        if hparams.environment_spec.simulated_env:
            env_model_loader = tf.train.Saver(
                tf.global_variables("next_frame*"))
        else:
            env_model_loader = None

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            initialization(sess)
            if env_model_loader:
                trainer_lib.restore_checkpoint(hparams.world_model_dir,
                                               env_model_loader,
                                               sess,
                                               must_restore=True)
            start_step = 0
            if model_saver and restore_agent:
                start_step = trainer_lib.restore_checkpoint(
                    model_dir, model_saver, sess)

            # Fail-friendly, don't train if already trained for this epoch
            if start_step >= ((hparams.epochs_num * (epoch + 1))):
                tf.logging.info(
                    "Skipping PPO training for epoch %d as train steps "
                    "(%d) already reached", epoch, start_step)
                return

            for epoch_index in range(hparams.epochs_num):
                summary = sess.run(train_summary_op)
                if summary_writer:
                    summary_writer.add_summary(summary, epoch_index)
                if (hparams.eval_every_epochs
                        and epoch_index % hparams.eval_every_epochs == 0):
                    if summary_writer and summary:
                        summary_writer.add_summary(summary, epoch_index)
                    else:
                        tf.logging.info("Eval summary not saved")
                if (model_saver and hparams.save_models_every_epochs and
                    (epoch_index % hparams.save_models_every_epochs == 0 or
                     (epoch_index + 1) == hparams.epochs_num)):
                    ckpt_path = os.path.join(
                        model_dir,
                        "model.ckpt-{}".format(epoch_index + 1 + start_step))
                    model_saver.save(sess, ckpt_path)
Exemplo n.º 40
0
    def __fit_by_tf(self, X, node_id, node_probability, index2type, type2index, type2prob, model_name, model_path,
                    result_path):
        ## Build layers for path2vec
        print('\t>> Building: path2vec layers...')
        logger.info('\t>> Building: path2vec layers...')
        timeref = time.time()
        center_node_holder, context_node_holder, negative_samples_holder, loss = self.__build_tf_place_holders(
            node_probability=node_probability)
        ## Optimization function for path2vec
        optimizer = self.__optimizer(center_node_holder, context_node_holder, negative_samples_holder)

        print('\t\t## Building layers consumed %.2f mintues' % (round((time.time() - timeref) / 60., 3)))
        logger.info('\t\t## Building layers consumed %.2f mintues' % (round((time.time() - timeref) / 60., 3)))
        print('\t>> Training path2vec...')
        logger.info('\t>> Training path2vec...')
        old_cost = np.inf
        timeref = time.time()
        cost_file_name = model_name + "_cost.txt"
        save_data('', file_name=cost_file_name, save_path=result_path, mode='w', w_string=True, print_tag=False)
        merged = tf.summary.merge_all()
        saver = tf.train.Saver(max_to_keep=self.num_models)
        config = tf.ConfigProto(intra_op_parallelism_threads=0,
                                inter_op_parallelism_threads=0,
                                allow_soft_placement=True)
        with tf.Session(config=config) as sess:
            sess.run(tf.global_variables_initializer())
            writer = tf.summary.FileWriter(self.log_path, sess.graph)
            # Define metadata variable.
            run_metadata = tf.RunMetadata()
            for epoch in np.arange(start=1, stop=self.num_epochs + 1):
                desc = '\t   {0:d})- Epoch count ({0:d}/{1:d})...'.format(epoch, self.num_epochs)
                print(desc)
                logger.info(desc)
                self.__shffule(X=X)
                list_batches = np.arange(start=0, stop=len(X), step=self.batch)
                epoch_timeref = time.time()
                new_cost = 0.0
                for idx, batch in enumerate(list_batches):
                    total_samples = (idx + 1) / len(list_batches)
                    desc = '\t       --> Learning: {0:.4f}% ...'.format(total_samples * 100)
                    logger.info(desc)
                    if (idx + 1) != len(list_batches):
                        print(desc, end="\r")
                    if (idx + 1) == len(list_batches):
                        print(desc)
                    ## Generate batch negative samples
                    center_nodes, context_nodes = self.__generate_batch(X=X[batch:batch + self.batch])
                    negative_nodes = self.__get_negative_samples(center_nodes=center_nodes, node_id=node_id,
                                                                 node_probability=node_probability,
                                                                 index2type=index2type, type2index=type2index,
                                                                 type2probs=type2prob)
                    batch_X_size = self.batch
                    if self.batch > 150000:
                        batch_X_size = 10000

                    list_batch_X = np.arange(start=0, stop=center_nodes.shape[0], step=batch_X_size)
                    for b_idx, batch_X_idx in enumerate(list_batch_X):
                        center_batch = center_nodes[batch_X_idx:batch_X_idx + batch_X_size]
                        context_batch = context_nodes[batch_X_idx:batch_X_idx + batch_X_size]
                        negative_batch = negative_nodes[batch_X_idx:batch_X_idx + batch_X_size]
                        for inner_iterations in np.arange(self.max_inner_iter):
                            feed_dict = {center_node_holder: center_batch,
                                         context_node_holder: context_batch,
                                         negative_samples_holder: negative_batch}
                            # We perform one update step by evaluating the optimizer op (including it
                            # in the list of returned values for session.run()
                            # Also, evaluate the merged op to get all summaries from the returned
                            # "summary" variable. Feed metadata variable to session for visualizing
                            # the graph in TensorBoard.
                            loss_batch, _, summary_str = sess.run([loss, optimizer, merged],
                                                                  feed_dict=feed_dict,
                                                                  run_metadata=run_metadata)
                            writer.add_summary(summary_str, inner_iterations)
                            loss_batch /= center_batch.shape[0]
                            new_cost += loss_batch / self.max_inner_iter
                    new_cost /= len(list_batch_X)
                new_cost /= len(list_batches)
                new_cost = new_cost * -1
                self.is_fit = True
                print('\t\t  ## Epoch {0} took {1} seconds...'.format(epoch, round(time.time() - epoch_timeref, 3)))
                logger.info(
                    '\t\t  ## Epoch {0} took {1} seconds...'.format(epoch, round(time.time() - epoch_timeref, 3)))
                data = str(epoch) + '\t' + str(round(time.time() - epoch_timeref, 3)) + '\t' + str(new_cost) + '\n'
                save_data(data=data, file_name=cost_file_name, save_path=result_path, mode='a', w_string=True,
                          print_tag=False)
                # Save models parameters based on test frequencies
                if (epoch % self.display_interval) == 0 or epoch == 1 or epoch == self.num_epochs:
                    print('\t\t  --> New cost: {0:.4f}; Old cost: {1:.4f}'.format(new_cost, old_cost))
                    logger.info('\t\t  --> New cost: {0:.4f}; Old cost: {1:.4f}'.format(new_cost, old_cost))
                    if new_cost < old_cost or epoch == self.num_epochs:
                        old_cost = new_cost
                        tag_final_file = "_tf.ckpt"
                        tag_final_embeddings = "_tf_embeddings.npz"
                        if epoch == self.num_epochs:
                            tag_final_file = "_final_tf.ckpt"
                            tag_final_embeddings = "_final_tf_embeddings.npz"

                        print('\t\t  --> Storing the path2vec model to: {0:s}'.format(model_name + tag_final_file))
                        logger.info(
                            '\t\t  --> Storing the path2vec model to: {0:s}'.format(model_name + tag_final_file))
                        saver.save(sess, os.path.join(model_path, model_name + tag_final_file))

                        print('\t\t  --> Storing the path2vec node embeddings as numpy array to: {0:s}'.format(
                            model_name + tag_final_embeddings))
                        logger.info('\t\t  --> Storing the path2vec node embeddings as numpy array to: {0:s}'.format(
                            model_name + tag_final_embeddings))
                        model_embeddings = tf.get_default_graph()
                        model_embeddings = model_embeddings.get_tensor_by_name("embeddings/embedding_matrix:0")
                        # Create a configuration for visualizing embeddings with the selected_pathways in TensorBoard.
                        # TODO: comment this
                        config = projector.ProjectorConfig()
                        embedding_conf = config.embeddings.add()
                        embedding_conf.tensor_name = model_embeddings.name
                        ##
                        model_embeddings = sess.run(model_embeddings)
                        np.savez(os.path.join(model_path, model_name + tag_final_embeddings), model_embeddings)
                        # TODO: comment this
                        embedding_conf.metadata_path = os.path.join(model_path, model_name + '_metadata.tsv')
                        projector.visualize_embeddings(writer, config)
            writer.close()
            print('\t  --> Training consumed %.2f mintues' % (round((time.time() - timeref) / 60., 3)))
            logger.info('\t  --> Training consumed %.2f mintues' % (round((time.time() - timeref) / 60., 3)))
Exemplo n.º 41
0
    def __init__(self,
                 graph_path,
                 target_size=(320, 240),
                 tf_config=None,
                 trt_bool=False):
        self.target_size = target_size

        # load graph
        logger.info('loading graph from %s(default size=%dx%d)' %
                    (graph_path, target_size[0], target_size[1]))
        with tf.gfile.GFile(graph_path, 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())

        if trt_bool is True:
            output_nodes = ["Openpose/concat_stage7"]
            graph_def = trt.create_inference_graph(
                graph_def,
                output_nodes,
                max_batch_size=1,
                max_workspace_size_bytes=1 << 20,
                precision_mode="FP16",
                # precision_mode="INT8",
                minimum_segment_size=3,
                is_dynamic_op=True,
                maximum_cached_engines=int(1e3),
                # use_calibration=True,
            )

        self.graph = tf.get_default_graph()
        tf.import_graph_def(graph_def, name='TfPoseEstimator')
        self.persistent_sess = tf.Session(graph=self.graph, config=tf_config)

        for ts in [n.name for n in tf.get_default_graph().as_graph_def().node]:
            print(ts)

        self.tensor_image = self.graph.get_tensor_by_name(
            'TfPoseEstimator/image:0')
        self.tensor_output = self.graph.get_tensor_by_name(
            'TfPoseEstimator/Openpose/concat_stage7:0')
        self.tensor_heatMat = self.tensor_output[:, :, :, :19]
        self.tensor_pafMat = self.tensor_output[:, :, :, 19:]
        self.upsample_size = tf.placeholder(dtype=tf.int32,
                                            shape=(2, ),
                                            name='upsample_size')
        self.tensor_heatMat_up = tf.image.resize_area(
            self.tensor_output[:, :, :, :19],
            self.upsample_size,
            align_corners=False,
            name='upsample_heatmat')
        self.tensor_pafMat_up = tf.image.resize_area(
            self.tensor_output[:, :, :, 19:],
            self.upsample_size,
            align_corners=False,
            name='upsample_pafmat')
        if trt_bool is True:
            smoother = Smoother({'data': self.tensor_heatMat_up}, 25, 3.0, 19)
        else:
            smoother = Smoother({'data': self.tensor_heatMat_up}, 25, 3.0)
        gaussian_heatMat = smoother.get_output()

        max_pooled_in_tensor = tf.nn.pool(gaussian_heatMat,
                                          window_shape=(3, 3),
                                          pooling_type='MAX',
                                          padding='SAME')
        self.tensor_peaks = tf.where(
            tf.equal(gaussian_heatMat, max_pooled_in_tensor), gaussian_heatMat,
            tf.zeros_like(gaussian_heatMat))

        self.heatMat = self.pafMat = None

        # warm-up
        self.persistent_sess.run(
            tf.variables_initializer([
                v for v in tf.global_variables() if v.name.split(':')[0] in [
                    x.decode('utf-8') for x in self.persistent_sess.run(
                        tf.report_uninitialized_variables())
                ]
            ]))
        self.persistent_sess.run(
            [self.tensor_peaks, self.tensor_heatMat_up, self.tensor_pafMat_up],
            feed_dict={
                self.tensor_image: [
                    np.ndarray(shape=(target_size[1], target_size[0], 3),
                               dtype=np.float32)
                ],
                self.upsample_size: [target_size[1], target_size[0]]
            })
        self.persistent_sess.run(
            [self.tensor_peaks, self.tensor_heatMat_up, self.tensor_pafMat_up],
            feed_dict={
                self.tensor_image: [
                    np.ndarray(shape=(target_size[1], target_size[0], 3),
                               dtype=np.float32)
                ],
                self.upsample_size: [target_size[1] // 2, target_size[0] // 2]
            })
        self.persistent_sess.run(
            [self.tensor_peaks, self.tensor_heatMat_up, self.tensor_pafMat_up],
            feed_dict={
                self.tensor_image: [
                    np.ndarray(shape=(target_size[1], target_size[0], 3),
                               dtype=np.float32)
                ],
                self.upsample_size: [target_size[1] // 4, target_size[0] // 4]
            })

        # logs
        if self.tensor_image.dtype == tf.quint8:
            logger.info('quantization mode enabled.')
Exemplo n.º 42
0
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_,
                                                            logits=y_conv)
cross_entropy = tf.reduce_mean(cross_entropy)

with tf.name_scope('adam_optimizer'):
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

with tf.name_scope('accuracy'):
    correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
    correct_prediction = tf.cast(correct_prediction, tf.float32)
accuracy = tf.reduce_mean(correct_prediction)

graph_location = logdir
print('Saving graph to: %s' % graph_location)
train_writer = tf.summary.FileWriter(graph_location)
train_writer.add_graph(tf.get_default_graph())

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    zipped_data = zip(data_sets['images_train'], data_sets['labels_train'])
    batches = data_helpers.gen_batch(list(zipped_data), FLAGS.batch_size,
                                     FLAGS.max_steps)

    for i in range(FLAGS.max_steps):
        batch = next(batches)

        images_batch, labels_batch = zip(*batch)

        # reshape
        labels_batch_reshape = np.zeros((len(labels_batch), CLASSES))
#!/usr/bin/env python
# coding:utf8
# @Date    : 2018-04-24 09:15:31
# @Author  : Koon
# @Link    : zpkoon.xyz
# When I wrote this, only God and I understood what I was doing. Now, God only knows.

import tensorflow as tf

saver = tf.train.import_meta_graph("./models/model.ckpt.meta")
with tf.Session() as sess:
    saver.restore(sess, "./models/model.ckpt")
    # 通过张量的名称来获取张量
    print(sess.run(tf.get_default_graph().get_tensor_by_name("add:0")))
Exemplo n.º 44
0
def main(argv):
    print("Evaluating the model ...")
    config = Config()
    config.beam_size = FLAGS.beam_size
    config.phase = 'eval'
    if not os.path.exists(config.eval_result_dir):
        os.mkdir(config.eval_result_dir)
    print("Building the vocabulary...")
    vocabulary = Vocabulary(config.vocabulary_size)
    vocabulary.load(config.vocabulary_file)
    print("Vocabulary built.")
    print("Number of words = %d" %(vocabulary.size))

    
    eval_data = DataProvider(config)
    eval_gt_coco = eval_data.returncoco()
    model = ShowAttendTell(config)
    model.build()

    with tf.Session() as sess:
        model.setup_graph_from_checkpoint(sess, config.caption_checkpoint_dir)
        tf.get_default_graph().finalize()

        captiongen = CaptionGenerator(model,
                                   vocabulary,
                                   config.beam_size,
                                   config.max_caption_length,
                                   config.batch_size)
        
        # Generate the captions for the images
        results = []
        idx = 0
        for k in tqdm(list(range(eval_data.num_batches)), desc='batch'):
            batch,images = eval_data.next_batch_and_images()
            caption_data = captiongen.beam_search(sess, images,vocabulary)

            fake_cnt = 0 if k<eval_data.num_batches-1 \
                         else eval_data.fake_count
            for l in range(eval_data.batch_size-fake_cnt):
                word_idxs = caption_data[l][0].sentence
                score = caption_data[l][0].score
                caption = vocabulary.get_sentence(word_idxs)
                results.append({'image_id': eval_data.image_ids[idx],
                                'caption': caption})
                idx += 1

                # Save the result in an image file, if requested
                if config.save_eval_result_as_image:
                    image_file = batch[l]
                    image_name = image_file.split(os.sep)[-1]
                    image_name = os.path.splitext(image_name)[0]
                    img = plt.imread(image_file)
                    plt.switch_backend('agg')
                    plt.imshow(img)
                    plt.axis('off')
                    plt.title(caption)
                    plt.savefig(os.path.join(config.eval_result_dir,
                                             image_name+'_result.jpg'))

        fp = open(config.eval_result_file, 'wb')
        json.dump(results, fp)
        fp.close()

        # Evaluate these captions
        eval_result_coco = eval_gt_coco.loadRes(config.eval_result_file)
        scorer = COCOEvalCap(eval_gt_coco, eval_result_coco)
        scorer.evaluate()
    print("Evaluation complete.")
Exemplo n.º 45
0
import os
import random
import tensorflow as tf

# In[2]:

# Get images
X = []
for filename in os.listdir('Train/'):
    X.append(img_to_array(load_img('Train/' + filename)))
X = np.array(X, dtype=float)
Xtrain = 1.0 / 255 * X

#Load weights
inception = InceptionResNetV2(weights='imagenet', include_top=True)
inception.graph = tf.get_default_graph()

# In[4]:

embed_input = Input(shape=(1000, ))

#Encoder
encoder_input = Input(shape=(
    256,
    256,
    1,
))
encoder_output = Conv2D(64, (3, 3),
                        activation='relu',
                        padding='same',
                        strides=2)(encoder_input)
Exemplo n.º 46
0
import numpy as np
import random
import os
import tensorflow as tf
from keras import backend as K

# fix the data generation seed for reproducibility
random.seed(1)
np.random.seed(1)

session_conf = tf.ConfigProto(intra_op_parallelism_threads=1,
                              inter_op_parallelism_threads=1)

tf.set_random_seed(1)

sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)


def get_training_samples(n_features, n_samples=100, upper_bound=100):
    temp = []
    samples = []
    target = []
    if not os.path.exists('test_dataset.txt') and not os.path.exists(
            'test_dataset_labels.txt'):
        for i in range(n_samples):
            input_seq = [
                random.randint(0, upper_bound) for _ in range(n_features)
            ]
            output_seq = np.sum(input_seq)
            samples.append(input_seq)
Exemplo n.º 47
0
def dynamic_decode(decoder,
                   impute_finished=False,
                   maximum_iterations=None,
                   parallel_iterations=32,
                   swap_memory=False,
                   scope=None):
    """Perform dynamic decoding with `decoder`.

  Calls initialize() once and step() repeatedly on the Decoder object.

  Args:
    decoder: A `Decoder` instance.
    impute_finished: Python boolean.  If `True`, then states for batch
      entries which are marked as finished get copied through and the
      corresponding outputs get zeroed out.  This causes some slowdown at
      each time step, but ensures that the final state and outputs have
      the correct values and that backprop ignores time steps that were
      marked as finished.
    maximum_iterations: `int32` scalar, maximum allowed number of decoding
       steps.  Default is `None` (decode until the decoder is fully done).
    parallel_iterations: Argument passed to `tf.while_loop`.
    swap_memory: Argument passed to `tf.while_loop`.
    scope: Optional variable scope to use.

  Returns:
    `(final_outputs, final_state, final_sequence_lengths)`.

  Raises:
    TypeError: if `decoder` is not an instance of `Decoder`.
    ValueError: if `maximum_iterations` is provided but is not a scalar.
  """
    if not isinstance(decoder, Decoder):
        raise TypeError("Expected decoder to be type Decoder, but saw: %s" %
                        type(decoder))

    with tf.variable_scope(scope, "decoder") as varscope:
        # Determine context types.
        ctxt = tf.get_default_graph()._get_control_flow_context()  # pylint: disable=protected-access
        is_xla = control_flow_util.GetContainingXLAContext(ctxt) is not None
        in_while_loop = (control_flow_util.GetContainingWhileContext(ctxt)
                         is not None)
        # Properly cache variable values inside the while_loop.
        # Don't set a caching device when running in a loop, since it is possible
        # that train steps could be wrapped in a tf.while_loop. In that scenario
        # caching prevents forward computations in loop iterations from re-reading
        # the updated weights.
        if not tf.executing_eagerly() and not in_while_loop:
            if varscope.caching_device is None:
                varscope.set_caching_device(lambda op: op.device)

        if maximum_iterations is not None:
            maximum_iterations = tf.convert_to_tensor(
                maximum_iterations, dtype=tf.int32, name="maximum_iterations")
            if maximum_iterations.get_shape().ndims != 0:
                raise ValueError("maximum_iterations must be a scalar")

        initial_finished, initial_inputs, initial_state = decoder.initialize()

        zero_outputs = _create_zero_outputs(decoder.output_size,
                                            decoder.output_dtype)

        if is_xla and maximum_iterations is None:
            raise ValueError(
                "maximum_iterations is required for XLA compilation.")
        if maximum_iterations is not None:
            initial_finished = tf.logical_or(initial_finished,
                                             0 >= maximum_iterations)
        initial_sequence_lengths = tf.zeros_like(initial_finished,
                                                 dtype=tf.int32)
        initial_time = tf.constant(0, dtype=tf.int32)

        dynamic_size = maximum_iterations is None or not is_xla

        def _create_ta(s, d):
            return tf.TensorArray(
                dtype=d,
                size=0 if dynamic_size else maximum_iterations,
                dynamic_size=dynamic_size,
                element_shape=s)

        initial_outputs_ta = tf.contrib.framework.nest.map_structure(
            _create_ta, decoder.output_size, decoder.output_dtype)

        def condition(unused_time, unused_outputs_ta, unused_state,
                      unused_inputs, finished, unused_sequence_lengths):
            return tf.logical_not(tf.reduce_all(finished))

        def body(time, outputs_ta, state, inputs, finished, sequence_lengths):
            """Internal while_loop body.

      Args:
        time: scalar int32 tensor.
        outputs_ta: structure of TensorArray.
        state: (structure of) state tensors and TensorArrays.
        inputs: (structure of) input tensors.
        finished: bool tensor (keeping track of what's finished).
        sequence_lengths: int32 tensor (keeping track of time of finish).

      Returns:
        `(time + 1, outputs_ta, next_state, next_inputs, next_finished,
          next_sequence_lengths)`.
        ```
      """
            (next_outputs, decoder_state, next_inputs,
             decoder_finished) = decoder.step(time, inputs, state)
            if decoder.tracks_own_finished:
                next_finished = decoder_finished
            else:
                next_finished = tf.logical_or(decoder_finished, finished)
            next_sequence_lengths = tf.where(
                tf.logical_not(finished),
                tf.fill(tf.shape(sequence_lengths), time + 1),
                sequence_lengths)

            tf.contrib.framework.nest.assert_same_structure(
                state, decoder_state)
            tf.contrib.framework.nest.assert_same_structure(
                outputs_ta, next_outputs)
            tf.contrib.framework.nest.assert_same_structure(
                inputs, next_inputs)

            # Zero out output values past finish
            if impute_finished:
                emit = tf.contrib.framework.nest.map_structure(
                    lambda out, zero: tf.where(finished, zero, out),
                    next_outputs, zero_outputs)
            else:
                emit = next_outputs

            # Copy through states past finish
            def _maybe_copy_state(new, cur):
                # TensorArrays and scalar states get passed through.
                if isinstance(cur, tf.TensorArray):
                    pass_through = True
                else:
                    new.set_shape(cur.shape)
                    pass_through = (new.shape.ndims == 0)
                return new if pass_through else tf.where(finished, cur, new)

            if impute_finished:
                next_state = tf.contrib.framework.nest.map_structure(
                    _maybe_copy_state, decoder_state, state)
            else:
                next_state = decoder_state

            outputs_ta = tf.contrib.framework.nest.map_structure(
                lambda ta, out: ta.write(time, out), outputs_ta, emit)
            return (time + 1, outputs_ta, next_state, next_inputs,
                    next_finished, next_sequence_lengths)

        res = tf.while_loop(condition,
                            body,
                            loop_vars=(
                                initial_time,
                                initial_outputs_ta,
                                initial_state,
                                initial_inputs,
                                initial_finished,
                                initial_sequence_lengths,
                            ),
                            parallel_iterations=parallel_iterations,
                            maximum_iterations=maximum_iterations,
                            swap_memory=swap_memory)

        final_outputs_ta = res[1]
        final_state = res[2]
        final_sequence_lengths = res[5]

        final_outputs = tf.contrib.framework.nest.map_structure(
            lambda ta: ta.stack(), final_outputs_ta)

        try:
            final_outputs, final_state = decoder.finalize(
                final_outputs, final_state, final_sequence_lengths)
        except NotImplementedError:
            pass

    return final_state.pred_ids
Exemplo n.º 48
0
def run_inference_for_single_image(graph):
    sess_config = tf.ConfigProto()
    sess_config.intra_op_parallelism_threads = args.num_intra_threads
    sess_config.inter_op_parallelism_threads = args.num_inter_threads
    if not os.environ.get("OMP_NUM_THREADS"):
        os.environ["OMP_NUM_THREADS"] = str(args.num_intra_threads)

    with graph.as_default():
        with tf.Session(config=sess_config) as sess:
            # Get handles to input and output tensors
            tensor_dict = {}
            if (args.evaluate_tensor is None):
                ops = tf.get_default_graph().get_operations()
                all_tensor_names = {
                    output.name
                    for op in ops for output in op.outputs
                }
                for key in [
                        'num_detections', 'detection_boxes',
                        'detection_scores', 'detection_classes'
                ]:
                    tensor_name = key + ':0'
                    if tensor_name in all_tensor_names:
                        tensor_dict[key] = tf.get_default_graph(
                        ).get_tensor_by_name(tensor_name)
            else:
                our_op = tf.get_default_graph().get_operation_by_name(
                    args.evaluate_tensor)
                tensor_names = our_op.outputs
                list_ops = []
                for i, tensor in enumerate(tensor_names):
                    list_ops.append(tensor.name)
                tensor_dict[args.evaluate_tensor] = list_ops

            if (args.timeline is not None):
                run_options = tf.RunOptions(
                    trace_level=tf.RunOptions.FULL_TRACE)
                run_metadata = tf.RunMetadata()
            total_duration = 0
            for index, image_np in images(TEST_IMAGE_PATHS):
                image_tensor = tf.get_default_graph().get_tensor_by_name(
                    'image_tensor:0')

                # Run inference
                start_time = time.time()
                if (args.timeline is not None):
                    output_dict = sess.run(
                        tensor_dict,
                        feed_dict={image_tensor: np.expand_dims(image_np, 0)},
                        options=run_options,
                        run_metadata=run_metadata)
                else:
                    output_dict = sess.run(
                        tensor_dict,
                        feed_dict={image_tensor: np.expand_dims(image_np, 0)})
                step_duration = time.time() - start_time
                if (index > 20):
                    total_duration = total_duration + step_duration

                if (index % 10 == 0 and index > 0):
                    print('Step ' + str(index) + ': ' + str(step_duration) +
                          ' seconds')

                if (args.number_of_steps is not None):
                    if (args.single_image):
                        sys.exit(
                            "single_image and number_of_steps cannot be both enabled!"
                        )
                    elif (index == (args.number_of_steps - 1)):
                        break

                if (args.timeline is not None):
                    trace = timeline.Timeline(
                        step_stats=run_metadata.step_stats)
                    with open(
                            'tl-' + time.strftime("%Y%m%d-%H%M%S") + '-' +
                            args.timeline, 'w') as file:
                        file.write(
                            trace.generate_chrome_trace_format(
                                show_memory=False))

                if (args.evaluate_tensor is not None):
                    for tensor in output_dict[args.evaluate_tensor]:
                        print(tensor.shape)
                    return None, None

                # all outputs are float32 numpy arrays, so convert types as appropriate
                output_dict['num_detections'] = int(
                    output_dict['num_detections'][0])
                output_dict['detection_classes'] = output_dict[
                    'detection_classes'][0].astype(np.uint8)
                output_dict['detection_boxes'] = output_dict[
                    'detection_boxes'][0]
                output_dict['detection_scores'] = output_dict[
                    'detection_scores'][0]

                if (args.print_accuracy):
                    print('num_detections:\n' +
                          str(output_dict['num_detections']))
                    print('detection_classes:\n' +
                          str(output_dict['detection_classes']))
                    print('detection_boxes:\n' +
                          str(output_dict['detection_boxes']))
                    print('detection_scores:\n' +
                          str(output_dict['detection_scores']))

                if 'detection_masks' in output_dict:
                    output_dict['detection_masks'] = output_dict[
                        'detection_masks'][0]

            if index % 10 != 0:
                print('Step ' + str(index) + ': ' + str(step_duration) +
                      ' seconds')
            # for single image option use step duration
            # for multiple images use total duration add 1 becouse index stats with 0 and substract 20 warmup steps
            steps, duration = (1, step_duration) if args.single_image else ((
                index + 1 - 20), total_duration)
            print('Avg. Duration per Step:' + str(duration / steps) +
                  'for processed number of steps: ' + str(steps))

    return output_dict, image_np
    rel_repr = encode_path_elem(rel_seq, rel_len, embd,
                                seq_embedder_params={'name': 'test_embd',
                                                     'with_projection': False},
                                seq_encoder_params={
                                    'module': 'average'
                                })
    rel_repr_lstm = encode_path_elem(rel_seq, rel_len, embd,
                                     name='lstm_encoder',
                                     seq_embedder_params={'name': 'test_embd',
                                                          'with_projection': False},
                                     seq_encoder_params={
                                         'module': 'lstm',
                                         'repr_dim': repr_dim
                                     })

    for op in tf.get_default_graph().get_operations():
        print(str(op.name))
    with tf.train.MonitoredTrainingSession() as sess:
        test_rel_seq_repr = sess.run(rel_repr,
                                     feed_dict={rel_seq: test_rel_seq,
                                                rel_len: test_rel_length})
        print('Output shape', test_rel_seq_repr.shape)
        np.testing.assert_allclose(t1_avg, test_rel_seq_repr[0, 0, :])
        np.testing.assert_allclose(t2_avg, test_rel_seq_repr[0, 1, :])
        np.testing.assert_allclose(t3_avg, test_rel_seq_repr[1, 0, :])
        assert (0 == test_rel_seq_repr[1, 1, :]).all()

        test_rel_seq_repr_lstm = sess.run(rel_repr_lstm,
                                          feed_dict={rel_seq: test_rel_seq,
                                                     rel_len: test_rel_length}
                                          )
Exemplo n.º 50
0
    y = tf.placeholder(tf.float32, (N, 1))
with tf.name_scope("weights"):
    W = tf.Variable(tf.random_normal((1, 1)))
    b = tf.Variable(tf.random_normal((1, )))
with tf.name_scope("prediction"):
    y_pred = tf.matmul(x, W) + b
with tf.name_scope("loss"):
    l = tf.reduce_sum((y - y_pred)**2)
with tf.name_scope("optim"):
    train_op = tf.train.AdamOptimizer(.001).minimize(l)

with tf.name_scope("summaries"):
    tf.summary.scalar("loss", l)
    merged = tf.summary.merge_all()

train_writer = tf.summary.FileWriter('/tmp/lr-train', tf.get_default_graph())

n_steps = 8000
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    # Train model
    for i in range(n_steps):
        feed_dict = {x: x_np, y: y_np}
        _, summary, loss = sess.run([train_op, merged, l], feed_dict=feed_dict)
        print("step %d, loss: %f" % (i, loss))
        train_writer.add_summary(summary, i)

    # Get weights
    w_final, b_final = sess.run([W, b])

    # Make Predictions
Exemplo n.º 51
0
    def __init__(self,
                 nb_clusters,
                 centroids_init=None,
                 nb_tries=10,
                 nb_iterations=10,
                 input_tensor=None,
                 latent_space_tensor=None,
                 beta=None,
                 threshold=2.5,
                 assign_at_end=False):

        self.nb_clusters = nb_clusters
        self.nb_iterations = nb_iterations
        self.nb_tries = nb_tries
        self.latent_space_tensor = latent_space_tensor
        self.beta = beta
        self.assign_at_end = assign_at_end

        if input_tensor is None:
            self.graph = tf.Graph()
        else:
            self.graph = tf.get_default_graph()

        with self.graph.as_default():
            with tf.name_scope('kmeans'):
                if input_tensor is None:
                    # Spectrogram, embeddings
                    # shape = [batch, L , E ]
                    self.X_in = tf.placeholder("float", [None, None, None],
                                               name='Kmeans_input')
                else:
                    self.X_in = input_tensor

                # mean, _ = tf.nn.moments(self.X_in, axes=-1, keep_dims=True)
                x_norm = tf.nn.l2_normalize(self.X_in, axis=-1)
                self.b = tf.shape(x_norm)[0]
                self.X_original = tf.identity(x_norm)
                self.X = tf.expand_dims(x_norm, 1)
                self.X = tf.tile(self.X, [1, self.nb_tries, 1, 1])

                self.L = tf.shape(self.X)[-2]
                self.E = tf.shape(self.X)[-1]
                self.X = tf.reshape(self.X, [-1, self.L, self.E])

                self.B = tf.shape(self.X)[0]

                self.ones = tf.ones_like(self.X, tf.float32)

                self.shifting = tf.tile(
                    tf.expand_dims(tf.range(self.B) * self.nb_clusters, 1),
                    [1, self.L])

                if centroids_init is None:

                    def random_without_replace(b, l):
                        a = np.array([
                            np.random.choice(range(l),
                                             size=self.nb_clusters,
                                             replace=False) for _ in range(b)
                        ])
                        return a.astype(np.int32)

                    y = tf.py_func(random_without_replace, [self.B, self.L],
                                   tf.int32)
                    random = tf.reshape(y, [self.B, self.nb_clusters, 1])

                    # Take randomly 'nb_clusters' vectors from X
                    batch_range = tf.tile(
                        tf.reshape(tf.range(self.B, dtype=tf.int32),
                                   shape=[self.B, 1, 1]),
                        [1, self.nb_clusters, 1])
                    indices = tf.concat([batch_range, random], axis=2)
                    self.centroid_init = tf.gather_nd(self.X, indices)
                else:
                    self.centroids = tf.identity(centroids_init)
                    self.centroids = tf.tile(self.centroids,
                                             [self.nb_tries, 1, 1])

                if not self.latent_space_tensor is None:
                    latent_space_tensor = tf.reshape(latent_space_tensor,
                                                     [self.b, self.L])
                    log_lst = log10(
                        tf.divide(
                            tf.reduce_max(latent_space_tensor, [-1],
                                          keep_dims=True),
                            latent_space_tensor))
                    self.notsilent_notry = tf.reshape(
                        tf.cast(log_lst < threshold, tf.float32),
                        [self.b, self.L, 1])
                    self.notsilent = tf.tile(self.notsilent_notry,
                                             [self.nb_tries, 1, 1])
                else:
                    self.notsilent = tf.ones([self.B, self.L, 1])

                self.network
def main(args):

    start = time.time()
    img_ = cv2.imread(args.image_file)

    mt = mtcnn(160)
    img_ = img_[:, :, ::-1]  # BGR转换为RGB
    image1, rectangles_ = mt.detectFace(img_)
    print(rectangles_)
    image1 = prewhiten(image1[0])
    print(image1)

    with tf.Graph().as_default():

        with tf.Session() as sess:
            # Load the model
            facenet.load_model(args.model)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name(
                "input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name(
                "embeddings:0")
            phase_train_placeholder = tf.get_default_graph(
            ).get_tensor_by_name("phase_train:0")

            cap = cv2.VideoCapture(0)
            print(image1.shape)
            image1 = np.expand_dims(image1, axis=0)
            print(image1.shape)
            cnt = 0
            while (1):
                start1 = int(round(time.time() * 1000))  # ms
                ret, img = cap.read()
                #cv2.imshow("333", img)
                #cv2.waitKey(0)
                img = img[:, :, ::-1]  # BGR转换为RGB
                image2, rectangles = mt.detectFace(img)
                # 再折腾回来
                img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

                if len(image1) == 0 or len(image2) == 0:
                    print("image1 or image2 is empty!")
                    cv2.imshow("face", img)
                    cv2.waitKey(10)
                    continue

                for i in range(len(image2)):
                    image2_ = copy.deepcopy(prewhiten(image2[i]))
                    _image2 = np.expand_dims(image2_, axis=0)
                    images = np.concatenate((image1, _image2), axis=0)
                    # Run forward pass to calculate embeddings
                    feed_dict = {
                        images_placeholder: images,
                        phase_train_placeholder: False
                    }
                    emb = sess.run(embeddings, feed_dict=feed_dict)
                    dist = np.sqrt(
                        np.sum(np.square(np.subtract(emb[0, :], emb[1, :]))))
                    sim = calculSimilar(emb[0, :], emb[1, :])
                    print("相似度: ", sim)
                    print("Distance: ", dist)
                    cv2.rectangle(
                        img, (int(rectangles[i][0]), int(rectangles[i][1])),
                        (int(rectangles[i][2]), int(rectangles[i][3])),
                        (255, 0, 0), 1)
                    cv2.putText(
                        img, 'Similar: ' + str(sim)[:5],
                        (int(rectangles[i][0]) + 5, int(rectangles[i][1]) + 5),
                        cv2.FONT_HERSHEY_COMPLEX, 2, (0, 255, 0), 2)
                cv2.imshow("face", img)
                if cv2.waitKey(10) and 0xFF == ord('q'):
                    break
                start2 = int(round(time.time() * 1000))
                print("cost time(ms): ", (start2 - start1))
                print("fps: ", 1000.0 / (start2 - start1))
            cap.release()
            cv2.destroyAllWindows()
def graph_create(graphpath):
    with tf.gfile.FastGFile(graphpath, 'rb') as graphfile:
        graphdef = tf.GraphDef()
        graphdef.ParseFromString(graphfile.read())

        return tf.import_graph_def(graphdef, name='', return_elements=[])


graph_create("ssdlite_mobilenet_v2_coco_2018_05_09/frozen_inference_graph.pb")
save_dir = ('output')
if not os.path.exists(save_dir):
    os.mkdir(save_dir)

with tf.Session() as sess:
    tensors = [tensor for tensor in tf.get_default_graph().as_graph_def().node]
    for t in tensors:
        if ('weights' in t.name \
               or 'bias' in t.name \
               or 'moving_variance' in t.name \
               or 'moving_mean' in t.name \
               or 'beta' in t.name \
               or 'gamma' in t.name \
               or 'BatchNorm/batchnorm/sub' in t.name \
               or 'BatchNorm/batchnorm/mul' in t.name) \
               and ('sub' in t.name or 'mul/' in t.name or 'read' in t.name):
            ts = tf.get_default_graph().get_tensor_by_name(t.name + ":0")
            data = ts.eval()
            #print(t.name)
            names = t.name.split('/')
            layer_name = None
Exemplo n.º 54
0
def main(args):  #输入的数据?
    train_X, train_y_1, train_y_2, test_X, test_y_1, test_y_2 = load_data(
    )  #加载数据集

    m = train_X.shape[0]  #输入的训练数据x的个数
    n_output_1 = test_y_1.shape[1]  #所属的类的维度(有多少个类)
    n_output_2 = test_y_2.shape[1]

    lr = args.lr  #未知,args并不知道是什么东西
    n_epoch = args.n_epoch
    n_batch_size = args.n_batch_size
    reg_lambda = args.reg_lambda
    keep_prob = args.keep_prob
    cross_stitch_enabled = args.cross_stitch_enabled

    with tf.variable_scope("placeholder"):  #让变量有相同的命名,包括tf.get_variable得到的变量
        #即括号里面的所有变量都是叫做placeholder
        X = tf.placeholder(tf.float32, (None, 28, 28, 1), "X")
        y_1 = tf.placeholder(tf.float32, (None, n_output_1), "y_1")
        y_2 = tf.placeholder(tf.float32, (None, n_output_2), "y_2")
        is_training = tf.placeholder(tf.bool, (), "is_training")

    with tf.variable_scope("network"):  #构建网络
        #括号里面所有的变量都是叫做network
        with contrib.framework.arg_scope(
            [contrib.layers.fully_connected, slim.layers.conv2d],
                # he initialization
                weights_initializer=contrib.layers.
                variance_scaling_initializer(),
                # l2 regularization
                weights_regularizer=contrib.layers.l2_regularizer(reg_lambda),
                # BN
                normalizer_fn=contrib.layers.batch_norm,
                normalizer_params={
                    "is_training": is_training,
                    "scale": True,
                    "updates_collections": None
                }):
            # (?, 28, 28, 1) -> (?, 28, 28, 32)
            conv1_1 = slim.layers.conv2d(X,
                                         32,
                                         kernel_size=[3, 3],
                                         scope="conv1_1")  #两层卷积两层pooling层
            conv1_2 = slim.layers.conv2d(X,
                                         32,
                                         kernel_size=[3, 3],
                                         scope="conv1_2")

            # (?, 28, 28, 32) -> (?, 14, 14, 32)
            pool1_1 = slim.layers.max_pool2d(conv1_1,
                                             kernel_size=[2, 2],
                                             stride=2,
                                             scope="pool_1_1")
            pool1_2 = slim.layers.max_pool2d(conv1_2,
                                             kernel_size=[2, 2],
                                             stride=2,
                                             scope="pool_1_2")

            # (?, 14, 14, 32) -> (?, 14, 14, 64)
            conv2_1 = slim.layers.conv2d(pool1_1,
                                         64,
                                         kernel_size=[3, 3],
                                         scope="conv2_1")
            conv2_2 = slim.layers.conv2d(pool1_2,
                                         64,
                                         kernel_size=[3, 3],
                                         scope="conv2_2")

            # (?, 14, 14, 64) -> (?, 7, 7, 64)
            pool2_1 = slim.layers.max_pool2d(conv2_1,
                                             kernel_size=[2, 2],
                                             stride=2,
                                             scope="pool_2_1")
            pool2_2 = slim.layers.max_pool2d(conv2_2,
                                             kernel_size=[2, 2],
                                             stride=2,
                                             scope="pool_2_2")

            # (?, 7, 7, 64) -> (?, 3136) -> -> (?, 1024)
            with tf.variable_scope("fc_3_1"):  #构建全连接层1
                flatten_1 = contrib.layers.flatten(pool2_1)
                print("fully")
                print(flatten_1.shape)
                fc_3_1 = contrib.layers.fully_connected(flatten_1, 1024)
            with tf.variable_scope("fc_3_2"):  #构建全连接层2
                flatten_2 = contrib.layers.flatten(pool2_2)
                fc_3_2 = contrib.layers.fully_connected(flatten_2, 1024)

            dropout_1 = contrib.layers.dropout(fc_3_1,
                                               keep_prob=keep_prob,
                                               is_training=is_training,
                                               scope="dropout_1")
            dropout_2 = contrib.layers.dropout(fc_3_2,
                                               keep_prob=keep_prob,
                                               is_training=is_training,
                                               scope="dropout_2")

            output_1 = contrib.layers.fully_connected(dropout_1,
                                                      n_output_1,
                                                      activation_fn=None,
                                                      scope="output_1")
            output_2 = contrib.layers.fully_connected(dropout_2,
                                                      n_output_2,
                                                      activation_fn=None,
                                                      scope="output_2")

    with tf.variable_scope("loss"):  #构建损失函数,分别为任务1和任务2的损失函数
        loss_base_1 = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(labels=y_1,
                                                    logits=output_1))
        loss_base_2 = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(labels=y_2,
                                                    logits=output_2))
        reg_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
        loss_total = loss_base_1 + loss_base_2 + tf.reduce_sum(reg_losses)

    with tf.variable_scope("evaluation"):
        accuracy_1 = tf.reduce_mean(tf.cast(
            tf.equal(tf.argmax(output_1, axis=-1), tf.argmax(y_1, axis=-1)),
            tf.float32),
                                    name="accuracy_1")
        accuracy_2 = tf.reduce_mean(tf.cast(
            tf.equal(tf.argmax(output_2, axis=-1), tf.argmax(y_2, axis=-1)),
            tf.float32),
                                    name="accuracy_2")
        accuracy = tf.divide(accuracy_1 + accuracy_2, 2.0, name="accuracy")

    with tf.variable_scope("train"):
        global_step = tf.get_variable("global_step",
                                      shape=(),
                                      dtype=tf.int32,
                                      trainable=False)
        train_op = tf.train.AdamOptimizer(learning_rate=lr).minimize(
            loss_total, global_step=global_step)

    with tf.variable_scope("summary"):
        summary_loss_total = tf.summary.scalar("loss_total", loss_total)
        summary_accuracy_test = tf.summary.scalar("accuracy_test", accuracy)
        summary_accuracy_train = tf.summary.scalar("accuracy_train", accuracy)

    # standardization
    train_X_reshaped = train_X.reshape([train_X.shape[0], -1])
    train_X_means = np.mean(train_X_reshaped, axis=0, keepdims=True)
    train_X_stds = np.std(train_X_reshaped, axis=0, keepdims=True)

    def standardization(x):
        x_reshaped = x.reshape([x.shape[0], -1])
        result = (x_reshaped - train_X_means) / (train_X_stds + 1e-9)
        return result.reshape(x.shape)

    normalized_test_X = standardization(test_X)

    with tf.Session() as sess, tf.summary.FileWriter(
            "./tf_logs/independently/result",
            graph=tf.get_default_graph()) as f:
        #+ str(datetime.now().timestamp()),

        sess.run(tf.global_variables_initializer())

        # similar logic as mnist's next_batch()
        epoch = 0
        index_in_epoch = 0
        while epoch < n_epoch:
            for _ in range(
                    m // n_batch_size +
                    1):  #get the integer part of a result from a division
                start = index_in_epoch
                if start + n_batch_size > m:
                    epoch += 1
                    n_rest_data = m - start
                    train_X_batch_rest = train_X[start:m]
                    train_y_batch_rest_1 = train_y_1[start:m]
                    train_y_batch_rest_2 = train_y_2[start:m]
                    # Shuffle train data
                    perm = np.arange(m)
                    np.random.shuffle(perm)
                    train_X = train_X[perm]
                    train_y_1 = train_y_1[perm]
                    train_y_2 = train_y_2[perm]
                    # Start next epoch
                    start = 0
                    index_in_epoch = n_batch_size - n_rest_data
                    end = index_in_epoch
                    train_X_batch_new = train_X[start:end]
                    train_y_batch_new_1 = train_y_1[start:end]
                    train_y_batch_new_2 = train_y_2[start:end]
                    # concatenate
                    train_X_batch = np.concatenate(
                        (train_X_batch_rest, train_X_batch_new), axis=0)
                    train_y_batch_1 = np.concatenate(
                        (train_y_batch_rest_1, train_y_batch_new_1), axis=0)
                    train_y_batch_2 = np.concatenate(
                        (train_y_batch_rest_2, train_y_batch_new_2), axis=0)
                else:
                    index_in_epoch += n_batch_size
                    end = index_in_epoch
                    train_X_batch = train_X[start:end]
                    train_y_batch_1 = train_y_1[start:end]
                    train_y_batch_2 = train_y_2[start:end]

                _, global_step_value, loss_total_value, summary_loss_total_value = \
                    sess.run([train_op, global_step, loss_total, summary_loss_total],
                             feed_dict={X: standardization(train_X_batch),
                                        y_1: train_y_batch_1,
                                        y_2: train_y_batch_2,
                                        is_training: True})

                if global_step_value % 100 == 0:
                    accuracy_train_value, summary_accuracy_train_value = \
                        sess.run([accuracy, summary_accuracy_train],
                                 feed_dict={X: standardization(train_X_batch),
                                            y_1: train_y_batch_1,
                                            y_2: train_y_batch_2,
                                            is_training: False})
                    accuracy_test_value, summary_accuracy_test_value = \
                        sess.run([accuracy, summary_accuracy_test],
                                 feed_dict={X: normalized_test_X,
                                            y_1: test_y_1,
                                            y_2: test_y_2,
                                            is_training: False})

                    print(global_step_value, epoch, loss_total_value,
                          accuracy_train_value, accuracy_test_value)
                    # cross_stitches = tf.get_collection("cross_stitches")
                    # print(cross_stitches[0].eval(sess))

                    f.add_summary(summary_loss_total_value,
                                  global_step=global_step_value)
                    f.add_summary(summary_accuracy_train_value,
                                  global_step=global_step_value)
                    f.add_summary(summary_accuracy_test_value,
                                  global_step=global_step_value)
                    f.add_graph(sess.graph)
Exemplo n.º 55
0
def get_optimistic_saver(ckpt_path, graph=tf.get_default_graph()):
    return tf.train.Saver(
        get_optimistic_restore_variables(ckpt_path, graph=graph))
Exemplo n.º 56
0
num_epochs = 1
size_models = 16
activations = "tanh"
rate = 5
result_file_path = 'result_encoder_decoder.csv'

nor_data, amax, amin = data.get_goodletrace_data(path, aspects)
x_train_encoder, y_train, x_test_encoder, y_test = data.get_data_samples(
    nor_data, n_slidings_encoder, predicted_aspect, rate)
x_train_decoder, x_test_decoder = data.get_data_decoder(
    x_train_encoder, x_test_encoder, n_slidings_decoder)
x_train_encoder, x_train_decoder, y_train, x_val_encoder, x_val_decoder, y_val = \
    data.getValidationSet(x_train_encoder, x_train_decoder, y_train, 5)

sess = tf.Session()
path = 'model_encoder_decoder'
encoder_saver = tf.train.import_meta_graph(path + '.meta')
encoder_graph = tf.get_default_graph()
X_encoder = encoder_graph.get_tensor_by_name('X_encoder:0')
outputs_encoder = encoder_graph.get_tensor_by_name('outputs_encoder:0')
last_output_encoder = outputs_encoder[:, -1, :]
print(last_output_encoder.shape, outputs_encoder.shape)
last_output_encoder = tf.reshape(last_output_encoder,
                                 (-1, last_output_encoder.shape[1]))
print(last_output_encoder.shape)
sliding_encoder = outputs_encoder.shape[1]
encoder_saver.restore(sess=sess, save_path=path)

outputs_encoder = sess.run(outputs_encoder,
                           feed_dict={X_encoder: x_train_encoder})
print(outputs_encoder.shape)
Exemplo n.º 57
0
import numpy as np
import tensorflow as tf
from shared import load_faces, reset_graph


_, X_test, _, y_test = load_faces()

reset_graph()

saver = tf.train.import_meta_graph("./models/cnn/cnn.ckpt.meta")
X = tf.get_default_graph().get_tensor_by_name("input/X:0")
y = tf.get_default_graph().get_tensor_by_name("input/y:0")
logits = tf.get_default_graph().get_tensor_by_name("cnn/outputs/dense/BiasAdd:0")

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    saver.restore(sess, "./models/cnn/cnn.ckpt")

    batch_size = 128
    y_pred = np.zeros(len(X_test))
    total_batch = len(X_test) // batch_size

    for i in range(total_batch):
        start = i * batch_size
        end = min((i + 1) * batch_size, len(X_test))
        x_batch = X_test[start:end]
        y_batch = y_test[start:end]
        accuracy = sess.run(tf.argmax(logits, axis=1), feed_dict={X: x_batch, y: y_batch})
        y_pred[start:end] = accuracy

    correct = (y_pred == y_test)
Exemplo n.º 58
0
def main(argv=None):
    import os
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu_list

    try:
        os.makedirs(FLAGS.output_dir)
    except OSError as e:
        if e.errno != 17:
            raise

    with tf.get_default_graph().as_default():
        input_images = tf.placeholder(tf.float32,
                                      shape=[None, None, None, 3],
                                      name='input_images')
        global_step = tf.get_variable('global_step', [],
                                      initializer=tf.constant_initializer(0),
                                      trainable=False)

        f_score, f_geometry = model.model(input_images, is_training=False)

        variable_averages = tf.train.ExponentialMovingAverage(
            0.997, global_step)
        saver = tf.train.Saver(variable_averages.variables_to_restore())

        with tf.Session(config=tf.ConfigProto(
                allow_soft_placement=True)) as sess:
            ckpt_state = tf.train.get_checkpoint_state(FLAGS.checkpoint_path)
            model_path = os.path.join(
                FLAGS.checkpoint_path,
                os.path.basename(ckpt_state.model_checkpoint_path))
            print('Restore from {}'.format(model_path))
            saver.restore(sess, model_path)

            im_fn_list = get_images()
            for im_fn in im_fn_list:
                im = cv2.imread(im_fn)[:, :, ::-1]
                start_time = time.time()
                im_resized, (ratio_h, ratio_w) = resize_image(im)

                timer = {'net': 0, 'restore': 0, 'nms': 0}
                start = time.time()
                score, geometry = sess.run(
                    [f_score, f_geometry],
                    feed_dict={input_images: [im_resized]})
                timer['net'] = time.time() - start

                boxes, timer = detect(score_map=score,
                                      geo_map=geometry,
                                      timer=timer)
                print(
                    '{} : net {:.0f}ms, restore {:.0f}ms, nms {:.0f}ms'.format(
                        im_fn, timer['net'] * 1000, timer['restore'] * 1000,
                        timer['nms'] * 1000))

                if boxes is not None:
                    boxes = boxes[:, :8].reshape((-1, 4, 2))
                    boxes[:, :, 0] /= ratio_w
                    boxes[:, :, 1] /= ratio_h

                duration = time.time() - start_time
                print('[timing] {}'.format(duration))

                # save to file
                if boxes is not None:
                    res_file = os.path.join(
                        FLAGS.output_dir,
                        '{}.txt'.format(os.path.basename(im_fn).split('.')[0]))

                    with open(res_file, 'w') as f:
                        for box in boxes:
                            # to avoid submitting errors
                            box = sort_poly(box.astype(np.int32))
                            if np.linalg.norm(box[0] -
                                              box[1]) < 5 or np.linalg.norm(
                                                  box[3] - box[0]) < 5:
                                continue
                            f.write('{},{},{},{},{},{},{},{}\r\n'.format(
                                box[0, 0],
                                box[0, 1],
                                box[1, 0],
                                box[1, 1],
                                box[2, 0],
                                box[2, 1],
                                box[3, 0],
                                box[3, 1],
                            ))
                            cv2.polylines(
                                im[:, :, ::-1],
                                [box.astype(np.int32).reshape((-1, 1, 2))],
                                True,
                                color=(255, 255, 0),
                                thickness=1)
                if not FLAGS.no_write_images:
                    img_path = os.path.join(FLAGS.output_dir,
                                            os.path.basename(im_fn))
                    cv2.imwrite(img_path, im[:, :, ::-1])
Exemplo n.º 59
0
    correct = tf.equal(tf.argmax(predict, axis=1), tf.argmax(labels, axis=1))
    total_correct = tf.reduce_sum(tf.cast(correct, tf.float32))

    grads = tf.train.AdamOptimizer(
        learning_rate=0.01, beta1=0.9, beta2=0.999,
        epsilon=1).compute_gradients(
            loss=loss,
            var_list=[conv1, conv2, conv3, conv4, conv5, fc1, fc2, fc3])

    # optimizer = tf.train.AdamOptimizer(learning_rate=0.01, beta1=0.9, beta2=0.999, epsilon=1).minimize(loss)
    optimizer = tf.train.GradientDescentOptimizer(
        learning_rate=0.01).minimize(loss)

    error = tf.subtract(tf.nn.softmax(fc3), labels)

    get_fc1_weights = tf.get_default_graph().get_tensor_by_name(
        os.path.split(fc1.name)[0] + '/kernel:0')
    get_fc1_bias = tf.get_default_graph().get_tensor_by_name(
        os.path.split(fc1.name)[0] + '/bias:0')

    get_fc2_weights = tf.get_default_graph().get_tensor_by_name(
        os.path.split(fc2.name)[0] + '/kernel:0')
    get_fc2_bias = tf.get_default_graph().get_tensor_by_name(
        os.path.split(fc2.name)[0] + '/bias:0')

    get_fc3_weights = tf.get_default_graph().get_tensor_by_name(
        os.path.split(fc3.name)[0] + '/kernel:0')
    get_fc3_bias = tf.get_default_graph().get_tensor_by_name(
        os.path.split(fc3.name)[0] + '/bias:0')

    get_conv1_weights = tf.get_default_graph().get_tensor_by_name(
        os.path.split(conv1.name)[0] + '/kernel:0')
Exemplo n.º 60
0
def main(_):

    os.environ["CUDA_VISIBLE_DEVICES"] = str(
        FLAGS.gpu)  # ----------which GPU is used-----------

    #modelDir = "./saveModel/model-750"     # "./saveModel/model-750" https://blog.csdn.net/sjtuxx_lee/article/details/82663394 ----------------
    saver = tf.train.import_meta_graph(args.modelDir + ".meta")
    graph = tf.get_default_graph()
    #tensor_name_list = [tensor.name for tensor in graph.as_graph_def().node]

    logits_lab = tf.get_collection(
        'logits_lab_save'
    )[0]  #------------------add for save and load model-------20191127------xiao------------
    logits_ema = tf.get_collection(
        'logits_ema_save'
    )[0]  #------------------add for save and load model-------20191127------xiao------------
    batch_size = tf.get_collection(
        'batch_size_save'
    )[0]  #------------------add for save and load model-------20191127------xiao------------

    #dataDir = args.dataDir  #   csiFile = args.csiFile   #  labelFile = args.labelFile
    (testx, testy) = loadDataTest(args.dataDir, args.csiFile, args.labelFile)

    nr_batches_test = int(testx.shape[0] / batch_size)
    #----if testx.shape[0] modulo  FLAGS.batch_size <> 0, there will be some samples to  be left, here fill some samples to testX------begin------
    #----for outputing full test samples------20191206-----------
    #print(testx.shape[0]/FLAGS.batch_size)
    modNumber = testx.shape[0] % batch_size
    if (modNumber != 0):
        testx2 = testx[0:batch_size - modNumber, :, :, :]
        testy2 = testy[0:batch_size - modNumber]
        #print(testx2.shape)
        testx = np.concatenate((testx, testx2), axis=0)
        testy = np.concatenate((testy, testy2), axis=0)
        #print(testx.shape)
        nr_batches_test += 1

    #----if testx.shape[0] modulo  batch_size <> 0, there will be some samples to  be left, here fill some samples to testX------end--------

    #print("Data:")
    print('test examples %d, batch %d' % (testx.shape[0], nr_batches_test))
    print('histogram test ', np.histogram(testy, bins=10)[0])
    print(testy)

    inp = graph.get_operation_by_name('labeled_data_input_pl').outputs[
        0]  #inp=gragh.get_tensor_by_name('labeled_data_input_pl')
    lbl = graph.get_operation_by_name('lbl_input_pl').outputs[0]
    is_training_pl = graph.get_operation_by_name('is_training_pl').outputs[0]

    correct_pred = tf.equal(tf.cast(tf.argmax(logits_lab, 1), tf.int32),
                            tf.cast(lbl, tf.int32))
    accuracy_classifier = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
    y_predict = tf.cast(tf.argmax(logits_lab, 1), tf.int32)

    correct_pred_ema = tf.equal(tf.cast(tf.argmax(logits_ema, 1), tf.int32),
                                tf.cast(lbl, tf.int32))
    accuracy_ema = tf.reduce_mean(tf.cast(correct_pred_ema, tf.float32))
    y_predict_ema = tf.cast(tf.argmax(logits_ema, 1), tf.int32)

    # --------added for computing entropy------xiao--------20191128-------begin------
    #fopen = open(args.csiFile.replace('.mat','')  + "_entropy", "w")
    #fopen_ema = open(args.csiFile.replace('.mat','')  + "_entropy_ema", "w")
    fopen_entropy_ema = open(
        args.csiFile.replace('.mat', '') + "_entropy_ema", "w")

    softmax_out = tf.nn.softmax(logits_lab, name='softmax_out')
    softmax_out_ema = tf.nn.softmax(logits_ema, name='softmax_out_ema')
    # --------added for computing entropy------xiao--------20191128-------end--------
    acc_all = acc_ema_all = f1_all = f1_ema_all = precsion_all = recall_all = 0
    acc_all2 = acc_ema_all2 = f1_all2 = f1_ema_all2 = 0  #----for outputing full test samples------20191206-----------
    with tf.Session() as sess:
        begin = time.time()
        saver.restore(
            sess, args.modelDir
        )  #saver.restore(sess, tf.train.latest_checkpoint("./saveModel"))
        print('finish loading model!')

        for t in range(nr_batches_test):
            ran_from = t * batch_size
            ran_to = (t + 1) * batch_size
            feed_dict = {
                inp: testx[ran_from:ran_to],
                lbl: testy[ran_from:ran_to],
                is_training_pl: False
            }

            #acc, acc_ema,y_pred,y_pred_ema = sess.run([accuracy_classifier, accuracy_ema,y_predict,y_predict_ema], feed_dict=feed_dict) # correct without entropy
            # --------added for computing entropy------xiao--------20191128-------begin------
            y_true = testy[ran_from:ran_to]

            acc, acc_ema, y_pred, y_pred_ema, softmax_entropy, softmax_entropy_ema = sess.run(
                [
                    accuracy_classifier, accuracy_ema, y_predict,
                    y_predict_ema, softmax_out, softmax_out_ema
                ],
                feed_dict=feed_dict)  # correct without entropy
            #print("softmax_out_entropy:" + str(softmax_out_entropy))
            #----if testx.shape[0] modulo  batch_size <> 0, there will be some samples to  be left, here fill some samples to testX------begin------
            entropyShape = softmax_entropy.shape[0]
            entropyShapeEma = softmax_entropy_ema.shape[0]
            if (modNumber != 0 and t == nr_batches_test - 1):
                entropyShape = modNumber
                entropyShapeEma = modNumber
                y_true = y_true[0:modNumber]
                y_pred = y_pred[0:modNumber]
                y_pred_ema = y_pred_ema[0:modNumber]
            #----if testx.shape[0] modulo  batch_size <> 0, there will be some samples to  be left, here fill some samples to testX------end--------
            '''
            for i in range(entropyShape):
                fopen.write(str(int(y_true[i])) +  "\t" + str(y_pred[i]) + "\t" + '{:.4f}'.format(max(softmax_entropy[i])) + '\t')                 
                fopen.write(str('{:.4f}'.format(entropy(softmax_entropy[i])) ) + '\t')
                for j in range(softmax_entropy.shape[1]):
                    fixFloat = '{:.4f}'.format(softmax_entropy[i][j])
                    fopen.write(str(fixFloat) + "\t")
                fopen.write("\n")
            
            for i in range(entropyShapeEma):
                fopen_ema.write(str(int(y_true[i])) +  "\t" + str(y_pred_ema[i]) + "\t" + '{:.4f}'.format(max(softmax_entropy_ema[i])) + '\t') 
                fopen_ema.write(str('{:.4f}'.format(entropy(softmax_entropy_ema[i])) ) + '\t')
                for j in range(softmax_entropy_ema.shape[1]):
                    fixFloat = '{:.4f}'.format(softmax_entropy_ema[i][j])
                    fopen_ema.write(str(fixFloat) + "\t")
                fopen_ema.write("\n")
            
            #for i in range(entropyShapeEma):
            #    fopen_ema.write(str(int(y_true[i])) +  "\t" + str(y_pred_ema[i]) + "\t" + '{:.4f}'.format(max(softmax_entropy_ema[i])) )                 
            #    fopen_ema.write("\n")            
            '''
            for i in range(entropyShapeEma):
                aa = 1 - entropy(softmax_entropy_ema[i])
                #print(aa)
                if (aa < 0):
                    aa = 0.0001
                #fopen_entropy_ema.write(str('{:.4f}'.format(entropy(softmax_entropy_ema[i])) ) + '\n')
                #fopen_entropy_ema.write(str('{:.4f}'.format(aa) ) + '\n')
                #fopen_entropy_ema.write(str('{:.4f}'.format(max(softmax_entropy_ema[i])) ) + '\n')
            for i in range(entropyShapeEma):
                fopen_entropy_ema.write(
                    str(y_pred_ema[i] + 1) + "\t" +
                    '{:.4f}'.format(max(softmax_entropy_ema[i])) + "\t" +
                    str('{:.4f}'.format(entropy(softmax_entropy_ema[i]))) +
                    "\t" + str(y_pred[i] + 1) + "\t" +
                    '{:.4f}'.format(max(softmax_entropy[i])) + "\t" +
                    str('{:.4f}'.format(entropy(softmax_entropy[i]))) + "\t" +
                    str(y_true[i] + 1) + "\n")

            # --------added for computing entropy------xiao--------20191128-------end--------
            acc2 = metrics.accuracy_score(
                y_true, y_pred
            )  #----for outputing full test samples------20191206-----------
            acc2_ema = metrics.accuracy_score(
                y_true, y_pred_ema
            )  #----for outputing full test samples------20191206-----------
            #print('acc:' + str(acc) + '\t' + 'acc2:' + str(acc2))
            #print('acc_ema:' + str(acc_ema) + '\t' + 'acc2_ema:' + str(acc2_ema))
            f1 = metrics.f1_score(y_true, y_pred,
                                  average="weighted")  # weighted  macro  micro
            f1_ema = metrics.f1_score(y_true, y_pred_ema,
                                      average="weighted")  #
            precsion = metrics.precision_score(y_true, y_pred, average="micro")
            recall = metrics.recall_score(y_true, y_pred, average="micro")
            acc_all2 += acc2  #----for outputing full test samples------20191206-----------
            f1_all2 += f1
            acc_ema_all2 += acc2_ema
            f1_ema_all2 += f1_ema  #----for outputing full test samples------20191206-----------

            if (
                    not (modNumber != 0 and t == nr_batches_test - 1)
            ):  #----for outputing full test samples------20191206-----------
                acc_all += acc  # excluding the last epoch if testx.shape[0]%batch_size <> 0
                acc_ema_all += acc_ema  # to keep the same with train result
                f1_all += f1
                f1_ema_all += f1_ema
                precsion_all += precsion
                recall_all += recall
        nr_batches_test2 = nr_batches_test  #----for outputing full test samples------20191206-----------
        if (modNumber != 0
            ):  #----for outputing full test samples------20191206-----------
            nr_batches_test -= 1
        acc_all2 /= nr_batches_test2  #----for outputing full test samples------20191206-----------
        f1_all2 /= nr_batches_test2  #----for outputing full test samples------20191206-----------
        acc_ema_all2 /= nr_batches_test2  #----for outputing full test samples------20191206-----------
        f1_ema_all2 /= nr_batches_test2  #----for outputing full test samples------20191206-----------

        acc_all /= nr_batches_test
        acc_ema_all /= nr_batches_test
        f1_all /= nr_batches_test
        f1_ema_all /= nr_batches_test
        precsion_all /= nr_batches_test
        recall_all /= nr_batches_test

        #print("%ds testAcc=%.2f testF1=%0.2f testAccE=%0.2f testF1E=%0.2f"
        #      % (time.time()-begin, acc_all*100,f1_all*100,acc_ema_all*100,f1_ema_all*100))
        print(
            "%ds --------NoLastEpoch: testAcc =%.2f testF1 =%0.2f testAccE =%0.2f testF1E =%0.2f \n \
              allData: testAcc2=%0.2f testF12=%0.2f testAcc2E=%0.2f testF12E=%0.2f"
            % (time.time() - begin, acc_all * 100, f1_all * 100,
               acc_ema_all * 100, f1_ema_all * 100, acc_all2 * 100,
               f1_all2 * 100, acc_ema_all2 * 100, f1_ema_all2 * 100))
        # acc_all is the result excluding the last epoch if testx.shape[0]%batch_size <> 0
        # acc_all2 is the result including all the epoches
        fopen_entropy_ema.close()

        #----------normalize entropy-----------------begin---------20191216----------------
        '''