python - 获取具有多个不明确元素的数组的真值以进行蛋白变换

标签 python machine-learning neural-network pytorch albumentations

我正在使用 albumentations 将变换应用于 Pytorch 模型,但收到此错误,并且我没有得到任何关于此错误的线索。我唯一知道的是,这是由于正在应用的转换而发生的,但不确定这有什么问题。

ValueError: Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py", line 99, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py", line 99, in <listcomp>
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "<ipython-input-23-119ea6bc360e>", line 24, in __getitem__
    image = self.transform(image)
  File "/opt/conda/lib/python3.6/site-packages/albumentations/core/composition.py", line 164, in __call__
    need_to_run = force_apply or random.random() < self.p
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

这是代码片段。 数据加载器getitem()方法:

        image = cv2.imread(p_path)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image = crop_image_from_gray(image)
        image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))
        image = cv2.addWeighted ( image,4, cv2.GaussianBlur( image , (0,0) , 10) ,-4 ,128)
        print(image.shape)
        image = self.transform(image)

应用变换:

val_transform = albumentations.Compose([
            Normalize(
                mean=[0.485, 0.456, 0.406],
                std=[0.229, 0.224, 0.225],
            ),
            ToTensor()
        ])

该类被调用:

valset       = MyDataset(val_df, transform = val_transform)

最佳答案

从官方 albumentation 文档中,您可以对图像应用转换

from PIL import Image
import cv2
import numpy as np
from torch.utils.data import Dataset
from torchvision import transforms
from albumentations import Compose, RandomCrop, Normalize, HorizontalFlip, Resize
from albumentations.pytorch import ToTensor


class AlbumentationsDataset(Dataset):
    """__init__ and __len__ functions are the same as in TorchvisionDataset"""
    def __init__(self, file_paths, labels, transform=None):
        self.file_paths = file_paths
        self.labels = labels
        self.transform = transform

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

    def __getitem__(self, idx):
        label = self.labels[idx]
        file_path = self.file_paths[idx]

        # Read an image with OpenCV
        image = cv2.imread(file_path)

        # By default OpenCV uses BGR color space for color images,
        # so we need to convert the image to RGB color space.
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        image = crop_image_from_gray(image)
        image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))
        image = cv2.addWeighted ( image,4, cv2.GaussianBlur( image , (0,0) , 10) ,-4 ,128)

        image = Img.fromarray(image, mode='RGB')  
        if self.transform:
            augmented = self.transform(image=np.array(image))
            image = augmented['image']

        image = np.transpose(image, (2, 0, 1))

        return image, label


albumentations_transform = Compose([

    Normalize(
        mean=[0.485, 0.456, 0.406],
        std=[0.229, 0.224, 0.225],
    ),
    ToTensor()
])


albumentations_dataset = AlbumentationsDataset(
    file_paths=['./images/image_1.jpg', './images/image_2.jpg', './images/image_3.jpg'],
    labels=[1, 2, 3],
    transform=albumentations_transform,
)

test_loader = DataLoader(dataset = albumentations_dataset, batch_size=4, drop_last=False, shuffle=False).

关于python - 获取具有多个不明确元素的数组的真值以进行蛋白变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57718447/

相关文章:

python - 在 TensorFlow 中使用 CNN 进行信号处理。类型错误 : DataType float32 for attr 'Tlabels' not in list of allowed values: int32, int64

artificial-intelligence - 用 sigmoid 神经元替换感知器网络

python - Pandas 中的分层多索引计数

python gtk 3.0 是否不支持从辅助线程访问 GUI?

python - 如何在 SQLite 中使用 python 语句作为 SELECT 条件?

machine-learning - 决策树如何可能不稳定?

r - 选择点的邻域并将其转换为向量

python - 我可以将 Json 反序列化为 Python 中的 C# Newtonsoft 类吗

r - 使用聚类分配矩阵为数据分配聚类标签

r - 除了 Logistic,RSNNS 包中还有哪些激活函数?