Sign In

A Little Bit Model Forensic

0

Sep 22, 2024

(Updated: 4 months ago)

resource guide
A Little Bit Model Forensic

Foreword

Once we have learned that an image can be converted into a tensor, and that this tensor can be converted back into an image, the next logical step is to look in a model to see if there are readable images in the model.

Read Images from Model

The following Python script shows how the Tensors can be extracted from a model. As known one can try to convert these Tensors in images.

The images are of interest if they are square images. These square images are written to a subdirectory test. There one can take a look on the retrieved images.

#!/usr/bin/python3

# Import all required modules.
import os
from PIL import Image
from torchvision.transforms import transforms
from safetensors import safe_open

# Set the filename.
FN="model.safetensors"

# Create an empty tensor.
tensors = {}

# Define a transform to convert a Torch tensor into a PIL image.
transform = transforms.ToPILImage()

# Initialise the count variable.
count = 0

# Initialise height and width.
h = 0
w = 0

# Set the file exension.
EXT = ".jpeg"

# Set the directory.
DIR = "./test/"

# Create directory.
if not os.path.exists(DIR):
    os.makedirs(DIR)

# Read tensor by tensor from file.
with safe_open(FN, framework="pt", device=0) as f:
    for k in f.keys():
        # Get the value to the given key.
        tensors[k] = f.get_tensor(k)
        # Try to create a PIL image.
        try:
            # Increment the counter.
            count += 1
            # Create an image.
            pilimage = transform(tensors[k])
            # Create a new filename.
            fn = DIR + str(count) + ".jpeg"
            # Get width and height.
            w, h = pilimage.size
            # Check if it is a sqaure image.
            if w == h:
                # Save the file.
                pilimage.save(fn,"JPEG")
                # Print the dimensions.
                print(h, w)
        except ValueError as err:
            pass

The try and except block in the script makes sure that only Tensors are considered which can be converted in images. Last but not least the small script still needs improvement.

Conclusion

One can show, that Tensors in a model can be converted into images. Up to now I did not found images which are more than noise. This it what one should expected.

Finally

Have a nice day. Have fun. Be inspired!

Ressources

[1] https://github.com/zentrocdot/artificial-intelligence-tools/tree/main/python

0