""" This code is based on the Torchvision repository, which was licensed under the BSD 3-Clause. """ import os import pickle import sys import numpy as np import torch from PIL import Image from torch.utils.data import Dataset from utils.mypath import MyPath from torchvision.datasets.utils import check_integrity, download_and_extract_archive class simclr_c10(Dataset): base_folder = 'D:/wjd/simclr_c10' filename = "simclr_c10" def __init__(self, root=MyPath.db_root_dir('wjd/simclr_c10/'), train=True, transform=None, download=False): super('wjd/simclr_c10/', self).__init__() self.data = None self.root = root self.transform = transform self.train = train # training set or test set self.classes = ['Al', 'Ag', 'Au', 'Cu', 'W', 'V', 'Ta', 'Mo'] def __getitem__(self, index): """ Args: index (int): Index Returns: dict: {'image': image, 'target': index of target class, 'meta': dict} """ img, target = self.data[index], self.targets[index] img_size = (img.shape[0], img.shape[1]) img = Image.fromarray(img) class_name = self.classes[target] if self.transform is not None: img = self.transform(img) out = {'image': img, 'target': target, 'meta': {'im_size': img_size, 'index': index, 'class_name': class_name}} return out def get_image(self, index): img = self.data[index] return img def __len__(self): return len(self.data) def extra_repr(self): return "Split: {}".format("Train" if self.train is True else "Test") return outside function,这段代码正确吗
时间: 2024-02-14 16:07:56 浏览: 120
repository.xbmc-addons-chinese-1.2.1.zip
这段代码有一些问题:
1. `super('wjd/simclr_c10/', self).__init__()` 这句话不正确,应该是 `super(simclr_c10, self).__init__()`
2. `self.targets` 在代码中没有定义,需要在 `__init__` 函数中将其定义并初始化
3. `return outside function` 这句话不应该出现在类定义中,需要将其删除
4. 另外,`check_integrity` 和 `download_and_extract_archive` 函数在代码中没有使用,可以删除
修正后的代码如下所示:
```
import os
import pickle
import sys
import numpy as np
import torch
from PIL import Image
from torch.utils.data import Dataset
from utils.mypath import MyPath
class simclr_c10(Dataset):
base_folder = 'D:/wjd/simclr_c10'
filename = "simclr_c10"
def __init__(self, root=MyPath.db_root_dir('wjd/simclr_c10/'), train=True, transform=None, download=False):
super(simclr_c10, self).__init__()
self.data = None
self.targets = []
self.root = root
self.transform = transform
self.train = train # training set or test set
self.classes = ['Al', 'Ag', 'Au', 'Cu', 'W', 'V', 'Ta', 'Mo']
self._load_data()
def _load_data(self):
if self.train:
data_file = os.path.join(self.root, self.base_folder, 'train.pkl')
else:
data_file = os.path.join(self.root, self.base_folder, 'test.pkl')
with open(data_file, 'rb') as f:
data = pickle.load(f)
self.data = data['data']
self.targets = data['targets']
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
dict: {'image': image, 'target': index of target class, 'meta': dict}
"""
img, target = self.data[index], self.targets[index]
img_size = (img.shape[0], img.shape[1])
img = Image.fromarray(img)
class_name = self.classes[target]
if self.transform is not None:
img = self.transform(img)
out = {'image': img, 'target': target, 'meta': {'im_size': img_size, 'index': index, 'class_name': class_name}}
return out
def get_image(self, index):
img = self.data[index]
return img
def __len__(self):
return len(self.data)
def extra_repr(self):
return "Split: {}".format("Train" if self.train is True else "Test")
```
阅读全文