WOLFRAM NOTEBOOK

Generation of Random Bethlehem Stars

Anton Antonov
MathematicaForPrediction at WordPress
MathematicaForPrediction at GitHub
December 2020

Introduction

This document/notebook is inspired by the Mathematica Stack Exchange (MSE) question "Plotting the Star of Bethlehem", [MSE1]. That MSE question requests efficient and fast plotting of a certain mathematical function that (maybe) looks like the Star of Bethlehem, [Wk1]. Instead of doing what the author of the questions suggests, I decided to use a generative art program and workflows from three of most important Machine Learning (ML) sub-cultures: Latent Semantic Analysis, Recommendations, and Classification.
Although we discuss making of Bethlehem Star-like images, the ML workflows and corresponding code presented in this document/notebook have general applicability -- in many situations we have to make classifiers based on data that has to be “feature engineered” through pipeline of several types of ML transformative workflows and that feature engineering requires multiple iterations of re-examinations and tuning in order to achieve the set goals.
The document/notebook is structured as follows:
1
.
Target Bethlehem Star images
2
.
Simplistic approach
3
.
Elaborated approach outline
4
.
Sections that follow through elaborated approach outline:
4
.
1
.
Data generation
4
.
2
.
Feature extraction
4
.
3
.
Recommender creation
4
.
4
.
Classifier creation and utilization experiments
(This document/notebook is a “raw” chapter for the book "Simplified Machine Learning Workflows", [AAr3].)

Target images

Here are the images taken from [MSE1] that we consider to be “Bethlehem Stars” in this document/notebook:
In[]:=
imgStar1=Import["https://i.stack.imgur.com/qmmOw.png"];imgStar2=Import["https://i.stack.imgur.com/5gtsS.png"];Row[{imgStar1,Spacer[5],imgStar2}]
Out[]=
We notice that similar images can be obtained using the Wolfram Function Repository (WFR) function RandomMandala, [AAr1]. Here are a dozen examples:
In[]:=
SeedRandom[5];Multicolumn[Table[MandalaToWhiterImage@ResourceFunction["RandomMandala"]["RotationalSymmetryOrder"2,"NumberOfSeedElements"RandomInteger[{2,8}],"ConnectingFunction"FilledCurve@*BezierCurve],12],6,BackgroundBlack]
Out[]=

Simplistic approach

We can just generate a large enough set of mandalas and pick the ones we like.
More precisely we have the following steps:
1
.
We generate, say, 200 random mandalas using BlockRandom and keeping track of the random seeds
1
.
1
.
The mandalas are generated with rotational symmetry order 2 and filled Bezier curve connections.
2
.
We pick mandalas that look, more or less, like Bethlehem Stars
3
.
Add picked mandalas to the results list
4
.
If too few mandalas are in the results list go to 1.
Here are some mandalas generated with those steps:
In[]:=
lsStarReferenceSeeds=DeleteDuplicates@{697734,227488491,296515155601,328716690761,25979673846,48784395076,61082107304,63772596796,128581744446,194807926867,254647184786,271909611066,296515155601,575775702222,595562118302,663386458123,664847685618,680328164429,859482663706};Multicolumn[Table[BlockRandom[ResourceFunction["RandomMandala"]["RotationalSymmetryOrder"2,"NumberOfSeedElements"Automatic,"ConnectingFunction"FilledCurve@*BezierCurve,BackgroundBlack],RandomSeedingrs],{rs,lsStarReferenceSeeds}]/.GrayLevel[0.25`]White,6,Appearance"Horizontal",BackgroundBlack]
Out[]=
Remark: The plot above looks prettier in notebook converted with the resource function DarkMode.

Elaborated approach

Assume that we want to automate the simplistic approach described in the previous section.
One way to automate is to create a Machine Learning (ML) classifier that is capable of discerning which RandomMandala objects look like Bethlehem Star target images and which do not. With such a classifier we can write a function BethlehemMandala that applies the classifier on multiple results from RandomMandala and returns those mandalas that the classifier says are good.
Here are the steps of building the proposed classifier:
  • Generate a large enough Random Mandala Images Set (RMIS)
  • Create a feature extractor from a subset of RMIS
  • Assign features to all of RMIS
  • Make a recommender with the RMIS features and other image data (like pixel values)
  • Apply the RMIS recommender over the target Bethlehem Star images and determine and examine image sets that are:
  • the best recommendations
  • the worse recommendations
  • With the best and worse recommendations sets compose training data for classifier making
  • Train a classifier
  • Examine classifier application to (filtering of) random mandala images (both in RMIS and not in RMIS)
  • If the results are not satisfactory redo some or all of the steps above
  • Remark: If the results are not satisfactory we should consider using the obtained classifier at the data generation phase. (This is not done in this document/notebook.)
    Remark: The elaborated approach outline and flow chart below have general applicability, not just for generation of random images of a certain type.

    Flow chart

    Here is a flow chart that corresponds to the outline above:
    Out[]=
    A few observations for the flow chart follow:
  • The flow chart has a feature extraction block that shows that the feature extraction can be done in several ways.
  • The application of LSA is a type of feature extraction which this document/notebook uses.
  • If the results are not good enough the flow chart shows that the classifier can be used at the data generation phase.
  • If the results are not good enough there are several alternatives to redo or tune the ML algorithms.
  • Changing or tuning the recommender implies training a new classifier.
  • Changing or tuning the feature extraction implies making a new recommender and a new classifier.
  • Data generation and preparation

    In this section we generate random mandala graphics, transform them into images and corresponding vectors.Those image-vectors can be used to apply dimension reduction algorithms. (Other feature extraction algorithms can be applied over the images.)

    Generated data

    Generate large number of mandalas:
    Check the number of mandalas generated:
    Show a sample of the generated mandalas:

    Data preparation

    Convert the mandala graphics into images using appropriately large (or appropriately small) image sizes:
    Flatten each of the images into vectors:
    Remark: Below those vectors are called image-vectors.

    Feature extraction

    In this section we use the software monad LSAMon, [AA1, AAp1], to do dimension reduction over a subset of random mandala images.
    Remark: Other feature extraction methods can be used through the built-in functions FeatureExtraction and FeatureExtract.

    Dimension reduction

    Create an LSAMon object and extract image topics using Singular Value Decomposition (SVD) or Independent Component Analysis (ICA), [AAr2]:
    Show the importance coefficients for the topics (if SVD was used the plot would show the singular values):
    Show the interpretation of the extracted image topics:

    Approximation

    Pick a test image that is a mandala image or a target image and pre-process it:
    Find the representation of the test image with the chosen feature extractor (LSAMon object here):
    Show the interpretation of the found representation:

    Recommendations

    In this section we utilize the software monad SMRMon, [AAp3], to create a recommender for the random mandala images.
    Remark: Instead of a Sparse Matrix Recommender (SMR) object the built-in function Nearest can be used.
    Create SSparseMatrix object for all image-vectors:
    Normalize the rows of the image-vectors matrix:
    Get the LSA topics matrix:
    Find the image topics representation for each image-vector (assuming matH was computed with SVD or ICA):
    Here we create a recommender based on the images data (pixels) and extracted image topics (or other image features):
    Remark: Note the weights assigned to the pixels and the topics in the recommender object above. Those weights were derived by examining the recommendations results shown below.
    Here is the image we want to find most similar mandala images to -- the target image:
    Here is the profile of the target image:
    Using the target image profile here we compute the recommendation scores for all mandala images of the recommender:
    Here is a plot of the similarity scores:
    Here are the closest (nearest neighbor) mandala images:
    Here are the most distant mandala images:

    Classifier creation and utilization

    In this section we:
  • Prepare classifier data
  • Build and examine a classifier using the software monad ClCon, [AA2, AAp2], using appropriate training, testing, and validation data ratios
  • Build a classifier utilizing all training data
  • Generate Bethlehem Star mandalas by filtering mandala candidates with the classifier
  • As it was mentioned above we prepare the data to build classifiers with by:
  • Selecting top, highest scores recommendations and labeling them with True
  • Selecting bad, low score recommendations and labeling them with False
  • Using ClCon train a classifier and show its performance measures:
    Remark: We can re-run the ClCon workflow above several times until we obtain a classifier we want to use.
    Train a classifier with all prepared data:
    Get the classifier function from ClCon object:
    Here we generate Bethlehem Star mandalas using the classifier trained above:
    Generate Bethlehem Star mandala images utilizing the classifier (with a specified classifier probabilities threshold):
    Show unfiltered Bethlehem Star mandala candidates:
    Remark: Examine the probabilities in the image-probability associations above -- they show that the classifier is “working.“
    Here is another set generated Bethlehem Star mandalas using rotational symmetry order 4:
    Remark: Note that although a higher rotational symmetry order is used the highly scored results still seem relevant -- they have the features of the target Bethlehem Star images.

    References

    [Wk1] Wikipedia entry, Star of Bethlehem.

    Packages

    [AAr1] Anton Antonov, RandomMandala, (2019), Wolfram Function Repository.

    Code definitions

    Wolfram Cloud

    You are using a browser not supported by the Wolfram Cloud

    Supported browsers include recent versions of Chrome, Edge, Firefox and Safari.


    I understand and wish to continue anyway »

    You are using a browser not supported by the Wolfram Cloud. Supported browsers include recent versions of Chrome, Edge, Firefox and Safari.