-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathTestResNet.py
69 lines (57 loc) · 1.9 KB
/
TestResNet.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
"""
Extract ResNet feature
Author: Kaihua Tang
"""
import math
import time
import tensorflow as tf
import ResNet as resnet
import numpy as np
import scipy.io as scio
from scipy import misc
from utils import *
# image size
WIDTH = 224
HEIGHT = 224
CHANNELS = 3
#Number of output labels
LABELSNUM = 1200
#"Path of Label.npy"
label_path = "./label/label.npy"
#"Path of image file names"
image_name_path = "./label/name.npy"
# image path
parentPath = "F:\\CACD2000_Crop\\"
def CalculateFeature():
"""
EXtract ResNet Feature by trained model
model_path: The model we use
feature_path: The path to save feature
"""
model_path = "./model/03.npy"
feature_path = "./resnet_feature.mat"
#Lists that store name of image and its label
testNameList = np.load(image_name_path)
testLabelList = np.load(label_path)
#num of total training image
num_test_image = testLabelList.shape[0]
#load all image data
allImageData = load_all_image(testNameList, HEIGHT, WIDTH, CHANNELS, parentPath)
#container for ResNet Feature
res_feature = np.zeros((num_test_image, 2048))
with tf.Session() as sess:
images = tf.placeholder(tf.float32, shape = [None, WIDTH, HEIGHT, CHANNELS])
# build resnet model
resnet_model = resnet.ResNet(ResNet_npy_path = model_path)
resnet_model.build(images, LABELSNUM, "softmax")
sess.run(tf.global_variables_initializer())
resnet_model.set_is_training(False)
for i in range(num_test_image):
if(i%1000 == 0):
print(i)
(minibatch_X, minibatch_Y) = get_minibatch([i], testLabelList, HEIGHT, WIDTH, CHANNELS, LABELSNUM, allImageData, True)
pool2 = sess.run([resnet_model.pool2], feed_dict={images: minibatch_X})
res_feature[i][:] = pool2[0][:]
scio.savemat(feature_path,{'feature' : res_feature})
if __name__ == '__main__':
CalculateFeature()