iT邦幫忙

0

ModuleNotFoundError: No module named 'keras.engine'

  • 分享至 

  • xImage

有在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
錯誤代碼:
Traceback (most recent call last):
File "C:\Users\ChaoHuaLee\PycharmProjects\pythonProject\instance(origin).py", line 6, in <module>
import mrcnn.model as modelling
File "C:\Users\ChaoHuaLee\PycharmProjects\pythonProject\venv\Lib\site-packages\mrcnn\model.py", line 24, in <module>
import keras.engine as KE
ModuleNotFoundError: No module named 'keras.engine'

Process finished with exit code 1
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
hokou
iT邦好手 1 級 ‧ 2023-09-13 13:30:13
最佳解答

因為 AI 這幾年變動很大,有時改版可能哪個模組就改掉或移位置了
就像 Keras 後來屬於 TF 的一部分一樣

我提供一點思路

  1. 降版本
  2. 看你目前的版本把你要的功能挪到哪邊去了
tensorflow>=1.3.0
keras>=2.0.8

補充,剛看到不同版本的架構,差異還真的很大
keras=2.0.8 的 keras.engine
目前最新版的keras.engine

其他找的資料
How to import keras.engine.topology in Tensorflow?

了解,我近期處理看看。
感謝回答~

froce iT邦大師 1 級 ‧ 2023-09-13 14:59:44 檢舉

你這個差異實在太大了,有留之前的套件版本資訊的話建議直接降版比較快。

剛剛將KE換成KL後可正常運作了~

我要發表回答

立即登入回答