materialsinnovation/pymks

Working with existing micro structure images

me19d506 opened this issue · 10 comments

Hi,
I have series of images on my computer which is generated using a Random sequential algorithm (RSA). For this, images I have output variables. I need to train the principal components (input variables) of the images with ouput variables.

Kindly Help ...!

RVE30
RVE40
RVE50

Code:

import numpy as np
import cv2
from PIL import Image
import matplotlib.pyplot as plt
from sklearn.pipeline import Pipeline

from pymks import (
generate_checkerboard,
plot_microstructures,
PrimitiveTransformer,
TwoPointCorrelation
)
from sklearn.decomposition import PCA
#PYTEST_VALIDATE_IGNORE_OUTPUT

%matplotlib inline
%load_ext autoreload
%autoreload 2

im = Image.open('RVE30.png')
bw_im=im.convert('L')
plt.imshow(bw_im,cmap='gray')
plt.title("converted Black and white picture")
plt.show()

imarray = np.expand_dims(np.array(bw_im), axis=0)
imarray.shape
data = PrimitiveTransformer(n_state=2, min_=0.0, max_=1.0).transform(imarray)
data.shape
plot_microstructures(
data[0, :, :, 0],
data[0, :, :, 1],
titles=['First phase with ones', 'Second phase with ones'],
cmap='gray',
colorbar=False
)auto_correlation = TwoPointCorrelation(
periodic_boundary=True,
cutoff=25,
correlations=[(0,0)]
).transform(data)
auto_correlation.shape
plot_microstructures(
auto_correlation[0, :, :, 0],
titles=['Correlation [0, 0]'],
#showticks=True
)

wd15 commented

Thanks for leaving a question. What happens when you run the code? Do you get an error or unexpected results?

It would be helpful if you can explain what's going wrong.

wd15 commented

Having run the code, the only issue I can find is that nothing is being plotted. This is because plot_microstructures was orginally used inside the notebook only so doesn't return a figure object to show when not in the notebook. The latest version of PyMKS in master fixes this, cf653e0. You can also modify plot_microstrucutres in the code to have return fig at the end and then use fig.show() to actually show the figure. Anyway, if you use the following code

import numpy as np
import cv2
from PIL import Image
import matplotlib.pyplot as plt
from sklearn.pipeline import Pipeline

from pymks import (
    generate_checkerboard,
    plot_microstructures,
    PrimitiveTransformer,
    TwoPointCorrelation
)
from sklearn.decomposition import PCA
#PYTEST_VALIDATE_IGNORE_OUTPUT

im = Image.open('RVE30.png')
bw_im=im.convert('L')
# plt.imshow(bw_im,cmap='gray')
# plt.title("converted Black and white picture")
# plt.show()

imarray = np.expand_dims(np.array(bw_im), axis=0)
imarray.shape

data = PrimitiveTransformer(n_state=2, min_=0.0, max_=1.0).transform(imarray)
data.shape
fig = plot_microstructures(
    data[0, :, :, 0],
    data[0, :, :, 1],
    titles=['First phase with ones', 'Second phase with ones'],
    cmap='gray',
    colorbar=False
)
#fig.show()


auto_correlation = TwoPointCorrelation(
    periodic_boundary=True,
    cutoff=25,
    correlations=[(0,0)]
).transform(data)

auto_correlation.shape
fig = plot_microstructures(
    auto_correlation[0, :, :, 0],
    titles=['Correlation [0, 0]'],
)
fig.show()
input('stopped')
#showticks=Tr

with version cf653e0 if PyMKS then things should work and you should see the correlation.

wd15 commented

Do include the PCA in a meaningful way, we'll need many more samples.

wd15 commented

@me19d506 I'm closing this now. Please feel free to reopen if you need further help

wd15 commented

Dear Daniel Wheeler, in the example given at "http://pymks.org/en/latest/rst/notebooks/intro.html" takes one experimental microstructure and computes 2-p correlation and moves forward to a used synthetic generation of microstructures. I have difficulty stacking multiple images along the sample axis. could you please help in this regard? Thank you

Yes, PyMKS expects the images to be in a single array with the first axis as the axis indexing the image set. This is the same as Scikit-learn, which expects the first axis in the array to be the sample axis. This is the same as PyMKS where the images are being arranged as samples. So, for example, if you have three images you can use np.stack to pull them into one array.

from PIL import Image
import numpy as np
import glob

image1 = Image.open('image1.png')
image2 = Image.open('image2.png')
image3 = Image.open('image3.png')

images = np.stack([image1, image2, image3])
print(images.shape)

That will give a shape of (3, 654, 654, 3) with the first axis indexing over the stacked images. To do this for any number of images use images = np.stack([Image.open(x) for x in glob.glob('*.png')]).

Hope that helps.

wd15 commented

Thank you for the clarification. I have imported using dask_image. It would be highly helpful if you can provide some source for the following 1. Understanding principal component of image 2. Training with PCs of images Thanks again for the support

I'm not sure what you're asking for here. Can you be more specific? Is it the theory or the Python implementation that you're having problems with? There are examples on the PyMKS website for example that guide users. These consist of a number of steps

  • preprocessing the data
  • discretizing the data into channels
  • performing 2-point correlations
  • PCA
  • possibly expanding the features using polynomials
  • some sort of regression

Does that help?