几行代码构建全功能的对象检测模型,他是如何做到的?




然后,将上面的图像另存为“fruit.jpg”,并在与图像相同的文件夹中创建一个Python文件。在Python文件中,编写以下5行代码:
fromdetectoimportcore, utils, visualize
?
image = utils.read_image('fruit.jpg')
model = core.Model()
?
labels, boxes, scores = model.predict_top(image)
visualize.show_labeled_image(image, boxes, labels)
运行此文件后(如果你的计算机上没有启用CUDA的GPU,可能会花费几秒钟;稍后再进行介绍),你应该会看到类似下面的图:




fromdetecto.utilsimportsplit_video
?
split_video('video.mp4','frames/', step_size=4)
images/
|?? image0.jpg
|?? image1.jpg
|?? image2.jpg
|?? ...
pip3 install labelImg???# Download LabelImg using pip
labelImg????????????????# Launch the application


images/
|?? image0.jpg
|?? image0.xml
|?? image1.jpg
|?? image1.xml
|?? ...

importtorch
?
print(torch.cuda.is_available())


importos
fromgoogle.colabimportdrive
?
drive.mount('/content/drive')
?
os.chdir('/content/drive/My Drive/Detecto Tutorial')
?
!pip install detecto

fromdetectoimportcore, utils, visualize
?
dataset = core.Dataset('images/')
model = core.Model(['alien','bat','witch'])
?
model.fit(dataset)

# 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)
?
#?????????? xmin?????? ymin?????? xmax?????? ymax
# tensor([[ 569.2125,? 203.6702, 1003.4383,? 658.1044],
#???????? [ 276.2478,? 144.0074,? 579.6044,? 508.7444],
#???????? [ 277.2929,? 162.6719,? 627.9399,? 511.9841]])
print(boxes)
?
# tensor([0.9952, 0.9837, 0.5153])
print(scores)
visualize.show_labeled_image(image, boxes, labels)

visualize.detect_video(model,'input.mp4','output.avi')
model.save('model_weights.pth')
?
# ... Later ...
?
model = core.Model.load('model_weights.pth', ['alien','bat','witch'])

fromtorchvisionimporttransforms
?
augmentations = transforms.Compose([
??? transforms.ToPILImage(),
??? transforms.RandomHorizontalFlip(0.5),
??? transforms.ColorJitter(saturation=0.5),
??? transforms.ToTensor(),
??? utils.normalize_transform(),
])
?
dataset = core.Dataset('images/', transform=augmentations)
?
loader = core.DataLoader(dataset, batch_size=2, shuffle=True)
importmatplotlib.pyplotasplt
?
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()


原文:
https://hackernoon.com/build-a-custom-trained-object-detection-model-with-5-lines-of-code-y08n33vi
(*本文由AI科技大本营翻译,转载请微信联系1092722531)
◆
精彩推荐
◆
技术战“疫”,贾扬清、李飞飞要给程序员直播讲AI技术!
2月18日、2月20日晚7点,阿里云CIO学院攻“疫”技术课程正式开启。您将获得与达摩院数据库首席科学家 、阿里巴巴集团副总裁、ACM 杰出科学家李飞飞,Caffe之父、ONNX创始人、阿里巴巴集团副总裁贾扬清,阿里巴巴集团副总裁、阿里 CIO 学院院长胡臣杰等顶级技术专家直播互动的机会。


你点的每个“在看”,我都认真当成了AI
关注公众号:拾黑(shiheibook)了解更多
[广告]赞助链接:
四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/
关注网络尖刀微信公众号随时掌握互联网精彩
- 1 打好关键核心技术攻坚战 7903934
- 2 在南海坠毁的2架美国军机已被捞出 7809209
- 3 立陶宛进入紧急状态 卢卡申科发声 7712688
- 4 持续巩固增强经济回升向好态势 7615740
- 5 多家店铺水银体温计售空 7523568
- 6 奶奶自爷爷去世9个月后变化 7423722
- 7 仅退款225个快递女子已归案 7328476
- 8 日舰曾收到中方提示 7232650
- 9 中国中冶跌10.03% 7141499
- 10 我国成功发射遥感四十七号卫星 7047322







AI100
