materialsinnovation / pymks

Materials Knowledge System in Python
https://pymks.readthedocs.io/
Other
115 stars 76 forks source link

Working with existing micro structure images #560

Open me19d506 opened 3 years ago

me19d506 commented 3 years ago

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 commented 3 years ago

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(nstate=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 3 years ago

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 3 years ago

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, cf653e0048. 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 cf653e0048 if PyMKS then things should work and you should see the correlation.

wd15 commented 3 years ago

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

wd15 commented 3 years ago

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

me19d506 commented 3 years ago

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

On Tue, Aug 3, 2021 at 5:20 AM Daniel Wheeler @.***> wrote:

Closed #560 https://github.com/materialsinnovation/pymks/issues/560.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/materialsinnovation/pymks/issues/560#event-5101743417, or unsubscribe https://github.com/notifications/unsubscribe-auth/AU5WORLVH77O3F73HJX6ZQDT24VNVANCNFSM5AYQA5LA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

-- P V Divakar Raju Ph.D. Scholar Dept. of Mechanical Engineering Indian Institute of Technology-Tirupati India-517619

me19d506 commented 3 years ago

difficulty in stacking the multiple experimental microstructures.

Thank you

On Thu, Aug 19, 2021 at 2:31 PM ME19D506 P V Divakar Raju < @.***> wrote:

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

On Tue, Aug 3, 2021 at 5:20 AM Daniel Wheeler @.***> wrote:

Closed #560 https://github.com/materialsinnovation/pymks/issues/560.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/materialsinnovation/pymks/issues/560#event-5101743417, or unsubscribe https://github.com/notifications/unsubscribe-auth/AU5WORLVH77O3F73HJX6ZQDT24VNVANCNFSM5AYQA5LA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

-- P V Divakar Raju Ph.D. Scholar Dept. of Mechanical Engineering Indian Institute of Technology-Tirupati India-517619

-- P V Divakar Raju Ph.D. Scholar Dept. of Mechanical Engineering Indian Institute of Technology-Tirupati India-517619

wd15 commented 3 years ago

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 commented 3 years ago

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

On Fri, Aug 20, 2021, 8:36 PM Daniel Wheeler @.***> wrote:

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.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/materialsinnovation/pymks/issues/560#issuecomment-902760655, or unsubscribe https://github.com/notifications/unsubscribe-auth/AU5WORIWUIXKGNAUHP2UZJ3T5ZVRFANCNFSM5AYQA5LA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

wd15 commented 3 years ago

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

Does that help?