Browse Source

Update autobatch.py (#5538)

* Update autobatch.py

* Update autobatch.py

* Update autobatch.py
modifyDataloader
Glenn Jocher GitHub 3 years ago
parent
commit
cb18cac33d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 5 deletions
  1. +6
    -5
      utils/autobatch.py

+ 6
- 5
utils/autobatch.py View File

@@ -35,11 +35,12 @@ def autobatch(model, imgsz=640, fraction=0.9, batch_size=16):
return batch_size

d = str(device).upper() # 'CUDA:0'
t = torch.cuda.get_device_properties(device).total_memory / 1024 ** 3 # (GB)
r = torch.cuda.memory_reserved(device) / 1024 ** 3 # (GB)
a = torch.cuda.memory_allocated(device) / 1024 ** 3 # (GB)
properties = torch.cuda.get_device_properties(device) # device properties
t = properties.total_memory / 1024 ** 3 # (GiB)
r = torch.cuda.memory_reserved(device) / 1024 ** 3 # (GiB)
a = torch.cuda.memory_allocated(device) / 1024 ** 3 # (GiB)
f = t - (r + a) # free inside reserved
print(f'{prefix}{d} {t:.3g}G total, {r:.3g}G reserved, {a:.3g}G allocated, {f:.3g}G free')
print(f'{prefix}{d} ({properties.name}) {t:.2f}G total, {r:.2f}G reserved, {a:.2f}G allocated, {f:.2f}G free')

batch_sizes = [1, 2, 4, 8, 16]
try:
@@ -52,5 +53,5 @@ def autobatch(model, imgsz=640, fraction=0.9, batch_size=16):
batch_sizes = batch_sizes[:len(y)]
p = np.polyfit(batch_sizes, y, deg=1) # first degree polynomial fit
b = int((f * fraction - p[1]) / p[0]) # y intercept (optimal batch size)
print(f'{prefix}Using batch-size {b} for {d} {t * fraction:.3g}G/{t:.3g}G ({fraction * 100:.0f}%)')
print(f'{prefix}Using batch-size {b} for {d} {t * fraction:.2f}G/{t:.2f}G ({fraction * 100:.0f}%)')
return b

Loading…
Cancel
Save