コード例 #1
0
    def do_aug(self, data, label, annot):
        if self.vis:
            self.img_num += 1
            # if self.img_num<=self.vis:
            #  filename = './vis/raw_%d.jpg' % (self.img_num)
            #  print('save', filename)
            #  draw = data.copy()
            #  for i in xrange(label.shape[0]):
            #    cv2.circle(draw, (label[i][1], label[i][0]), 1, (0, 0, 255), 2)
            #  scipy.misc.imsave(filename, draw)

        rotate = 0
        # scale = 1.0
        if 'scale' in annot:
            scale = annot['scale']
        else:
            scale = max(data.shape[0], data.shape[1])
        if 'center' in annot:
            center = annot['center']
        else:
            center = np.array((data.shape[0] / 2, data.shape[1] / 2))
        max_retry = 3
        if self.aug_level == 0:
            max_retry = 6
        retry = 0
        found = False
        _scale = scale
        while retry < max_retry:
            retry += 1
            succ = True
            if self.aug_level > 0:
                rotate = np.random.randint(-40, 40)
                # rotate2 = np.random.randint(-40, 40)
                rotate2 = 0
                scale_config = 0.2
                # rotate = 0
                # scale_config = 0.0
                _scale = min(
                    1 + scale_config,
                    max(1 - scale_config,
                        (np.random.randn() * scale_config) + 1))
                _scale *= scale
                _scale = int(_scale)
                # translate = np.random.randint(-5, 5, size=(2,))
                # center += translate
            if self.mode == 1:
                cropped = img_helper.crop2(
                    data,
                    center,
                    _scale, (self.input_img_size, self.input_img_size),
                    rot=rotate)
                if self.use_coherent == 2:
                    cropped2 = img_helper.crop2(
                        data,
                        center,
                        _scale, (self.input_img_size, self.input_img_size),
                        rot=rotate2)
            else:
                cropped = img_helper.crop(
                    data,
                    center,
                    _scale, (self.input_img_size, self.input_img_size),
                    rot=rotate)
            # print('cropped', cropped.shape)
            label_out = np.zeros(self.label_shape, dtype=np.float32)
            label2_out = np.zeros(self.label_shape, dtype=np.float32)
            G = 0
            # if self.use_coherent:
            #    G = 1
            _g = G
            if G == 0:
                _g = 1
            # print('shape', label.shape, label_out.shape)
            for i in xrange(label.shape[0]):
                pt = label[i].copy()
                pt = pt[::-1]
                # print('before gaussian', label_out[i].shape, pt.shape)
                _pt = pt.copy()
                trans = img_helper.transform(
                    _pt,
                    center,
                    _scale, (self.output_label_size, self.output_label_size),
                    rot=rotate)
                # print(trans.shape)
                if not img_helper.gaussian(label_out[i], trans, _g):
                    succ = False
                    break
                if self.use_coherent == 2:
                    _pt = pt.copy()
                    trans2 = img_helper.transform(
                        _pt,
                        center,
                        _scale,
                        (self.output_label_size, self.output_label_size),
                        rot=rotate2)
                    if not img_helper.gaussian(label2_out[i], trans2, _g):
                        succ = False
                        break
            if not succ:
                if self.aug_level == 0:
                    _scale += 20
                continue

            if self.use_coherent == 1:
                cropped2 = np.copy(cropped)
                for k in xrange(cropped2.shape[2]):
                    cropped2[:, :, k] = np.fliplr(cropped2[:, :, k])
                label2_out = np.copy(label_out)
                for k in xrange(label2_out.shape[0]):
                    label2_out[k, :, :] = np.fliplr(label2_out[k, :, :])
                new_label2_out = np.copy(label2_out)
                for item in self.mirror_set:
                    mir = (item[0] - 1, item[1] - 1)
                    new_label2_out[mir[1]] = label2_out[mir[0]]
                    new_label2_out[mir[0]] = label2_out[mir[1]]
                label2_out = new_label2_out
            elif self.use_coherent == 2:
                pass
            elif ((self.aug_level > 0 and np.random.rand() < 0.5)
                  or self.force_mirror):  # flip aug
                for k in xrange(cropped.shape[2]):
                    cropped[:, :, k] = np.fliplr(cropped[:, :, k])
                for k in xrange(label_out.shape[0]):
                    label_out[k, :, :] = np.fliplr(label_out[k, :, :])
                new_label_out = np.copy(label_out)
                for item in self.mirror_set:
                    mir = (item[0] - 1, item[1] - 1)
                    new_label_out[mir[1]] = label_out[mir[0]]
                    new_label_out[mir[0]] = label_out[mir[1]]
                label_out = new_label_out

            if G == 0:
                for k in xrange(label.shape[0]):
                    ind = np.unravel_index(np.argmax(label_out[k], axis=None),
                                           label_out[k].shape)
                    label_out[k, :, :] = 0.0
                    label_out[k, ind[0], ind[1]] = 1.0
                    if self.use_coherent:
                        ind = np.unravel_index(
                            np.argmax(label2_out[k], axis=None),
                            label2_out[k].shape)
                        label2_out[k, :, :] = 0.0
                        label2_out[k, ind[0], ind[1]] = 1.0
            found = True
            break

        # self.stats[0]+=1
        if not found:
            # self.stats[1]+=1
            # print('find aug error', retry)
            # print(self.stats)
            return None

        if self.vis > 0 and self.img_num <= self.vis:
            print('crop', data.shape, center, _scale, rotate, cropped.shape)
            filename = './vis/cropped_%d.jpg' % (self.img_num)
            print('save', filename)
            draw = cropped.copy()
            alabel = label_out.copy()
            for i in xrange(label.shape[0]):
                a = cv2.resize(alabel[i],
                               (self.input_img_size, self.input_img_size))
                ind = np.unravel_index(np.argmax(a, axis=None), a.shape)
                cv2.circle(draw, (ind[1], ind[0]), 1, (0, 0, 255), 2)
            scipy.misc.imsave(filename, draw)
        if not self.use_coherent:
            return cropped, label_out
        else:
            rotate2 = 0
            r = rotate - rotate2
            # r = rotate2 - rotate
            r = math.pi * r / 180
            cos_r = math.cos(r)
            sin_r = math.sin(r)
            # c = cropped2.shape[0]//2
            # M = cv2.getRotationMatrix2D( (c, c), rotate2-rotate, 1)
            M = np.array([[cos_r, -1 * sin_r, 0.0], [sin_r, cos_r, 0.0]])
            # print(M)
            # M=np.array([16, 15, 14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1,  0,
            #    26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 27, 28, 29, 30, 35, 34, 33,
            #    32, 31, 45, 44, 43, 42, 47, 46, 39, 38, 37, 36, 41, 40, 54, 53, 52,
            #    51, 50, 49, 48, 59, 58, 57, 56, 55, 64, 63, 62, 61, 60, 67, 66, 65])
            return cropped, label_out, cropped2, label2_out, M.flatten()
コード例 #2
0
    def get_data(self, data, label, annot):
        if self.vis:
            self.img_num += 1
            #if self.img_num<=self.vis:
            #  filename = './vis/raw_%d.jpg' % (self.img_num)
            #  print('save', filename)
            #  draw = data.copy()
            #  for i in xrange(label.shape[0]):
            #    cv2.circle(draw, (label[i][1], label[i][0]), 1, (0, 0, 255), 2)
            #  scipy.misc.imsave(filename, draw)

        rotate = 0
        #scale = 1.0
        if 'scale' in annot:
            scale = annot['scale']
        else:
            scale = max(data.shape[0], data.shape[1])
        if 'center' in annot:
            center = annot['center']
        else:
            center = np.array((data.shape[1] / 2, data.shape[0] / 2))
        max_retry = 3
        if self.aug_level == 0:  #validation mode
            max_retry = 6
        retry = 0
        found = False
        base_scale = scale
        while retry < max_retry:
            retry += 1
            succ = True
            _scale = base_scale
            if self.aug_level > 0:
                rotate = np.random.randint(-40, 40)
                scale_config = 0.2
                #rotate = 0
                #scale_config = 0.0
                scale_ratio = min(
                    1 + scale_config,
                    max(1 - scale_config,
                        (np.random.randn() * scale_config) + 1))
                _scale = int(base_scale * scale_ratio)
                #translate = np.random.randint(-5, 5, size=(2,))
                #center += translate
                data_out, trans = img_helper.transform(data, center,
                                                       self.input_img_size,
                                                       _scale, rotate)
            else:
                data, label, trans = img_helper.preprocess(
                    data, label, data.shape[0])
                data_out, trans = img_helper.transform2(
                    data, label, self.input_img_size)
            #data_out = img_helper.crop2(data, center, _scale, (self.input_img_size, self.input_img_size), rot=rotate)
            label_out = np.zeros(self.label_shape, dtype=np.float32)
            #print('out shapes', data_out.shape, label_out.shape)
            for i in xrange(label.shape[0]):
                pt = label[i].copy()
                #pt = pt[::-1]
                npt = img_helper.transform_pt(pt, trans)
                if npt[0] >= data_out.shape[1] or npt[1] >= data_out.shape[
                        0] or npt[0] < 0 or npt[1] < 0:
                    succ = False
                    #print('err npt', npt)
                    break
                if config.losstype == 'heatmap':
                    pt_scale = float(
                        self.output_label_size) / self.input_img_size
                    npt *= pt_scale
                    npt = npt.astype(np.int32)
                    img_helper.gaussian(label_out[i], npt, config.gaussian)
                else:
                    label_out[i] = (npt / self.input_img_size)
                #print('before gaussian', label_out[i].shape, pt.shape)
                #trans = img_helper.transform(pt, center, _scale, (self.output_label_size, self.output_label_size), rot=rotate)
                #print(trans.shape)
                #if not img_helper.gaussian(label_out[i], trans, _g):
                #    succ = False
                #    break
            if not succ:
                if self.aug_level == 0:
                    base_scale += 20
                continue

            flip_data_out = None
            flip_label_out = None
            if config.net_coherent:
                flip_data_out, flip_label_out = self.get_flip(
                    data_out, label_out)
            elif ((self.aug_level > 0 and np.random.rand() < 0.5)
                  or self.force_mirror):  #flip aug
                flip_data_out, flip_label_out = self.get_flip(
                    data_out, label_out)
                data_out, label_out = flip_data_out, flip_label_out

            found = True
            break

        #self.stats[0]+=1
        if not found:
            #self.stats[1]+=1
            #print('find aug error', retry)
            #print(self.stats)
            #print('!!!ERR')
            return None
        #print('found with scale', _scale, rotate)

        if self.vis > 0 and self.img_num <= self.vis:
            print('crop', data.shape, center, _scale, rotate, data_out.shape)
            filename = './vis/cropped_%d.jpg' % (self.img_num)
            print('save', filename)
            draw = data_out.copy()
            alabel = label_out.copy()
            for i in xrange(label.shape[0]):
                a = cv2.resize(alabel[i],
                               (self.input_img_size, self.input_img_size))
                ind = np.unravel_index(np.argmax(a, axis=None), a.shape)
                cv2.circle(draw, (ind[1], ind[0]), 1, (0, 0, 255), 2)
            scipy.misc.imsave(filename, draw)
            filename = './vis/raw_%d.jpg' % (self.img_num)
            scipy.misc.imsave(filename, data)

        return data_out, label_out, flip_data_out, flip_label_out
コード例 #3
0
ファイル: data.py プロジェクト: LHQ0308/insightface
    def do_aug(self, data, label, annot):
      if self.vis:
        self.img_num+=1
        #if self.img_num<=self.vis:
        #  filename = './vis/raw_%d.jpg' % (self.img_num)
        #  print('save', filename)
        #  draw = data.copy()
        #  for i in xrange(label.shape[0]):
        #    cv2.circle(draw, (label[i][1], label[i][0]), 1, (0, 0, 255), 2)
        #  scipy.misc.imsave(filename, draw)

      rotate = 0
      #scale = 1.0
      if 'scale' in annot:
          scale = annot['scale']
      else:
          scale = max(data.shape[0], data.shape[1])
      if 'center' in annot:
        center = annot['center']
      else:
        center = np.array( (data.shape[0]/2, data.shape[1]/2) )
      max_retry = 3
      if self.aug_level==0:
          max_retry = 6
      retry = 0
      found = False
      _scale = scale
      while retry<max_retry:
          retry+=1
          succ = True
          if self.aug_level>0:
            rotate = np.random.randint(-40, 40)
            #rotate2 = np.random.randint(-40, 40)
            rotate2 = 0
            scale_config = 0.2
            #rotate = 0
            #scale_config = 0.0
            _scale = min(1+scale_config, max(1-scale_config, (np.random.randn() * scale_config) + 1))
            _scale *= scale
            _scale = int(_scale)
            #translate = np.random.randint(-5, 5, size=(2,))
            #center += translate
          if self.mode==1:
              cropped = img_helper.crop2(data, center, _scale, (self.input_img_size, self.input_img_size), rot=rotate)
              if self.use_coherent==2:
                cropped2 = img_helper.crop2(data, center, _scale, (self.input_img_size, self.input_img_size), rot=rotate2)
          else:
              cropped = img_helper.crop(data, center, _scale, (self.input_img_size, self.input_img_size), rot=rotate)
          #print('cropped', cropped.shape)
          label_out = np.zeros(self.label_shape, dtype=np.float32)
          label2_out = np.zeros(self.label_shape, dtype=np.float32)
          G = 0
          #if self.use_coherent:
          #    G = 1
          _g = G
          if G==0:
              _g = 1
          #print('shape', label.shape, label_out.shape)
          for i in xrange(label.shape[0]):
            pt = label[i].copy()
            pt = pt[::-1]
            #print('before gaussian', label_out[i].shape, pt.shape)
            _pt = pt.copy()
            trans = img_helper.transform(_pt, center, _scale, (self.output_label_size, self.output_label_size), rot=rotate)
            #print(trans.shape)
            if not img_helper.gaussian(label_out[i], trans, _g):
                succ = False
                break
            if self.use_coherent==2:
                _pt = pt.copy()
                trans2 = img_helper.transform(_pt, center, _scale, (self.output_label_size, self.output_label_size), rot=rotate2)
                if not img_helper.gaussian(label2_out[i], trans2, _g):
                    succ = False
                    break
          if not succ:
              if self.aug_level==0:
                  _scale+=20
              continue

          
          if self.use_coherent==1:
            cropped2 = np.copy(cropped)
            for k in xrange(cropped2.shape[2]):
                cropped2[:,:,k] = np.fliplr(cropped2[:,:,k])
            label2_out = np.copy(label_out)
            for k in xrange(label2_out.shape[0]):
                label2_out[k,:,:] = np.fliplr(label2_out[k,:,:])
            new_label2_out = np.copy(label2_out)
            for item in self.mirror_set:
                mir = (item[0]-1, item[1]-1)
                new_label2_out[mir[1]] = label2_out[mir[0]]
                new_label2_out[mir[0]] = label2_out[mir[1]]
            label2_out = new_label2_out
          elif self.use_coherent==2:
            pass
          elif ((self.aug_level>0 and np.random.rand() < 0.5) or self.force_mirror): #flip aug
              for k in xrange(cropped.shape[2]):
                  cropped[:,:,k] = np.fliplr(cropped[:,:,k])
              for k in xrange(label_out.shape[0]):
                  label_out[k,:,:] = np.fliplr(label_out[k,:,:])
              new_label_out = np.copy(label_out)
              for item in self.mirror_set:
                  mir = (item[0]-1, item[1]-1)
                  new_label_out[mir[1]] = label_out[mir[0]]
                  new_label_out[mir[0]] = label_out[mir[1]]
              label_out = new_label_out

          if G==0:
              for k in xrange(label.shape[0]):
                  ind = np.unravel_index(np.argmax(label_out[k], axis=None), label_out[k].shape)
                  label_out[k,:,:] = 0.0
                  label_out[k,ind[0],ind[1]] = 1.0
                  if self.use_coherent:
                      ind = np.unravel_index(np.argmax(label2_out[k], axis=None), label2_out[k].shape)
                      label2_out[k,:,:] = 0.0
                      label2_out[k,ind[0],ind[1]] = 1.0
          found = True
          break


      #self.stats[0]+=1
      if not found:
          #self.stats[1]+=1
          #print('find aug error', retry)
          #print(self.stats)
          return None

      if self.vis>0 and self.img_num<=self.vis:
        print('crop', data.shape, center, _scale, rotate, cropped.shape)
        filename = './vis/cropped_%d.jpg' % (self.img_num)
        print('save', filename)
        draw = cropped.copy()
        alabel = label_out.copy()
        for i in xrange(label.shape[0]):
          a = cv2.resize(alabel[i], (self.input_img_size, self.input_img_size))
          ind = np.unravel_index(np.argmax(a, axis=None), a.shape)
          cv2.circle(draw, (ind[1], ind[0]), 1, (0, 0, 255), 2)
        scipy.misc.imsave(filename, draw)
      if not self.use_coherent:
          return cropped, label_out
      else:
          rotate2 = 0
          r = rotate - rotate2
          #r = rotate2 - rotate
          r = math.pi*r/180
          cos_r = math.cos(r)
          sin_r = math.sin(r)
          #c = cropped2.shape[0]//2
          #M = cv2.getRotationMatrix2D( (c, c), rotate2-rotate, 1)
          M = np.array( [ 
              [cos_r, -1*sin_r, 0.0], 
              [sin_r, cos_r, 0.0]
              ] )
          #print(M)
          #M=np.array([16, 15, 14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1,  0,
          #    26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 27, 28, 29, 30, 35, 34, 33,
          #    32, 31, 45, 44, 43, 42, 47, 46, 39, 38, 37, 36, 41, 40, 54, 53, 52,
          #    51, 50, 49, 48, 59, 58, 57, 56, 55, 64, 63, 62, 61, 60, 67, 66, 65])
          return cropped, label_out, cropped2, label2_out, M.flatten()