-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_network.py
48 lines (38 loc) · 1.41 KB
/
test_network.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
from __future__ import print_function
import tensorflow as tf
from keras.models import load_model
from imutils import paths
import numpy as np
import argparse
import imutils
import cv2
from tensorflow.keras.models import load_model
def image_to_feature_vector(image, size=(32, 32)):
return cv2.resize(image, size).flatten()
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--model", required=True,
help="path to output model file")
ap.add_argument("-t", "--test-images", required=True,
help="path to the directory of testing images")
ap.add_argument("-b", "--batch-size", type=int, default=32,
help="size of mini-batches passed to network")
args = vars(ap.parse_args())
CLASSES = ["cat", "dog"]
# load the network
print("[INFO] loading network architecture and weights...")
model = tf.keras.models.load_model('cnnCat2.h5')
print("[INFO] testing on images in {}".format(args["test_images"]))
for imagePath in paths.list_images(args["test_images"]):
print("[INFO] classifying {}".format(
imagePath[imagePath.rfind("/") + 1:]))
image = cv2.imread(imagePath)
features = image_to_feature_vector(image) / 255.0
features = np.array([features])
probs = model.predict(features)[0]
prediction = probs.argmax(axis=0)
label = "{}: {:.2f}%".format(CLASSES[prediction],
probs[prediction] * 100)
cv2.putText(image, label, (10, 35), cv2.FONT_HERSHEY_SIMPLEX,
1.0, (0, 255, 0), 3)
cv2.imshow("Image", image)
cv2.waitKey(0)