(params)
| 27 | """ |
| 28 | |
| 29 | def main(params): |
| 30 | |
| 31 | # load the checkpoint |
| 32 | checkpoint_path = params['checkpoint_path'] |
| 33 | print 'loading checkpoint %s' % (checkpoint_path, ) |
| 34 | checkpoint = pickle.load(open(checkpoint_path, 'rb')) |
| 35 | checkpoint_params = checkpoint['params'] |
| 36 | dataset = checkpoint_params['dataset'] |
| 37 | model = checkpoint['model'] |
| 38 | misc = {} |
| 39 | misc['wordtoix'] = checkpoint['wordtoix'] |
| 40 | ixtoword = checkpoint['ixtoword'] |
| 41 | |
| 42 | # output blob which we will dump to JSON for visualizing the results |
| 43 | blob = {} |
| 44 | blob['params'] = params |
| 45 | blob['checkpoint_params'] = checkpoint_params |
| 46 | blob['imgblobs'] = [] |
| 47 | |
| 48 | # load the tasks.txt file |
| 49 | root_path = params['root_path'] |
| 50 | img_names = open(os.path.join(root_path, 'tasks.txt'), 'r').read().splitlines() |
| 51 | |
| 52 | # load the features for all images |
| 53 | features_path = os.path.join(root_path, 'vgg_feats.mat') |
| 54 | features_struct = scipy.io.loadmat(features_path) |
| 55 | features = features_struct['feats'] # this is a 4096 x N numpy array of features |
| 56 | D,N = features.shape |
| 57 | |
| 58 | # iterate over all images and predict sentences |
| 59 | BatchGenerator = decodeGenerator(checkpoint_params) |
| 60 | for n in xrange(N): |
| 61 | print 'image %d/%d:' % (n, N) |
| 62 | |
| 63 | # encode the image |
| 64 | img = {} |
| 65 | img['feat'] = features[:, n] |
| 66 | img['local_file_path'] =img_names[n] |
| 67 | |
| 68 | # perform the work. heavy lifting happens inside |
| 69 | kwparams = { 'beam_size' : params['beam_size'] } |
| 70 | Ys = BatchGenerator.predict([{'image':img}], model, checkpoint_params, **kwparams) |
| 71 | |
| 72 | # build up the output |
| 73 | img_blob = {} |
| 74 | img_blob['img_path'] = img['local_file_path'] |
| 75 | |
| 76 | # encode the top prediction |
| 77 | top_predictions = Ys[0] # take predictions for the first (and only) image we passed in |
| 78 | top_prediction = top_predictions[0] # these are sorted with highest on top |
| 79 | candidate = ' '.join([ixtoword[ix] for ix in top_prediction[1] if ix > 0]) # ix 0 is the END token, skip that |
| 80 | print 'PRED: (%f) %s' % (top_prediction[0], candidate) |
| 81 | img_blob['candidate'] = {'text': candidate, 'logprob': top_prediction[0]} |
| 82 | blob['imgblobs'].append(img_blob) |
| 83 | |
| 84 | # dump result struct to file |
| 85 | save_file = os.path.join(root_path, 'result_struct.json') |
| 86 | print 'writing predictions to %s...' % (save_file, ) |
no test coverage detected