모델이 얼마나 잘 학습되었는지 알아보기 위해 테스트 데이터셋을 이용하여 예측 결과를 ResNet.csv로 저장합니다.

    코드 6-88 테스트 데이터셋을 이용한 모델 예측

    import pandas as pd
    id_list = []
    pred_list = []
    _id = 0
    with torch.no_grad():
        for test_path in test_images_filepaths:
            img = Image.open(test_path)
            _id = test_path.split('/')[-1].split('.')[1]
            transform = ImageTransform(size, mean, std)
            img = transform(img, phase='val')
            img = img.unsqueeze(0)
            img = img.to(device)
    
            model.eval()
            outputs = model(img)
            preds = F.softmax(outputs[0], dim=1)[:, 1].tolist()
            id_list.append(_id)
            pred_list.append(preds[0])
    
    res = pd.DataFrame({
        'id': id_list,
        'label': pred_list
    })
    
    res.sort_values(by='id', inplace=True)
    res.reset_index(drop=True, inplace=True)
    
    res.to_csv('../chap06/data/ResNet.csv', index=False)
    res.head(10)
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.