File size: 1,742 Bytes
961d213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tensorflow as tf
from keras.models import load_model
from keras.layers import Layer, Softmax
import numpy as np

test_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1. / 255)

test_dir = "./dataset/test"
batch_size = 1

test_data = test_datagen.flow_from_directory(
    test_dir,
    target_size=(224, 224),
    color_mode="rgb",
    batch_size=batch_size,
    class_mode="categorical",
    shuffle=True,
    seed=42
)

class_names = ['Black Widow',
               'Blue Tarantula',
               'Bold Jumper',
               'Brown Grass Spider',
               'Brown Recluse Spider',
               'Deinopis Spider',
               'Golden Orb Weaver',
               'Hobo Spider',
               'Huntsman Spider',
               'Ladybird Mimic Spider',
               'Peacock Spider',
               'Red Knee Tarantula',
               'Spiny-backed Orb-weaver',
               'White Kneed Tarantula',
               'Yellow Garden Spider']


class Normalization(Layer):
    def __init__(self, name=None, **kwargs):
        super(Normalization, self).__init__()
        self.mean = [0.485, 0.456, 0.406]
        self.std = [0.229, 0.224, 0.225]

    def call(self, inputs):
        return (inputs - self.mean) / self.std


cnn_model = load_model("./spider.h5", custom_objects={'Normalization': Normalization})
image, label = next(iter(test_data))
cnt = 0
max_num = 0
for (image, label) in test_data:
    max_num += 1
    y_hat = np.argmax(cnn_model.predict(image)[0])
    y_true = np.argmax(label)
    if y_true == y_hat:
        cnt += 1
    print("{:30s}{:30s}{}".format(class_names[y_hat], class_names[y_true], y_true == y_hat))
    if max_num == 200:
        break

print("Accuracy: ", cnt/max_num)