Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kekehurry committed Sep 30, 2022
0 parents commit 97f5c67
Show file tree
Hide file tree
Showing 55 changed files with 1,789 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.vscode
*.pkl
*.pth
*.pickle
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 kekehurry

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Evolvable-Case-based-Design

158 changes: 158 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import streamlit as st
import streamlit.components.v1 as components
import os
import numpy as np
from io import BytesIO
from PIL import Image
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import torch
import random
import json,cv2
from model.ddpg import Env,DDPG
import plotly.express as px
import plotly.graph_objects as go

agent = DDPG()
env = Env()

st.set_page_config(
page_title="Evolvable Case-based Design",
page_icon="🧊",
layout="wide",
initial_sidebar_state="expanded",
menu_items={
'About': "Design Future Lab\n Contant:[email protected] "
}
)


st.title("Evolvable Case-based Design")
st.text("Genrate City Morphology with specific FSI, GSI and road system")
placeholder1 = st.empty()
placeholder2 = st.empty()
# col1,col2 = st.columns([4,2])
# with col1:
# placeholder2 = st.empty()
# with col2:
# placeholder3 = st.empty()

st.session_state.fsi_coeff = 0.
st.session_state.gsi_coeff = 0.
st.session_state.l_coeff = 0.
st.session_state.osr_coeff = 0.

#generate
def generate(input,target_FSI,target_GSI,max_iter):
env.seg = env.path2seg(input)
env.target_FSI = target_FSI
env.target_GSI = target_GSI
state = env.reset()
action_history = []
reward_history = []
p=placeholder1.progress(0)
for i in range(max_iter):
p.progress(i/(max_iter-1))
action = agent.select_action(state)
action = (action + np.random.normal(0, agent.args.exploration_noise, size=action.shape)).clip(
-agent.args.max_action, agent.args.max_action)
next_state, reward, done, info = env.step(action)
agent.replay_buffer.push((state, next_state, action, reward, float(done)))
action_history.append(action)
reward_history.append(reward)
state = next_state
agent.update()
idx = np.argmax(reward_history)
action = action_history[idx]
img,latent_vector = env.generate_image(action)
data,FSI,GSI,L,OSR = env.createmodel(img)
placeholder1.empty()
return data,FSI,GSI,L,OSR,latent_vector


#visualizer
def visualizer(FSI,GSI,L,OSR,target_FSI,target_GSI):
target_L = target_FSI/target_GSI
target_OSR = (1-target_GSI)/target_FSI
target_values = [target_FSI,target_GSI,target_L,target_OSR]
max_values = [4,1,20,1]
values = [FSI,GSI,L,OSR]
labels = ['FSI','GSI','L', 'OSR']

with open('templates/index.html','r') as f:
html = f.read()
with placeholder2.container():
components.html(html.replace(r'{{data}}',data), height=600)

# with placeholder3.container():
# st.markdown('<br></br><br></br>',unsafe_allow_html=True)
# df1 = pd.DataFrame(dict(
# r=np.array(target_values+values)/np.array(max_values+max_values),
# theta=labels+labels,
# type = np.array(['target','target','target','target','result','result','result','result'])))
# fig1 = px.line_polar(df1, r='r', theta='theta',color='type',line_close=True)
# fig1.update_traces(fill='toself')
# fig1.update_layout(
# polar=dict(
# radialaxis=dict(
# range = [0,1],
# visible=True
# ),
# ),
# showlegend=False,
# )
# st.plotly_chart(fig1, use_container_width=True)

cols = st.columns(4)
for i in range(4):
with cols[i]:
st.number_input(labels[i],value=values[i])


st.sidebar.caption("INPUT PARAMETERS")
form1 = st.sidebar.form('Form1')
option = form1.selectbox("EXAMPLE INPUT",('example_1','example_2','example_3'))
input1 = form1.file_uploader('INPUT')
if input1:
input = input1
else:
with open('static/%s.png'%option,'rb') as f:
input = f.read()
input = BytesIO(input)
form1.image(input)
# target
target_FSI=form1.slider('TARGET FSI',0.,5.0,2.6,0.1)
target_GSI=form1.slider('TARGET GSI',0.,0.5,0.36,0.01)
seed = form1.slider('SEED',0,100,50,1)
max_iter=form1.slider('MAX ITER',10,100,50,10)
gen_button = form1.form_submit_button('generate')
if gen_button:
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
data,FSI,GSI,L,OSR,init_latent_vector = generate(input,target_FSI,target_GSI,max_iter)
st.session_state.init_latent_vector = init_latent_vector
st.session_state.input = input
visualizer(FSI,GSI,L,OSR,target_FSI,target_GSI)

# adjust
st.sidebar.caption("ADJUST PARAMETERS")
form2 = st.sidebar.form('Form2',clear_on_submit=True)
fsi_coeff = form2.slider('FSI DIRECTION',-1.,1.,0.,step=0.01)
gsi_coeff = form2.slider('GSI DIRECTION',-1.,1.,0.,step=0.01)*3
l_coeff = form2.slider('L DIRECTION',-1.,1.,0.,step=0.01)
osr_coeff = form2.slider('OSR DIRECTION',-1.,1.,0.,step=0.01)
col5,col6 = form2.columns(2)
adj_button = form2.form_submit_button('adjust')
if adj_button:
env.seg = env.path2seg(st.session_state.input)
init_latent_vector = st.session_state.init_latent_vector
new_img,latent_vector = env.adjust_image(init_latent_vector,env.fsi_direction,fsi_coeff)
new_img,latent_vector = env.adjust_image(latent_vector,env.gsi_direction,gsi_coeff)
new_img,latent_vector = env.adjust_image(latent_vector,env.l_direction,l_coeff)
new_img,latent_vector = env.adjust_image(latent_vector,env.osr_direction,osr_coeff)
st.session_state.init_latent_vector = latent_vector
data,FSI,GSI,L,OSR = env.createmodel(new_img)
visualizer(FSI,GSI,L,OSR,target_FSI,target_GSI)

1 change: 1 addition & 0 deletions data/Shenzhen.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[0,0,0],[255,255,255]]
Binary file added data/__pycache__/labeldataset.cpython-38.pyc
Binary file not shown.
Binary file added data/__pycache__/labeldataset.cpython-39.pyc
Binary file not shown.
Binary file added data/__pycache__/pairdataset.cpython-38.pyc
Binary file not shown.
125 changes: 125 additions & 0 deletions data/labeldataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
from torch.utils.data import DataLoader, Dataset
from torchvision import transforms
from PIL import Image
import numpy as np
import cv2
import os
import random
import torch
import matplotlib.pyplot as plt

class LabelDataset(Dataset):
def __init__(self,root,colors,split='train',img_size = 256,suffix='png',random_flip=True):
self.root = root
self.split = split
self.img_size = img_size
self.suffix = suffix

self.files = {}
self.annotations_base = os.path.join(self.root,self.split,'vis')
self.images_base = os.path.join(self.root,self.split,'images')

self.files[self.split] = self.recursive_glob(self.images_base)

self.colors = colors
if self.colors:
self.label_colours = dict(zip(range(len(self.colors)),self.colors))

self.random_flip = random_flip

self.transform = transforms.Compose([
transforms.Resize(self.img_size,interpolation=transforms.InterpolationMode.NEAREST),
transforms.ToTensor(),
])

def is_image_file(self,filename):
IMG_EXTENSIONS = ['.jpg', '.JPG', '.jpeg', '.JPEG','.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', '.tiff', '.webp']
return any(filename.endswith(extension) for extension in IMG_EXTENSIONS)

def recursive_glob(self,rootdir="."):
return [
os.path.join(looproot, filename)
for looproot, _, filenames in os.walk(rootdir)
for filename in filenames
if self.is_image_file(filename)
]

def encode_segmap(self,seg):
if self.colors:
seg = np.array(seg,dtype=np.uint8)
for i in range(len(self.colors)):
color = np.array(self.colors[i])
mask = cv2.inRange(seg,color-50,color+50)
mask = cv2.cvtColor(mask,cv2.COLOR_GRAY2RGB)
seg[mask==255] = i
seg[seg>=len(self.colors)] = len(self.colors)
return seg[:,:,0]
else:
return seg

def decode_segmap(self, temp):
if self.colors:
r = temp.copy()
g = temp.copy()
b = temp.copy()
for i in range(len(self.colors)):
r[r == i] = self.colors[i][0]
g[g == i] = self.colors[i][1]
b[b == i] = self.colors[i][2]

rgb = np.zeros((temp.shape[0], temp.shape[1], 3))
rgb[:, :, 0] = r / 255.0
rgb[:, :, 1] = g / 255.0
rgb[:, :, 2] = b / 255.0
return rgb
else:
return temp

def __getitem__(self,index):
img_path = self.files[self.split][index].rstrip()
seg_path = os.path.join(self.annotations_base,'%s.%s'%(os.path.basename(img_path)[:-4],self.suffix))

img = Image.open(img_path).convert('RGB')
seg = Image.open(seg_path).convert('RGB')

seg = self.encode_segmap(seg)
seg = self.transform(Image.fromarray(seg))
img = self.transform(img)

if self.random_flip:
p1 = random.randint(0,1)
p2 = random.randint(0,1)
img = transforms.RandomHorizontalFlip(p1)(img)
seg = transforms.RandomHorizontalFlip(p1)(seg)
img = transforms.RandomVerticalFlip(p2)(img)
seg = transforms.RandomVerticalFlip(p2)(seg)

return img, seg

def __len__(self):
return len(self.files[self.split])

if __name__ == '__main__':
colors = [[0,0,0],[255,255,255],[0,128,0],[0,0,255],[128,0,128]]
dataset = {
x : LabelDataset(root='../datasets/Manhattan',colors=colors,split=x,img_size=256) for x in ['train','test']
}
data = {
x : DataLoader( dataset[x],batch_size=1,shuffle=True,num_workers=0) for x in ['train','test']
}
img,seg = next(iter(data['train']))

print(img.shape)
print(seg.shape)
seg = transforms.ToPILImage()(seg[0])
seg = dataset['train'].decode_segmap(np.array(seg))

img = transforms.ToPILImage()(img[0])

ax = plt.subplot(1,2,1)
ax.imshow(seg)

ax1 = plt.subplot(1,2,2)
ax1.imshow(img)

plt.show()
Loading

0 comments on commit 97f5c67

Please sign in to comment.