diffusive_size_factor_AI#

PoreSpy’s diffusive_size_factor_AI includes the steps for predicting the diffusive size factors of the conduit images decribed here. Note that the diffusive conductance of the conduits can be then calculated by multiplying the size factor by diffusivity of the phase. The function takes in the images of segmented porous medium and returns an array of diffusive size factors for all conduits in the image. Therefore, the framework can be applied to both one conduit image as well as a segmented image of porous medium.

PS_dl

Trained model and supplementary materials#

To use the diffusive_size_factor_AI, the trained model, and training data distribution are required. The AI model files and additional files used in this example are available here. The folder contains following files:

  • Trained model weights: This file includes only weights of the deep learning layers. To use this file, the Resnet50 model structure must be built first.

  • Trained data distribution: This file will be used in denormalizing predicted values based on normalized transform applied on training data. The denormalizing step is included in diffusive_size_factor_AI method.

  • Finite difference diffusive conductance: This file is used in this example to compare the prediction results with finite difference method for segmented regions

  • Pair of regions: This file is used in this example to compare the prediction results with finite difference method for a pair of regions

Let’s download the tensorflow files required to run this notebook:

try:
    import tensorflow as tf
except ImportError:
    !pip install tensorflow

try:
    import sklearn
except ImportError:
    !pip install scikit-learn

import os

if not os.path.exists("sf-model-lib"):
    !git clone https://github.com/PMEAL/sf-model-lib
Requirement already satisfied: tensorflow in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (2.10.0)
Requirement already satisfied: grpcio<2.0,>=1.24.3 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (1.48.1)
Requirement already satisfied: gast<=0.4.0,>=0.2.1 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (0.4.0)
Requirement already satisfied: packaging in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (21.3)
Requirement already satisfied: absl-py>=1.0.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (1.2.0)
Requirement already satisfied: h5py>=2.9.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (3.7.0)
Requirement already satisfied: six>=1.12.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (1.16.0)
Requirement already satisfied: wrapt>=1.11.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (1.14.1)
Requirement already satisfied: tensorflow-estimator<2.11,>=2.10.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (2.10.0)
Requirement already satisfied: protobuf<3.20,>=3.9.2 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (3.19.4)
Requirement already satisfied: google-pasta>=0.1.1 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (0.2.0)
Requirement already satisfied: opt-einsum>=2.3.2 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (3.3.0)
Requirement already satisfied: termcolor>=1.1.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (1.1.0)
Requirement already satisfied: tensorboard<2.11,>=2.10 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (2.10.0)
Requirement already satisfied: keras<2.11,>=2.10.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (2.10.0)
Requirement already satisfied: keras-preprocessing>=1.1.1 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (1.1.2)
Requirement already satisfied: astunparse>=1.6.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (1.6.3)
Requirement already satisfied: numpy>=1.20 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (1.23.3)
Requirement already satisfied: typing-extensions>=3.6.6 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (4.3.0)
Requirement already satisfied: flatbuffers>=2.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (2.0.7)
Requirement already satisfied: setuptools in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (59.8.0)
Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (0.27.0)
Requirement already satisfied: libclang>=13.0.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorflow) (14.0.6)
Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from astunparse>=1.6.0->tensorflow) (0.37.1)
Requirement already satisfied: werkzeug>=1.0.1 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorboard<2.11,>=2.10->tensorflow) (2.2.2)
Requirement already satisfied: markdown>=2.6.8 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorboard<2.11,>=2.10->tensorflow) (3.4.1)
Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorboard<2.11,>=2.10->tensorflow) (0.6.1)
Requirement already satisfied: requests<3,>=2.21.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorboard<2.11,>=2.10->tensorflow) (2.28.1)
Requirement already satisfied: google-auth<3,>=1.6.3 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorboard<2.11,>=2.10->tensorflow) (2.11.0)
Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorboard<2.11,>=2.10->tensorflow) (0.4.6)
Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from tensorboard<2.11,>=2.10->tensorflow) (1.8.1)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from packaging->tensorflow) (3.0.9)
Requirement already satisfied: rsa<5,>=3.1.4 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from google-auth<3,>=1.6.3->tensorboard<2.11,>=2.10->tensorflow) (4.9)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from google-auth<3,>=1.6.3->tensorboard<2.11,>=2.10->tensorflow) (0.2.8)
Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from google-auth<3,>=1.6.3->tensorboard<2.11,>=2.10->tensorflow) (5.2.0)
Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.11,>=2.10->tensorflow) (1.3.1)
Requirement already satisfied: importlib-metadata>=4.4 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from markdown>=2.6.8->tensorboard<2.11,>=2.10->tensorflow) (4.11.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from requests<3,>=2.21.0->tensorboard<2.11,>=2.10->tensorflow) (1.26.11)
Requirement already satisfied: idna<4,>=2.5 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from requests<3,>=2.21.0->tensorboard<2.11,>=2.10->tensorflow) (3.3)
Requirement already satisfied: certifi>=2017.4.17 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from requests<3,>=2.21.0->tensorboard<2.11,>=2.10->tensorflow) (2022.6.15.1)
Requirement already satisfied: charset-normalizer<3,>=2 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from requests<3,>=2.21.0->tensorboard<2.11,>=2.10->tensorflow) (2.1.1)
Requirement already satisfied: MarkupSafe>=2.1.1 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from werkzeug>=1.0.1->tensorboard<2.11,>=2.10->tensorflow) (2.1.1)
Requirement already satisfied: zipp>=0.5 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from importlib-metadata>=4.4->markdown>=2.6.8->tensorboard<2.11,>=2.10->tensorflow) (3.8.1)
Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard<2.11,>=2.10->tensorflow) (0.4.8)
Requirement already satisfied: oauthlib>=3.0.0 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.11,>=2.10->tensorflow) (3.2.1)
Collecting scikit-learn
  Using cached scikit_learn-1.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.2 MB)
Requirement already satisfied: numpy>=1.17.3 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from scikit-learn) (1.23.3)
Collecting joblib>=1.0.0
  Downloading joblib-1.1.0-py2.py3-none-any.whl (306 kB)
?25l     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.0/307.0 kB ? eta -:--:--
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 307.0/307.0 kB 26.5 MB/s eta 0:00:00
?25h
Collecting threadpoolctl>=2.0.0
  Downloading threadpoolctl-3.1.0-py3-none-any.whl (14 kB)
Requirement already satisfied: scipy>=1.3.2 in /usr/share/miniconda/envs/test/lib/python3.8/site-packages (from scikit-learn) (1.9.1)
Installing collected packages: threadpoolctl, joblib, scikit-learn
Successfully installed joblib-1.1.0 scikit-learn-1.1.2 threadpoolctl-3.1.0
Cloning into 'sf-model-lib'...
remote: Enumerating objects: 24, done.
remote: Counting objects:  11% (1/9)
remote: Counting objects:  22% (2/9)
remote: Counting objects:  33% (3/9)
remote: Counting objects:  44% (4/9)
remote: Counting objects:  55% (5/9)
remote: Counting objects:  66% (6/9)
remote: Counting objects:  77% (7/9)
remote: Counting objects:  88% (8/9)
remote: Counting objects: 100% (9/9)
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects:  12% (1/8)
remote: Compressing objects:  25% (2/8)
remote: Compressing objects:  37% (3/8)
remote: Compressing objects:  50% (4/8)
remote: Compressing objects:  62% (5/8)
remote: Compressing objects:  75% (6/8)
remote: Compressing objects:  87% (7/8)
remote: Compressing objects: 100% (8/8)
remote: Compressing objects: 100% (8/8), done.
Receiving objects:   4% (1/24)
Receiving objects:   8% (2/24)
Receiving objects:  12% (3/24)
Receiving objects:  16% (4/24)
Receiving objects:  20% (5/24)
Receiving objects:  25% (6/24)
Receiving objects:  29% (7/24)
Receiving objects:  33% (8/24)
Receiving objects:  37% (9/24)
Receiving objects:  41% (10/24)
Receiving objects:  45% (11/24)
Receiving objects:  50% (12/24)
Receiving objects:  50% (12/24), 42.41 MiB | 42.40 MiB/s
Receiving objects:  54% (13/24), 64.46 MiB | 42.97 MiB/s
Receiving objects:  54% (13/24), 87.21 MiB | 43.60 MiB/s
Receiving objects:  54% (13/24), 133.03 MiB | 44.34 MiB/s
Receiving objects:  58% (14/24), 156.74 MiB | 44.78 MiB/s
Receiving objects:  62% (15/24), 156.74 MiB | 44.78 MiB/s
Receiving objects:  66% (16/24), 156.74 MiB | 44.78 MiB/s
Receiving objects:  70% (17/24), 156.74 MiB | 44.78 MiB/s
Receiving objects:  75% (18/24), 156.74 MiB | 44.78 MiB/s
Receiving objects:  79% (19/24), 156.74 MiB | 44.78 MiB/s
Receiving objects:  83% (20/24), 156.74 MiB | 44.78 MiB/s
Receiving objects:  87% (21/24), 156.74 MiB | 44.78 MiB/s
Receiving objects:  91% (22/24), 156.74 MiB | 44.78 MiB/s
remote: Total 24 (delta 2), reused 7 (delta 1), pack-reused 15
Receiving objects:  95% (23/24), 156.74 MiB | 44.78 MiB/s
Receiving objects: 100% (24/24), 156.74 MiB | 44.78 MiB/s
Receiving objects: 100% (24/24), 167.24 MiB | 44.81 MiB/s, done.
Resolving deltas:   0% (0/6)
Resolving deltas:  16% (1/6)
Resolving deltas:  33% (2/6)
Resolving deltas:  50% (3/6)
Resolving deltas:  66% (4/6)
Resolving deltas:  83% (5/6)
Resolving deltas: 100% (6/6)
Resolving deltas: 100% (6/6), done.
Updating files:  87% (7/8)
Updating files: 100% (8/8)
Updating files: 100% (8/8), done.

Also, since the model weights have been stored in chunks, they need to be recombined first:

import importlib
h5tools = importlib.import_module("sf-model-lib.h5tools")
DIR_WEIGHTS = "sf-model-lib/diffusion"
fname_in = [f"{DIR_WEIGHTS}/model_weights_part{j}.h5" for j in [0, 1]]
h5tools.combine(fname_in, fname_out=f"{DIR_WEIGHTS}/model_weights.h5")

Note that to use diffusive_size_factor_AI, Scikit-learn and Tensorflow must be installed. Import necessary packages and the AI model:

import inspect
import os
import warnings

import h5py
import numpy as np
import porespy as ps
import scipy as sp
from matplotlib import pyplot as plt
from sklearn.metrics import r2_score

ps.visualization.set_mpl_style()
warnings.filterwarnings("ignore")
inspect.signature(ps.networks.diffusive_size_factor_AI)
<Signature (regions, throat_conns, model, g_train, voxel_size=1)>

model , g_train#

Import AI model and training data from the downloaded folder:

path = "./sf-model-lib/diffusion"
path_train = os.path.join(path, 'g_train_original.hdf5')
path_weights = os.path.join(path, 'model_weights.h5')
g_train = h5py.File(path_train, 'r')['g_train'][()]
model = ps.networks.create_model()
model.load_weights(path_weights)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In [4], line 5
      3 path_weights = os.path.join(path, 'model_weights.h5')
      4 g_train = h5py.File(path_train, 'r')['g_train'][()]
----> 5 model = ps.networks.create_model()
      6 model.load_weights(path_weights)

File ~/work/porespy/porespy/porespy/networks/_size_factors.py:408, in create_model()
    400 def create_model():
    401     '''
    402     Returns
    403     -------
   (...)
    406 
    407     '''
--> 408     from tensorflow.keras.optimizers import Adam
    409     model = _resnet3d()
    410     model.compile(loss='mse', optimizer=Adam(lr=1e-4), metrics=['mse'])

File /usr/share/miniconda/envs/test/lib/python3.8/site-packages/tensorflow/__init__.py:37
     34 import sys as _sys
     35 import typing as _typing
---> 37 from tensorflow.python.tools import module_util as _module_util
     38 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
     40 # Make sure code inside the TensorFlow codebase can use tf2.enabled() at import.

ModuleNotFoundError: No module named 'tensorflow.python.tools'

regions#

We can create a 3D image using PoreSpy’s poly_disperese_spheres generator and segment the image using snow_partitioning method. Note that find_conns method returns the connections in the segmented region. The order of values in conns is similar to the network extraction conns. Therefore, the region with label=1 in the segmented image is mapped to indice 0 in conns.

np.random.seed(17)
shape = [120, 120, 120]
dist = sp.stats.norm(loc=7, scale=5)
im = ps.generators.polydisperse_spheres(shape=shape,
                                        porosity=0.7,
                                        dist=dist,
                                        r_min=7)
results = ps.filters.snow_partitioning(im=im.astype(bool))
regions = results['regions']
fig, ax = plt.subplots(1, 1, figsize=[4, 4])
ax.imshow(regions[:, :, 20], origin='lower', interpolation='none')
ax.axis(False);
../../../_images/4b9083512ea2ec587e132edd021e23767b6dfbb68484c6b79764166c6edcb963.svg

throat_conns#

PoreSpy’s diffusive_size_factor_AI method takes in the segmented image, model, training ground truth values, and the conncetions of regions in the segmented image (throat conns). In this example we have created an image with voxel_size=1. For a different voxel size, the voxel_size argument needs to be passed to the method.

conns = ps.networks.find_conns(regions)
size_factors = ps.networks.diffusive_size_factor_AI(regions,
                                                    model=model,
                                                    g_train=g_train,
                                                    throat_conns=conns)

Compare with finite difference#

Assuming a diffusivity of 1, the diffusive conductance of the conduits will be equal to their diffusive size factors. Now let’s compare the AI-based diffusive conductances with the conductance values calculated by finite difference method. The finite difference method results are found using the steps explained here.

fname = os.path.join(path, 'g_finite_difference120-phi7.hdf5')
g_FD = h5py.File(fname, 'r')['g_finite_difference'][()]
g_AI = size_factors
plt.figure(figsize=[4, 4])
plt.xlim([0, 80])
plt.ylim([0, 80])
plt.plot(g_FD, g_AI, '*', [0, 80], [0, 80], 'r')
plt.xlabel('g reference')
plt.ylabel('g prediction')
r2 = r2_score(g_FD, g_AI)
print(f"The R^2 prediction accuracy is {r2:.3}")
The R^2 prediction accuracy is 0.952
../../../_images/5f62c8f7eb8460c94b75a2ce61ecd60fe9d2f97215101823ca4a0ce49bb8eb53.svg

Note on runtime: A larger part of AI_size_factors runtime is related to extracting the pairs of conduits, which is the common step required for both AI and finite difference method. Once the data is prepared, AI Prediction on the tensor takes a smaller amount of time in contrast to finite difference method, as it was shown here.

Apply on one conduit#

PoreSpy’s diffusive_size_factor_AI method can take in an image of a pair of regions. An image of a conduit and its diffusive size factor was saved for comparison:

fname = os.path.join(path, 'pair.hdf5')
pair_in = h5py.File(fname, 'r')
im_pair = pair_in['pair'][()]
conns = ps.networks.find_conns(im_pair)
sf_FD = pair_in['size_factor'][()]
sf_AI = ps.networks.diffusive_size_factor_AI(im_pair,
                                             model=model,
                                             g_train=g_train,
                                             throat_conns=conns)
print(f"Diffusive size factor from FD: {sf_FD[0]:.2f}, and AI: {sf_AI[0]:.2f}")
Diffusive size factor from FD: 9.87, and AI: 9.91