小编Alk*_*ian的帖子

PyTorch“在 DataLoader 工作进程 0 中捕获 IndexError”、“IndexError:数组索引过多”

我正在尝试基于PyTorch的“微调对象检测”官方教程实现一个检测模型。它似乎只需要最少的数据(10 张图像)。不过,我将整个数据集上传到云端硬盘并检查了索引-数据-标签的对应关系。我的设置中没有不匹配的项目,我已解决该部分中的所有错误。(我从 GDrive 上的标签中删除了多余的项目)

class SomeDataset(torch.utils.data.Dataset):
def __init__(self, root_path, transforms):
    self.root_path = root_path
    self.transforms = transforms
    # load all image files, sorting them to
    # ensure that they are aligned
    self.imgs = list(sorted(os.listdir(os.path.join(root_path, "images"))))
    self.labels = list(sorted(os.listdir(os.path.join(root_path, "labels"))))


def __getitem__(self, idx):
    # load images ad masks
    img_path = os.path.join(self.root_path, "images", self.imgs[idx])
    label_path = os.path.join(self.root_path, "labels", self.labels[idx])


    img = Image.open(img_path).convert("RGB")

    # get labels and boxes
    label_data = np.loadtxt(label_path, dtype=str, delimiter=' ');
    print(f"{len(label_data)} is the length of label data")
    num_objs = label_data.shape[0]; …
Run Code Online (Sandbox Code Playgroud)

python machine-learning deep-learning pytorch

5
推荐指数
2
解决办法
4万
查看次数

子类初始化参数

我想问一下,如果我们使用基类定义子类,为什么我们需要在__init__方法中初始化父类中的参数.我和JAVA OOP类似,我记得在Java中我们只是在子类中添加新参数.

如果我对Java也是错的,有人可以解释一下这样做的原因吗?是不是继承应该让我们的生活更容易编程.

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg

    def display_car(self):
        print "This is a %s %s with %s MPG." % (self.color, self.model, self.mpg)

    def drive_car(self):
        self.condition = "used"

class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        self.model = model
        self.color = color
        self.mpg = mpg
        self.battery_type = battery_type



my_car = ElectricCar("Auris", "golden", 89, "molten salt")
Run Code Online (Sandbox Code Playgroud)

我的意思是为什么self.battery_type = battery_type内部的ElectricCar类不足以进行这种继承?

python inheritance class

2
推荐指数
1
解决办法
2346
查看次数

列表理解中迭代器链的替代方法?

我想在列表理解中将两个或多个不同的迭代器链接在一起。

假设我想将大写和小写字母组合成一个列表。我原来的方式是这样的,

lst1 = [chr(i) for i in range(97,123)]
lst2 = [chr(i) for i in range(65,91)]
lst = lst1+lst2
Run Code Online (Sandbox Code Playgroud)

但是我认为必须有其他一些方法可以在一行中很好地做到这一点,然后我将它与 itertools 模块一起使用,

lst = [chr(i) for i in itertools.chain(range(97,123), range(65,91))]
Run Code Online (Sandbox Code Playgroud)

最后我也想到了元组拆包,

lst = [chr(i) for i in (*range(97,123), *range(65,91))]
Run Code Online (Sandbox Code Playgroud)

与其他两种方法相比,Itertools 速度较慢(元组解包是最快的一种)

例如,第一个范围和第二个范围中的字符构成

>>> lst1
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

>>> lst2
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', …
Run Code Online (Sandbox Code Playgroud)

python iterator list-comprehension list

0
推荐指数
1
解决办法
109
查看次数