|
a |
|
b/eval.py |
|
|
1 |
from __future__ import absolute_import, division, print_function |
|
|
2 |
|
|
|
3 |
from os import environ, getcwd |
|
|
4 |
from os.path import join |
|
|
5 |
|
|
|
6 |
import keras |
|
|
7 |
import numpy as np |
|
|
8 |
import pandas as pd |
|
|
9 |
import sklearn as skl |
|
|
10 |
import tensorflow as tf |
|
|
11 |
from keras.applications import NASNetMobile |
|
|
12 |
from keras.layers import Dense, GlobalAveragePooling2D |
|
|
13 |
from keras.metrics import binary_accuracy, binary_crossentropy |
|
|
14 |
from keras.models import Model |
|
|
15 |
from keras.optimizers import Adam |
|
|
16 |
from keras.preprocessing.image import ImageDataGenerator |
|
|
17 |
|
|
|
18 |
from mura import Mura |
|
|
19 |
|
|
|
20 |
pd.set_option('display.max_rows', 20) |
|
|
21 |
pd.set_option('precision', 4) |
|
|
22 |
np.set_printoptions(precision=4) |
|
|
23 |
|
|
|
24 |
environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Shut up tensorflow! |
|
|
25 |
print("tf : {}".format(tf.__version__)) |
|
|
26 |
print("keras : {}".format(keras.__version__)) |
|
|
27 |
print("numpy : {}".format(np.__version__)) |
|
|
28 |
print("pandas : {}".format(pd.__version__)) |
|
|
29 |
print("sklearn : {}".format(skl.__version__)) |
|
|
30 |
|
|
|
31 |
# Hyper-parameters / Globals |
|
|
32 |
BATCH_SIZE = 512 # tweak to your GPUs capacity |
|
|
33 |
IMG_HEIGHT = 224 # ResNetInceptionv2 & Xception like 299, ResNet50/VGG/Inception 224, NASM 331 |
|
|
34 |
IMG_WIDTH = IMG_HEIGHT |
|
|
35 |
CHANNELS = 3 |
|
|
36 |
DIMS = (IMG_HEIGHT, IMG_WIDTH, CHANNELS) # blame theano |
|
|
37 |
MODEL_TO_EVAL = './models/NASNetMobile.hdf5' |
|
|
38 |
DATA_DIR = 'MURA-v1.0/' |
|
|
39 |
EVAL_CSV = 'valid.csv' |
|
|
40 |
EVAL_DIR = 'data/val' |
|
|
41 |
|
|
|
42 |
# load up our csv with validation factors |
|
|
43 |
data_dir = join(getcwd(), DATA_DIR) |
|
|
44 |
eval_csv = join(data_dir, EVAL_CSV) |
|
|
45 |
df = pd.read_csv(eval_csv, names=['img', 'label'], header=None) |
|
|
46 |
eval_imgs = df.img.values.tolist() |
|
|
47 |
eval_labels = df.label.values.tolist() |
|
|
48 |
|
|
|
49 |
eval_datagen = ImageDataGenerator(rescale=1. / 255) |
|
|
50 |
eval_generator = eval_datagen.flow_from_directory( |
|
|
51 |
EVAL_DIR, class_mode='binary', shuffle=False, target_size=(IMG_HEIGHT, IMG_WIDTH), batch_size=BATCH_SIZE) |
|
|
52 |
n_samples = eval_generator.samples |
|
|
53 |
base_model = NASNetMobile(input_shape=DIMS, weights='imagenet', include_top=False) |
|
|
54 |
x = base_model.output |
|
|
55 |
x = GlobalAveragePooling2D(name='avg_pool')(x) # comment for RESNET |
|
|
56 |
x = Dense(1, activation='sigmoid', name='predictions')(x) |
|
|
57 |
model = Model(inputs=base_model.input, outputs=x) |
|
|
58 |
model.load_weights(MODEL_TO_EVAL) |
|
|
59 |
model.compile(optimizer=Adam(lr=1e-3), loss=binary_crossentropy, metrics=['binary_accuracy']) |
|
|
60 |
score, acc = model.evaluate_generator(eval_generator, n_samples / BATCH_SIZE) |
|
|
61 |
print(model.metrics_names) |
|
|
62 |
print('==> Metrics with eval') |
|
|
63 |
print("loss :{:0.4f} \t Accuracy:{:0.4f}".format(score, acc)) |
|
|
64 |
y_pred = model.predict_generator(eval_generator, n_samples / BATCH_SIZE) |
|
|
65 |
mura = Mura(eval_generator.filenames, y_true=eval_generator.classes, y_pred=y_pred) |
|
|
66 |
print('==> Metrics with predict') |
|
|
67 |
print(mura.metrics()) |
|
|
68 |
print(mura.metrics_by_encounter()) |
|
|
69 |
# print(mura.metrics_by_study_type()) |