Skip to content

Commit

Permalink
update CPU mode for SBR
Browse files Browse the repository at this point in the history
  • Loading branch information
D-X-Y committed Jan 14, 2020
1 parent 9e49df6 commit 8c277d7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ cache_data/full.pdf
cache_data/cache/
*.zip
*/*.zip
.DS_Store
*/.DS_Store
7 changes: 6 additions & 1 deletion SBR/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ image_path annotation_path x1 y1 x2 y2 (face_size)

## Training

See the `configs` directory for some example configurations, and a pre-trained model on 300-W is at [Google Driver](https://drive.google.com/drive/folders/1ylMoVuUaNPqP7GSeWS3yE-wfU9JEJSSu).
See the `configs` directory for some example configurations, and a pre-trained model on 300-W is at [Google Driver](https://drive.google.com/drive/folders/1ylMoVuUaNPqP7GSeWS3yE-wfU9JEJSSu) and [Google Driver](https://drive.google.com/open?id=1znVYaCheFJ6VbZmzS1fcfkHoOV4K9H6L).

### Basic Training
```
Expand Down Expand Up @@ -88,6 +88,11 @@ CUDA_VISIBLE_DEVICES=0 python ./exps/eval.py --image ./cache_data/cache/self.jpe
- face : the face bounding box
- save : save the visualized results

Or if you want to run on CPU:
```
python ./exps/eval.py --cpu --image ./cache_data/cache/self.jpeg --model ./snapshots/300W-CPM-DET/checkpoint/cpm_vgg16-epoch-049-050.pth --face 250 150 900 1100 --save ./cache_data/cache/test.jpeg
```


## Visualization

Expand Down
16 changes: 10 additions & 6 deletions SBR/exps/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@


def evaluate(args):
assert torch.cuda.is_available(), 'CUDA is not available.'
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True
if not args.cpu:
assert torch.cuda.is_available(), 'CUDA is not available.'
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark = True

print ('The image is {:}'.format(args.image))
print ('The model is {:}'.format(args.model))
snapshot = Path(args.model)
assert snapshot.exists(), 'The model path {:} does not exist'
print ('The face bounding box is {:}'.format(args.face))
assert len(args.face) == 4, 'Invalid face input : {:}'.format(args.face)
snapshot = torch.load(snapshot)
if args.cpu: snapshot = torch.load(snapshot, map_location='cpu')
else : snapshot = torch.load(snapshot)

# General Data Argumentation
mean_fill = tuple( [int(x*255) for x in [0.485, 0.456, 0.406] ] )
Expand All @@ -47,7 +49,7 @@ def evaluate(args):
dataset.reset(param.num_pts)

net = obtain_model(model_config, param.num_pts + 1)
net = net.cuda()
if not args.cpu: net = net.cuda()
#import pdb; pdb.set_trace()
try:
weights = remove_module_dict(snapshot['detector'])
Expand All @@ -58,7 +60,8 @@ def evaluate(args):
[image, _, _, _, _, _, cropped_size], meta = dataset.prepare_input(args.image, args.face)
# network forward
with torch.no_grad():
inputs = image.unsqueeze(0).cuda()
if args.cpu: inputs = image.unsqueeze(0)
else : inputs = image.unsqueeze(0).cuda()
batch_heatmaps, batch_locs, batch_scos = net(inputs)
flops, params = get_model_infos(net, inputs.shape)
print ('IN-shape : {:}, FLOPs : {:} MB, Params : {:} MB'.format(list(inputs.shape), flops, params))
Expand Down Expand Up @@ -92,5 +95,6 @@ def evaluate(args):
parser.add_argument('--model', type=str, help='The snapshot to the saved detector.')
parser.add_argument('--face', nargs='+', type=float, help='The coordinate [x1,y1,x2,y2] of a face')
parser.add_argument('--save', type=str, help='The path to save the visualized results.')
parser.add_argument('--cpu', action='store_true', help='Use CPU or not.')
args = parser.parse_args()
evaluate(args)

0 comments on commit 8c277d7

Please sign in to comment.