-
Notifications
You must be signed in to change notification settings - Fork 18
/
get_logits_from_xception.py
120 lines (85 loc) · 3.44 KB
/
get_logits_from_xception.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
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
108
109
110
111
112
113
114
115
116
117
118
119
import numpy as np
from tqdm import tqdm
import sys, os
import json
from utils.image_preprocessing_ver1 import ImageDataGenerator
# it outputs not only x_batch and y_batch but also image names
from keras.models import Model as KerasModel
from xception import Xception, preprocess_input
import argparse
parser = argparse.ArgumentParser(description='Process input arguments')
parser.add_argument('--data-folder', default='256_ObjectCategories_preproc', type=str, dest='data_dir', help='data folder mounting point')
parser.add_argument('--batch_size', dest="batch_size", default=64, help='Batch size', type=int, required=False)
parser.add_argument('--output_data', default='./data/preprocessed/UCSDped1', type=str, dest='output_data', help='data folder mounting point')
args = parser.parse_args()
data_dir = args.data_dir
batch_size = args.batch_size
output_data = args.output_data
data_generator = ImageDataGenerator(
data_format='channels_last',
preprocessing_function=preprocess_input
)
train_generator = data_generator.flow_from_directory(
os.path.join(data_dir, 'train_no_resizing'),
target_size=(299, 299),
batch_size=batch_size, shuffle=False
)
val_generator = data_generator.flow_from_directory(
os.path.join(data_dir, 'val_no_resizing'),
target_size=(299, 299),
batch_size=batch_size, shuffle=False
)
train_samples = train_generator.__dict__['samples']
val_samples = val_generator.__dict__['samples']
# # Get model and remove the last layer
from azureml.core import Workspace
from azureml.core.authentication import ServicePrincipalAuthentication
from azureml.core.model import Model
from azureml.exceptions._azureml_exception import ModelNotFoundException
config_json = 'config.json'
with open(config_json, 'r') as f:
config = json.load(f)
try:
svc_pr = ServicePrincipalAuthentication(
tenant_id=config['tenant_id'],
service_principal_id=config['service_principal_id'],
service_principal_password=config['service_principal_password'])
except KeyError as e:
print("Getting Service Principal Authentication from Azure Devops")
svr_pr = None
pass
ws = Workspace.from_config(path=config_json, auth=svc_pr)
try:
model_root = Model.get_model_path('trained_xception', _workspace=ws)
except ModelNotFoundException as e:
print("Didn't find model, cannot perform knowledge distillation.")
model = Xception()
model.load_weights(os.path.join(model_root, "xception_weights.hdf5"))
# Remove softmax
model.layers.pop()
# Now model outputs logits
model = KerasModel(model.input, model.layers[-1].output)
# # Save logits as a dict: image name -> logit (256 dimensional vector)
train_logits = {}
batches = 0
for x_batch, _, name_batch in tqdm(train_generator):
batch_logits = model.predict_on_batch(x_batch)
for i, n in enumerate(name_batch):
train_logits[n] = batch_logits[i]
batches += 1
if batches >= train_samples / batch_size:
break
# We do the same for the validation set
val_logits = {}
batches = 0
for x_batch, _, name_batch in tqdm(val_generator):
batch_logits = model.predict_on_batch(x_batch)
for i, n in enumerate(name_batch):
val_logits[n] = batch_logits[i]
batches += 1
if batches >= val_samples / batch_size:
break
# Save logits
os.makedirs(output_data, exist_ok=True)
np.save(os.path.join(output_data, 'train_logits.npy'), train_logits)
np.save(os.path.join(output_data, 'val_logits.npy'), val_logits)