I’ve been messing around with Keras, a ML library that makes it pretty easy to do AI experiments. I decided to try out image recognition, so I found a picture of Domino in a fez:
Then I wrote the following Python program which uses the existing ResNet50 image recognition library.
import numpy as np from keras.preprocessing import image from keras.applications import resnet50 # Load the image img = image.load_img('fez.jpg', target_size=(224, 224)) input = image.img_to_array(img) # Keras can process an array of images, but we're only passing one, so turn it into an array with one element. input = np.expand_dims(input, axis=0) # Normalize the RGB values into a 0-1 range, since ML algorithms get thrown off by big ranges. input = resnet50.preprocess_input(input) # Predict what's in the photo. model = resnet50.ResNet50() predictions = model.predict(input) predicted_classes = resnet50.decode_predictions(predictions, top=10) for _, name, liklihood in predicted_classes[0]: print("this is an image of {} {}".format(name, liklihood))
In addition to the obvious prereqs, you have to sudo apt-get install libhdf5-dev; pip install pillow certifi h5py
.
This prints:
this is an image of Bouvier_des_Flandres 0.4082675576210022 this is an image of briard 0.3710797429084778 this is an image of Newfoundland 0.10781265050172806 this is an image of giant_schnauzer 0.04042242094874382 this is an image of Scotch_terrier 0.038422249257564545 this is an image of komondor 0.012891216203570366 this is an image of Tibetan_terrier 0.0026010528672486544 this is an image of affenpinscher 0.0024157813750207424 this is an image of standard_poodle 0.0021669857669621706 this is an image of Kerry_blue_terrier 0.002110496861860156
It’s pretty solid on “it’s a dog.” I’m disappointed in the lack of fez-related IDs. However, I like some of these as possible breeds for Domino:



So, still needs some work. But not bad for less than 30 lines of code.