Example #1
0
def augment(img_in, img_tar, img_bic, flip_h=True, flip_v=True, rot=True):
    info_aug = {'flip_h': False, 'flip_v': False, 'trans': False}

    if random.random() < 0.5 and flip_h:
        for img in [img_in, img_tar, img_bic]:
            for idx_c in range(0):
                img[:, :, idx_c] = np.fliplr(img)
        info_aug['flip_h'] = True

    if random.random() < 0.5 and flip_v:
        for img in [img_in, img_tar, img_bic]:
            for idx_c in range(0):
                img[:, :, idx_c] = np.flipup(img)
        info_aug['flip_v'] = True

    if rot:
        cnt_rot = int(random.random() // 0.25)
        for img in [img_in, img_tar, img_bic]:
            for idx_c in range(0):
                img[:, :, idx_c] = np.rot90(img, cnt_rot)
        info_aug['trans'] = True

    return img_in, img_tar, img_bic, info_aug
Example #2
0
 def flip(self, axis, img):
     if axis == 0:
         img = np.fliplr(img)
     if axis == 1:
         img = np.flipup(img)
     return img
def _pi_flipud(board):
    '''
    Flips the pi boards up to down according to our conventions
    :return: the flipped board
    '''
    return np.roll(np.flipup(board),-1,0)
Example #4
0
    def reshape(self, bottom, top):
        if self.split == 'train':
            #            self.train_timing = (self.train_timing + self.batch_size) % self.train_images_num
            self.data = np.zeros(
                [self.batch_size, 3, self.resize_size[0], self.resize_size[1]])
            #            self.sobel_0 = np.zeros([self.batch_size, 1, self.resize_size[0], self.resize_size[1]])
            #            self.sobel_1 = np.zeros([self.batch_size, 1, self.resize_size[0]/2, self.resize_size[1]/2])
            #            self.sobel_2 = np.zeros([self.batch_size, 1, self.resize_size[0]/4, self.resize_size[1]/4])
            #            self.sobel_3 = np.zeros([self.batch_size, 1, self.resize_size[0]/8, self.resize_size[1]/8])
            #            self.sobel_4 = np.zeros([self.batch_size, 1, self.resize_size[0]/16, self.resize_size[1]/16])
            #            self.sobel_5 = np.zeros([self.batch_size, 1, self.resize_size[0]/32, self.resize_size[1]/32])
            self.labels = np.zeros(
                [self.batch_size, 1, self.resize_size[0], self.resize_size[1]])
            for i in range(self.batch_size):
                self.train_timing = (self.train_timing +
                                     1) % self.train_images_num
                orignial_image_data = mping.imread(
                    osp.join(self.data_dir, self.train_data_dir,
                             self.train_images[self.train_timing]))
                #                orignial_image_sobel = mping.imread(osp.join(self.data_dir,  self.train_sobel_dir, self.train_images[self.train_timing]))
                orignial_image_labels = mping.imread(
                    osp.join(
                        self.data_dir, self.train_labels_dir,
                        self.train_images[self.train_timing].split('.jpg')[0] +
                        '.png'))
                #                print osp.join(self.data_dir, self.train_labels_dir, self.train_images[self.train_timing].split('.jpg')[0] + '.png')
                GV.data_name = self.train_images[self.train_timing].split(
                    '.jpg')[0] + '.png'

                if orignial_image_data.shape[:
                                             2] != orignial_image_labels.shape[:
                                                                               2]:
                    raise Exception('image and labels must be same size')
                height, width = orignial_image_data.shape[:2]

                if height < self.resize_size[0]:
                    height_add_flag = 1
                else:
                    height_add_flag = 0

                if width < self.resize_size[1]:
                    width_add_flag = 1
                else:
                    width_add_flag = 0

                if height_add_flag or width_add_flag:
                    add_image = np.zeros([
                        height * (1 + height_add_flag),
                        width * (1 + width_add_flag), 3
                    ])
                    add_label = np.zeros([
                        height * (1 + height_add_flag),
                        width * (1 + width_add_flag), 3
                    ])
                    if height_add_flag and not width_add_flag:
                        add_image[:height, :width] = np.flipup(
                            orignial_image_data)
                        add_image[height:2 *
                                  height, :width] = orignial_image_data
                        add_label[:height, :width] = np.flipup(
                            orignial_image_labels)
                        add_label[height:2 *
                                  height, :width] = orignial_image_labels
                    elif not height_add_flag and width_add_flag:
                        add_image[:height, :width] = np.fliplr(
                            orignial_image_data)
                        add_image[:height:width,
                                  2 * width] = orignial_image_data
                        add_label[:height, :width] = np.fliplr(
                            orignial_image_labels)
                        add_label[:height,
                                  width:2 * width] = orignial_image_labels
                    elif height_add_flag and width_add_flag:
                        add_image[height:2 * height, :width] = np.fliplr(
                            orignial_image_data)
                        add_image[height:2 * height, width,
                                  2 * width] = orignial_image_data
                        add_image[:height, width,
                                  2 * width] = np.flipud(orignial_image_data)
                        add_image[:height, :width] = np.fliplr(
                            np.flipud(orignial_image_data))

                        add_label[height:2 * height, :width] = np.fliplr(
                            orignial_image_labels)
                        add_label[height:2 * height, width,
                                  2 * width] = orignial_image_labels
                        add_label[:height, width,
                                  2 * width] = np.flipud(orignial_image_labels)
                        add_label[:height, :width] = np.fliplr(
                            np.flipud(orignial_image_labels))
                    orignial_image_data = add_image
                    orignial_image_labels = add_label
                    height, width = orignial_image_data.shape[:2]

                start_x = random.randint(0, height - self.resize_size[0])
                start_y = random.randint(0, width - self.resize_size[1])
                image_data = orignial_image_data[start_x:start_x +
                                                 self.resize_size[0],
                                                 start_y:start_y +
                                                 self.resize_size[1]]
                #                image_sobel = orignial_image_sobel[end_x - int(height * tmp_crop_ratio) : end_x, end_y - int(width * tmp_crop_ratio) : end_y]
                image_labels = orignial_image_labels[start_x:start_x +
                                                     self.resize_size[0],
                                                     start_y:start_y +
                                                     self.resize_size[1]]

                #                end_x = random.randint(int(height * self.crop_ratio), height)
                #                end_y = random.randint(int(width * self.crop_ratio), width)
                #                image_data = orignial_image_data[:end_x, :end_y]
                #                image_sobel = orignial_image_sobel[:end_x, :end_y]
                #                image_labels = orignial_image_labels[:end_x, :end_y]

                #                plt.figure(1)
                #                plt.subplot(221)
                #                plt.imshow(orignial_image_data)
                #                plt.subplot(222)
                #                plt.imshow(orignial_image_labels)
                #                plt.subplot(223)
                #                plt.imshow(image_sobel)
                #                plt.subplot(224)
                #                plt.imshow(image_labels)
                #                GV.data_name = self.test_images[self.train_timing].split('.')[0]
                #                print self.data_dir,  self.train_data_dir, self.train_images[self.train_timing]
                #                image_data = imresize(np.array(image_data, dtype = np.uint8), [self.resize_size[0], self.resize_size[1], 3])
                #                image_labels = imresize(np.array(image_labels, dtype = np.uint8), [self.resize_size[0], self.resize_size[1]])
                flip = np.random.randint(0, 2)
                if len(image_data.shape) == 3:
                    a = 1
#                    image_data = imresize(np.array(image_data, dtype = np.uint8), [self.resize_size[0], self.resize_size[1], 3])
#                    image_sobel_0 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0], self.resize_size[1]])
#                    image_sobel_1 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/2, self.resize_size[1]/2])
#                    image_sobel_2 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/4, self.resize_size[1]/4])
#                    image_sobel_3 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/8, self.resize_size[1]/8])
#                    image_sobel_4 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/16, self.resize_size[1]/16])
#                    image_sobel_5 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/32, self.resize_size[1]/32])
                else:
                    #                    image_data = imresize(np.array(image_data, dtype = np.uint8), [self.resize_size[0], self.resize_size[1]])
                    image_data = np.tile(image_data[:, :, np.newaxis],
                                         [1, 1, 3])
                    #                    image_sobel_0 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0], self.resize_size[1]])
                    #                    image_sobel_1 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/2, self.resize_size[1]/2])
                    #                    image_sobel_2 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/4, self.resize_size[1]/4])
                    #                    image_sobel_3 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/8, self.resize_size[1]/8])
                    #                    image_sobel_4 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/16, self.resize_size[1]/16])
                    #                    image_sobel_5 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/32, self.resize_size[1]/32])
                    GV.abnormal_files.append(GV.data_name)
#                image_data = np.concatenate((image_data, image_sobel[:,:,np.newaxis]), axis = 2)
                if len(image_labels.shape) == 3:
                    image_labels = image_labels.mean(axis=2)
#                    GV.abnormal_files.append(GV.data_name)
                else:
                    pass
                image_labels[np.where(image_labels > 0.1)] = 1
                image_labels[np.where(image_labels < 0.1)] = 0
                if flip == 1:
                    image_data = np.fliplr(image_data)
                    image_labels = np.fliplr(image_labels)
#                flip = np.random.randint(0, 2)
#                if flip == 1:
#                    image_data = np.flipud(image_data)
#                    image_labels = np.flipud(image_labels)
                GV, image = image_data
                GV.image_labels = image_labels
                self.data[i] = image_data.transpose(2, 0, 1)
                #                self.sobel_0[i, 0] = image_sobel_0
                #                self.sobel_1[i, 0] = image_sobel_1
                #                self.sobel_2[i, 0] = image_sobel_2
                #                self.sobel_3[i, 0] = image_sobel_3
                #                self.sobel_4[i, 0] = image_sobel_4
                #                self.sobel_5[i, 0] = image_sobel_5
                self.labels[i, 0] = image_labels
#            self.data = np.array(self.data, dtype = np.float32)
#            self.labels = np.array(self.labels, dtype = np.float32)

        elif self.split == 'test':
            #            self.resize_size[0] = 64
            #            self.resize_size[1] = 64
            self.data = np.zeros([
                self.test_batch_size, 3, self.resize_size[0],
                self.resize_size[1]
            ])
            #            self.sobel_0 = np.zeros([self.test_batch_size, 1, self.resize_size[0], self.resize_size[1]])
            #            self.sobel_1 = np.zeros([self.test_batch_size, 1, self.resize_size[0]/2, self.resize_size[1]/2])
            #            self.sobel_2 = np.zeros([self.test_batch_size, 1, self.resize_size[0]/4, self.resize_size[1]/4])
            #            self.sobel_3 = np.zeros([self.test_batch_size, 1, self.resize_size[0]/8, self.resize_size[1]/8])
            #            self.sobel_4 = np.zeros([self.test_batch_size, 1, self.resize_size[0]/16, self.resize_size[1]/16])
            #            self.sobel_5 = np.zeros([self.test_batch_size, 1, self.resize_size[0]/32, self.resize_size[1]/32])
            self.labels = np.zeros([
                self.test_batch_size, 1, self.resize_size[0],
                self.resize_size[1]
            ])
            for i in range(self.test_batch_size):
                #                print i
                self.test_timing = (self.test_timing +
                                    1) % self.test_images_num
                suffix = osp.join(
                    self.data_dir, self.test_data_dir,
                    self.test_images[self.test_timing]).split('.')[-1]
                if suffix == 'png':
                    image_data = mping.imread(
                        osp.join(self.data_dir, self.test_data_dir,
                                 self.test_images[self.test_timing])) * 255
#                    image_sobel = mping.imread(osp.join(self.data_dir,  self.test_sobel_dir, self.test_images[self.test_timing]))
                else:
                    image_data = mping.imread(
                        osp.join(self.data_dir, self.test_data_dir,
                                 self.test_images[self.test_timing]))
#                    image_sobel = mping.imread(osp.join(self.data_dir,  self.test_sobel_dir, self.test_images[self.test_timing]))
#            print self.test_images[self.test_timing], suffix
                image_labels = mping.imread(
                    osp.join(
                        self.data_dir, self.test_labels_dir,
                        self.test_images[self.test_timing].split('.' +
                                                                 suffix)[0] +
                        '.png'))
                #            print osp.join(self.data_dir, self.test_labels_dir, self.test_images[self.test_timing].split('.' + suffix)[0] + '.png')

                image_labels[np.where(image_labels > 0.1)] = 1

                #                hegiht, width, _ = image_labels.shape
                #                max_height = 64
                #                if height > max_height:
                #                    image_labels = imresize(image_labels, [max_height, max_height * width / height, 3])
                #                    image_labels = imresize(image_labels, [max_height, max_height * width / height, 3])

                GV.data_name = self.test_images[self.test_timing].split('.')[0]
                GV.data_dir = osp.join(self.data_dir, self.test_data_dir)
                print self.data_dir, self.test_labels_dir, self.test_images[
                    self.test_timing], GV.data_name
                GV.a = self.data_dir
                GV.b = self.test_labels_dir
                GV.c = self.test_images[self.test_timing]
                GV.d = GV.data_name
                #            print 'hello', GV.data_name
                if len(image_data.shape) == 3:
                    if image_data.shape[2] != 3:
                        GV.image = image_data
                        image_data = imresize(
                            np.array(image_data[:, :, 0] * 255,
                                     dtype=np.uint8),
                            [self.resize_size[0], self.resize_size[1]])
                        image_data = np.tile(image_data[:, :, np.newaxis],
                                             [1, 1, 3])
#                        image_sobel_0 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0], self.resize_size[1]])
#                        image_sobel_1 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/2, self.resize_size[1]/2])
#                        image_sobel_2 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/4, self.resize_size[1]/4])
#                        image_sobel_3 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/8, self.resize_size[1]/8])
#                        image_sobel_4 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/16, self.resize_size[1]/16])
#                        image_sobel_5 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/32, self.resize_size[1]/32])
                    else:
                        image_data = imresize(
                            np.array(image_data, dtype=np.uint8),
                            [self.resize_size[0], self.resize_size[1], 3])
#                        image_sobel_0 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0], self.resize_size[1]])
#                        image_sobel_1 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/2, self.resize_size[1]/2])
#                        image_sobel_2 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/4, self.resize_size[1]/4])
#                        image_sobel_3 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/8, self.resize_size[1]/8])
#                        image_sobel_4 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/16, self.resize_size[1]/16])
#                        image_sobel_5 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/32, self.resize_size[1]/32])
#                    dasda
                elif len(image_data.shape) == 2:
                    image_data = imresize(
                        np.array(image_data, dtype=np.uint8),
                        [self.resize_size[0], self.resize_size[1]])
                    image_data = np.tile(image_data[:, :, np.newaxis],
                                         [1, 1, 3])
#                    image_sobel_0 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0], self.resize_size[1]])
#                    image_sobel_1 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/2, self.resize_size[1]/2])
#                    image_sobel_2 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/4, self.resize_size[1]/4])
#                    image_sobel_3 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/8, self.resize_size[1]/8])
#                    image_sobel_4 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/16, self.resize_size[1]/16])
#                    image_sobel_5 = imresize(np.array(image_sobel, dtype = np.uint8), [self.resize_size[0]/32, self.resize_size[1]/32])

                if len(image_labels.shape) == 3:
                    image_labels = imresize(
                        np.array(image_labels[:, :, 0], dtype=np.uint8),
                        [self.resize_size[0], self.resize_size[1]])
                else:
                    image_labels = imresize(
                        np.array(image_labels, dtype=np.uint8),
                        [self.resize_size[0], self.resize_size[1]])
#                image_data = np.concatenate((image_data, image_sobel[:,:,np.newaxis]), axis = 2)
                GV.image = image_data
                GV.labels = image_labels
                self.data[i] = image_data.transpose(2, 0, 1)
                #                self.sobel_0[i, 0] = image_sobel_0
                #                self.sobel_1[i, 0] = image_sobel_1
                #                self.sobel_2[i, 0] = image_sobel_2
                #                self.sobel_3[i, 0] = image_sobel_3
                #                self.sobel_4[i, 0] = image_sobel_4
                #                self.sobel_5[i, 0] = image_sobel_5
                self.labels[i, 0] = image_labels

        top[0].reshape(*self.data.shape)
        #        top[1].reshape(*self.sobel_0.shape)
        #        top[2].reshape(*self.sobel_1.shape)
        #        top[3].reshape(*self.sobel_2.shape)
        #        top[4].reshape(*self.sobel_3.shape)
        #        top[5].reshape(*self.sobel_4.shape)
        #        top[6].reshape(*self.sobel_5.shape)
        top[1].reshape(*self.labels.shape)
Example #5
0
 def mirror_horizontal(batch):
     return np.flipup(batch)