我在Jupyter Notebook中运行代码时遇到了以下错误“AttributeError:'numpy.int64'对象没有属性'read'“。我已经搜索了很多,但每个人都有不同的错误。
model = models.resnet18(pretrained = True)
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 4)
model.load_state_dict(torch.load('4_stage_model.pt'))
model.eval()
classes = ('1', '2', '3', '4')
class_probs = []
class_preds = []
with torch.no_grad():
for data in testloader:
images = data
output = model(images)
class_probs_batch = [F.softmax(el, dim=0) for el in output]
_, class_preds_batch = torch.max(output, 1)
class_probs.append(class_probs_batch)
class_preds.append(class_preds_batch)
test_probs = torch.cat([torch.stack(batch) for batch in class_probs])
test_preds = torch.cat(class_preds)
我也有一个CustomDataset类。代码是
class CustomDataSet(Dataset):
def __init__(self, csv_file, root_dir, transform):
self.root_dir = root_dir
self.transform = transform
self.dataframe = pd.read_csv(csv_file)
def __len__(self):
return len(self.dataframe)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
img_path = self.dataframe.iloc[idx, 15]
image = Image.open(img_path).convert("RGB")
tensor_image = self.transform(image)
return tensor_image
错误为:
File~/.conda/envs/flowering/lib/python3.10/site-packages/PIL/Image.py:3098,inopen(fp,mode,formats)
3096
fp. seek(0)
3097 except (AttributeError, io.Unsupportedoperation):
-
3098
fp
= i0.BytesI0(fp. read() )
3099
exclusive_fp = True
3101 prefix = fp. read(16)
AttributeError: 'numpy. int64' object has no attribute
* read"
1条答案
按热度按时间enyaitl31#
Image.open()需要一个文件名,而不是一个数字数组。