pythontensorflowkerash5pyimageai

Finding the intent of a H5 file


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")

Solution

  • 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.