16

I'm unable to import this module

import keras.applications.resnet

ModuleNotFoundError
in () ----> 1 import keras.applications.resnet

ModuleNotFoundError: No module named 'keras.applications.resnet'


keras resnet link

Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111
Aayush Bajaj
  • 161
  • 1
  • 1
  • 4

8 Answers8

29

Keras team hasn't included resnet, resnet_v2 and resnext in the current module, they will be added from Keras 2.2.5, as mentioned here.

For a workaround, you can use keras_applications module directly to import all ResNet, ResNetV2 and ResNeXt models, as given below

from keras_applications.resnet import ResNet50

Or if you just want to use ResNet50

from keras.applications.resnet50 import ResNet50

Alternatively, you can always build from source as mentioned here.

suvigyavijay
  • 454
  • 3
  • 6
  • @SuvigyaVijay I am trying your method, but I seem to be getting a NoneType function back? `mport keras_applications.resnet_v2 as resenet2 return resenet2.ResNet101V2` Save the return to `base_model` But if I call ` base_model(weights='imagenet', include_top=False)`, I get back `AttributeError: 'NoneType' object has no attribute 'image_data_format'` – MasayoMusic Jul 11 '19 at 06:16
  • `keras_applications` doesn't seem to be compatible with eager execution, so I get an error when I try to use this in tf2.0: `"module 'tensorflow' has no attribute 'get_default_graph'"` – craq Sep 23 '19 at 03:44
  • 1
    I tried this and got `ModuleNotFoundError: No module named 'keras.applications.resnet50'` – Revolucion for Monica Sep 08 '22 at 16:25
10

try to use

from tensorflow.keras.applications.resnet50 import ResNet50
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
alireza
  • 101
  • 1
  • 2
6

Found a workaround to use ResNeXt in Keras 2.2.4 here.

ResNeXt50() function needs 4 more arguments: backend, layers, models and utils.

import keras
from keras_applications.resnext import ResNeXt50

model = ResNeXt50(weights='imagenet',
                  backend=keras.backend,
                  layers=keras.layers,
                  models=keras.models,
                  utils=keras.utils)
Hsinwei
  • 69
  • 2
  • 3
1

In Keras there are multiple flavours of ResNet, you will have to specify the version of ResNet that you want e.g. You wish to load the ResNet50.

Use

from keras.applications import ResNet50

Edit 2 This is the list you get when you use dir() command on applications

['DenseNet121', 'DenseNet169', 'DenseNet201', 'InceptionResNetV2', 'InceptionV3', 'MobileNet', 'MobileNetV2', 'NASNetLarge', 'NASNetMobile', 'ResNet50', 'VGG16', 'VGG19', 'Xception', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'absolute_import', 'backend', 'densenet', 'division', 'inception_resnet_v2', 'inception_v3', 'keras_applications', 'keras_modules_injection', 'layers', 'mobilenet', 'mobilenet_v2', 'models', 'nasnet', 'print_function', 'resnet50', 'utils', 'vgg16', 'vgg19', 'xception'], the models visible here can be laoded like this, There are some models like ResNet101 missing here, let me see if I can come up with a way to fix this.

Edit Proof that this works too

enter image description here

To see all the available versions of the Resnet models, visit https://keras.io/applications/#resnet

anand_v.singh
  • 2,768
  • 1
  • 16
  • 35
0

Check the versions:

pip list | grep Keras

If it's already installed, uninstall and upgrade:

pip uninstall Keras
pip install Keras==2.3.1

pip uninstall Keras-Applications
pip install Keras-Applications==1.0.8

pip uninstall Keras-Preprocessing
pip install Keras-Preprocessing==1.1.0
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Progga Ilma
  • 578
  • 6
  • 8
  • I think it would be better to use "pip install --upgrade", as these versions have quickly become outdated. – jtb Sep 19 '21 at 17:43
0

There is a python package named 'keras-resnet' which has ResNet50, ResNet101, ResNet152 and many more variants of ResNet. (https://pypi.org/project/keras-resnet/)

Installation is also quite easy. Just type

pip install keras-resnet

It will install this module and then use it like:

from keras_resnet.models import ResNet50, ResNet101, ResNet152

backbone = ResNet50(inputs=image_input, include_top=False, freeze_bn=True)
C2, C3, C4, C5 = backbone.outputs # this will give you intermediate 
# outputs of four blocks of resnet if you want to merge low and high level features

I am using backbones from this module and is working fine for me!

Dharman
  • 30,962
  • 25
  • 85
  • 135
jd95
  • 404
  • 6
  • 14
0
from keras.applications.resnet
import ResNet101    

tf.keras.backend.clear_session
model=VGG19()
model.summary()


tf.keras.utils.plot_model(model,show_shapes=True)
visualkeras.layered_view(model,legend=True)
  • 1
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P May 30 '22 at 13:53
0

Before running

tensorflow.keras.applications.resnet50 import ResNet50 

you need to run

from tensorflow import keras