有在google爬文,只是查到的解決方法沒辦法解決問題(刪除後重灌Tensorflow和Keras)。
以下是出問題的程式碼(instance(origin).py)
import os
import sys
import skimage.io
import matplotlib.pyplot as plt
from mrcnn import utils
import mrcnn.model as modelling
from mrcnn import visualize
from mrcnn.config import Config
# Root directory of the Mask R-CNN library
ROOT_DIR = os.path.abspath("C:/Users/ChaoHuaLee/Desktop/Mask_RCNN")
# Import Mask RCNN
sys.path.append(ROOT_DIR)
# Path to trained weights file (Make sure to specify the correct path)
COCO_MODEL_PATH = "C:/Users/ChaoHuaLee/PycharmProjects/pythonProject/mask_rcnn_coco.h5"
# Directory to save logs and model checkpoints
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
# Download COCO trained weights if necessary
if not os.path.exists(COCO_MODEL_PATH):
utils.download_trained_weights(COCO_MODEL_PATH)
# Define the configuration for your custom dataset
class CustomConfig(Config):
NAME = "custom_dataset"
IMAGES_PER_GPU = 1 # Adjust based on your GPU memory
NUM_CLASSES = 1 + 1 # Number of classes (1 for background + your custom classes)
GPU_COUNT = 1
DETECTION_MIN_CONFIDENCE = 0.9
# Create the inference model
inference_config = CustomConfig()
model = modelling.MaskRCNN(mode="inference", config=inference_config, model_dir=MODEL_DIR)
# Load pre-trained weights (COCO)
model.load_weights(COCO_MODEL_PATH, by_name=True)
# Load the image you want to perform instance segmentation on
image = skimage.io.imread("C:/Users/ChaoHuaLee/Desktop/Bone_age_CNN/testimg")
# Perform instance segmentation
results = model.detect([image], verbose=1)
# Visualize the results
r = results[0]
visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
class_names=None,
scores=r['scores'], title="Instance Segmentation", show_mask=True
)
# Save or display the segmented image as needed
plt.savefig("output_image.png")
plt.show()
我再附上導入的mask rCNN程式碼(model.py)問題產生的部分
import os
import random
import datetime
import re
import math
import logging
from collections import OrderedDict
import multiprocessing
import numpy as np
import skimage.transform
import tensorflow as tf
import keras
import keras.backend as K
import keras.layers as KL
import keras.engine as KE # 這裡
import keras.models as KM
因為 AI 這幾年變動很大,有時改版可能哪個模組就改掉或移位置了
就像 Keras 後來屬於 TF 的一部分一樣
我提供一點思路
tensorflow>=1.3.0
keras>=2.0.8
補充,剛看到不同版本的架構,差異還真的很大
keras=2.0.8 的 keras.engine
目前最新版的keras.engine