nick-leland commited on
Commit
ef63822
·
1 Parent(s): 04d3a4d

Big grind session, still working on implimenting the model correctly

Browse files
Files changed (3) hide show
  1. app.py +69 -28
  2. bulk_bulge_generation.py +19 -2
  3. requirements.txt +2 -2
app.py CHANGED
@@ -3,7 +3,11 @@ import gradio as gr
3
  from PIL import Image
4
  from scipy import ndimage
5
  import matplotlib.pyplot as plt
6
- from bulk_bulge_generation import definitions
 
 
 
 
7
 
8
  def apply_vector_field_transform(image, func, radius, center=(0.5, 0.5), strength=1, edge_smoothness=0.1, center_smoothness=0.20):
9
  # 0.106 strength = .50
@@ -15,6 +19,15 @@ def apply_vector_field_transform(image, func, radius, center=(0.5, 0.5), strengt
15
  # Y Needs to be flipped
16
  center_y = int(center[1] * rows)
17
  center_x = int(center[0] * cols)
 
 
 
 
 
 
 
 
 
18
 
19
  pixel_radius = int(max_dim * radius)
20
 
@@ -34,6 +47,11 @@ def apply_vector_field_transform(image, func, radius, center=(0.5, 0.5), strengt
34
  # Creating a sigmoid function to apply to masks
35
  def sigmoid(x, center, steepness):
36
  return 1 / (1 + np.exp(-steepness * (x - center)))
 
 
 
 
 
37
 
38
  # Masking
39
  edge_mask = np.clip((radius - dist_from_center) / (radius * edge_smoothness), 0, 1)
@@ -77,6 +95,7 @@ def apply_vector_field_transform(image, func, radius, center=(0.5, 0.5), strengt
77
 
78
  return transformed_image, (gx, gy)
79
 
 
80
  def create_gradient_vector_field(gx, gy, image_shape, step=20, reverse=False):
81
  """
82
  Create a gradient vector field visualization with option to reverse direction.
@@ -123,7 +142,22 @@ def create_gradient_vector_field(gx, gy, image_shape, step=20, reverse=False):
123
 
124
  return vector_field
125
 
126
- def transform_image(image, func_choice, randomization_check, radius, center_x, center_y, strength, edge_smoothness, center_smoothness, reverse_gradient=True, spiral_frequency=1):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  I = np.asarray(Image.open(image))
128
 
129
  def pinch(x, y):
@@ -133,22 +167,7 @@ def transform_image(image, func_choice, randomization_check, radius, center_x, c
133
  return np.arctan2(y, x)
134
 
135
  def bulge(x, y):
136
- # Where does this make an array???:w
137
-
138
- print(x.shape)
139
- print(y.shape)
140
- # Mess with this number
141
- # num = 10
142
-
143
- # if x < num or y < num:
144
- # # This might not be correct
145
- # return 1
146
- # else:
147
- # r = -np.sqrt(x**2 + y**2)
148
  r = -np.sqrt(x**2 + y**2)
149
- print(r.shape)
150
- print(type(r))
151
- # return -1 / (r + 1)
152
  return r
153
 
154
  def spiral(x, y, frequency=1):
@@ -156,44 +175,66 @@ def transform_image(image, func_choice, randomization_check, radius, center_x, c
156
  theta = np.arctan2(y, x)
157
  return r * np.sin(theta - frequency * r)
158
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  if func_choice == "Pinch":
160
  func = pinch
161
  elif func_choice == "Spiral":
162
  func = shift
163
  elif func_choice == "Bulge":
164
  func = bulge
 
 
 
 
165
  elif func_choice == "Shift Up":
166
  func = lambda x, y: spiral(x, y, frequency=spiral_frequency)
167
 
168
- if randomization_check == True:
169
- rng = np.random.default_rng()
170
- radius, location, strength, edge_smoothness= definitions(rng)
171
- center_x = location[0]
172
- center_y = location[1]
173
-
174
 
175
  transformed, (gx, gy) = apply_vector_field_transform(I, func, radius, (center_x, center_y), strength, edge_smoothness, center_smoothness)
176
  vector_field = create_gradient_vector_field(gx, gy, I.shape[:2], reverse=reverse_gradient)
177
 
178
- return transformed, vector_field
 
 
 
 
 
 
 
 
 
 
179
 
180
  demo = gr.Interface(
181
  fn=transform_image,
182
  inputs=[
183
  gr.Image(type="filepath"),
184
- gr.Dropdown(["Pinch", "Spiral", "Shift Up", "Bulge"], value="Bulge", label="Function"),
185
  gr.Checkbox(label="Randomize inputs?"),
186
  gr.Slider(0, 0.5, value=0.25, label="Radius (as fraction of image size)"),
187
  gr.Slider(0, 1, value=0.5, label="Center X"),
188
  gr.Slider(0, 1, value=0.5, label="Center Y"),
189
  gr.Slider(0, 1, value=0.5, label="Strength"),
190
- gr.Slider(0, 1, value=0.5, label="Edge Smoothness"),
191
- gr.Slider(0, 0.5, value=0.1, label="Center Smoothness")
192
  # gr.Checkbox(label="Reverse Gradient Direction"),
193
  ],
194
  outputs=[
195
  gr.Image(label="Transformed Image"),
196
- gr.Image(label="Gradient Vector Field")
 
 
197
  ],
198
  title="Image Transformation Demo!",
199
  description="This is the baseline function that will be used to generate the database for a machine learning model I am working on called 'DistortionMl'! The goal of this model is to detect and then reverse image transformations that can be generated here! You can read more about the project at this repository link : https://github.com/nick-leland/DistortionML. The main function that I was working on is the 'Bulge' function, I can't really guarantee that the others work well (;"
 
3
  from PIL import Image
4
  from scipy import ndimage
5
  import matplotlib.pyplot as plt
6
+ from bulk_bulge_generation import definitions, smooth
7
+ from transformers import pipeline
8
+ import fastai
9
+ from fastcore.all import *
10
+ from fastai.vision.all import *
11
 
12
  def apply_vector_field_transform(image, func, radius, center=(0.5, 0.5), strength=1, edge_smoothness=0.1, center_smoothness=0.20):
13
  # 0.106 strength = .50
 
19
  # Y Needs to be flipped
20
  center_y = int(center[1] * rows)
21
  center_x = int(center[0] * cols)
22
+
23
+ # Inverts the Y axis (Numpy is 0 index at top of image)
24
+ center_y = abs(rows - center_y)
25
+
26
+ print()
27
+ print(rows, cols)
28
+ print("y =", center_y, "/", rows)
29
+ print("x =", center_x, "/", cols)
30
+ print()
31
 
32
  pixel_radius = int(max_dim * radius)
33
 
 
47
  # Creating a sigmoid function to apply to masks
48
  def sigmoid(x, center, steepness):
49
  return 1 / (1 + np.exp(-steepness * (x - center)))
50
+
51
+ print(radius)
52
+ print(strength)
53
+ print(edge_smoothness)
54
+ print(center_smoothness)
55
 
56
  # Masking
57
  edge_mask = np.clip((radius - dist_from_center) / (radius * edge_smoothness), 0, 1)
 
95
 
96
  return transformed_image, (gx, gy)
97
 
98
+
99
  def create_gradient_vector_field(gx, gy, image_shape, step=20, reverse=False):
100
  """
101
  Create a gradient vector field visualization with option to reverse direction.
 
142
 
143
  return vector_field
144
 
145
+
146
+ #############################
147
+ # MAIN FUNCTION HERE
148
+ #############################
149
+
150
+ # pipeline = pipeline(task="image-classification", model="nick-leland/distortionml")
151
+
152
+ # Version Check
153
+ print(f"NumPy version: {np.__version__}")
154
+ print(f"PyTorch version: {torch.__version__}")
155
+ print(f"FastAI version: {fastai.__version__}")
156
+
157
+ learn = load_learner('model.pkl')
158
+
159
+
160
+ def transform_image(image, func_choice, randomization_check, radius, center_x, center_y, strength, reverse_gradient=True, spiral_frequency=1):
161
  I = np.asarray(Image.open(image))
162
 
163
  def pinch(x, y):
 
167
  return np.arctan2(y, x)
168
 
169
  def bulge(x, y):
 
 
 
 
 
 
 
 
 
 
 
 
170
  r = -np.sqrt(x**2 + y**2)
 
 
 
171
  return r
172
 
173
  def spiral(x, y, frequency=1):
 
175
  theta = np.arctan2(y, x)
176
  return r * np.sin(theta - frequency * r)
177
 
178
+ rng = np.random.default_rng()
179
+ if randomization_check == True:
180
+ radius, location, strength, edge_smoothness= definitions(rng)
181
+ center_x = location[0]
182
+ center_y = location[1]
183
+
184
+ # Temporarily disabling and using these values.
185
+ # edge_smoothness = 0.25 * strength
186
+ # center_smoothness = 0.25 * strength
187
+ edge_smoothness, center_smoothness = smooth(rng, strength)
188
+
189
+
190
  if func_choice == "Pinch":
191
  func = pinch
192
  elif func_choice == "Spiral":
193
  func = shift
194
  elif func_choice == "Bulge":
195
  func = bulge
196
+ edge_smoothness = 0
197
+ center_smoothness = 0
198
+ elif func_choice == "Volcano":
199
+ func = bulge
200
  elif func_choice == "Shift Up":
201
  func = lambda x, y: spiral(x, y, frequency=spiral_frequency)
202
 
 
 
 
 
 
 
203
 
204
  transformed, (gx, gy) = apply_vector_field_transform(I, func, radius, (center_x, center_y), strength, edge_smoothness, center_smoothness)
205
  vector_field = create_gradient_vector_field(gx, gy, I.shape[:2], reverse=reverse_gradient)
206
 
207
+ # GRADIO CHANGE HERE
208
+ # predictions = pipeline(transformed)
209
+
210
+ # Have to convert to image first
211
+ result = Image.fromarray(transformed)
212
+
213
+ result = str(learn.predict(result))
214
+ print("result")
215
+ print(result)
216
+
217
+ return transformed, vector_field, result
218
 
219
  demo = gr.Interface(
220
  fn=transform_image,
221
  inputs=[
222
  gr.Image(type="filepath"),
223
+ gr.Dropdown(["Pinch", "Spiral", "Shift Up", "Bulge", "Volcano"], value="Bulge", label="Function"),
224
  gr.Checkbox(label="Randomize inputs?"),
225
  gr.Slider(0, 0.5, value=0.25, label="Radius (as fraction of image size)"),
226
  gr.Slider(0, 1, value=0.5, label="Center X"),
227
  gr.Slider(0, 1, value=0.5, label="Center Y"),
228
  gr.Slider(0, 1, value=0.5, label="Strength"),
229
+ # gr.Slider(0, 1, value=0.5, label="Edge Smoothness"),
230
+ # gr.Slider(0, 0.5, value=0.1, label="Center Smoothness")
231
  # gr.Checkbox(label="Reverse Gradient Direction"),
232
  ],
233
  outputs=[
234
  gr.Image(label="Transformed Image"),
235
+ gr.Image(label="Gradient Vector Field"),
236
+ # gr.Image(label="Result", num_top_classes=2)
237
+ # gr.Textbox(label='Result')
238
  ],
239
  title="Image Transformation Demo!",
240
  description="This is the baseline function that will be used to generate the database for a machine learning model I am working on called 'DistortionMl'! The goal of this model is to detect and then reverse image transformations that can be generated here! You can read more about the project at this repository link : https://github.com/nick-leland/DistortionML. The main function that I was working on is the 'Bulge' function, I can't really guarantee that the others work well (;"
bulk_bulge_generation.py CHANGED
@@ -7,8 +7,8 @@ from os import path
7
  def definitions(generator):
8
 
9
  # The image bulge should be entirly contained within the image. For instance, if we have a radius of 0.5 (the max), the image should be force to be at 0.5 (x and y) locations.
10
- radius = generator.random() * 0.5
11
- # radius = generator.normal(loc=0.5, scale=(0.5/4))
12
  # print(f"Radius is {radius}")
13
  # strengtH = Generator.random()
14
  strength = generator.normal(loc=1, scale=(1/6))
@@ -42,6 +42,23 @@ def definitions(generator):
42
  # print(f"({x[0]}, {x[1]})")
43
  return radius, x, strength, smoothness
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  def bulge(x, y):
46
  return -np.sqrt(x**2 + y**2)
47
 
 
7
  def definitions(generator):
8
 
9
  # The image bulge should be entirly contained within the image. For instance, if we have a radius of 0.5 (the max), the image should be force to be at 0.5 (x and y) locations.
10
+ # radius = generator.random() * 0.5
11
+ radius = generator.normal(loc=0.25, scale=(0.5/6))
12
  # print(f"Radius is {radius}")
13
  # strengtH = Generator.random()
14
  strength = generator.normal(loc=1, scale=(1/6))
 
42
  # print(f"({x[0]}, {x[1]})")
43
  return radius, x, strength, smoothness
44
 
45
+ def smooth(generator, strength):
46
+ # edge
47
+ emaxval, eminval = 0.75, 0.25
48
+ emean = (emaxval + eminval) / 2
49
+ estd = (emaxval - eminval) / 6
50
+
51
+ # center
52
+ cmaxval, cminval = 0.5, 0.25
53
+ cmean = (cmaxval + cminval) / 2
54
+ cstd = (cmaxval - cminval) / 4
55
+
56
+
57
+ edge = generator.normal(loc=emean, scale=estd)
58
+ center = generator.normal(loc=cmean, scale=cstd)
59
+
60
+ return edge, center
61
+
62
  def bulge(x, y):
63
  return -np.sqrt(x**2 + y**2)
64
 
requirements.txt CHANGED
@@ -71,5 +71,5 @@ uvicorn==0.30.1
71
  uvloop==0.19.0
72
  watchfiles==0.22.0
73
  websockets==11.0.3
74
- transformers
75
- torch
 
71
  uvloop==0.19.0
72
  watchfiles==0.22.0
73
  websockets==11.0.3
74
+ torch==2.2.0+cpu
75
+ fastai