Error creating model "segmentation_models" in Keras

Issue

A week ago, my Notebook in Google Colaboratory was working fine after installing the following libraries:

!pip install te
!pip install tensorflow==2.1
!pip install keras==2.3.1

!pip install -U segmentation-models
!pip install -U --pre segmentation-models

and

import tensorflow as tf
import segmentation_models as sm
import glob
import cv2
import numpy as np
from matplotlib import pyplot as plt
import keras 

from keras import normalize
from keras.metrics import MeanIoU

It worked:

# set class weights for dice_loss (car: 1.; pedestrian: 2.; background: 0.5;)
dice_loss = sm.losses.DiceLoss(class_weights=np.array([0.25, 0.25, 0.25, 0.25])) 
focal_loss = sm.losses.CategoricalFocalLoss()
total_loss = dice_loss + (1 * focal_loss)
metrics = [sm.metrics.IOUScore(threshold=0.5), sm.metrics.FScore(threshold=0.5)]

BACKBONE1 = 'resnet34'
preprocess_input1 = sm.get_preprocessing(BACKBONE1)

# preprocess input
X_train1 = preprocess_input1(X_train)
X_test1 = preprocess_input1(X_test)

# define model
model1 = sm.Unet(BACKBONE1, encoder_weights='imagenet', classes=n_classes, activation=activation)

Then, due to an error, I made changes:

!pip install -q tensorflow==2.1
!pip install -q keras==2.3.1
!pip install -q tensorflow-estimator==2.1

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ["SM_FRAMEWORK"] = "tf.keras"

from tensorflow import keras

from tensorflow.keras.utils import normalize
from tensorflow.keras.metrics import MeanIoU 

After that, this part does not work:

 # define model
    model1 = sm.Unet(BACKBONE1, encoder_weights='imagenet', classes=n_classes, activation=activation)

Error:

/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/keras/saving/hdf5_format.py
in load_weights_from_hdf5_group(f, layers)
649 """
650 if ‘keras_version’ in f.attrs:
–> 651 original_keras_version = f.attrs[‘keras_version’].decode(‘utf8’)
652 else:
653 original_keras_version = ‘1’

AttributeError: ‘str’ object has no attribute ‘decode’

Problem with loading scale values. But I don’t know how to fix it

Solution

You may need to install h5py of the following version, source.

pip install -q h5py==2.10.0

FYI, I was able to reproduce your error on colab and the above solution resolve this.

Answered By – M.Innat

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published