{ "cells": [ { "cell_type": "markdown", "id": "f2ffe44a", "metadata": {}, "source": [ "# Hyperparameter Optimization in AutoMM\n", "\n", "[](https://colab.research.google.com/github/autogluon/autogluon/blob/stable/docs/tutorials/multimodal/advanced_topics/hyperparameter_optimization.ipynb)\n", "[](https://studiolab.sagemaker.aws/import/github/autogluon/autogluon/blob/stable/docs/tutorials/multimodal/advanced_topics/hyperparameter_optimization.ipynb)\n", "\n", "Hyperparameter optimization (HPO) is a method that helps solve the challenge of tuning hyperparameters of machine learning models. ML algorithms have multiple complex hyperparameters that generate an enormous search space, and the search space in deep learning methods is even larger than traditional ML algorithms. Tuning on a massive search space is a tough challenge, but AutoMM provides various options for you to guide the fitting process based on your domain knowledge and the constraint on computing resources.\n", "\n", "## Create Image Dataset\n", "\n", "In this tutorial, we are going to again use the subset of the Shopee-IET dataset from Kaggle for demonstration purpose. Each image contains a clothing item and the corresponding label specifies its clothing category. Our subset of the data contains the following possible labels: `BabyPants`, `BabyShirt`, `womencasualshoes`, `womenchiffontop`.\n", "\n", "We can load a dataset by downloading a url data automatically:" ] }, { "cell_type": "code", "execution_count": null, "id": "aa00faab-252f-44c9-b8f7-57131aa8251c", "metadata": { "tags": [ "remove-cell" ] }, "outputs": [], "source": [ "!pip install autogluon.multimodal\n" ] }, { "cell_type": "code", "execution_count": null, "id": "364d104f", "metadata": {}, "outputs": [], "source": [ "import warnings\n", "warnings.filterwarnings('ignore')\n", "from datetime import datetime\n", "\n", "from autogluon.multimodal.utils.misc import shopee_dataset\n", "download_dir = './ag_automm_tutorial_hpo'\n", "train_data, test_data = shopee_dataset(download_dir)\n", "train_data = train_data.sample(frac=0.5)\n", "print(train_data)" ] }, { "cell_type": "markdown", "id": "a8c1f74b", "metadata": {}, "source": [ "There are in total 400 data points in this dataset. The `image` column stores the path to the actual image, and the `label` column stands for the label class. \n", "\n", "\n", "## The Regular Model Fitting\n", "\n", "Recall that if we are to use the default settings predefined by Autogluon, we can simply fit the model using `MultiModalPredictor` with three lines of code:" ] }, { "cell_type": "code", "execution_count": null, "id": "c8c7e903", "metadata": {}, "outputs": [], "source": [ "from autogluon.multimodal import MultiModalPredictor\n", "predictor_regular = MultiModalPredictor(label=\"label\")\n", "start_time = datetime.now()\n", "predictor_regular.fit(\n", " train_data=train_data,\n", " hyperparameters = {\"model.timm_image.checkpoint_name\": \"ghostnet_100\"}\n", ")\n", "end_time = datetime.now()\n", "elapsed_seconds = (end_time - start_time).total_seconds()\n", "elapsed_min = divmod(elapsed_seconds, 60)\n", "print(\"Total fitting time: \", f\"{int(elapsed_min[0])}m{int(elapsed_min[1])}s\")" ] }, { "cell_type": "markdown", "id": "fa513a16", "metadata": {}, "source": [ "Let's check out the test accuracy of the fitted model:" ] }, { "cell_type": "code", "execution_count": null, "id": "0fed0f46", "metadata": {}, "outputs": [], "source": [ "scores = predictor_regular.evaluate(test_data, metrics=[\"accuracy\"])\n", "print('Top-1 test acc: %.3f' % scores[\"accuracy\"])" ] }, { "cell_type": "markdown", "id": "f3b185e2", "metadata": {}, "source": [ "## Use HPO During Model Fitting\n", "\n", "If you would like more control over the fitting process, you can specify various options for hyperparameter optimizations(HPO) in `MultiModalPredictor` by simply adding more options in `hyperparameter` and `hyperparameter_tune_kwargs`.\n", "\n", "There are a few options we can have in MultiModalPredictor. We use [Ray Tune](https://docs.ray.io/en/latest/tune/index.html) `tune` library in the backend, so we need to pass in a [Tune search space](https://docs.ray.io/en/latest/tune/api/search_space.html) or an [AutoGluon search space](https://auto.gluon.ai/stable/api/autogluon.common.space.html) which will be converted to Tune search space.\n", "\n", "1. Defining the search space of various `hyperparameter` values for the training of neural networks:\n", "\n", "