THEODOROS commited on
Commit
e11de81
·
1 Parent(s): c8d69e4

contiguous mutation

Browse files
Files changed (1) hide show
  1. app.py +28 -22
app.py CHANGED
@@ -68,10 +68,11 @@ def prompt_to_layout(user_prompt, fpath=None):
68
  input_ids = tokenizer(model_prompt, return_tensors='pt')
69
  output = finetuned.generate(**input_ids, do_sample=True, top_p=0.94, top_k=100, max_length=300)
70
  output = tokenizer.batch_decode(output, skip_special_tokens=True)
71
- output = output[0].split('[Layout]')[1].split(', ')
72
- spaces = [txt.split(':')[0] for txt in output]
 
73
 
74
- coordinates = [txt.split(':')[1] for txt in output]
75
  coordinates = [re.findall(regex, coord) for coord in coordinates]
76
 
77
  polygons = []
@@ -82,20 +83,20 @@ def prompt_to_layout(user_prompt, fpath=None):
82
  for poly in polygons:
83
  geom.append(Polygon(np.array(poly, dtype=int)))
84
 
85
- colors = [architext_colors[housegan_labels[space]] for space in spaces]
86
 
87
  _, im = draw_polygons(geom, colors, fpath=fpath)
88
 
89
- legend = Image.open("legend.png")
90
 
91
  im = np.array(im)
92
  im[:40, :] = np.array(legend)
93
  im = Image.fromarray(im)
94
 
95
- return im, output
96
 
97
  def mut_txt2layout(mut_output):
98
- output = mut_output[0].split('[Layout]')[1].split(', ')
99
  spaces = [txt.split(':')[0].strip(' ') for txt in output]
100
  coordinates = [txt.split(':')[1] for txt in output]
101
  coordinates = [re.findall(regex, coord) for coord in coordinates]
@@ -108,10 +109,10 @@ def mut_txt2layout(mut_output):
108
  for poly in polygons:
109
  geom.append(Polygon(np.array(poly, dtype=int)))
110
 
111
- colors = [architext_colors[housegan_labels[space]] for space in spaces]
112
  _, im = draw_polygons(geom, colors, fpath=None)
113
 
114
- legend = Image.open("legend.png")
115
 
116
  im = np.array(im)
117
  im[:40, :] = np.array(legend)
@@ -119,22 +120,23 @@ def mut_txt2layout(mut_output):
119
 
120
  return im
121
 
122
- def prompt_with_mutation(user_prompt, fpath=None):
123
 
124
  #Create initial layout based on prompt
125
- im, output = prompt_to_layout(user_prompt)
126
 
127
  #Create mutated layout based on initial
128
- cut_off = np.random.randint(1, 3, size=1)[0]
129
- cut_off = min(cut_off, len(output)-1)
130
- to_keep = ', '.join(output[:cut_off]) + ', '
131
- new_prompt = '[User prompt] {} [Layout] {}'.format(user_prompt, to_keep)
 
132
  input_ids = tokenizer(new_prompt, return_tensors='pt')
133
  mut_output = finetuned.generate(**input_ids, do_sample=True, top_p=0.94, top_k=100, max_length=300)
134
  mut_output = tokenizer.batch_decode(mut_output, skip_special_tokens=True)
135
  mut_im = mut_txt2layout(mut_output)
136
 
137
- return im, mut_im, output, mut_output
138
 
139
  # Gradio App
140
 
@@ -211,24 +213,28 @@ custom_css="""
211
  opacity: 1 !important;
212
  }"""
213
 
214
- def gen_and_mutate(user_prompt, mutate=False):
215
  if(mutate):
216
  im, mut_im = None, None
217
  while (mut_im is None):
218
- im, mut_im, output, mut_output = prompt_with_mutation(user_prompt)
 
 
 
219
  else:
220
  mut_im=Image.open("empty.png")
221
- im, _ = prompt_to_layout(user_prompt)
222
-
223
  return im, mut_im
224
 
225
  checkbox = gr.inputs.Checkbox(label='Mutate')
226
- textbox = gr.inputs.Textbox(placeholder='house with two bedrooms and one bathroom', lines="4", label="DESCRIBE YOUR DESIGN")
 
227
 
228
  generated = gr.outputs.Image(label='Generated Layout')
229
  mutated = gr.outputs.Image(label='Mutated Layout')
230
 
231
- iface = gr.Interface(fn=gen_and_mutate, inputs=[textbox, checkbox], outputs=[generated, mutated],
232
  css=custom_css,
233
  thumbnail="thumbnail_gradio.PNG",
234
  description='Demo of Semantic Generation of Residential Layouts \n',
 
68
  input_ids = tokenizer(model_prompt, return_tensors='pt')
69
  output = finetuned.generate(**input_ids, do_sample=True, top_p=0.94, top_k=100, max_length=300)
70
  output = tokenizer.batch_decode(output, skip_special_tokens=True)
71
+
72
+ layout = output[0].split('[User prompt]')[1].split('[Layout]')[1].split(', ')
73
+ spaces = [txt.split(':')[0] for txt in layout]
74
 
75
+ coordinates = [txt.split(':')[1] for txt in layout]
76
  coordinates = [re.findall(regex, coord) for coord in coordinates]
77
 
78
  polygons = []
 
83
  for poly in polygons:
84
  geom.append(Polygon(np.array(poly, dtype=int)))
85
 
86
+ colors = [architext_colors2[housegan_labels[space]] for space in spaces]
87
 
88
  _, im = draw_polygons(geom, colors, fpath=fpath)
89
 
90
+ legend = Image.open("legend3.png")
91
 
92
  im = np.array(im)
93
  im[:40, :] = np.array(legend)
94
  im = Image.fromarray(im)
95
 
96
+ return im, layout, output
97
 
98
  def mut_txt2layout(mut_output):
99
+ output = mut_output[0].split('[User prompt]')[1].split('[Layout]')[1].split(', ')
100
  spaces = [txt.split(':')[0].strip(' ') for txt in output]
101
  coordinates = [txt.split(':')[1] for txt in output]
102
  coordinates = [re.findall(regex, coord) for coord in coordinates]
 
109
  for poly in polygons:
110
  geom.append(Polygon(np.array(poly, dtype=int)))
111
 
112
+ colors = [architext_colors2[housegan_labels[space]] for space in spaces]
113
  _, im = draw_polygons(geom, colors, fpath=None)
114
 
115
+ legend = Image.open("legend3.png")
116
 
117
  im = np.array(im)
118
  im[:40, :] = np.array(legend)
 
120
 
121
  return im
122
 
123
+ def prompt_with_mutation(user_prompt, mut_rate, fpath=None):
124
 
125
  #Create initial layout based on prompt
126
+ im, layout, output = prompt_to_layout(user_prompt)
127
 
128
  #Create mutated layout based on initial
129
+ mut_len = int((1-mut_rate)*len(layout))
130
+ index1 = random.randrange(0,len(layout)-mut_len)
131
+ rooms = layout[index1:index1+mut_len]
132
+ rooms = ', '.join(rooms) + ', '
133
+ new_prompt = '[User prompt] {} [Layout] {}'.format(user_prompt, rooms)
134
  input_ids = tokenizer(new_prompt, return_tensors='pt')
135
  mut_output = finetuned.generate(**input_ids, do_sample=True, top_p=0.94, top_k=100, max_length=300)
136
  mut_output = tokenizer.batch_decode(mut_output, skip_special_tokens=True)
137
  mut_im = mut_txt2layout(mut_output)
138
 
139
+ return im, mut_im
140
 
141
  # Gradio App
142
 
 
213
  opacity: 1 !important;
214
  }"""
215
 
216
+ def gen_and_mutate(user_prompt, mutate=False, mut_rate=0.2):
217
  if(mutate):
218
  im, mut_im = None, None
219
  while (mut_im is None):
220
+ try:
221
+ im, mut_im = prompt_with_mutation(user_prompt, mut_rate)
222
+ except:
223
+ pass
224
  else:
225
  mut_im=Image.open("empty.png")
226
+ im, _, _ = prompt_to_layout(user_prompt)
227
+
228
  return im, mut_im
229
 
230
  checkbox = gr.inputs.Checkbox(label='Mutate')
231
+ slider = gr.inputs.Slider(0.2, 0.8, 0.1, label='Mutation rate')
232
+ textbox = gr.inputs.Textbox(placeholder='house with two bedrooms and one bathroom', lines="3", label="DESCRIBE YOUR DESIGN")
233
 
234
  generated = gr.outputs.Image(label='Generated Layout')
235
  mutated = gr.outputs.Image(label='Mutated Layout')
236
 
237
+ iface = gr.Interface(fn=gen_and_mutate, inputs=[textbox, checkbox, slider], outputs=[generated, mutated],
238
  css=custom_css,
239
  thumbnail="thumbnail_gradio.PNG",
240
  description='Demo of Semantic Generation of Residential Layouts \n',