-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathminibatch.py
More file actions
22 lines (18 loc) · 813 Bytes
/
Copy pathminibatch.py
File metadata and controls
22 lines (18 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import numpy as np
import math
def iterator(samples, targets, img, box_size, batchsize=100, shuffle=False):
assert len(samples) == len(targets)
def sample_box(x, y):
margin = int(math.floor(box_size / 2))
return img[x-margin:x+margin+1, y-margin:y+margin+1]
indices = np.arange(len(targets))
if shuffle:
np.random.shuffle(indices)
for start_idx in range(0, len(targets) - batchsize + 1, batchsize):
batch_inputs = np.zeros((batchsize,1,box_size,box_size))
batch_targets = np.zeros(batchsize)
for kk in range(0, batchsize,1):
ind = indices[start_idx+kk]
batch_inputs[kk,0:,:] = sample_box(samples[ind][0], samples[ind][1])
batch_targets[kk] = targets[ind]
yield batch_inputs, batch_targets