Getting error:([WinError 123] The filename, directory name, or volume label syntax is incorrect: '//') while training custom dataset through mask rcnn

Question:

I’m new to Python and I’m trying to use mask-rcnn-tf2 to detect objects in Conda’s jupyter notebook. But I get this error every time. I am just stuck here.

Here is my code:

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')

Above is the code I have written for getting the dataset and training the dataset.

Error:

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:Najammatterportmrcnnmodel.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)

~anaconda3envsmyenvlibos.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

~anaconda3envsmyenvlibos.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

~anaconda3envsmyenvlibos.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: '//'
Asked By: najam iqbal

||

Answers:

So you’re getting an error because the script is trying to create some folders for your logs at a path that is not accepted.

I’m not sure why you’re getting this error, usually it happens when in the specified folders path that you’re trying to create there are forbidden characters such as forward slash, asterisks, question marks, backslash, double quotes, etc. (a complete list can be found here).

However with your path //logdir//train this doesn’t seem the case. What I can suggest you is to try setting self.log_dir (if you’re looking for it inside the code is here) to a path of your choice. Maybe something like this (you’re under windows so it will be a little bit different):

self.log_dir = "/Users/claudia/Desktop/logs/"

Just make sure that you don’t have forbidden characters in your path and you should be ok. Also if you want you can create the folders on your own and you won’t have the error.

Hope this helps.

Answered By: ClaudiaR

sorry to reopen this issue.

I have the exact same Problem:
by running my Code:

# prepare config
config = mebheaderConfig()
config.display()
# define the model
model = MaskRCNN(mode='training', model_dir='.\', config=config)
# load weights (mscoco) and exclude the output layers
model.load_weights('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=5, layers='heads')

i get the Error:

OSError: [WinError 123] Die Syntax für den Dateinamen, Verzeichnisnamen oder die Datenträgerbezeichnung ist falsch: '//'

i already changed self.log_dir to different path’s.

at this moment it’s self.log_dir = ‘.logdir’

i also updatet the TensorBoard function to:

callbacks = [
            keras.callbacks.TensorBoard(log_dir=self.log_dir,
                                        histogram_freq=0, write_graph=True, write_images=False,profile_batch = 0),
            keras.callbacks.ModelCheckpoint(self.checkpoint_path,
                                            verbose=0, save_weights_only=True, profile_batch = 0),
    ]

the error still exists. Is there any more idea to solve this problem ?

Answered By: phonigs
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.