File size: 3,594 Bytes
bfd1c35 ae98e87 bfd1c35 ae98e87 bfd1c35 ae98e87 bfd1c35 ae98e87 bfd1c35 ae98e87 bfd1c35 |
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import os
import gradio as gr
import numpy as np
from PIL import Image
from tqdm import tqdm
import matplotlib.pyplot as plt
# Disable GPU usage by default
os.environ['CUDA_VISIBLE_DEVICES'] = ''
# HDC Encoding and Decoding Functions
DIM = 1000 # Hypervector dimensionality
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) # Aggregate hypervector values
decoded_pixels = np.clip((decoded_pixels + 1) / 2 * 255, 0, 255) # Rescale to [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:
# Convert agent's position and target image into HDC space
agent_hdc = image_to_hdc(agent.position)
target_hdc = image_to_hdc(self.target_image)
# Compute similarity between the agent's position and the target image
similarity = np.mean(agent_hdc * target_hdc, axis=1) # Cosine-like similarity
attention = similarity / np.sum(similarity)
# Adjust the agent's position based on HDC-guided noise reduction
noise = np.random.randn(*self.image_shape) * 0.1
agent.position += attention.reshape(self.image_shape) * noise
# Clip values to ensure valid range
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()
# Gradio Interface
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() |