Python学习坑——init

在进行深度学习操作中,误将类中的init函数写成int结果怎么运行都出错,一直显示:

File "D:\ProgramData\Anaconda3\lib\typing.py", line 875, in __new__
    obj = super().__new__(cls, *args, **kwds)
TypeError: object.__new__() takes exactly one argument (the type to instantiate)

粗心大意结果浪费了一天多才搞出来,这次真的张教训了。

from torch.utils.data import Dataset
from PIL import Image
import os
class TensorDataset(Dataset):
    def __init__(self, ro1otDir, labelDir):    #这里是init
        self.rootDir = rootDir
        self.labelDir = labelDir
        self.path = os.path.join(self.rootDir,self.labelDir)
        self.imagePathList = os.listdir(self.path)

    def __getitem__(self, index):
        imageName = self.imagePathList[index]
        imageItemPath = os.path.join(self.rootDir,self.labelDir,imageName)
        image = Image.open(imageItemPath)
        label = self.labelDir
        return  image, label

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

rootDir = "data/练手数据集/val"
antsLabel = "ants"
beesLabel = 'bees'

antsDataset = TensorDataset ( rootDir, antsLabel)
print(antsDataset)

完美运行,自己也长点心吧
烦死了一下是自己的基础掌握的不牢固,之前在练习的时候没有把语法学透,而且在看视频时只是停留在看学,而没有去练习。

你可能感兴趣的:(Python,javascript,css3,html)