Computer, What Is That Color?
Computer, What Is That Color?
Make a simple artificial intelligence that will classify a color based on how close it is to a subset of named colors.
Appropriate for ages 13+.
Allow 60 minutes to complete the module.
Important note: This module should be led by an instructor with basic Wolfram Language knowledge. If you would like to learn the language, please try this free online introduction. If you would like a Computational Thinking Initiative ambassador or volunteer to help you run an adventure, please contact us.
Appropriate for ages 13+.
Allow 60 minutes to complete the module.
Important note: This module should be led by an instructor with basic Wolfram Language knowledge. If you would like to learn the language, please try this free online introduction. If you would like a Computational Thinking Initiative ambassador or volunteer to help you run an adventure, please contact us.
Learning Objective
◼
Students will be able to build a classifier that identifies colors.
Computational Thinking Principles and Practices
◼
Interpreting a problem or idea in such a way that a computer can assist with it
◼
Treating the computer’s “misunderstandings” as proxies for one’s own
◼
Simulating things that are hard or impossible to do by performing real-world experiments
Standards Alignment
◼
AP Computer Science Principles:
◼
EK 5.1.1B: programs developed for creative expression, to satisfy personal curiosity or to create new knowledge may have visual, audible or tactile inputs and outputs
◼
EK 5.1.2C: incrementally adding tested program segments to correct working programs helps create large correct programs
Helpful Background
◼
Information on color space: https://en.wikipedia.org/wiki/Color_space
STARTING POINT
STARTING POINT
“Have you ever disagreed with someone about the name of a color? For example, would you call this color purple or pink?”
In[1]:=
Image@RGBColor[.7,.2,.5]
Out[1]=
Run the code above and then expand the size of the image so students can see the color when projected. Based on your projector, you may need to adjust the values of the color so that it falls more between purple and pink. Aim for a color that is hard for the class to identify.
All throughout this activity, you may want to include Image in your code as you project for students to see your work easily.
“How many of you think this is purple? How many of you think this is pink? How many of you are not sure?”
Take a survey of the class and list a tally of the groups on the board (i.e. the number of students that think the color is purple, pink or cannot decide).
“Today we are going to build an artificial intelligence that will analyze this color and decide what it should be called. The Wolfram Language includes symbols that represent common colors.”
Direct students to the Documentation Center guide page for colors (http://reference.wolfram.com/language/guide/Colors.html) and show them the list of named colors.
In[2]:=
Red
Out[2]=
“All of the named colors are given RGB values.”
Hover over the color above to show the RGB value. If needed, explain RGB to the class.
In[3]:=
RGBColor[1,0,0]
Out[3]=
“We can use RBGColor to make other colors that we can name. For instance, Purple is a mixture of a little Red and a little Blue.”
In[4]:=
RGBColor[.5,0,.5]
Out[4]=
“The artificial intelligence we build today will look at a random color and determine which of the named colors the random color is nearest to. It’s actually pretty simple.”
Students should set a variable to their random color so that it can be used later. They should know that the value of the variable will update each time RandomColor is run. Using RandomColor is helpful so that students do not try to make “normal” colors that are easier to identify.
In[5]:=
color=RandomColor[]
Out[5]=
In[6]:=
Nearest[{Red,Yellow,Green},color]
Out[6]=
“Try to identify other colors, and add colors to the list.”
“Let’s try the color from earlier. Remember, some of you thought it was purple, while others said pink. What does the computer say?”
In[7]:=
Nearest[{Purple,Pink},RGBColor[.7,.2,.5]]
Out[7]=
◼ Try to identify other colors. Are you surprised by the outputs?
◼ We only ask the computer to compare it to three other colors (i.e. Red, Yellow and Green). What would happen if you included more colors in the list?
◼ Is the color identifier more accurate if we give it more choices for what our unknown color could be?
CHECKPOINT
Check that the students have assigned a variable to their RandomColor and have attempted to identify it more accurately by adding more colors to the list.
“Let’s build a lot of colors that are hard to identify and see when they switch over from one color to another when they are identified.”
In[8]:=
Table[RGBColor[.5,g,.5],{g,0,1,0.2}]
Out[8]=
,,,,,
“This table builds a color that starts purple but then looks more and more green. Can you tell where these colors switch from more purple to more green?”
Take a survey to find out where the class thinks the colors become more purple or more green.
“Who thinks only the first color will be determined to be purple while the rest are green? Who thinks the first two colors will be purple? etc.”
In[9]:=
Nearest[{Purple,Green},Table[RGBColor[.5,g,.5],{g,0,1,0.2}]]
Out[9]=
,,,,,
“Let’s make this a little harder.”
In[10]:=
Table[RGBColor[r,g,.5],{g,0,1,0.2},{r,0,1,0.2}]
Out[10]=
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Add ArrayPlot to see the output in a more comprehensive way.
In[11]:=
ArrayPlot[Table[RGBColor[r,g,.5],{g,0,1,0.2},{r,0,1,0.2}]]
Out[11]=
“Everyone sketch a picture that shows where you think the line will be drawn between colors that are more green to where they would be more purple if we only classified them as green or purple.”
◼ What would you call this color? Green or purple? Point to a color in a student’s grid.
◼ If we only identified these colors as green and purple, where do you think the line would be where they cross over from green to purple?
CHECKPOINT
Check to see that students have made hypotheses about where the colors classify.
“Let’s classify these colors using our identifier from earlier, but only have it say if the colors are more green or more purple.”
In[12]:=
ArrayPlot[Table[Nearest[{Green,Purple},RGBColor[r,g,.5]],{g,0,1,0.2},{r,0,1,0.2}]]
Out[12]=
“Is that what you guessed? Change the list so that the identifier includes other colors as well.”
◼ Did you add other colors? What color names do you think you should include?
◼ Did you think that color near the top left would end up being more green than purple?
◼ How does your ArrayPlot change if you include Red? What if you add Pink? White? Black? LightBlue?
CHECKPOINT
Check to see that students have included other colors in their identification lists and that they understand that the more colors in the list, the more accurate the classifier will be.
Add some colors to your identifier and show your class.
“Great! Now I want the identifier to identify a color and respond with the name of the color. We can build rules to do this.”
Copy the Table portion of the code above and add rules that convert the colors to named strings. Use the saved random color from earlier to test the identifier.
Students might find it easier to understand the difference between the actual colors and their String names (e.g. Red and “Red”) if you use lowercase strings.
◼ Students with desktop computers may want the computer to actually say the word. Instruct them to use Speak. First will be needed to remove the output from the braces.
◼ Try converting the color name to another language. Use WordTranslation.
◼ Make your own colors using RGBColor and/or your own names for the colors to make fun comparisons. For example, ColorData[“Crayola”,“ColorRules”] will show you the interesting names Crayola has come up with for their colors.
◼ Talk to the class about the Stroop effect (https://en.wikipedia.org/wiki/Stroop_effect) and compare the results of a Stroop effect test on a human to what would happen if we trained a machine to take a Stroop effect test. Use TextRecognize if the goal is to read the text instead of state the color of the text, and use the Nearest function used in this adventure along with Rasterize to change the text into an image, DominantColors to pull the common color out of the image and another First to identify the color of the text. (i.e. Nearest[{Green“green”,Purple“purple”,White“white”,Pink“pink”,LightBlue“light blue”,Red“red”}, First[DominantColors[Rasterize[Style[“PINK”,Orange]]]]]). Is the computer slower when the font color and text do not match, the way a human would be slower? Use RepeatedTiming to check.
FINAL POINT
FINAL POINT
Ten minutes before the end of the module time.
Summarize
Summarize
Summarize what was done in the module and talk about findings.
Refer
Refer
Refer back to the learning objective and summarize how you have reached it.
Extend
Extend
Extend the module to the future. For example, "If you have time at home, try building an artificial intelligence that uses color to classify other characteristics of objects, like the flavors of a certain kind of candy that are correlated to color."
Possible Additional Relevant Functions
Possible Additional Relevant Functions
DominantColors • Rasterize • Style • RepeatedTiming • First • TextRecognize • ColorData • Replace • WordTranslation • Speak
Possible Pitfalls
Possible Pitfalls
◼
Please be aware that Speak does not yet work in the cloud. If students are doing this adventure in a cloud notebook, Speak will not work for them.