【发布时间】:2020-11-23 01:11:35
【问题描述】:
我正在运行下面的数据加载器,它在训练之前将过滤器应用于显微镜图像。为了计算红色和绿色。此代码过滤红细胞。由于我已将此应用于代码,因此我不断收到上面的错误消息。我尝试将内存分配增加到可能的最大允许值,但这没有帮助。请问有什么方法可以修改过滤器,使其不会导致此问题吗?非常感谢提前
import os
import numpy as np
import torch
from PIL import Image
from torch.utils.data import Dataset
from torchvision import transforms, utils
#from torchvision.transforms import Grayscalei
import pandas as pd
import pdb
import cv2
class CellsDataset(Dataset):
# a very simple dataset
def __init__(self, root_dir, transform=None, return_filenames=False):
self.root = root_dir
self.transform = transform
self.return_filenames = return_filenames
self.files = [os.path.join(self.root,filename) for filename in os.listdir(self.root)]
self.files = [path for path in self.files
if os.path.isfile(path) and os.path.splitext(path)[1]=='.png']
def __len__(self):
return len(self.files)
def __getitem__(self, idx):
path = self.files[idx]
image = cv2.imread(path)
sample = image.copy()
# set blue and green channels to 0
sample[:, :, 0] = 0
sample[:, :, 1] = 0
channel.
if self.transform:
sample = self.transform(sample)
if self.return_filenames:
return sample, path
else:
return sample
【问题讨论】:
-
您能否毫无问题地遍历数据集本身(而不是 DataLoader)?例如
for x in dataset:。如果是这样,那么也许您需要减少数据加载器的num_workers?
标签: python image-processing deep-learning pytorch