This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
/
object_detection.py
55 lines (46 loc) · 1.78 KB
/
object_detection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import flash
from flash.core.integrations.fiftyone import visualize
from flash.core.utilities.imports import example_requires
from flash.image import ObjectDetectionData, ObjectDetector
example_requires("image")
import icedata # noqa: E402
# 1. Create the DataModule
data_dir = icedata.fridge.load_data()
datamodule = ObjectDetectionData.from_icedata(
train_folder=data_dir,
predict_folder=os.path.join(data_dir, "odFridgeObjects", "images"),
val_split=0.1,
transform_kwargs={"image_size": 128},
parser=icedata.fridge.parser,
batch_size=8,
)
# 2. Build the task
model = ObjectDetector(
head="efficientdet",
backbone="d0",
labels=datamodule.labels,
image_size=128,
lr_scheduler=("multisteplr", {"milestones": [20]}),
)
# 3. Create the trainer and finetune the model
trainer = flash.Trainer(max_epochs=30)
trainer.finetune(model, datamodule=datamodule, strategy="freeze")
# 4. Set the output and get some predictions
predictions = trainer.predict(model, datamodule=datamodule, output="fiftyone") # output FiftyOne format
# 5. Visualize predictions in FiftyOne app
# Optional: pass `wait=True` to block execution until App is closed
session = visualize(predictions, wait=True)