Mask R-CNN摩托车检测训练报错:Python int无法转C ssize_t
OverflowError: Python int too large to convert to C ssize_t in Mask R-CNN Motorcycle Detection Hey there, let's break down why you're hitting this error and how to fix it quickly—no need to re-annotate all those images!
The core issue here is two small but critical mistakes in your dataset class code, plus a misunderstanding of how skimage.draw.polygon expects coordinates. Here's the step-by-step fix:
1. Swap Coordinate Order for skimage.draw.polygon
Skimage's polygon function expects row (y-axis) coordinates first, then column (x-axis) coordinates—but you're passing x values first, then y values. Since your image width (1280) is larger than its height (720), passing x values as row indices creates values way beyond the image's height, which triggers that overflow error when converting to a C integer type.
Fix this line in your load_mask method:
# Before (wrong order) rr, cc = skimage.draw.polygon(p[0], p[1]) # After (correct order) rr, cc = skimage.draw.polygon(p[1], p[0])
2. Fix Image Index Off-by-One Error
In your load_bike method, you start count at 0 but immediately increment it to 1 before accessing list_file[count]. Since list_file is 0-indexed, this means you're skipping the first image and risking an index out-of-bounds error later on.
Adjust the counting logic like this:
count = 0 # Keep starting at 0 image_dir = "" for annot in annotations: if annot['objects']: for i in annot['objects']: points = i['points'] exterior = points['exterior'] # Set image_dir as before... image_path = os.path.join(image_dir, list_file[count]) # Use count directly # Rest of your code for processing polygons remains the same... count += 1 # Increment only after processing all objects in the current annotation
3. Add Coordinate Clipping (Optional but Recommended)
To make your code more robust against any accidental out-of-bounds coordinates in future annotations, add a quick clip to ensure all coordinates stay within the image dimensions:
def load_mask(self, image_id): print('We are in Load mask function.................') image_info = self.image_info[image_id] if image_info["source"] != "bike": return super(self.__class__, self).load_mask(image_id) info = self.image_info[image_id] mask = np.zeros([info["height"], info["width"], len(info["polygons"])], dtype=np.uint8) for i, p in enumerate(info["polygons"]): # Clip coordinates to stay within image bounds y_coords = np.clip(p[1], 0, info["height"] - 1) x_coords = np.clip(p[0], 0, info["width"] - 1) rr, cc = skimage.draw.polygon(y_coords, x_coords) mask[rr, cc, i] = 1 return mask.astype(np.bool), np.ones([mask.shape[-1]], dtype=np.int32)
Your annotation data looks perfectly valid (all coordinates are within the 1280x720 image size), so once you apply these fixes, your dataset should load correctly, and training can start without issues.
内容的提问来源于stack exchange,提问作者saba javad

