Skip to content

Commit

Permalink
v0.1.1
Browse files Browse the repository at this point in the history
Enhancements

Issue #2
 - Model saving mechanism rewritten from scratch (using timestamp as name)
  - Every model will be now saved in a different directory
  - Every data related to the model (dataset + configuration) will be saved in the same folder
  - Configuration file changed due to new implementation of model folder
  - dump_model (dataset) rewritten and migrated to utils
  - dump_model (classifier) rewriten in order to be compliant with new folder architecture
 - Remove migrated parallelism from "different person" from "different image same person"
 - Enabled progress bar during face analysis
 - Response constuctor will now accept parameter

Issue #4
 - Create function for retrieve the dataset from the input HTML form and return to tune/train function
 - Standardize and refactor logic for train/tune

BugFix
 - Dump the real classifier (grid.best_estimator_)
  • Loading branch information
alessiosavi committed May 21, 2019
1 parent 73055f1 commit 22b6d27
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 154 deletions.
101 changes: 34 additions & 67 deletions api/Api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
"""
Custom function that will be wrapped for be HTTP compliant
"""
import os
import pickle

import time
import zipfile
from datetime import datetime
from logging import getLogger
from os.path import join as path_join

from datastructure.Response import Response
from utils.util import print_prediction_on_image, random_string, remove_dir, unzip_data
from utils.util import print_prediction_on_image, random_string, retrieve_dataset

log = getLogger()

Expand All @@ -24,12 +22,12 @@ def predict_image(img_path, clf, PREDICTION_PATH):
:return: Response dictionary jsonizable
"""
response = Response()
log.debug("predict_image | Predicting {}".format(img_path))
if clf is None:
log.error("predict_image | FATAL | Classifier is None!")
prediction = None
else:
prediction = clf.predict(img_path)
log.debug("predict_image | Image analyzed!")
log.debug("predict_image | Predicting {}".format(img_path))
prediction = clf.predict(img_path, distance_threshold=0.45)
# Manage success
if prediction is not None and isinstance(prediction, list) and len(prediction) == 1:
img_name = random_string() + ".png"
Expand Down Expand Up @@ -79,23 +77,23 @@ def train_network(folder_uncompress, zip_file, clf):
:param clf:
:return:
"""
log.debug("train_network | uncompressing zip file ...")
folder_name = path_join(folder_uncompress, random_string())
zip_ref = zipfile.ZipFile(zip_file)
zip_ref.extractall(folder_name)
zip_ref.close()
log.debug("train_network | zip file uncompressed!")
clf.init_peoples_list(peoples_path=folder_name)
dataset = clf.init_dataset()
neural_model_file = clf.train(dataset["X"], dataset["Y"])
log.debug("train_network | Removing unzipped files")
remove_dir(folder_name)
response = Response()
response.status = "OK"
response.data = neural_model_file
response.description = "Model succesfully trained!"

return response.__dict__
log.debug("train_network | Starting training phase ...")
dataset = retrieve_dataset(folder_uncompress, zip_file, clf)

if dataset is None:
return Response(error="ERROR DURING LOADING DAT", description="Seems that the dataset is not valid").__dict__

else:
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
neural_model_file, elapsed_time = clf.train(dataset["X"], dataset["Y"], timestamp)

response = Response(status="OK", data=neural_model_file)
response.description = "Model succesfully trained! | {}".format(
time.strftime("%H:%M:%S.%f", time.gmtime(elapsed_time)))
log.debug("train_network | Tuning phase finihsed! | {}".format(response.description))

return response.__dict__


def tune_network(folder_uncompress, zip_file, clf):
Expand All @@ -106,50 +104,19 @@ def tune_network(folder_uncompress, zip_file, clf):
:param clf:
:return:
"""
log.debug("tune_network | uncompressing zip file ...")
check = verify_extension(zip_file.filename)
if check == "zip": # Image provided
folder_name = unzip_data(folder_uncompress, zip_file)
log.debug("tune_network | zip file uncompressed!")
clf.init_peoples_list(peoples_path=folder_name)
dataset = clf.init_dataset()
elif check == "dat":
dataset = pickle.load(zip_file)
log.debug("tune_network | Starting tuning phase ...")
dataset = retrieve_dataset(folder_uncompress, zip_file, clf)

if dataset is None:
return Response(error="ERROR DURING LOADING DAT", description="Seems that the dataset is not valid").__dict__

else:
dataset = None

if dataset is not None:
start_time = time.time()
neural_model_file = clf.tuning(dataset["X"], dataset["Y"])
elapsed_time = time.time() - start_time

log.debug("tune_network | Removing unzipped files")
if check == "zip":
# TODO: Refactor this method :/
remove_dir(folder_name)
response = Response()
response.status = "OK"
response.data = neural_model_file
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
neural_model_file, elapsed_time = clf.tuning(dataset["X"], dataset["Y"], timestamp)

response = Response(status="OK", data=neural_model_file)
response.description = "Model succesfully trained! | {}".format(
time.strftime("%H:%M:%S.%f", time.gmtime(elapsed_time)))
else:
response = Response()
response.error = "ERROR DURING LOADING DAT"
return response.__dict__

log.debug("train_network | Tuning phase finihsed! | {}".format(response.description))

def verify_extension(file):
"""
Wrapper for validate file
:param file:
:return:
"""
extension = os.path.splitext(file)[1]
log.debug("verify_extension | File: {} | Ext: {}".format(file, extension))
if extension == ".zip":
# In this case we have to analyze the photos
return "zip"
elif extension == ".dat":
# Photos have been alredy analyzed, dataset is ready!
return "dat"
return None
return response.__dict__
13 changes: 9 additions & 4 deletions conf/test.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"PyRecognizer": {
"Version": "0.0.1",
"Version": "0.1.1 ",
"temp_upload_training": "uploads/training/",
"temp_upload_predict": "uploads/predict/",
"temp_upload": "uploads/upload"
Expand All @@ -23,9 +23,14 @@
"classifier": {
"trainin_dir": "dataset/images/",
"model_path": "dataset/model/",
"model": "model-20190518_191827.clf",
"n_neighbors": "",
"knn_algo": ""
"timestamp": "20190521_131449",
"params": {
"algorithm": "ball_tree",
"metric": "minkowski",
"n_neighbors": 78,
"p": 2,
"weights": "distance"
}
},
"data": {
"test_data": "/tmp/test_data/"
Expand Down
Loading

0 comments on commit 22b6d27

Please sign in to comment.