feat: add tasks closes #74

This commit is contained in:
2024-04-12 20:36:23 +01:00
parent 143ad3b02b
commit eb20c1b0ac
21 changed files with 986 additions and 232 deletions

View File

@@ -89,7 +89,7 @@ func fileProcessor(
defer f.Close()
f.Write(file_data)
if !testImgForModel(c, model, file_path) {
if !TestImgForModel(c, model, file_path) {
c.Logger.Errorf("Image did not have valid format for model %s (in zip: %s)!", file_path, file.Name)
c.Logger.Warn("Not failling updating data point to status -1")
message := "Image did not have valid format for the model"

View File

@@ -18,7 +18,6 @@ func HandleModels (handle *Handle) {
model_classes.HandleList(handle)
// Train endpoints
handleRun(handle)
models_train.HandleTrainEndpoints(handle)
}

View File

@@ -1,12 +1,12 @@
package models
import (
"bytes"
"io"
"errors"
"os"
"path"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/tasks/utils"
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
tf "github.com/galeone/tensorflow/tensorflow/go"
@@ -35,7 +35,7 @@ func ReadJPG(scope *op.Scope, imagePath string, channels int64) *image.Image {
return image.Scale(0, 255)
}
func runModelNormal(c *Context, model *BaseModel, def_id string, inputImage *tf.Tensor) (order int, confidence float32, err error) {
func runModelNormal(base BasePack, model *BaseModel, def_id string, inputImage *tf.Tensor) (order int, confidence float32, err error) {
order = 0
err = nil
@@ -62,7 +62,7 @@ func runModelNormal(c *Context, model *BaseModel, def_id string, inputImage *tf.
return
}
func runModelExp(c *Context, model *BaseModel, def_id string, inputImage *tf.Tensor) (order int, confidence float32, err error) {
func runModelExp(base BasePack, model *BaseModel, def_id string, inputImage *tf.Tensor) (order int, confidence float32, err error) {
err = nil
order = 0
@@ -82,12 +82,12 @@ func runModelExp(c *Context, model *BaseModel, def_id string, inputImage *tf.Ten
Range_start int
}
heads, err := GetDbMultitple[head](c, "exp_model_head where def_id=$1;", def_id)
heads, err := GetDbMultitple[head](base.GetDb(), "exp_model_head where def_id=$1;", def_id)
if err != nil {
return
}
c.Logger.Info("test", "count", len(heads))
base.GetLogger().Info("test", "count", len(heads))
var vmax float32 = 0.0
@@ -102,9 +102,8 @@ func runModelExp(c *Context, model *BaseModel, def_id string, inputImage *tf.Ten
var predictions = results[0].Value().([][]float32)[0]
for i, v := range predictions {
c.Logger.Info("predictions", "class", i, "preds", v)
base.GetLogger().Debug("predictions", "class", i, "preds", v)
if v > vmax {
order = element.Range_start + i
vmax = v
@@ -115,139 +114,105 @@ func runModelExp(c *Context, model *BaseModel, def_id string, inputImage *tf.Ten
// TODO runthe head model
confidence = vmax
c.Logger.Info("Got", "heads", len(heads), "order", order, "vmax", vmax)
base.GetLogger().Debug("Got", "heads", len(heads), "order", order, "vmax", vmax)
return
}
func handleRun(handle *Handle) {
handle.Post("/models/run", func(c *Context) *Error {
if !c.CheckAuthLevel(1) {
return nil
}
func ClassifyTask(base BasePack, task Task) (err error) {
task.UpdateStatusLog(base, TASK_RUNNING, "Runner running task")
read_form, err := c.R.MultipartReader()
model, err := GetBaseModel(base.GetDb(), task.ModelId)
if err != nil {
task.UpdateStatusLog(base, TASK_FAILED_RUNNING, "Failed to obtain the model")
return err
}
if !model.CanEval() {
task.UpdateStatusLog(base, TASK_FAILED_RUNNING, "Failed to obtain the model")
return errors.New("Model not in the right state for evaluation")
}
def := JustId{}
err = GetDBOnce(base.GetDb(), &def, "model_definition where model_id=$1", model.Id)
if err != nil {
task.UpdateStatusLog(base, TASK_FAILED_RUNNING, "Failed to obtain the model")
return
}
def_id := def.Id
// TODO create a database table with tasks
run_path := path.Join("/tmp", model.Id, "runs")
os.MkdirAll(run_path, os.ModePerm)
img_path := path.Join("savedData", model.Id, "tasks", task.Id+"."+model.Format)
root := tg.NewRoot()
var tf_img *image.Image = nil
switch model.Format {
case "png":
tf_img = ReadPNG(root, img_path, int64(model.ImageMode))
case "jpeg":
tf_img = ReadJPG(root, img_path, int64(model.ImageMode))
default:
task.UpdateStatusLog(base, TASK_FAILED_RUNNING, "Failed to obtain the model")
}
exec_results := tg.Exec(root, []tf.Output{tf_img.Value()}, nil, &tf.SessionOptions{})
inputImage, err := tf.NewTensor(exec_results[0].Value())
if err != nil {
task.UpdateStatusLog(base, TASK_FAILED_RUNNING, "Failed to run model")
return
}
vi := -1
var confidence float32 = 0
if model.ModelType == 2 {
base.GetLogger().Info("Running model normal", "model", model.Id, "def", def_id)
vi, confidence, err = runModelExp(base, model, def_id, inputImage)
if err != nil {
return c.JsonBadRequest("Invalid muilpart body")
task.UpdateStatusLog(base, TASK_FAILED_RUNNING, "Failed to run model")
return
}
var id string
var file []byte
for {
part, err_part := read_form.NextPart()
if err_part == io.EOF {
break
} else if err_part != nil {
return c.JsonBadRequest("Invalid multipart data")
}
if part.FormName() == "id" {
buf := new(bytes.Buffer)
buf.ReadFrom(part)
id = buf.String()
}
if part.FormName() == "file" {
buf := new(bytes.Buffer)
buf.ReadFrom(part)
file = buf.Bytes()
}
}
model, err := GetBaseModel(handle.Db, id)
if err == ModelNotFoundError {
return c.JsonBadRequest("Models not found")
} else if err != nil {
return c.Error500(err)
}
if model.Status != READY && model.Status != READY_RETRAIN && model.Status != READY_RETRAIN_FAILED && model.Status != READY_ALTERATION && model.Status != READY_ALTERATION_FAILED {
return c.JsonBadRequest("Model not ready to run images")
}
def := JustId{}
err = GetDBOnce(c, &def, "model_definition where model_id=$1", model.Id)
if err == NotFoundError {
return c.JsonBadRequest("Could not find definition")
} else if err != nil {
return c.Error500(err)
}
def_id := def.Id
// TODO create a database table with tasks
run_path := path.Join("/tmp", model.Id, "runs")
os.MkdirAll(run_path, os.ModePerm)
img_path := path.Join(run_path, "img."+model.Format)
img_file, err := os.Create(img_path)
} else {
base.GetLogger().Info("Running model normal", "model", model.Id, "def", def_id)
vi, confidence, err = runModelNormal(base, model, def_id, inputImage)
if err != nil {
return c.Error500(err)
task.UpdateStatusLog(base, TASK_FAILED_RUNNING, "Failed to run model")
return
}
defer img_file.Close()
img_file.Write(file)
}
if !testImgForModel(c, model, img_path) {
return c.JsonBadRequest("Provided image does not match the model")
}
var GetName struct {
Name string
Id string
}
err = GetDBOnce(base.GetDb(), &GetName, "model_classes where model_id=$1 and class_order=$2;", model.Id, vi)
if err != nil {
task.UpdateStatusLog(base, TASK_FAILED_RUNNING, "Failed to obtain model results")
return
}
root := tg.NewRoot()
returnValue := struct {
ClassId string `json:"class_id"`
Class string `json:"class"`
Confidence float32 `json:"confidence"`
}{
Class: GetName.Name,
ClassId: GetName.Id,
Confidence: confidence,
}
var tf_img *image.Image = nil
err = task.SetResult(base, returnValue)
if err != nil {
task.UpdateStatusLog(base, TASK_FAILED_RUNNING, "Failed to save model results")
return
}
switch model.Format {
case "png":
tf_img = ReadPNG(root, img_path, int64(model.ImageMode))
case "jpeg":
tf_img = ReadJPG(root, img_path, int64(model.ImageMode))
default:
panic("Not sure what to do with '" + model.Format + "'")
}
task.UpdateStatusLog(base, TASK_DONE, "Model ran successfully")
exec_results := tg.Exec(root, []tf.Output{tf_img.Value()}, nil, &tf.SessionOptions{})
inputImage, err := tf.NewTensor(exec_results[0].Value())
if err != nil {
return c.Error500(err)
}
vi := -1
var confidence float32 = 0
if model.ModelType == 2 {
c.Logger.Info("Running model normal", "model", model.Id, "def", def_id)
vi, confidence, err = runModelExp(c, model, def_id, inputImage)
if err != nil {
return c.Error500(err)
}
} else {
c.Logger.Info("Running model normal", "model", model.Id, "def", def_id)
vi, confidence, err = runModelNormal(c, model, def_id, inputImage)
if err != nil {
return c.Error500(err)
}
}
os.RemoveAll(run_path)
rows, err := handle.Db.Query("select name from model_classes where model_id=$1 and class_order=$2;", model.Id, vi)
if err != nil {
return c.Error500(err)
}
if !rows.Next() {
return c.SendJSON(nil)
}
var name string
if err = rows.Scan(&name); err != nil {
return c.Error500(err)
}
returnValue := struct {
Class string `json:"class"`
Confidence float32 `json:"confidence"`
}{
Class: name,
Confidence: confidence,
}
return c.SendJSON(returnValue)
})
return
}

View File

@@ -10,7 +10,7 @@ import (
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
)
func testImgForModel(c *Context, model *BaseModel, path string) (result bool) {
func TestImgForModel(c *Context, model *BaseModel, path string) (result bool) {
result = false
infile, err := os.Open(path)

View File

@@ -58,11 +58,6 @@ func ModelDefinitionUpdateStatus(c *Context, id string, status ModelDefinitionSt
return
}
func UpdateStatus(c *Context, table string, id string, status int) (err error) {
_, err = c.Db.Exec(fmt.Sprintf("update %s set status = $1 where id = $2", table), status, id)
return
}
func MakeLayer(db *sql.DB, def_id string, layer_order int, layer_type LayerType, shape string) (err error) {
_, err = db.Exec("insert into model_definition_layer (def_id, layer_order, layer_type, shape) values ($1, $2, $3, $4)", def_id, layer_order, layer_type, shape)
return
@@ -341,7 +336,7 @@ func generateCvsExpandExp(c *Context, run_path string, model_id string, offset i
// This is to load some extra data so that the model has more things to train on
//
data_other, err := c.Db.Query("select mdp.id, mc.class_order, mdp.file_path from model_data_point as mdp inner join model_classes as mc on mc.id = mdp.class_id where mc.model_id = $1 and mdp.model_mode=$2 and mc.status=$3 limit $4;", model_id, model_classes.DATA_POINT_MODE_TRAINING, MODEL_CLASS_STATUS_TRAINED, count)
data_other, err := c.Db.Query("select mdp.id, mc.class_order, mdp.file_path from model_data_point as mdp inner join model_classes as mc on mc.id = mdp.class_id where mc.model_id = $1 and mdp.model_mode=$2 and mc.status=$3 limit $4;", model_id, model_classes.DATA_POINT_MODE_TRAINING, MODEL_CLASS_STATUS_TRAINED, count * 10)
if err != nil {
return
}

View File

@@ -5,18 +5,6 @@ import (
"errors"
)
type BaseModel struct {
Name string
Status int
Id string
ModelType int
ImageMode int
Width int
Height int
Format string
}
const (
FAILED_TRAINING = -4
FAILED_PREPARING_TRAINING = -3
@@ -75,6 +63,18 @@ const (
MODEL_HEAD_STATUS_READY = 5
)
type BaseModel struct {
Name string
Status int
Id string
ModelType int
ImageMode int
Width int
Height int
Format string
}
var ModelNotFoundError = errors.New("Model not found error")
func GetBaseModel(db *sql.DB, id string) (base *BaseModel, err error) {
@@ -99,6 +99,13 @@ func GetBaseModel(db *sql.DB, id string) (base *BaseModel, err error) {
return
}
func (m BaseModel) CanEval() bool {
if m.Status != READY && m.Status != READY_RETRAIN && m.Status != READY_RETRAIN_FAILED && m.Status != READY_ALTERATION && m.Status != READY_ALTERATION_FAILED {
return false
}
return true
}
func StringToImageMode(colorMode string) int {
switch colorMode {
case "greyscale":