File size: 1,098 Bytes
3322c73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import pickle
import argparse
import cv2
import numpy as np
from sklearn.svm import SVC
from sklearn.preprocessing import LabelEncoder
from sklearn.neighbors import KNeighborsClassifier



def train(storage_client, bucket_name, embeddings_file):

    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(embeddings_file)
    blob.download_to_filename(embeddings_file)

    data = pickle.loads(open(embeddings_file, "rb").read())

    # train a classification model on these embeddings
    # use the model to make predictions on the test data

    X = data['encodings']
    y_raw = data['names']

    le = LabelEncoder()
    y = le.fit_transform(y_raw)
    print(le.classes_)
    # save the labels in a file 
    f = open('labels.pkl', "wb")
    f.write(pickle.dumps(le.classes_))
    f.close()

    model = KNeighborsClassifier(n_neighbors=3)
    model.fit(X, y)

    accuracy = model.score(X, y)
    print(f'Accuracy: {accuracy}')

    #save the model to disk
    f = open('model.pkl', "wb")
    f.write(pickle.dumps(model))
    f.close()


if __name__ == '__main__':
    train()