TuringsSolutions commited on
Commit
bfd1c35
·
verified ·
1 Parent(s): c97126c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import spaces
3
+ import gradio as gr
4
+ import numpy as np
5
+ import tensorflow as tf
6
+ from keras.applications.mobilenet_v2 import MobileNetV2, preprocess_input
7
+ from keras.models import Model
8
+ import matplotlib.pyplot as plt
9
+ import logging
10
+ from skimage.transform import resize
11
+ from PIL import Image, ImageEnhance, ImageFilter
12
+ from tqdm import tqdm
13
+
14
+ # Disable GPU usage by default
15
+ os.environ['CUDA_VISIBLE_DEVICES'] = ''
16
+
17
+ # HDC Encoding and Decoding Functions
18
+ DIM = 1000 # Hypervector dimensionality
19
+
20
+ def pixel_to_hypervector(pixel):
21
+ """Convert a pixel intensity to a high-dimensional binary hypervector."""
22
+ np.random.seed(int(pixel))
23
+ return np.random.choice([1, -1], size=(DIM,))
24
+
25
+ def image_to_hdc(image):
26
+ """Encode the entire image into hypervectors (by pixel intensity)."""
27
+ return np.array([pixel_to_hypervector(p) for p in image.flatten()])
28
+
29
+ def hdc_to_image(hdc_vectors, shape):
30
+ """Decode hypervectors back into an image."""
31
+ decoded_pixels = np.mean(hdc_vectors, axis=1) # Aggregate hypervector values
32
+ decoded_pixels = np.clip((decoded_pixels + 1) / 2 * 255, 0, 255) # Rescale to [0, 255]
33
+ return decoded_pixels.reshape(shape).astype(np.uint8)
34
+
35
+ class SwarmAgent:
36
+ def __init__(self, position, velocity):
37
+ self.position = position
38
+ self.velocity = velocity
39
+
40
+ class SwarmNeuralNetwork:
41
+ def __init__(self, num_agents, image_shape, target_image_path):
42
+ self.image_shape = image_shape
43
+ self.agents = [SwarmAgent(self.random_position(), self.random_velocity()) for _ in range(num_agents)]
44
+ self.target_image = self.load_target_image(target_image_path)
45
+ self.mobilenet = self.load_mobilenet_model()
46
+
47
+ def random_position(self):
48
+ return np.random.randn(*self.image_shape)
49
+
50
+ def random_velocity(self):
51
+ return np.random.randn(*self.image_shape) * 0.01
52
+
53
+ def load_target_image(self, img_path):
54
+ img = Image.open(img_path).resize((self.image_shape[1], self.image_shape[0]))
55
+ return np.array(img) / 127.5 - 1
56
+
57
+ def load_mobilenet_model(self):
58
+ mobilenet = MobileNetV2(weights='imagenet', include_top=False, input_shape=(128, 128, 3))
59
+ return Model(inputs=mobilenet.input, outputs=mobilenet.get_layer('block_13_expand_relu').output)
60
+
61
+ def update_agents(self, timestep):
62
+ for agent in self.agents:
63
+ # Convert agent's position and target image into HDC space
64
+ agent_hdc = image_to_hdc(agent.position)
65
+ target_hdc = image_to_hdc(self.target_image)
66
+
67
+ # Compute similarity between the agent's position and the target image
68
+ similarity = np.mean(agent_hdc * target_hdc, axis=1) # Cosine-like similarity
69
+ attention = similarity / np.sum(similarity)
70
+
71
+ # Adjust the agent's position based on HDC-guided noise reduction
72
+ noise = np.random.randn(*self.image_shape) * 0.1
73
+ agent.position += attention.reshape(self.image_shape) * noise
74
+
75
+ # Clip values to ensure valid range
76
+ agent.position = np.clip(agent.position, -1, 1)
77
+
78
+ def generate_image(self):
79
+ generated_image = np.mean([agent.position for agent in self.agents], axis=0)
80
+ return (generated_image + 1) / 2
81
+
82
+ def train(self, epochs):
83
+ for epoch in tqdm(range(epochs), desc="Training"):
84
+ self.update_agents(epoch)
85
+ generated_image = self.generate_image()
86
+
87
+ # Display the generated image
88
+ self.display_image(generated_image, title=f'Epoch {epoch}')
89
+
90
+ def display_image(self, image, title=''):
91
+ plt.imshow(image)
92
+ plt.title(title)
93
+ plt.axis('off')
94
+ plt.show()
95
+
96
+ # Gradio Interface
97
+ def train_snn(image_path, num_agents, epochs):
98
+ snn = SwarmNeuralNetwork(num_agents=num_agents, image_shape=(128, 128, 3), target_image_path=image_path)
99
+ snn.train(epochs=epochs)
100
+ generated_image = snn.generate_image()
101
+ return generated_image
102
+
103
+ interface = gr.Interface(
104
+ fn=train_snn,
105
+ inputs=[
106
+ gr.Image(type="filepath", label="Upload Target Image"),
107
+ gr.Slider(minimum=100, maximum=1000, value=500, label="Number of Agents"),
108
+ gr.Slider(minimum=5, maximum=20, value=10, label="Number of Epochs")
109
+ ],
110
+ outputs=gr.Image(type="numpy", label="Generated Image"),
111
+ title="HDC Swarm Neural Network Image Generation"
112
+ )
113
+
114
+ interface.launch()