TypeError: Can't convert object to 'str' for 'filename'

with torch.no_grad():
    for i in test_data:
        test_data = cv2.imread(i)
        
        #transform = ImageTransform(size, mean, std)
        #img = transform(img)
        #img = img.unsqueeze(0)
        #img = img.to(device)

        # 推論モード
        model.eval()

        # 推論
        outputs = model(test_data)
        # ソフトマックス関数を使って確率を出力
        preds = F.softmax(outputs, dim=1)[:, 1].tolist()
        
        image_paths.append(img)
        predictions.append(preds[0])
    
res = pd.DataFrame({
    'ImageID': image_paths,
    'predictions': predictions})

res.sort_values(by='img', inplace=True)
res.reset_index(drop=True, inplace=True)

# CSV出力
res.to_csv('test_data/sample_submission.csv', index=False)

TypeError Traceback (most recent call last)
Input In [11], in <cell line: 1>()
1 with torch.no_grad():
2 for i in test_data:
----> 3 test_data = cv2.imread(i)
5 #transform = ImageTransform(size, mean, std)
6 #img = transform(img)
7 #img = img.unsqueeze(0)
8 #img = img.to(device)
9
10 # 推論モード
11 model.eval()

TypeError: Can’t convert object to ‘str’ for ‘filename’

It seems you are trying to overwrite the test_data while also iterating it, so I would recommend to use another variable name and check if you are still hitting the issue.

1 Like