Skip to content

Commit

Permalink
Merge pull request #35 from airwzz999/main
Browse files Browse the repository at this point in the history
Add How to run on macOS
  • Loading branch information
liutaocode authored Aug 10, 2024
2 parents a710c55 + a2fd6ec commit 7aab4c1
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ gfpgan
enhance_face_test.py
checkpoints
tmp
train_stage2.py
train_stage2.py
espnet
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/AniTalker.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ ckpts/

[Explanation of Parameters for demo.py](md_docs/config.md)

## Run the demo on macOS

[How to run on macOS](md_docs/run_on_macOS.md)


### Main Inference Scripts (Hubert, Better Result 💪) - Recommended

Expand Down
Binary file added assets/models_huggingface.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/results_run_on_macOS.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 10 additions & 1 deletion code/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def main(args):
padding = np.tile(pose_obj[-1, :], (frame_end - pose_obj.shape[0], 1))
pose_obj = np.vstack((pose_obj, padding))

pose_signal = torch.Tensor(pose_obj).unsqueeze(0).to(args.device) / 90 # 90 is for normalization here
pose_signal = torch.Tensor(pose_obj).unsqueeze(0).to(args.device)/ 90 # 90 is for normalization here
else:
yaw_signal = torch.zeros(1, frame_end, 1).to(args.device) + args.pose_yaw
pitch_signal = torch.zeros(1, frame_end, 1).to(args.device) + args.pose_pitch
Expand Down Expand Up @@ -292,4 +292,13 @@ def main(args):

args = parser.parse_args()

# macOS Config
# Check if MPS is available
if torch.backends.mps.is_available():
args.device = torch.device("mps")
print("MPS backend is available.")
# else:
# args.device = torch.device("cpu")
# print("MPS backend is not available. Using CPU instead.")

main(args)
9 changes: 8 additions & 1 deletion code/diffusion/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,14 @@ def _extract_into_tensor(arr, timesteps, broadcast_shape):
dimension equal to the length of timesteps.
:return: a tensor of shape [batch_size, 1, ...] where the shape has K dims.
"""
res = th.from_numpy(arr).to(device=timesteps.device)[timesteps].float()

if th.backends.mps.is_available():
arr = arr.astype(np.float32)
# Convert the numpy array to a tensor and then move to the device
res = th.from_numpy(arr).to(device=timesteps.device)[timesteps]
else:
res = th.from_numpy(arr).to(device=timesteps.device)[timesteps].float()

while len(res.shape) < len(broadcast_shape):
res = res[..., None]
return res.expand(broadcast_shape)
Expand Down
2 changes: 2 additions & 0 deletions code/networks/styledecoder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import math
# import os
# os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
import torch
from torch import nn
from torch.nn import functional as F
Expand Down
50 changes: 50 additions & 0 deletions md_docs/run_on_macOS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Successfully run on Macbook pro M3 Max (128GB + 8TB), Sonoma 14.6.1

# 1. Project Download

```
git clone https://github.com/X-LANCE/AniTalker.git
```

# 2. Dependencies Installation

```
conda create -n anitalker python==3.9.0 -c conda-forge
conda activate anitalker
conda install pytorch torchvision torchaudio -c pytorch
# install espnet
git clone https://github.com/espnet/espnet.git
cd espnet
pip install -e .
# install python_speech_features
pip install python_speech_features
conda install libffi
pip install -r requirements_macOS.txt
```

# 3. Assets Download

```
# Model
cd AniTalker
mkdir ckpts
Go to https://huggingface.co/taocode/anitalker_ckpts/tree/main
then download all six models in path ~/AniTalker/ckpts/
# npy Go to https://huggingface.co/datasets/taocode/anitalker_hubert_feature_samples/blob/main/monalisa.npy
then download monalisa.npy in path ~/AniTalker/test_demos/audios_hubert/
```
![](../assets/models_huggingface.png)
# 4. Run

```
PYTORCH_ENABLE_MPS_FALLBACK=1 python ./code/demo.py \ --infer_type 'hubert_audio_only' \ --stage1_checkpoint_path 'ckpts/stage1.ckpt' \ --stage2_checkpoint_path 'ckpts/stage2_audio_only_hubert.ckpt' \ --test_image_path 'test_demos/portraits/monalisa.jpg' \ --test_audio_path 'test_demos/audios/monalisa.wav' \ --test_hubert_path 'test_demos/audios_hubert/monalisa.npy' \ --result_path 'outputs/monalisa_hubert/'
```
![](../assets/results_run_on_macOS.png)

# 5. Modify log

- dependencies: requirements.txt
- use mps insted of cuda
- change float64 to float32
- PYTORCH_ENABLE_MPS_FALLBACK=1
Binary file modified outputs/monalisa_hubert/monalisa-monalisa.mp4
Binary file not shown.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pytorch-lightning==1.6.5
torchmetrics==0.5.0
torch==1.8.0
torchvision
scipy==1.5.4
scipy
numpy==1.19.5
tqdm
espnet==202301
Expand Down
7 changes: 7 additions & 0 deletions requirements_macOS.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pytorch-lightning==1.6.5
torchmetrics==0.5.0
scipy
numpy==1.19.5
tqdm
moviepy
transformers==4.19.2

0 comments on commit 7aab4c1

Please sign in to comment.