Create an Image Classification Model¶
- Tutorial Difficulty: ★☆☆☆☆
- 10 min read
- Languages: SQL (100%)
- File location: tutorial_en/thanosql_ml/classification/image_classification.ipynb
- References: (AI-Hub) Product image data, A ConvNet for the 2020s
Tutorial Introduction¶
Understanding Classification
Classification is a type of Machine Learning that predicts which category(Category or Class) the target belongs to. For example, both binary classifications(used for classifying men or women) and multiple classifications(used to predict animal species such as dogs, cats, rabbits, etc.) are included in the classification tasks.
The image classification contest(ImageNet) has been held since 2010. The winning model at the beginning of the competition showed 72% accuracy. In 2015, the ResNet model that won showed 96% accuracy and started to surpass human classification capabilities.
The human ability to classify the same data is estimated at about 95%.
Even though Data labeling is important for accurate image classification, methods of correcting the dataset using the weights of the pre-trained model are widely used. This method allows deep learning models training even with a relatively small number of data.
ThanoSQL provides a variety of pre-trained models and allows model creation using simple queries. With this, users can derive insights from images with difficult to quantify features with properly trained models and utilize them for various services.
The following are examples and applications of the ThanoSQL image classification model.
The ThanoSQL image classification model reduces the process of finding suitable categories for product registration in online sales services. You can categorize product images with a simple query. Users can save time compared to traditional image classification by focusing just on correcting some misclassified data.
Using the image classification model, you can roughly classify art works that would be otherwise difficult to classify due to their vague criteria, such as the feeling, technique, and suitable location of each work.
You can detect and classify defective products with scratches and damage from manufacturing plants.
You can also create a classification model based on the behavior of art enthusiasts to classify who is most likely to enjoy a particular piece of art. In other words, using only artwork images, you can create a model that predicts art preferences based on age, gender, place, and etc.
In This Tutorial
👉 Build an image classification model to classify more than 10,000 products using the Product Image dataset from AI-Hub, a data sharing platform. The model can be used for detection and identification in smart warehouses and unmanned stores.
Dataset consists total of 1,440,000 images. In this tutorial, you will use 1,800 training data and 200 test data to learn how to use ThanoSQL.
Tutorial Precautions
- The image classification model can be used to predict one target value(Target, Category) from one image.
- Both a column representing the image path and a column representing the target value of the image must exist.
- The base model of the corresponding image classification model(CONVNEXT) uses GPU. Depending on the size and the batch size of the model used, GPU memory may be insufficient. In this case, try using a smaller model or reducing the batch size of the model.
0. Prepare Dataset¶
As mentioned in the ThanoSQL Workspace, you must create an API token and run the query below to execute the query of ThanoSQL.
%load_ext thanosql
%thanosql API_TOKEN=<Issued_API_TOKEN>
Prepare Dataset¶
%%thanosql
GET THANOSQL DATASET product_image_data
OPTIONS (overwrite=True)
Success
Query Details
- "GET THANOSQL DATASET" downloads the specified dataset to the workspace.
- "OPTIONS" specifies the option values to be used for the GET THANOSQL DATASET clause.
- "overwrite": determines whether to overwrite a dataset if it already exists. If set as True, the old dataset is replaced with the new dataset (bool, optional, True|False, default: False)
%%thanosql
COPY product_image_train
OPTIONS (if_exists='replace')
FROM 'thanosql-dataset/product_image_data/product_image_train.csv'
Success
%%thanosql
COPY product_image_test
OPTIONS (if_exists='replace')
FROM 'thanosql-dataset/product_image_data/product_image_test.csv'
Success
Query Details
- "COPY" specifies the name of the dataset to be saved as a database table.
- "OPTIONS" specifies the option values to be used for the COPY clause.
- "if_exists": determines how the function should handle the case where the table already exists, it can either raise an error, append to the existing table, or replace the existing table (str, optional, 'fail'|'replace'|'append', default: 'fail')
1. Check Dataset¶
To create an image classification model, we use the product_image_train table from the ThanoSQL workspace database. To check the table's contents, run the following query.
%%thanosql
SELECT *
FROM product_image_train
LIMIT 5
image_path | div_l | div_m | div_s | div_n | comp_nm | img_prod_nm | multi | |
---|---|---|---|---|---|---|---|---|
0 | thanosql-dataset/product_image_data/product_im... | 유제품 | 요구르트 | 떠먹는 요구르트 | 떠먹는 요구르트 | 기타 | 토핑오트&애플시나몬 | False |
1 | thanosql-dataset/product_image_data/product_im... | 홈클린 | 위생용품 | 일반비누 | 일반비누 | 크리오 | 크리오)골드디비누 | True |
2 | thanosql-dataset/product_image_data/product_im... | 면류 | 용기면 | 국물용기라면 | 짬뽕라면 | 농심 | 농심오징어짬뽕컵67G | True |
3 | thanosql-dataset/product_image_data/product_im... | 디저트 | 디저트/베이커리 | 냉장디저트 | 냉장디저트 | Dole 코리아 | Dole후룻볼슬라이스복숭아198g | False |
4 | thanosql-dataset/product_image_data/product_im... | 주류 | 기타주류 | 칵테일 | 칵테일 | 롯데주류 | 순하리소다톡바나나355ML | True |
Understanding the Data Table
product_image_train table contains the following information.
- image_path: image file's path
- div_l: large classification of products
- div_m: middle classification of products
- div_s: subclassification of products
- div_n: detailed classification of products
- comp_nm: manufacturer
- img_prod_nm: product name (image)
- multi: whether image has multiple products
%%thanosql
PRINT IMAGE
AS
SELECT image_path
FROM product_image_train
LIMIT 5
/home/jovyan/thanosql-dataset/product_image_data/product_image/10246_00_s_21.png
/home/jovyan/thanosql-dataset/product_image_data/product_image/10180_60_m_9.png
/home/jovyan/thanosql-dataset/product_image_data/product_image/10101_30_m_17.png
/home/jovyan/thanosql-dataset/product_image_data/product_image/10242_60_s_12.png
/home/jovyan/thanosql-dataset/product_image_data/product_image/10054_30_m_13.png
2. Build an Image Classification Model¶
To create an image classification model with the name my_product_classifier using the product_image_train dataset, run the following query.
(Estimated duration of query execution: 5 min)
%%thanosql
BUILD MODEL my_product_classifier
USING ConvNeXt_Tiny
OPTIONS (
image_col='image_path',
label_col='div_l',
max_epochs=1,
overwrite=True
)
AS
SELECT *
FROM product_image_train
Success
Query Details
- "BUILD MODEL" creates and trains a model named my_product_classifier.
- "USING" specifies ConvNeXt_Tiny as the base model.
- "OPTIONS" specifies the option values used to create the model.
- "image_col": name of column containing the image path (str, default: 'image_path')
- "label_col": name of the column containing information about the target value (str, default: 'label')
- "max_epochs": number of times to train with the training dataset (int, optional, default: 3)
- "overwrite": determines whether to overwrite a model if it already exists. If set as True, the old model is replaced with the new model (bool, optional, True|False, default: False)
In this example, we set "max_epochs" to 1 to train the model quickly. In general, larger number of "max_epochs" increases performance of the inference at the cost of the computation time.
3. Predict¶
To use the image classification model created in the previous step for prediction of product_image_test, run the following query.
%%thanosql
PREDICT USING my_product_classifier
OPTIONS (
image_col='image_path',
result_col='predict_result'
)
AS
SELECT *
FROM product_image_test
image_path | div_l | div_m | div_s | div_n | comp_nm | img_prod_nm | multi | predict_result | |
---|---|---|---|---|---|---|---|---|---|
0 | thanosql-dataset/product_image_data/product_im... | 생활용품 | 위생용품 | 면봉 | 면봉 | 기타 | 콩맥스전자담배용크리닝면봉 | True | 생활용품 |
1 | thanosql-dataset/product_image_data/product_im... | 소스 | 장류 | 쌈장 | 쌈장 | 씨제이제일제당 | 해찬들고기전용쌈장450G | False | 소스 |
2 | thanosql-dataset/product_image_data/product_im... | 디저트 | 디저트/베이커리 | 냉장디저트 | 냉장디저트 | Dole 코리아 | Dole후룻볼슬라이스복숭아198g | False | 디저트 |
3 | thanosql-dataset/product_image_data/product_im... | 음료 | 기능성음료 | 한방음료 | 한방음료 | 광동제약 | 유어스광동어성초500ml | False | 음료 |
4 | thanosql-dataset/product_image_data/product_im... | 주류 | 기타주류 | 칵테일 | 칵테일 | 롯데주류 | 순하리소다톡바나나355ML | False | 주류 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
197 | thanosql-dataset/product_image_data/product_im... | 의약외품 | 기능성음료 | 숙취해소음료 | 숙취해소음료 | 동아제약 | 동아제약)가그린제로100ML | False | 의약외품 |
198 | thanosql-dataset/product_image_data/product_im... | 소스 | 장류 | 쌈장 | 쌈장 | 씨제이제일제당 | 해찬들고기전용쌈장450G | True | 소스 |
199 | thanosql-dataset/product_image_data/product_im... | 주류 | 기타주류 | 칵테일 | 칵테일 | 롯데주류 | 순하리소다톡바나나355ML | True | 주류 |
200 | thanosql-dataset/product_image_data/product_im... | 유제품 | 요구르트 | 떠먹는 요구르트 | 떠먹는 요구르트 | 기타 | 토핑오트&애플시나몬 | False | 유제품 |
201 | thanosql-dataset/product_image_data/product_im... | 주류 | 기타주류 | 칵테일 | 칵테일 | 롯데주류 | 순하리소다톡바나나355ML | True | 주류 |
202 rows × 9 columns
Query details
- "PREDICT USING" predicts the outcome using the my_product_classifier.
- "OPTIONS" specifies the option values to be used for prediction.
- "image_col": the name of the column where the path of the image used for prediction is stored (str, default: 'image_path')
- "result_col": the column that contains the predicted results (str, optional, default: 'predict_result')
4. In Conclusion¶
In this tutorial, we created an image classification model using the product image dataset. As this is a beginner-level tutorial, we focused on the process rather than accuracy. The image classification model can be improved in accuracy through fine tuning that is suitable for the user's needs. It is also possible to train the base model using your own data, or to vectorize and transform your data using a self-supervised model, and then distributing it using automated machine learning(AutoML) technique. Create your own model and provide competitive services by combining various unstructured data(image, audio, video, etc.) and structured data with ThanoSQL.
- How to Upload My Data to the ThanoSQL Workspace
- How to Create a Table Using My Data
- How to Upload My Model to the ThanoSQL Workspace
Inquiries About Deploying a Model for Your Own Service
If you have any difficulties creating your own model using ThanoSQL or applying it to your service, please feel free to contact us below😊
For inquiries regarding building an image classification model: contact@smartmind.team