Esempio n. 1
0
 def _collect_single_seq(self, seq_idx):
   """
   :param int seq_idx: sorted seq idx
   :return:
   """
   corpus_seq_idx = self.get_corpus_seq_idx(seq_idx)
   return DatasetSeq(
     seq_idx, features=self._dataset[corpus_seq_idx], seq_tag=self._dataset.get_seq_tag(corpus_seq_idx))
Esempio n. 2
0
 def generate_seq(self, seq_idx):
   seq_len = self.seq_len
   i1 = seq_idx
   i2 = i1 + seq_len * self.num_inputs
   features = np.array(range(i1, i2)).reshape((seq_len, self.num_inputs))
   i1, i2 = i2, i2 + seq_len
   targets = np.array(range(i1, i2))
   return DatasetSeq(seq_idx=seq_idx, features=features, targets=targets)
Esempio n. 3
0
 def get_dataset_seq_for_name(self, name, seq_idx=-1):
   """
   :param str name:
   :param int seq_idx:
   :rtype: DatasetSeq
   """
   data = {key: d.read(name) for (key, d) in self.data.items()}  # type: typing.Dict[str,numpy.ndarray]
   return DatasetSeq(seq_idx=seq_idx, seq_tag=name, features=data["data"], targets=data)
Esempio n. 4
0
    def _collect_single_seq(self, seq_idx):
        """Returns the sequence specified by the index seq_idx.
    Normalization is applied to the input features if mean and variance
    have been specified during dataset creating (see the constructor).

    :type seq_idx: int
    :rtype: DatasetSeq | None
    :returns: None if seq_idx >= num_seqs or the corresponding sequence.
    """
        if self._seq_index_list is None:
            self.init_seq_order()

        if seq_idx >= len(self._seq_index_list):
            return None

        # map the seq_idx to the shuffled sequence indices
        shuf_seq_idx = self._seq_index_list[seq_idx]
        partition_offset = int(
            np.sum([
                self._get_partition_size(i1)
                for i1 in range(self._current_partition)
            ]))
        shuf_seq_idx += partition_offset

        seqMapping = self._seqMap[shuf_seq_idx]
        fileIdx = seqMapping[0]
        datasetName = seqMapping[1]
        fileHandler = self._fileHandlers[fileIdx]
        inputFeatures = fileHandler['inputs'][datasetName][...]
        targets = None
        if 'outputs' in fileHandler:
            targets = fileHandler['outputs'][datasetName][...]

        # optional normalization
        if self._normData is not None:
            assert isinstance(self._normData, NormalizationData)
            if self._flag_normalizeInputs:
                inputFeatures = StereoHdfDataset._normalizeVector(
                    inputFeatures, self._normData.inputMean,
                    self._normData.inputVariance)
            if self._flag_normalizeTargets:
                targets = StereoHdfDataset._normalizeVector(
                    targets, self._normData.outputMean,
                    self._normData.outputVariance)

        # enforce float32 to enable Theano optimizations
        inputFeatures = inputFeatures.astype(np.float32)
        if (targets is not None) and targets.shape[1] > 1:
            targets = targets.astype(np.float32)
        elif targets.shape[1] == 1:
            targets = np.reshape(targets.astype(np.int32),
                                 (targets.shape[0], ))

        return DatasetSeq(seq_idx, inputFeatures, targets)
Esempio n. 5
0
    def _collect_single_seq_from_buffer(self, wav_file_id, seq_idx):
        """
    returns the sequence specified by the index seq_idx

    :type wav_file_id: int
    :type seq_idx: int
    :rtype: DatasetSeq | None
    :returns DatasetSeq or None if seq_idx >= num_seqs.
    """
        inputFeatures = self._get_input_features(wav_file_id)
        outputFeatures = self._get_output_features(wav_file_id)
        inputFeatures = inputFeatures.astype(np.float32)
        if outputFeatures is not None:
            outputFeatures = outputFeatures.astype(np.float32)
        return DatasetSeq(seq_idx, inputFeatures, outputFeatures)
Esempio n. 6
0
    def _collect_single_seq(self, seq_idx):
        """this method implements stacking the features

    :type seq_idx: int
    :param seq_idx: index of a sequence
    :rtype: DatasetSeq
    :return: DatasetSeq
    """
        if seq_idx >= self.num_seqs:
            return None
        originalSeq = super(DatasetWithTimeContext,
                            self)._collect_single_seq(seq_idx)
        inputFeatures = originalSeq.get_data('data')
        frames, bins = inputFeatures.shape
        leftContext = deque()
        rightContext = deque()
        inFeatWithContext = []
        for i in range(self._tau):
            leftContext.append(np.zeros(bins))
            if i + 1 < frames:
                rightContext.append(inputFeatures[i + 1, ...])
            else:
                rightContext.append(np.zeros(bins))
        for t in range(frames):
            f = inputFeatures[t, ...]
            newFeature = np.concatenate([
                np.concatenate(leftContext, axis=0), f,
                np.concatenate(rightContext, axis=0)
            ],
                                        axis=0)
            inFeatWithContext.append(newFeature)
            leftContext.popleft()
            leftContext.append(f)
            rightContext.popleft()
            if t + 1 + self._tau < frames:
                rightContext.append(inputFeatures[t + 1 + self._tau, ...])
            else:
                rightContext.append(np.zeros(bins))
        inputFeatures = np.array(inFeatWithContext)
        targets = None
        if 'classes' in originalSeq.get_data_keys():
            targets = originalSeq.get_data('classes')
        return DatasetSeq(seq_idx, inputFeatures, targets)
Esempio n. 7
0
  def add_new_data(self, features, targets=None, segment_name=None):
    """
    Adds a new seq.
    This is called via the Sprint main thread.

    :param numpy.ndarray features: format (input-feature,time) (via Sprint)
    :param dict[str,numpy.ndarray|str] targets: format (time) (idx of output-feature)
    :param str|None segment_name:
    :returns the sorted seq index
    :rtype: int
    """

    # is in format (feature,time)
    assert self.num_inputs == features.shape[0]
    num_frames = features.shape[1]
    # must be in format: (time,feature)
    features = features.transpose()
    assert features.shape == (num_frames, self.num_inputs)
    if self.input_stddev != 1:
      features /= self.input_stddev
    if self.window > 1:
      features = self._sliding_window(features)
      assert features.shape == (num_frames, self.num_inputs * self.window)

    if targets is None:
      targets = {}
    if not isinstance(targets, dict):
      targets = {"classes": targets}
    if "classes" in targets:
      # 'classes' is always the alignment
      assert targets["classes"].shape == (num_frames,), (  # is in format (time,)
        "Number of targets %s does not equal to number of features %s" % (targets["classes"].shape, (num_frames,)))
    if "orth" in targets:
      targets["orth"] = targets["orth"].decode("utf8").strip()
    if "orth" in targets and self.orth_post_process:
      targets["orth"] = self.orth_post_process(targets["orth"])
    if self.bpe:
      assert "orth" in targets
      orth = targets["orth"]
      assert isinstance(orth, (str, unicode))
      assert "bpe" not in targets
      targets["bpe"] = numpy.array(self.bpe.get_seq(orth), dtype="int32")
    if self.orth_vocab:
      assert "orth" in targets
      orth = targets["orth"]
      assert isinstance(orth, (str, unicode))
      assert "orth_classes" not in targets
      targets["orth_classes"] = numpy.array(self.orth_vocab.get_seq(orth), dtype="int32")

    # Maybe convert some targets.
    if self.target_maps:
      for key, target_map in self.target_maps.items():
        assert key in targets
        v = target_map[targets[key]]
        v = numpy.asarray(v)
        if v.ndim == 0:
          v = numpy.zeros((num_frames,), dtype=v.dtype) + v  # add time dimension
        targets[key] = v

    # Maybe remove some targets.
    for key in self._target_black_list:
      if key in targets:
        del targets[key]

    # Check if all targets are valid.
    for key, v in sorted(list(targets.items())):
      if isinstance(v, numpy.ndarray):
        continue  # ok
      if isinstance(v, unicode):
        v = v.encode("utf8")
      if isinstance(v, (str, bytes)):
        if PY3:
          assert isinstance(v, bytes)
          v = list(v)
        else:
          v = list(map(ord, v))
        v = numpy.array(v, dtype="uint8")
        targets[key] = v
        if self.str_add_final_zero:
          v = numpy.append(v, numpy.array([0], dtype=v.dtype))
          assert key + "0" not in targets
          targets[key + "0"] = v
        continue
      print("%s, we will ignore the target %r because it is not a numpy array: %r" % (self, key, v), file=log.v3)
      self._target_black_list += [key]
      del targets[key]

    with self.lock:
      # This gets called in the Sprint main thread.
      # If this is used together with SprintInterface.getSegmentList(), we are always in a state where
      # we just yielded a segment name, thus we are always in a Sprint epoch and thus ready for data.
      assert self.ready_for_data
      assert not self.reached_final_seq
      assert not self.sprint_finalized

      seq_idx = self.next_seq_to_be_added

      if self.predefined_seq_list_order:
        # Note: Only in ExternSprintDataset, we can reliably set the seq order for now.
        assert seq_idx < len(self.predefined_seq_list_order), "seq_idx %i, expected predef num seqs %i" % (
          seq_idx, len(self.predefined_seq_list_order))
        expected_seq_name = self.predefined_seq_list_order[seq_idx]
        if expected_seq_name != segment_name:
          if segment_name in self.predefined_seq_list_order:
            raise Exception("seq_idx %i expected to be tag %r but got tag %r; tag %r is at idx %i" % (
              seq_idx, expected_seq_name, segment_name, segment_name,
              self.predefined_seq_list_order.index(segment_name)))
          raise Exception("seq_idx %i expected to be tag %r but got tag %r; tag %r not found" % (
            seq_idx, expected_seq_name, segment_name, segment_name))

      self.next_seq_to_be_added += 1
      self._num_timesteps += num_frames
      self.cond.notify_all()

      if seq_idx > self.requested_load_seq_end - 1 + self.SprintCachedSeqsMax:
        print("%s add_new_data: seq=%i, len=%i. Cache filled, waiting to get loaded..." % (
          self, seq_idx, num_frames), file=log.v5)
        while seq_idx > self.requested_load_seq_end - 1 + self.SprintCachedSeqsMin:
          assert not self.reached_final_seq
          assert seq_idx + 1 == self.next_seq_to_be_added
          self.cond.wait()

      self.added_data += [DatasetSeq(seq_idx, features, targets, seq_tag=segment_name)]
      self.cond.notify_all()
      return seq_idx