Khi chạy huấn luyện mô hình sử dụng mmcv, bạn có thể gặp lỗi sau:
Traceback (most recent call last):
File "D:/Projects/project_name/tools/train.py", line 178, in <module>
main()
File "D:/Projects/project_name/tools/train.py", line 167, in main
train_detector(
File "D:\Projects\project_name\mmdet\apis\train.py", line 147, in train_detector
runner.resume(cfg.resume_from)
File "D:\Anaconda3\envs\hzmd\lib\site-packages\mmcv\runner\base_runner.py", line 346, in resume
config = mmcv.Config.fromstring(
File "D:\Anaconda3\envs\hzmd\lib\site-packages\mmcv\utils\config.py", line 279, in fromstring
cfg = Config.fromfile(temp_file.name)
File "D:\Anaconda3\envs\hzmd\lib\site-packages\mmcv\utils\config.py", line 251, in fromfile
cfg_dict, cfg_text = Config._file2dict(filename,
File "D:\Anaconda3\envs\hzmd\lib\site-packages\mmcv\utils\config.py", line 137, in _file2dict
Config._substitute_predefined_vars(filename,
File "D:\Anaconda3\envs\hzmd\lib\site-packages\mmcv\utils\config.py", line 112, in _substitute_predefined_vars
with open(filename, 'r') as f:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Admin\\AppData\\Local\\Temp\\tmp06sdogpt.py'
Lỗi này xảy ra do hàm _file2dict() trong mmcv/utils/config.py cố gắng đọc hoặc tạo file cấu hình tạm trong thư mục %TEMP% mặc định trên ổ C:, nhưng tiến trình Python không có quyền ghi vào thư mục đó.
Một cách giải quyết là chuyển thư mục tạm sang một vị trí khác mà người dùng có đầy đủ quyền truy cập, ví dụ như D:\Temp. Để làm điều này, bạn có thể chỉnh sửa lại phương thức _file2dict như sau:
@staticmethod
def _file2dict(filename, use_predefined_variables=True):
import tempfile
import shutil
import platform
import os.path as osp
filename = osp.abspath(osp.expanduser(filename))
# Đảm bảo thư mục D:\Temp tồn tại
temp_root = r'D:\Temp'
os.makedirs(temp_root, exist_ok=True)
check_file_exist(filename)
file_ext = osp.splitext(filename)[1]
if file_ext not in ['.py', '.json', '.yaml', '.yml']:
raise IOError('Only py/yml/yaml/json types are supported.')
# Tạo thư mục tạm trong D:\Temp thay vì %TEMP%
with tempfile.TemporaryDirectory(dir=temp_root) as temp_dir:
temp_config_file = tempfile.NamedTemporaryFile(
dir=temp_dir, suffix=file_ext, delete=False)
temp_path = temp_config_file.name
temp_config_file.close()
if use_predefined_variables:
Config._substitute_predefined_vars(filename, temp_path)
else:
shutil.copyfile(filename, temp_path)
# Đọc nội dung file tạm đã được xử lý
cfg_dict, cfg_text = Config._parse_file(temp_path)
return cfg_dict, cfg_text
Thay đổi này đảm bảo rằng mọi file tạm được tạo trong quá trình phân tích cấu hình sẽ nằm trong D:\Temp — nơi người dùng có quyền ghi — thay vì thư mục tạm hệ thống trên ổ C:, từ đó tránh được lỗi PermissionError.