def main(): apollocaffe.set_device(0) #apollocaffe.set_cpp_loglevel(0) apollocaffe.set_random_seed(0) np.random.seed(0) job = sys.argv[1] corpus_name = sys.argv[2] config = util.Struct(**yaml.load(CONFIG)) if corpus_name == "abstract": train_scenes, dev_scenes, test_scenes = corpus.load_abstract() else: assert corpus_name == "birds" train_scenes, dev_scenes, test_scenes = corpus.load_birds() apollo_net = ApolloNet() print "loaded data" print "%d training examples" % len(train_scenes) listener0_model = Listener0Model(apollo_net, config.model) speaker0_model = Speaker0Model(apollo_net, config.model) sampling_speaker1_model = SamplingSpeaker1Model(apollo_net, config.model) compiled_speaker1_model = CompiledSpeaker1Model(apollo_net, config.model) if job == "train.base": train(train_scenes, dev_scenes, listener0_model, apollo_net, config.opt) train(train_scenes, dev_scenes, speaker0_model, apollo_net, config.opt) apollo_net.save("models/%s.base.caffemodel" % corpus_name) exit() if job == "train.compiled": apollo_net.load("models/%s.base.caffemodel" % corpus_name) print "loaded model" train(train_scenes, dev_scenes, compiled_speaker1_model, apollo_net, config.opt) apollo_net.save("models/%s.compiled.caffemodel" % corpus_name) exit() if job in ("sample.base", "sample.compiled"): if job == "sample.base": apollo_net.load("models/%s.base.caffemodel" % corpus_name) else: apollo_net.load("models/%s.compiled.caffemodel" % corpus_name) print "loaded model" if job == "sample.base": models = { "sampling_speaker1": sampling_speaker1_model, } elif job == "sample.compiled": models = { "compiled_speaker1": compiled_speaker1_model, } name = job.split(".")[1] run_experiment("one_different", corpus_name, name, models, dev_scenes) run_experiment("by_similarity", corpus_name, name, models, dev_scenes) run_experiment("all_same", corpus_name, name, models, dev_scenes)
def make_abstract(): train_data, val_data, test_data = corpus.load_abstract() by_prop = defaultdict(list) for datum in val_data: props = {(p.type_index, p.object_index) for p in datum.props} props = {(i,j) if i not in (2,3) else (i,0) for i,j in props} props = tuple(sorted(props)) by_prop[props].append(datum) with open("experiments/all_same/abstract.ids.txt", "w") as id_f: counter = 0 for key in by_prop: if counter >= N_PAIRS: break images = set(d.image_id for d in by_prop[key]) if len(images) <= 3: continue images = list(images) np.random.shuffle(images) images = images[:2] print >>id_f, ",".join(images) + ",0" counter += 1 with open("experiments/one_different/abstract.ids.txt", "w") as id_f: counter = 0 for key1 in by_prop: if counter >= N_PAIRS: break keys = list(by_prop.keys()) np.random.shuffle(keys) for key2 in by_prop: if len(key1) != len(key2): continue if len(set(key1) ^ set(key2)) != 2: continue if key1 > key2: continue images = [by_prop[key1][0].image_id, by_prop[key2][0].image_id] print >>id_f, ",".join(images) + ",2" counter += 1 break with open("experiments/by_similarity/abstract.ids.txt", "w") as id_f: keys = list(by_prop.keys()) counter = 0 for key1 in by_prop: if counter >= N_PAIRS: break attempts = 0 while attempts < 100: attempts += 1 key2 = keys[np.random.randint(len(keys))] similarity = len(set(key1) ^ set(key2)) if similarity > MAX_DIFFERENCES: continue if np.random.random() > 0.45 ** similarity: continue img1 = by_prop[key1][0].image_id img2 = by_prop[key2][0].image_id if img1 == img2: continue print >>id_f, "%s,%s,%s" % (img1, img2, similarity) counter += 1 #print counter break
def make_abstract(): train_data, val_data, test_data = corpus.load_abstract() by_prop = defaultdict(list) for datum in val_data: props = {(p.type_index, p.object_index) for p in datum.props} props = {(i, j) if i not in (2, 3) else (i, 0) for i, j in props} props = tuple(sorted(props)) by_prop[props].append(datum) with open("experiments/all_same/abstract.ids.txt", "w") as id_f: counter = 0 for key in by_prop: if counter >= N_PAIRS: break images = set(d.image_id for d in by_prop[key]) if len(images) <= 3: continue images = list(images) np.random.shuffle(images) images = images[:2] print >> id_f, ",".join(images) + ",0" counter += 1 with open("experiments/one_different/abstract.ids.txt", "w") as id_f: counter = 0 for key1 in by_prop: if counter >= N_PAIRS: break keys = list(by_prop.keys()) np.random.shuffle(keys) for key2 in by_prop: if len(key1) != len(key2): continue if len(set(key1) ^ set(key2)) != 2: continue if key1 > key2: continue images = [by_prop[key1][0].image_id, by_prop[key2][0].image_id] print >> id_f, ",".join(images) + ",2" counter += 1 break with open("experiments/by_similarity/abstract.ids.txt", "w") as id_f: keys = list(by_prop.keys()) counter = 0 for key1 in by_prop: if counter >= N_PAIRS: break attempts = 0 while attempts < 100: attempts += 1 key2 = keys[np.random.randint(len(keys))] similarity = len(set(key1) ^ set(key2)) if similarity > MAX_DIFFERENCES: continue if np.random.random() > 0.45**similarity: continue img1 = by_prop[key1][0].image_id img2 = by_prop[key2][0].image_id if img1 == img2: continue print >> id_f, "%s,%s,%s" % (img1, img2, similarity) counter += 1 #print counter break
RHO = 0.95 EPS = 0.000001 LR = args.LR CLIP = 10 N_TEST_IMAGES = 100 N_TEST = N_TEST_IMAGES * 10 N_EXPERIMENT_PAIRS = 100 # Not sure about num_scenes?? NUM_SCENES = 10 util.setup_logging(args) train_scenes, dev_scenes, test_scenes = corpus.load_abstract() VOCAB_SIZE = len(corpus.WORD_INDEX) VOCAB_SIZE = len(corpus.WORD_INDEX) output_size = 1 listener0_model = model.Listener0Model(VOCAB_SIZE, NUM_SCENES, args.hidden_sz, output_size, args.dropout) # need to pass in some parameters speaker0_model = model.Speaker0Model(VOCAB_SIZE, 100, args.dropout, args.dec) if torch.cuda.is_available(): listener0_model.cuda() speaker0_model.cuda() optimizer_l0 = optim.Adam(listener0_model.parameters(), lr=LR)