pythontransfer-learning

Can't Import Pre Trained ResNet34 for U-Net Segmentation


I load the pre-trained ResNet34 for my downsampling path in encoder with this code

from tensorflow.keras.applications import ResNet34
from tensorflow.keras.layers import Input

# create the ResNet34 encoder
inputs = Input(shape=(512, 512, 3))
encoder = ResNet34(include_top=False, weights='imagenet', input_tensor=inputs)

# set encoder layers to non-trainable
for layer in encoder.layers:
    layer.trainable = False

but it get an error like this

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-44-45ab730a26ec> in <module>
----> 1 from tensorflow.keras.applications import ResNet34
      2 from tensorflow.keras.layers import Input
      3 
      4 # create the ResNet34 encoder
      5 inputs = Input(shape=(512, 512, 3))

ImportError: cannot import name 'ResNet34' from 'tensorflow.keras.applications' (/usr/local/lib/python3.8/dist-packages/keras/api/_v2/keras/applications/__init__.py)

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

i try with keras too, but the weight parameter isn't recognized


Solution

  • Your import path looks correct to me but I doubt that keras provides ResNet34.

    Looking at the available models in the documentation, I don't see ResNet34. https://keras.io/api/applications/

    The smallest available Resnet is ResNet50. I suggest you to try that one, and it should work.

    Additionally, I found this SO answer which also argues that keras.applications does not have ResNet34 and provide an alternative solution. Is ResNet34 available in Keras.application?