> For the complete documentation index, see [llms.txt](https://guide.petoi.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guide.petoi.com/extensible-modules/petoi-ai-vision-module/custom-petoi-vision-model/steps-on-how-to-get-started/step-2-train-model/train-model-locally-on-windows-advanced.md).

# Train model locally on Windows (Advanced)

Connect Google Colab to a local Jupyter runtime and GPU.

{% hint style="warning" %}
This setup is for advanced users. Use [cloud training](/extensible-modules/petoi-ai-vision-module/custom-petoi-vision-model/steps-on-how-to-get-started/step-2-train-model/train-model-on-the-cloud.md) for the simpler workflow.
{% endhint %}

{% hint style="info" %}
Reminder: All code and scripts for this project are executed in the cloud. Open the [Google Colab Notebook](https://colab.research.google.com/drive/1OPTGxdn03eJU69hFMEPp0hzXAPZfEDNZ?usp=sharing) and select Runtime > Run all to execute the setup.
{% endhint %}

Train in Google Colab while keeping datasets, models, and outputs on your computer.

This workflow connects Colab to a local Jupyter server on Windows. It also uses a dedicated Python environment.

Install 64-bit Python 3.13 or earlier before starting. Use the same Python installation throughout.

## Connect Colab to your computer

{% stepper %}
{% step %}

### Install JupyterLab

Install JupyterLab from the [Jupyter installation guide](https://jupyter.org/install).

![JupyterLab installation instructions](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/OO6bMYHq3GBT01iLNX0H/image)
{% endstep %}

{% step %}

### Start a local Jupyter server

Open PowerShell and run:

```shell
jupyter notebook --NotebookApp.allow_origin='https://colab.research.google.com' --port=8888 --NotebookApp.port_retries=0 --NotebookApp.allow_credentials=True
```

Keep this terminal open while using the local runtime.
{% endstep %}

{% step %}

### Fix a missing `jupyter` command

If PowerShell reports that `jupyter` is not recognized, add Python's `Scripts` folder to your user `Path`.

1. Press **Windows**, search for **Environment Variables**, then select **Edit the system environment variables**.
2. Select **Environment Variables**.
3. Under **User variables**, select **Path → Edit → New**.
4. Add your Python `Scripts` path. For example:

   ```
   C:\Users\<your-username>\AppData\Roaming\Python\Python313\Scripts
   ```
5. Select **OK** in every dialog.
6. Close and reopen PowerShell.

![Windows Environment Variables dialog](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/bsGW99WKEta3qtrcpEPY/image)
{% endstep %}

{% step %}

### Copy the local server URL

When the server starts, PowerShell displays a URL with a token. Copy the complete URL.

![Local Jupyter server URL in PowerShell](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/9mfEWxX5CEgJUYitciMv/image)

The address resembles `http://localhost:8888/?token=...`.

![Copying the local Jupyter server URL](https://docs-example.gitbook.io/petoi-ai-vision/~gitbook/image?url=https%3A%2F%2F1337386192-files.gitbook.io%2F%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FYCgatKxq7gJgRKRU9dFK%252Fuploads%252FtzAWhvuvU4g4lCizuXEQ%252Funknown.png%3Falt%3Dmedia%26token%3D0ac8ca3b-8563-4068-9110-c7d7036048ef\&width=768\&dpr=3\&quality=100\&sign=529c2219\&sv=2)
{% endstep %}

{% step %}

### Connect Google Colab

In Colab, select **Connect → Connect to local runtime**.

![Google Colab Connect to local runtime dialog](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/xkEGwNwFrwftbESqD83V/image)

Paste the local server URL, then select **Connect**.
{% endstep %}
{% endstepper %}

## Work with local files

Colab now uses your local runtime and storage. Your datasets, models, and training outputs remain on your local drive.

Create an empty project folder before training. This keeps generated files organized.

Colab’s **Files** pane may not immediately show local file changes. Use the local Jupyter URL to verify files and folders.

![Local Jupyter file browser](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/zRvvKNY6Y30HmI9XsL3M/image)

Open the project folder in Jupyter. It contains your datasets, models, charts, and training outputs.

![Project folder in the local Jupyter file browser](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/2rFdpCLO7xCqeL16AtQg/image)

Use 64-bit Python 3.12 or earlier. Download it from the [Python website](https://www.python.org/downloads/windows/).

![Python version requirement](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/apmDZ1y39wTSZF6kkZWw/image)

Locate the `Petoi_Workspace` folder.

![Empty Petoi Workspace folder in Windows Explorer](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/Mqvm884UcZWJPcokWCtJ/image)

### Create and activate the virtual environment

Right-click the folder, then select **Open in Terminal**.

Create the environment. Replace `3.10` with your installed Python version.

```shell
py -3.10 -m venv venv
```

![Creating a virtual environment from PowerShell](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/QYY9SpO2i68B4MOBXAPb/image)

Activate the environment:

```shell
venv\Scripts\activate
```

If PowerShell blocks activation, run `cmd`. Then run the activation command again.

![PowerShell error while activating the virtual environment](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/sPCaRkuuhq9lo3vfs9pl/image)

![Opening Command Prompt from PowerShell](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/meNhRviEcnbgMvD8mC3j/image)

### Install GPU dependencies

Update `pip` and install the Jupyter kernel:

```shell
python -m pip install --upgrade pip
```

```shell
pip install ipykernel
```

Install PyTorch with CUDA:

```shell
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
```

![PyTorch and CUDA packages installed in the virtual environment](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/XQmDD98jVgwEc9KIUIdL/image)

Verify that PyTorch detects your GPU:

```shell
python -c "import torch; print(torch.cuda.is_available())"
```

Continue only when the command returns `True`.

### Register and select the kernel

Register the virtual environment as a Jupyter kernel:

```shell
python -m ipykernel install --user --name=petoi-kernel --display-name "Python (Petoi Workspace)"
```

![Registering the Petoi Workspace Jupyter kernel](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/LYqPgXWX9mfD8AfbVJFw/image)

In Colab, select the **Python (Petoi Workspace)** kernel. Reconnect to the local runtime if prompted.

![Selecting a local kernel in Google Colab](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/cS1RbMwV683EdgVsggj7/image)

![Connecting Google Colab to a local kernel](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/yg4ySqKr2ye68DjOYFk4/image)

If the connection resets, connect to the local runtime again.

![Reconnecting to the local runtime in Google Colab](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/vBvsS1C1UqMFjy8aj1KS/image)

Rerun the GPU check. It should show your GPU.

![GPU available in the local Colab runtime](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/9cEmfv1bK6H4dBChHFdZ/image)

Before start training, remember to relocate your folder:

![](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/YOHuN32reT6KjjPRnqu1/image)

## Install Ultralytics

Run this in a Colab cell after connecting to the local runtime:

```python
%pip install ultralytics==8.4.21
```

Confirm that it is installed in the virtual environment:

```python
%pip show ultralytics
```

The displayed path must contain `venv\Lib\site-packages`.

![Ultralytics installed in the virtual environment](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/KlPKao7ohWsexMoXoeB0/image)

If the package is missing, activate the virtual environment and install it again.

![Checking the Ultralytics installation with pip show](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/ao5YH9KmmNC5BW9QKbJD/image)

Restart the Colab session after installing packages. Then reconnect to the local runtime.

![Restarting the Google Colab kernel](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/q6UUwWqhX8UU4vWNQ73P/image)

## Train with local files

Before training, confirm that the notebook uses your project folder. Training files save there.

If a restart changes the working folder, select the project folder again in Jupyter.

![Selecting the project folder before training](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/I63lfpWFoMoNs4nmo531/image)

## Export the final model

{% hint style="warning" %}
Windows does not support the required `full_integer_quant` export. Complete this final step in [cloud training](/extensible-modules/petoi-ai-vision-module/custom-petoi-vision-model/steps-on-how-to-get-started/step-2-train-model/train-model-on-the-cloud.md).
{% endhint %}

Exit the local environment before continuing in cloud training.

![Exiting the local environment before cloud export](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/9XehaBhWrgLaI47BfkvK/image)

Upload the model to Colab by dragging it from the **Files** pane.

![Preparing the cloud environment for model export](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/fpySha946WrVQIkklLHX/image)

Continue with the cloud export steps by running the code block.

![Running the cloud export command](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/w2TN6RSf1k6pm19ToVLE/image)

Run the remaining code blocks to complete the export.

The NPU must show 100% utilization during camera inference. Otherwise, verify the `full_integer_quant` export.

![NPU utilization during camera inference](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/fNAOwmIPNBRfAFKwc1zG/image)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://guide.petoi.com/extensible-modules/petoi-ai-vision-module/custom-petoi-vision-model/steps-on-how-to-get-started/step-2-train-model/train-model-locally-on-windows-advanced.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
