-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Chen-Yulin/yolo160
Yolo160
- Loading branch information
Showing
12 changed files
with
207 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,169 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Unity.Barracuda; | ||
using UnityEngine; | ||
using UnityEngine.Profiling; | ||
|
||
namespace NN | ||
{ | ||
public class YOLOv8 | ||
{ | ||
protected YOLOv8OutputReader outputReader; | ||
|
||
private NNHandler nn; | ||
|
||
public YOLOv8(NNHandler nn) | ||
{ | ||
this.nn = nn; | ||
outputReader = new(); | ||
} | ||
|
||
public List<ResultBox> Run(Texture2D image) | ||
{ | ||
Profiler.BeginSample("YOLO.Run"); | ||
var outputs = ExecuteModel(image); | ||
var results = Postprocess(outputs); | ||
//var results = new List<ResultBox>(); | ||
Profiler.EndSample(); | ||
return results; | ||
} | ||
|
||
protected Tensor[] ExecuteModel(Texture2D image) | ||
{ | ||
Tensor input = new Tensor(image); | ||
ExecuteBlocking(input); | ||
input.tensorOnDevice.Dispose(); | ||
return PeekOutputs().ToArray(); | ||
} | ||
|
||
private void ExecuteBlocking(Tensor preprocessed) | ||
{ | ||
Profiler.BeginSample("YOLO.Execute"); | ||
nn.worker.Execute(preprocessed); | ||
nn.worker.FlushSchedule(blocking: true); | ||
Profiler.EndSample(); | ||
} | ||
|
||
|
||
private IEnumerable<Tensor> PeekOutputs() | ||
{ | ||
foreach (string outputName in nn.model.outputs) | ||
{ | ||
Tensor output = nn.worker.PeekOutput(outputName); | ||
yield return output; | ||
} | ||
} | ||
|
||
protected List<ResultBox> Postprocess(Tensor[] outputs) | ||
{ | ||
Profiler.BeginSample("YOLOv8Postprocessor.Postprocess"); | ||
Tensor boxesOutput = outputs[0]; | ||
List<ResultBox> boxes = outputReader.ReadOutput(boxesOutput).ToList(); | ||
boxes = DuplicatesSupressor.RemoveDuplicats(boxes); | ||
Profiler.EndSample(); | ||
return boxes; | ||
} | ||
} | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Unity.Barracuda; | ||
using UnityEngine; | ||
using UnityEngine.Profiling; | ||
|
||
namespace NN | ||
{ | ||
public class YOLOv8 : MonoBehaviour | ||
{ | ||
protected YOLOv8OutputReader outputReader; | ||
|
||
protected NNHandler nn; | ||
|
||
public bool workerbusy = false; | ||
|
||
public bool jobcomplete = false; | ||
|
||
public bool ResAvailable = false; | ||
|
||
public bool StartOfDetection = false; | ||
|
||
private List<ResultBox> results = new List<ResultBox>(); | ||
|
||
public List<ResultBox> Results | ||
{ | ||
get { return results; } | ||
set | ||
{ | ||
if (value != null) | ||
{ | ||
results = value; | ||
} | ||
} | ||
} | ||
|
||
Tensor input; | ||
|
||
Texture2D sourceImg; | ||
|
||
bool on; | ||
|
||
public void SetSource(Texture2D img) | ||
{ | ||
sourceImg = img; | ||
} | ||
|
||
public void TurnOffYolo() | ||
{ | ||
on = false; | ||
workerbusy = false; | ||
jobcomplete = false; | ||
ResAvailable = false; | ||
StartOfDetection = false; | ||
|
||
StopAllCoroutines(); | ||
try | ||
{ | ||
input.tensorOnDevice.Dispose(); | ||
} | ||
catch { } | ||
} | ||
|
||
public void TurnOnYolo() | ||
{ | ||
on = true; | ||
StartOfDetection = true; | ||
} | ||
|
||
public YOLOv8(NNHandler nn) | ||
{ | ||
this.nn = nn; | ||
outputReader = new(); | ||
} | ||
public virtual void InitYOLOv8(NNHandler nn) | ||
{ | ||
this.nn = nn; | ||
outputReader = new(); | ||
} | ||
|
||
public void Run() | ||
{ | ||
Results = Postprocess(ExecuteModel()); | ||
} | ||
public void Update() | ||
{ | ||
if (on) | ||
{ | ||
Run(); | ||
} | ||
} | ||
|
||
protected Tensor[] ExecuteModel() | ||
{ | ||
if (!workerbusy) // start of execution | ||
{ | ||
if (jobcomplete) // complete | ||
{ | ||
jobcomplete = false; | ||
ResAvailable = true; | ||
input.tensorOnDevice.Dispose(); | ||
return PeekOutputs().ToArray(); | ||
} | ||
else // ready to start | ||
{ | ||
if (sourceImg)// start of job | ||
{ | ||
input = new Tensor(sourceImg); | ||
// ExecuteBlocking(input); | ||
workerbusy = true; | ||
StartCoroutine(ExecuteUnblocking(input)); | ||
} | ||
return null; | ||
} | ||
} | ||
else //during execution | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
IEnumerator ExecuteUnblocking(Tensor preprocessed) | ||
{ | ||
var it = nn.worker.StartManualSchedule(preprocessed); | ||
workerbusy = true; | ||
int cnt = 0; | ||
|
||
while (it.MoveNext()) | ||
{ | ||
++cnt; | ||
if (cnt % 5 == 0) | ||
{ | ||
nn.worker.FlushSchedule(false); | ||
yield return null; | ||
} | ||
} | ||
|
||
nn.worker.FlushSchedule(true); | ||
workerbusy = false; | ||
jobcomplete = true; | ||
} | ||
|
||
private IEnumerable<Tensor> PeekOutputs() | ||
{ | ||
foreach (string outputName in nn.model.outputs) | ||
{ | ||
Tensor output = nn.worker.PeekOutput(outputName); | ||
yield return output; | ||
} | ||
} | ||
|
||
protected List<ResultBox> Postprocess(Tensor[] outputs) | ||
{ | ||
if (outputs == null) | ||
{ | ||
return null; | ||
} | ||
Profiler.BeginSample("YOLOv8Postprocessor.Postprocess"); | ||
Tensor boxesOutput = outputs[0]; | ||
List<ResultBox> boxes = outputReader.ReadOutput(boxesOutput).ToList(); | ||
boxes = DuplicatesSupressor.RemoveDuplicats(boxes); | ||
Profiler.EndSample(); | ||
return boxes; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.