Beispiel #1
0
def load_model(modules):
    model = YOLO(modules)
    model.to(DEVICE)
    model.load_weights('./weights/yolov3.weights')
    return model
Beispiel #2
0
        yield imgs, tars


device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
"""params"""
# anchors as unit of pixel
anchors = np.array([[10, 13], [16, 30], [33, 23], [30, 61], [62, 45],
                    [59, 119], [116, 90], [156, 198], [373, 326]])
anchor_mask = np.array([[6, 7, 8], [3, 4, 5], [0, 1, 2]])
image_size = (416, 416)  # default input image size
# transform into unit of image
anchors = anchors / np.asarray(image_size).reshape(1, 2)
num_classes = 80
"""train"""
net = YOLO()
net.to(device)
optimizer = torch.optim.Adam(net.parameters(), lr=1e-3)
loss_fn = yolo_loss

for epoch in range(10):
    loss = 0.
    num_of_batch = 20
    for imgs, tars in create_fake_data(image_size, num_of_batch=num_of_batch):
        imgs = imgs.to(device)
        feats = net(imgs)
        loss_ = loss_fn(feats, tars, anchors, anchor_mask, device, image_size)
        loss += loss_.item()
        optimizer.zero_grad()
        loss_.backward()
        optimizer.step()
    print("epoch {0}, loss {1}".format(epoch, loss / num_of_batch))