{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[2mUsing Python 3.12.10 environment at: /Users/andrew/Developer/ML/whale/.venv\u001b[0m\n", "\u001b[2mAudited \u001b[1m3 packages\u001b[0m \u001b[2min 63ms\u001b[0m\u001b[0m\n" ] } ], "source": [ "!uv pip install cairosvg torch colour-science" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Gradient Shapes Dataset\n", "\n", "Dataset consisting of:\n", "- Shapes: circles and rounded rectangles\n", "- Positioned at various coordinates\n", "- **Gradient fills**: linear (top-to-bottom, left-to-right) or radial (from center)\n", "- 2 or 3 color stops per gradient\n", "- Sometimes gradient is applied to stroke instead of fill\n", "\n", "Output:\n", "- text prompt\n", "- png images of the shapes\n", "- svg text for the shapes" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/andrew/Developer/ML/whale/.venv/lib/python3.12/site-packages/colour/utilities/verbose.py:322: ColourUsageWarning: \"Matplotlib\" related API features are not available: \"No module named 'matplotlib'\".\n", "See the installation guide for more information: https://www.colour-science.org/installation-guide/\n", " warn(*args, **kwargs) # noqa: B028\n", "/Users/andrew/Developer/ML/whale/.venv/lib/python3.12/site-packages/colour/utilities/verbose.py:322: ColourUsageWarning: The \"colour.RGB_to_XYZ\" definition signature has changed with \"Colour 0.4.3\". The used call arguments are deprecated, please refer to the documentation for more information about the new signature.\n", " warn(*args, **kwargs) # noqa: B028\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Failures in 1000 attempts: 0\n", "Contrast ratio: 2.09\n" ] }, { "data": { "image/svg+xml": [ "" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import torch\n", "import colour\n", "from IPython.display import SVG\n", "\n", "def uniform(low=0, high=1):\n", " return torch.rand(1).item() * (high - low) + low\n", "\n", "\n", "def random_color():\n", " r = uniform(0.0, 1.0)\n", " g = uniform(0.0, 1.0)\n", " b = uniform(0.0, 1.0)\n", " return r, g, b\n", "\n", "def hex(r: float, g: float, b: float):\n", " return f\"#{int(r*255):02x}{int(g*255):02x}{int(b*255):02x}\"\n", "\n", "ColorType = tuple[float, float, float]\n", "\n", "def contrast_ratio(bg: ColorType, color: ColorType):\n", " r1, g1, b1 = bg\n", " r2, g2, b2 = color\n", " # Convert to CIE XYZ\n", " _, bg_lum, _ = colour.RGB_to_XYZ([r1, g1, b1], colour.models.RGB_COLOURSPACES['sRGB'].whitepoint, colour.models.RGB_COLOURSPACES['sRGB'].whitepoint, colour.models.RGB_COLOURSPACES['sRGB'].matrix_RGB_to_XYZ)\n", " _, fg_lum, _ = colour.RGB_to_XYZ([r2, g2, b2], colour.models.RGB_COLOURSPACES['sRGB'].whitepoint, colour.models.RGB_COLOURSPACES['sRGB'].whitepoint, colour.models.RGB_COLOURSPACES['sRGB'].matrix_RGB_to_XYZ)\n", " # Relative luminance is the Y component in XYZ\n", " L1, L2 = max(bg_lum, fg_lum), min(bg_lum, fg_lum)\n", " eps = 1e-6\n", " return (L1 + eps) / (L2 + eps)\n", "\n", "def contrasting_color(background: ColorType, threshold=2.0, max_attempts=100):\n", " \"\"\"\n", " Generate a random color with contrast ratio above the threshold with the given background.\n", " The threshold has been tuned so that it doesn't fail for random colors, if you have fixed colors you can increase the threshold\n", " \"\"\"\n", " for _ in range(max_attempts):\n", " color = random_color()\n", " ratio = contrast_ratio(background, color)\n", " if ratio >= threshold:\n", " return color, ratio\n", " raise ValueError(f'No color found with contrast ratio >= {threshold} for background {background} in {max_attempts} attempts')\n", "\n", "def check_generate_contrast(tests=1000, max_attempts=100):\n", " failures_per_1000 = 0\n", " for _ in range(tests):\n", " try:\n", " contrasting_color(random_color(), max_attempts=max_attempts)\n", " except ValueError:\n", " failures_per_1000 += 1\n", " return failures_per_1000\n", "\n", "print(f\"Failures in 1000 attempts: {check_generate_contrast(tests=1000, max_attempts=100)}\")\n", "\n", "test_color = random_color()\n", "test_bg_color, ratio = contrasting_color(test_color)\n", "print(f'Contrast ratio: {ratio:.2f}')\n", "SVG(f'')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " 0%| | 0/10000 [00:00\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "circle\n", "#960459\n", "linear-horizontal\n", "3\n", "False\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] }, { "data": { "image/svg+xml": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from typing import Any\n", "from tqdm import tqdm\n", "import uuid\n", "\n", "shape_types = [\n", " (0.5, 'circle'),\n", " (0.5, 'rounded-rect'),\n", "]\n", "\n", "gradient_types = [\n", " (1.0, 'linear-vertical'), # top to bottom\n", " (1.0, 'linear-horizontal'), # left to right\n", " (0.5, 'radial'), # from center\n", "]\n", "\n", "backgrounds = [\n", " (1.0, ('black', (0, 0, 0))),\n", " (0.5, ('#000000', (0, 0, 0))),\n", " (1.0, ('white', (1.0, 1.0, 1.0))),\n", " (0.5, ('#ffffff', (1.0, 1.0, 1.0))),\n", " (0.5, ('gray', (0.5, 0.5, 0.5))),\n", " (0.5, (\"RANDOM\", None)),\n", "]\n", "\n", "\n", "def sample(options: list, value: float) -> Any:\n", " \"\"\" Takes a random sample from a list of (weight, option) tuples\"\"\"\n", " total_weight = sum(weight for weight, _ in options)\n", " options_normalized = [(weight / total_weight, option) for weight, option in options]\n", " v = value\n", " for weight, option in options_normalized:\n", " if v < weight:\n", " return option\n", " v -= weight\n", " raise ValueError(f\"Value {v} is out of range for options {options_normalized}\")\n", "\n", "\n", "def generate_gradient_colors(bg_color: ColorType, num_stops: int) -> list[ColorType]:\n", " \"\"\"Generate contrasting colors for gradient stops\"\"\"\n", " colors = []\n", " for _ in range(num_stops):\n", " color, _ = contrasting_color(background=bg_color)\n", " colors.append(color)\n", " return colors\n", "\n", "\n", "def create_gradient_def(grad_id: str, grad_type: str, colors: list[ColorType]) -> tuple[str, str]:\n", " \"\"\"Create SVG gradient definition and return (defs_xml, gradient_description)\"\"\"\n", " stops = []\n", " num_stops = len(colors)\n", " for i, color in enumerate(colors):\n", " offset = i / (num_stops - 1) * 100\n", " stops.append(f'')\n", " stops_xml = \"\\n \".join(stops)\n", " \n", " color_desc = \" to \".join([hex(*c) for c in colors])\n", " \n", " if grad_type == 'linear-vertical':\n", " grad_xml = f'''\n", " {stops_xml}\n", " '''\n", " grad_desc = f\"a linear gradient from top to bottom ({color_desc})\"\n", " elif grad_type == 'linear-horizontal':\n", " grad_xml = f'''\n", " {stops_xml}\n", " '''\n", " grad_desc = f\"a linear gradient from left to right ({color_desc})\"\n", " elif grad_type == 'radial':\n", " grad_xml = f'''\n", " {stops_xml}\n", " '''\n", " grad_desc = f\"a radial gradient from center ({color_desc})\"\n", " else:\n", " raise ValueError(f\"Unknown gradient type: {grad_type}\")\n", " \n", " return grad_xml, grad_desc\n", "\n", "\n", "# iterate over the shapes and make random examples\n", "def gradient_shape_data(num_examples=1000, use_background=True): \n", " c_width = 512\n", " c_height = 512\n", " margin_x = c_width * 0.02\n", " margin_y = c_height * 0.02\n", " \n", " def wrap(svg: str, background: ColorType, defs: str = \"\"):\n", " background_str = f'\\n ' if background else ''\n", " defs_str = f'\\n \\n {defs}\\n ' if defs else ''\n", " return f'{defs_str}{background_str}\\n {svg}\\n'\n", " \n", " for _ in tqdm(range(num_examples)):\n", " shape_name = sample(shape_types, uniform())\n", " grad_type = sample(gradient_types, uniform())\n", " bg_name, bg_color = sample(backgrounds, uniform()) if use_background else (None, None)\n", " \n", " if bg_name == \"RANDOM\":\n", " bg_color = random_color()\n", " bg_name = hex(*bg_color)\n", " \n", " # Decide number of gradient stops (2 or 3)\n", " num_stops = 2 if uniform() < 0.5 else 3\n", " \n", " # Generate gradient colors\n", " grad_colors = generate_gradient_colors(bg_color, num_stops)\n", " \n", " # Decide if gradient is fill or stroke\n", " use_stroke_gradient = uniform() < 0.3 # 30% chance of stroke gradient\n", " \n", " grad_id = f\"grad_{uuid.uuid4().hex[:8]}\"\n", " grad_def, grad_desc = create_gradient_def(grad_id, grad_type, grad_colors)\n", " \n", " bg_desc = f'within a {c_width} x {c_height} box of color \"{bg_name}\"'\n", " \n", " if shape_name == 'circle':\n", " # Choose a radius > 0, and ensure the circle is strictly inside the bounds\n", " max_r = min(c_width - 2 * margin_x, c_height - 2 * margin_y) / 2\n", " r = uniform(30, max_r) # radius at least 30 for gradients to be visible\n", " r = round(r, 0)\n", " x = uniform(r + margin_x, c_width - r - margin_x)\n", " x = round(x, 0)\n", " y = uniform(r + margin_y, c_height - r - margin_y)\n", " y = round(y, 0)\n", " \n", " if use_stroke_gradient:\n", " stroke_width = round(uniform(2, 6), 1)\n", " # Fill with one of the gradient colors\n", " fill_color = grad_colors[0]\n", " desc = f'a circle with radius {r} at ({x}, {y}) with {grad_desc} stroke (width {stroke_width}) and {hex(*fill_color)} fill {bg_desc}'\n", " svg = f''\n", " else:\n", " desc = f'a circle with radius {r} at ({x}, {y}) filled with {grad_desc} {bg_desc}'\n", " svg = f''\n", " \n", " meta = (shape_name, hex(*bg_color), grad_type, num_stops, use_stroke_gradient)\n", " yield desc, wrap(svg, bg_color, grad_def), *meta\n", " \n", " elif shape_name == 'rounded-rect':\n", " # Constrain to leave at least 2% margin on any side\n", " max_w = c_width - 2 * margin_x\n", " max_h = c_height - 2 * margin_y\n", " min_w = max_w * 0.15 # slightly larger min for gradients to be visible\n", " min_h = max_h * 0.15\n", " w = uniform(min_w, max_w)\n", " w = round(w, 0)\n", " h = uniform(min_h, max_h)\n", " h = round(h, 0)\n", " x = uniform(margin_x, c_width - margin_x - w)\n", " x = round(x, 0)\n", " y = uniform(margin_y, c_height - margin_y - h)\n", " y = round(y, 0)\n", " corner_r = uniform(5, min(w, h) / 2)\n", " corner_r = round(corner_r, 1)\n", " \n", " if use_stroke_gradient:\n", " stroke_width = round(uniform(2, 6), 1)\n", " fill_color = grad_colors[0]\n", " desc = f'a rounded rectangle with width {w}, height {h}, and corner radius {corner_r} at ({x}, {y}) with {grad_desc} stroke (width {stroke_width}) and {hex(*fill_color)} fill {bg_desc}'\n", " svg = f''\n", " else:\n", " desc = f'a rounded rectangle with width {w}, height {h}, and corner radius {corner_r} at ({x}, {y}) filled with {grad_desc} {bg_desc}'\n", " svg = f''\n", " \n", " meta = (shape_name, hex(*bg_color), grad_type, num_stops, use_stroke_gradient)\n", " yield desc, wrap(svg, bg_color, grad_def), *meta\n", "\n", "\n", "examples = list(gradient_shape_data(10000, use_background=True))\n", "print(f'Generated {len(examples)} examples')\n", "\n", "print('\\n'.join(str(x) for x in examples[0]))\n", "SVG(examples[0][1])" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "

rounded-rect, linear-vertical, 3 stops, stroke

\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "

circle, linear-horizontal, 3 stops, stroke

\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "

circle, linear-vertical, 2 stops

\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "

circle, linear-vertical, 3 stops, stroke

\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "

rounded-rect, linear-horizontal, 3 stops

\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "

rounded-rect, linear-vertical, 2 stops

\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "

circle, radial, 2 stops

\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "

rounded-rect, linear-horizontal, 3 stops

\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "

rounded-rect, radial, 3 stops

\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "

rounded-rect, linear-vertical, 3 stops

\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "

rounded-rect, linear-vertical, 3 stops

\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "

circle, linear-horizontal, 3 stops

\n", "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Display a grid of sample images to inspect variety\n", "from IPython.display import display, HTML\n", "import random\n", "import re\n", "\n", "# Sample 12 random examples\n", "sample_indices = random.sample(range(len(examples)), 12)\n", "\n", "def scale_svg(svg: str, width: int, height: int) -> str:\n", " \"\"\"Scale only the outer element dimensions, preserving viewBox scaling\"\"\"\n", " # Only replace the width/height on the opening tag, not on child elements\n", " return re.sub(\n", " r'\n", " {scaled_svg}\n", "

{shape}, {grad_type}, {num_stops} stops{', stroke' if is_stroke else ''}

\n", " '''\n", "html += ''\n", "\n", "display(HTML(html))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 10000/10000 [01:38<00:00, 101.12it/s]\n" ] } ], "source": [ "import os\n", "from tqdm import tqdm\n", "import json\n", "os.environ[\"DYLD_LIBRARY_PATH\"] = \"/opt/homebrew/lib\"\n", "import cairosvg\n", "\n", "\n", "def generate_images(output_dir=\"./data/gradient-shapes\"):\n", " os.makedirs(output_dir, exist_ok=True)\n", "\n", " # delete all the old files\n", " for file in os.listdir(output_dir):\n", " os.remove(f\"{output_dir}/{file}\")\n", "\n", " def render_example(svg, i):\n", " file_name = f\"{i:06d}.png\"\n", " temp_path = f\"{output_dir}/{file_name}\"\n", " cairosvg.svg2png(svg, write_to=temp_path)\n", " return file_name\n", "\n", "\n", " with open(f\"{output_dir}/metadata.jsonl\", \"w\") as f:\n", " for i, example in tqdm(enumerate(examples), total=len(examples)):\n", " img = render_example(example[1], i)\n", " # Add metadata to metadata.jsonl\n", " f.write(json.dumps({\n", " \"desc\": example[0], \n", " \"svg\": example[1], \n", " \"file_name\": img, \n", " \"shape_name\": example[2], \n", " \"bg_color\": example[3],\n", " \"gradient_type\": example[4],\n", " \"num_stops\": example[5],\n", " \"is_stroke_gradient\": example[6]\n", " }) + \"\\n\")\n", " if i%100 == 0:\n", " f.flush()\n", "\n", "generate_images()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAIAAAB7GkOtAAAABmJLR0QA/wD/AP+gvaeTAAAUXklEQVR4nO3d6ZNd9Z3f8XPu0ou6tbSkVktoA4EkMBKYZcAxtrE9drARM+XxeBJ7qjIPk/9oKnmUSlI1qUxmnFBjJsYebIM9NgFjgYSQhHahrbu19L7fe/LAjMXIE7O4u3/nnu/rVSrqqnjAp1Xivu8599xz879sfCcDIJ5a6gEApCEAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAAEEJAEBQAgAQlAAABCUAA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rfemBrYdG3v/qv33j+zYVOSJfWxie6zF7vOXqyNTSQZUHmOAIAPMbNuw41de27svHts6/Z2o2tFjwBqiwvdw8Pdl650XbpSn5xK/aNXnAAAH1VRq01tHBwf2j4xuHVmYPPsuoEsr/2eAagXrebEWPetm93XR7tHRus3b+a+s2W1OAUEfFR5u732+vDa68O//m27Vp9Zv3F+3fr5vnXz/esWe/uWunpbXd2tZk9Rq+dZrd1oZFlWX1ysZa1ae6m5MFtfmOtamG3MTndNTTSnp5qTE82JsbztewvSEADgE6q1W/23RvtvjaYewifkdtAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFACABCUAAAEJQAAQQkAQFD/D3vK1loB+yj/AAAAAElFTkSuQmCC", "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import Image\n", "cairosvg.svg2png(examples[0][1], write_to=\"./output_gradient.png\")\n", "Image(filename=\"./output_gradient.png\")" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " \n", "

rounded-rect, linear-horizontal, 2 stops

\n", "
\n", " \n", "

rounded-rect, linear-vertical, 3 stops

\n", "
\n", " \n", "

circle, linear-horizontal, 3 stops

\n", "
\n", " \n", "

rounded-rect, linear-horizontal, 2 stops

\n", "
\n", " \n", "

circle, linear-vertical, 2 stops

\n", "
\n", " \n", "

circle, linear-vertical, 3 stops

\n", "
\n", " \n", "

circle, linear-vertical, 3 stops

\n", "
\n", " \n", "

rounded-rect, radial, 3 stops

\n", "
\n", " \n", "

circle, linear-vertical, 2 stops

\n", "
\n", " \n", "

rounded-rect, linear-vertical, 2 stops

\n", "
\n", " \n", "

rounded-rect, linear-horizontal, 3 stops, stroke

\n", "
\n", " \n", "

circle, linear-vertical, 3 stops, stroke

\n", "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Display a grid of rendered PNG samples\n", "from IPython.display import display, HTML\n", "import base64\n", "import random\n", "\n", "# Sample 12 random examples and render them\n", "sample_indices = random.sample(range(len(examples)), 12)\n", "\n", "html = '
'\n", "for idx in sample_indices:\n", " desc, svg, shape, bg, grad_type, num_stops, is_stroke = examples[idx]\n", " # Render to PNG in memory\n", " png_data = cairosvg.svg2png(svg)\n", " b64 = base64.b64encode(png_data).decode('utf-8')\n", " html += f'''
\n", " \n", "

{shape}, {grad_type}, {num_stops} stops{', stroke' if is_stroke else ''}

\n", "
'''\n", "html += '
'\n", "\n", "display(HTML(html))" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "446bab50dccc477d8e1858d6ec15c132", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Resolving data files: 0%| | 0/10001 [00:00