TuringsSolutions commited on
Commit
575f7ee
1 Parent(s): bc7a8d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -24,7 +24,7 @@ class SwarmAgent:
24
  class SwarmNeuralNetwork:
25
  def __init__(self, num_agents, image_shape, target_image_path):
26
  self.image_shape = image_shape
27
- self.resized_shape = (256, 256, 3) # High resolution
28
  self.agents = [SwarmAgent(self.random_position(), self.random_velocity()) for _ in range(num_agents)]
29
  self.target_image = self.load_target_image(target_image_path)
30
  self.generated_image = np.random.randn(*image_shape) # Start with noise
@@ -127,7 +127,7 @@ class SwarmNeuralNetwork:
127
  mse = np.mean(((self.generated_image * 2 - 1) - self.target_image)**2)
128
  logging.info(f"Epoch {epoch}, MSE: {mse}")
129
 
130
- if epoch % 5 == 0:
131
  print(f"Epoch {epoch}, MSE: {mse}")
132
  self.display_image(self.generated_image, title=f'Epoch {epoch}')
133
  self.current_epoch += 1
@@ -161,7 +161,7 @@ class SwarmNeuralNetwork:
161
  self.current_epoch = model_state['current_epoch']
162
 
163
  @spaces.GPU(duration=120)
164
- def generate_new_image(self, num_steps=500): # Optimized number of steps
165
  for agent in self.agents:
166
  agent.position = np.random.randn(*self.image_shape)
167
 
@@ -184,7 +184,7 @@ class SwarmNeuralNetwork:
184
 
185
  # Gradio Interface
186
  def train_snn(image_path, num_agents, epochs, arm_position, leg_position, brightness, contrast, color):
187
- snn = SwarmNeuralNetwork(num_agents=num_agents, image_shape=(256, 256, 3), target_image_path=image_path) # High resolution
188
 
189
  # Apply user-specified adjustments to the target image
190
  image = Image.open(image_path)
@@ -203,7 +203,7 @@ def train_snn(image_path, num_agents, epochs, arm_position, leg_position, bright
203
  return generated_image
204
 
205
  def generate_new_image():
206
- snn = SwarmNeuralNetwork(num_agents=2000, image_shape=(256, 256, 3), target_image_path=None) # High resolution and optimized number of agents
207
  snn.load_model('snn_model.npy')
208
  new_image = snn.generate_new_image()
209
  return new_image
@@ -212,8 +212,8 @@ interface = gr.Interface(
212
  fn=train_snn,
213
  inputs=[
214
  gr.Image(type="filepath", label="Upload Target Image"),
215
- gr.Slider(minimum=500, maximum=2000, value=1000, label="Number of Agents"), # Adjusted range for number of agents
216
- gr.Slider(minimum=10, maximum=100, value=50, label="Number of Epochs"), # Adjusted range for number of epochs
217
  gr.Slider(minimum=-100, maximum=100, value=0, label="Arm Position"),
218
  gr.Slider(minimum=-100, maximum=100, value=0, label="Leg Position"),
219
  gr.Slider(minimum=0.5, maximum=2.0, value=1.0, label="Brightness"),
 
24
  class SwarmNeuralNetwork:
25
  def __init__(self, num_agents, image_shape, target_image_path):
26
  self.image_shape = image_shape
27
+ self.resized_shape = (128, 128, 3) # Reduced resolution
28
  self.agents = [SwarmAgent(self.random_position(), self.random_velocity()) for _ in range(num_agents)]
29
  self.target_image = self.load_target_image(target_image_path)
30
  self.generated_image = np.random.randn(*image_shape) # Start with noise
 
127
  mse = np.mean(((self.generated_image * 2 - 1) - self.target_image)**2)
128
  logging.info(f"Epoch {epoch}, MSE: {mse}")
129
 
130
+ if epoch % 2 == 0: # Display more frequently for faster feedback
131
  print(f"Epoch {epoch}, MSE: {mse}")
132
  self.display_image(self.generated_image, title=f'Epoch {epoch}')
133
  self.current_epoch += 1
 
161
  self.current_epoch = model_state['current_epoch']
162
 
163
  @spaces.GPU(duration=120)
164
+ def generate_new_image(self, num_steps=200): # Reduced number of steps
165
  for agent in self.agents:
166
  agent.position = np.random.randn(*self.image_shape)
167
 
 
184
 
185
  # Gradio Interface
186
  def train_snn(image_path, num_agents, epochs, arm_position, leg_position, brightness, contrast, color):
187
+ snn = SwarmNeuralNetwork(num_agents=num_agents, image_shape=(128, 128, 3), target_image_path=image_path) # Reduced resolution
188
 
189
  # Apply user-specified adjustments to the target image
190
  image = Image.open(image_path)
 
203
  return generated_image
204
 
205
  def generate_new_image():
206
+ snn = SwarmNeuralNetwork(num_agents=1000, image_shape=(128, 128, 3), target_image_path=None) # Reduced number of agents
207
  snn.load_model('snn_model.npy')
208
  new_image = snn.generate_new_image()
209
  return new_image
 
212
  fn=train_snn,
213
  inputs=[
214
  gr.Image(type="filepath", label="Upload Target Image"),
215
+ gr.Slider(minimum=100, maximum=1000, value=500, label="Number of Agents"), # Further reduced range for number of agents
216
+ gr.Slider(minimum=5, maximum=20, value=10, label="Number of Epochs"), # Further reduced range for number of epochs
217
  gr.Slider(minimum=-100, maximum=100, value=0, label="Arm Position"),
218
  gr.Slider(minimum=-100, maximum=100, value=0, label="Leg Position"),
219
  gr.Slider(minimum=0.5, maximum=2.0, value=1.0, label="Brightness"),