Steve Chan bio photo

Steve Chan

Artificial Intelligence,
Machine Learning,
Numerical Algorithms,
Numerical Methods,
Hyper-Heuristics,
Metaheuristics,
Data Analytics,
Information Science,
Decision Science

Twitter GitHub GitLab Website E-Mail

Experimentation Scripts for AD #15

So that reviewers and readers can reproduce the results for this IEEE paper, please find the "Experimentation Scripts for AD #15" in Python, Go (Golang), and Rust below (rather than in the paper):

Python Script for AD #15

(as referenced in Section IIIC. Experimentation)

Prospective Efficacy Value-added Proposition: Triangulated Evidence, Uncertain Detections, Multi-Pass, Visual-Attention, etc. Prompts


import openai
import base64
import json
from PIL import Image
import io

# ------------------------
# Configuration for AD #15
# ------------------------
openai.api_key = "PERTINENT_OPENAI_API_KEY"  # Pertinent API key
IMAGE_FILE = "AD15_input_image.png"
OUTPUT_JSON = "AD15_image_analysis_output.json"
ANNOTATED_IMAGE_FILE = "AD15_annotated_image.png"

# ---------------------------------------
# 1. Read and encode the image for AD #15
# ---------------------------------------
with open(IMAGE_FILE, "rb") as f:
	image_bytes = f.read()
image_base64 = base64.b64encode(image_bytes).decode("utf-8")

# ------------------------------------------------
# 2. AD #15 Reasoning and Value-Added Propositions
# ------------------------------------------------
prompt = """
Task:
Analyze the uploaded image and produce a simplified annotated version highlighting all detected objects, anomalies, and structural layouts.

Reasoning Instructions:
- Think step-by-step and generate multiple independent reasoning paths (self-consistency)
- Compare reasoning paths to produce final answer
- Explicitly analyze false positives and false negatives
- Define each, provide examples, assess likelihood and consequences
- Note assumptions and uncertainties
- Summarize conclusions based on the most consistent reasoning

Grid-based Visual Attention:
- Divide image into logical grid (e.g., 3x3 or 4x4)
- Detect shapes, objects, and patterns in each region
- Classify objects using engineering/contextual reasoning
- Identify anomalies
- Reconcile overlapping or duplicate detections

Multi-agent Analysis:
- Agent A: Visual Detection Specialist
- Agent B: Infrastructure / Engineering Analyst
- Agent C: Context Analyst
- Agent D: Dataset Verification Analyst
- Agent E: Anomaly & Risk Evaluator

Triangulated Evidence & Confidence Scoring:
- For each object, assign confidence: High (3/3), Medium (2/3), Low (1/3)

Internal Verification Checklist:
- All regions processed
- Ambiguous objects reviewed
- False positives/negatives analyzed
- Cross-region reconciliation
- Conclusions supported by triangulated evidence
- Anomalies highlighted
- Uncertainties and assumptions noted

Image Simplification & Overlay:
- Preserve essential shapes and layout
- Remove minor details
- Overlay objects with bright outlines
- Highlight anomalies in red
- Keep base image visible

Output Format:
1. Annotated simplified image (base64)
2. Object detection table: category, location, confidence, anomaly status
3. False positive analysis
4. False negative analysis
5. Assumptions and uncertainties
6. Final synthesis
"""

# -------------------------------
# 3. Initiate the OpenAI API call
# -------------------------------
response = openai.Image.create(
	model="gpt-5.0-mini",  # gpt-5.0-mini
	prompt=prompt,
	images=[image_base64],
	size="1024x1024",
	response_format="b64_json"
)

# ----------------------------------------------
# 4. Extract the AD #15 annotated image/metadata
# ----------------------------------------------
annotated_b64 = response['data'][0]['b64_json']
annotated_bytes = base64.b64decode(annotated_b64)

# Save annotated image
with open(ANNOTATED_IMAGE_FILE, "wb") as f:
	f.write(annotated_bytes)

# Optional: Display image
img = Image.open(io.BytesIO(annotated_bytes))
img.show()

# ----------------------------------------
# 5. Save the AD #15-related JSON metadata
# ----------------------------------------
analysis_output = {
	"annotated_image_file": ANNOTATED_IMAGE_FILE,
	"object_detection_table": response['data'][0].get('objects', []),
	"false_positive_analysis": response['data'][0].get('false_positives', []),
	"false_negative_analysis": response['data'][0].get('false_negatives', []),
	"assumptions_uncertainties": response['data'][0].get('assumptions', []),
	"final_synthesis": response['data'][0].get('final_synthesis', "")
}

with open(OUTPUT_JSON, "w") as f:
	json.dump(analysis_output, f, indent=2)

print(f"Annotated image saved as: {ANNOTATED_IMAGE_FILE}")
print(f"Analysis metadata saved as: {OUTPUT_JSON}")

 

Go (Golang) Script for AD #15

(as referenced in Section IIIC. Experimentation)

Prospective Efficacy Value-added Proposition: Triangulated Evidence, Uncertain Detections, Multi-Pass, Visual-Attention, etc. Prompts


package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
    "os"
)

type ChatMessage struct {
    Role    string `json:"role"`
    Content string `json:"content"`
}

type ChatRequest struct {
    Model    string `json:"model"`
    Messages []ChatMessage `json:"messages"`
}

type ChatResponseChoice struct {
    Message ChatMessage `json:"message"`
}

type ChatResponse struct {
    Choices []ChatResponseChoice `json:"choices"`
}

func generatePrompt(context string) string {
    return fmt.Sprintf(`
Task:
Analyze the uploaded image and produce a simplified annotated version highlighting all detected objects, anomalies, and structural layouts.

Context:
%s

Reasoning Instructions:
- Think carefully step-by-step.
- Analyze false positives and false negatives.
- Summarize conclusions based on the most consistent reasoning.

Grid-based Visual Attention:
- Divide the image into a grid and process each region.
- Detect objects, classify them, and identify anomalies.

Multi-agent Analysis:
- Agent A: Visual Detection Specialist
- Agent B: Infrastructure / Engineering Analyst
- Agent C: Context Analyst
- Agent D: Dataset Verification Analyst
- Agent E: Anomaly & Risk Evaluator

Triangulated Evidence & Confidence Scoring:
- Assign confidence: High=3/3, Medium=2/3, Low=1/3

Internal Verification Checklist:
- All regions processed
- False positives/negatives reviewed
- Conclusions supported by triangulated evidence

Image Simplification & Overlay:
- Preserve essential shapes
- Overlay detected objects
- Highlight anomalies in red

Output Format:
1. Annotated image with overlays
2. Object detection table
3. False positive analysis
4. False negative analysis
5. Assumptions and uncertainties
6. Final synthesis
`, context)
}

func callOpenAI(prompt string) (string, error) {
    apiKey := os.Getenv("PERTINENT_OPENAI_API_KEY")
    if apiKey == "" {
        return "", fmt.Errorf("PERTINENT_OPENAI_API_KEY environment variable not set")
    }

    requestBody := ChatRequest{
        Model: "gpt-5.0-mini",
        Messages: []ChatMessage{
            {Role: "system", Content: "Variant #15"},
            {Role: "user", Content: prompt},
        },
    }

    bodyBytes, err := json.Marshal(requestBody)
    if err != nil {
        return "", err
    }

    req, err := http.NewRequest("POST", "https://api.openai.com/v1/chat/completions", bytes.NewBuffer(bodyBytes))
    if err != nil {
        return "", err
    }

    req.Header.Set("Content-Type", "application/json")
    req.Header.Set("Authorization", "Bearer "+apiKey)

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        return "", err
    }
    defer resp.Body.Close()

    respBody, _ := ioutil.ReadAll(resp.Body)

    if resp.StatusCode != 200 {
        return "", fmt.Errorf("OpenAI API error: %s", string(respBody))
    }

    var chatResp ChatResponse
    if err := json.Unmarshal(respBody, &chatResp); err != nil {
        return "", err
    }

    if len(chatResp.Choices) > 0 {
        return chatResp.Choices[0].Message.Content, nil
    }

    return "", fmt.Errorf("No response from OpenAI API")
}

func main() {
    var context string
    fmt.Println("Enter a brief description of the image and analysis goal:")
    fmt.Scanln(&context)

    prompt := generatePrompt(context)

    response, err := callOpenAI(prompt)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    fmt.Println(\n=== AD #15 ===\n)
    fmt.Println(response)
}

 

Rust Script for AD #15

(as referenced in Section IIIC. Experimentation)

Prospective Efficacy Value-added Proposition: Triangulated Evidence, Uncertain Detections, Multi-Pass, Visual-Attention, etc. Prompts


use std::fs::File;
use std::io::{Read, Write};
use base64::{engine::general_purpose, Engine as _};

fn main() -> std::io::Result<()> {
    // ------------------------
    // Configuration for AD #15
    // ------------------------
    let image_path = "input_image.png";
    let output_prompt_file = "generated_prompt.txt";
    let grid_size = "3x3";
    let agents = "Agent A — Visual Detection Specialist, Agent B — Infrastructure/Engineering Analyst, Agent C — Context Analyst, Agent D — Dataset Verification Analyst, Agent E — Anomaly & Risk Evaluator";
    let custom_instructions = ""; 

    // ---------------------------------------
    // 1. Read and encode the image for AD #15
    // ---------------------------------------
    let mut file = File::open(image_path)?;
    let mut buffer = Vec::new();
    file.read_to_end(&mut buffer)?;
    let image_base64 = general_purpose::STANDARD.encode(&buffer);

    // ------------------------------------------------
    // 2. AD #15 Reasoning and Value-Added Propositions
    // ------------------------------------------------
    let prompt = format!(
r#"Task:
Analyze the uploaded image and produce a simplified annotated version highlighting all detected objects, anomalies, and structural layouts.

Reasoning Instructions:
Think carefully and reason step-by-step. Generate multiple independent reasoning paths (self-consistency) and compare them before producing the final answer. Explicitly analyze false positives and false negatives, define each, provide examples, assess likelihood and consequences, note assumptions and uncertainties, and summarize conclusions based on the most consistent reasoning.
{} 

Grid-based Visual Attention:
Divide the image into a logical grid ({}).
Sequentially process each region:
- Detect shapes, objects, and patterns
- Classify detected objects using engineering or contextual reasoning
- Identify anomalies
Reconcile overlapping or duplicate detections across regions.

Multi-agent Analysis:
{}

Triangulated Evidence & Confidence Scoring:
For each object, assign confidence based on:
- visual evidence
- contextual reasoning
- dataset or pattern agreement
Confidence Levels:
High = 3/3 sources, Medium = 2/3, Low = 1/3

Internal Verification Checklist:
- all regions processed and major structures considered
- ambiguous objects reviewed
- false positives checked
- false negatives checked
- cross-region reconciliation completed
- conclusions supported by triangulated evidence
- anomalies highlighted
- uncertainties and assumptions noted

Image Simplification & Overlay:
- Preserve essential shapes and layout
- Remove minor details
- Overlay detected objects with bright outlines
- Highlight anomalies in red
- Keep base image visible

Output Format:
1. Annotated simplified image with overlays
2. Object detection table: category, location, confidence, anomaly status
3. False positive analysis
4. False negative analysis
5. Assumptions and uncertainties
6. Final synthesis based on the most consistent reasoning

[Image Base64: {}]
"#,
        custom_instructions, grid_size, agents, image_base64
    );

    // ----------------------------------------
    // 3. Save the AD #15-related JSON metadata
    // ----------------------------------------
    let mut output_file = File::create(output_prompt_file)?;
    output_file.write_all(prompt.as_bytes())?;

    println!("Generated prompt saved to {}", output_prompt_file);

    Ok(())
}

 

   In brief, the Python script is useful for prototyping (but slower) while Go (Golang) has high-throughput advantages and Rust can be ultra-high performance (and is well suited for bulk processing).