|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Example: Logging Tables with Images |
| 4 | +
|
| 5 | +This example demonstrates the capability to include trackio.Image objects |
| 6 | +in trackio.Table columns. The images will be displayed as thumbnails in the |
| 7 | +dashboard with captions as alt text. |
| 8 | +
|
| 9 | +Run with: python examples/table/table-with-images.py |
| 10 | +""" |
| 11 | + |
| 12 | +import random |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +import pandas as pd |
| 16 | + |
| 17 | +import trackio |
| 18 | + |
| 19 | + |
| 20 | +def create_sample_images(): |
| 21 | + """Create some sample images for demonstration.""" |
| 22 | + images = [] |
| 23 | + |
| 24 | + red_square = np.full((100, 100, 3), [255, 0, 0], dtype=np.uint8) |
| 25 | + images.append(trackio.Image(red_square, caption="Red Square")) |
| 26 | + |
| 27 | + blue_data = np.zeros((100, 100, 3), dtype=np.uint8) |
| 28 | + center = 50 |
| 29 | + radius = 40 |
| 30 | + y, x = np.ogrid[:100, :100] |
| 31 | + mask = (x - center) ** 2 + (y - center) ** 2 <= radius**2 |
| 32 | + blue_data[mask] = [0, 0, 255] |
| 33 | + images.append(trackio.Image(blue_data, caption="Blue Circle")) |
| 34 | + |
| 35 | + gradient = np.zeros((100, 100, 3), dtype=np.uint8) |
| 36 | + for i in range(100): |
| 37 | + gradient[i, :, 1] = int(255 * i / 100) |
| 38 | + images.append(trackio.Image(gradient, caption="Green Gradient")) |
| 39 | + |
| 40 | + checkerboard = np.zeros((100, 100, 3), dtype=np.uint8) |
| 41 | + for i in range(0, 100, 20): |
| 42 | + for j in range(0, 100, 20): |
| 43 | + if (i // 20 + j // 20) % 2 == 0: |
| 44 | + checkerboard[i : i + 20, j : j + 20] = [255, 255, 255] |
| 45 | + images.append(trackio.Image(checkerboard, caption="Checkerboard")) |
| 46 | + |
| 47 | + return images |
| 48 | + |
| 49 | + |
| 50 | +def main(): |
| 51 | + trackio.init( |
| 52 | + project=f"table-with-images-demo-{random.randint(0, 1000000)}", |
| 53 | + name="sample-run", |
| 54 | + ) |
| 55 | + images = create_sample_images() |
| 56 | + |
| 57 | + data = { |
| 58 | + "experiment_id": [1, 2, 3, 4], |
| 59 | + "model_type": ["CNN", "ResNet", "VGG", "Custom"], |
| 60 | + "accuracy": [0.85, 0.92, 0.88, 0.95], |
| 61 | + "loss": [0.15, 0.08, 0.12, 0.05], |
| 62 | + "sample_output": images, |
| 63 | + "notes": [ |
| 64 | + "Basic convolutional model", |
| 65 | + "Deep residual network", |
| 66 | + "Very deep network", |
| 67 | + "Custom architecture", |
| 68 | + ], |
| 69 | + } |
| 70 | + |
| 71 | + df = pd.DataFrame(data) |
| 72 | + table = trackio.Table(dataframe=df) |
| 73 | + |
| 74 | + trackio.log({"experiment_results": table}) |
| 75 | + |
| 76 | + for step in range(10): |
| 77 | + trackio.log( |
| 78 | + { |
| 79 | + "training_loss": 1.0 * np.exp(-step * 0.1) + 0.1, |
| 80 | + "validation_accuracy": 0.5 + 0.4 * (1 - np.exp(-step * 0.15)), |
| 81 | + "learning_rate": 0.001 * (0.95**step), |
| 82 | + }, |
| 83 | + step=step, |
| 84 | + ) |
| 85 | + |
| 86 | + mixed_data = { |
| 87 | + "test_id": [1, 2, 3, 4, 5], |
| 88 | + "test_type": ["visual", "numerical", "visual", "numerical", "visual"], |
| 89 | + "result_image": [images[0], None, images[1], None, images[2]], |
| 90 | + "score": [95.5, 87.2, 91.8, 89.1, 93.4], |
| 91 | + "passed": [True, True, True, False, True], |
| 92 | + } |
| 93 | + |
| 94 | + mixed_df = pd.DataFrame(mixed_data) |
| 95 | + mixed_table = trackio.Table(dataframe=mixed_df) |
| 96 | + trackio.log({"mixed_test_results": mixed_table}) |
| 97 | + |
| 98 | + trackio.finish() |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + main() |
0 commit comments