-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
49 lines (40 loc) · 1.45 KB
/
test.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from keras.applications import inception_v3
from keras.models import load_model
from tqdm import tqdm
from util import get_labels, get_images, one_hot
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sn
from sklearn.metrics import confusion_matrix, accuracy_score, log_loss
# Define constants
INPUT_SIZE = 299
fname = 'model1_finetune.h5'
fpred = 'pred_val1.csv'
fconf = 'conf_mat1.csv'
# Load labels
print('Load labels...')
labels = get_labels()
# Load images
print('Load images...')
images = np.zeros((len(labels), INPUT_SIZE, INPUT_SIZE, 3), dtype='float16')
for i, (img, img_id) in tqdm(enumerate(get_images('train', INPUT_SIZE))):
x = inception_v3.preprocess_input(np.expand_dims(img, axis=0))
images[i] = x
# Load one-hot encodings
y_train = one_hot(labels['breed'].values)
# Load model weights
print(f'Load model from {fname}')
model = load_model(fname)
# Evaluate model on data
print('Predict...')
predictions = model.predict(images, verbose=1)
np.savetxt(fpred, predictions, delimiter=",")
print(f'Loss: {log_loss(y_train, predictions)}')
print(f'Accuracy: {accuracy_score(np.argmax(y_train, axis=1), np.argmax(predictions, axis=1))}')
conf_arr = confusion_matrix(np.argmax(y_train, axis=1), np.argmax(predictions, axis=1))
conf_arr = conf_arr / conf_arr.sum(axis=1)[:, np.newaxis]
print(f'Save confusion matrix to {fconf}')
np.savetxt(fconf, conf_arr, delimiter=",")