-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from Keasys/transformer
Transformer
- Loading branch information
Showing
7 changed files
with
1,420 additions
and
6 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import torch | ||
import torch.nn as nn | ||
|
||
class rPPGTransformer(nn.Transformer): | ||
"""Documentation for rPPGTransformer | ||
""" | ||
def __init__(self, emb_dim, nhead, num_encoder_layers=12, num_decoder_layers=12): | ||
super(rPPGTransformer, self).__init__(emb_dim, nhead, num_decoder_layers, num_encoder_layers) | ||
|
||
|
||
def forward(self, X): | ||
Y = super(rPPGTransformer,self).forward(X,X) | ||
return torch.mean(Y, axis=1) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .mtts_can import * | ||
from .hr_cnn import * | ||
from .hr_cnn import * | ||
from .transformer import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import pyVHR | ||
import numpy as np | ||
import os | ||
import torch | ||
import time | ||
import torchvision.transforms as transforms | ||
|
||
from torch.utils.data import DataLoader | ||
|
||
from .TRANSFORMER.model import rPPGTransformer | ||
from .HR_CNN.utils import butter_bandpass_filter | ||
|
||
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") | ||
|
||
def RPPG_TRANSFORMER_bvp_pred(frames): | ||
model_path = pyVHR.__path__[0] + "/deepRPPG/TRANSFORMER/model_vipl_next_50_1.dct" | ||
model_name = model_path.split('/')[-1].split('.')[0] | ||
if not os.path.isfile(model_path): | ||
url = "" | ||
print('Downloading rPPG Transformer model...') | ||
r = requests.get(url, allow_redirects=True) | ||
open(model_path, 'wb').write(r.content) | ||
model = rPPGTransformer(250, nhead=1, num_encoder_layers=12, num_decoder_layers=12).to(device) | ||
model.load_state_dict(torch.load(model_path)) | ||
|
||
|
||
model.eval() | ||
|
||
frames = torch.as_tensor(frames) | ||
frames = torch.utils.data.TensorDataset(frames.to(device, dtype=torch.float)) | ||
frames_loader = torch.utils.data.DataLoader(frames, | ||
batch_size=16, | ||
shuffle=True, | ||
drop_last=True) | ||
|
||
start = time.time() | ||
|
||
outputs = [] | ||
|
||
for i, chrom_patches in enumerate(frames_loader): | ||
output = model(chrom_patches[0]).detach().squeeze().cpu().numpy() | ||
outputs.append(output) | ||
end = time.time() | ||
print("processing time: ", end - start) | ||
|
||
outputs=np.array(outputs) | ||
|
||
mean = np.mean(outputs) | ||
outputs = (outputs - np.mean(outputs)) / np.std(outputs) | ||
|
||
fs = 30 | ||
|
||
lowcut = 0.8 | ||
highcut = 6 | ||
|
||
filtered_outputs = butter_bandpass_filter(outputs, lowcut, highcut, fs, order=4) | ||
filtered_outputs = (filtered_outputs - np.mean(filtered_outputs)) / np.std(filtered_outputs) | ||
|
||
# rearrange output unbatched | ||
filtered_outputs = np.array(filtered_outputs) | ||
filtered_outputs = np.concatenate(np.concatenate(filtered_outputs, axis=0), axis=0) | ||
|
||
return filtered_outputs | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters