[PYTHON] A model that identifies the guitar with fast.ai

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))

Images.png

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.

TrainingDone.PNG

You can also see the learning problems and the results of the validation dataset.


interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()

ConfusionMatrix.PNG

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))

Guitars.PNG

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.

download.png

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 recommendedClick this link for Fast.ai lessons!

Recommended Posts

A model that identifies the guitar with fast.ai
A story that struggled with the common set HTTP_PROXY = ~
A memo that I touched the Datastore with python
Calibrate the model with PyCaret
[Golang] A program that determines the turn with random numbers
Analyze the topic model of becoming a novelist with GensimPy3
Make a model iterator with PySide
Validate the learning model with Pylearn2
I made a program that automatically calculates the zodiac with tkinter
The story of making a module that skips mail with python
About the matter that torch summary can be really used when building a model with Pytorch
Tweet the weather forecast with a bot
[Python] A program that creates stairs with #
A story that visualizes the present of Qiita with Qiita API + Elasticsearch + Kibana
A class that hits the DMM API
Let's tune the model hyperparameters with scikit-learn!
The LXC Web Panel that can operate LXC with a browser was wonderful
Extract lines that match the conditions from a text file with python
Run the interaction model with Attention Seq2 Seq
Search the maze with the python A * algorithm
A typed world that begins with Python
A code that corrects the yoon / sokuon (sokuon)
Implement a model with state and behavior
[Python] A program that rounds the score
Try TensorFlow RNN with a basic model
I want to create an API that returns a model with a recursive relationship in the Django REST Framework
An article that just tries a little HTTP request with the curl command
Around the authentication of PyDrive2, a package that operates Google Drive with Python
The story of making a web application that records extensive reading with Django
Implementation of a model that predicts the exchange rate (dollar-yen rate) by machine learning
I tried to create a model with the sample of Amazon SageMaker Autopilot
[Python] Get the files in a folder with Python
Tweet the weather forecast with a bot Part 2
Find a position above the threshold with NumPy
A memo that made a graph animated with plotly
Exposing the DCGAN model for Cifar 10 with keras
Create a page that loads infinitely with python
Follow the AR marker with a 2-axis servo
Predict hot summers with a linear regression model
A program that searches for the same image
Solving the Lorenz 96 model with Julia and Python
Save the object to a file with pickle
Load the TensorFlow model file .pb with readNetFromTensorflow ().
Implement a discrete-time logistic regression model with stan
Create a translation tool with the Translate Toolkit
The story that fits in with pip installation
A shell program that displays the Fibonacci sequence
I made a twitter app that decodes the characters of Pricone with heroku (failure)
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
A story that failed when trying to remove the suffix from the string with rstrip
Verify the effect of leave as a countermeasure against the new coronavirus with the SEIR model
[Python] Leave only the elements that start with a specific character string in the array
It seems that a new lottery with a total score will start in the sports lottery,
Try to create a waveform (audio spectrum) that moves according to the sound with python
A script that keeps looking up until the URL is bookmarked with Hatena Bookmark
Created a fooling image for the caption generative model
A story that reduces the effort of operation / maintenance
[Python] A program that counts the number of valleys
Simulate a good Christmas date with a Python optimized model
How to create a submenu with the [Blender] plugin
Monitor the training model with TensorBord on Jupyter Notebook