Skip to content

working on cpu version #1

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

Open
wants to merge 1 commit 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: 2 additions & 1 deletion model.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def __init__(self, num_classes=1, num_filters=32, pretrained=True, is_deconv=Tru

self.num_classes = num_classes
self.Dropout = Dropout
res_blocks_dec = False
self.res_blocks_dec = res_blocks_dec
self.pool = nn.MaxPool2d(2, 2)
self.relu = nn.ReLU(inplace=True)
Expand Down Expand Up @@ -324,4 +325,4 @@ def _initialize_weights(self):
elif isinstance(m, nn.Linear):
n = m.weight.size(1)
m.weight.data.normal_(0, 0.01)
m.bias.data.zero_()
m.bias.data.zero_()
2 changes: 1 addition & 1 deletion predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def split_video(filename, n_frames=20):
print("file not found")
sys.exit(-1)

if file_path.split(".")[-1] == "png":
if file_path.split(".")[-1] != "mp4":
imgs = cv2.imread(file_path)
imgs = cv2.cvtColor(imgs, cv2.COLOR_BGR2RGB)
imgs = np.array(imgs, dtype=np.uint8)
Expand Down
13 changes: 10 additions & 3 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import torch
from torch.utils import data
from torchvision import transforms
import cv2
from torch.autograd import Variable

from model import *
Expand All @@ -22,7 +23,7 @@ def save_checkpoint(checkpoint_path, model, optimizer):
print('model saved to %s' % checkpoint_path)

def load_checkpoint(checkpoint_path, model, optimizer):
state = torch.load(checkpoint_path)
state = torch.load(checkpoint_path, map_location='cpu')
model.load_state_dict(state['state_dict'])
if optimizer:
optimizer.load_state_dict(state['optimizer'])
Expand Down Expand Up @@ -125,7 +126,10 @@ def __init__(self, path=None, **kwargs):

def get_model(self, model):
model = model.train()
return model.cuda(self.device_idx)
if torch.cuda.is_available():
return model.cuda(self.device_idx)
else:
return model

def LR_finder(self, dataset, **kwargs):

Expand Down Expand Up @@ -379,7 +383,10 @@ def predict_crop(self, imgs):
for i in range(imgs.shape[0]):
img = self.norm(cv2.resize(imgs[i], (256, 320), interpolation=cv2.INTER_LANCZOS4))
img = img.unsqueeze_(0)
img = img.type(torch.FloatTensor).cuda()
if torch.cuda.is_available():
img = img.type(torch.FloatTensor).cuda()
else:
img = img.type(torch.FloatTensor)
output = torch.nn.functional.sigmoid(self.model(Variable(img)))
output = output.cpu().data.numpy()
y_pred = np.squeeze(output[0])
Expand Down