Skip to content

Commit

Permalink
PyTorch 1.7.0 Compatibility Updates (#1233)
Browse files Browse the repository at this point in the history
* torch 1.7.0 compatibility updates

* add inference verification
  • Loading branch information
glenn-jocher authored Oct 28, 2020
1 parent 453acde commit c8c5ef3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
8 changes: 8 additions & 0 deletions hubconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,11 @@ def yolov5x(pretrained=False, channels=3, classes=80):

if __name__ == '__main__':
model = create(name='yolov5s', pretrained=True, channels=3, classes=80) # example
model = model.fuse().eval().autoshape() # for autoshaping of PIL/cv2/np inputs and NMS

This comment has been minimized.

Copy link
@Greg-Tarr

Greg-Tarr Oct 28, 2020

@glenn-jocher sorry to bother you, love all of your work. Mind if I ask what .autoshape() is and how it works... It sounds fantastic and a real solution to a lot of headaches, but I've never seen it before, nor can I find any documentation 👍 Thanks!

This comment has been minimized.

Copy link
@glenn-jocher

glenn-jocher Oct 28, 2020

Author Member

It's never existed before. It's a YOLOv5 wrapper that automatically handles input and output processing to allow you to pass PIL/cv2/numpy or torch inputs. Multiple images may be passes in as a list for batched inference.

We have an example in the README:
https://github.com/ultralytics/yolov5#pytorch-hub

And a full PyTorch Hub tutorial here:
https://github.com/ultralytics/yolov5#tutorials


# Verify inference
from PIL import Image

img = Image.open('inference/images/zidane.jpg')
y = model(img)
print(y[0].shape)
7 changes: 7 additions & 0 deletions models/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ def attempt_load(weights, map_location=None):
attempt_download(w)
model.append(torch.load(w, map_location=map_location)['model'].float().fuse().eval()) # load FP32 model

# Compatibility updates
for m in model.modules():
if type(m) in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6]:
m.inplace = True # pytorch 1.7.0 compatibility
elif type(m) is Conv:
m._non_persistent_buffers_set = set() # pytorch 1.6.0 compatibility

if len(model) == 1:
return model[-1] # return model
else:
Expand Down
1 change: 0 additions & 1 deletion models/yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ def fuse(self): # fuse model Conv2d() + BatchNorm2d() layers
print('Fusing layers... ')
for m in self.model.modules():
if type(m) is Conv and hasattr(m, 'bn'):
m._non_persistent_buffers_set = set() # pytorch 1.6.0 compatability
m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv
delattr(m, 'bn') # remove batchnorm
m.forward = m.fuseforward # update forward
Expand Down
2 changes: 1 addition & 1 deletion utils/torch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def initialize_weights(model):
elif t is nn.BatchNorm2d:
m.eps = 1e-3
m.momentum = 0.03
elif t in [nn.LeakyReLU, nn.ReLU, nn.ReLU6]:
elif t in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6]:
m.inplace = True


Expand Down

0 comments on commit c8c5ef3

Please sign in to comment.