def tally_job(args):
        features, data, threshold, tally_labels, tally_units, tally_units_cat, tally_both, start, end = args
        units = features.shape[1]
        size_RF = (settings.IMG_SIZE / features.shape[2], settings.IMG_SIZE / features.shape[3])
        fieldmap = ((0, 0), size_RF, size_RF)
        pd = SegmentationPrefetcher(data, categories=data.category_names(),
                                    once=True, batch_size=settings.TALLY_BATCH_SIZE,
                                    ahead=settings.TALLY_AHEAD, start=start, end=end)
        count = start
        start_time = time.time()
        last_batch_time = start_time
        for batch in pd.batches():
            batch_time = time.time()
            rate = (count - start) / (batch_time - start_time + 1e-15)
            batch_rate = len(batch) / (batch_time - last_batch_time + 1e-15)
            last_batch_time = batch_time

            print('labelprobe image index %d, items per sec %.4f, %.4f' % (count, rate, batch_rate))

            for concept_map in batch:
                count += 1
                img_index = concept_map['i']
                scalars, pixels = [], []
                for cat in data.category_names():
                    label_group = concept_map[cat]
                    shape = np.shape(label_group)
                    if len(shape) % 2 == 0:
                        label_group = [label_group]
                    if len(shape) < 2:
                        scalars += label_group
                    else:
                        pixels.append(label_group)
                for scalar in scalars:
                    tally_labels[scalar] += concept_map['sh'] * concept_map['sw']
                if pixels:
                    pixels = np.concatenate(pixels)
                    tally_label = np.bincount(pixels.ravel())
                    if len(tally_label) > 0:
                        tally_label[0] = 0
                    tally_labels[:len(tally_label)] += tally_label

                for unit_id in range(units):
                    feature_map = features[img_index][unit_id]
                    if feature_map.max() > threshold[unit_id]:
                        mask = imresize(feature_map, (concept_map['sh'], concept_map['sw']), mode='F')
                        #reduction = int(round(settings.IMG_SIZE / float(concept_map['sh'])))
                        #mask = upsample.upsampleL(fieldmap, feature_map, shape=(concept_map['sh'], concept_map['sw']), reduction=reduction)
                        indexes = np.argwhere(mask > threshold[unit_id])

                        tally_units[unit_id] += len(indexes)
                        if len(pixels) > 0:
                            tally_bt = np.bincount(pixels[:, indexes[:, 0], indexes[:, 1]].ravel())
                            if len(tally_bt) > 0:
                                tally_bt[0] = 0
                            tally_cat = np.dot(tally_bt[None,:], data.labelcat[:len(tally_bt), :])[0]
                            tally_both[unit_id,:len(tally_bt)] += tally_bt
                        for scalar in scalars:
                            tally_cat += data.labelcat[scalar]
                            tally_both[unit_id, scalar] += len(indexes)
                        tally_units_cat[unit_id] += len(indexes) * (tally_cat > 0)
Ejemplo n.º 2
0
 def __init__(self):
     if not os.path.exists(settings.OUTPUT_FOLDER):
         os.makedirs(os.path.join(settings.OUTPUT_FOLDER, 'image'))
     self.data = SegmentationData(settings.DATA_DIRECTORY,
                                  categories=settings.CATAGORIES)
     self.loader = SegmentationPrefetcher(self.data,
                                          categories=['image'],
                                          once=True,
                                          batch_size=settings.BATCH_SIZE)
     self.mean = [109.5388, 118.6897, 124.6901]
Ejemplo n.º 3
0
    def concept_indexmap(self, feat, feature_name, save=True):
        b, u, h, w = feat.shape
        print("generating concept index map ...")
        filename = os.path.join(settings.OUTPUT_FOLDER,
                                "%s-concept-map.pickle" % feature_name)
        if os.path.exists(filename):
            with open(filename, 'rb') as f:
                return pickle.load(f)

        concept_indexes = [set() for i in range(len(self.data.label))]
        pd = SegmentationPrefetcher(self.data,
                                    categories=self.data.category_names(),
                                    once=True,
                                    batch_size=settings.TALLY_BATCH_SIZE,
                                    ahead=settings.TALLY_AHEAD)
        for batch in pd.batches():
            for concept_map in batch:
                scalars, pixels = [], []
                for cat in self.data.category_names():
                    label_group = concept_map[cat]
                    shape = np.shape(label_group)
                    if len(shape) % 2 == 0:
                        label_group = [label_group]
                    if len(shape) < 2:
                        scalars += label_group
                    else:
                        pixels.append(label_group)
                for pixel in pixels:
                    pixel = imresize(pixel[0], (h, w),
                                     interp='nearest',
                                     mode='F').astype(int)
                    for hi in range(h):
                        for wi in range(w):
                            if pixel[hi, wi]:
                                concept_indexes[pixel[hi, wi]].add(
                                    (concept_map['i'], hi, wi))

        if save:
            with open(filename, 'wb') as f:
                pickle.dump(concept_indexes, f)
        return concept_indexes
Ejemplo n.º 4
0
    def generate_label(self, shape, seg_data):
        b, u, h, w = shape
        img_ind = 0
        print("generating concept index map ...")
        pd = SegmentationPrefetcher(seg_data,
                                    categories=seg_data.category_names(),
                                    once=True,
                                    batch_size=settings.TALLY_BATCH_SIZE,
                                    ahead=settings.TALLY_AHEAD)
        for batch in pd.batches():
            print("handling image index %d" % img_ind)
            for concept_map in batch:
                scalars, pixels = [], []
                for cat in seg_data.category_names():
                    label_group = concept_map[cat]
                    shape = np.shape(label_group)
                    if len(shape) % 2 == 0:
                        label_group = [label_group]
                    if len(shape) < 2:
                        scalars += label_group
                    else:
                        pixels.append(label_group)

                for i, pixel in enumerate(pixels):
                    pixels[i] = imresize(
                        pixel[0],
                        (settings.SEG_RESOLUTION, settings.SEG_RESOLUTION),
                        interp='nearest',
                        mode='F').astype(int)

                labels = np.array(pixels)
                if len(labels) == 2:
                    labels = labels[0] + (labels[0] == 0) * labels[1]
                else:
                    labels = labels[0]

                self.labels[img_ind] = labels
                img_ind += 1
Ejemplo n.º 5
0
    def generate_label(self, shape, feature_name, seg_data):
        b, u, h, w = shape
        img_ind = 0
        filename = os.path.join(settings.OUTPUT_FOLDER,
                                "%s-concept-map.npy" % feature_name)
        if os.path.exists(filename):
            print("loading concept index map ...")
            self.concept_indexes, self.labels = np.load(filename)
            return

        print("generating concept index map ...")
        pd = SegmentationPrefetcher(seg_data,
                                    categories=seg_data.category_names(),
                                    once=True,
                                    batch_size=settings.TALLY_BATCH_SIZE,
                                    ahead=settings.TALLY_AHEAD)
        for batch in pd.batches():
            print("handling image index %d" % img_ind)
            for concept_map in batch:
                scalars, pixels = [], []
                for cat in seg_data.category_names():
                    label_group = concept_map[cat]
                    shape = np.shape(label_group)
                    if len(shape) % 2 == 0:
                        label_group = [label_group]
                    if len(shape) < 2:
                        scalars += label_group
                    else:
                        pixels.append(label_group)

                if settings.SINGLE_LABEL:
                    if pixels:
                        pixel = imresize(pixels[0][0], (h, w),
                                         interp='nearest',
                                         mode='F').astype(int)
                        self.labels[img_ind] = pixel
                        for hi in range(h):
                            for wi in range(w):
                                if pixel[hi, wi]:
                                    self.concept_indexes[pixel[hi, wi]].add(
                                        (img_ind, hi, wi))
                    else:
                        self.labels[img_ind] = np.full((h, w), scalars[0])
                        for hi in range(h):
                            for wi in range(w):
                                if pixel[hi, wi]:
                                    self.concept_indexes[scalars[0]].add(
                                        (img_ind, hi, wi))

                else:
                    if len(pixels) >= 1:
                        pixels = np.concatenate(pixels, 0)
                        pixel_sm = np.zeros((len(pixels), h, w),
                                            dtype=np.int16)
                        for i in range(len(pixels)):
                            # ty, tx = (np.arange(ts) for ts in (h, w))
                            # sy, sx = (np.arange(ss) * s + o for ss, s, o in zip(pixels[i].shape, (4, 4), (5, 5)))
                            # from scipy.interpolate import RectBivariateSpline
                            # levels = RectBivariateSpline(sy, sx, pixels[i], kx=1, ky=1)(ty, tx, grid=True)
                            pixel_sm[i] = imresize(pixels[i], (h, w),
                                                   interp='nearest',
                                                   mode='F').astype(int)
                            for hi in range(h):
                                for wi in range(w):
                                    if pixel_sm[i][hi, wi]:
                                        self.concept_indexes[pixel_sm[i][
                                            hi, wi]].add((img_ind, hi, wi))
                        self.labels[img_ind] = pixel_sm
                img_ind += 1

        for label in range(self.label_size):
            self.concept_indexes[label] = np.array(
                list(self.concept_indexes[label]))
        np.save(filename, (self.concept_indexes, self.labels))