|
import os |
|
import gradio as gr |
|
import numpy as np |
|
from PIL import Image |
|
from tqdm import tqdm |
|
import matplotlib.pyplot as plt |
|
|
|
|
|
os.environ['CUDA_VISIBLE_DEVICES'] = '' |
|
|
|
|
|
DIM = 1000 |
|
|
|
def pixel_to_hypervector(pixel): |
|
"""Convert a pixel intensity to a high-dimensional binary hypervector.""" |
|
np.random.seed(int(pixel)) |
|
return np.random.choice([1, -1], size=(DIM,)) |
|
|
|
def image_to_hdc(image): |
|
"""Encode the entire image into hypervectors (by pixel intensity).""" |
|
return np.array([pixel_to_hypervector(p) for p in image.flatten()]) |
|
|
|
def hdc_to_image(hdc_vectors, shape): |
|
"""Decode hypervectors back into an image.""" |
|
decoded_pixels = np.mean(hdc_vectors, axis=1) |
|
decoded_pixels = np.clip((decoded_pixels + 1) / 2 * 255, 0, 255) |
|
return decoded_pixels.reshape(shape).astype(np.uint8) |
|
|
|
class SwarmAgent: |
|
def __init__(self, position, velocity): |
|
self.position = position |
|
self.velocity = velocity |
|
|
|
class SwarmNeuralNetwork: |
|
def __init__(self, num_agents, image_shape, target_image_path): |
|
self.image_shape = image_shape |
|
self.agents = [SwarmAgent(self.random_position(), self.random_velocity()) for _ in range(num_agents)] |
|
self.target_image = self.load_target_image(target_image_path) |
|
|
|
def random_position(self): |
|
return np.random.randn(*self.image_shape) |
|
|
|
def random_velocity(self): |
|
return np.random.randn(*self.image_shape) * 0.01 |
|
|
|
def load_target_image(self, img_path): |
|
img = Image.open(img_path).convert('RGB').resize((self.image_shape[1], self.image_shape[0])) |
|
return np.array(img) / 127.5 - 1 |
|
|
|
def update_agents(self, timestep): |
|
for agent in self.agents: |
|
|
|
agent_hdc = image_to_hdc(agent.position) |
|
target_hdc = image_to_hdc(self.target_image) |
|
|
|
|
|
similarity = np.mean(agent_hdc * target_hdc, axis=1) |
|
attention = similarity / np.sum(similarity) |
|
|
|
|
|
noise = np.random.randn(*self.image_shape) * 0.1 |
|
agent.position += attention.reshape(self.image_shape) * noise |
|
|
|
|
|
agent.position = np.clip(agent.position, -1, 1) |
|
|
|
def generate_image(self): |
|
generated_image = np.mean([agent.position for agent in self.agents], axis=0) |
|
return (generated_image + 1) / 2 |
|
|
|
def train(self, epochs): |
|
for epoch in tqdm(range(epochs), desc="Training"): |
|
self.update_agents(epoch) |
|
|
|
return self.generate_image() |
|
|
|
|
|
def train_snn(image_path, num_agents, epochs): |
|
snn = SwarmNeuralNetwork(num_agents=num_agents, image_shape=(128, 128, 3), target_image_path=image_path) |
|
generated_image = snn.train(epochs=epochs) |
|
return (generated_image * 255).astype(np.uint8) |
|
|
|
interface = gr.Interface( |
|
fn=train_snn, |
|
inputs=[ |
|
gr.Image(type="filepath", label="Upload Target Image"), |
|
gr.Slider(minimum=100, maximum=1000, value=500, label="Number of Agents"), |
|
gr.Slider(minimum=5, maximum=20, value=10, label="Number of Epochs") |
|
], |
|
outputs=gr.Image(type="numpy", label="Generated Image"), |
|
title="HDC Swarm Neural Network Image Generation" |
|
) |
|
|
|
interface.launch() |