(path, input)
| 58 | |
| 59 | |
| 60 | def run_test(path, input): |
| 61 | param_dict = dict(np.load(path)) |
| 62 | param_dict = {k.replace('/W', '/kernel').replace('/b', '/bias'): v for k, v in six.iteritems(param_dict)} |
| 63 | |
| 64 | predict_func = OfflinePredictor(PredictConfig( |
| 65 | input_signature=[tf.TensorSpec((None, 224, 224, 3), tf.float32, 'input')], |
| 66 | tower_func=tower_func, |
| 67 | session_init=SmartInit(param_dict), |
| 68 | input_names=['input'], |
| 69 | output_names=['prob'] # prob:0 is the probability distribution |
| 70 | )) |
| 71 | |
| 72 | im = cv2.imread(input) |
| 73 | assert im is not None, input |
| 74 | im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) |
| 75 | im = cv2.resize(im, (224, 224)).reshape((1, 224, 224, 3)).astype('float32') |
| 76 | |
| 77 | # VGG19 requires channelwise mean substraction |
| 78 | VGG_MEAN = [103.939, 116.779, 123.68] |
| 79 | im -= VGG_MEAN[::-1] |
| 80 | outputs = predict_func(im)[0] |
| 81 | prob = outputs[0] |
| 82 | ret = prob.argsort()[-10:][::-1] |
| 83 | print("Top10 predictions:", ret) |
| 84 | |
| 85 | meta = ILSVRCMeta().get_synset_words_1000() |
| 86 | print("Top10 class names:", [meta[k] for k in ret]) |
| 87 | |
| 88 | |
| 89 | if __name__ == '__main__': |
no test coverage detected