forked from acse-srm3018/DeeplearningProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
107 lines (100 loc) · 3.37 KB
/
tests.py
File metadata and controls
107 lines (100 loc) · 3.37 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
### Unit test for some models including RR U-net and A RR U-net###
# import required libraries and packages
import unittest
import vae_uae as vae
from inception.utils import download_file_from_google_drive, image_to_tensor, batchnorm
from pathlib import Path
import tensorflow as tf
import os
from tensorflow.keras.callbacks import EarlyStopping
import numpy as np
import unet_uae as vae_util
import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"
from tensorflow.keras import layers
from tensorflow.keras.models import load_model, Model
from tensorflow.python.keras import backend as K
from keras.optimizers import Adam
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.3
sess = tf.compat.v1.Session(config=config)
K.set_session(sess)
def make_predictions(model, test_path):
"""
Testing to ensure model input and true values enter to model correctly.
Parameters
----------
model: Dict
the model
test path: string
the location of test file
Returns
-------
correct_preds: float
tensor including targets
all_samples: float
tensor including all_samples
"""
correct_preds = 0
all_samples = 0
for _, _, filenames in os.walk(test_path):
all_samples = len(filenames)
for filename in filenames:
X = batchnorm(image_to_tensor(test_path + filename)).to('cpu')
pred = int(model(X).max(1)[1].item()) + 1
label = int(filename.split('_')[0])
correct_preds += 1 if pred == label else 0
return correct_preds, all_samples
class TestModelOutputShape(unittest.TestCase):
"""
Tests to ensure the output tensor has the correct shape.
"""
def test_vae_net(self):
"""
Testing vae_net model with test data
----------
Returns
-------
True: boolean
"""
model = inception_v2(pretrained=True).to('cpu')
x = torch.ones(8, 3, 299, 299)
y = model(x)
self.assertEqual(y.shape, torch.Size([8, 4]))
return True
class TestModelAccuracy(unittest.TestCase):
"""
Testing to nsure about shaping of model output
"""
def __init__(self, *args, **kwargs):
"""
Testing inception_v2 model with test data
----------
Returns
-------
True: boolean
"""
super(TestModelAccuracy, self).__init__(*args, **kwargs)
self.dir_path = os.path.dirname(os.path.realpath(__file__)) + '/test_images'
test_dir = Path(self.dir_path)
if not test_dir.is_dir():
zip_path = self.dir_path + '.zip'
print("Download test images")
download_file_from_google_drive('1nILi82OjswYySH631dcLaHZyoVB4cNJS', zip_path)
print("Extract test images")
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(self.dir_path)
os.remove(zip_path)
else:
print('Test images are already downloaded')
self.dir_path += '/test_images/'
def test_vae_net(self):
"""
Testing vae_net model
"""
model = inception_v2(pretrained=True).to('cpu')
correct_preds, all_samples = make_predictions(model, self.dir_path)
self.assertGreater(correct_preds, 0.6 * all_samples)
if __name__ == '__main__':
unittest.main()