chore: moved model page to svelte

This commit is contained in:
2024-03-02 12:45:49 +00:00
parent e990b832d3
commit 30c5b57378
15 changed files with 1150 additions and 768 deletions

View File

@@ -42,9 +42,9 @@ 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 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) {
@@ -246,7 +246,7 @@ func trainDefinitionExp(c *Context, model *BaseModel, definition_id string, load
return
}
UpdateStatus(c, "exp_model_head", exp.id, MODEL_DEFINITION_STATUS_TRAINING)
UpdateStatus(c, "exp_model_head", exp.id, MODEL_DEFINITION_STATUS_TRAINING)
layers, err := c.Db.Query("select layer_type, shape, exp_type from model_definition_layer where def_id=$1 order by layer_order asc;", definition_id)
if err != nil {
@@ -283,7 +283,7 @@ func trainDefinitionExp(c *Context, model *BaseModel, definition_id string, load
got = append(got, layerrow{
LayerType: LAYER_DENSE,
Shape: fmt.Sprintf("%d", exp.end-exp.start + 1),
Shape: fmt.Sprintf("%d", exp.end-exp.start+1),
ExpType: 2,
LayerNum: i,
})
@@ -625,14 +625,14 @@ func trainModelExp(c *Context, model *BaseModel) {
var rowv TrainModelRow
rowv.acuracy = 0
if err = definitionsRows.Scan(&rowv.id, &rowv.target_accuracy, &rowv.epoch); err != nil {
failed("Failed to train Model Could not read definition from db!")
failed("Failed to train Model Could not read definition from db!")
return
}
definitions = append(definitions, rowv)
}
if len(definitions) == 0 {
failed("No Definitions defined!")
failed("No Definitions defined!")
return
}
@@ -661,13 +661,13 @@ func trainModelExp(c *Context, model *BaseModel) {
c.Logger.Info("Found a definition that reaches target_accuracy!")
_, err = c.Db.Exec("update model_definition set accuracy=$1, status=$2, epoch=$3 where id=$4", accuracy, MODEL_DEFINITION_STATUS_TRANIED, def.epoch, def.id)
if err != nil {
failed("Failed to train definition!")
failed("Failed to train definition!")
return
}
_, err = c.Db.Exec("update model_definition set status=$1 where id!=$2 and model_id=$3 and status!=$4", MODEL_DEFINITION_STATUS_CANCELD_TRAINING, def.id, model.Id, MODEL_DEFINITION_STATUS_FAILED_TRAINING)
if err != nil {
failed("Failed to train definition!")
failed("Failed to train definition!")
return
}
@@ -684,7 +684,7 @@ func trainModelExp(c *Context, model *BaseModel) {
_, err = c.Db.Exec("update model_definition set accuracy=$1, epoch=$2, status=$3 where id=$4", accuracy, def.epoch, MODEL_DEFINITION_STATUS_PAUSED_TRAINING, def.id)
if err != nil {
failed("Failed to train definition!")
failed("Failed to train definition!")
return
}
}
@@ -737,30 +737,30 @@ func trainModelExp(c *Context, model *BaseModel) {
rows, err := c.Db.Query("select id from model_definition where model_id=$1 and status=$2 order by accuracy desc limit 1;", model.Id, MODEL_DEFINITION_STATUS_TRANIED)
if err != nil {
failed("DB: failed to read definition")
failed("DB: failed to read definition")
return
}
defer rows.Close()
if !rows.Next() {
failed("All definitions failed to train!")
failed("All definitions failed to train!")
return
}
var id string
if err = rows.Scan(&id); err != nil {
failed("Failed to read id")
failed("Failed to read id")
return
}
if _, err = c.Db.Exec("update model_definition set status=$1 where id=$2;", MODEL_DEFINITION_STATUS_READY, id); err != nil {
failed("Failed to update model definition")
failed("Failed to update model definition")
return
}
to_delete, err := c.Db.Query("select id from model_definition where status != $1 and model_id=$2", MODEL_DEFINITION_STATUS_READY, model.Id)
if err != nil {
failed("Failed to select model_definition to delete")
failed("Failed to select model_definition to delete")
return
}
defer to_delete.Close()
@@ -768,7 +768,7 @@ func trainModelExp(c *Context, model *BaseModel) {
for to_delete.Next() {
var id string
if to_delete.Scan(&id); err != nil {
failed("Failed to scan the id of a model_definition to delete")
failed("Failed to scan the id of a model_definition to delete")
return
}
os.RemoveAll(path.Join("savedData", model.Id, "defs", id))
@@ -776,24 +776,24 @@ func trainModelExp(c *Context, model *BaseModel) {
// TODO Check if returning also works here
if _, err = c.Db.Exec("delete from model_definition where status!=$1 and model_id=$2;", MODEL_DEFINITION_STATUS_READY, model.Id); err != nil {
failed("Failed to delete model_definition")
failed("Failed to delete model_definition")
return
}
if err = splitModel(c, model); err != nil {
failed("Failed to split the model")
if err = splitModel(c, model); err != nil {
failed("Failed to split the model")
return
}
// There should only be one def availabale
def := JustId{}
}
if err = GetDBOnce(c, &def, "model_definition where model_id=$1", model.Id); err != nil {
return
}
// Remove the base model
c.Logger.Warn("Removing base model for", "model", model.Id, "def", def.Id)
// There should only be one def availabale
def := JustId{}
if err = GetDBOnce(c, &def, "model_definition where model_id=$1", model.Id); err != nil {
return
}
// Remove the base model
c.Logger.Warn("Removing base model for", "model", model.Id, "def", def.Id)
os.RemoveAll(path.Join("savedData", model.Id, "defs", def.Id, "model"))
os.RemoveAll(path.Join("savedData", model.Id, "defs", def.Id, "model.keras"))
@@ -802,17 +802,17 @@ func trainModelExp(c *Context, model *BaseModel) {
func splitModel(c *Context, model *BaseModel) (err error) {
def := JustId{}
def := JustId{}
if err = GetDBOnce(c, &def, "model_definition where model_id=$1", model.Id); err != nil {
return
}
if err = GetDBOnce(c, &def, "model_definition where model_id=$1", model.Id); err != nil {
return
}
head := JustId{}
head := JustId{}
if err = GetDBOnce(c, &head, "exp_model_head where def_id=$1", def.Id); err != nil {
return
}
if err = GetDBOnce(c, &head, "exp_model_head where def_id=$1", def.Id); err != nil {
return
}
// Generate run folder
run_path := path.Join("/tmp", model.Id, "defs", def.Id)
@@ -821,7 +821,7 @@ func splitModel(c *Context, model *BaseModel) (err error) {
if err != nil {
return
}
// TODO reneable it
// TODO reneable it
// defer os.RemoveAll(run_path)
// Create python script
@@ -838,9 +838,9 @@ func splitModel(c *Context, model *BaseModel) (err error) {
// Copy result around
result_path := path.Join(getDir(), "savedData", model.Id, "defs", def.Id)
// TODO maybe move this to a select count(*)
// Get only fixed lawers
// TODO maybe move this to a select count(*)
// Get only fixed lawers
layers, err := c.Db.Query("select exp_type from model_definition_layer where def_id=$1 and exp_type=$2 order by layer_order asc;", def.Id, 1)
if err != nil {
return
@@ -848,24 +848,24 @@ func splitModel(c *Context, model *BaseModel) (err error) {
defer layers.Close()
type layerrow struct {
ExpType int
ExpType int
}
count := -1
count := -1
for layers.Next() {
count += 1
count += 1
}
if count == -1 {
err = errors.New("Can not get layers")
return
}
if count == -1 {
err = errors.New("Can not get layers")
return
}
log.Warn("Spliting model", "def", def.Id, "head", head.Id, "count", count)
log.Warn("Spliting model", "def", def.Id, "head", head.Id, "count", count)
basePath := path.Join(result_path, "base")
headPath := path.Join(result_path, "head", head.Id)
basePath := path.Join(result_path, "base")
headPath := path.Join(result_path, "head", head.Id)
if err = os.MkdirAll(basePath, os.ModePerm); err != nil {
return
@@ -876,10 +876,10 @@ func splitModel(c *Context, model *BaseModel) (err error) {
}
if err = tmpl.Execute(f, AnyMap{
"SplitLen": count,
"ModelPath": path.Join(result_path, "model.keras"),
"BaseModelPath": basePath,
"HeadModelPath": headPath,
"SplitLen": count,
"ModelPath": path.Join(result_path, "model.keras"),
"BaseModelPath": basePath,
"HeadModelPath": headPath,
}); err != nil {
return
}
@@ -892,7 +892,7 @@ func splitModel(c *Context, model *BaseModel) (err error) {
c.Logger.Info("Python finished running")
return
return
}
func removeFailedDataPoints(c *Context, model *BaseModel) (err error) {
@@ -1089,7 +1089,7 @@ func ExpModelHeadUpdateStatus(db *sql.DB, id string, status ModelDefinitionStatu
// This generates a definition
func generateExpandableDefinition(c *Context, model *BaseModel, target_accuracy int, number_of_classes int, complexity int) *Error {
c.Logger.Info("Generating expandable new definition for model", "id", model.Id, "complexity", complexity)
c.Logger.Info("Generating expandable new definition for model", "id", model.Id, "complexity", complexity)
var err error = nil
failed := func() *Error {
@@ -1133,13 +1133,13 @@ func generateExpandableDefinition(c *Context, model *BaseModel, target_accuracy
// Create the blocks
loop := int((math.Log(float64(model.Width)) / math.Log(float64(10))))
if model.Width < 50 && model.Height < 50 {
loop = 0
}
if model.Width < 50 && model.Height < 50 {
loop = 0
}
log.Info("Size of the simple block", "loop", loop)
log.Info("Size of the simple block", "loop", loop)
//loop = max(loop, 3)
//loop = max(loop, 3)
for i := 0; i < loop; i++ {
err = MakeLayerExpandable(c.Db, def_id, order, LAYER_SIMPLE_BLOCK, "", 1)
@@ -1165,9 +1165,9 @@ func generateExpandableDefinition(c *Context, model *BaseModel, target_accuracy
loop = int((math.Log(float64(number_of_classes)) / math.Log(float64(10))) / 2)
log.Info("Size of the dense layers", "loop", loop)
log.Info("Size of the dense layers", "loop", loop)
// loop = max(loop, 3)
// loop = max(loop, 3)
for i := 0; i < loop; i++ {
err = MakeLayer(c.Db, def_id, order, LAYER_DENSE, fmt.Sprintf("%d,1", number_of_classes*(loop-i)))
@@ -1176,7 +1176,7 @@ func generateExpandableDefinition(c *Context, model *BaseModel, target_accuracy
return failed()
}
}
_, err = CreateExpModelHead(c, def_id, 0, number_of_classes-1, MODEL_DEFINITION_STATUS_INIT)
if err != nil {
return failed()
@@ -1190,6 +1190,7 @@ func generateExpandableDefinition(c *Context, model *BaseModel, target_accuracy
return nil
}
// TODO make this json friendy
func generateExpandableDefinitions(c *Context, model *BaseModel, target_accuracy int, number_of_models int) *Error {
cls, err := model_classes.ListClasses(c.Db, model.Id)
if err != nil {
@@ -1225,13 +1226,77 @@ func generateExpandableDefinitions(c *Context, model *BaseModel, target_accuracy
return nil
}
func handle_models_train_json(w http.ResponseWriter, r *http.Request, c *Context) *Error {
var dat struct {
Id string `json:"id"`
ModelType string `json:"model_type"`
NumberOfModels int `json:"number_of_models"`
Accuracy int `json:"accuracy"`
}
if err_ := c.ToJSON(r, &dat); err_ != nil {
return err_
}
if dat.Id == "" {
return c.JsonBadRequest("Please provide a id")
}
modelTypeId := 1
if dat.ModelType == "expandable" {
modelTypeId = 2
} else if dat.ModelType != "simple" {
return c.JsonBadRequest("Invalid model type!")
}
model, err := GetBaseModel(c.Db, dat.Id)
if err == ModelNotFoundError {
return c.JsonBadRequest("Model not found")
} else if err != nil {
return c.Error500(err)
}
if model.Status != CONFIRM_PRE_TRAINING {
return c.JsonBadRequest("Model in invalid status for training")
}
if modelTypeId == 2 {
full_error := generateExpandableDefinitions(c, model, dat.Accuracy, dat.NumberOfModels)
if full_error != nil {
return full_error
}
} else {
full_error := generateDefinitions(c, model, dat.Accuracy, dat.NumberOfModels)
if full_error != nil {
return full_error
}
}
if modelTypeId == 2 {
go trainModelExp(c, model)
} else {
go trainModel(c, model)
}
_, err = c.Db.Exec("update models set status = $1, model_type = $2 where id = $3", TRAINING, modelTypeId, model.Id)
if err != nil {
fmt.Println("Failed to update model status")
fmt.Println(err)
// TODO improve this response
return Error500(err)
}
return c.SendJSON(model.Id)
}
func handleTrain(handle *Handle) {
handle.Post("/models/train", func(w http.ResponseWriter, r *http.Request, c *Context) *Error {
if !CheckAuthLevel(1, w, r, c) {
return nil
}
if c.Mode == JSON {
panic("TODO /models/train JSON")
return handle_models_train_json(w, r, c)
}
r.ParseForm()