name '_get_ade20k_pairs' is not defined
So I'm trying to make function for preprocessing dataaset in semantic segmentation. but it tells me that my function is not define. Whereas is actually define on there. my code is like this
import os
import random
import numpy as np
import torch
import torch.utils.data as data

from PIL import Image, ImageOps, ImageFilter

__all__ = ['ADE20KSegmentation']

class ADE20KSegmentation(data.Dataset):
    
    BASE_DIR = 'ADEChallengeData2016'
    NUM_CLASS = 150
    CLASSES = (&quot;wall&quot;, &quot;building, edifice&quot;, &quot;sky&quot;, &quot;floor, flooring&quot;, &quot;tree&quot;,
               &quot;ceiling&quot;, &quot;road, route&quot;, &quot;bed&quot;, &quot;windowpane, window&quot;, &quot;grass&quot;,
               &quot;cabinet&quot;, &quot;sidewalk, pavement&quot;,
               &quot;person, individual, someone, somebody, mortal, soul&quot;,
               &quot;earth, ground&quot;, &quot;door, double door&quot;, &quot;table&quot;, &quot;mountain, mount&quot;,
               &quot;plant, flora, plant life&quot;, &quot;curtain, drape, drapery, mantle, pall&quot;,
               &quot;chair&quot;, &quot;car, auto, automobile, machine, motorcar&quot;,
               &quot;water&quot;, &quot;painting, picture&quot;, &quot;sofa, couch, lounge&quot;, &quot;shelf&quot;,
               &quot;house&quot;, &quot;sea&quot;, &quot;mirror&quot;, &quot;rug, carpet, carpeting&quot;, &quot;field&quot;, &quot;armchair&quot;,
               &quot;seat&quot;, &quot;fence, fencing&quot;, &quot;desk&quot;, &quot;rock, stone&quot;, &quot;wardrobe, closet, press&quot;,
               &quot;lamp&quot;, &quot;bathtub, bathing tub, bath, tub&quot;, &quot;railing, rail&quot;, &quot;cushion&quot;,
               &quot;base, pedestal, stand&quot;, &quot;box&quot;, &quot;column, pillar&quot;, &quot;signboard, sign&quot;,
               &quot;chest of drawers, chest, bureau, dresser&quot;, &quot;counter&quot;, &quot;sand&quot;, &quot;sink&quot;,
               &quot;skyscraper&quot;, &quot;fireplace, hearth, open fireplace&quot;, &quot;refrigerator, icebox&quot;,
               &quot;grandstand, covered stand&quot;, &quot;path&quot;, &quot;stairs, steps&quot;, &quot;runway&quot;,
               &quot;case, display case, showcase, vitrine&quot;,
               &quot;pool table, billiard table, snooker table&quot;, &quot;pillow&quot;,
               &quot;screen door, screen&quot;, &quot;stairway, staircase&quot;, &quot;river&quot;, &quot;bridge, span&quot;,
               &quot;bookcase&quot;, &quot;blind, screen&quot;, &quot;coffee table, cocktail table&quot;,
               &quot;toilet, can, commode, crapper, pot, potty, stool, throne&quot;,
               &quot;flower&quot;, &quot;book&quot;, &quot;hill&quot;, &quot;bench&quot;, &quot;countertop&quot;,
               &quot;stove, kitchen stove, range, kitchen range, cooking stove&quot;,
               &quot;palm, palm tree&quot;, &quot;kitchen island&quot;,
               &quot;computer, computing machine, computing device, data processor, &quot;
               &quot;electronic computer, information processing system&quot;,
               &quot;swivel chair&quot;, &quot;boat&quot;, &quot;bar&quot;, &quot;arcade machine&quot;,
               &quot;hovel, hut, hutch, shack, shanty&quot;,
               &quot;bus, autobus, coach, charabanc, double-decker, jitney, motorbus, &quot;
               &quot;motorcoach, omnibus, passenger vehicle&quot;,
               &quot;towel&quot;, &quot;light, light source&quot;, &quot;truck, motortruck&quot;, &quot;tower&quot;,
               &quot;chandelier, pendant, pendent&quot;, &quot;awning, sunshade, sunblind&quot;,
               &quot;streetlight, street lamp&quot;, &quot;booth, cubicle, stall, kiosk&quot;,
               &quot;television receiver, television, television set, tv, tv set, idiot &quot;
               &quot;box, boob tube, telly, goggle box&quot;,
               &quot;airplane, aeroplane, plane&quot;, &quot;dirt track&quot;,
               &quot;apparel, wearing apparel, dress, clothes&quot;,
               &quot;pole&quot;, &quot;land, ground, soil&quot;,
               &quot;bannister, banister, balustrade, balusters, handrail&quot;,
               &quot;escalator, moving staircase, moving stairway&quot;,
               &quot;ottoman, pouf, pouffe, puff, hassock&quot;,
               &quot;bottle&quot;, &quot;buffet, counter, sideboard&quot;,
               &quot;poster, posting, placard, notice, bill, card&quot;,
               &quot;stage&quot;, &quot;van&quot;, &quot;ship&quot;, &quot;fountain&quot;,
               &quot;conveyer belt, conveyor belt, conveyer, conveyor, transporter&quot;,
               &quot;canopy&quot;, &quot;washer, automatic washer, washing machine&quot;,
               &quot;plaything, toy&quot;, &quot;swimming pool, swimming bath, natatorium&quot;,
               &quot;stool&quot;, &quot;barrel, cask&quot;, &quot;basket, handbasket&quot;, &quot;waterfall, falls&quot;,
               &quot;tent, collapsible shelter&quot;, &quot;bag&quot;, &quot;minibike, motorbike&quot;, &quot;cradle&quot;,
               &quot;oven&quot;, &quot;ball&quot;, &quot;food, solid food&quot;, &quot;step, stair&quot;, &quot;tank, storage tank&quot;,
               &quot;trade name, brand name, brand, marque&quot;, &quot;microwave, microwave oven&quot;,
               &quot;pot, flowerpot&quot;, &quot;animal, animate being, beast, brute, creature, fauna&quot;,
               &quot;bicycle, bike, wheel, cycle&quot;, &quot;lake&quot;,
               &quot;dishwasher, dish washer, dishwashing machine&quot;,
               &quot;screen, silver screen, projection screen&quot;,
               &quot;blanket, cover&quot;, &quot;sculpture&quot;, &quot;hood, exhaust hood&quot;, &quot;sconce&quot;, &quot;vase&quot;,
               &quot;traffic light, traffic signal, stoplight&quot;, &quot;tray&quot;,
               &quot;ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, &quot;
               &quot;dustbin, trash barrel, trash bin&quot;,
               &quot;fan&quot;, &quot;pier, wharf, wharfage, dock&quot;, &quot;crt screen&quot;,
               &quot;plate&quot;, &quot;monitor, monitoring device&quot;, &quot;bulletin board, notice board&quot;,
               &quot;shower&quot;, &quot;radiator&quot;, &quot;glass, drinking glass&quot;, &quot;clock&quot;, &quot;flag&quot;)
    def __init__(self, root='/content/dataset', split='training', mode=None, transform=None,
                 base_size=520, crop_size=480, **kwargs):
        super(ADE20KSegmentation, self).__init__()
        self.root = root
        self.split = split
        self.mode = mode if mode is not None else split
        self.transform = transform
        self.base_size = base_size
        self.crop_size = crop_size
        self.images, self.mask_paths = _get_ade20k_pairs(self.root, self.split)
        assert (len(self.images) == len(self.mask_paths))
        if len(self.images) == 0:
            raise RuntimeError(&quot;Found 0 images in subfolders of: &quot; + self.root + &quot;\n&quot;)
        self.valid_classes = [7, 8, 11, 12, 13, 17, 19, 20, 21, 22,
                              23, 24, 25, 26, 27, 28, 31, 32, 33]
        self._key = np.array([-1, -1, -1, -1, -1, -1,
                              -1, -1, 0, 1, -1, -1,
                              2, 3, 4, -1, -1, -1,
                              5, -1, 6, 7, 8, 9,
                              10, 11, 12, 13, 14, 15,
                              -1, -1, 16, 17, 18])
        self._mapping = np.array(range(-1, len(self._key) - 1)).astype('int32')

    def _class_to_index(self, mask):
        values = np.unique(mask)
        for value in values:
            assert (value in self._mapping)
        index = np.digitize(mask.ravel(), self._mapping, right=True)
        return self._key[index].reshape(mask.shape)

    def __getitem__(self, index):
        img = PIL.Image.open(self.images[index]).convert('RGB')
        if self.mode == 'test':
            if self.transform is not None:
                img = self.transform(img)
            return img, os.path.basename(self.images[index])
        mask = PIL.Image.open(self.mask_paths[index])
        # synchrosized transform
        if self.mode == 'training':
            img, mask = self._sync_transform(img, mask)
        elif self.mode == 'valdation':
            img, mask = self._val_sync_transform(img, mask)
        else:
            assert self.mode == 'testval'
            img, mask = self._img_transform(img), self._mask_transform(mask)
        # general resize, normalize and toTensor
        if self.transform is not None:
            img = self.transform(img)
        return img, mask

    def _val_sync_transform(self, img, mask):
        outsize = self.crop_size
        short_size = outsize
        w, h = img.size
        if w &gt; h:
            oh = short_size
            ow = int(1.0 * w * oh / h)
        else:
            ow = short_size
            oh = int(1.0 * h * ow / w)
        img = img.resize((ow, oh), Image.BILINEAR)
        mask = mask.resize((ow, oh), Image.NEAREST)
        # center crop
        w, h = img.size
        x1 = int(round((w - outsize) / 2.))
        y1 = int(round((h - outsize) / 2.))
        img = img.crop((x1, y1, x1 + outsize, y1 + outsize))
        mask = mask.crop((x1, y1, x1 + outsize, y1 + outsize))
        # final transform
        img, mask = self._img_transform(img), self._mask_transform(mask)
        return img, mask

    def _sync_transform(self, img, mask):
        # random mirror
        if random.random() &lt; 0.5:
            img = img.transpose(Image.FLIP_LEFT_RIGHT)
            mask = mask.transpose(Image.FLIP_LEFT_RIGHT)
        crop_size = self.crop_size
        # random scale (short edge)
        short_size = random.randint(int(self.base_size * 0.5), int(self.base_size * 2.0))
        w, h = img.size
        if h &gt; w:
            ow = short_size
            oh = int(1.0 * h * ow / w)
        else:
            oh = short_size
            ow = int(1.0 * w * oh / h)
        img = img.resize((ow, oh), Image.BILINEAR)
        mask = mask.resize((ow, oh), Image.NEAREST)
        # pad crop
        if short_size &lt; crop_size:
            padh = crop_size - oh if oh &lt; crop_size else 0
            padw = crop_size - ow if ow &lt; crop_size else 0
            img = ImageOps.expand(img, border=(0, 0, padw, padh), fill=0)
            mask = ImageOps.expand(mask, border=(0, 0, padw, padh), fill=0)
        # random crop crop_size
        w, h = img.size
        x1 = random.randint(0, w - crop_size)
        y1 = random.randint(0, h - crop_size)
        img = img.crop((x1, y1, x1 + crop_size, y1 + crop_size))
        mask = mask.crop((x1, y1, x1 + crop_size, y1 + crop_size))
        # gaussian blur as in PSP
        if random.random() &lt; 0.5:
            img = img.filter(ImageFilter.GaussianBlur(
                radius=random.random()))
        # final transform
        img, mask = self._img_transform(img), self._mask_transform(mask)
        return img, mask    

    def _img_transform(self, img):
        return np.array(img)

    def _mask_transform(self, mask):
        target = self._class_to_index(np.array(mask).astype('int32'))
        return torch.LongTensor(np.array(target).astype('int32'))

    def __len__(self):
        return len(self.images)

    @property
    def classes(self):
        &quot;&quot;&quot;Category names.&quot;&quot;&quot;
        return type(self).CLASSES

    @property
    def pred_offset(self):
        return 1



    def _get_ade20k_pairs(folder, mode='training'):
        def get_path_pairs(img_folder, mask_folder):
          img_paths = []
          mask_paths = []
          for root, _, files in os.walk(img_folder):
              for filename in files:
                  if filename.endswith(&quot;.jpg&quot;):
                      imgpath = os.path.join(root, filename)
                      foldername = os.path.basename(os.path.dirname(imgpath))
                      maskname = filename.replace('images', 'annotations')
                      maskpath = os.path.join(mask_folder, foldername, maskname)
                      if os.path.isfile(imgpath) and os.path.isfile(maskpath):
                          img_paths.append(imgpath)
                          mask_paths.append(maskpath)
                      else:
                          print('cannot find the mask or image:', imgpath, maskpath)
          print('Found {} images in the folder {}'.format(len(img_paths), img_folder))
          return img_paths, mask_paths

        if split in ('training', 'validation'):
            img_folder = os.path.join(folder, 'images/' + split)
            mask_folder = os.path.join(folder, 'annotations/' + split)
            img_paths, mask_paths = get_path_pairs(img_folder, mask_folder)
            return img_paths, mask_paths
        else:
            assert split == 'trainval'
            print('trainval set')
            train_img_folder = os.path.join(folder, 'images/training')
            train_mask_folder = os.path.join(folder, 'annotations/training')
            val_img_folder = os.path.join(folder, 'images/validation')
            val_mask_folder = os.path.join(folder, 'annotations/validation')
            train_img_paths, train_mask_paths = get_path_pairs(train_img_folder, train_mask_folder)
            val_img_paths, val_mask_paths = get_path_pairs(val_img_folder, val_mask_folder)
            img_paths = train_img_paths + val_img_paths
            mask_paths = train_mask_paths + val_mask_paths
        return img_paths, mask_paths

if __name__ == '__main__':
    dataset = ADE20KSegmentation()
    img, label = dataset[0]

when I run that it always showing NameError: name '_get_ade20k_pairs' is not defined. whats wrong with my definition? because sometime it can get run and sometime it can't

I suppose you were copying the code from here and you failed to copy _get_ade20k_pairs correctly.
You need it indented with 0 tabs.
