2

I have downloaded some H5 files, which to my current understanding contain trained models for image recognition. I can successfully apply these models on images using Python, Keras, Tensorflow and ImageAI.

From some examples on the Internet, I also figured out that one of the models was trained for detecting cars and persons. So I fed some images of cars into it and it worked.

I'm now trying to get that information from the H5 file itself, so that I can pass some expected input and some non-expected into the detector to see what happens.

I searched Stack Overflow on how to read an H5 file and get information out of it [1], [2], [3], [4], but all output I get is just a bunch of technical data.

Let's have a concrete example. I have a model that obviously recognizes cars and trucks:

Image of cars and trucks detected

As we can see in the image, the rectangles have labels like car and truck, so that's the type of objects it can recognize. I want to get exactly that information out of the H5 file.

I have

import h5py

def printH5Content(filename: str):
    with h5py.File(filename, 'r') as f:
        print("Keys: %s" % f.keys())
        a_group_key = list(f.keys())[0]
        print(list(f[a_group_key]))

printH5Content(model_path)

but it only gives me

Keys: <KeysViewHDF5 ['model_weights']>
['add_1', 'add_10', 'add_11', 'add_12', 'add_13', 'add_14', ... 'zero_padding2d_4', 'zero_padding2d_5']

Also, the visitor does not give more information:

def printH5Content(filename: str):
    with h5py.File(filename, 'r') as f:
        f.visit(print)

How do I get the words car and truck from the H5 file, so that I can find out what it was trained for?

Edit from the comments:

I'm convinced that the term car and truck must be inside the H5 file by elimination. I have 3 inputs: the code, the H5 model and the JPG image.

  1. A JPG is just an arragement of pixels. It does not know anything about the content.
  2. My code is only 6 lines and does not contain any of the terms
  3. The only option left is the H5 file

The minimal version of my code is:

from imageai.Detection import ObjectDetection
detector = ObjectDetection()
detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath("./models/yolo-tiny.h5")
detector.loadModel()
detection = detector.detectObjectsFromImage(input_image="./input/cars.jpg", output_image_path="./output/cars.jpg")
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • If you mean HDF5 as keras model files, the classes are not stored in the HDF5 file, so you won't be able to find this information. That is post-processing that is done in python. – Dr. Snoopy Dec 26 '19 at 16:33
  • In the python code or program that you are running, you have not provided information about that. – Dr. Snoopy Dec 26 '19 at 17:01
  • The contents of an `H5` file are `groups`, `datasets` and attributes, if any, at each level. You could find all that with `h5py` (read its docs), or it might be easier to use one of the `HDF5` readers. `h5dump` is probably available on your operating system. – hpaulj Dec 26 '19 at 17:05
  • Then your question cannot be answered, as I said the HDF5 file does not contain class information, the semantic meaning of the classes is interpreted by code outside of the HDF5 file when doing model.predict. The model itself does not know anything about classes other that indices. – Dr. Snoopy Dec 26 '19 at 18:37
  • No, you cannot execute a H5 file, the program code you are running and loads the H5 model has that information. – Dr. Snoopy Dec 26 '19 at 18:41
  • I was checking the ImageAI library, the classes are defined in there, just see: https://github.com/OlafenwaMoses/ImageAI/tree/master/imageai/Detection#custom-object-detection – Dr. Snoopy Dec 26 '19 at 18:56
  • `HDFView` is a `java` based visual viewer. Being visual and interactive it might be better than the `h5dump`. I haven't installed the java base on my linux machine, so I haven't tried it. – hpaulj Dec 26 '19 at 19:24

1 Answers1

0

I was wrong with the assumption that the terms are not in the code. They are not in my code, but in the imported ImageAI library. This can be confirmed by using ImageAI without even loading an H5 file:

from imageai.Detection import ObjectDetection
detector = ObjectDetection()
detector.setModelTypeAsTinyYOLOv3()
for _, value in detector.numbers_to_names.items():
    print(value)

This gives a list of 80 items which can be detected by ImageAI, including car and truck.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222