Python tensorflow.InteractiveSession() Examples

The following are 30 code examples of tensorflow.InteractiveSession(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow , or try the search function .
Example #1
Source File: utils.py    From tcav with Apache License 2.0 6 votes vote down vote up
def create_session(timeout=10000, interactive=True):
  """Create a tf session for the model.
  # This function is slight motification of code written by Alex Mordvintsev

  Args:
    timeout: tfutil param.

  Returns:
    TF session.
  """
  graph = tf.Graph()
  config = tf.ConfigProto()
  config.gpu_options.allow_growth = True
  config.operation_timeout_in_ms = int(timeout*1000)
  if interactive:
    return tf.InteractiveSession(graph=graph, config=config)
  else:
    return tf.Session(graph=graph, config=config) 
Example #2
Source File: TestUpd.py    From NTM-One-Shot-TF with MIT License 6 votes vote down vote up
def omniglot():

    sess = tf.InteractiveSession()

    """    def wrapper(v):
        return tf.Print(v, [v], message="Printing v")

    v = tf.Variable(initial_value=np.arange(0, 36).reshape((6, 6)), dtype=tf.float32, name='Matrix')

    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())

    temp = tf.Variable(initial_value=np.arange(0, 36).reshape((6, 6)), dtype=tf.float32, name='temp')
    temp = wrapper(v)
    #with tf.control_dependencies([temp]):
    temp.eval()
    print 'Hello'"""

    def update_tensor(V, dim2, val):  # Update tensor V, with index(:,dim2[:]) by val[:]
        val = tf.cast(val, V.dtype)
        def body(_, (v, d2, chg)):
            d2_int = tf.cast(d2, tf.int32)
            return tf.slice(tf.concat_v2([v[:d2_int],[chg] ,v[d2_int+1:]], axis=0), [0], [v.get_shape().as_list()[0]])
        Z = tf.scan(body, elems=(V, dim2, val), initializer=tf.constant(1, shape=V.get_shape().as_list()[1:], dtype=tf.float32), name="Scan_Update")
        return Z 
Example #3
Source File: function.py    From Gather-Deployment with MIT License 6 votes vote down vote up
def run(self):
        g_emotion = load_graph('emotion.pb')
        x_emotion = g_emotion.get_tensor_by_name('import/Placeholder:0')
        logits_emotion = g_emotion.get_tensor_by_name('import/logits:0')
        sess_emotion = tf.InteractiveSession(graph = g_emotion)

        with open('fast-text-emotion.json') as fopen:
            dict_emotion = json.load(fopen)

        with self.input()['Sentiment'].open('r') as fopen:
            outputs = json.load(fopen)
        for i in range(0, len(outputs), self.batch_size):
            batch_x_text = outputs[i : min(i + self.batch_size, len(outputs))]
            batch_x_text = [t['text'] for t in batch_x_text]
            batch_x_text = [clearstring(t) for t in batch_x_text]
            batch_x = str_idx(batch_x_text, dict_emotion['dictionary'], 100)
            output_emotion = sess_emotion.run(
                logits_emotion, feed_dict = {x_emotion: batch_x}
            )
            labels = [emotion_label[l] for l in np.argmax(output_emotion, 1)]
            for no, label in enumerate(labels):
                outputs[i + no]['emotion_label'] = label

        with self.output().open('w') as fopen:
            fopen.write(json.dumps(outputs)) 
Example #4
Source File: readTFevents.py    From TEGAN with Apache License 2.0 6 votes vote down vote up
def get_tags_from_event(filename):
    sess = tf.InteractiveSession()
    i = 0;
    tags = [];
    with sess.as_default():
        for event in tf.train.summary_iterator(filename):
            if (i==0):
                printed = 0;
                for val in event.summary.value:
                    print(val.tag)
                    tags.append[val.tag]
                    printed = 1
                if (printed):
                    i = 1
            else:
                break;
    return tags 
Example #5
Source File: readTFevents.py    From TEGAN with Apache License 2.0 6 votes vote down vote up
def read_images_data_from_event(filename, tag):
    
    image_str = tf.placeholder(tf.string)
    image_tf = tf.image.decode_image(image_str)

    image_list = [];
    
    sess = tf.InteractiveSession()
    with sess.as_default():
        count = 0
        for event in tf.train.summary_iterator(filename):
            for val in event.summary.value:
                if val.tag == tag:
                    im = image_tf.eval({image_str: val.image.encoded_image_string})
                    image_list.append(im)
                    count += 1

        return image_list 
Example #6
Source File: readTFevents.py    From TEGAN with Apache License 2.0 6 votes vote down vote up
def save_images_from_event(filename, tag, output_dir='./'):
    assert(os.path.isdir(output_dir))

    image_str = tf.placeholder(tf.string)
    image_tf = tf.image.decode_image(image_str)

    sess = tf.InteractiveSession()
    with sess.as_default():
        count = 0
        for event in tf.train.summary_iterator(filename):
            for val in event.summary.value:
                if val.tag == tag:
                    im = image_tf.eval({image_str: val.image.encoded_image_string})
                    output_fn = os.path.realpath('{}/image_{}_{:05d}.png'.format(output_dir, tag, count))
                    print("Saving '{}'".format(output_fn))
                    scipy.misc.imsave(output_fn, im)
                    count += 1
    return 
Example #7
Source File: freeze.py    From adversarial_audio with MIT License 6 votes vote down vote up
def main(_):

  # Create the model and load its weights.
  sess = tf.InteractiveSession()
  create_inference_graph(FLAGS.wanted_words, FLAGS.sample_rate,
                         FLAGS.clip_duration_ms, FLAGS.clip_stride_ms,
                         FLAGS.window_size_ms, FLAGS.window_stride_ms,
                         FLAGS.dct_coefficient_count, FLAGS.model_architecture)
  models.load_variables_from_checkpoint(sess, FLAGS.start_checkpoint)

  # Turn all the variables into inline constants inside the graph and save it.
  frozen_graph_def = graph_util.convert_variables_to_constants(
      sess, sess.graph_def, ['labels_softmax'])
  tf.train.write_graph(
      frozen_graph_def,
      os.path.dirname(FLAGS.output_file),
      os.path.basename(FLAGS.output_file),
      as_text=False)
  tf.logging.info('Saved frozen graph to %s', FLAGS.output_file) 
Example #8
Source File: breakout_a3c.py    From reinforcement-learning with MIT License 6 votes vote down vote up
def __init__(self, action_size):
        # environment settings
        self.state_size = (84, 84, 4)
        self.action_size = action_size

        self.discount_factor = 0.99
        self.no_op_steps = 30

        # optimizer parameters
        self.actor_lr = 2.5e-4
        self.critic_lr = 2.5e-4
        self.threads = 8

        # create model for actor and critic network
        self.actor, self.critic = self.build_model()

        # method for training actor and critic network
        self.optimizer = [self.actor_optimizer(), self.critic_optimizer()]

        self.sess = tf.InteractiveSession()
        K.set_session(self.sess)
        self.sess.run(tf.global_variables_initializer())

        self.summary_placeholders, self.update_ops, self.summary_op = self.setup_summary()
        self.summary_writer = tf.summary.FileWriter('summary/breakout_a3c', self.sess.graph) 
Example #9
Source File: tf_util.py    From stable-baselines with MIT License 6 votes vote down vote up
def make_session(num_cpu=None, make_default=False, graph=None):
    """
    Returns a session that will use <num_cpu> CPU's only

    :param num_cpu: (int) number of CPUs to use for TensorFlow
    :param make_default: (bool) if this should return an InteractiveSession or a normal Session
    :param graph: (TensorFlow Graph) the graph of the session
    :return: (TensorFlow session)
    """
    if num_cpu is None:
        num_cpu = int(os.getenv('RCALL_NUM_CPU', multiprocessing.cpu_count()))
    tf_config = tf.ConfigProto(
        allow_soft_placement=True,
        inter_op_parallelism_threads=num_cpu,
        intra_op_parallelism_threads=num_cpu)
    # Prevent tensorflow from taking all the gpu memory
    tf_config.gpu_options.allow_growth = True
    if make_default:
        return tf.InteractiveSession(config=tf_config, graph=graph)
    else:
        return tf.Session(config=tf_config, graph=graph) 
Example #10
Source File: mnist_classifiers.py    From TensorFlask with Apache License 2.0 6 votes vote down vote up
def __init__(self, model_param_path='model/model_params.npz', max_batch_size=64):
        """Initalized tensorflow session, set up tensorflow graph, and loads
        pretrained model weights

        Arguments:
            model_params (string) : Path to pretrained model parameters
            max_batch_size (int) : Maximum number of images handled in a single
                                   call to classify.
        """
        self.max_batch_size = max_batch_size
        self.model_param_path = model_param_path

        #Setup tensorflow graph and initizalize variables
        self.session = tf.InteractiveSession()
        self.x = tf.placeholder(tf.float32, shape=[None, 28, 28, 1], name='x')
        self.network, self.predictions, self.probabilities = self._load_model_definition()
        self.session.run( tf.global_variables_initializer() )

        #Load saved parametes
        self._load_model_parameters() 
Example #11
Source File: TestUpd.py    From How-to-Learn-from-Little-Data with MIT License 6 votes vote down vote up
def omniglot():

    sess = tf.InteractiveSession()

    """    def wrapper(v):
        return tf.Print(v, [v], message="Printing v")

    v = tf.Variable(initial_value=np.arange(0, 36).reshape((6, 6)), dtype=tf.float32, name='Matrix')

    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())

    temp = tf.Variable(initial_value=np.arange(0, 36).reshape((6, 6)), dtype=tf.float32, name='temp')
    temp = wrapper(v)
    #with tf.control_dependencies([temp]):
    temp.eval()
    print 'Hello'"""

    def update_tensor(V, dim2, val):  # Update tensor V, with index(:,dim2[:]) by val[:]
        val = tf.cast(val, V.dtype)
        def body(_, (v, d2, chg)):
            d2_int = tf.cast(d2, tf.int32)
            return tf.slice(tf.concat_v2([v[:d2_int],[chg] ,v[d2_int+1:]], axis=0), [0], [v.get_shape().as_list()[0]])
        Z = tf.scan(body, elems=(V, dim2, val), initializer=tf.constant(1, shape=V.get_shape().as_list()[1:], dtype=tf.float32), name="Scan_Update")
        return Z 
Example #12
Source File: model_vnet3d.py    From LiTS---Liver-Tumor-Segmentation-Challenge with MIT License 6 votes vote down vote up
def __init__(self, image_height, image_width, image_depth, channels=1, costname=("dice coefficient",),
                 inference=False, model_path=None):
        self.image_width = image_width
        self.image_height = image_height
        self.image_depth = image_depth
        self.channels = channels

        self.X = tf.placeholder("float", shape=[None, self.image_depth, self.image_height, self.image_width,
                                                self.channels])
        self.Y_gt = tf.placeholder("float", shape=[None, self.image_depth, self.image_height, self.image_width,
                                                   self.channels])
        self.lr = tf.placeholder('float')
        self.phase = tf.placeholder(tf.bool)
        self.drop = tf.placeholder('float')

        self.Y_pred = _create_conv_net(self.X, self.image_depth, self.image_width, self.image_height, self.channels,
                                       self.phase, self.drop)
        self.cost = self.__get_cost(costname[0])
        self.accuracy = -self.__get_cost(costname[0])
        if inference:
            init = tf.global_variables_initializer()
            saver = tf.train.Saver()
            self.sess = tf.InteractiveSession()
            self.sess.run(init)
            saver.restore(self.sess, model_path) 
Example #13
Source File: tensorlayer-layers.py    From Conv3D_BICLSTM with MIT License 6 votes vote down vote up
def retrieve_seq_length_op2(data):
    """An op to compute the length of a sequence, from input shape of [batch_size, n_step(max)],
    it can be used when the features of padding (on right hand side) are all zeros.

    Parameters
    -----------
    data : tensor
        [batch_size, n_step(max)] with zero padding on right hand side.

    Examples
    --------
    >>> data = [[1,2,0,0,0],
    ...         [1,2,3,0,0],
    ...         [1,2,6,1,0]]
    >>> o = retrieve_seq_length_op2(data)
    >>> sess = tf.InteractiveSession()
    >>> sess.run(tf.initialize_all_variables())
    >>> print(o.eval())
    ... [2 3 4]
    """
    return tf.reduce_sum(tf.cast(tf.greater(data, tf.zeros_like(data)), tf.int32), 1)


# Dynamic RNN 
Example #14
Source File: tensorflow_grad_inverter.py    From ddpg-aigym with MIT License 5 votes vote down vote up
def __init__(self, action_bounds):

        self.sess = tf.InteractiveSession()       
        
        self.action_size = len(action_bounds[0])
        
        self.action_input = tf.placeholder(tf.float32, [None, self.action_size])
        self.pmax = tf.constant(action_bounds[0], dtype = tf.float32)
        self.pmin = tf.constant(action_bounds[1], dtype = tf.float32)
        self.prange = tf.constant([x - y for x, y in zip(action_bounds[0],action_bounds[1])], dtype = tf.float32)
        self.pdiff_max = tf.div(-self.action_input+self.pmax, self.prange)
        self.pdiff_min = tf.div(self.action_input - self.pmin, self.prange)
        self.zeros_act_grad_filter = tf.zeros([self.action_size])
        self.act_grad = tf.placeholder(tf.float32, [None, self.action_size])
        self.grad_inverter = tf.select(tf.greater(self.act_grad, self.zeros_act_grad_filter), tf.mul(self.act_grad, self.pdiff_max), tf.mul(self.act_grad, self.pdiff_min)) 
Example #15
Source File: actor_net.py    From ddpg-aigym with MIT License 5 votes vote down vote up
def __init__(self,num_states,num_actions):
        self.g=tf.Graph()
        with self.g.as_default():
            self.sess = tf.InteractiveSession()
            
           
            #actor network model parameters:
            self.W1_a, self.B1_a, self.W2_a, self.B2_a, self.W3_a, self.B3_a,\
            self.actor_state_in, self.actor_model = self.create_actor_net(num_states, num_actions)
            
                                   
            #target actor network model parameters:
            self.t_W1_a, self.t_B1_a, self.t_W2_a, self.t_B2_a, self.t_W3_a, self.t_B3_a,\
            self.t_actor_state_in, self.t_actor_model = self.create_actor_net(num_states, num_actions)
            
            #cost of actor network:
            self.q_gradient_input = tf.placeholder("float",[None,num_actions]) #gets input from action_gradient computed in critic network file
            self.actor_parameters = [self.W1_a, self.B1_a, self.W2_a, self.B2_a, self.W3_a, self.B3_a]
            self.parameters_gradients = tf.gradients(self.actor_model,self.actor_parameters,-self.q_gradient_input)#/BATCH_SIZE) 
            self.optimizer = tf.train.AdamOptimizer(LEARNING_RATE).apply_gradients(zip(self.parameters_gradients,self.actor_parameters))  
            #initialize all tensor variable parameters:
            self.sess.run(tf.initialize_all_variables())    
            
            #To make sure actor and target have same intial parmameters copy the parameters:
            # copy target parameters
            self.sess.run([
				self.t_W1_a.assign(self.W1_a),
				self.t_B1_a.assign(self.B1_a),
				self.t_W2_a.assign(self.W2_a),
				self.t_B2_a.assign(self.B2_a),
				self.t_W3_a.assign(self.W3_a),
				self.t_B3_a.assign(self.B3_a)])

            self.update_target_actor_op = [
                self.t_W1_a.assign(TAU*self.W1_a+(1-TAU)*self.t_W1_a),
                self.t_B1_a.assign(TAU*self.B1_a+(1-TAU)*self.t_B1_a),
                self.t_W2_a.assign(TAU*self.W2_a+(1-TAU)*self.t_W2_a),
                self.t_B2_a.assign(TAU*self.B2_a+(1-TAU)*self.t_B2_a),
                self.t_W3_a.assign(TAU*self.W3_a+(1-TAU)*self.t_W3_a),
                self.t_B3_a.assign(TAU*self.B3_a+(1-TAU)*self.t_B3_a)] 
Example #16
Source File: transfer_cifar10_softmax_b1.py    From deligan with MIT License 5 votes vote down vote up
def serialize_cifar_pool3(X,filename):
    print 'About to generate file: %s' % filename
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.1)
    sess = tf.InteractiveSession(config=tf.ConfigProto(gpu_options=gpu_options))
    X_pool3 = batch_pool3_features(sess,X)
    np.save(filename,X_pool3) 
Example #17
Source File: TripleTrainer.py    From MassImageRetrieval with Apache License 2.0 5 votes vote down vote up
def __init__(self, sample_creator=None, triple_model=None):
        self.sample_creator = sample_creator
        self.triple_model = triple_model

        self.batch_size = 2000
        self.epochs = 100
        self.plot_size = 60000
        self.is_update = True

        self.xy = None

        self.sess = tf.InteractiveSession()
        self.saver = None
        self.log_save_dir = "./log/" 
Example #18
Source File: ClusterTrainer.py    From MassImageRetrieval with Apache License 2.0 5 votes vote down vote up
def __init__(self, sample_creator=None, train_model=None):
        self.sample_creator = sample_creator
        self.train_model = train_model

        self.batch_size = 512
        self.epochs = 4000
        self.plot_size = 60000
        self.is_update = True
        self.lr = 0.001

        self.global_step = tf.Variable(0, trainable=False, name='global_step')
        self.model_param = None
        self.sess = tf.InteractiveSession()
        self.saver = None
        self.log_save_dir = "./log/cluster/" 
Example #19
Source File: ImageSegmentor.py    From CVTron with Apache License 2.0 5 votes vote down vote up
def __init__(self, model_path, args):
        self.sess = tf.InteractiveSession()
        self.model_path = model_path
        self.resnet_model = args['resnet_model']
        self.x = tf.placeholder("float",[None, None, None, 3])
        self.network = deeplab_v3(self.x, args, is_training=False, reuse=False)
        self.pred = tf.argmax(self.network, axis=3) 
Example #20
Source File: image_classifier.py    From CVTron with Apache License 2.0 5 votes vote down vote up
def __init__(self, model_name='vgg_19', model_path=MODEL_ZOO_PATH):
        self.model_name = model_name
        self.model_path = model_path
        if model_name not in ['vgg_19', 'inception_v3']:
            raise NotImplementedError
        self.download(self.model_path)
        self.sess = tf.InteractiveSession()
        if model_name == 'vgg_19':
            from cvtron.model_zoo.vgg.vgg_19 import VGG19
            vgg19 = VGG19()
            self.x = tf.placeholder("float", [None, 224, 224, 3])
            self.network = vgg19.inference(self.x)
        elif model_name == 'inception_v3':
            from cvtron.model_zoo.inception.inceptionV3 import InceptionV3
            inception_config = {
                'num_classes': 1001,
                'is_training': False
            }
            inceptionv3 = InceptionV3(inception_config)
            self.x = tf.placeholder(tf.float32, shape=[None, 299, 299, 3])
            self.network = inceptionv3.inference(self.x)
        else:
            raise ValueError('Only VGG 19 and Inception V3 are allowed')
        y = self.network.outputs
        self.probs = tf.nn.softmax(y, name="prob")
        self._init_model_() 
Example #21
Source File: SpectraLearnPredict.py    From SpectralMachine with GNU General Public License v3.0 5 votes vote down vote up
def predTF(A, Cl, R, Root):
    print('==========================================================================\n')
    print('\033[1m Running Basic TensorFlow Prediction...\033[0m')
    
    import tensorflow as tf
    import tensorflow.contrib.learn as skflow
    from sklearn import preprocessing
    
    if tfDef.logCheckpoint == True:
        tf.logging.set_verbosity(tf.logging.INFO)
    
    tfTrainedData = Root + '.tfmodel'

    x,y,y_ = setupTFmodel(A, Cl)
    sess = tf.InteractiveSession()
    tf.global_variables_initializer().run()
    print(' Opening TF training model from:', tfTrainedData)
    saver = tf.train.Saver()
    saver.restore(sess, './' + tfTrainedData)
    
    res1 = sess.run(y, feed_dict={x: R})
    res2 = sess.run(tf.argmax(y, 1), feed_dict={x: R})
    
    sess.close()
    
    rosterPred = np.where(res1[0]>tfDef.thresholdProbabilityTFPred)[0]
    print('\n  ==============================')
    print('  \033[1mTF\033[0m - Probability >',str(tfDef.thresholdProbabilityTFPred),'%')
    print('  ==============================')
    print('  Prediction\tProbability [%]')
    for i in range(rosterPred.shape[0]):
        print(' ',str(np.unique(Cl)[rosterPred][i]),'\t\t',str('{:.1f}'.format(res1[0][rosterPred][i])))
    print('  ==============================\n')
    
    print('\033[1m Predicted value (TF): ' + str(np.unique(Cl)[res2][0]) + ' (Probability: ' + str('{:.1f}'.format(res1[0][res2][0])) + '%)\n' + '\033[0m' )
    return np.unique(Cl)[res2][0], res1[0][res2][0]

#********************************************** 
Example #22
Source File: mnist_softmax.py    From MachineLearning with Apache License 2.0 5 votes vote down vote up
def main(_):
  mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)

  # Create the model
  x = tf.placeholder(tf.float32, [None, 784])
  W = tf.Variable(tf.zeros([784, 10]))
  b = tf.Variable(tf.zeros([10]))
  y = tf.matmul(x, W) + b

  # Define loss and optimizer
  y_ = tf.placeholder(tf.float32, [None, 10])

  # The raw formulation of cross-entropy,
  #
  #   tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)),
  #                                 reduction_indices=[1]))
  #
  # can be numerically unstable.
  #
  # So here we use tf.nn.softmax_cross_entropy_with_logits on the raw
  # outputs of 'y', and then average across the batch.
  cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, y_))
  train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

  sess = tf.InteractiveSession()
  # Train
  tf.global_variables_initializer().run()
  for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

  # 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})) 
Example #23
Source File: evaluate.py    From MachineLearning with Apache License 2.0 5 votes vote down vote up
def main(_):
    udacity_data.read_data(shuffe=False)
    with tf.Graph().as_default():
        config = tf.ConfigProto()
        config.gpu_options.allocator_type = 'BFC'
        sess = tf.InteractiveSession(config=config)

        saver = tf.train.import_meta_graph(os.path.join(LOG_DIR, "steering.ckpt.meta"))
        saver.restore(sess, tf.train.latest_checkpoint(LOG_DIR))

        graph = tf.get_default_graph()
        x_image = graph.get_tensor_by_name("x_image:0")
        y_label = graph.get_tensor_by_name("y_label:0")
        keep_prob = graph.get_tensor_by_name("keep_prob:0")
        logits = tf.get_collection("logits")[0]

        if os.path.exists(OUTPUT):
            os.remove(OUTPUT)

        for epoch in range(EPOCH):
            image_batch, label_batch = udacity_data.load_val_batch(BATCH_SIZE)
            feed_dict = {
                x_image: image_batch,
                y_label: label_batch,
                keep_prob: 0.6
            }
            prediction = sess.run([logits], feed_dict)
            with open(OUTPUT, 'a') as out:
                for batch in range(BATCH_SIZE):
                    out.write("%s %.10f\n" % (udacity_data.val_xs[epoch * BATCH_SIZE + batch],
                                            prediction[0][batch])) 
Example #24
Source File: generate_ml_output.py    From terrain-erosion-3-ways with MIT License 5 votes vote down vote up
def main(argv):
  # First argument is the path to the progressive_growing_of_gans clone. This
  # is needed to for proper loading of the weights via pickle.
  # Second argument is the network weights pickle file.
  # Third argument is the number of output samples to generate. Defaults to 20
  if len(argv) < 3:
    print('Usage: %s path/to/progressive_growing_of_gans weights.pkl '
           '[number_of_samples]' % argv[0])
    sys.exit(-1)
  my_dir = os.path.dirname(argv[0])
  pgog_path = argv[1]
  weight_path = argv[2]
  num_samples = int(argv[3]) if len(argv) >= 4 else 20

  # Load the GAN tensors.
  tf.InteractiveSession()
  sys.path.append(pgog_path)
  with open(weight_path, 'rb') as f:
    G, D, Gs = pickle.load(f)

  # Generate input vectors.
  latents = np.random.randn(num_samples, *Gs.input_shapes[0][1:])
  labels = np.zeros([latents.shape[0]] + Gs.input_shapes[1][1:])

  # Run generator to create samples.
  samples = Gs.run(latents, labels)

  # Make output directory
  output_dir = os.path.join(my_dir, 'ml_outputs')
  try: os.mkdir(output_dir)
  except: pass

  # Write outputs.
  for idx in range(samples.shape[0]):
    sample = (np.clip(np.squeeze((samples[idx, 0, :, :] + 1.0) / 2), 0.0, 1.0)
                 .astype('float64'))
    np.save(os.path.join(output_dir, '%d.npy' % idx), sample) 
Example #25
Source File: slp_tf.py    From SpectralMachine with GNU General Public License v3.0 5 votes vote down vote up
def predTF(A, Cl, R, Root):
    print('==========================================================================\n')
    print('\033[1m Running Basic TensorFlow Prediction...\033[0m')
    
    import tensorflow as tf
    import tensorflow.contrib.learn as skflow
    from sklearn import preprocessing
    
    if tfDef.logCheckpoint == True:
        tf.logging.set_verbosity(tf.logging.INFO)
    
    tfTrainedData = Root + '.tfmodel'

    x,y,y_ = setupTFmodel(A, Cl)
    sess = tf.InteractiveSession()
    tf.global_variables_initializer().run()
    print(' Opening TF training model from:', tfTrainedData)
    saver = tf.train.Saver()
    saver.restore(sess, './' + tfTrainedData)
    
    res1 = sess.run(y, feed_dict={x: R})
    res2 = sess.run(tf.argmax(y, 1), feed_dict={x: R})
    
    sess.close()
    
    rosterPred = np.where(res1[0]>tfDef.thresholdProbabilityTFPred)[0]
    print('\n  ==============================')
    print('  \033[1mTF\033[0m - Probability >',str(tfDef.thresholdProbabilityTFPred),'%')
    print('  ==============================')
    print('  Prediction\tProbability [%]')
    for i in range(rosterPred.shape[0]):
        print(' ',str(np.unique(Cl)[rosterPred][i]),'\t\t',str('{:.1f}'.format(res1[0][rosterPred][i])))
    print('  ==============================\n')
    
    print('\033[1m Predicted value (TF): ' + str(np.unique(Cl)[res2][0]) + ' (Probability: ' + str('{:.1f}'.format(res1[0][res2][0])) + '%)\n' + '\033[0m' )
    return np.unique(Cl)[res2][0], res1[0][res2][0]

#********************************************** 
Example #26
Source File: tf_util.py    From stable-baselines with MIT License 5 votes vote down vote up
def single_threaded_session(make_default=False, graph=None):
    """
    Returns a session which will only use a single CPU

    :param make_default: (bool) if this should return an InteractiveSession or a normal Session
    :param graph: (TensorFlow Graph) the graph of the session
    :return: (TensorFlow session)
    """
    return make_session(num_cpu=1, make_default=make_default, graph=graph) 
Example #27
Source File: slp_tf.py    From SpectralMachine with GNU General Public License v3.0 5 votes vote down vote up
def predTF(A, Cl, R, Root):
    print('==========================================================================\n')
    print('\033[1m Running Basic TensorFlow Prediction...\033[0m')
    
    import tensorflow as tf
    import tensorflow.contrib.learn as skflow
    from sklearn import preprocessing
    
    if tfDef.logCheckpoint:
        tf.logging.set_verbosity(tf.logging.INFO)
    
    tfTrainedData = Root + '.tfmodel'

    x,y,y_ = setupTFmodel(A, Cl)
    sess = tf.InteractiveSession()
    tf.global_variables_initializer().run()
    print(' Opening TF training model from:', tfTrainedData)
    saver = tf.train.Saver()
    saver.restore(sess, './' + tfTrainedData)
    
    res1 = sess.run(y, feed_dict={x: R})
    res2 = sess.run(tf.argmax(y, 1), feed_dict={x: R})
    
    sess.close()
    
    rosterPred = np.where(res1[0]>tfDef.thresholdProbabilityTFPred)[0]
    print('\n  ==============================')
    print('  \033[1mTF\033[0m - Probability >',str(tfDef.thresholdProbabilityTFPred),'%')
    print('  ==============================')
    print('  Prediction\tProbability [%]')
    for i in range(rosterPred.shape[0]):
        print(' ',str(np.unique(Cl)[rosterPred][i]),'\t\t',str('{:.1f}'.format(res1[0][rosterPred][i])))
    print('  ==============================\n')
    
    print('\033[1m Predicted value (TF): ' + str(np.unique(Cl)[res2][0]) + ' (Probability: ' + str('{:.1f}'.format(res1[0][res2][0])) + '%)\n' + '\033[0m' )
    return np.unique(Cl)[res2][0], res1[0][res2][0]

#********************************************** 
Example #28
Source File: SpectraLearnPredict.py    From SpectralMachine with GNU General Public License v3.0 5 votes vote down vote up
def predTF(A, Cl, R, Root):
    print('==========================================================================\n')
    print('\033[1m Running Basic TensorFlow Prediction...\033[0m')
    
    import tensorflow as tf
    import tensorflow.contrib.learn as skflow
    from sklearn import preprocessing
    
    if tfDef.logCheckpoint == True:
        tf.logging.set_verbosity(tf.logging.INFO)
    
    tfTrainedData = Root + '.tfmodel'

    x,y,y_ = setupTFmodel(A, Cl)
    sess = tf.InteractiveSession()
    tf.global_variables_initializer().run()
    print(' Opening TF training model from:', tfTrainedData)
    saver = tf.train.Saver()
    saver.restore(sess, './' + tfTrainedData)
    
    res1 = sess.run(y, feed_dict={x: R})
    res2 = sess.run(tf.argmax(y, 1), feed_dict={x: R})
    
    sess.close()
    
    rosterPred = np.where(res1[0]>tfDef.thresholdProbabilityTFPred)[0]
    print('\n  ==============================')
    print('  \033[1mTF\033[0m - Probability >',str(tfDef.thresholdProbabilityTFPred),'%')
    print('  ==============================')
    print('  Prediction\tProbability [%]')
    for i in range(rosterPred.shape[0]):
        print(' ',str(np.unique(Cl)[rosterPred][i]),'\t\t',str('{:.1f}'.format(res1[0][rosterPred][i])))
    print('  ==============================\n')
    
    print('\033[1m Predicted value (TF): ' + str(np.unique(Cl)[res2][0]) + ' (Probability: ' + str('{:.1f}'.format(res1[0][res2][0])) + '%)\n' + '\033[0m' )
    return np.unique(Cl)[res2][0], res1[0][res2][0]

#********************************************** 
Example #29
Source File: padding.py    From Neural-Network-Programming-with-TensorFlow with MIT License 5 votes vote down vote up
def main():
    session = tf.InteractiveSession()
    input_batch = tf.constant([
        [  # First Input (6x6x1)
            [[0.0], [1.0], [2.0], [3.0], [4.0], [5.0]],
            [[0.1], [1.1], [2.1], [3.1], [4.1], [5.1]],
            [[0.2], [1.2], [2.2], [3.2], [4.2], [5.2]],
            [[0.3], [1.3], [2.3], [3.3], [4.3], [5.3]],
            [[0.4], [1.4], [2.4], [3.4], [4.4], [5.4]],
            [[0.5], [1.5], [2.5], [3.5], [4.5], [5.5]],
        ],
    ])

    kernel = tf.constant([  # Kernel (3x3x1)
        [[[0.0]], [[0.5]], [[0.0]]],
        [[[0.0]], [[0.5]], [[0.0]]],
        [[[0.0]], [[0.5]], [[0.0]]]
    ])

    # NOTE: the change in the size of the strides parameter.
    conv2d = tf.nn.conv2d(input_batch, kernel, strides=[1, 3, 3, 1], padding='SAME')
    conv2d_output = session.run(conv2d)
    print(conv2d_output)

    conv2d = tf.nn.conv2d(input_batch, kernel, strides=[1, 3, 3, 1], padding='VALID')
    conv2d_output = session.run(conv2d)
    print(conv2d_output) 
Example #30
Source File: SpectraLearnPredict.py    From SpectralMachine with GNU General Public License v3.0 5 votes vote down vote up
def predTF(A, Cl, R, Root):
    print('==========================================================================\n')
    print('\033[1m Running Basic TensorFlow Prediction...\033[0m')
    
    import tensorflow as tf
    import tensorflow.contrib.learn as skflow
    from sklearn import preprocessing
    
    if tfDef.logCheckpoint == True:
        tf.logging.set_verbosity(tf.logging.INFO)
    
    tfTrainedData = Root + '.tfmodel'

    x,y,y_ = setupTFmodel(A, Cl)
    sess = tf.InteractiveSession()
    tf.global_variables_initializer().run()
    print(' Opening TF training model from:', tfTrainedData)
    saver = tf.train.Saver()
    saver.restore(sess, './' + tfTrainedData)
    
    res1 = sess.run(y, feed_dict={x: R})
    res2 = sess.run(tf.argmax(y, 1), feed_dict={x: R})
    
    sess.close()
    
    rosterPred = np.where(res1[0]>tfDef.thresholdProbabilityTFPred)[0]
    print('\n  ==============================')
    print('  \033[1mTF\033[0m - Probability >',str(tfDef.thresholdProbabilityTFPred),'%')
    print('  ==============================')
    print('  Prediction\tProbability [%]')
    for i in range(rosterPred.shape[0]):
        print(' ',str(np.unique(Cl)[rosterPred][i]),'\t\t',str('{:.1f}'.format(res1[0][rosterPred][i])))
    print('  ==============================\n')
    
    print('\033[1m Predicted value (TF): ' + str(np.unique(Cl)[res2][0]) + ' (Probability: ' + str('{:.1f}'.format(res1[0][res2][0])) + '%)\n' + '\033[0m' )
    return np.unique(Cl)[res2][0], res1[0][res2][0]

#**********************************************