def benchmark_clevr_sketch(share_funcs, batch_norm, batch_size, arch, log_dir, lr, arch_opt, sample, conversions=std_conversions(), **kwargs): all_arch = archs.MLPNet sample_args = {'pbatch_norm': int(batch_norm)} functypes = genfuns.func_types(*std_types()) import pdb pdb.set_trace() neu_clevr = clevrarch.funcs(all_arch, arch_opt, sample, sample_args, **functypes) neuclevr = asl.modules.modules.ModuleDict(neu_clevr) refclevr = ref_clevr clevr_sketch = ClevrSketch(neuclevr, refclevr) util.cuda(clevr_sketch) data_itr = data_iter(batch_size, conversions) def loss_gen(): nonlocal data_itr try: progs, objsets, rels, answers = next(data_itr) except StopIteration: data_itr = data_iter(batch_size, conversions) progs, objsets, rels, answers = next(data_itr) outputs = clevr_sketch(progs, objsets, rels) anstensors = [convert(parse(ans), conversions) for ans in answers] acc, ncorrect = accuracy(outputs, anstensors) asl.log("accuracy", acc) asl.log("ncorrect", ncorrect) asl.log("outof", len(answers)) deltas = [ asl.dist(outputs[i], anstensors[i]) for i in range(len(outputs)) ] return sum(deltas) / len(deltas) optimizer = optim.Adam(clevr_sketch.parameters(), lr) asl.train( loss_gen, optimizer, cont=asl.converged(100), callbacks=[ asl.print_loss(10), print_accuracy(10), asl.nancancel, # every_n(plot_sketch, 500), # common.plot_empty, # common.plot_observes, asl.save_checkpoint(100, clevr_sketch) ], log_dir=log_dir)
def benchmark_clevr_sketch(share_funcs, batch_norm, batch_size, arch, log_dir, lr, arch_opt, sample, conversions=std_conversions(), **kwargs): all_arch = archs.MLPNet sample_args = {'pbatch_norm': int(batch_norm)} functypes = genfuns.func_types(*std_types()) import pdb; pdb.set_trace() neu_clevr = clevrarch.funcs(all_arch, arch_opt, sample, sample_args, **functypes) neuclevr = asl.modules.modules.ModuleDict(neu_clevr) refclevr = ref_clevr clevr_sketch = ClevrSketch(neuclevr, refclevr) util.cuda(clevr_sketch) data_itr = data_iter(batch_size, conversions) def loss_gen(): nonlocal data_itr try: progs, objsets, rels, answers = next(data_itr) except StopIteration: data_itr = data_iter(batch_size, conversions) progs, objsets, rels, answers = next(data_itr) outputs = clevr_sketch(progs, objsets, rels) anstensors = [convert(parse(ans), conversions) for ans in answers] acc, ncorrect = accuracy(outputs, anstensors) asl.log("accuracy", acc) asl.log("ncorrect", ncorrect) asl.log("outof", len(answers)) deltas = [asl.dist(outputs[i], anstensors[i]) for i in range(len(outputs))] return sum(deltas) / len(deltas) optimizer = optim.Adam(clevr_sketch.parameters(), lr) asl.train(loss_gen, optimizer, cont=asl.converged(100), callbacks=[asl.print_loss(10), print_accuracy(10), asl.nancancel, # every_n(plot_sketch, 500), # common.plot_empty, # common.plot_observes, asl.save_checkpoint(100, clevr_sketch)], log_dir=log_dir)
def from_clevr_object(clevr_object): tensor = Variable(util.cuda(util.onehotmany([clevr_object.color.value, clevr_object.size.value, clevr_object.material.value, clevr_object.shape.value], TensorClevrObject.max_prop_len))) return TensorClevrObject(tensor)
def from_clevr_object(clevr_object): tensor = Variable( util.cuda( util.onehotmany([ clevr_object.color.value, clevr_object.size.value, clevr_object.material.value, clevr_object.shape.value ], TensorClevrObject.max_prop_len))) return TensorClevrObject(tensor)
def from_relations(relations): nrels = 4 maxnobjs = 10 rel_ten = torch.zeros(nrels, maxnobjs, maxnobjs) for (i, rel) in enumerate(['behind', 'front', 'left', 'right']): for (j, obj1rels) in enumerate(relations.listform[rel]): for obj2 in obj1rels: rel_ten[i, j, obj2] = 1.0 return TensorRelations(Variable(util.cuda(rel_ten)))
def std_conversions(): "Options sampler" conversions = {} conversions[clevr.ClevrObjectSet] = clevr.TensorClevrObjectSet.from_clevr_object_set conversions[clevr.ClevrObject] = clevr.TensorClevrObject.from_clevr_object conversions[clevr.Relations] = clevr.TensorRelations.from_relations conversions[int] = lambda x: clevr.IntegerOneHot1D(Variable(util.cuda(util.onehot(x, 11, 1)))) conversions[clevr.BooleanEnum] = asl.onehot1d conversions[clevr.ColorEnum] = asl.onehot1d conversions[clevr.ColorEnum] = asl.onehot1d conversions[clevr.MaterialEnum] = asl.onehot1d conversions[clevr.ShapeEnum] = asl.onehot1d conversions[clevr.SizeEnum] = asl.onehot1d return conversions
def from_clevr_object_set(clevr_object_set, max_n_objects=10): obj_tensors = [ TensorClevrObject.from_clevr_object(obj).value for obj in clevr_object_set.objects ] ndummies = max_n_objects - len(obj_tensors) assert ndummies >= 0 dummies = [ Variable( util.cuda(torch.zeros(1, 4, TensorClevrObjectSet.max_prop_len))) for i in range(ndummies) ] return TensorClevrObjectSet(torch.cat(obj_tensors + dummies, 0))
def std_conversions(): "Options sampler" conversions = {} conversions[ clevr. ClevrObjectSet] = clevr.TensorClevrObjectSet.from_clevr_object_set conversions[clevr.ClevrObject] = clevr.TensorClevrObject.from_clevr_object conversions[clevr.Relations] = clevr.TensorRelations.from_relations conversions[int] = lambda x: clevr.IntegerOneHot1D( Variable(util.cuda(util.onehot(x, 11, 1)))) conversions[clevr.BooleanEnum] = asl.onehot1d conversions[clevr.ColorEnum] = asl.onehot1d conversions[clevr.ColorEnum] = asl.onehot1d conversions[clevr.MaterialEnum] = asl.onehot1d conversions[clevr.ShapeEnum] = asl.onehot1d conversions[clevr.SizeEnum] = asl.onehot1d return conversions
def from_clevr_object_set(clevr_object_set, max_n_objects=10): obj_tensors = [TensorClevrObject.from_clevr_object(obj).value for obj in clevr_object_set.objects] ndummies = max_n_objects - len(obj_tensors) assert ndummies >= 0 dummies = [Variable(util.cuda(torch.zeros(1, 4, TensorClevrObjectSet.max_prop_len))) for i in range(ndummies)] return TensorClevrObjectSet(torch.cat(obj_tensors + dummies, 0))