{"id":19307420,"url":"https://github.com/artefactory/train-fastai-custom-aiplatform","last_synced_at":"2026-06-20T07:31:45.989Z","repository":{"id":103035318,"uuid":"327920618","full_name":"artefactory/train-fastai-custom-aiplatform","owner":"artefactory","description":"Train a Fastai model using a custom container on AI Platform","archived":false,"fork":false,"pushed_at":"2021-03-25T20:00:33.000Z","size":151,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-06-16T00:23:07.053Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/artefactory.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-01-08T14:09:35.000Z","updated_at":"2021-11-17T14:29:50.000Z","dependencies_parsed_at":"2023-03-13T15:09:45.524Z","dependency_job_id":null,"html_url":"https://github.com/artefactory/train-fastai-custom-aiplatform","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/artefactory/train-fastai-custom-aiplatform","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artefactory%2Ftrain-fastai-custom-aiplatform","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artefactory%2Ftrain-fastai-custom-aiplatform/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artefactory%2Ftrain-fastai-custom-aiplatform/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artefactory%2Ftrain-fastai-custom-aiplatform/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/artefactory","download_url":"https://codeload.github.com/artefactory/train-fastai-custom-aiplatform/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artefactory%2Ftrain-fastai-custom-aiplatform/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34561766,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-20T02:00:06.407Z","response_time":98,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T00:10:53.847Z","updated_at":"2026-06-20T07:31:45.967Z","avatar_url":"https://github.com/artefactory.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Train a FastAI model using a custom container hosted in GCP AI Platform\n- [Introduction](#introduction)\n- [Pre-requisites](#pre-requisites)\n- [Set-up your environment](#set-up-your-environment)\n- [Train the model](#train-the-model)\n\n\n# Introduction\nEver wanted to quickly train a text classifier without having to worry about \"which framework should I use ?\" or \"should I use a VM to run on GPU?\" ? Well, this repo should help you.\n\nThanks to AI Platform, a fully-managed cost-effective service provided by Google Cloud Platform, you can now train ML models directly on cloud using any framework you want by deploying a custom Docker container hosting your training code on GCR, and calling it from your machine with a simple command. \n\nYou will find in this repo everything you need to easily build and deploy a custom Docker container to train a text-classifier on AI Platform with FastAI, a PyTorch wrapper that allows you to train powerful models with only few samples thanks to transfer learning. Indeed, FastAI text classifiers are created using ULM FiT method, which allows you to create classifiers using pre-trained Language models (you can find more details [here](https://towardsdatascience.com/understanding-language-modelling-nlp-part-1-ulmfit-b557a63a672b#:~:text=Universal%20Language%20Model%20FIne%2DTuning,by%20XLNet%20in%20text%20classification%5D.), but don't worry, you can follow this tutorial without knowing anything about language models).\n\nBy simply modifying few lines of code, you'll be able to create a text classifier adapted to your particular use case, whether you need to assign sentiment scores to movie reviews of a french website, or predict if a Japanese Instagram publication is about sport or food. \n\n\n# Pre-requisites\nTo follow this tutorial, be sure to possess the following elements:\n- A GCP environment, with access to Container Registry, Cloud Storage and AI Platform\n- A labelled dataset to train your model on, where the labels to predict are one-hot encoded (i.e there is a column for every label, that contains True/False or 0/1 for each texts).\nFor example, we'll use in this tutorial a dataset that has three columns : text_clean | categ1 | categ2, where \"text_clean\" contains the text to train the model on, and where \"categ1\" and \"categ2\" are filled with 0 or 1 to indicates if a text belongs to one of these categories. A sample is available in \"sample\" folder.\n- A pre-trained universal language model adapted to your target language if you're working on another language than English. One for French, Korean, Japanese and Chinese is available on the Google Drive (link below in the ReadMe). This pre-trained LM is composed of a weights.pth file, a vocab.pkl file and a spm.model file (the last one is used by the sentencepiece tokenizer)\n- (Optional) A VM or your personal computer with a GPU and enough RAM to build the Docker Image, which is not necessary but will allow you to test if everything is running OK before pushing your container to AI Platform. \nWe’ll consider here that you can access a Google VM with GPU enabled, which can be easily created using a ready-to-use Deep Learning VM from Google’s Market Place.\n\n# Set-up your environment\nThe first thing you’ll have to do will be to setup your working environment:\n- Install Docker and nvidia-docker on your VM. You can find how to install them here:\n  - https://docs.docker.com/engine/install/ubuntu/ to install docker\n  - https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#docker to install nvidia-docker\n- Install and initiate Google Cloud SDK https://cloud.google.com/sdk/docs/install#linux\n- Allow yourself to run Docker without using sudo\n\u003e ```python\n\u003e sudo usermod -a -G docker ${USER}\n\u003e ```\n- Configure docker with gcloud\n\u003e ```python\n\u003e gcloud auth configure-docker\n\u003e ```\n\n- Create a bucket on GCS with the same architecture as in the following drive, and upload your files in it:\n  https://drive.google.com/drive/folders/1JHfan6SFOXz5X0h49GOdORQQI3-rjpJX?usp=sharing.\n\n  You will find on this Drive all the files that are necessary to train a classifier in 5 languages (English, French, Korean, Japanese or Chinese):\n  - A labelled dataset to train your classifier (that can be replaced by your own)\n  - A pretrained language model (forward or backward may be available) defined by four files:\n    - A vocab.pkl file corresponding to the vocabulary of the LM\n    - A config.json file where the configuration of the model is encoded\n    - A weights.pth file containing the weights of the model\n    - A spm.model file corresponding to the sentencepiece tokenizer used by the model\n  You will be able to specify if you want to use a backward model to train your classifier if it's available (which is usually more efficient). English LM being already handled by Fastai, no pretrained LM is necessary for this language.\n\nIf your architecture in your bucket is different than the Drive's, you can modify the scheme of your files path in trainer/fastai_config.py\n\n- Define global variables\n  - BUCKET_NAME: The name of your bucket in GCS (e.g \"my_bucket\")\n  - PROJECT_ID: The name of your GCP Project, accessible by doing:\n  \u003e ```python\n  \u003e gcloud config list project --format \"value(core.project)\"\n  \u003e ```\n  - REGION: The region the training will operate in. Be sure to choose one with GPUs available (e.g \"europe-west1\" for Europe)\n  - IMAGE_REPO_NAME: The name of the folder where your containers will be stored in Container Registry, (e.g \"fastai_gpu_container\")\n  - IMAGE_TAG: The tag name you want to give to your future container (e.g \"french_training\")\n  - IMAGE_URI: The URI to your future container:\n  \u003e ```python\n  \u003e export IMAGE_URI=gcr.io/$PROJECT_ID/$IMAGE_REPO_NAME:$IMAGE_TAG\n  \u003e ```\n  - MODEL_DIR: The directory in your bucket that will store your trained model (e.g \"models\")\n  - JOB_NAME: An AI Platform job name (e.g \"fastai_gpu_french_job1\")\n\n\n# Train the model\n## Copy the repo on your VM\n\u003e ```python\n\u003e $ git clone https://github.com/artefactory/train-fastai-custom-aiplatform.git\n\u003e $ cd train-fastai-custom-aiplatform\n\u003e ```\n\n\n## Take a look at the configuration file and make necessary changes\nThe fastai_config.py file contains variables that will be useful to train your model, and the names of the files to fetch your model with. \n\nHere's the list of the training parameters variables:\n  - LABEL_COL_NAME: Name of the column containing the separated labels, that will be created from your one-hot encoded columns\n  - PREDICTION_COL_NAME: Name of the column that will store the predictions during inference on test dataset (to assess model's performances)\n  - RANDOM_STATE: Random state of train/test/valid split of dataframe\n  - VAL_SIZE: Proportion of samples to be used for validation\n  - TEST_SIZE: Proportion of samples to be used for testing\n \nThe other variables refer to the names and locations of all the files to be used during the training. If you use the same architecture and same file names as in the provided Google Drive, you shouldn't have to modify anything regarding that part.\n\n\n## Build image.\nThe image is built based on Nvidia Cuda 10.2-devel image, which should automatically be pulled when trying to build.\n\u003e ```python\n\u003e docker build -f Dockerfile -t $IMAGE_URI ./\n\u003e ```\nYou’ll get a success message if the image is built correctly, or an error message otherwise. Be really careful of the python, pip, pytorch and nvidia version you choose, because incompatibilities will cause the package you want to install to fail without any specific reason. There shouldn't be any problem regarding that part though.\nBuild also may fail due to a lack of RAM so be sure that the machine you're using can handle it. You shouldn't have any problem if you use Google's GPU-enabled VMs.\n\n\n## (Optional) Run the code in the container before pushing to ensure its well-behavior\nOnce your image is built, you can run the code in the container with basic parameters just to ensure everything works. Run this command and wait for it to complete before following this tutorial.\n\u003e ```python\n\u003e docker run --runtime=nvidia $IMAGE_URI --epochs 1 --bucket-name $BUCKET_NAME\n\u003e ```\n\nNote: We only specified here a few parameters:\n- --runtime=nvidia: We ask our container to use the GPU (if your machine has one obviously)\n- --epochs 1: We train our model on only 1 epoch, to minimize the time needed to train (we don't aim performances, we only want to see if the training goes well)\n- --bucket-name $BUCKET_NAME: We specifiy the name of the bucket to get the training files from\n\n\n## Push your image to GCR\nAfter running the image and ensured it worked, you can push it to GCR using the following command:\n\u003e ```python\n\u003e docker push $IMAGE_URI\n\u003e ```\n\n\n## Run AI Platform job\nNow that your custom container is in GCR, everything is ready to go. You can start the training on AI Platform using a simple command, from any machine you want, assuming that it has access to your GCP project and to AI Platform.\nYou just have to run the following command for everything to start running:\n\n\u003e ```python\n\u003e gcloud ai-platform jobs submit training $JOB_NAME \\\n\u003e --scale-tier BASIC_GPU \\\n\u003e --region $REGION \\\n\u003e --master-image-uri $IMAGE_URI \\\n\u003e -- \\\n\u003e --lang=fr \\\n\u003e --epochs=10 \\\n\u003e --bucket-name=$BUCKET_NAME \\\n\u003e --model-dir=$MODEL_DIR\n\u003e ```\n\nWe specified a few parameters here:\n  - The name of the job\n  - The type of scaling you want, BASIC_GPU here\n  - The region you want your running machine to be\n  - The URI of your custom container stored in GCR, that contains the training code\n  - Training arguments, that have been defined in the args_getter.py file\n  \nThese training arguments are not all necessary if they have a default value, but allow you to customize the training of your model. They can be specified after the \"-- \\\" when running your command:\n  - --bucket-name: The name of your bucket on GCS\n  - --model-dir: The name of the directory to store your trained model on GCS\n  - --model-filename: The name given to the trained model in GCS (default = \"fastai_model.pth\")\n  - --score-filename: The name given to the json file containing your model performances in GCS (default = \"label_scores.json\")\n  - --dataset-filename: The name of your labelled dataset to be retrieved in GCS (default = \"labelled_dataset.csv\")\n  - --lang: The language of your dataset, to choose among \"en\", \"fr\", \"ja\", \"ko\" and \"zh\" (default = \"fr\")\n  - --drop-mult: The value of the Drop-Multiplier to decrease risk of overfitting (default = 0.3)\n  - --batch-size: The size of text batches to be processed at the same time during model's fitting (default = 16)\n  - --epochs: The number of epochs to train your model during each cycle of fitting (default = 8)\n  - --bw: The type of pretrained LM you want to use (forward or backward). Specify '--bw' if you want to use a backward model, nothing otherwise (you can see in the GCS bucket if a backward LM is available for the language you want to work on)\n  - --monitored-metric: The metric to monitor to keep the best model during the training (default = \"valid_loss\")\n  - --prediction-threshold: The threshold from which we consider a prediction probability is positive (default = 0.3)\n  - --text-col: The name of the column containing the texts in your labelled dataset (default = \"text\")\n  - --label-delim: The delimiter to be used when separating the various labels (default = \",\")\n  - --label-list: List of the labels in your training dataset, separated by label_delim (default = \"categ1,categ2\")\n  - --other-label: The name of the label to be assigned when no other labeled has been assigned (default = \"other\")\n\n\nThe training might take some time depending on the training arguments you chose (especially the number of epochs)\n\n\n## Follow your model training\n\nTo follow the training of your model, you can go to AI Platform menu on GCP, select \"Jobs\", and click on the name of your job. You'll be able to view the logs, and some basic information about the job, like the input arguments file, or the % of use of the CPU/GPU.\n\nYou can also see the status of your training by running the command\n\u003e ```python\n\u003e gcloud ai-platform jobs describe $JOB_NAME\n\u003e ```\n\nThe evolution of the training itself will be displayed in the logs, where you'll be able to see the model fitting for each epoch.\n\nOnce the job is complete, your model will be available in your bucket, in the folder $MODEL_DIR, along with the performance metrics of your model for each label, stored in a JSON file, that you'll also be able to see in the logs.\n\n\n# References\n- [Getting started with AI Platform Training with custom containers](https://cloud.google.com/ai-platform/training/docs/custom-containers-training#submit_a_hyperparameter_tuning_job)\n- [Use the GPU within a Docker container](https://blog.roboflow.com/use-the-gpu-in-docker/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartefactory%2Ftrain-fastai-custom-aiplatform","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fartefactory%2Ftrain-fastai-custom-aiplatform","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartefactory%2Ftrain-fastai-custom-aiplatform/lists"}