Beispiel #1
0
    def next(self):
        crop_size = (self.data_shape[3], self.data_shape[2])
        batch_data = mx.ndarray.empty(self.data_shape)
        i = 0
        if self.pic_cnt < NUM_TEST_PICS:
            for prod in self.bff_iter:
                str_image = io.BytesIO(prod['pic']).read()
                picture = mx.img.imdecode(str_image)
                picture = mx.img.CenterCropAug(crop_size, 2)(picture)
                picture = mx.ndarray.transpose(picture, axes=(2, 0, 1))
                batch_data[i] = picture

                i += 1
                self.pic_cnt += 1

                if self.pic_cnt % 1000 == 0 or self.pic_cnt == NUM_TEST_PICS:
                    logger.info("converted {} images".format(self.pic_cnt))

                if self.pic_cnt % self.batch_size == 0:
                    return DataBatch(data=[batch_data],
                                     pad=0,
                                     index=None,
                                     provide_data=self.provide_data)
                elif self.pic_cnt == NUM_TEST_PICS:
                    return DataBatch(data=[batch_data],
                                     pad=self.batch_size - i,
                                     index=None,
                                     provide_data=self.provide_data)
        else:
            raise StopIteration
Beispiel #2
0
    def _produce_data(self):
        while True:
            images = mx.nd.zeros((self.batch_size, 3, self.crop_size, self.crop_size))
            mask_dim = int(self.crop_size * self.label_shrink_scale)
            masks =  mx.nd.zeros((self.batch_size, mask_dim * mask_dim))
            if self.provide_g_labels:
                g_labels =  mx.nd.zeros((self.batch_size, self.class_num, 1, 1))
            sub_list = self.flist_item_queue.get()


            batch_images = []
            for image_path in list(sub_list):
                buf = mx.nd.array(
                    np.frombuffer(open(os.path.join(self.im_root, image_path+".jpg"), 'rb').read(), dtype=np.uint8),
                    dtype=np.uint8, ctx=mx.cpu())
                batch_images.append(mx.image.imdecode(buf))
            batch_labels = []
            for ind, label_path in enumerate(sub_list):
                mask = Image.open(os.path.join(self.mask_root, label_path+".png"))
                mask_arr = np.array(mask, dtype=np.uint8)
                if self.provide_g_labels:
                    g_l_arr = np.zeros(self.class_num, dtype=np.uint8)
                    ul = np.unique(mask_arr)
                    ul = ul[ul != 255]
                    g_l_arr[ul] = 1
                    g_l_arr = g_l_arr.reshape(-1, 1, 1)
                    g_labels[ind][:] = g_l_arr
                batch_labels.append(mask_arr)

            for ind in range(len(batch_images)):
                im_arr = batch_images[ind]
                l_arr = batch_labels[ind]
                r_start, c_start, new_crop_size = preprocessing.calc_crop_params(im_arr, self.scale_range,
                                                                                 self.crop_size)
                if self.random_flip:
                    im_arr, l_arr = preprocessing.random_flip(im_arr, l_arr)
                im_arr, l_arr = preprocessing.pad_image(im_arr, l_arr, new_crop_size, self.rgb_mean,
                                                        ignored_label=255)
                im_arr = im_arr[r_start:r_start + new_crop_size, c_start:c_start + new_crop_size, :]
                l_arr = l_arr[r_start:r_start + new_crop_size, c_start:c_start + new_crop_size]
                batch_images[ind] = im_arr
                batch_labels[ind] = l_arr

            l_dim = int(self.crop_size * self.label_shrink_scale)
            batch_images = [mx.image.imresize(im, self.crop_size, self.crop_size, interp=1) for im in batch_images]
            for ind in range(len(batch_labels)):
                mask_arr = batch_labels[ind]
                mask_arr = Image.fromarray(mask_arr).resize((l_dim, l_dim), Image.NEAREST)
                mask_arr = np.array(mask_arr, dtype=np.uint8)
                batch_labels[ind] = mask_arr
            for i in range(len(sub_list)):
                images[i][:] = mx.nd.transpose(batch_images[i], (2, 0, 1))
                masks[i][:] = batch_labels[i].reshape(-1)

            images -= mx.nd.reshape(self.rgb_mean, (1, 3, 1, 1)).astype(np.float32)

            if self.provide_g_labels:
                self.data_queue.put(DataBatch(data=[images], label=[masks, g_labels], pad=None, index=None))
            else:
                self.data_queue.put(DataBatch(data=[images], label=[masks], pad=None, index=None))
def test_DataBatch():
    from mxnet.io import DataBatch
    import re
    batch = DataBatch(data=[mx.nd.ones((2, 3))])
    assert re.match(
        r'DataBatch: data shapes: \[\(2L?, 3L?\)\] label shapes: None', str(batch))
    batch = DataBatch(data=[mx.nd.ones((2, 3)), mx.nd.ones(
        (7, 8))], label=[mx.nd.ones((4, 5))])
    assert re.match(
        r'DataBatch: data shapes: \[\(2L?, 3L?\), \(7L?, 8L?\)\] label shapes: \[\(4L?, 5L?\)\]', str(batch))
    def _inference(self, data):
        """Internal inference methods for MXNet. Run forward computation and
        return output.

        Parameters
        ----------
        data : list of NDArray
            Preprocessed inputs in NDArray format.

        Returns
        -------
        list of NDArray
            Inference output.
        """
        # Check input shape
        check_input_shape(data, self.signature)
        data = [item.as_in_context(self.ctx) for item in data]
        self.mx_model.forward(DataBatch(data))
        data = self.mx_model.get_outputs()
        # by pass lazy evaluation get_outputs either returns a list of nd arrays
        # a list of list of ndarrays
        for d in data:
            if isinstance(d, list):
                for n in data:
                    if isinstance(n, mx.ndarray.ndarray.NDArray):
                        n.wait_to_read()
            elif isinstance(d, mx.ndarray.ndarray.NDArray):
                d.wait_to_read()
        return data
Beispiel #5
0
    def next(self):
        if self.curr_idx == len(self.idx):
            raise StopIteration
        i, j = self.idx[self.curr_idx]
        self.curr_idx += 1
        # print('sample iter: ', i, j)

	step = self.batch_size
        if self.major_axis == 1:
            data = self.nddata[i][j:j+step].T

	    #NTL -> TNL
            label = ndarray.transpose(data=self.ndlabel[i][j:j+step], axes=(1,0,2))
            label_weight = ndarray.transpose(data=self.ndlabel_weight[i][j:j+step], axes=(1,0,2))
        else:
            data = self.nddata[i][j:j+step]
            label = self.ndlabel[i][j:j+step]
            label_weight = self.ndlabel_weight[i][j:j+step]

        return DataBatch([data], [label, label_weight], pad=0,
                         bucket_key=self.buckets[i],
                         provide_data=[(self.data_name, data.shape)],
                         provide_label=[
                             (self.label_name, label.shape),
                             (self.label_weight_name, label_weight.shape)
                             ])
Beispiel #6
0
    def next(self):
        """Returns the next batch of data."""
        if self.curr_idx == len(self.idx):
            raise StopIteration
        i, j = self.idx[self.curr_idx]
        self.curr_idx += 1

        if self.major_axis == 1:
            data = self.nddata[i][j:j + self.batch_size].T
            label = self.ndlabel[i][j:j + self.batch_size].T
        else:
            data = self.nddata[i][j:j + self.batch_size]
            label = self.ndlabel[i][j:j + self.batch_size]

        return DataBatch([data], [label],
                         pad=0,
                         bucket_key=self.buckets[i],
                         provide_data=[
                             DataDesc(name=self.data_name,
                                      shape=data.shape,
                                      layout=self.layout)
                         ],
                         provide_label=[
                             DataDesc(name=self.label_name,
                                      shape=label.shape,
                                      layout=self.layout)
                         ])
    def next(self):
        """Returns the next batch of data."""
        if self.curr_idx == len(self.idx):
            raise StopIteration
        #i = batches index, j = starting record
        i, j = self.idx[self.curr_idx]
        self.curr_idx += 1

        indices = self.ndindex[i][j:j + self.batch_size]
        sentences = self.ndsent[i][j:j + self.batch_size]
        characters = self.ndchar[i][j:j + self.batch_size]
        label = self.ndlabel[i][j:j + self.batch_size]

        return DataBatch([sentences, characters], [label],
                         pad=0,
                         index=indices,
                         bucket_key=self.buckets[i],
                         provide_data=[
                             DataDesc(name=self.data_names[0],
                                      shape=sentences.shape,
                                      layout=self.layout),
                             DataDesc(name=self.data_names[1],
                                      shape=characters.shape,
                                      layout=self.layout)
                         ],
                         provide_label=[
                             DataDesc(name=self.label_name,
                                      shape=label.shape,
                                      layout=self.layout)
                         ])
Beispiel #8
0
 def next(self):
     try:
         batch1 = self.iter1.next()
     except StopIteration:
         raise StopIteration
     while True:
         try:
             batch2 = self.iter2.next()
             break
         except StopIteration:
             self.iter2.reset()
     data1 = batch1.data[0]
     data2 = batch2.data[0]
     label1 = batch1.label[0]
     label2 = batch2.label[0]
     data_parts = []
     label_parts = []
     for i in xrange(self.ctx_num):
         a1 = i * self.per_size1
         b1 = (i + 1) * self.per_size1
         a2 = i * self.per_size2
         b2 = (i + 1) * self.per_size2
         _data1 = self.part_slice(data1, a1, b1)
         _data2 = self.part_slice(data2, a2, b2)
         _label1 = self.part_slice(label1, a1, b1)
         _label2 = self.part_slice(label2, a2, b2)
         #_target_label = mx.ndarray.ones_like(_target_label)*9999
         data_parts.append(_data1)
         data_parts.append(_data2)
         label_parts.append(_label1)
         label_parts.append(_label2)
     data = mx.ndarray.concat(*data_parts, dim=0)
     label = mx.ndarray.concat(*label_parts, dim=0)
     return DataBatch(data=(data, ), label=(label, ))
    def inference(self, model_input):
        """
        Internal inference methods for MXNet. Run forward computation and
        return output.

        :param model_input: list of NDArray
            Preprocessed inputs in NDArray format.
        :return: list of NDArray
            Inference output.
        """
        if self.error is not None:
            return None

        # Check input shape
        check_input_shape(model_input, self.signature)
        model_input = [item.as_in_context(self.mxnet_ctx) for item in model_input]
        self.mx_model.forward(DataBatch(model_input))
        model_input = self.mx_model.get_outputs()
        # by pass lazy evaluation get_outputs either returns a list of nd arrays
        # a list of list of NDArray
        for d in model_input:
            if isinstance(d, list):
                for n in model_input:
                    if isinstance(n, mx.ndarray.ndarray.NDArray):
                        n.wait_to_read()
            elif isinstance(d, mx.ndarray.ndarray.NDArray):
                d.wait_to_read()
        return model_input
Beispiel #10
0
 def next(self):
     if self.iter_next():
         return DataBatch(data=self.getdata(),
                          label=self.getlabel(),
                          pad=self.getpad())
     else:
         raise StopIteration
Beispiel #11
0
    def next(self):
        """Get next data batch from iterator.

        Returns
        -------
        DataBatch
            The data of next batch.

        Raises
        ------
        StopIteration
            If the end of the data is reached.
        """
        if self.iter_next():
            with self.lock:
                data, label = next(self.itr)
                data = np.transpose(data, (0, 3, 1, 2))  # NCHW
                label = label[:, :, :, None]
                label = np.transpose(label, (0, 3, 1, 2))  # NCHW
                data = [[
                    mx.nd.array(data[self.batch_size * i:self.batch_size *
                                     (i + 1)])
                ] for i in range(self.gpu_nums)
                        ]  # multi-gpu distribute data, time-consuming!!!
                label = [[
                    mx.nd.array(label[self.batch_size * i:self.batch_size *
                                      (i + 1)])
                ] for i in range(self.gpu_nums)]
                return DataBatch(data=data, label=label, \
                    pad=0, index= None, provide_data=self.provide_data,provide_label=self.provide_label)
        else:
            raise StopIteration
Beispiel #12
0
    def next(self):
        if self._cur_pointer + self._batch_size < len(self._name_id):
            cur_images = np.zeros(
                (self._batch_size, 3, self._crop_h, self._crop_w))
            cur_labels = np.zeros(
                (self._batch_size, 1, self._crop_h / self._label_stride,
                 self._crop_w / self._label_stride))
            for i in range(self._batch_size):
                image_path, label_path = self.parse_lst(
                    self._name_list[self._name_id[i + self._cur_pointer]])

                ori_image = np.array(Image.open(image_path))
                ori_label = np.array(Image.open(label_path))
                if self._use_flip:
                    do_flip = np.random.randint(2) == 0
                    if do_flip:
                        ori_image = ori_image[:, ::-1, :]
                        ori_label = ori_label[:, ::-1]

                crop_image, crop_label = self.random_crop(image=ori_image,
                                                          label=ori_label)
                cur_images[i, ...] = crop_image
                cur_labels[i, ...] = crop_label
        else:
            raise StopIteration
        self._cur_pointer += self._batch_size
        return DataBatch(data=[mx.nd.array(cur_images)],
                         label=[mx.nd.array(cur_labels)])
Beispiel #13
0
 def next(self):
     if self.iter_next():
         while True:
             data, label = self.get_data_label()
             return DataBatch(data=data, label=label, pad=self.getpad(), index=self.getindex())
     else:
         raise StopIteration
Beispiel #14
0
def predict(url):
    # global net
    start_time = time.time()

    img = get_image(url, show=False)
    # compute the predict probabilities
    net.forward(DataBatch([mx.nd.array(img)]))
    # net.forward(list([mx.nd.array(img)]))
    prob = net.get_outputs()[0].asnumpy()
    # print the top-2
    prob = np.squeeze(prob)
    classes = np.argsort(prob)[::-1]

    # print("Total size of the features = %s" %(len(classes)))
    index, value = max(enumerate(prob), key=operator.itemgetter(1))

    # for i in classes:
        # print('probability=%f, label=%s' % (prob[i], labels[i]))

    end_time = time.time()
    # print("Total execution time: {} seconds".format(end_time - start_time))

    # with open(("errors/classification_time.csv"),"a") as f:
    #     f.write(','.join([url, str(end_time - start_time),"\n"]))
    return labels[index], value,prob[0],prob[1],(end_time - start_time)
Beispiel #15
0
 def next(self):
     """return one dict which contains "data" and "label" """
     if self.iter_next():
         # self.data, self.label,\
         start = time.time()
         self.batch_image, self.batch_label, self.batch_img_size, self.batch_original_img_size, \
         self.batch_image_name, self.batch_label_name = self.getdata()
         # end = time.time()
         # print 'load data: %s' % (end - start)
         # print self.batch_image_name
         self.cursor += self.batch_size
         # return DataBatch(self.batch_image,
         #                  label=self.batch_label,
         #                  )
         # print mx.nd.concatenate(self.batch_image)
         # print self.batch_label
         # return {
         #     self.data_name: mx.nd.array(self.batch_image),
         #     self.label_name: mx.nd.array(self.batch_label),
         # }
         return DataBatch(data=[mx.nd.array(self.batch_image)],
                          label=[mx.nd.array(self.batch_label)],
                          )
     else:
         raise StopIteration
Beispiel #16
0
 def next(self):
     source_batch = self.source_iter.next()
     target_batch = self.target_iter.next()
     source_data = source_batch.data[0]
     target_data = target_batch.data[0]
     source_label = source_batch.label[0]
     target_label = target_batch.label[0]
     data_parts = []
     label_parts = []
     for i in xrange(self.gpu_num):
         _source_data = self.part_slice(source_data, i)
         _target_data = self.part_slice(target_data, i)
         _source_label = self.part_slice(source_label, i)
         _target_label = self.part_slice(target_label, i)
         #_target_label = mx.ndarray.ones_like(_target_label)*9999
         data_parts.append(_source_data)
         data_parts.append(_target_data)
         label_parts.append(_source_label)
         label_parts.append(_target_label)
     self.data = mx.ndarray.concat(*data_parts, dim=0)
     self.label = mx.ndarray.concat(*label_parts, dim=0)
     #self.data = mx.ndarray.concat( source_data, target_data, dim=0)
     #print(self.data.shape)
     #self.label = mx.ndarray.concat(source_batch.label[0], target_batch.label[0], dim=0)
     #provide_data = [(self.data_name, self.data.shape)]
     #provide_label = [(self.label_name, self.label.shape)]
     #print(self.label.shape)
     #return {self.data_name  :  [self.data],
     #        self.label_name :  [self.label]}
     return DataBatch(data=(self.data, ), label=(self.label, ))
Beispiel #17
0
    def next(self):
        batches = self.iters.next()
        #for idx,label in enumerate(self.label_list):
        #    print(batches.label[0].shape)

        split_label_reshape = []
        split_label = mx.nd.split(batches.label[0],
                                  axis=1,
                                  num_outputs=len(self.label_list_origin))

        batch_size = split_label[0].size
        #for split in split_label:
        index = np.random.permutation(batch_size)

        lam = 0.8
        mixed_x = batches.data[0] * lam + (1.0 -
                                           lam) * batches.data[0][index, :]
        batches.data[0] = mixed_x

        split_label_reshape = [split.reshape([
            0,
        ]) for split in split_label]
        split_label_reshape_mixup = [
            split.reshape([
                0,
            ])[index] for split in split_label
        ]
        split_label_reshape = split_label_reshape + split_label_reshape_mixup

        #print(batches.data[0].shape)
        if self.summary_writer is not None:
            self.summary_writer.add_image('image', (batches.data[0] + 123.68) /
                                          274.740997314)

        return DataBatch(data=batches.data, label=split_label_reshape)
    def get_next(self):
        """in zoo, each DataBatch has pic of one time step of sample of #batch_size """
        assert (self.cursor < self.num_data), "DataIter needs reset."

        if self.cursor + self.batch_size <= self.num_data:
            data = [
                array(d[1][self.cursor:self.cursor + self.batch_size])
                for d in self.data
            ]
            label = [
                array(l[1][self.cursor:self.cursor + self.batch_size])
                for l in self.label
            ]
        else:
            pad = self.batch_size - self.num_data + self.cursor
            data = [
                array(np.concatenate((d[1][self.cursor:], d[1][:pad]), axis=0))
                for d in self.data
            ]
            label = [
                array(np.concatenate((l[1][self.cursor:], l[1][:pad]), axis=0))
                for l in self.label
            ]

        batch = DataBatch(data=data,
                          label=label,
                          pad=self.getpad(),
                          index=None)

        return batch
def create_batch():
    length = input_shape[0] * input_shape[1] * input_shape[2] * input_shape[3]
    frame = np.zeros(length, dtype=np.float32)
    frame[:] = 1.0
    batch_frame = [mx.nd.array(frame.reshape(input_shape))]
    batch_shape = [DataDesc('data', batch_frame[0].shape)]
    batch = DataBatch(data=batch_frame, provide_data=batch_shape)
    return batch
Beispiel #20
0
 def next(self):
     if self.iter_next():
         #print("return batch")
         return DataBatch(self.getdata(), self.getlabel())
     else:
         # print("raise StopIteration")
         # self.reset()
         raise StopIteration
Beispiel #21
0
    def _produce_data(self):
        while True:
            mask_dim = int(self.im_size * self.label_shrink_scale)
            images = mx.nd.zeros(
                (self.batch_size, 3, self.im_size, self.im_size), ctx=mx.cpu())
            labels = mx.nd.zeros((self.batch_size, 1, 1, self.class_num),
                                 ctx=mx.cpu())
            cues = mx.nd.zeros(
                (self.batch_size, self.class_num, mask_dim, mask_dim),
                ctx=mx.cpu())
            small_ims = mx.nd.zeros((self.batch_size, mask_dim, mask_dim, 3),
                                    ctx=mx.cpu())
            sub_list = self.flist_item_queue.get()

            batch_images = []
            for ind, item_name in enumerate(sub_list):
                buf = mx.nd.array(np.frombuffer(open(
                    os.path.join(self.im_root, item_name + ".jpg"),
                    'rb').read(),
                                                dtype=np.uint8),
                                  dtype=np.uint8,
                                  ctx=mx.cpu())
                im = mx.image.imdecode(buf)
                tmp = self.cue_dict[item_name + "_cues"]
                cue = np.zeros((self.class_num, mask_dim, mask_dim))
                cue[tmp[0], tmp[1], tmp[2]] = 1

                if self.random_flip and np.random.rand() > 0.5:
                    im = mx.nd.flip(im, axis=1)
                    cue = cue[:, :, ::-1]
                batch_images.append(im)
                label = np.zeros(self.class_num)
                label[self.cue_dict[item_name + "_labels"]] = 1
                labels[ind][:] = label.reshape(1, 1, -1)
                cues[ind][:] = cue

            batch_images = [
                mx.image.imresize(im, self.im_size, self.im_size, interp=1)
                for im in batch_images
            ]
            sm_images = [
                mx.image.imresize(im, mask_dim, mask_dim, interp=1)
                for im in batch_images
            ]

            for i in range(len(sub_list)):
                images[i][:] = mx.nd.transpose(batch_images[i], (2, 0, 1))
                small_ims[i][:] = sm_images[i]

            images -= self.rgb_mean

            self.data_queue.put(
                DataBatch(data=[images, small_ims],
                          label=[labels, cues],
                          pad=None,
                          index=None))
 def next(self):
     """return one dict which contains "data" and "label" """
     if self.iter_next():
         self.data, self.label = self._read()
         res = DataBatch(data=[mx.nd.array(self.data[0][1])],
                         label=[mx.nd.array(self.label[0][1])],
                         index=None)
         return res
     else:
         raise StopIteration
Beispiel #23
0
    def next(self):
        if self._fetcher.iter_next():
            tic = time.time()
            data_batch = self._fetcher.get()
            print 'Waited for {} seconds'.format(time.time() - tic)
        else:
            raise StopIteration

        return DataBatch(data=[array(data_batch[0])],
                         label=[array(data_batch[1])])
Beispiel #24
0
 def next(self):
     if (self.regenerate):
         self.regenerate = False
         self._generate_next()  # This can raise StopIteration
     if self.iter_next():
         return DataBatch(data=self.getdata(), label=self.getlabel(), \
                 pad=self.getpad(), index=None)
     else:
         self.regenerate = True
         return self.next()
Beispiel #25
0
 def next(self):
     """For DataBatch definition, see this page:
        https://mxnet.incubator.apache.org/api/python/io.html#mxnet.io.DataBatch
     """
     return DataBatch(data=(self.data, ),
                      label=(self.label, ),
                      pad=0,
                      index=None,
                      provide_data=self.provide_data,
                      provide_label=self.provide_label)
 def next(self):
     self.cur_iter += 1
     if self.cur_iter <= self.max_iter:
         return DataBatch(data=(self.data, ),
                          label=(self.label, ),
                          pad=0,
                          index=None,
                          provide_data=self.provide_data,
                          provide_label=self.provide_label)
     else:
         raise StopIteration
Beispiel #27
0
 def next(self):
     batches = self.iters.next()
     #for idx,label in enumerate(self.label_list):
     #    print(batches.label[0].shape)
     split_label_reshape =[]
     split_label = mx.nd.split(batches.label[0], axis=1, num_outputs=len(self.label_list)) 
     
     split_label_reshape = [split.reshape([0,]) for split in split_label]
         
     return DataBatch(data=batches.data,
                      label=split_label_reshape)
Beispiel #28
0
 def next(self):
     try:
         if self.iter_next():
             data, label = self.imgs_queue.get(block=True)
             return DataBatch(data=[data],
                              label=[label],
                              pad=self.getpad(),
                              index=self.getindex())
         else:
             raise StopIteration
     except Exception as ex:
         print str(ex)
Beispiel #29
0
    def next(self):
        if self.cursor >= self.size:
            raise StopIteration

        data, label = self.result_queue.get()

        self.cursor += 1

        return DataBatch(data=data,
                         label=label,
                         provide_data=self.provide_data,
                         provide_label=self.provide_label)
Beispiel #30
0
    def next(self):
        if self.cursor + self.pair_batch_size >= self.num_data:
            raise StopIteration

        data, label = self._fetch_data()
        self.cursor += self.pair_batch_size

        return DataBatch(data=data,
                         label=label,
                         pad=0,
                         index=None,
                         provide_data=self.provide_data,
                         provide_label=self.provide_label)