运行时出现下面的错误:
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'
这是因为mmcv\utils\config.py的_file2dict()函数想在C盘创建临时文件,但是PyTorch没有在C盘创建文件的权限,因此造成了PermissionError.解决的办法是在D盘创建一个Temp文件夹,让_file2dict()在D盘创建临时文件,就不会有PermissionError了.因此对_file2dict(filename, use_predefined_variables=True)这个函数进行了修改:
@staticmethod def _file2dict(filename, use_predefined_variables=True): filename = osp.abspath(osp.expanduser(filename)) filenames = filename.split('\\') if filenames[0] == 'C:': filename = 'D:\\Temp\\' +filenames[-1] f = open(filename, 'w') f.close() check_file_exist(filename) fileExtname = osp.splitext(filename)[1] if fileExtname not in ['.py', '.json', '.yaml', '.yml']: raise IOError('Only py/yml/yaml/json type are supported now!') with tempfile.TemporaryDirectory() as temp_config_dir: temp_config_dir = 'D:\\Temp\\' + temp_config_dir.split('\\')[-1] os.makedirs(temp_config_dir) temp_config_file = tempfile.NamedTemporaryFile( dir=temp_config_dir, suffix=fileExtname) if platform.system() == 'Windows': temp_config_file.close() temp_config_name = osp.basename(temp_config_file.name) # Substitute predefined variables if use_predefined_variables: Config._substitute_predefined_vars(filename, temp_config_file.name) else: shutil.copyfile(filename, temp_config_file.name)
先判断是否在C盘创建临时文件,如果是,则在D:\Temp下创建相同名字的临时文件夹和文件,这样就不会有PermissionError了.