Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with existing micro structure images #560

Open
me19d506 opened this issue Jul 21, 2021 · 10 comments
Open

Working with existing micro structure images #560

me19d506 opened this issue Jul 21, 2021 · 10 comments
Labels

Comments

@me19d506
Copy link

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 ...!

@me19d506
Copy link
Author

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
Copy link
Contributor

wd15 commented Jul 22, 2021

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 wd15 added the question label Jul 22, 2021
@wd15
Copy link
Contributor

wd15 commented Jul 22, 2021

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
Copy link
Contributor

wd15 commented Jul 22, 2021

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

@wd15
Copy link
Contributor

wd15 commented Aug 2, 2021

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

@wd15 wd15 closed this as completed Aug 2, 2021
@me19d506
Copy link
Author

me19d506 commented Aug 19, 2021 via email

@me19d506
Copy link
Author

me19d506 commented Aug 19, 2021 via email

@wd15
Copy link
Contributor

wd15 commented Aug 20, 2021

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.

@me19d506
Copy link
Author

me19d506 commented Aug 20, 2021 via email

@wd15 wd15 reopened this Aug 20, 2021
@wd15
Copy link
Contributor

wd15 commented Aug 22, 2021

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants