Skip to content

Commit 0d5d23b

Browse files
committed
torch.no_grad() instead of deprecated "volatile", dim set in softmax call explicitly
1 parent b79915d commit 0d5d23b

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

mtcnn/detector.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ def detect_faces(image, min_face_size=20.0,
7676
# STAGE 2
7777

7878
img_boxes = get_image_boxes(bounding_boxes, image, size=24)
79-
img_boxes = Variable(torch.FloatTensor(img_boxes), volatile=True)
80-
output = rnet(img_boxes)
81-
offsets = output[0].data.numpy() # shape [n_boxes, 4]
82-
probs = output[1].data.numpy() # shape [n_boxes, 2]
79+
with torch.no_grad():
80+
img_boxes = Variable(torch.FloatTensor(img_boxes))
81+
output = rnet(img_boxes)
82+
offsets = output[0].data.numpy() # shape [n_boxes, 4]
83+
probs = output[1].data.numpy() # shape [n_boxes, 2]
8384

8485
keep = np.where(probs[:, 1] > thresholds[1])[0]
8586
bounding_boxes = bounding_boxes[keep]
@@ -97,11 +98,13 @@ def detect_faces(image, min_face_size=20.0,
9798
img_boxes = get_image_boxes(bounding_boxes, image, size=48)
9899
if len(img_boxes) == 0:
99100
return [], []
100-
img_boxes = Variable(torch.FloatTensor(img_boxes), volatile=True)
101-
output = onet(img_boxes)
102-
landmarks = output[0].data.numpy() # shape [n_boxes, 10]
103-
offsets = output[1].data.numpy() # shape [n_boxes, 4]
104-
probs = output[2].data.numpy() # shape [n_boxes, 2]
101+
102+
with torch.no_grad():
103+
img_boxes = Variable(torch.FloatTensor(img_boxes))
104+
output = onet(img_boxes)
105+
landmarks = output[0].data.numpy() # shape [n_boxes, 10]
106+
offsets = output[1].data.numpy() # shape [n_boxes, 4]
107+
probs = output[2].data.numpy() # shape [n_boxes, 2]
105108

106109
keep = np.where(probs[:, 1] > thresholds[2])[0]
107110
bounding_boxes = bounding_boxes[keep]

mtcnn/first_stage.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ def run_first_stage(image, net, scale, threshold):
3131
img = cv2.resize(image, (sw, sh))
3232
img = np.asarray(img, 'float32')
3333

34-
img = Variable(torch.FloatTensor(_preprocess(img)), volatile=True)
35-
output = net(img)
36-
probs = output[1].data.numpy()[0, 1, :, :]
37-
offsets = output[0].data.numpy()
38-
# probs: probability of a face at each sliding window
39-
# offsets: transformations to true bounding boxes
34+
with torch.no_grad():
35+
img = Variable(torch.FloatTensor(_preprocess(img)))
36+
output = net(img)
37+
probs = output[1].data.numpy()[0, 1, :, :]
38+
offsets = output[0].data.numpy()
39+
# probs: probability of a face at each sliding window
40+
# offsets: transformations to true bounding boxes
4041

4142
boxes = _generate_bboxes(probs, offsets, scale, threshold)
4243
if len(boxes) == 0:

mtcnn/get_nets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def forward(self, x):
6868
x = self.features(x)
6969
a = self.conv4_1(x)
7070
b = self.conv4_2(x)
71-
a = F.softmax(a)
71+
a = F.softmax(a, dim=1)
7272
return b, a
7373

7474

@@ -113,7 +113,7 @@ def forward(self, x):
113113
x = self.features(x)
114114
a = self.conv5_1(x)
115115
b = self.conv5_2(x)
116-
a = F.softmax(a)
116+
a = F.softmax(a, dim=1)
117117
return b, a
118118

119119

@@ -166,5 +166,5 @@ def forward(self, x):
166166
a = self.conv6_1(x)
167167
b = self.conv6_2(x)
168168
c = self.conv6_3(x)
169-
a = F.softmax(a)
169+
a = F.softmax(a, dim=1)
170170
return c, b, a

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='mtcnn-pytorch',
5-
version='1.0.0',
5+
version='1.0.2',
66
packages=['mtcnn'],
77
package_data={'mtcnn': ['weights/*.npy']},
88
url='',

0 commit comments

Comments
 (0)