测试自己训练出来的模型

先贴跑通的代码与对应输出

# -*- coding: utf-8 -*-
import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt
import cv2
import time
from mrcnn.config import Config
from datetime import datetime
# Root directory of the project
ROOT_DIR = os.path.abspath("C:\\Users\\91078\\Desktop\\Mask_RCNN\\Mask_RCNN-master")
 
# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
# Import COCO config
# sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local version
# from samples.coco import coco
 
 
# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
print(MODEL_DIR)
# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(MODEL_DIR ,"mask_rcnn_shapes_0001.h5")
# !!!注意.h5文件的路径
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
    utils.download_trained_weights(COCO_MODEL_PATH)
    print("cuiwei***********************")
 
# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")
 
class ShapesConfig(Config):
    """Configuration for training on the toy shapes dataset.
    Derives from the base Config class and overrides values specific
    to the toy shapes dataset.
    """
    # Give the configuration a recognizable name
    NAME = "shapes"
 
    # Train on 1 GPU and 8 images per GPU. We can put multiple images on each
    # GPU because the images are small. Batch size is 8 (GPUs * images/GPU).
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1
 
    # Number of classes (including background)
    NUM_CLASSES = 1 + 3  # background + 3 shapes
 
    # Use small images for faster training. Set the limits of the small side
    # the large side, and that determines the image shape.
    IMAGE_MIN_DIM = 480
    IMAGE_MAX_DIM = 640
 
    # Use smaller anchors because our image and objects are small
    RPN_ANCHOR_SCALES = (8 * 6, 16 * 6, 32 * 6, 64 * 6, 128 * 6)  # anchor side in pixels
 
    # Reduce training ROIs per image because the images are small and have
    # few objects. Aim to allow ROI sampling to pick 33% positive ROIs.
    TRAIN_ROIS_PER_IMAGE =100
 
    # Use a small epoch since the data is simple
    STEPS_PER_EPOCH = 100
 
    # use small validation steps since the epoch is small
    VALIDATION_STEPS = 50
 
#import train_tongue
#class InferenceConfig(coco.CocoConfig):
class InferenceConfig(ShapesConfig):
    # 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()
 
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
 
 
# 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)
model.load_weights(COCO_MODEL_PATH, by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc","mrcnn_bbox", "mrcnn_mask"])
 
# 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', 'ball', 'triangle', 'rectangle']
# Load a random image from the images folder
file_names = next(os.walk(IMAGE_DIR))[2]
image = skimage.io.imread(os.path.join(IMAGE_DIR, random.choice(file_names)))
 
a=datetime.now()
# Run detection
results = model.detect([image], verbose=1)
b=datetime.now()
# Visualize results
print("shijian",(b-a).seconds)
r = results[0]
visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
                            class_names, r['scores'])
 

输出:

 

ERROR

ERROR1:

ValueError: Layer #389 (named "mrcnn_bbox_fc"), weight <tf.Variable 'mrcnn_bbox_fc/kernel:0' shape=(1024, 8) dtype=float32_ref> has shape (1024, 8), but the saved weight has shape (1024, 324).

把model.load_weights(COCO_MODEL_PATH, by_name=True)改为:

model.load_weights(COCO_MODEL_PATH, by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc","mrcnn_bbox", "mrcnn_mask"])

参考文章:

mask rcnn测试中遇到的问题解决_ilinda的博客-优快云博客https://blog.youkuaiyun.com/qq_34713831/article/details/85797622

ERROR2:

OSError: Unable to open file (truncated file: eof = 22118400, sblock->base_addr = 0, stored_eof = 152662144)

将下载未完成的.h5文件删除

参考文章:

解决问题:OSError: Unable to open file (truncated file: eof = 22118400, sblock->base_addr = 0, stored_eof_贵在坚持-优快云博客背景载入模型或者运行代码时候出现了File “h5py/_objects.pyx”, line 54, in h5py._objects.with_phil.wrapperFile “h5py/_objects.pyx”, line 55, in h5py._objects.with_phil.wrapperFile “h5py/h5f.pyx”, line 88, in h5py.h5f.openOSError: Unable to open file (truncated file: eofhttps://blog.youkuaiyun.com/liupeng19970119/article/details/108963671

### Linux 桥接网络配置教程及故障排查方法 #### 配置桥接网络 为了使 VMware 中的 Ubuntu 16.04 正确使用桥接模式并获取 IPv6 连接,需确保主机和客户机之间的网络设置正确。以下是具体操作: 1. **编辑 `/etc/network/interfaces` 文件** 打开终端并输入以下命令来编辑网络接口文件: ```bash sudo nano /etc/network/interfaces ``` 添加或修改如下内容以创建桥接设备 `br0` 并将其关联至物理网卡(假设为 `eth0` 或者其他名称): ```plaintext auto lo br0 iface lo inet loopback iface eth0 inet manual iface br0 inet dhcp bridge_ports eth0 bridge_stp off bridge_fd 0 bridge_maxwait 0 ``` 2. **重启网络服务** 修改完成后保存退出,并运行下面这条指令让改动生效。 ```bash sudo systemctl restart networking.service ``` 3. **验证桥接是否成功建立** 可以通过 `ifconfig` 命令查看新创建的桥梁是否存在以及其 IP 地址分配情况[^3]。 #### 故障排除指南 如果按照上述步骤完成配置后仍然无法访问互联网,则可依照下列建议逐一排查可能存在的问题: - **确认 DHCP 客户端工作正常**:有时由于某些原因导致 DHCP 请求失败而未能自动获取到有效的 IP 地址。此时可以尝试手动指定静态 IP 地址来进行测试。 - **检查防火墙规则**:部分情况下,默认的安全策略可能会阻止外部通信。可以通过临时禁用防火墙(`sudo ufw disable`)观察是否有改善;若有则表明需要调整相应规则允许必要的流量进出。 - **核查路由表项**:利用 `route -n` 查看当前系统的路由信息,特别注意默认网关的存在性和可达性。对于多网卡环境尤其重要,因为错误的优先级设定可能导致数据包被误导向不合适的出口路径。 - **诊断 DNS 解析能力**:即使获得了正确的 IP 地址也有可能因域名解析异常造成网页加载困难等问题。这时应该检验 `/etc/resolv.conf` 内容指向可靠的公共DNS服务器如 Google (8.8.8.8) 或 Cloudflare (1.1.1.1),并且借助 `nslookup` 工具进一步定位潜在瓶颈所在之处。 最后值得注意的是,在较新的发行版里推荐采用 Netplan 来替代传统的 ifupdown 方式管理网络配置,因为它更易于理解和维护。不过针对特定场景下的最佳实践还需依据实际情况灵活运用参考资料中的知识进行适当调整[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值