File size: 6,086 Bytes
a577b73
3e26a38
 
a577b73
 
 
2eea8e7
3e26a38
 
53bea02
cd2924d
4d6055a
3e26a38
cd2924d
 
 
 
 
 
3e26a38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a577b73
cd2924d
 
9bc9fb6
cd2924d
 
 
 
3e26a38
cd2924d
 
 
3e26a38
cd2924d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import streamlit as st
import streamlit.components.v1 as components
import matplotlib.pyplot as plt
import pyvista as pv
import torch
import requests
import numpy as np
import numpy.typing as npt
from dcgan import DCGAN3D_G
import os
import pathlib
pv.start_xvfb()

STREAMLIT_STATIC_PATH = pathlib.Path(st.__path__[0]) / 'static'

DOWNLOADS_PATH = (STREAMLIT_STATIC_PATH / "downloads")
if not DOWNLOADS_PATH.is_dir():
    DOWNLOADS_PATH.mkdir()

def download_checkpoint(url: str, path: str) -> None:
    resp = requests.get(url)

    with open(path, 'wb') as f:
        f.write(resp.content)


def generate_image(path: str,
                   image_size: int = 64,
                   z_dim: int = 512,
                   n_channels: int = 1,
                   n_features: int = 32,
                   ngpu: int = 1,
                   latent_size: int = 3) -> npt.ArrayLike:
    netG = DCGAN3D_G(image_size, z_dim, n_channels, n_features, ngpu)
    netG.load_state_dict(torch.load(path, map_location=torch.device('cpu')))
    z = torch.randn(1, z_dim, latent_size, latent_size, latent_size)
    with torch.no_grad():
        X = netG(z)
    img = 1 - (X[0, 0].numpy() + 1) / 2
    return img


def create_uniform_mesh_marching_cubes(img: npt.ArrayLike):
    grid = pv.UniformGrid(
        dims=img.shape,
        spacing=(1, 1, 1),
        origin=(0, 0, 0),
    )

    values = img.flatten()
    grid.point_data['my_array'] = values
    slices = grid.slice_orthogonal()
    mesh = grid.contour(1, values, method='marching_cubes', rng=[1, 0], preference="points")
    dist = np.linalg.norm(mesh.points, axis=1)
    return slices, mesh, dist


def create_matplotlib_figure(img: npt.ArrayLike, midpoint: int):
    fig, ax = plt.subplots(1, 3, figsize=(18, 6))
    ax[0].imshow(img[midpoint], cmap="gray", vmin=0, vmax=1)
    ax[1].imshow(img[:, midpoint], cmap="gray", vmin=0, vmax=1)
    ax[2].imshow(img[..., midpoint], cmap="gray", vmin=0, vmax=1)

    for a, title in zip(ax, ["Front", "Right", "Top"]):
        a.set_title(title, fontsize=18)

    for a in ax:
        a.set_axis_off()
    return fig

def main():
    st.title("Generating Porous Media with GANs")

    st.markdown(
        """
        ### Author
        _[Lukas Mosser](https://scholar.google.com/citations?user=y0R9snMAAAAJ&hl=en&oi=ao) (2022)_ - :bird:[porestar](https://twitter.com/porestar)
    
        ## Description
        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)
        published in Physical Review E ([Mosser et. al 2017](https://journals.aps.org/pre/abstract/10.1103/PhysRevE.96.043309))
    
        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.  
        
        ## Intent
        I hope this encourages others to create interactive demos of their research for knowledge sharing and validation.
        
        ## The Demo
        Slices through the 3D volume are rendered using [PyVista](https://www.pyvista.org/) and [PyThreeJS](https://pythreejs.readthedocs.io/en/stable/)
        
        The model itself currently runs on the :hugging_face: [Huggingface Spaces](https://huggingface.co/spaces) instance.  
        Future migration to the :hugging_face: [Huggingface Models](https://huggingface.co/models) repository is possible.
        
        ### Interactive Model Parameters
        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_
        allows one to generate larger synthetic images. 
        
        """
    , unsafe_allow_html=True)

    view_width = 400
    view_height = 400

    model_fname = "berea_generator_epoch_24.pth"
    checkpoint_url = "https://github.com/LukasMosser/PorousMediaGan/blob/master/checkpoints/berea/{0:}?raw=true".format(model_fname)

    download_checkpoint(checkpoint_url, (DOWNLOADS_PATH / model_fname))

    latent_size = st.slider("Latent Space Size z", min_value=1, max_value=5, step=1)
    img = generate_image((DOWNLOADS_PATH / model_fname), latent_size=latent_size)
    slices, mesh, dist = create_uniform_mesh_marching_cubes(img)

    pv.set_plot_theme("document")
    pl = pv.Plotter(shape=(1, 1),
                         window_size=(view_width, view_height))
    _ = pl.add_mesh(slices, cmap="gray")
    pl.export_html((DOWNLOADS_PATH / 'slices.html'))

    pl = pv.Plotter(shape=(1, 1),
                         window_size=(view_width, view_height))
    _ = pl.add_mesh(mesh, scalars=dist)
    pl.export_html((DOWNLOADS_PATH / 'mesh.html'))

    st.header("2D Cross-Section of Generated Volume")
    fig = create_matplotlib_figure(img, img.shape[0]//2)
    st.pyplot(fig=fig)



    HtmlFile = open((DOWNLOADS_PATH / 'slices.html'), 'r', encoding='utf-8')
    source_code = HtmlFile.read()
    st.header("3D Intersections")
    components.html(source_code, width=view_width, height=view_height)
    st.markdown("_Click and drag to spin, right click to shift._")

    HtmlFile = open((DOWNLOADS_PATH / 'mesh.html'), 'r', encoding='utf-8')
    source_code = HtmlFile.read()
    st.header("3D Pore Space Mesh")
    components.html(source_code, width=view_width, height=view_height)
    st.markdown("_Click and drag to spin, right click to shift._")

    st.markdown("""
        ## Citation
        If you use our code for your own research, we would be grateful if you cite our publication:
        ```
        @article{pmgan2017,
            title={Reconstruction of three-dimensional porous media using generative adversarial neural networks},
            author={Mosser, Lukas and Dubrule, Olivier and Blunt, Martin J.},
            journal={arXiv preprint arXiv:1704.03225},
            year={2017}
        }```
        """)

    #os.remove("slices.html")
    #os.remove("mesh.html")

if __name__ == "__main__":
    main()