def build_edit_friend_form(home_username, friend_username, review): sql_instance = SQL() result = sql_instance.get_individual_friend( home_username=home_username, friend_username=friend_username) print(result) friend = Friend(username=result[0], user_id=result[1], first_name=result[2], last_name=result[3]) web_img_paths = [] if review: friend_path = os.path.abspath('static/img/out/needs_review/') print(friend_path) image_paths = list(paths.list_images(friend_path)) for image_path in image_paths: file_name = image_path.split('/')[-1] web_path = '../static/img/out/needs_review/' + file_name web_img_paths.append(web_path) print(web_img_paths) else: friend_path = os.path.abspath('static/img/data/' + str(friend.user_id)) print(friend_path) image_paths = list(paths.list_images(friend_path)) for image_path in image_paths: file_name = image_path.split('/')[-1] web_path = '../static/img/data/' + str( friend.user_id) + '/' + file_name web_img_paths.append(web_path) print(web_img_paths) return EditFriendForm(friend=friend, image_paths=web_img_paths)
def validate_sign_in(username, password): sql_instance = SQL() hashed_pass = sql_instance.get_hashed_pass(username=username) if hashed_pass is None: return False else: return pbkdf2_sha256.verify(password, hashed_pass)
def __init__(self, home_id, model_name): self.model_path = os.path.abspath('./static/models/' + str(home_id) + '/' + model_name + '.pt') model = torch.load(self.model_path) model.eval() self.model = model self.model_name = model_name sql_instance = SQL() self.model_id = sql_instance.get_model_id_by_name(model_name)
def validate_delete_friend(home_username, friend_username): sql_instance = SQL() friend = models.friend.load(home_username, friend_username) data_folder = os.path.abspath('./static/img/data/' + str(friend.user_id) + '/') training_folder = os.path.abspath('./static/img/out/training/' + str(friend.user_id) + '/') shutil.rmtree(data_folder) shutil.rmtree(training_folder) sql_instance.delete_friend(home_username, friend_username)
def validate_friend(username, first_name, last_name, home_username): sql_instance = SQL() hashed_pass = pbkdf2_sha256.hash(random_string()) try: sql_instance.add_friend( username=username, first_name=first_name, last_name=last_name, home_username=home_username, hashed_pass=hashed_pass ) return True except(): return False
def validate_sign_up(username, password, first_name, last_name): sql_instance = SQL() hashed_pass = pbkdf2_sha256.hash(password) try: sql_instance.create_account(username=username, password=hashed_pass, first_name=first_name, last_name=last_name) return True except (): return False
def build_friends_form(username): sql_instance = SQL() results = sql_instance.get_friends(username=username) friends = [] for friend in results: friends.append( Friend(username=friend[0], user_id=friend[1], first_name=friend[2], last_name=friend[3])) return FriendsForm(friends=friends)
def build_home_form(user_id, time_frame): sql_instance = SQL() time_stamp = get_time_stamp(time_frame) active_model_id = sql_instance.get_active_model_id(user_id) print('Active model id:', active_model_id) if active_model_id is not None: results = sql_instance.load_model_classifications_since_time_stamp(model_id=active_model_id, time_stamp=time_stamp) else: results = [] # need to convert these classifications to actual classification objects print(results) classifications = [] for c in results: classifications.append(Classification(c[0], c[1], c[2], c[3], c[4], c[5])) table_data = TableData(classifications=classifications, time_frame=time_frame.name) chart_y_values = [data_point.confidence for data_point in table_data.data_points] chart_x_values = [data_point.timestamp for data_point in table_data.data_points] return UserHomeForm(table_data=table_data, chart_x_values=chart_x_values, chart_y_values=chart_y_values)
def build_model_form(username): sql_instance = SQL() results = sql_instance.get_friends(username=username) user_id = sql_instance.get_user_id(username, username) friends = [] for friend in results: friends.append( Friend(username=friend[0], user_id=friend[1], first_name=friend[2], last_name=friend[3])) model_results = sql_instance.get_models_for_user(username=username) models = [] for model in model_results: classification_results = sql_instance.get_all_classifications_for_model( model_id=model[0]) classifications = [] for c in classification_results: orig_path = c[6] print(orig_path) web_path = '../static/img/out/training/' + orig_path.split( '/')[-2] + '/' + orig_path.split('/')[-1] classifications.append( Classification(c[1], c[2], c[3], c[4], c[5], web_path)) models.append( ModelData(model_id=model[0], classifications=classifications, file_path=model[1])) x_vals, y_vals = get_training_data_distribution(username, sql_instance) return ModelsForm(user_id=user_id, friends=friends, models=models, x_vals=x_vals, y_vals=y_vals)
def get_active_name(user_id): sql_instance = SQL() return sql_instance.get_active_model_name(user_id)
def get_active_id(user_id): sql_instance = SQL() return int(sql_instance.get_active_model_id(user_id))
def set_active(user_id, model_id): sql_instance = SQL() sql_instance.set_model_active(user_id, model_id)
def init_model_train_pipeline(home_id, friends, model_name, learning_rate=0.001, momentum=0.9, epochs=10000, split=0.2, hidden_layers=[20]): ce.encode_faces(friends) test_results, metrics = tm.train_classifier(home_id, friends, model_name, hidden_layers=hidden_layers, learning_rate=learning_rate, epochs=epochs, momentum=momentum, split=split) # now that model is trained, we need to create objects to be stored in mysql # we need Model - modelid, userid, filepath # ModelClassifications - on test set we need to return the user_id, model_id, is_train (no), timestamp, image_path, # and confidence model_path = os.path.abspath('./static/models/' + str(home_id) + '/' + model_name + '.pt') model = torch.load(model_path) model.eval() model_name = model_path.split('/')[-1][:-3] sql_instance = SQL() sql_instance.save_model(home_id, model_path, model_name) print(model_name) model_id = sql_instance.get_model_id_by_name(model_name) print(model_id) metrics.model_id = model_id cls = [] for result in test_results: r = [] train_img_index = result[2] result = result[0].tolist() r.append((result.index(max(result)), max(result), train_img_index)) cls.append(r) print(cls) classifications = [] image_paths = [] for f in friends: current_dir = os.path.abspath('./static/img/out/training/' + str(f.user_id) + '/') for img in os.listdir(current_dir): image_paths.append(current_dir + '/' + img) # for i, path in enumerate(image_paths): # print(i, path.split('/')[-2:]) for i, c in enumerate(cls): classifications.append( Classification(friends[c[0][0]].user_id, friends[c[0][0]].first_name, friends[c[0][0]].last_name, c[0][1], datetime.now(), image_paths[c[0][2]])) for c in classifications: print(c.user_id) sql_instance.add_classification(model_id, 1, c) metrics_path = os.path.abspath('./static/models/metrics/' + str(home_id) + '/') if not os.path.exists(metrics_path): os.mkdir(metrics_path) f = open(metrics_path + '/' + str(model_id) + '.pickle', "wb+") f.write(pickle.dumps(metrics)) f.close()
def save_classification(model_id, classification, is_train=False): sql_instance = SQL() sql_instance.add_classification(model_id, int(is_train), classification)