使用Mask RCNN训练自定义数据集时遇[WinError 123]路径语法错误
Mask-RCNN训练时路径语法错误(WinError 123)
我是Python新手,在Conda环境的Jupyter Notebook里用mask-rcnn-tf2做车辆目标检测,训练自定义数据集时每次都触发路径语法错误,卡在这了。
我的代码
from mrcnn.utils import Dataset from mrcnn.visualize import display_instances from mrcnn.utils import extract_bboxes from mrcnn.config import Config from mrcnn.model import MaskRCNN # class that defines and loads the kangaroo dataset class Contest(Dataset): # load the dataset definitions def load_dataset(self, dataset_csv,image_dir, is_train=True): # define one class self.add_class("dataset", 1, "Car") # define data locations images_dir = image_dir + 'train_images/' # annotations_dir = dataset_dir + '/annots/' # find all images #for filename in os.listdir(images_dir): for id, filename in zip(id_s,img): # extract image id #print(i) image_id = id # skip bad images #if image_id in ["100"]: #continue # skip all images after 150 if we are building the train set if is_train and int(image_id) >= 5000: continue # skip all images before 150 if we are building the test/val set if not is_train and int(image_id) >= 4000: continue img_path = images_dir + filename if os.path.isfile(img_path) == False: continue # if im.size == 0: # continue # ann_path = annotations_dir + image_id + '.xml' # add to dataset self.add_image('dataset', image_id=image_id, path=img_path) def extract_boxes(self,dataset_csv,image_id): #For getting all the bbox category ids and width and height of the image on the bases of image id # box = np.array([]) info = self.image_info[image_id] box = list() # bbox = data[data['image_id']==image_id]['bbox'] for i in range(len(b_mat)): if b_mat[i][2] == 1 and b_mat[i][1] == info['id']: # print(b_mat[i][0]) bbox = b_mat[i][0] box.append(bbox) wid = data[data['image_id']==image_id]['width'].unique()[0] hei = data[data['image_id']==image_id]['height'].unique()[0] return box , wid ,hei # load the masks for an image def load_mask(self, image_id): # get details of image info = self.image_info[image_id] #print(info[0]) # define box file location #path = info['annotation'] # load XML boxes, w, h = self.extract_boxes(data,image_id) # create one array for all masks, each on a different channel masks = zeros([h, w, len(boxes)], dtype='uint8') # create masks class_ids = list() for i in range(len(boxes)): box = boxes[i] row_s, col_s = box[0], box[1] row_e, col_e = box[0]+box[2], box[1]+box[3] #print(i) #masks[row_e-row_s:col_e-col_s,col_s-col_e:row_s-row_e,i] = 1 #masks[row_s:row_e,col_s:col_e,i] = 1 masks[col_s:col_e, row_s:row_e,i] = 1 #masks[col_s:row_s, col_e:row_e,i] = 1 #print(row_s,row_e ,col_s,col_e,info,image_id) #print(row_s,row_e, col_s,col_e) #print(box[0], box[1], box[2], box[3],info['id']) class_ids.append(self.class_names.index('Car')) return masks, asarray(class_ids, dtype='int32') # load an image reference def image_reference(self, image_id): info = self.image_info[image_id] return info['path'] # define a configuration for the model class CarConfig(Config): # define the name of the configuration NAME = "Contest_cfg" # number of classes (background + kangaroo) NUM_CLASSES = 1 + 1 # number of training steps per epoch STEPS_PER_EPOCH = 200 # train set image_dir = 'G:/My Drive/train_images/' train_set = Contest() train_set.load_dataset(data,image_dir,is_train=True) train_set.prepare() print('Train: %d' % len(train_set.image_ids)) test_set = Contest() test_set.load_dataset(data,image_dir, is_train=False) test_set.prepare() print('Test: %d' % len(test_set.image_ids)) # image_id = 1 # # load the image # image = train_set.load_image(image_id) # # load the masks and the class ids # mask, class_ids = train_set.load_mask(image_id) # # extract bounding boxes from the masks # bbox = extract_bboxes(mask) # # display image with masks and bounding boxes # display_instances(image, bbox, mask, class_ids, train_set.class_names) #prepare config config = CarConfig() config.display() # define the model model = MaskRCNN(mode='training', model_dir=r"D:/", config=config) # load weights (mscoco) and exclude the output layers model.load_weights(r"D:/Najam/mask_rcnn_coco.h5", by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"]) # train weights (output layers or 'heads') model.train(train_set,test_set, learning_rate=config.LEARNING_RATE, epochs=3, layers='head')
报错信息
OSError Traceback (most recent call last) <ipython-input-25-c0b9708402b6> in <module> 118 model.load_weights(r"D:/Najam/mask_rcnn_coco.h5", by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"]) 119 # train weights (output layers or 'heads') --> 120 model.train(train_set,test_set, learning_rate=config.LEARNING_RATE, epochs=3, layers='head') D:\Najam\matterport\mrcnn\model.py in train(self, train_dataset, val_dataset, learning_rate, epochs, layers, augmentation, custom_callbacks, no_augmentation_sources) 2345 batch_size=self.config.BATCH_SIZE) 2346 --> 2347 # Create log_dir if it does not exist 2348 if not os.path.exists(self.log_dir): 2349 print(self.log_dir) ~\anaconda3\envs\myenv\lib\os.py in makedirs(name, mode, exist_ok) 208 if head and tail and not path.exists(head): 209 try: --> 210 makedirs(head, mode, exist_ok) 211 except FileExistsError: 212 # Defeats race condition when another thread created the path ~\anaconda3\envs\myenv\lib\os.py in makedirs(name, mode, exist_ok) 208 if head and tail and not path.exists(head): 209 try: --> 210 makedirs(head, mode, exist_ok) 211 except FileExistsError: 212 # Defeats race condition when another thread created the path ~\anaconda3\envs\myenv\lib\os.py in makedirs(name, mode, exist_ok) 218 return 219 try: --> 220 mkdir(name, mode) 221 except OSError: 222 # Cannot rely on checking for EEXIST, since the operating system OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '//'
解决方案
核心问题修复
错误的直接原因是初始化MaskRCNN时指定的model_dir=r"D:/"有问题。Windows系统下,单独的磁盘根路径D:/会导致MaskRCNN内部生成日志目录时解析出无效的//路径,触发WinError 123。
修改方式很简单,把model_dir改成具体的子目录(确保目录存在,或者让代码能创建它):
model = MaskRCNN(mode='training', model_dir=r"D:/MaskRCNN_Train_Logs/", config=config)
额外路径优化建议
- 避免字符串拼接路径:别用
images_dir = image_dir + 'train_images/'这种方式,改用os.path.join(),跨系统兼容性更好:import os images_dir = os.path.join(image_dir, 'train_images') - 处理带空格的路径:你的
image_dir = 'G:/My Drive/train_images/'包含空格,虽然Windows支持,但用os.path.join或者原始字符串(r"...")能避免潜在问题。
内容的提问来源于stack exchange,提问作者najam iqbal
相关产品推荐
相关产品推荐

