@@ -0,0 +1,2772 @@
+%22%22%22Given a trained multimodal model, generate embeddings.%22%22%22%0A%0Afrom __future__ import division%0Afrom __future__ import print_function%0Afrom __future__ import absolute_import%0A%0Aimport os%0Aimport sys%0Aimport numpy as np%0Afrom glob import glob%0A%0Aimport torch%0Afrom torch.autograd import Variable%0Afrom convmodel import load_checkpoint%0A%0A%0Adef mimic_dir(new_dir, old_dir):%0A    if not os.path.exists(new_dir):%0A        os.makedirs(new_dir)%0A    # check and create all sub-directories%0A    all_folders = %5Bx%5B0%5D for x in os.walk(old_dir)%5D%0A    for folder in all_folders:%0A        new_folder = folder.replace(old_dir, new_dir)%0A        if not os.path.exists(new_folder):%0A            os.makedirs(new_folder)%0A%0A%0A%0Aif __name__ == '__main__':%0A    import argparse%0A    parser = argparse.ArgumentParser()%0A    parser.add_argument('photo_emb_dir', type=str)%0A    parser.add_argument('sketch_emb_dir', type=str)%0A    parser.add_argument('photo_mmemb_dir', type=str)%0A    parser.add_argument('sketch_mmemb_dir', type=str)%0A    parser.add_argument('model_path', type=str, help='where to find trained model.')%0A    parser.add_argument('--cuda', action='store_true', default=False)%0A    args = parser.parse_args()%0A    args.cuda = args.cuda and torch.cuda.is_available()%0A    dtype = torch.cuda.FloatTensor if args.cuda else torch.FloatTensor%0A%0A    # load trained multimodal model%0A    model = load_checkpoint(args.model_path, use_cuda=args.cuda)%0A    model.eval()%0A    if args.cuda:%0A        model.cuda()%0A%0A    # create new directories if not existing.%0A    mimic_dir(args.photo_mmemb_dir, args.photo_emb_dir)%0A    mimic_dir(args.sketch_mmemb_dir, args.sketch_emb_dir)%0A%0A    photo_paths = glob(os.path.join(args.photo_emb_dir, '*.npy'))%0A    sketch_paths = glob(os.path.join(args.sketch_emb_dir, '*.npy'))%0A    %0A    n_photos = len(photo_paths)%0A    n_sketches = len(sketch_paths)%0A    print(%22number of photos: %7B%7D%22.format(n_photos))%0A    print(%22number of sketches: %7B%7D%22.format(n_sketches))    %0A%0A    for i in xrange(n_photos):%0A        photo = np.load(photo_paths%5Bi%5D)%0A        photo = Variable(torch.from_numpy(photo).type(dtype), volatile=True)%0A        photo = model.photo_adaptor(photo.unsqueeze(0))%0A        photo = photo.cpu().data.numpy()%5B0%5D%0A        np.save(photo_paths%5Bi%5D.replace(args.photo_emb_dir, args.photo_mmemb_dir), photo)%0A        print('Compute Photo Embedding %5B%7B%7D/%7B%7D%5D.'.format(i + 1, n_photos))%0A%0A%0A    for i in xrange(n_sketches):%0A        sketch = np.load(sketch_paths%5Bi%5D)%0A        sketch = Variable(torch.from_numpy(sketch).type(dtype), volatile=True)%0A        sketch = model.sketch_adaptor(sketch.unsqueeze(0))%0A        sketch = sketch.cpu().data.numpy()%5B0%5D%0A        np.save(sketch_paths%5Bi%5D.replace(args.sketch_emb_dir, args.sketch_mmemb_dir), sketch)%0A        print('Compute Sketch Embedding %5B%7B%7D/%7B%7D%5D.'.format(i + 1, n_sketches))%0A%0A

add embed script to stric_test dir