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:\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: '//'