Brendan Dawes
The Art of Form and Code

Training an image-model LoRA on a rented GPU, step by step

I wanted an image model to learn my own visual style so I could use it inside a generative artwork. The way to do that is a LoRA: a small add-on file, about 170MB in my case, that nudges a huge model towards a particular look. You train it on a handful of example images, each with a short caption.

Training needs a powerful NVIDIA graphics card. Rather than buy one, I rented one by the hour from Runpod for an afternoon. The whole run cost $3.37. This is how it went, from setting up SSH to deleting the machine.

Pick the right model first

A LoRA only works with the model family it was trained on. I'd already trained one on SDXL, but my artwork runs on Flux Schnell, and the two are built differently. The SDXL file would have loaded and done nothing. So I started again, on Flux Schnell.

What you need

1. Prepare the images and captions

I used 26 of my own renders. Each image has a text file with the same name, holding a one-line description that starts with a made-up trigger word, here dawes:

dawes, 3D render of a dense tangled mass of glossy ribbons and hair-like strands,
olive yellow, dark green and brown, on a pale blue-grey gradient background

Captions describe what's in the picture, not the style. The style is what the model is meant to learn.

2. Set up SSH, once

SSH is how your own computer talks to the rented one. It gives you a command line on the machine and lets you copy files back and forth. You set it up once and reuse it for every machine you rent.

An SSH key pair is two files: a private key that never leaves your computer, and a public key that you hand to Runpod.

ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_runpod -C "runpod"

Then print the public key and copy the whole line, which starts with ssh-ed25519:

cat ~/.ssh/id_ed25519_runpod.pub

In the Runpod console, open the Credentials page, select the SSH Public Keys tab, click Add SSH Key, paste the key, give it a name and save.

Every key on your account is added to every pod you rent, including ones that are already running.

Doing it from code instead? When you create a pod through the API you can pass your public key as an environment variable called PUBLIC_KEY. That's what I did, and it meant nothing had to be saved on my account.

3. Check stock and rent the GPU

I looked at what was in stock, then tried to rent. My first two choices, an L40S at $1.09 an hour and an RTX A6000 at $0.69, had both been taken in the few seconds between checking and renting. That happens a lot, so have a second and third choice ready.

The third try worked: an A100 with 80GB of memory, at $2.19 an hour, on Runpod's Secure Cloud. I used one of Runpod's official PyTorch templates, which already run an SSH service, so there was nothing to install. I added a 50GB container disk and a 100GB persistent volume mounted at /workspace, because the base model alone is tens of gigabytes. For file copying, the pod needs a public address and port 22 exposed, which the official templates provide.

4. Find the connection command and connect

Open the Pods page, select your pod, and open its Connect tab. There are two ways in:

A pod can say "running" before its connection address shows up. Give it about a minute. The address and port are different for every pod, and they change if you stop and restart it.

Paste the command, adding -i to point at your key if it isn't the default name:

ssh root@<POD_IP> -p <PORT> -i ~/.ssh/id_ed25519_runpod

The first time, SSH asks whether you trust the new machine. Type yes. You land at a prompt as root on the pod.

5. Upload and install

Copy the images up. Note the capital -P for the port, where plain ssh uses a lowercase -p:

scp -i ~/.ssh/id_ed25519_runpod -P <PORT> -r ./dataset root@<POD_IP>:/workspace/

I used ai-toolkit, a free training tool. On the pod:

git clone https://github.com/ostris/ai-toolkit.git
cd ai-toolkit && python3 -m venv venv && . venv/bin/activate
pip3 install -r requirements.txt

Install the matching PyTorch build first, using the exact versions in the project's README, because they change quickly. The whole install took about ten minutes.

6. The settings

The tool reads one configuration file. The settings that matter, in plain English:

The last two matter most. Saving a checkpoint every 250 steps is what let me pick the best moment afterwards.

7. Start the training

The base model is gated, so the pod needs your Hugging Face token. I sent it over SSH into a file only I could read, instead of putting it on a command line where other users on the machine could see it:

cat ~/.huggingface/token | ssh -i ~/.ssh/id_ed25519_runpod -p <PORT> root@<POD_IP> 'umask 077; cat > /root/.hf_token'

Then start the run detached. A job started in your SSH session stops when the session ends, so send its output to a log file and let it run in the background:

export HF_TOKEN=$(cat /root/.hf_token)
setsid nohup python run.py /workspace/my_lora.yaml > /workspace/train.log 2>&1 < /dev/null &

You can disconnect and check on it later:

tail -f /workspace/train.log

It first downloaded the base model, about five minutes, then ran at roughly two seconds per step.

8. Watch it learn

Every 250 steps the tool makes three test images from fixed prompts, so you can compare them like a flip book. Here's the same prompt, "a dense tangled mass of glossy ribbons", across the run:

The ribbons prompt at steps 0, 250, 500, 750, 1000, 1250 and 1500
Step 0 is plain Flux. By 500 the ribbons are thickening. By 1000 it's my style.

Here's a different prompt, a folded cream form, across the same steps:

A second test prompt, a folded cream form, across the same steps
A different prompt drifting away from the brief as training goes on.

The style was in by about step 1000, and everything after that mostly made things worse. I stopped the run at about step 1880.

9. Copy it home and delete the machine

The checkpoints were 172MB each. I copied them down and checked the sizes matched:

scp -i ~/.ssh/id_ed25519_runpod -P <PORT> "root@<POD_IP>:/workspace/output/my_lora/*.safetensors" ./checkpoints/

Then I terminated the pod from the Runpod console and checked the pod list was empty. A running pod bills by the hour whether you use it or not. A stopped pod keeps its disk and still bills a small storage fee, and a terminated pod is gone and bills nothing. Deleting wipes the disk, so copy everything off first.

10. Try it for real

I put the step-1000 file into my image tool's LoRA folder and wired it into my artwork's network as one more style layer. The same seed, with the LoRA off and then at increasing strengths:

Same seed with no LoRA, then the LoRA at 0.9 and 1.2, on three different seeds
Left to right: no LoRA, strength 0.9, strength 1.2.

What it cost

Summary

image
image
image