コード例 #1
0
    def test_errors(self):
        with self.assertRaises(TypeError):
            F.to_tensor(1)

        with self.assertRaises(ValueError):
            fake_img = Image.fromarray((np.random.rand(28, 28, 3) * 255).astype(
                'uint8'))
            F.to_tensor(fake_img, data_format=1)

        with self.assertRaises(ValueError):
            fake_img = paddle.rand((3, 100, 100))
            F.pad(fake_img, 1, padding_mode='symmetric')

        with self.assertRaises(TypeError):
            fake_img = paddle.rand((3, 100, 100))
            F.resize(fake_img, {1: 1})

        with self.assertRaises(TypeError):
            fake_img = Image.fromarray((np.random.rand(28, 28, 3) * 255).astype(
                'uint8'))
            F.resize(fake_img, '1')

        with self.assertRaises(TypeError):
            F.resize(1, 1)

        with self.assertRaises(TypeError):
            F.pad(1, 1)

        with self.assertRaises(TypeError):
            F.crop(1, 1, 1, 1, 1)

        with self.assertRaises(TypeError):
            F.hflip(1)

        with self.assertRaises(TypeError):
            F.vflip(1)

        with self.assertRaises(TypeError):
            F.adjust_brightness(1, 0.1)

        with self.assertRaises(TypeError):
            F.adjust_contrast(1, 0.1)

        with self.assertRaises(TypeError):
            F.adjust_hue(1, 0.1)

        with self.assertRaises(TypeError):
            F.adjust_saturation(1, 0.1)

        with self.assertRaises(TypeError):
            F.rotate(1, 0.1)

        with self.assertRaises(TypeError):
            F.to_grayscale(1)

        with self.assertRaises(ValueError):
            set_image_backend(1)

        with self.assertRaises(ValueError):
            image_load('tmp.jpg', backend=1)
コード例 #2
0
def data_prepare(data_root):
    '''
        Make a vector of weights for each image in the dataset, based
        on class frequency. The returned vector of weights can be used
        to create a WeightedRandomSampler for a DataLoader to have
        class balancing when sampling for a training batch.
            images - torchvisionDataset.imgs
            nclasses - len(torchvisionDataset.classes)
        https://discuss.pytorch.org/t/balanced-sampling-between-classes-with-torchvision-dataloader/2703/3
    '''
    count = []
    images_data = []
    images_label = []
    class_file_list = os.listdir(data_root)
    for i in tqdm.tqdm(range(len(class_file_list))):
        images_path = os.listdir(os.path.join(data_root, class_file_list[i]))
        count_value = 0
        for image_path in images_path:
            images_data.append(
                vision.image_load(data_root + '/' + class_file_list[i] + '/' +
                                  image_path,
                                  backend='cv2'))
            images_label.append(i)
            count_value += 1
        count.append(count_value)
    num_classes = len(class_file_list)
    num_sample = len(images_label)
    weight_per_class = [0.] * num_classes
    for i in tqdm.tqdm(range(num_classes)):
        weight_per_class[i] = 1 / (num_classes * count[i])
    weight = [0] * num_sample
    for idx, val in enumerate(images_label):
        weight[idx] = weight_per_class[images_label[idx]]

    return weight, images_data, images_label, num_classes
コード例 #3
0
def data_reader():
    # 数据读取器,这里要单独写个
    data_root = 'data/Casia_maxpy_clean'
    images_data = []
    images_label = []
    batch_size = 128
    count_value = 0
    class_file_list = os.listdir(data_root)
    for i in tqdm.tqdm(range(len(class_file_list)),ncols=80):
        images_path = os.listdir(os.path.join(data_root, class_file_list[i]))
        for image_path in images_path:
            images_data.append(
                vision.image_load(data_root + '/' + class_file_list[i] + '/' + image_path, backend='cv2'))
            images_label.append(i)
            count_value += 1
    print(count_value)
    assert len(images_data) == len(images_label)
    def reader():
        for i in range(int(len(images_data)/batch_size)-1):
            data = images_data[i*batch_size:(i+1)*batch_size]
            data = np.array(data)
            data = np.transpose(data,(0,3, 1, 2))
            data = (data - 127.5) / 127.5
            yield data
    return reader
コード例 #4
0
ファイル: test_transforms.py プロジェクト: sandyhouse/Paddle
    def test_image_load(self):
        fake_img = Image.fromarray((np.random.random(
            (32, 32, 3)) * 255).astype('uint8'))

        path = 'temp.jpg'
        fake_img.save(path)

        set_image_backend('pil')

        pil_img = image_load(path).convert('RGB')

        print(type(pil_img))

        set_image_backend('cv2')

        np_img = image_load(path)

        os.remove(path)
コード例 #5
0
    def data_prepare(self):
        data_root = self.data_root
        images_data = []
        images_label = []
        count_value = 0
        class_file_list = os.listdir(self.data_root)
        for i in tqdm.tqdm(range(len(class_file_list)), ncols=80):
            images_path = os.listdir(
                os.path.join(self.data_root, class_file_list[i]))
            for image_path in images_path:
                images_data.append(
                    vision.image_load(data_root + '/' + class_file_list[i] +
                                      '/' + image_path,
                                      backend='cv2'))
                images_label.append(i)
                count_value += 1
        print(count_value)
        assert len(images_data) == len(images_label)

        return images_data, images_label
コード例 #6
0
ファイル: test_transforms.py プロジェクト: sandyhouse/Paddle
    def test_errors(self):
        with self.assertRaises(TypeError):
            F.to_tensor(1)

        with self.assertRaises(ValueError):
            fake_img = Image.fromarray(
                (np.random.rand(28, 28, 3) * 255).astype('uint8'))
            F.to_tensor(fake_img, data_format=1)

        with self.assertRaises(ValueError):
            fake_img = paddle.rand((3, 100, 100))
            F.pad(fake_img, 1, padding_mode='symmetric')

        with self.assertRaises(TypeError):
            fake_img = paddle.rand((3, 100, 100))
            F.resize(fake_img, {1: 1})

        with self.assertRaises(TypeError):
            fake_img = Image.fromarray(
                (np.random.rand(28, 28, 3) * 255).astype('uint8'))
            F.resize(fake_img, '1')

        with self.assertRaises(TypeError):
            F.resize(1, 1)

        with self.assertRaises(TypeError):
            F.pad(1, 1)

        with self.assertRaises(TypeError):
            F.crop(1, 1, 1, 1, 1)

        with self.assertRaises(TypeError):
            F.hflip(1)

        with self.assertRaises(TypeError):
            F.vflip(1)

        with self.assertRaises(TypeError):
            F.adjust_brightness(1, 0.1)

        with self.assertRaises(TypeError):
            F.adjust_contrast(1, 0.1)

        with self.assertRaises(TypeError):
            F.adjust_hue(1, 0.1)

        with self.assertRaises(TypeError):
            F.adjust_saturation(1, 0.1)

        with self.assertRaises(TypeError):
            F.affine('45')

        with self.assertRaises(TypeError):
            F.affine(45, translate=0.3)

        with self.assertRaises(TypeError):
            F.affine(45, translate=[0.2, 0.2, 0.3])

        with self.assertRaises(TypeError):
            F.affine(45, translate=[0.2, 0.2], scale=-0.5)

        with self.assertRaises(TypeError):
            F.affine(45, translate=[0.2, 0.2], scale=0.5, shear=10)

        with self.assertRaises(TypeError):
            F.affine(45, translate=[0.2, 0.2], scale=0.5, shear=[-10, 0, 10])

        with self.assertRaises(TypeError):
            F.affine(45,
                     translate=[0.2, 0.2],
                     scale=0.5,
                     shear=[-10, 10],
                     interpolation=2)

        with self.assertRaises(TypeError):
            F.affine(45,
                     translate=[0.2, 0.2],
                     scale=0.5,
                     shear=[-10, 10],
                     center=0)

        with self.assertRaises(TypeError):
            F.rotate(1, 0.1)

        with self.assertRaises(TypeError):
            F.to_grayscale(1)

        with self.assertRaises(ValueError):
            set_image_backend(1)

        with self.assertRaises(ValueError):
            image_load('tmp.jpg', backend=1)