-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgas_meter_eval.py
71 lines (61 loc) · 2.06 KB
/
gas_meter_eval.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import pymongo
import ssl
import secret
from tensorflow.keras.preprocessing.image import ImageDataGenerator
client = pymongo.MongoClient(secret.connstring,
ssl=True,
ssl_cert_reqs=ssl.CERT_NONE) #connstring is a variable comming from secret.py containing mongo db connection string
#connstring is a variable comming from secret.py containing mongo db connection string
db=client.test
print(tf.__version__)
print(np.__version__)
model = tf.keras.models.load_model('model/digit_model6')
#preprocessing functions applied on picture to be predicted
datagen=ImageDataGenerator(
samplewise_center=True,
samplewise_std_normalization=True,
)
print("loading data")
cursor=db.gas_digit.find({
'label': {
'$in': [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
]
},
'dataset':{
'$in': [
'validation','training'
]
}
},{"image":1,"date":1,"label":1,'digit_position':1,'dataset':1})
def predict_class(im):
prediction = model.predict(datagen.flow(im))
return int(prediction.argmax())#convert numpy int64 to python int in order to be able to write to database later on
def showimage(image):
#preprocess picture for display
image=(image-image.min())/(image.max()-image.min())#normalize
image=image/image.max() #scale to 0-1
#plt.figure()
#plt.imshow(image[0])
#plt.colorbar()
#plt.grid(False)
#plt.ion()
#plt.show()
#plt.pause(0.1)
#plt.close()
for document in cursor:
id=document['_id']
image=np.array(document['image']).reshape(1,28,28,3)
label=document['label']
position=document['digit_position']
prediction=predict_class(image)
error=""
if prediction!=label:
error=" bad prediction!"
print("prediction:",predict_class(image),"label:",label,"position:",position," ",error)
db.gas_digit.update_one({"_id":id},{"$set":{"model1_prediction":prediction}})
showimage(image)