Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support to run model on CPU out of the box #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cuda_device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import torch

CUDA_DEVICE = 'gpu' if torch.cuda.is_available() else 'cpu'
6 changes: 5 additions & 1 deletion demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from networks.transforms import trimap_transform, groupnorm_normalise_image
from networks.models import build_model
from dataloader import PredDataset
from cuda_device import CUDA_DEVICE

# System libs
import os
Expand All @@ -14,7 +15,10 @@


def np_to_torch(x):
return torch.from_numpy(x).permute(2, 0, 1)[None, :, :, :].float().cuda()
val = torch.from_numpy(x).permute(2, 0, 1)[None, :, :, :].float()
if CUDA_DEVICE == 'gpu':
val = val.cuda()
return val


def scale_input(x: np.ndarray, scale: float, scale_type) -> np.ndarray:
Expand Down
6 changes: 4 additions & 2 deletions networks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import networks.resnet_GN_WS as resnet_GN_WS
import networks.layers_WS as L
import networks.resnet_bn as resnet_bn
from cuda_device import CUDA_DEVICE


def build_model(args):
Expand All @@ -17,10 +18,11 @@ def build_model(args):

model = MattingModule(net_encoder, net_decoder)

model.cuda()
if CUDA_DEVICE == 'gpu':
model.cuda()

if(args.weights != 'default'):
sd = torch.load(args.weights)
sd = torch.load(args.weights, map_location=torch.device(CUDA_DEVICE))
model.load_state_dict(sd, strict=True)

return model
Expand Down
5 changes: 4 additions & 1 deletion networks/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np
import torch
import cv2
from cuda_device import CUDA_DEVICE


def dt(a):
Expand Down Expand Up @@ -50,7 +51,9 @@ def groupnorm_denormalise_image(img, format='nhwc'):
for i in range(3):
img[:, :, :, i] = img[:, :, :, i] * group_norm_std[i] + group_norm_mean[i]
else:
img1 = torch.zeros_like(img).cuda()
img1 = torch.zeros_like(img)
if CUDA_DEVICE == 'gpu':
img1 = img1.cuda()
for i in range(3):
img1[:, i, :, :] = img[:, i, :, :] * group_norm_std[i] + group_norm_mean[i]
return img1
Expand Down