Ejemplo n.º 1
0
 def build_inputs(self):
  if self.mode=="inference":
   image_feed=tf.placeholder(dtype=tf.string,shape=[],name="image_feed")
   word_prob_feed=tf.placeholder(dtype=tf.float32,shape=[319],name="word_prob_feed")
   input_feed=tf.placeholder(dtype=tf.int64,shape=[None],name="input_feed")
   images=tf.expand_dims(self.process_image(image_feed),0)
   word_prob=tf.expand_dims(word_prob_feed,0)
   input_seqs=tf.expand_dims(input_feed,1)
   target_seqs=None
   input_mask=None
  else:
   input_queue=input_ops.prefetch_input_data(self.reader,self.config.input_file_pattern,is_training=self.is_training(),batch_size=self.config.batch_size,values_per_shard=self.config.values_per_input_shard,input_queue_capacity_factor=self.config.input_queue_capacity_factor,num_reader_threads=self.config.num_input_reader_threads)
   assert self.config.num_preprocess_threads%2==0
   images_word_prob_and_captions=[]
   for thread_id in range(self.config.num_preprocess_threads):
    serialized_sequence_example=input_queue.dequeue()
    encoded_image,caption,word_prob=input_ops.parse_sequence_example(serialized_sequence_example,image_feature=self.config.image_feature_name,word_prob_feature=self.config.word_prob_feature_name,caption_feature=self.config.caption_feature_name)
    image=self.process_image(encoded_image,thread_id=thread_id)
    images_word_prob_and_captions.append([image,word_prob,caption])
   queue_capacity=(2*self.config.num_preprocess_threads*self.config.batch_size)
   images,word_prob,input_seqs,target_seqs,input_mask=(input_ops.batch_with_dynamic_pad(images_word_prob_and_captions,batch_size=self.config.batch_size,queue_capacity=queue_capacity))
  self.images=images
  self.word_prob=word_prob
  self.input_seqs=input_seqs
  self.target_seqs=target_seqs
  self.input_mask=input_mask
    def build_inputs(self):
        """Input prefetching, preprocessing and batching.

        Outputs:
          self.images
          self.input_seqs
          self.target_seqs (training and eval only)
          self.input_mask (training and eval only)
        """
        if self.mode == "inference":
            # In inference mode, images and inputs are fed via placeholders.
            image_feed = tf.placeholder(
                dtype=tf.string, shape=[], name="image_feed")
            input_feed = tf.placeholder(dtype=tf.int64,
                                        shape=[None],  # batch_size
                                        name="input_feed")

            # Process image and insert batch dimensions.
            images = tf.expand_dims(self.process_image(image_feed), 0)
            input_seqs = tf.expand_dims(input_feed, 1)

            # No target sequences or input mask in inference mode.
            target_seqs = None
            input_mask = None
        else:
            # Prefetch serialized SequenceExample protos.
            input_queue = input_ops.prefetch_input_data(
                self.reader,
                self.config.input_file_pattern,
                is_training=self.is_training(),
                batch_size=self.config.batch_size,
                values_per_shard=self.config.values_per_input_shard,
                input_queue_capacity_factor=self.config.input_queue_capacity_factor,
                num_reader_threads=self.config.num_input_reader_threads)

            # Image processing and random distortion. Split across multiple threads
            # with each thread applying a slightly different distortion.
            assert self.config.num_preprocess_threads % 2 == 0
            images_and_captions = []
            for thread_id in range(self.config.num_preprocess_threads):
                serialized_sequence_example = input_queue.dequeue()
                encoded_image, caption = input_ops.parse_sequence_example(
                    serialized_sequence_example,
                    image_feature=self.config.image_feature_name,
                    caption_feature=self.config.caption_feature_name)
                image = self.process_image(encoded_image, thread_id=thread_id)
                images_and_captions.append([image, caption])

            # Batch inputs.
            queue_capacity = (2 * self.config.num_preprocess_threads *
                              self.config.batch_size)
            images, input_seqs, target_seqs, input_mask = (
                input_ops.batch_with_dynamic_pad(images_and_captions,
                                                 batch_size=self.config.batch_size,
                                                 queue_capacity=queue_capacity))

        self.images = images
        self.input_seqs = input_seqs
        self.target_seqs = target_seqs
        self.input_mask = input_mask
Ejemplo n.º 3
0
  def build_inputs(self):
    """Input prefetching, preprocessing and batching.

    Outputs:
      self.images
      self.input_seqs
      self.target_seqs (training and eval only)
      self.input_mask (training and eval only)
    """
    if self.mode == "inference":
      # In inference mode, images and inputs are fed via placeholders.
      image_feed = tf.placeholder(dtype=tf.string, shape=[], name="image_feed")
      input_feed = tf.placeholder(dtype=tf.int64,
                                  shape=[None],  # batch_size
                                  name="input_feed")

      # Process image and insert batch dimensions.
      images = tf.expand_dims(self.process_image(image_feed), 0)
      input_seqs = tf.expand_dims(input_feed, 1)

      # No target sequences or input mask in inference mode.
      target_seqs = None
      input_mask = None
    else:
      # Prefetch serialized SequenceExample protos.
      input_queue = input_ops.prefetch_input_data(
          self.reader,
          self.config.input_file_pattern,
          is_training=self.is_training(),
          batch_size=self.config.batch_size,
          values_per_shard=self.config.values_per_input_shard,
          input_queue_capacity_factor=self.config.input_queue_capacity_factor,
          num_reader_threads=self.config.num_input_reader_threads)

      # Image processing and random distortion. Split across multiple threads
      # with each thread applying a slightly different distortion.
      assert self.config.num_preprocess_threads % 2 == 0
      images_and_captions = []
      for thread_id in range(self.config.num_preprocess_threads):
        serialized_sequence_example = input_queue.dequeue()
        encoded_image, caption = input_ops.parse_sequence_example(
            serialized_sequence_example,
            image_feature=self.config.image_feature_name,
            caption_feature=self.config.caption_feature_name)
        image = self.process_image(encoded_image, thread_id=thread_id)
        images_and_captions.append([image, caption])

      # Batch inputs.
      queue_capacity = (2 * self.config.num_preprocess_threads *
                        self.config.batch_size)
      images, input_seqs, target_seqs, input_mask = (
          input_ops.batch_with_dynamic_pad(images_and_captions,
                                           batch_size=self.config.batch_size,
                                           queue_capacity=queue_capacity))

    self.images = images
    self.input_seqs = input_seqs
    self.target_seqs = target_seqs
    self.input_mask = input_mask
    def build_inputs(self):
        # Prefetch serialized SequenceExample protos.
        input_queue = input_ops.prefetch_input_data(
            self.reader,
            self.config.input_file_pattern,
            is_training=self.is_training(),
            batch_size=self.config.batch_size,
            values_per_shard=self.config.values_per_input_shard,
            # approximate values nums for all shard
            input_queue_capacity_factor=self.config.
            input_queue_capacity_factor,
            # queue_capacity_factor for shards
            num_reader_threads=self.config.num_input_reader_threads)

        # Image processing and random distortion. Split across multiple threads
        # with each thread applying a slightly different color distortions.
        assert self.config.num_preprocess_threads % 2 == 0
        images_and_label = []
        for thread_id in range(self.config.num_preprocess_threads):
            # thread
            serialized_sequence_example = input_queue.dequeue()
            encoded_image, image_label, image_name = input_ops.parse_sequence_example(
                serialized_sequence_example,
                image_feature=self.config.image_feature_name,
                label_feature=self.config.label_feature_name,
                filename_feature=self.config.filename_feature_name)
            # preprocessing, for different thread_id use different distortion function
            image = self.process_image(encoded_image, thread_id=thread_id)

            images_and_label.append([image, image_name])
            # mutil threads preprocessing the image

        queue_capacity = (2 * self.config.num_preprocess_threads *
                          self.config.batch_size)
        images, image_names = tf.train.batch_join(
            images_and_label,
            batch_size=self.config.batch_size,
            capacity=queue_capacity,
            dynamic_pad=True,
            name="batch")

        self.images = images  # batch_size, 224, 224 , 3
        #self.images = tf.cast(self.images, tf.float32)
        tf.summary.image("raw_images", self.images)
        self.images = tf.cast(self.images, tf.float32) / 255.
        if self.mode == "train":
            #with tf.variable_scope('noised'):
            #    self.inputs = tf.add(self.images, self.scale * tf.random_normal(shape=self.images.shape))
            self.inputs = self.images
            tf.summary.image("noised_images", self.images)
        elif self.mode == "inference":
            self.inputs = self.images
        self.image_names = image_names
Ejemplo n.º 5
0
    def build_inputs(self):
        """Input prefetching, preprocessing and batching.

        Outputs:
            self.images
            self.input_seqs
            self.targets (training and eval only)
        """
        if self.mode == "inference":
            with tf.name_scope("image_input"):
                "In inference mode, images are fed via placeholders."
                image_feed = tf.placeholder(dtype=tf.string,
                                            shape=[],
                                            name="image_feed")

                # Process image and insert batch dimensions
                images, old_h, old_w = self.process_image(image_feed)

                self.old_img_height = old_h
                self.old_img_width = old_w
                self.new_img_height = tf.shape(images)[1]
                self.new_img_width = tf.shape(images)[2]

                # No targets in inference mode.
                targets = None

        else:
            with tf.name_scope("load_tf_records"), tf.device("/cpu:0"):
                # Prefetch serialized SequenceExample protos.
                input_queue = input_ops.prefetch_input_data(
                    self.reader,
                    self.config.input_file_pattern,
                    is_training=self.is_training(),
                    batch_size=self.config.batch_size,
                    values_per_shard=self.config.values_per_input_shard,
                    input_queue_capacity_factor=self.config.
                    input_queue_capacity_factor,
                    num_reader_threads=self.config.num_input_reader_threads)

                # Decode data from sequence example.
                serialized_sequence_example = input_queue.dequeue()
                image, targets, img_file = input_ops.parse_sequence_example(
                    serialized_sequence_example)
                images = tf.expand_dims(self.process_image(image), 0)

        self.images = images
        self.targets = targets
Ejemplo n.º 6
0
    def build_inputs(self):
        # Prefetch serialized SequenceExample protos.
        input_queue = input_ops.prefetch_input_data(
            self.reader,
            self.config.input_file_pattern,
            is_training=self.is_training(),
            batch_size=self.config.batch_size,
            values_per_shard=self.config.values_per_input_shard,
            # approximate values nums for all shard
            input_queue_capacity_factor=self.config.
            input_queue_capacity_factor,
            # queue_capacity_factor for shards
            num_reader_threads=self.config.num_input_reader_threads)

        # Image processing and random distortion. Split across multiple threads
        # with each thread applying a slightly different color distortions.
        assert self.config.num_preprocess_threads % 2 == 0
        images_and_label = []
        for thread_id in range(self.config.num_preprocess_threads):
            # thread
            serialized_sequence_example = input_queue.dequeue()
            encoded_image, image_label, image_name = input_ops.parse_sequence_example(
                serialized_sequence_example,
                image_feature=self.config.image_feature_name,
                label_feature=self.config.label_feature_name,
                filename_feature=self.config.filename_feature_name)
            # preprocessing, for different thread_id use different distortion function
            image = self.process_image(encoded_image, thread_id=thread_id)

            images_and_label.append([image, image_label, image_name])
            # mutil threads preprocessing the image

        queue_capacity = (2 * self.config.num_preprocess_threads *
                          self.config.batch_size)

        images, labels, image_names = tf.train.batch_join(
            images_and_label,
            batch_size=self.config.batch_size,
            capacity=queue_capacity,
            dynamic_pad=True,
            name="batch")

        self.images = images
        self.labels = labels
        self.image_names = image_names
Ejemplo n.º 7
0
    def build_inputs(self):
        """Input prefetching, preprocessing and batching.

            Outputs:
                self.images
                self.input_seqs
                self.targets (training and eval only)
        """
        if self.mode == "inference":
            with tf.name_scope("image_input"):
                "In inference mode, images are fed via placeholders."
                image_feed = tf.placeholder(dtype=tf.string,
                                            shape=[],
                                            name="image_feed")

                # Process image and insert batch dimensions
                images = tf.expand_dims(self.process_image(image_feed), 0)

                # No target word in inference mode.
                targets = None

                # No sequence length in inference mode.
                seq_lengths = None

        else:
            with tf.name_scope("load_tf_records"):
                # Prefetch serialized SequenceExample protos.
                input_queue = input_ops.prefetch_input_data(
                    self.reader,
                    self.config.input_file_pattern,
                    is_training=self.is_training(),
                    batch_size=self.config.batch_size,
                    values_per_shard=self.config.values_per_input_shard,
                    input_queue_capacity_factor=self.config.
                    input_queue_capacity_factor,
                    num_reader_threads=self.config.num_input_reader_threads)

                assert self.config.num_preprocess_threads % 2 == 0
                images_and_words = []
                for thread_id in range(self.config.num_preprocess_threads):
                    serialized_sequence_example = input_queue.dequeue()
                    encoded_image, word = input_ops.parse_sequence_example(
                        serialized_sequence_example,
                        image_feature=self.config.image_feature_name,
                        word_feature=self.config.word_feature_name)
                    image = self.process_image(encoded_image,
                                               thread_id=thread_id)
                    images_and_words.append([image, word])

                # Batch inputs.
                queue_capacity = (2 * self.config.num_preprocess_threads *
                                  self.config.batch_size)
                images, targets, seq_lengths = input_ops.make_batch(
                    images_and_words,
                    batch_size=self.config.batch_size,
                    queue_capacity=queue_capacity,
                    vocab_size=self.config.vocab_size)

        self.images = images
        self.targets = targets
        self.seq_lengths = seq_lengths
    def build_inputs(self):
        """Input prefetching, preprocessing and batching.

    Outputs:
      images and seqs
    """
        if self.mode == "inference":
            # In inference mode, images and inputs are fed via placeholders.

            image_feed = tf.placeholder(dtype=tf.string,
                                        shape=[],
                                        name="image_feed")
            # Process image and insert batch dimensions.
            image_feed = self.process_image(image_feed)

            # Process image and insert batch dimensions.
            image_seqs = tf.expand_dims(image_feed, 0)

            # No target sequences or input mask in inference mode.
            input_mask = tf.placeholder(
                dtype=tf.int64,
                shape=[1, 8],  # batch_size
                name="input_mask")
        else:
            # Prefetch serialized SequenceExample protos.
            input_queue = input_ops.prefetch_input_data(
                self.reader,
                self.config.input_file_pattern,
                is_training=self.is_training(),
                batch_size=self.config.batch_size,
                values_per_shard=self.config.values_per_input_shard,
                input_queue_capacity_factor=self.config.
                input_queue_capacity_factor,
                num_reader_threads=self.config.num_input_reader_threads)

            # Image processing and random distortion. Split across multiple threads
            # with each thread applying a slightly different distortion.
            # assert self.config.num_preprocess_threads % 2 == 0
            images_and_captions = []
            for thread_id in range(self.config.num_preprocess_threads):
                serialized_sequence_example = input_queue.dequeue()
                set_id, encoded_images, image_ids, captions, likes = (
                    input_ops.parse_sequence_example(
                        serialized_sequence_example,
                        set_id=self.config.set_id_name,
                        image_feature=self.config.image_feature_name,
                        image_index=self.config.image_index_name,
                        caption_feature=self.config.caption_feature_name,
                        number_set_images=self.config.number_set_images))

                images = []
                for i in range(self.config.number_set_images):
                    images.append(
                        self.process_image(encoded_images[i], image_idx=i))

                images_and_captions.append(
                    [set_id, images, image_ids, captions, likes])

            # Batch inputs.
            queue_capacity = (5 * self.config.num_preprocess_threads *
                              self.config.batch_size)
            #(set_ids, image_seqs, image_ids, f_input_seqs, f_target_seqs,
            # b_input_seqs, b_target_seqs, input_mask, cap_seqs, cap_mask) = (
            (set_ids, image_seqs, image_ids, input_mask, loss_mask, cap_seqs,
             cap_mask, likes) = (input_ops.batch_with_dynamic_pad(
                 images_and_captions,
                 batch_size=self.config.batch_size,
                 queue_capacity=queue_capacity))
        self.images = image_seqs
        self.input_mask = input_mask
Ejemplo n.º 9
0
    def build_inputs(self):
        """Input prefetching, preprocessing and batching.

    Outputs:
      self.images
      self.input_seqs
      self.input_mask (training and eval only)
    """
        # in prediction mode, we use a batch size of one
        batch_size = self.config.batch_size

        if self.mode == "prediction":
            batch_size = 1

            # In inference mode, images and inputs are fed via placeholders.
            image_feed = tf.placeholder(
                dtype=tf.string, shape=[],
                name="image_feed")  # shape: scalar value

            #image_fn_feed = tf.placeholder(dtype=tf.string, shape=[], name="image_fn_feed")

            #image_filename_queue = tf.train.string_input_producer([image_fn_feed]) #  list of files to read

            #reader = tf.WholeFileReader()
            #_, image_feed = reader.read(image_filename_queue)

            text_feed = tf.placeholder(
                dtype=tf.int64,
                shape=[
                    None, self.config.sentence_length
                ],  # shape 2D tensor - variable size (first dimension sentence sequence, second dimension token sequence (actually fixed size))
                name="text_feed")

            # arbitrary labels (not used)
            mi_label = tf.constant(-1, dtype=tf.int64)
            sc_label = tf.constant(-1.0, dtype=tf.float32)

            image = self.process_image(image_feed)

            # Process image and insert batch dimensions.
            images = tf.expand_dims(self.process_image(image_feed), 0)
            input_seqs = tf.expand_dims(text_feed, 0)
            mi_labels = tf.expand_dims(mi_label, 0)
            sc_labels = tf.expand_dims(sc_label, 0)
            input_mask = tf.expand_dims(tf.constant([1], dtype=tf.int32), 0)

        else:
            # Prefetch serialized SequenceExample protos.
            input_queue = input_ops.prefetch_input_data(
                self.reader,
                self.config.input_file_pattern,
                is_training=self.is_training(),
                batch_size=batch_size,
                values_per_shard=self.config.values_per_input_shard,
                input_queue_capacity_factor=self.config.
                input_queue_capacity_factor,
                num_reader_threads=self.config.num_input_reader_threads,
                mode=self.mode)

            # Image processing and random distortion. Split across multiple threads
            # with each thread applying a slightly different distortion.
            assert self.config.num_preprocess_threads % 2 == 0
            images_and_texts = []
            for thread_id in range(self.config.num_preprocess_threads):
                serialized_sequence_example = input_queue.dequeue()
                encoded_image, text, mi, sc = input_ops.parse_sequence_example(
                    serialized_sequence_example,
                    image_feature=self.config.image_feature_name,
                    sentences_feature=self.config.sentences_feature_name,
                    sentence_length=self.config.sentence_length,
                    mi_feature=self.config.mi_feature_name,
                    sc_feature=self.config.sc_feature_name)
                image = self.process_image(encoded_image, thread_id=thread_id)
                images_and_texts.append([image, text, mi, sc])

            # Batch inputs.
            queue_capacity = (2 * self.config.num_preprocess_threads *
                              batch_size)
            images, input_seqs, mi_labels, sc_labels, input_mask = (
                input_ops.batch_with_dynamic_pad(
                    images_and_texts,
                    batch_size=batch_size,
                    queue_capacity=queue_capacity))

        #print('Shapes')
        #print('Shape images: ' + str(images.get_shape()))
        #print('Shape input_seqs: ' + str(input_seqs.get_shape()))
        #print('Shape input_mask: ' + str(input_mask.get_shape()))

        self.images = images
        self.input_seqs = input_seqs
        if self.mode == "prediction":
            self.mi_labels = None
            self.sc_labels = None
        else:
            self.mi_labels = mi_labels
            self.sc_labels = sc_labels
        self.input_mask = input_mask
    def build_inputs(self):
        """Input prefetching, preprocessing and batching.

    Outputs:
      Inputs of the model.
    """
        if self.mode == "inference":
            # In inference mode, images and inputs are fed via placeholders.
            image_feed = tf.placeholder(dtype=tf.string,
                                        shape=[],
                                        name="image_feed")
            # Process image and insert batch dimensions.
            image_feed = self.process_image(image_feed)

            input_feed = tf.placeholder(
                dtype=tf.int64,
                shape=[None],  # batch_size
                name="input_feed")

            # Process image and insert batch dimensions.
            image_seqs = tf.expand_dims(image_feed, 0)
            cap_seqs = tf.expand_dims(input_feed, 1)

            # No target sequences or input mask in inference mode.
            input_mask = tf.placeholder(
                dtype=tf.int64,
                shape=[1, 6],  # batch_size
                name="input_mask")
            #      input_mask = None
            cap_mask = None
            loss_mask = None
            set_ids = None

        else:
            # Prefetch serialized SequenceExample protos.
            input_queue = input_ops.prefetch_input_data(
                self.reader,
                self.config.input_file_pattern,
                is_training=self.is_training(),
                batch_size=self.config.batch_size,
                values_per_shard=self.config.values_per_input_shard,
                input_queue_capacity_factor=self.config.
                input_queue_capacity_factor,
                num_reader_threads=self.config.num_input_reader_threads)

            # Image processing and random distortion. Split across multiple threads
            # with each thread applying a slightly different distortion. But we only
            # use one thread in our Polyvore model. weights are not used.
            images_and_captions = []
            for thread_id in range(self.config.num_preprocess_threads):
                serialized_sequence_example = input_queue.dequeue()
                global set_id
                set_id, encoded_images, image_ids, captions, weights, votes = (
                    input_ops.parse_sequence_example(
                        serialized_sequence_example,
                        set_id=self.config.set_id_name,
                        image_feature=self.config.image_feature_name,
                        image_index=self.config.image_index_name,
                        caption_feature=self.config.caption_feature_name,
                        number_set_images=self.config.number_set_images))
                set_id = tf.Print(set_id, [set_id], message="This is set_id: ")
                #        weights = tf.Print(weights, [weights], message="This is weights: ")
                #        votes = tf.to_float(votes, name='ToFloat')
                #        votes = tf.Print(votes, [votes], message="This is votes: ")
                #        votes = np.ceil(votes/100)+1

                print("go inside the loop")
                images = []
                for i in range(self.config.number_set_images):
                    print('hello this is a debugging information')
                    images.append(
                        self.process_image(encoded_images[i], image_idx=i))

                images_and_captions.append(
                    [set_id, images, image_ids, captions, weights, votes])

            # Batch inputs.
            queue_capacity = (5 * self.config.num_preprocess_threads *
                              self.config.batch_size)

            (set_ids, image_seqs, image_ids, input_mask, loss_mask, cap_seqs,
             cap_mask, weights, votes) = (input_ops.batch_with_dynamic_pad(
                 images_and_captions,
                 batch_size=self.config.batch_size,
                 queue_capacity=queue_capacity))
#    set_ids = tf.Print(set_ids, [set_ids], message="This is set_id: ")
#    weights = tf.Print(weights, [weights], message="This is weights: ")
        self.images = image_seqs
        self.input_mask = input_mask
        self.loss_mask = loss_mask
        self.cap_seqs = cap_seqs
        self.cap_mask = cap_mask
        self.set_ids = set_ids
        self.weights = weights
        self.votes = votes
Ejemplo n.º 11
0
    def build_inputs(self):
        """Input prefetching, preprocessing and batching.

    Outputs:
      self.center_words
      self.context_words
    """

        if self.mode == "train":

            # Prefetch serialized SequenceExample protos.
            input_queue = input_ops.prefetch_input_data(
                self.reader,
                self.config.input_file_pattern,
                is_training=self.is_training(),
                batch_size=self.config.batch_size,
                values_per_shard=self.config.values_per_input_shard,
                input_queue_capacity_factor=self.config.
                input_queue_capacity_factor,
                num_reader_threads=self.config.num_input_reader_threads)

            assert self.config.num_preprocess_threads % 2 == 0

            # create a queue for samples (center_word, context_word) from multiple sequence examples
            min_queue_examples = self.config.tokens_per_sequence_example * self.config.sample_queue_capacity_factor * self.config.num_skips
            capacity = min_queue_examples + 100 * self.config.batch_size  # the extra capacity allows the Queue to have enough time to enqueue new samples, since it should always hold at least min_queue_examples
            samples_queue = tf.RandomShuffleQueue(
                capacity=capacity,
                min_after_dequeue=min_queue_examples,
                dtypes=[tf.int64],
                shapes=[2],
                name="random_samples_queue")

            enqueue_ops = []
            for thread_id in range(self.config.num_preprocess_threads):
                serialized_sequence_example = input_queue.dequeue()
                sample_center_words, sample_context_words = input_ops.parse_sequence_example(
                    serialized_sequence_example)

                sample_center_words = tf.expand_dims(sample_center_words, 1)
                sample_context_words = tf.expand_dims(sample_context_words, 1)

                samples = tf.concat(
                    1, [sample_center_words, sample_context_words])

                enqueue_ops.append(samples_queue.enqueue_many(samples))

            tf.train.queue_runner.add_queue_runner(
                tf.train.queue_runner.QueueRunner(samples_queue, enqueue_ops)
            )  # this op will start the actual threads as soon as the session runs

            random_samples = []
            for thread_id in range(self.config.num_preprocess_threads):
                sample = samples_queue.dequeue()

                random_samples.append(sample)

            # Batch inputs.
            queue_capacity = (2 * self.config.num_preprocess_threads *
                              self.config.batch_size)

            center_words, context_words = (input_ops.batch_samples(
                random_samples,
                batch_size=self.config.batch_size,
                queue_capacity=queue_capacity))

            #print('Shapes')
            #print('Shape center_words: ' + str(center_words.get_shape()))
            #print('Shape context_words: ' + str(context_words.get_shape()))

            self.center_words = center_words
            self.context_words = context_words

        else:
            valid_size = 16
            valid_dataset = tf.placeholder(tf.int64, [valid_size],
                                           name="valid_dataset")
            self.valid_dataset = valid_dataset
Ejemplo n.º 12
0
    def build_inputs(self):
        """Input prefetching, preprocessing and batching.

    Outputs:
      self.images
      self.input_seqs
      self.target_seqs (training and eval only)
      self.input_mask (training and eval only)
    """

        if self.mode == "inference":
            # In inference mode, images and inputs are fed via placeholders.
            image_feed = tf.placeholder(dtype=tf.string,
                                        shape=[],
                                        name="image_feed")
            input_feed = tf.placeholder(
                dtype=tf.int64,
                shape=[None],  # batch_size
                name="input_feed")

            # Process image and insert batch dimensions.
            images = tf.expand_dims(self.process_image(image_feed), 0)
            input_seqs = tf.expand_dims(input_feed, 1)
            #TODO

            # senti_feed_0= tf.Variable([1],dtype=tf.int64)
            # senti_feed_0= tf.get_variable("senti_input_0",[1],initializer = tf.zeros_initializer,dtype=tf.int64)
            # senti_feed_0= tf.Variable([FLAGS.sentiment_input],dtype=tf.int64,name="senti_input_0")
            senti_feed_0 = tf.cast([FLAGS.sentiment_input], tf.int64)
            out = tf.expand_dims(senti_feed_0, 1)

            # No target sequences or input mask in inference mode.
            target_seqs = None
            input_mask = None
            # out= None
        else:
            # Prefetch serialized SequenceExample protos.
            input_queue = input_ops.prefetch_input_data(
                self.reader,
                self.config.input_file_pattern,
                is_training=self.is_training(),
                batch_size=self.config.batch_size,
                values_per_shard=self.config.values_per_input_shard,
                input_queue_capacity_factor=self.config.
                input_queue_capacity_factor,
                num_reader_threads=self.config.num_input_reader_threads)

            # Image processing and random distortion. Split across multiple threads
            # with each thread applying a slightly different distortion.
            assert self.config.num_preprocess_threads % 2 == 0
            images_and_captions = []
            captions_co = []
            for thread_id in range(self.config.num_preprocess_threads):
                serialized_sequence_example = input_queue.dequeue()
                encoded_image, caption = input_ops.parse_sequence_example(
                    serialized_sequence_example,
                    image_feature=self.config.image_feature_name,
                    caption_feature=self.config.caption_feature_name)
                # print(caption.get_shape().as_list())
                image = self.process_image(encoded_image, thread_id=thread_id)
                images_and_captions.append([image, caption])
                captions_co.append(caption)
            # Batch inputs.
            queue_capacity = (2 * self.config.num_preprocess_threads *
                              self.config.batch_size)
            images, input_seqs, target_seqs, input_mask, out = (
                input_ops.batch_with_dynamic_pad(
                    images_and_captions,
                    batch_size=self.config.batch_size,
                    queue_capacity=queue_capacity))

            # print(out.get_shape().as_list())
            # print("out")
        # keys = self.sentiment_dict.keys()
        # values = self.sentiment_dict.values()
        # dic_value = tf.cast(values, dtype=tf.float32)
        # dic_keys = tf.cast(keys, dtype=tf.int64)
        # self.table = tf.contrib.lookup.HashTable(
        #       tf.contrib.lookup.KeyValueTensorInitializer(dic_keys, dic_value), -1
        #   )
        self.images = images
        self.input_seqs = input_seqs
        self.target_seqs = target_seqs
        self.input_mask = input_mask
        self.sentiments = out