lmoss commited on
Commit
cd2924d
·
1 Parent(s): 53bea02

seeing if runtime error can be fixed

Browse files
Files changed (1) hide show
  1. app.py +92 -81
app.py CHANGED
@@ -8,8 +8,15 @@ import numpy as np
8
  import numpy.typing as npt
9
  from dcgan import DCGAN3D_G
10
  import os
 
11
  pv.start_xvfb()
12
 
 
 
 
 
 
 
13
  def download_checkpoint(url: str, path: str) -> None:
14
  resp = requests.get(url)
15
 
@@ -61,87 +68,91 @@ def create_matplotlib_figure(img: npt.ArrayLike, midpoint: int):
61
  a.set_axis_off()
62
  return fig
63
 
64
- st.title("Generating Porous Media with GANs")
65
-
66
- st.markdown(
67
- """
68
- ### Author
69
- _[Lukas Mosser](https://scholar.google.com/citations?user=y0R9snMAAAAJ&hl=en&oi=ao) (2022)_ - :bird:[porestar](https://twitter.com/porestar)
70
-
71
- ## Description
72
- This is a demo of the Generative Adversarial Network (GAN, [Goodfellow 2014](https://arxiv.org/abs/1406.2661)) trained for our publication [PorousMediaGAN](https://github.com/LukasMosser/PorousMediaGan)
73
- published in Physical Review E ([Mosser et. al 2017](https://journals.aps.org/pre/abstract/10.1103/PhysRevE.96.043309))
74
 
75
- The model is a pretrained 3D Deep Convolutional GAN ([Radford 2015](https://arxiv.org/abs/1511.06434)) that generates a volumetric image of a porous medium, here a Berea sandstone, from a set of pretrained weights.
76
-
77
- ## Intent
78
- I hope this encourages others to create interactive demos of their research for knowledge sharing and validation.
79
-
80
- ## The Demo
81
- Slices through the 3D volume are rendered using [PyVista](https://www.pyvista.org/) and [PyThreeJS](https://pythreejs.readthedocs.io/en/stable/)
82
-
83
- The model itself currently runs on the :hugging_face: [Huggingface Spaces](https://huggingface.co/spaces) instance.
84
- Future migration to the :hugging_face: [Huggingface Models](https://huggingface.co/models) repository is possible.
85
 
86
- ### Interactive Model Parameters
87
- The GAN used here in this study is fully convolutional "_Look Ma' no MLP's_": Changing the spatial extent of the latent space vector _z_
88
- allows one to generate larger synthetic images.
89
 
90
- """
91
- , unsafe_allow_html=True)
92
-
93
- view_width = 400
94
- view_height = 400
95
-
96
- model_fname = "berea_generator_epoch_24.pth"
97
- checkpoint_url = "https://github.com/LukasMosser/PorousMediaGan/blob/master/checkpoints/berea/{0:}?raw=true".format(model_fname)
98
-
99
- download_checkpoint(checkpoint_url, model_fname)
100
-
101
- latent_size = st.slider("Latent Space Size z", min_value=1, max_value=5, step=1)
102
- img = generate_image(model_fname, latent_size=latent_size)
103
- slices, mesh, dist = create_uniform_mesh_marching_cubes(img)
104
-
105
- pv.set_plot_theme("document")
106
- pl = pv.Plotter(shape=(1, 1),
107
- window_size=(view_width, view_height))
108
- _ = pl.add_mesh(slices, cmap="gray")
109
- pl.export_html('slices.html')
110
-
111
- pl = pv.Plotter(shape=(1, 1),
112
- window_size=(view_width, view_height))
113
- _ = pl.add_mesh(mesh, scalars=dist)
114
- pl.export_html('mesh.html')
115
-
116
- st.header("2D Cross-Section of Generated Volume")
117
- fig = create_matplotlib_figure(img, img.shape[0]//2)
118
- st.pyplot(fig=fig)
119
-
120
-
121
-
122
- HtmlFile = open("slices.html", 'r', encoding='utf-8')
123
- source_code = HtmlFile.read()
124
- st.header("3D Intersections")
125
- components.html(source_code, width=view_width, height=view_height)
126
- st.markdown("_Click and drag to spin, right click to shift._")
127
-
128
- HtmlFile = open("mesh.html", 'r', encoding='utf-8')
129
- source_code = HtmlFile.read()
130
- st.header("3D Pore Space Mesh")
131
- components.html(source_code, width=view_width, height=view_height)
132
- st.markdown("_Click and drag to spin, right click to shift._")
133
-
134
- st.markdown("""
135
- ## Citation
136
- If you use our code for your own research, we would be grateful if you cite our publication:
137
- ```
138
- @article{pmgan2017,
139
- title={Reconstruction of three-dimensional porous media using generative adversarial neural networks},
140
- author={Mosser, Lukas and Dubrule, Olivier and Blunt, Martin J.},
141
- journal={arXiv preprint arXiv:1704.03225},
142
- year={2017}
143
- }```
144
- """)
145
-
146
- os.remove("slices.html")
147
- os.remove("mesh.html")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  import numpy.typing as npt
9
  from dcgan import DCGAN3D_G
10
  import os
11
+ import pathlib
12
  pv.start_xvfb()
13
 
14
+ STREAMLIT_STATIC_PATH = pathlib.Path(st.__path__[0]) / 'static'
15
+
16
+ DOWNLOADS_PATH = (STREAMLIT_STATIC_PATH / "downloads")
17
+ if not DOWNLOADS_PATH.is_dir():
18
+ DOWNLOADS_PATH.mkdir()
19
+
20
  def download_checkpoint(url: str, path: str) -> None:
21
  resp = requests.get(url)
22
 
 
68
  a.set_axis_off()
69
  return fig
70
 
71
+ def main():
72
+ st.title("Generating Porous Media with GANs")
 
 
 
 
 
 
 
 
73
 
74
+ st.markdown(
75
+ """
76
+ ### Author
77
+ _[Lukas Mosser](https://scholar.google.com/citations?user=y0R9snMAAAAJ&hl=en&oi=ao) (2022)_ - :bird:[porestar](https://twitter.com/porestar)
 
 
 
 
 
 
78
 
79
+ ## Description
80
+ This is a demo of the Generative Adversarial Network (GAN, [Goodfellow 2014](https://arxiv.org/abs/1406.2661)) trained for our publication [PorousMediaGAN](https://github.com/LukasMosser/PorousMediaGan)
81
+ published in Physical Review E ([Mosser et. al 2017](https://journals.aps.org/pre/abstract/10.1103/PhysRevE.96.043309))
82
 
83
+ The model is a pretrained 3D Deep Convolutional GAN ([Radford 2015](https://arxiv.org/abs/1511.06434)) that generates a volumetric image of a porous medium, here a Berea sandstone, from a set of pretrained weights.
84
+
85
+ ## Intent
86
+ I hope this encourages others to create interactive demos of their research for knowledge sharing and validation.
87
+
88
+ ## The Demo
89
+ Slices through the 3D volume are rendered using [PyVista](https://www.pyvista.org/) and [PyThreeJS](https://pythreejs.readthedocs.io/en/stable/)
90
+
91
+ The model itself currently runs on the :hugging_face: [Huggingface Spaces](https://huggingface.co/spaces) instance.
92
+ Future migration to the :hugging_face: [Huggingface Models](https://huggingface.co/models) repository is possible.
93
+
94
+ ### Interactive Model Parameters
95
+ The GAN used here in this study is fully convolutional "_Look Ma' no MLP's_": Changing the spatial extent of the latent space vector _z_
96
+ allows one to generate larger synthetic images.
97
+
98
+ """
99
+ , unsafe_allow_html=True)
100
+
101
+ view_width = 400
102
+ view_height = 400
103
+
104
+ model_fname = "berea_generator_epoch_24.pth"
105
+ checkpoint_url = "https://github.com/LukasMosser/PorousMediaGan/blob/master/checkpoints/berea/{0:}?raw=true".format(model_fname)
106
+
107
+ download_checkpoint(checkpoint_url, (DOWNLOADS_PATH / model_fname))
108
+
109
+ latent_size = st.slider("Latent Space Size z", min_value=1, max_value=5, step=1)
110
+ img = generate_image((DOWNLOADS_PATH / model_fname), latent_size=latent_size)
111
+ slices, mesh, dist = create_uniform_mesh_marching_cubes(img)
112
+
113
+ pv.set_plot_theme("document")
114
+ pl = pv.Plotter(shape=(1, 1),
115
+ window_size=(view_width, view_height))
116
+ _ = pl.add_mesh(slices, cmap="gray")
117
+ pl.export_html((DOWNLOADS_PATH / 'slices.html'))
118
+
119
+ pl = pv.Plotter(shape=(1, 1),
120
+ window_size=(view_width, view_height))
121
+ _ = pl.add_mesh(mesh, scalars=dist)
122
+ pl.export_html((DOWNLOADS_PATH / 'mesh.html'))
123
+
124
+ st.header("2D Cross-Section of Generated Volume")
125
+ fig = create_matplotlib_figure(img, img.shape[0]//2)
126
+ st.pyplot(fig=fig)
127
+
128
+
129
+
130
+ HtmlFile = open((DOWNLOADS_PATH / 'slices.html'), 'r', encoding='utf-8')
131
+ source_code = HtmlFile.read()
132
+ st.header("3D Intersections")
133
+ components.html(source_code, width=view_width, height=view_height)
134
+ st.markdown("_Click and drag to spin, right click to shift._")
135
+
136
+ HtmlFile = open((DOWNLOADS_PATH / 'mesh.html'), 'r', encoding='utf-8')
137
+ source_code = HtmlFile.read()
138
+ st.header("3D Pore Space Mesh")
139
+ components.html(source_code, width=view_width, height=view_height)
140
+ st.markdown("_Click and drag to spin, right click to shift._")
141
+
142
+ st.markdown("""
143
+ ## Citation
144
+ If you use our code for your own research, we would be grateful if you cite our publication:
145
+ ```
146
+ @article{pmgan2017,
147
+ title={Reconstruction of three-dimensional porous media using generative adversarial neural networks},
148
+ author={Mosser, Lukas and Dubrule, Olivier and Blunt, Martin J.},
149
+ journal={arXiv preprint arXiv:1704.03225},
150
+ year={2017}
151
+ }```
152
+ """)
153
+
154
+ #os.remove("slices.html")
155
+ #os.remove("mesh.html")
156
+
157
+ if __name__ == "__main__":
158
+ main()