In this era, you can learn new techniques through online courses. I recently completed the Deep Learning specialization taught by Andrew Ng at Coursera, which has been a great learning experience. There are many explanations of mathematics, and the first model is completely made with python / numpy. As you continue the lesson, you may also use Tensorflow and Keras. It will be a style of learning to study and utilize the basis of theory.
However, fast.ai, which has been a hot topic for several years, is the exact opposite. This is a "let's do it first" approach. By trying it, it's exciting to see how easy it is to create an artificial intelligence model with the basic library of fast.ai.
Based on the chords from Lesson 1 of fast.ai, I created a model that can identify the guitar. Sorry for those who don't know much about guitars, but this example is likely to be of little use!
First, before preparing the model, we collected image data of four types of guitars.
The reason for choosing these four types is that the guitars have a distinctive body shape and head shape. Also, because they are very popular guitars, it was easy to collect images. Thank you for your Google search. I made folders for each guitar type and collected hundreds of them.
First, import fastai.vision. Set the path where you saved the data and define the class. The class name matches the folder name.
from fastai.vision import *
path = Path('data/guitars')
classes = ['gibson_les_paul', 'fender_telecaster', 'fender_stratocaster', 'explorer']
fast.ai provides a method to easily prepare the data. With one method, you can set the changes to be reflected in the image (for example, rotation for data augmentation), resizing, and the ratio of training and validation datasets. For more information, please refer to the API Details Page.
data = ImageDataBunch.from_folder(path, train=".", valid_pct=0.2,
ds_tfms=get_transforms(), size=224, num_workers=4, bs=16).normalize(imagenet_stats)
It is also possible to display data examples!
data.show_batch(rows=3, figsize=(7,8))
The learning itself is very easy. It can be trained based on the CNN model trained in two lines of code.
learn = cnn_learner(data, models.resnet34, metrics=error_rate)
learn.fit_one_cycle(4)
The result of learning is as follows. 90% system! Isn't it the State of the Art of the guitar identification engine? It may be the only model.
You can also see the learning problems and the results of the validation dataset.
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()
This is the Confusion Matrix! Telecaster and Stratocaster are certainly the most mixed. You can also view images that the model did not properly predict.
losses,idxs = interp.top_losses()
interp.plot_top_losses(9, figsize=(15,15))
Certainly there are many images that are difficult to judge. The image on the upper left is a guitar case in the first place. The far right in the middle is probably the Stratocaster, but the image is too cropped and difficult for the model. The image on the lower right is difficult for humans to judge. It's another guitar. That is, there is noise in the training and validation data. "Garbage in, Garbage out". As a future improvement, we will make the data cleaner and learn. You can also learn more by unfixing the parameters of the imported model, but of course it takes time.
Let's make a prediction with a model.
img = open_image(path/'gibson_les_paul'/'00000059.jpg')
pred_class,pred_idx,outputs = learn.predict(img)
This guitar.
The prediction result is: Category gibson_les_paul
: v: Pipong Pipong: v:
To use the model created in My App, you can export the model and make it API with simple python code.
Most of the above content is from Fast.ai Lesson 1. It's amazing how easy it is to get good results so quickly. Moreover, the lessons are free! it's recommended
Recommended Posts