Ejemplo n.º 1
0
    def __init__(self,
                 url,
                 max_annotations_per_image,
                 pad_width,
                 pad_height,
                 pad_value,
                 randomize,
                 use_flipping,
                 max_images=None):

        self.image_si = StreamInformation("image", 0, 'dense', np.float32, (
            3,
            pad_height,
            pad_width,
        ))
        self.roi_si = StreamInformation("annotation", 1, 'dense', np.float32, (
            max_annotations_per_image,
            5,
        ))
        self.dims_si = StreamInformation("dims", 1, 'dense', np.float32, (4, ))

        self.od_reader = ObjectDetectionReader(url, max_annotations_per_image,
                                               pad_width, pad_height,
                                               pad_value, randomize,
                                               use_flipping, max_images)

        super(ObjectDetectionMinibatchSource, self).__init__()
Ejemplo n.º 2
0
    def __init__(self, f_dim, l_dim):
        self.f_dim, self.l_dim = f_dim, l_dim

        self.fsi = StreamInformation("features", 0, 'sparse', np.float32,
                                     (self.f_dim, ))
        self.lsi = StreamInformation("labels", 1, 'dense', np.float32,
                                     (self.l_dim, ))

        # MBDATA_SPARSE fits into memory we will, so we will read it in all at
        # once. It follows the CNTKTextFormat:
        #   sequence ID |feature1 data |feature2 data
        # where in this case feature1's data is encoded as one-hot and we will
        # convert to CSR, and feature2's data is a one-hot encoded as dense.

        # We will store
        #   sequence id -> "features" -> list of features
        # and
        #   sequence id -> "labels" -> label

        self.data = {}
        for line in MBDATA_SPARSE.split('\n'):
            line = line.strip()
            if not line:
                continue
            seq_id, data = line.split('|', 1)
            data = data.split("|")
            seq_id = int(seq_id.strip())

            if seq_id not in self.data:
                self.data[seq_id] = {'features': []}

            # Processing features - expecting one per line.
            # We accumulate the vocabulary indices and convert them into a
            # Value object when requested in next_minibatch()
            features = data[0].split(" ")
            assert features[0] == 'x'
            vocab_idx = int(features[1].split(":")[0])
            self.data[seq_id]['features'].append(vocab_idx)

            # Process label, if exists
            if len(data) == 2:
                # Only one label definition per sequence allowed
                assert 'labels' not in self.data[seq_id]

                labels = data[1].split(" ")
                assert labels[0] == 'y'
                # We don't have many label classes, and only one label per
                # sequence, so we just read it in as dense, all at once.
                val = np.asarray([labels[1:]], dtype=np.float32)
                self.data[seq_id]['labels'] = val

        self.sequences = sorted(self.data)
        self.next_seq_idx = 0

        super(MyDataSource, self).__init__()
Ejemplo n.º 3
0
def test_user_deserializer_sequence_mode():
    import scipy.sparse as sp
    streams = [StreamInformation('x', 0, 'dense', np.float32, (2, 3)), 
               StreamInformation('y', 1, 'sparse', np.float32, (3,))]

    def run_minibatch_source(minibatch_source, num_chunks, num_sequences_per_value):
        sequence_x_values = np.zeros(num_chunks, dtype=np.int32)
        sequence_y_values = np.zeros(num_chunks, dtype=np.int32)
        mb_count = 0
        while True:
            if mb_count % 10 == 1: # perform checkpointing
                checkpoint_state = minibatch_source.get_checkpoint_state()
                for i in range(3): 
                    minibatch_source.next_minibatch(20)
                minibatch_source.restore_from_checkpoint(checkpoint_state)
                mb_count +=1
                continue            

            mb = minibatch_source.next_minibatch(20)
            mb_count += 1
            if not mb:
                break

            for sequence in mb[minibatch_source.streams.x].asarray():
                sequence_x_values[int(sequence[0][0][0])] +=1

            for sequence in mb[minibatch_source.streams.y].as_sequences(C.sequence.input_variable((3,), True)):             
                sequence_y_values[int(sequence.toarray()[0][0])] += 1
            mb = None

        expected_values = np.full(num_chunks, fill_value=num_sequences_per_value, dtype=np.int32)
        assert (sequence_x_values == expected_values).all()
        assert (sequence_y_values == expected_values).all()

    # Big chunks
    d = GenDeserializer(stream_infos=streams, num_chunks=15, 
                        num_sequences=100, max_sequence_len=10)
    mbs = MinibatchSource([d], randomize=False, max_sweeps=2)
    state = mbs.get_checkpoint_state()
    mbs.restore_from_checkpoint(state)
    run_minibatch_source(mbs, num_chunks=15, num_sequences_per_value=200)
    # Randomized
    mbs = MinibatchSource([d], randomize=True, max_sweeps=2, randomization_window_in_chunks=5)
    state = mbs.get_checkpoint_state()
    mbs.restore_from_checkpoint(state)
    run_minibatch_source(mbs, num_chunks=15, num_sequences_per_value=200)

    # Small chunks of 1
    d = GenDeserializer(stream_infos=streams, num_chunks=15,
                        num_sequences=1, max_sequence_len=10)
    mbs = MinibatchSource([d], randomize=False, max_sweeps=3)
    run_minibatch_source(mbs, num_chunks=15, num_sequences_per_value=3)
    # Randomized
    mbs = MinibatchSource([d], randomize=True, max_sweeps=3, randomization_window_in_chunks=5)
    run_minibatch_source(mbs, num_chunks=15, num_sequences_per_value=3)
    def __init__(self,
                 img_map_file,
                 roi_map_file,
                 num_classes,
                 max_annotations_per_image,
                 pad_width,
                 pad_height,
                 pad_value,
                 randomize,
                 use_flipping,
                 proposal_provider,
                 proposal_iou_threshold=0.5,
                 provide_targets=False,
                 normalize_means=None,
                 normalize_stds=None,
                 max_images=None):

        self.image_si = StreamInformation("image", 0, 'dense', np.float32, (
            3,
            pad_height,
            pad_width,
        ))
        self.roi_si = StreamInformation("annotation", 1, 'dense', np.float32, (
            max_annotations_per_image,
            5,
        ))
        self.dims_si = StreamInformation("dims", 1, 'dense', np.float32, (4, ))

        if proposal_provider is not None:
            num_proposals = proposal_provider.num_proposals()
            self.proposals_si = StreamInformation("proposals", 1, 'dense',
                                                  np.float32,
                                                  (num_proposals, 4))
            self.label_targets_si = StreamInformation(
                "label_targets", 1, 'dense', np.float32,
                (num_proposals, num_classes))
            self.bbox_targets_si = StreamInformation(
                "bbox_targets", 1, 'dense', np.float32,
                (num_proposals, num_classes * 4))
            self.bbiw_si = StreamInformation("bbiw", 1, 'dense', np.float32,
                                             (num_proposals, num_classes * 4))
        else:
            self.proposals_si = None

        self.od_reader = ObjectDetectionReader(
            img_map_file, roi_map_file, num_classes, max_annotations_per_image,
            pad_width, pad_height, pad_value, randomize, use_flipping,
            proposal_provider, proposal_iou_threshold, provide_targets,
            normalize_means, normalize_stds, max_images)

        super(ObjectDetectionMinibatchSource, self).__init__()
def to_cntk_stream_infos(stream_indices):
    """
    Converts the stream indices list into a list of CNTK stream info objects.

    :param stream_indices:  The selected streams to emit, and the attribute indices they
                            correspond to.
    :return:                The StreamInformation objects as required by stream_infos(self).
    """

    # Initialise the stream-info list
    stream_infos = []

    # Process each set of stream indices in turn
    for i, stream_indices_pair in enumerate(stream_indices):
        name = stream_indices_pair[0]
        indices = stream_indices_pair[1]
        from cntk.io import StreamInformation
        stream_infos.append(
            StreamInformation(name, i, 'dense', np.float32, (len(indices), )))

    # Return the stream infos
    return stream_infos
Ejemplo n.º 6
0
    def __init__(self, path, word_config, location_config, seqlength, batchsize):
        self.word_index = load_vocab_from_file(word_config)
        self.word_position = load_vocab_location_from_file(location_config)
        self.vocab_dim = len(self.word_index)
        self.vocab_base = int(ceil(sqrt(self.vocab_dim)))
        self.reader = FileReader(path)
        self.seqlength = seqlength
        self.batchsize = batchsize
        
        self.input1 = StreamInformation("input1", 0, 'sparse', np.float32, (self.vocab_base,))
        self.input2 = StreamInformation("input2", 1, 'sparse', np.float32, (self.vocab_base,))
        self.label1 = StreamInformation("label1", 2, 'sparse', np.float32, (self.vocab_base,))
        self.label2 = StreamInformation("label2", 3, 'sparse', np.float32, (self.vocab_base,))
        self.word1 = StreamInformation("word1", 4, 'dense', np.float32, (1,))
        self.word2 = StreamInformation("word2", 5, 'dense', np.float32, (1,))

        super(DataSource, self).__init__()