forked from Azure/ImageSimilarityUsingCntk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA2_featurizeImagesInDir.py
75 lines (63 loc) · 2.81 KB
/
A2_featurizeImagesInDir.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
# -*- coding: utf-8 -*-
from helpers import *
from helpers_cntk import *
# This script shows how to use the refined model to computes
# features for all images in a directory. Note that this
# script does not import any parameters and file paths
# specified in PARAMETERS.py.
####################################
# Parameters
####################################
imgDir = "./data/fashionTexture/leopard/" # Directory with images to featurize
modelPath = "./proc/fashionTexture/cntk.model" # Path to trained CNTK model anywhere on disk
outFeaturesPath = "features.pickle" # Output location where computed features will be save to
boImgDirRecursive = False # Set to "true" if images are in sub-folders within imgDir
####################################
# Main
####################################
random.seed(0)
printDeviceType()
# Load cntk model
print("Loading CNTK model from: " + modelPath)
if not os.path.exists(modelPath):
raise Exception("Model file does not exist: " + modelPath)
model = load_model(modelPath)
node = model.find_by_name("poolingLayer")
model = combine([node.owner])
# Get list of images and image IDs
if boImgDirRecursive:
imgUUIs = []
imgPaths = []
subdirs = getDirectoriesInDirectory(imgDir)
for subdir in subdirs:
for filename in getFilesInDirectory(os.path.join(imgDir,subdir), ".jpg"):
imgPaths.append(os.path.join(imgDir, subdir, filename))
imgUUIs.append(subdir + "/" + filename)
else:
filenames = getFilesInDirectory(imgDir, ".jpg")
imgPaths = [os.path.join(imgDir, f) for f in filenames]
imgUUIs = filenames
if len(imgPaths) == 0:
raise Exception("No jpeg images found in directory " + imgDir)
# Featurize each image
feats = dict()
width, height = find_by_name(model, "input").shape[1:]
print("CNTK model image input width = {} pixels and height = {} pixels.".format(width,height))
for index, (imgPath, imgUUI) in enumerate(zip(imgPaths,imgUUIs)):
print("Processing image {} of {}: {}".format(index, len(imgPaths), imgPath))
img = imread(imgPath)
# Prepare DNN inputs
# NOTE: CNTK rc1 (or higher) has a bug where during padding only the first dimension is assigned the pad value of 114.
# This bug can be simulated here by padColor = [114,0,0] instead of [114, 114, 114]
imgPadded = imresizeAndPad(img, width, height, padColor = [114,0,0])
arguments = {
model.arguments[0]: [np.ascontiguousarray(np.array(imgPadded, dtype=np.float32).transpose(2, 0, 1))], # convert to CNTKs HWC format
}
# Run DNN model
dnnOut = model.eval(arguments)
feat = np.concatenate(dnnOut, axis=0).squeeze()
feat = np.array(feat, np.float32)
feats[imgUUI] = feat
print("Saving features to file {}.".format(outFeaturesPath))
saveToPickle(outFeaturesPath, feats)
print("DONE.")