0

I am trying to develop a simple image classification model in Azure ML notebooks. ResNet50 model was trained and the custom weights from the model is being used in the following code for image classification. The custom weights are saved in a folder called Model.

import os
working_directory = os.getcwd()
model_directory = working_directory + "/Model/model.h5" 

The above code is being used for accessing the saved model.

def classifyingImages(image_list):
    value = 0

    for image in image_list:
        image_resized = cv2.resize(image, (img_height, img_width))
        image = np.expand_dims(image_resized, axis=0)

        
        model = load_model(model_directory)
        #classifying the image
        prediction = model.predict(image)
        output_class = class_names[np.argmax(prediction)]

        #getting the image name
        image_name = img_name_list[value]
        print(image_name)

        if(np.argmax(prediction)==0):
            print("check negative")
            # cv2.imwrite((negative_path+"/"+image_name), image)
        else:
            print("check positive")
            # cv2.imwrite((path_positive+"/"+image_name), image)

        value = value +1


    return value

classifyingImages(image_list)

The code added above is the image classification code

The image_list contains the test images which saved in the blob storage.

After running the classification function i get the error str' object has no attribute 'decode and as a solution i tried to change the h5py lib version using below code. But still it gives me the same error. It would be great if i could a solution for this issue. Thank you in advance. !pip install h5py==2.10.0

The stack trace

AttributeError                            Traceback (most recent call last)
<ipython-input-96-6e38a2f74291> in <module>
     42     return value
     43 
---> 44 classifyingImages(image_list)

<ipython-input-96-6e38a2f74291> in classifyingImages(image_list)
     20         print(image.dtype)
     21 
---> 22         model = load_model(model_directory)
     23 
     24         #classifying the image

/anaconda/envs/azureml_py36/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/save.py in load_model(filepath, custom_objects, compile)
    144   if (h5py is not None and (
    145       isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
--> 146     return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
    147 
    148   if isinstance(filepath, six.string_types):

/anaconda/envs/azureml_py36/lib/python3.6/site-packages/tensorflow_core/python/keras/saving/hdf5_format.py in load_model_from_hdf5(filepath, custom_objects, compile)
    164     if model_config is None:
    165       raise ValueError('No model found in config file.')
--> 166     model_config = json.loads(model_config.decode('utf-8'))
    167     model = model_config_lib.model_from_config(model_config,
    168                                                custom_objects=custom_objects)

AttributeError: 'str' object has no attribute 'decode'
Wishva
  • 25
  • 7
  • strings dont have decode, they have encode. Bytes have decode – Nullman Mar 13 '22 at 08:29
  • is it because of the image type? – Wishva Mar 13 '22 at 08:51
  • what is happening is that a string is getting somewhere it shouldn't, Could you provide a stack trace? it will help track the issue down – Nullman Mar 13 '22 at 08:52
  • Hi i added the stack trace – Wishva Mar 13 '22 at 09:11
  • `json.loads(model_config.decode('utf-8'))` is trying to decode the byte content of the file, but is getting a string instead. what is the type/contents of `image_list` ? – Nullman Mar 13 '22 at 09:15
  • 1
    you may have a buggy combination of tensorflow and h5py, look at [this](https://stackoverflow.com/questions/53740577/does-any-one-got-attributeerror-str-object-has-no-attribute-decode-whi) question and the answers – Nullman Mar 13 '22 at 09:17
  • The images are a type of uint8 – Wishva Mar 13 '22 at 10:59
  • 1
    Thank you @Nullman it was due to the different versions of tensorflow after i changed the version it worked – Wishva Mar 16 '22 at 19:03
  • i did down grade the h5py but i never did it with the tensorflow and keras. But when i check the training code the versions of tensor and keras were different from the version i was using Azure so i down graded both of them to match the initial training version – Wishva Mar 19 '22 at 18:48

0 Answers0