错误:没有名为'tensorflow.keras'的模块

ego6inou  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(287)

我正在编写使用深度学习构建提取的代码,但当我尝试导入库文件时,它显示错误“No module named 'tensorflow. keras'”。我无法解决它。有人能帮助我吗?

import os
import cv2
import numpy as np
import random
from matplotlib import pyplot as plt
from patchify import patchify
from PIL import Image
import segmentation_models as sm
from sklearn.model_selection import train_test_split

from tensorflow.keras.utils import to_categorical

from tensorflow.keras.metrics import MeanIoU

我目前的tensorflow 版本是-2.9.1,keras版本是-2.2.0
我得到的错误是:-

ImportError                               Traceback (most recent call last)
File ~\anaconda3\envs\tensorflow\lib\site-packages\segmentation_models\__init__.py:98, in <module>
     97 try:
---> 98     set_framework(_framework)
     99 except ImportError:

File ~\anaconda3\envs\tensorflow\lib\site-packages\segmentation_models\__init__.py:67, in set_framework(name)
     66 if name == _KERAS_FRAMEWORK_NAME:
---> 67     import keras
     68     import efficientnet.keras  # init custom objects

File ~\anaconda3\envs\tensorflow\lib\site-packages\keras\__init__.py:3, in <module>
      1 from __future__ import absolute_import
----> 3 from . import utils
      4 from . import activations

File ~\anaconda3\envs\tensorflow\lib\site-packages\keras\utils\__init__.py:25, in <module>
     24 from .np_utils import normalize
---> 25 from .multi_gpu_utils import multi_gpu_model

File ~\anaconda3\envs\tensorflow\lib\site-packages\keras\utils\multi_gpu_utils.py:7, in <module>
      5 from __future__ import print_function
----> 7 from ..layers.merge import concatenate
      8 from .. import backend as K

File ~\anaconda3\envs\tensorflow\lib\site-packages\keras\layers\__init__.py:4, in <module>
      3 from ..utils.generic_utils import deserialize_keras_object
----> 4 from ..engine.base_layer import Layer
      5 from ..engine import Input

File ~\anaconda3\envs\tensorflow\lib\site-packages\keras\engine\__init__.py:8, in <module>
      7 from .network import get_source_inputs
----> 8 from .training import Model

File ~\anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py:21, in <module>
     20 from .training_utils import weighted_masked_objective
---> 21 from . import training_arrays
     22 from . import training_generator

File ~\anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training_arrays.py:14, in <module>
     13 from .. import backend as K
---> 14 from .. import callbacks as cbks
     15 from ..utils.generic_utils import Progbar

File ~\anaconda3\envs\tensorflow\lib\site-packages\keras\callbacks.py:18, in <module>
     17 from collections import OrderedDict
---> 18 from collections import Iterable
     19 from .utils.generic_utils import Progbar

ImportError: cannot import name 'Iterable' from 'collections' (C:\Users\DOLONCHAPA\anaconda3\envs\tensorflow\lib\collections\__init__.py)

During handling of the above exception, another exception occurred:

ModuleNotFoundError                       Traceback (most recent call last)
Input In [1], in <cell line: 8>()
      6 from patchify import patchify
      7 from PIL import Image
----> 8 import segmentation_models as sm
      9 from sklearn.model_selection import train_test_split
     11 from tensorflow.keras.utils import to_categorical

File ~\anaconda3\envs\tensorflow\lib\site-packages\segmentation_models\__init__.py:101, in <module>
     99 except ImportError:
    100     other = _TF_KERAS_FRAMEWORK_NAME if _framework == _KERAS_FRAMEWORK_NAME else _KERAS_FRAMEWORK_NAME
--> 101     set_framework(other)
    103 print('Segmentation Models: using `{}` framework.'.format(_KERAS_FRAMEWORK))
    105 # import helper modules

File ~\anaconda3\envs\tensorflow\lib\site-packages\segmentation_models\__init__.py:71, in set_framework(name)
     69 elif name == _TF_KERAS_FRAMEWORK_NAME:
     70     from tensorflow import keras
---> 71     import efficientnet.tfkeras  # init custom objects
     72 else:
     73     raise ValueError('Not correct module name `{}`, use `{}` or `{}`'.format(
     74         name, _KERAS_FRAMEWORK_NAME, _TF_KERAS_FRAMEWORK_NAME))

File ~\anaconda3\envs\tensorflow\lib\site-packages\efficientnet\tfkeras.py:6, in <module>
      2 from . import model
      4 from .preprocessing import center_crop_and_resize
----> 6 EfficientNetB0 = inject_tfkeras_modules(model.EfficientNetB0)
      7 EfficientNetB1 = inject_tfkeras_modules(model.EfficientNetB1)
      8 EfficientNetB2 = inject_tfkeras_modules(model.EfficientNetB2)

File ~\anaconda3\envs\tensorflow\lib\site-packages\efficientnet\__init__.py:50, in inject_tfkeras_modules(func)
     49 def inject_tfkeras_modules(func):
---> 50     import tensorflow.keras as tfkeras
     51     @functools.wraps(func)
     52     def wrapper(*args, **kwargs):
     53         kwargs['backend'] = tfkeras.backend

ModuleNotFoundError: No module named 'tensorflow.keras'
dba5bblo

dba5bblo1#

我在我的系统上复制了你的情况,它运行得很完美。
我认为有版本相关的问题你可以使用这套版本。

tensorflow version:          2.8.2
keras version:               2.8.0
numpy version:               1.21.6
cv2 version:                 4.6.0
segmentation_models version: 1.0.1
python version:              3.7.14
watbbzwu

watbbzwu2#

@世树·库马尔·乔杜里提到,
您的系统环境中存在tensorflowkeras版本不匹配。(TF=2.9.1,Keras=2.2)
要解决此问题,您需要安装与已安装的tensorflow==2.9版本相同的keras==2.9版本。
请使用下面的代码安装keras==2.9,然后再次尝试运行代码

!pip install keras==2.9

!pip install patchify    #To install and import other mentioned libraries  in code
!pip install segmentation_models

相关问题