Skip to content

Instantly share code, notes, and snippets.

View alankbi's full-sized avatar

Alan Bi alankbi

View GitHub Profile
@alankbi
alankbi / detecto_demo_12.py
Last active April 8, 2020 06:56
Read in and predict on an image with Detecto
# Specify the path to your image
image = utils.read_image('images/image0.jpg')
predictions = model.predict(image)
# predictions format: (labels, boxes, scores)
labels, boxes, scores = predictions
print(labels)
print(boxes)
print(scores)
@alankbi
alankbi / detecto_demo_11.py
Created April 7, 2020 06:11
Create datasets with Detecto
from detecto import core
# All images and XML files in the same folder
dataset = core.Dataset('images/')
# Images and XML files in separate folders
dataset = core.Dataset('labels/', 'images/')
image, target = dataset[0]
print(image, target)
@alankbi
alankbi / detecto_demo_10.py
Last active February 16, 2020 01:33
Advanced training with Detecto
import matplotlib.pyplot as plt
val_dataset = core.Dataset('validation_images/')
losses = model.fit(loader, val_dataset, epochs=10, learning_rate=0.001,
lr_step_size=5, verbose=True)
plt.plot(losses)
plt.show()
@alankbi
alankbi / detecto_demo_8.py
Created February 14, 2020 17:39
Save and load models from files with Detecto
model.save('model_weights.pth')
# ... Later ...
model = core.Model.load('model_weights.pth', ['alien', 'bat', 'witch'])
@alankbi
alankbi / detecto_demo_9.py
Last active February 14, 2020 17:35
Data augmentation and DataLoaders with Detecto
from torchvision import transforms
augmentations = transforms.Compose([
transforms.ToPILImage(),
transforms.RandomHorizontalFlip(0.5),
transforms.ColorJitter(saturation=0.5),
transforms.ToTensor(),
utils.normalize_transform(),
])
@alankbi
alankbi / detecto_demo_7.py
Last active February 14, 2020 16:55
Run object detection on a video with Detecto
visualize.detect_video(model, 'input.mp4', 'output.avi')
@alankbi
alankbi / detecto_demo_6.py
Created February 12, 2020 23:17
Plot your predictions with Detecto
visualize.show_labeled_image(image, boxes, labels)
@alankbi
alankbi / detecto_demo_5.py
Last active February 14, 2020 16:51
Read in and predict on an image with Detecto
# Specify the path to your image
image = utils.read_image('images/image0.jpg')
predictions = model.predict(image)
# predictions format: (labels, boxes, scores)
labels, boxes, scores = predictions
# ['alien', 'bat', 'bat']
print(labels)
@alankbi
alankbi / detecto_demo_4.py
Created February 11, 2020 01:48
Train a custom model with Detecto in 4 lines of code
from detecto import core, utils, visualize
dataset = core.Dataset('images/')
model = core.Model(['alien', 'bat', 'witch'])
model.fit(dataset)
@alankbi
alankbi / detecto_demo_3.py
Last active June 23, 2023 03:30
Google Colab: mount your Drive, change directory, and download Detecto
import os
from google.colab import drive
drive.mount('/content/drive')
os.chdir('/content/drive/My Drive/Detecto Tutorial')
!pip install detecto