Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I used the trained model to test an image. There are some errors. #22

Closed
miraclewxl opened this issue Nov 3, 2017 · 5 comments
Closed

Comments

@miraclewxl
Copy link

The code is here followed the demo.ipynb. But there are some errros.

import os
import sys
import random
import math
import numpy as np
import scipy.misc
import matplotlib
import matplotlib.pyplot as plt

import coco
import utils
import model as modellib
import visualize

Create model object in inference mode.

model = modellib.MaskRCNN(mode="inference", model_dir='mask_rcnn_coco.h5', config=0)

Load weights trained on MS-COCO

model.load_weights('mask_rcnn_coco.h5', by_name=True)

COCO Class names

Index of the class in the list is its ID. For example, to get ID of

the teddy bear class, use: class_names.index('teddy bear')

class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
'kite', 'baseball bat', 'baseball glove', 'skateboard',
'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
'teddy bear', 'hair drier', 'toothbrush']

Load a random image from the images folder

file_names = next(os.walk('/media/wxl/00063DAF000FECC3/Mask_RCNN-master/images'))
image = scipy.misc.imread(os.path.join('/media/wxl/00063DAF000FECC3/Mask_RCNN-master/images', random.choice(file_names)))

Run detection

results = model.detect([image], verbose=1)

Visualize results

r = results[0]
visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
class_names, r['scores'])

The errors locate in:
File "/media/wxl/00063DAF000FECC3/Mask_RCNN-master/test.py", line 20, in
model = modellib.MaskRCNN(mode="inference", model_dir='mask_rcnn_coco.h5', config=0)
I think the model is load incorrect. But I have no idea about the mistakes. Thanks for your helps

@miraclewxl
Copy link
Author

That's the test code

@miraclewxl
Copy link
Author

I think the logs of trained model not being load. I search it in the files but I cann't find it. Where can I find it?

@andersy005
Copy link

@miraclewxl,

Here is the documentation snippet for the model module
screenshot-2017-11-3 my_demo

  • First, you need to change model_dir argument to a directory you want to save logs and trained model. In your example, you are setting this argument to the path of mask_rcnn_coco.h5 pre-trained weights.

  • Second, config should be a sub-class of the Config class and not an integer.

As mentioned in the notebook, set the following variables as follows

# Root directory of the project
ROOT_DIR = os.getcwd()

# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")

# Path to trained weights file
# Download this file and place in the root of your 
# project (See README file for details)
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

Then, sub-class the config class as follows:

class InferenceConfig(coco.CocoConfig):
    # Set batch size to 1 since we'll be running inference on
    # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

config = InferenceConfig()

NOTE: Make sure to download the pre-trained weights from this link

Finally, create a model and load pre-trained weights

# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

# Load weights trained on MS-COCO
model.load_weights(COCO_MODEL_PATH, by_name=True)

@miraclewxl
Copy link
Author

Thank for your help @sandersy005

@andersy005
Copy link

@miraclewxl, You are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants