Visual AI Python Examples¶
Sample Images¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #! /usr/bin/env python3
#
# Copyright 2021 DataRobot, Inc. and its affiliates.
#
# All rights reserved.
#
# DataRobot, Inc.
#
# This is proprietary source code of DataRobot, Inc. and its
# affiliates.
#
# Released under the terms of DataRobot Tool and Utility Agreement.
"""Show sample images for a project.
The following will open a project, get a list of sample images, and
then display a few images to the GUI.
The parameters may be adjusted to use your project name, feature name, and
the number of images to display.
"""
import io
import PIL.Image
from datarobot.models import Project
from datarobot.models.visualai import SampleImage
def display_images(project_name, feature_name, max_images):
project = Project.list(search_params={"project_name": project_name})[0]
for sample in SampleImage.list(project.id, feature_name)[:max_images]:
with io.BytesIO(sample.image.image_bytes) as bio, PIL.Image.open(bio) as img:
img.show()
if __name__ == "__main__":
project_name = "dataset_2k.zip"
feature_name = "image"
max_images = 2
display_images(project_name, feature_name, max_images)
|
Activation Maps¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #! /usr/bin/env python3
#
# Copyright 2021 DataRobot, Inc. and its affiliates.
#
# All rights reserved.
#
# DataRobot, Inc.
#
# This is proprietary source code of DataRobot, Inc. and its
# affiliates.
#
# Released under the terms of DataRobot Tool and Utility Agreement.
"""Show a small sample of images and associated activation maps images.
The following will open a project, get the first model id where the feature
name matches, and then get a list of the activation maps. Then it will
display a few of the images and the associated images with overlay in the
GUI.
The parameters may be adjusted to use your project name, feature name, and
the number of images to display.
"""
import io
import PIL.Image
from datarobot.models import Project
from datarobot.models.visualai import ImageActivationMap
def display_images(project_name, feature_name, max_images):
project = Project.list(search_params={"project_name": project_name})[0]
model_id = next(
mid for mid, name in ImageActivationMap.models(project.id) if name == feature_name
)
for amap in ImageActivationMap.list(project.id, model_id, feature_name)[:max_images]:
with io.BytesIO(amap.image.image_bytes) as bio, PIL.Image.open(bio) as img:
img.show()
with io.BytesIO(amap.overlay_image.image_bytes) as bio, PIL.Image.open(bio) as img:
img.show()
if __name__ == "__main__":
project_name = "dataset_2k.zip"
feature_name = "image"
max_images = 2
display_images(project_name, feature_name, max_images)
|
Image Embeddings¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #! /usr/bin/env python3
#
# Copyright 2021 DataRobot, Inc. and its affiliates.
#
# All rights reserved.
#
# DataRobot, Inc.
#
# This is proprietary source code of DataRobot, Inc. and its
# affiliates.
#
# Released under the terms of DataRobot Tool and Utility Agreement.
"""Show image embedding vectors.
The following will open a project, get the first model id where the feature
name matches, and then print out the image id and the embedding vector.
"""
from datarobot.models import Project
from datarobot.models.visualai import ImageEmbedding
def print_vectors(project_name, feature_name):
project = Project.list(search_params={"project_name": project_name})[0]
model_id = next(mid for mid, name in ImageEmbedding.models(project.id) if name == feature_name)
for embed in ImageEmbedding.list(project.id, model_id, feature_name):
print("{0} [{1:1.6f}, {2:1.6f}]".format(embed.image.id, embed.position_x, embed.position_y))
if __name__ == "__main__":
project_name = "dataset_2k.zip"
feature_name = "image"
print_vectors(project_name, feature_name)
|