summaryrefslogtreecommitdiff
path: root/cli/app/commands/biggan/extract_dense_vectors.py
blob: ddea99d1a13ce756c52312d6384c58a98ff7d642 (plain)
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
75
76
77
import click
import os

from app.utils.cortex_utils import cortex_folder, download_cortex_files, find_unprocessed_files
from app.search.search_class import find_nearest_vector_for_images
from app.search.search_dense import find_dense_embedding_for_images
from app.search.json import params_dense_dict
from app.search.params import timestamp

@click.command('')
@click.option('-f', '--folder_id', 'opt_folder_id', type=int,
  help='Folder ID to process')
@click.option('-ls', '--latent_steps', 'opt_latent_steps', default=2000, type=int,
  help='Number of optimization iterations')
@click.option('-ds', '--dense_steps', 'opt_dense_steps', default=2000, type=int,
  help='Number of optimization iterations')
@click.option('-v', '--video', 'opt_video', is_flag=True,
  help='Export a video for each dataset')
@click.option('-rp', '--reprocess', 'opt_reprocess', is_flag=True,
  help='Reprocess images')
@click.option('-sc', '--stochastic_clipping', 'opt_stochastic_clipping', is_flag=True,
  help='Compute feature loss')
@click.option('-lc', '--label_clipping', 'opt_label_clipping', is_flag=True,
  help='Normalize labels every N steps')
@click.option('-feat', '--use_feature_detector', 'opt_use_feature_detector', is_flag=True,
  help='Compute feature loss')
@click.option('-ll', '--feature_layers', 'opt_feature_layers', default="1a,2a,3a,4a,7a",
  help='Feature layers used for loss')
@click.option('-snap', '--snapshot_interval', 'opt_snapshot_interval', default=20,
  help='Interval to store sample images')
@click.option('-clip', '--clip_interval', 'opt_clip_interval', default=300,
  help='Interval to clip vectors')
@click.pass_context
def cli(ctx, opt_folder_id, opt_latent_steps, opt_dense_steps, opt_video, opt_reprocess,
  opt_stochastic_clipping, opt_label_clipping, opt_use_feature_detector, opt_feature_layers, opt_snapshot_interval, opt_clip_interval):
  """
  The full process:
  - Fetch new images from the cortex
  - Extract labels and base latents
  - Extract dense embeddings
  - Upload extract images to the cortex
  """
  folder = cortex_folder(opt_folder_id)
  files = download_cortex_files(opt_folder_id)
  unprocessed_files = find_unprocessed_files(files, reprocess=opt_reprocess)
  if len(unprocessed_files) == 0:
    print("All files processed, nothing to do")
    return
  
  print("Processing folder {} ({}), {} new files".format(folder['name'], folder['id'], len(unprocessed_files)))

  tag = "folder_{}_{}".format(folder['id'], timestamp())
  paths = [file['path'] for file in unprocessed_files]
  opt_feature_layers = opt_feature_layers.split(',')
  
  find_nearest_vector_for_images(
    paths=paths,
    opt_dims=512,
    opt_steps=opt_latent_steps,
    opt_video=False,
    opt_tag=tag,
    opt_limit=-1,
    opt_stochastic_clipping=opt_stochastic_clipping,
    opt_label_clipping=opt_label_clipping,
    opt_use_feature_detector=True,
    opt_feature_layers=opt_feature_layers,
    opt_snapshot_interval=opt_snapshot_interval,
    opt_clip_interval=opt_clip_interval,
    opt_folder_id=folder['id']
  )

  params = params_dense_dict(tag, folder_id=folder['id'])
  find_dense_embedding_for_images(params,
    opt_tag=tag,
    opt_feature_layers=opt_feature_layers,
    opt_save_progress=True)