[Resource] Training Using Google Colab

Want to use Faceswap in The Cloud? This is not directly supported by the Devs, but you may find community support here


Forum rules

Read the FAQs and search the forum before posting a new topic.

NB: The Devs do not directly support using Cloud based services, but you can find community support here.

Please mark any answers that fixed your problems so others can find the solutions.

User avatar
Korben
Posts: 17
Joined: Wed Aug 19, 2020 3:17 pm
Has thanked: 2 times
Been thanked: 3 times

Re: [Resource] Training Using Google Colab

Post by Korben »

manintan wrote: Mon Nov 23, 2020 11:52 am

This doesnt even work, many of the steps are outdated or wrong.

If anyone has gotten this to work, please post a working version with all steps and folder naming etc...

This is a cleaned up version of what I am using.

Code: Select all

# -*- coding: utf-8 -*-
"""# Setup Faceswap"""
#@title Set Time Zone to eastern
!rm /etc/localtime
!ln -s /usr/share/zoneinfo/America/New_York /etc/localtime
!date
#above is for HST, you can find yours in
#/usr/share/zoneinfo

#@title Mount Google Drive
from google.colab import drive
drive.mount('/content/drive', force_remount=True)

#@title Download training data
!rm -r face_a
!rm -r face_b
#under the root folder of my google drive ("My Drive") I created a /colab_files/faceswap/faces 
#folder that contains zipped face data.  Important the zip does not contain folders only the 
#faces and alignments files. 
#this copies the zip files from your google drive to the workspace while renaming them to a generic name
!cp "/content/drive/My Drive/colab_files/faceswap/faces/face_a_SomeName.zip" "./face_a.zip"
!cp "/content/drive/My Drive/colab_files/faceswap/faces/face_b_SomeName.zip" "./face_b.zip"
#unzip them in folders
!unzip face_a.zip -d face_a
!unzip face_b.zip -d face_b
#delete the zip files
!rm face_a.zip
!rm face_b.zip

#@title Grab the V1 version of Faceswap
!git clone --single-branch --branch r1.0 https://github.com/deepfakes/faceswap.git

#@title Install Tensorflow
#ignore the errors it still works
!pip install -r faceswap/requirements_nvidia.txt

#@title Copy configuration files
#if you copied config files from your pc to your google drive and want to use them uncomment this section.
#!cp "/content/drive/My Drive/colab_files/faceswap/config/train.ini" faceswap/config/
#!cp "/content/drive/My Drive/colab_files/faceswap/config/.faceswap" faceswap/config/
#!ls -lA faceswap/config/
#!cat faceswap/config/train.ini

"""# Run Training"""

#set variables start, will varry according to your trainer type etc...
num_iterations = "3000000"
save_every = "300"
save_model_every = "15000"
batch_num = "48"
num_gpus = "1"

trainer_type = "dfl-h128"

model_dir = "/content/drive/My Drive/colab_files/faceswap/models/YourModelFolder" 
alignments_file_a = "face_a/alignments.fsa"
alignments_file_b = "face_b/alignments.fsa"
#set variables end
!python3 faceswap/faceswap.py train \
  -A 'face_a' -ala '{alignments_file_a}' \
  -B 'face_b' -alb '{alignments_file_b}' \
  -m '{model_dir}' \
  -t '{trainer_type}' \
  -bs '{batch_num}' \
  -it '{num_iterations}' \
  -g '{num_gpus}' \
  -s '{save_every}' \
  -ss '{save_model_every}' \

Tags:

User avatar
police.bike
Posts: 22
Joined: Tue Jun 30, 2020 3:37 pm
Has thanked: 7 times
Been thanked: 5 times

Re: [Resource] Training Using Google Colab

Post by police.bike »

Update - Feb 20, 2021

I was able to get both python 3.7 & 3.8 working on google collab and train using latest versions. Pretty amazing ones !

Just use this as block 1 before running any steps within the Collab notebook

Code: Select all

!sudo apt install python3.7
!update-alternatives --install /usr/local/bin/python3 python3 /usr/bin/python3.6 1
!update-alternatives --install /usr/local/bin/python3 python3 /usr/bin/python3.7 2
!python --version
!sudo apt update && upgrade
!sudo apt install python3-pip
!python -m pip install --upgrade pip
!pip --version
!python --version
!pip --version

Change 3.7 to 3.8 if you need it. This will reset python runtime to 3.7.5 and will get latest faceswap editions working.

Have fun !!!


User avatar
oNafodo
Posts: 2
Joined: Wed Feb 24, 2021 6:34 am

Re: [Resource] Training Using Google Colab

Post by oNafodo »

I put that all in a notebook and attempted to run it, all the steps worked except the final one of actually running the training.

This is the final cell I used

Code: Select all

"""# Run Training"""

#set variables start, will varry according to your trainer type etc...
num_iterations = "3000000"
save_every = "300"
save_model_every = "15000"
batch_num = "48"
num_gpus = "1"

trainer_type = "dfl-h128"

model_dir = "/content/drive/My Drive/colab_files/faceswap/models/Olya" 
alignments_file_a = "face_a/yt1s.fsa"
alignments_file_b = "face_b/Olya.fsa"
#set variables end
!python3 faceswap/faceswap.py train \
  -A 'face_a' -ala '{alignments_file_a}' \
  -B 'face_b' -alb '{alignments_file_b}' \
  -m '{model_dir}' \
  -t '{trainer_type}' \
  -bs '{batch_num}' \
  -it '{num_iterations}' \
  -g '{num_gpus}' \
  -s '{save_every}' \
  -ss '{save_model_every}' \

But when I run it, this is what I get back.

Code: Select all

Setting Faceswap backend to NVIDIA
usage: faceswap.py [-h] {extract,train,convert,gui} ...

positional arguments:
  {extract,train,convert,gui}
    extract             Extract the faces from pictures or a video
    train               Train a model for the two faces A and B
    convert             Convert source pictures or video to a new one with the
                        face swapped
    gui                 Launch the Faceswap Graphical User Interface

optional arguments:
  -h, --help            show this help message and exit
faceswap.py: error: unrecognized arguments: 1

Is there something I am supposed to be changing that I didn't?


User avatar
hereforfun
Posts: 1
Joined: Wed Mar 03, 2021 6:11 pm

Re: [Resource] Training Using Google Colab

Post by hereforfun »

oNafodo wrote: Wed Feb 24, 2021 6:47 am

I put that all in a notebook and attempted to run it, all the steps worked except the final one of actually running the training.

But when I run it, this is what I get back.

Code: Select all

Setting Faceswap backend to NVIDIA
usage: faceswap.py [-h] {extract,train,convert,gui} ...

positional arguments:
  {extract,train,convert,gui}
    extract             Extract the faces from pictures or a video
    train               Train a model for the two faces A and B
    convert             Convert source pictures or video to a new one with the
                        face swapped
    gui                 Launch the Faceswap Graphical User Interface

optional arguments:
  -h, --help            show this help message and exit
faceswap.py: error: unrecognized arguments: 1

Had the same issue, just remove the line containing the -g flag.


User avatar
oNafodo
Posts: 2
Joined: Wed Feb 24, 2021 6:34 am

Re: [Resource] Training Using Google Colab

Post by oNafodo »

Thanks a million, that sorted it out.


User avatar
zachdidit
Posts: 1
Joined: Mon Mar 15, 2021 10:12 pm
Been thanked: 1 time

Re: [Resource] Training Using Google Colab

Post by zachdidit »

I've got a Collab working with the latest version of Faceswap. Also added a cell to keep the runtime open through idle.

Be sure to change folders, filenames, timezone, etc

https://colab.research.google.com/drive ... sp=sharing


User avatar
HoloByteus
Posts: 7
Joined: Mon Apr 12, 2021 11:31 pm

Re: [Resource] Training Using Google Colab

Post by HoloByteus »

Spent a couple days learning colab and using faceswap with it using the script in this post which saved a ton of time. I'm getting an error on training though which I can't seem to figure out. I have the alignment files in the face_a and b folders which are under /content but it doesn't seem to like the alignment arguments. Any ideas?

This is the error:

faceswap.py: error: unrecognized arguments: -ala face_a/alignments.fsa -alb face_b/alignments.fsa

And my training code.

Code: Select all

#set variables start
num_iterations = "120000"
save_every = "360"
save_model_every = "25000"
batch_num = "16"
num_gpus = "1"

trainer_type = "villain"

model_dir = "/content/drive/MyDrive/colab_files/faceswap/models/ModelAB"
alignments_file_a = "face_a/alignments.fsa"
alignments_file_b = "face_b/alignments.fsa"
timelapse_dir = "/content/drive/MyDrive/colab_files/faceswap/output/timelapse"
#set variables end

!python3 faceswap/faceswap.py train \
  -A 'face_a' -ala '{alignments_file_a}' \
  -B 'face_b' -alb '{alignments_file_b}' \
  -m '{model_dir}' \
  -t '{trainer_type}' \
  -bs '{batch_num}' \
  -it '{num_iterations}' \
  -s '{save_every}' \
  -ss '{save_model_every}' \
  -tia 'face_a' \
  -tib 'face_b' \
  -to '{timelapse_dir}'

I did notices one thing wierd. i have about 7000 images in face_a and scrolling though the list I can't see the alignment file, you see a ... and no way to expand it. I tried to upload a copy of the alignment file and move it to the folder and it fails. My storage isn't full. If I can't see it perhaps neither can the scripts. Is there a limit on number of files in a folder?

Last edited by HoloByteus on Tue Apr 13, 2021 12:57 am, edited 1 time in total.

User avatar
HoloByteus
Posts: 7
Joined: Mon Apr 12, 2021 11:31 pm

Re: [Resource] Training Using Google Colab

Post by HoloByteus »

Getting the following with Tensorflow install.

ERROR: albumentations 0.1.12 has requirement imgaug<0.2.7,>=0.2.5, but you'll have imgaug 0.2.9 which is incompatible.

WARNING: The following packages were previously imported in this runtime:
[psutil]
You must restart the runtime in order to use newly installed versions.


User avatar
sp13
Posts: 7
Joined: Sat Apr 10, 2021 12:20 am
Has thanked: 2 times
Been thanked: 2 times

Re: [Resource] Training Using Google Colab

Post by sp13 »

I get the same error message about albumentations but it doesn't seem to hurt anything so I just ignore it. You do have to press the button to restart the runtime though (or at least I always do it.)

One thing to note is that Colab comes with Tensorflow already installed and Google advises you not to install your own because theirs is optimized for their hardware. So you shouldn't install another if you can get away with it. However they currently use Tensorflow 2.4 and I use 2.2 locally, so I have to downgrade theirs or I can't use the models it produces.

About your previous question, training doesn't need alignment files anymore so you shouldn't set those options. The necessary data is stored right in the face set image files, provided that you have extracted them with a recent version of faceswap.


User avatar
HoloByteus
Posts: 7
Joined: Mon Apr 12, 2021 11:31 pm

Re: [Resource] Training Using Google Colab

Post by HoloByteus »

Thank you. I was thinking something with the colab required the alignment files but doh, yea I guess the notebook was made before that change so removed them from the training cell resolved that issue.

Figured out the psutil issue, I was running the GPU check (which includes a psutil install) before installing Tensorflow. Not doing that resolved it. Looks like Villian requires Tensorflow install and upon restort ... TRAINING NOW WORKS!

The master notebook needs a few updates. I also changed the Data transfer to point to the content directory and then just zip the extracted faces folders and have the unzip create the folders on colab. Bit easier zip process.

Code: Select all

#@title Download training data
!unzip "/content/drive/MyDrive/colab_files/faceswap/faces/face_a.zip" -d /content
!unzip "/content/drive/MyDrive/colab_files/faceswap/faces/face_b.zip" -d /content

Last edited by HoloByteus on Fri Apr 16, 2021 12:36 am, edited 2 times in total.

User avatar
sp13
Posts: 7
Joined: Sat Apr 10, 2021 12:20 am
Has thanked: 2 times
Been thanked: 2 times

Re: [Resource] Training Using Google Colab

Post by sp13 »

You should be able to unzip them without even copying them. I imagine something like

Code: Select all

!unzip "/content/drive/MyDrive/colab_files/faceswap/faces/face_a.zip" -d /content/

should work, though I use tar instead of zip.

Also for GPU check, I like to throw in

Code: Select all

!nvidia-smi

import tensorflow as tf
print("Tensorflow version: " + tf.__version__)

which will tell you which GPU and Tensorflow version you have.


User avatar
HoloByteus
Posts: 7
Joined: Mon Apr 12, 2021 11:31 pm

Re: [Resource] Training Using Google Colab

Post by HoloByteus »

Good point, will make the change to data transfer.

What are you using to bypass the colab interactive timeout? I have the javascript to click the connect button but I'm not a fan of how that keeps opening the cpu/gpu graphs. I found some javascript to click the comments button instead but would like to click the favorite Star instead for minimal visual impact. I haven't tested it yet but was wondering if this would work guessing the star for favorites would be labeled star.

Code: Select all

function ClickConnect(){

console.log("Working"); 
document.querySelector("#star > span").click() 
}
setInterval(ClickConnect,5000)

User avatar
sp13
Posts: 7
Joined: Sat Apr 10, 2021 12:20 am
Has thanked: 2 times
Been thanked: 2 times

Re: [Resource] Training Using Google Colab

Post by sp13 »

HoloByteus wrote: Thu Apr 15, 2021 2:17 am

What are you using to bypass the colab interactive timeout?

I use some Javascript snippet that I'm not sure actually works because I've gotten timed out before and sometimes get a "Are you still here?" captcha.

I ran across this stackoverflow the other day with lots of things to try, though i haven't done so
https://stackoverflow.com/questions/571 ... connecting


User avatar
HoloByteus
Posts: 7
Joined: Mon Apr 12, 2021 11:31 pm

Re: [Resource] Training Using Google Colab

Post by HoloByteus »

Yes, found that same thread and decided on the following since it has a method to enable/disable. Changed it to target the comments instead of connect and that seemed to work enough to pass the first level check for interactivity. Carried me all the way through the hours I had left of the 12hr timeout after jumping through all the hoops to get this running.

Code: Select all

var startClickConnect = function startClickConnect(){
    var clickConnect = function clickConnect(){
        console.log("Connnect Clicked - Start");
        document.querySelector("#comments > span").click();
        console.log("Connnect Clicked - End"); 
    };

    var intervalId = setInterval(clickConnect, 60000);

    var stopClickConnectHandler = function stopClickConnect() {
        console.log("Connnect Clicked Stopped - Start");
        clearInterval(intervalId);
        console.log("Connnect Clicked Stopped - End");
    };

    return stopClickConnectHandler;
};

var stopClickConnect = startClickConnect();

User avatar
sp13
Posts: 7
Joined: Sat Apr 10, 2021 12:20 am
Has thanked: 2 times
Been thanked: 2 times

Re: [Resource] Training Using Google Colab

Post by sp13 »

Another random tip that I don't think is in this thread:
If you put the following line somewhere after the git clone that installs faceswap

Code: Select all

!echo "{\"backend\": \"nvidia\"}" > faceswap/config/.faceswap

then faceswap will know that you want to use nvidia and won't ask you about it when you start training.


User avatar
HoloByteus
Posts: 7
Joined: Mon Apr 12, 2021 11:31 pm

Re: [Resource] Training Using Google Colab

Post by HoloByteus »

Yes, I copied the .faceswap from my local install to the config folder in drive and copy it and the training.ini.


Post Reply