Package: detecto
Detecto is a Python package that allows you to build fully-functioning computer vision and object detection models with just 5 lines of code.
Inference on still images and videos, transfer learning on custom datasets, and serialization of models to files are just a few of Detecto's features.
Detecto is also built on top of PyTorch, allowing an easy transfer of models between the two libraries.The table below shows a few examples of Detecto's performance:Still ImageVideoInstallationTo install Detecto using pip, run the following command:pip3 install detectoInstalling with pip should download all of Detecto's dependencies automatically.
However, if an issue arises, you can manually download the dependencies listed in therequirements.txtfile.UsageThe power of Detecto comes from its simplicity and ease of use. Creating and running a pre-trainedFaster R-CNN ResNet-50 FPNfrom PyTorch's model zoo takes 4 lines of code:fromdetecto.coreimportModelfromdetecto.visualizeimportdetect_videomodel=Model()# Initialize a pre-trained modeldetect_video(model,'input_video.mp4','output.avi')# Run inference on a videoBelow are several more examples of things you can do with Detecto:Transfer Learning on Custom DatasetsMost of the times, you want a computer vision model that can detect custom objects. With Detecto, you can train a model on a custom dataset with 5 lines of code:fromdetecto.coreimportModel,Datasetdataset=Dataset('custom_dataset/')# Load images and label data from the custom_dataset/ foldermodel=Model(['dog','cat','rabbit'])# Train to predict dogs, cats, and rabbitsmodel.fit(dataset)model.predict(...)# Start using your trained model!Inference and VisualizationWhen using a model for inference, Detecto returns predictions in an easy-to-use format and provides several visualization tools:fromdetecto.coreimportModelfromdetectoimportutils,visualizemodel=Model()image=utils.read_image('image.jpg')# Helper function to read in imageslabels,boxes,scores=model.predict(image)# Get all predictions on an imagepredictions=model.predict_top(image)# Same as above, but returns only the top predictionsprint(labels,boxes,scores)print(predictions)visualize.show_labeled_image(image,boxes,labels)# Plot predictions on a single imageimages=[...]visualize.plot_prediction_grid(model,images)# Plot predictions on a list of imagesvisualize.detect_video(model,'input_video.mp4','output.avi')# Run inference on a videovisualize.detect_live(model)# Run inference on a live webcamAdvanced UsageIf you want more control over how you train your model, Detecto lets you do just that:fromdetectoimportcore,utilsfromtorchvisionimporttransformsimportmatplotlib.pyplotasplt# Convert XML files to CSV formatutils.xml_to_csv('training_labels/','train_labels.csv')utils.xml_to_csv('validation_labels/','val_labels.csv')# Define custom transforms to apply to your datasetcustom_transforms=transforms.Compose([transforms.ToPILImage(),transforms.Resize(800),transforms.ColorJitter(saturation=0.3),transforms.ToTensor(),utils.normalize_transform(),])# Pass in a CSV file instead of XML files for faster Dataset initialization speedsdataset=core.Dataset('train_labels.csv','images/',transform=custom_transforms)val_dataset=core.Dataset('val_labels.csv','val_images')# Validation dataset for training# Create your own DataLoader with custom optionsloader=core.DataLoader(dataset,batch_size=2,shuffle=True)# Use MobileNet instead of the default ResNetmodel=core.Model(['car','truck','boat','plane'],model_name='fasterrcnn_mobilenet_v3_large_fpn')losses=model.fit(loader,val_dataset,epochs=15,learning_rate=0.001,verbose=True)plt.plot(losses)# Visualize loss throughout trainingplt.show()model.save('model_weights.pth')# Save model to a file# Directly access underlying torchvision model for even more controltorch_model=model.get_internal_model()print(type(torch_model))For more examples, visit thedocs, which includes aquickstarttutorial.Alternatively, check out thedemo on Colab.API DocumentationThe full API documentation can be found atdetecto.readthedocs.io.
The docs are split into three sections, each corresponding to one of Detecto's modules:CoreThedetecto.coremodule contains the central classes of the package: Dataset, DataLoader, and Model.
These are used to read in a labeled dataset and train a functioning object detection model.UtilsThedetecto.utilsmodule contains a variety of useful helper functions.
With it, you can read in images, convert XML files into CSV files, apply standard transforms to images, and more.VisualizeThedetecto.visualizemodule is used to display labeled images, plot predictions, and run object detection on videos.ContributingAll issues and pull requests are welcome! To run the code locally, first fork the repository and then run the following commands on your computer:gitclonehttps://github.com/<your-username>/detecto.gitcddetecto# Recommended to create a virtual environment before the next steppip3install-rrequirements.txtWhen adding code, be sure to write unit tests and docstrings where necessary.Tests are located indetecto/testsand can be run using pytest:python3 -m pytestTo generate the documentation locally, run the following commands:cddocs
makehtmlThe documentation can then be viewed atdocs/_build/html/index.html.ContactDetecto was created byAlan Bi. Feel free to reach out onTwitteror throughemail!