old_contents: from gensim.models import word2vec
  from keras.applications.vgg16 import VGG16, preprocess_input
  from keras.preprocessing import image
  from pycocotools.coco import COCO
  from scipy.spatial import distance
  from sklearn.externals import joblib
  import logging
  import os
  import numpy as np
  import time
  
  logging.basicConfig(filename='cca.log', format='%(asctime)s %(message)s', level=logging.INFO)
  
  model = word2vec.Word2Vec.load_word2vec_format('text.model.bin', binary=True)
  net = VGG16(weights='imagenet', include_top=False)
  
  assert os.path.isfile('pca_img.pkl')
  pca = joblib.load('pca_img.pkl')
  
  assert os.path.isfile('W_img.npy')
  W_img = np.load('W_img.npy')
  
  assert os.path.isfile('W_tag.npy')
  W_tag = np.load('W_tag.npy')
  
  annFile = 'annotations/captions_val2014.json'
  coco_val = COCO(annFile)
  ids = coco_val.getAnnIds()
  annotations = coco_val.loadAnns(ids)
  
  img_info = {}
  logging.info('Testing: get all different image ids')
  for ann in annotations:
      image_id = ann['image_id']
      img_info[image_id] = coco_val.imgs[image_id]
  
  img_features = np.zeros((10, W_img.shape[0]))
  img_ids = []
  pos = 0
  logging.info('Testing: loading images and calculate image embeddings')
  for image_id, info in img_info.iteritems():
      file_name = info['file_name']
      img = image.load_img('val2014/' + file_name, target_size=(224, 224))
      img = image.img_to_array(img)
      img = np.expand_dims(img, axis=0)
      img = preprocess_input(img)
      features = net.predict(img)
      features = features.reshape(img.shape[0], - 1)
      img_features[pos,:] = pca.transform(features)
      img_ids.append(image_id)
      pos += 1
      if pos == 10:
          break
  
  tag_keys = []
  tag_list = []
  logging.info('Testing: get embedding of all words in the vocabulary')
  for k in model.wv.vocab.keys():
      tag_keys.append(k)
      tag_list.append(model[k])
  
  logging.info('Testing: prediction')
  N_RESULTS = 10
  pos = 0
  for img in img_features:
     v_img = np.dot(img, W_img)
     scores = np.zeros(len(tag_list))
     for i in range(len(tag_list)):
         tag = tag_list[i]
         v_tag = np.dot(tag, W_tag)
         scores[i] = distance.euclidean(v_img, v_tag)
     index = np.argsort(scores)
     print scores[index][:N_RESULTS]
     print coco_val.imgs[ img_ids[pos] ]['flickr_url']
     results = []
     for i in range(N_RESULTS):
         results.append(tag_keys[ index[i] ])
     print results
     pos += 1
  
new_contents: from gensim.models import word2vec
  from keras.applications.vgg16 import VGG16, preprocess_input
  from keras.preprocessing import image
  from pycocotools.coco import COCO
  from scipy.spatial import distance
  from sklearn.externals import joblib
  import logging
  import os
  import numpy as np
  import time
  
  logging.basicConfig(filename='cca.log', format='%(asctime)s %(message)s', level=logging.INFO)
  
  annFile = 'annotations/captions_val2014.json'
  coco_val = COCO(annFile)
  ids = coco_val.getAnnIds()
  annotations = coco_val.loadAnns(ids)
  
  model = word2vec.Word2Vec.load_word2vec_format('text.model.bin', binary=True)
  net = VGG16(weights='imagenet', include_top=False)
  
  assert os.path.isfile('pca_img.pkl')
  pca = joblib.load('pca_img.pkl')
  
  assert os.path.isfile('W_img.npy')
  W_img = np.load('W_img.npy')
  
  assert os.path.isfile('W_tag.npy')
  W_tag = np.load('W_tag.npy')
  
  tag_keys = []
  tag_list = []
  logging.info('Testing: get embedding of all words in the vocabulary')
  for k in model.wv.vocab.keys():
      tag_keys.append(k)
      tag_list.append(model[k])
  
  img_info = {}
  logging.info('Testing: get all different image ids')
  for ann in annotations:
      image_id = ann['image_id']
      img_info[image_id] = coco_val.imgs[image_id]
  
  N_TEST = len(img_info)
  logging.info('Testing: number of images = %d', N_TEST)
  
  N_RESULTS = 10
  f = open('test_tags.txt', 'w')
  img_ids = []
  pos = 0
  logging.info('Testing: prediction')
  for image_id, info in img_info.iteritems():
      file_name = info['file_name']
      img = image.load_img('val2014/' + file_name, target_size=(224, 224))
  
      img = image.img_to_array(img)
      img = np.expand_dims(img, axis=0)
      img = preprocess_input(img)
      img_features = net.predict(img)
      img_features = img_features.reshape(img.shape[0], - 1)
      img_features = pca.transform(img_features)
      img_ids.append(image_id)
  
      v_img = np.dot(img_features, W_img)
      scores = np.zeros(len(tag_list))
      for i in range(len(tag_list)):
          tag_features = tag_list[i]
          v_tag = np.dot(tag_features, W_tag)
          scores[i] = distance.euclidean(v_img, v_tag)
  
      index = np.argsort(scores)
      f.write(coco_val.imgs[ img_ids[pos] ]['flickr_url'] + '\n')
      for i in range(N_RESULTS):
          f.write(tag_keys[ index[i] ] + '\n')
  
      pos += 1
      if pos == 10:
          break
  
subject: save test results in a file