def sample_values(self, mode, correct): super(AttributesRelationCaptioner, self).sample_values(mode=mode, correct=correct) self.attribute_mode = util.sample(self.attribute_distribution) self.existing_attribute = correct or (random() < self.existing_attribute_ratio) while not correct and self.attribute_mode > 0 and self.existing_attribute: self.attribute_mode = util.sample(self.attribute_distribution) self.existing_attribute = correct or (random() < self.existing_attribute_ratio) self.incorrect_mode = 0 if correct else 1 + util.sample(self.incorrect_distribution) while (self.incorrect_mode == 1 and len(self.shapes) <= 1) or (self.incorrect_mode == 2 and len(self.colors) <= 1) or (self.incorrect_mode == 3 and len(self.textures) <= 1): self.incorrect_mode = 0 if correct else 1 + util.sample(self.incorrect_distribution)
def sample_values(self, mode): super(GeneratorMixer, self).sample_values(mode=mode) if mode is None: self.generator = util.sample(self.distribution, self.generators) elif mode == 'train': self.generator = util.sample(self.train_distribution, self.generators) elif mode == 'validation': self.generator = util.sample(self.validation_distribution, self.generators) elif mode == 'test': self.generator = util.sample(self.test_distribution, self.generators) self.generator.sample_values(mode=mode)
def sample_values(self, mode, correct, predication): if not super(CaptionerMixer, self).sample_values(mode=mode, correct=correct, predication=predication): return False if mode is None: self.captioner = util.sample(self.distribution, self.internal_captioners) elif mode == 'train': self.captioner = util.sample(self.train_distribution, self.internal_captioners) elif mode == 'validation': self.captioner = util.sample(self.validation_distribution, self.internal_captioners) elif mode == 'test': self.captioner = util.sample(self.test_distribution, self.internal_captioners) return self.captioner.sample_values(mode=mode, correct=correct, predication=predication)
def sample_values(self, mode, predication): assert predication.empty() if not super(ExistentialCaptioner, self).sample_values( mode=mode, predication=predication): return False self.incorrect_mode = util.sample(self.incorrect_distribution) predication = predication.copy() if self.incorrect_mode == 0: # 0: incorrect restrictor # incorrect after correct if not self.body_captioner.sample_values(mode=mode, predication=predication): return False if not self.restrictor_captioner.sample_values( mode=mode, predication=predication): return False else: if not self.restrictor_captioner.sample_values( mode=mode, predication=predication): return False if not self.body_captioner.sample_values(mode=mode, predication=predication): return False return True
def sample_values(self, mode, predication): if not super(SelectorCaptioner, self).sample_values( mode=mode, predication=predication): return False for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): self.predtype, self.value = choice(self.selectors) if self.predtype in ('size-two', 'size-max') and predication.redundant( predicate='shape'): continue elif self.predtype in ('shade-two', 'shade-max') and predication.redundant( predicate='color'): continue break self.incorrect_mode = util.sample(self.incorrect_distribution) self.incorrect_predtype = self.predtype self.incorrect_value = self.value if self.incorrect_mode == 0: # 0: incorrect selectors for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): self.incorrect_predtype, self.incorrect_value = choice( self.selectors) if self.incorrect_predtype == self.predtype and self.incorrect_value == self.value: continue elif self.incorrect_predtype in ( 'size-two', 'size-max') and predication.redundant( predicate='shape'): continue elif self.incorrect_predtype in ( 'shade-two', 'shade-max') and predication.redundant( predicate='color'): continue break else: return False if not self.scope_captioner.sample_values(mode=mode, predication=predication): return False if self.predtype in Selector.comparison_selectors or self.incorrect_predtype in Selector.comparison_selectors: comp_predication = predication.copy(reset=True) if not self.comparison_captioner.sample_values( mode=mode, predication=comp_predication): return False if self.predtype in ('size-two', 'size-max') or self.incorrect_predtype in ( 'size-two', 'size-max'): predication.apply(predicate='shape') elif self.predtype in ('shade-two', 'shade-max') or self.incorrect_predtype in ( 'shade-two', 'shade-max'): predication.apply(predicate='color') return True
def initialize(self, mode): super(GeneratorMixer, self).initialize(mode=mode) if mode is None: self.generator = util.sample(self.distribution, self.generators) elif mode == 'train': self.generator = util.sample(self.train_distribution, self.generators) elif mode == 'validation': self.generator = util.sample(self.validation_distribution, self.generators) elif mode == 'test': self.generator = util.sample(self.test_distribution, self.generators) return self.generator.initialize(mode=mode)
def sample_values(self, mode, correct, predication): assert predication.empty() if not super(QuantifierCaptioner, self).sample_values(mode=mode, correct=correct, predication=predication): return False self.incorrect_mode = 0 if correct else 1 + util.sample(self.incorrect_distribution) self.qtype, self.qrange, self.quantity = choice(self.quantifiers) predication = predication.copy() if not self.restrictor_captioner.sample_values(mode=mode, correct=(self.incorrect_mode != 1), predication=predication): # 1: incorrect restrictor return False if (self.qtype, self.qrange, self.quantity) in QuantifierCaptioner.zero_quantifiers: # always incorrect body for zero quantification, since we need an incorrect body for a correct caption if not self.body_captioner.sample_values(mode=mode, correct=False, predication=predication): # 2: incorrect body return False else: if not self.body_captioner.sample_values(mode=mode, correct=(self.incorrect_mode != 2), predication=predication): # 2: incorrect body return False if self.incorrect_mode == 3: # 3: incorrect quantifier self.incorrect_quantifiers = [(qtype, qrange, quantity) for qtype, qrange, quantity in self.quantifiers if qtype != self.qtype or qrange != self.qrange or quantity != self.quantity] return True
def sample_values(self, mode, predication): assert predication.empty() if not super(ImplicationCaptioner, self).sample_values( mode=mode, predication=predication): return False predication1 = predication.copy() predication2 = predication.copy() if not self.captioner1.sample_values(mode=mode, predication=predication1): return False if not self.captioner2.sample_values(mode=mode, predication=predication2): return False for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): self.correct_mode = util.sample(self.correct_distribution) if self.correct_mode == 1 and not self.captioner2.incorrect_possible( ): continue if self.correct_mode == 2 and ( not self.captioner1.incorrect_possible() or not self.captioner2.incorrect_possible()): continue break else: return False return True
def sample_values(self, mode, correct, predication): assert predication.empty() if not super(DisjunctionCaptioner, self).sample_values( mode=mode, correct=correct, predication=predication): return False self.captioner1 = choice(self.internal_captioners) self.captioner2 = choice(self.internal_captioners) self.correct_mode = 0 if not correct else 1 + util.sample( self.correct_distribution) correct1 = (self.correct_mode == 1) or ( self.correct_mode == 3) # 1: first correct, 3: both correct correct2 = (self.correct_mode == 2) or ( self.correct_mode == 3) # 2: second correct, 3: both correct predication1 = predication.copy() predication2 = predication.copy() if not self.captioner1.sample_values( mode=mode, correct=correct1, predication=predication1): return False if not self.captioner2.sample_values( mode=mode, correct=correct2, predication=predication2): return False return True
def sample_values(self, mode, correct, predication): if not super(MaxAttributeCaptioner, self).sample_values( mode=mode, correct=correct, predication=predication): return False self.incorrect_mode = 0 if correct else 1 + util.sample( self.incorrect_distribution) self.predtype, self.value = choice(self.attributes) if not self.scope_captioner.sample_values( mode=mode, correct=(self.incorrect_mode != 1), predication=predication): # 1: incorrect scope return False # instead of self.logical_redundancy, since it uniquely selects one entity if not self.logical_tautology and predication.redundant( predicate=self.predtype): return False if self.incorrect_mode == 2: # 2: incorrect max attribute self.incorrect_attributes = [ (predtype, value) for predtype, value in self.attributes if predtype != self.predtype or value != self.value ] predication.apply(predicate=self.predtype) return True
def sample_values(self, mode, correct, predication): assert predication.empty() if not super(ExistentialCaptioner, self).sample_values( mode=mode, correct=correct, predication=predication): return False self.incorrect_mode = 0 if correct else 1 + util.sample( self.incorrect_distribution) predication = predication.copy() if self.incorrect_mode == 1: # incorrect after correct if not self.body_captioner.sample_values( mode=mode, correct=True, predication=predication): # 2: incorrect body return False if not self.restrictor_captioner.sample_values( mode=mode, correct=False, predication=predication): # 1: incorrect restrictor return False else: if not self.restrictor_captioner.sample_values( mode=mode, correct=True, predication=predication): # 1: incorrect restrictor return False if not self.body_captioner.sample_values( mode=mode, correct=(self.incorrect_mode != 2), predication=predication): # 2: incorrect body return False return True
def sample_values(self, mode, predication): assert predication.empty() if not super(ExistentialCaptioner, self).sample_values( mode=mode, predication=predication): return False # predication = predication.copy() if not self.body_captioner.sample_values(mode=mode, predication=predication): return False if not self.restrictor_captioner.sample_values( mode=mode, predication=predication): return False for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): self.incorrect_mode = util.sample(self.incorrect_distribution) if self.incorrect_mode == 0 and not self.restrictor_captioner.incorrect_possible( ): continue elif self.incorrect_mode == 1 and not self.body_captioner.incorrect_possible( ): continue break else: return False return True
def sample_values(self, mode, correct): super(CaptionerMixer, self).sample_values(mode=mode, correct=correct) if mode is None: self.captioner = util.sample(self.distribution, self.internal_captioners) elif mode == 'train': self.captioner = util.sample(self.train_distribution, self.internal_captioners) elif mode == 'validation': self.captioner = util.sample(self.validation_distribution, self.internal_captioners) elif mode == 'test': self.captioner = util.sample(self.test_distribution, self.internal_captioners) self.captioner.sample_values(mode=mode, correct=correct)
def sample_values(self, mode, predication): if not super(RelationCaptioner, self).sample_values( mode=mode, predication=predication): return False ref_predication = predication.copy(reset=True) if not self.reference_captioner.sample_values( mode=mode, predication=ref_predication): return False comp_predication = predication.copy(reset=True) if not self.comparison_captioner.sample_values( mode=mode, predication=comp_predication): return False self.predtype, self.value = choice(self.relations) for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): self.incorrect_mode = util.sample(self.incorrect_distribution) if self.incorrect_mode == 0 and not self.reference_captioner.incorrect_possible( ): continue elif self.incorrect_mode == 2 and self.predtype in Relation.no_inverse_relations: continue break else: return False self.incorrect_predtype = self.predtype self.incorrect_value = self.value if self.incorrect_mode == 1: # 1: incorrect relation for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): self.incorrect_predtype, self.incorrect_value = choice( self.relations) if self.incorrect_predtype == self.predtype and self.incorrect_value == self.value: continue break else: return False predication.apply(predicate=self.predtype) if (self.predtype == 'size-rel' or self.incorrect_predtype == 'size-rel') and ref_predication.redundant( predicate='shape'): predication.apply(predicate='shape') elif (self.predtype == 'shape-rel' or self.incorrect_predtype == 'shape-rel') and ref_predication.redundant(predicate='shape'): predication.block(predicate='shape') elif (self.predtype == 'shade-rel' or self.incorrect_predtype == 'shade-rel') and ref_predication.redundant(predicate='color'): predication.apply(predicate='color') elif (self.predtype == 'color-rel' or self.incorrect_predtype == 'color-rel') and ref_predication.redundant(predicate='color'): predication.block(predicate='color') return True
def sample_values(self, mode, predication): if not super(NegationRelationCaptioner, self).sample_values(mode=mode, predication=predication): return False self.incorrect_mode = util.sample(self.incorrect_distribution) self.negation = random() < 0.5 return self.relation_captioner.sample_values(mode=mode, predication=predication)
def sample_values(self, mode, correct): super(ExistentialCaptioner, self).sample_values(mode=mode, correct=correct) self.incorrect_mode = 0 if correct else 1 + util.sample( self.incorrect_distribution) self.restrictor_captioner.sample_values( mode=mode, correct=(self.incorrect_mode != 1)) # 1: incorrect restrictor self.body_captioner.sample_values( mode=mode, correct=(self.incorrect_mode != 2)) # 2: incorrect body
def generate(self, n, mode=None, noise_range=None, include_model=False, alternatives=False): if mode is None: distribution = self.distribution if mode == 'train': distribution = self.train_distribution elif mode == 'validation': distribution = self.validation_distribution elif mode == 'test': distribution = self.test_distribution if self.consistent_batches: dataset = util.sample(distribution, self.datasets) return dataset.generate(n=n, mode=mode, noise_range=noise_range, include_model=include_model, alternatives=alternatives) else: batch = self.zero_batch(n, include_model=include_model, alternatives=alternatives) for i in range(n): dataset = util.sample(distribution, self.datasets) generated = dataset.generate(n=1, mode=mode, noise_range=noise_range, include_model=include_model, alternatives=alternatives) for value_name, value_type in self.values.items(): value = generated[value_name][0] if value_type == 'text': batch[value_name][i][:len(value)] = value else: batch[value_name][i] = value return batch
def sample_values(self, mode, correct): super(AbsoluteQuantifierCaptioner, self).sample_values(mode=mode, correct=correct) self.qrange, self.quantity = choice(self.quantifiers) if self.quantity == 0 and (self.qrange == 'eq' or self.qrange == 'leq'): self.incorrect_mode = 0 if not correct else 1 + util.sample( self.incorrect_distribution) else: self.incorrect_mode = 0 if correct else 1 + util.sample( self.incorrect_distribution) self.restrictor_captioner.sample_values( mode=mode, correct=(self.incorrect_mode != 2)) # 2: incorrect restrictor self.body_captioner.sample_values( mode=mode, correct=(self.incorrect_mode != 3)) # 3: incorrect body if self.incorrect_mode == 1: # 1: incorrect quantifier self.incorrect_quantifiers = [ (qrange, quantity) for qrange, quantity in self.quantifiers if qrange != self.qrange or quantity != self.quantity ]
def sample_values(self, mode, correct, predication): assert predication.empty() if not super(ComparativeQuantifierCaptioner, self).sample_values( mode=mode, correct=correct, predication=predication): return False self.incorrect_mode = 0 if correct else 1 + util.sample( self.incorrect_distribution) self.qtype, self.qrange, self.quantity = choice(self.quantifiers) for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): rstr_predication = predication.copy() comp_predication = predication.copy() if not self.restrictor_captioner.sample_values( mode=mode, correct=(self.incorrect_mode != 1), predication=rstr_predication): # 1: incorrect restrictor return False if not self.comparison_captioner.sample_values( mode=mode, correct=(self.incorrect_mode != 2), predication=comp_predication): # 2: incorrect comparison return False union_predication = rstr_predication.union(other=comp_predication) if self.body_captioner.sample_values( mode=mode, correct=(self.incorrect_mode != 3), predication=union_predication): # 3: incorrect body break else: return False if self.incorrect_mode == 4: # 4: incorrect quantifier self.incorrect_quantifiers = [ (qtype, qrange, quantity) for qtype, qrange, quantity in self.quantifiers if qtype != self.qtype or qrange != self.qrange or quantity != self.quantity ] return True
def sample_values(self, mode, correct): super(ComparisonRelationCaptioner, self).sample_values(mode=mode, correct=correct) self.reltype, self.value = choice(self.relations) self.incorrect_mode = 0 if correct else 1 + util.sample( self.incorrect_distribution) self.reference_captioner.sample_values( mode=mode, correct=(self.incorrect_mode != 3)) # 3: incorrect reference if self.incorrect_mode == 1: # 1: incorrect comparison relation self.incorrect_relations = [ (reltype, value) for reltype, value in self.relations if reltype != self.reltype or value != self.value ]
def sample_values(self, mode, correct): super(ConjunctionCaptioner, self).sample_values(mode=mode, correct=correct) self.captioner1 = choice(self.internal_captioners) self.captioner2 = choice(self.internal_captioners) self.incorrect_mode = 0 if correct else 1 + util.sample(self.incorrect_distribution) if self.incorrect_mode == 0: # both correct self.captioner1.sample_values(mode=mode, correct=True) self.captioner2.sample_values(mode=mode, correct=True) elif self.incorrect_mode == 1: # second incorrect self.captioner1.sample_values(mode=mode, correct=True) self.captioner2.sample_values(mode=mode, correct=False) elif self.incorrect_mode == 2: # first incorrect self.captioner1.sample_values(mode=mode, correct=False) self.captioner2.sample_values(mode=mode, correct=True) elif self.incorrect_mode == 3: # both incorrect self.captioner1.sample_values(mode=mode, correct=False) self.captioner2.sample_values(mode=mode, correct=False)
def sample_values(self, mode, predication): if not super(RelationCaptioner, self).sample_values( mode=mode, predication=predication): return False self.predtype, self.value = choice(self.relations) for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): self.incorrect_mode = util.sample(self.incorrect_distribution) if self.incorrect_mode == 1 and self.predtype not in Relation.ternary_relations: # if incorrect comparison but relation not ternary continue break else: return False ref_predication = predication.copy(reset=True) if self.predtype == 'size-rel': ref_predication.apply(predicate='shape') predication.apply(predicate='shape') elif self.predtype == 'shade-rel': ref_predication.apply(predicate='color') predication.apply(predicate='color') if not self.reference_captioner.sample_values( mode=mode, predication=ref_predication): return False comp_predication = predication.copy(reset=True) if not self.comparison_captioner.sample_values( mode=mode, predication=comp_predication): return False if self.incorrect_mode == 2: # 2: incorrect spatial relation self.incorrect_relations = [ (predtype, value) for predtype, value in self.relations if predtype != self.predtype or value != self.value ] predication.apply(predicate=self.predtype) return True
def sample_values(self, mode, predication): assert predication.empty() if not super(DisjunctionCaptioner, self).sample_values( mode=mode, predication=predication): return False self.correct_mode = util.sample(self.correct_distribution) predication1 = predication.copy() predication2 = predication.copy() if not self.captioner1.sample_values(mode=mode, predication=predication1): return False if not self.captioner2.sample_values(mode=mode, predication=predication2): return False return True
def sample_values(self, mode, correct, predication): if not super(RelationCaptioner, self).sample_values( mode=mode, correct=correct, predication=predication): return False self.predtype, self.value = choice(self.relations) for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): self.incorrect_mode = 0 if correct else 1 + util.sample( self.incorrect_distribution) if (self.incorrect_mode != 2 or self.predtype in Relation.ternary_relations): # if incorrect comparison but relation not ternary break else: return False ref_predication = predication.copy(reset=True) if not self.reference_captioner.sample_values( mode=mode, correct=(self.incorrect_mode != 1), predication=ref_predication): # 1: incorrect reference return False comp_predication = predication.copy(reset=True) if not self.comparison_captioner.sample_values( mode=mode, correct=(self.incorrect_mode != 2), predication=comp_predication): # 2: incorrect comparison return False if self.incorrect_mode == 3: # 3: incorrect spatial relation self.incorrect_relations = [ (predtype, value) for predtype, value in self.relations if predtype != self.predtype or value != self.value ] predication.apply(predicate=self.predtype) return True
def sample_values(self, mode, correct, predication): assert predication.empty() if not super(NumberBoundCaptioner, self).sample_values(mode=mode, correct=correct, predication=predication): return False self.incorrect_mode = 0 if correct else 1 + util.sample(self.incorrect_distribution) if not self.quantifier_captioner.sample_values(mode=mode, correct=(self.incorrect_mode != 1), predication=predication): # 1: incorrect quantifier return False # potentially option to choose fixed number bound? # -----> # qtype = self.quantifier_captioner.qtype # quantity = self.quantifier_captioner.quantity # for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): # self.bound = choice(self.number_bounds) # if not correct or ((qtype != 'count' or self.bound >= quantity) and (qtype != 'ratio' or quantity == 0.0 or self.bound >= 1.0 // quantity)): # break # else: # return False return True
def sample_values(self, mode, predication): assert predication.empty() if not super(NumberBoundCaptioner, self).sample_values( mode=mode, predication=predication): return False if not self.quantifier_captioner.sample_values( mode=mode, predication=predication): return False for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): self.incorrect_mode = util.sample(self.incorrect_distribution) if self.incorrect_mode == 0 and not self.quantifier_captioner.incorrect_possible( ): continue break else: return False # potentially option to choose fixed number bound? # self.bound = choice(self.number_bounds) return True
def sample_values(self, mode, predication): assert predication.empty() if not super(QuantifierCaptioner, self).sample_values( mode=mode, predication=predication): return False # predication = predication.copy() if not self.body_captioner.sample_values(mode=mode, predication=predication): return False if not self.restrictor_captioner.sample_values( mode=mode, predication=predication): return False self.qtype = choice(list(self.quantifiers)) self.qrange, self.quantity = choice(self.quantifiers[self.qtype]) if (self.qtype, self.qrange, self.quantity) in self.zero_quantifiers: assert self.body_captioner.incorrect_possible() self.zero_quantification = True elif (self.qtype, self.qrange, self.quantity) in self.zero_included_quantifiers: self.zero_quantification = self.body_captioner.incorrect_possible( ) and random() < self.zero_quantification_rate else: self.zero_quantification = False if (self.qtype, self.qrange, self.quantity) in self.all_quantifiers: self.all_quantification = True elif (self.qtype, self.qrange, self.quantity) in self.all_included_quantifiers: self.all_quantification = random() < self.all_quantification_rate else: self.all_quantification = False for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): self.incorrect_mode = util.sample(self.incorrect_distribution) if self.incorrect_mode == 0 and not self.restrictor_captioner.incorrect_possible( ): continue elif self.incorrect_mode == 1 and not self.body_captioner.incorrect_possible( ): continue elif self.incorrect_mode == 0 and self.zero_quantification: continue elif self.incorrect_mode in ( 0, 1) and (self.qtype, self.qrange, self.quantity) in self.tautological_quantifiers: # always true in whatever way restrictor/body is changed continue elif self.incorrect_mode == 3 and not any( q == self.quantity and r != self.qrange for r, q in self.quantifiers[self.qtype]): continue elif self.incorrect_mode == 4 and not any( r == self.qrange and q != self.quantity for r, q in self.quantifiers[self.qtype]): continue break else: return False if self.incorrect_mode < 2: self.incorrect_qrange = self.qrange self.incorrect_quantity = self.quantity else: # incorrect quantifier if self.incorrect_mode == 2: closest_quantities = list() for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): if self.incorrect_mode == 2: # 2: closest quantity self.incorrect_qrange = self.qrange self.incorrect_quantity = None if self.qrange in ('lt', 'leq') or (self.qrange in ('eq', 'neq') and random() < 0.5): for r, q in self.quantifiers[self.qtype]: if r != self.qrange: continue elif self.qtype == 'ratio' and q >= self.quantity: continue elif self.qtype == 'count' and ( q + 1000 * (q < 0) >= self.quantity + 1000 * (self.quantity < 0)): continue elif q in closest_quantities: continue elif self.incorrect_quantity is None or q > self.incorrect_quantity: self.incorrect_quantity = q else: for r, q in self.quantifiers[self.qtype]: if r != self.qrange: continue elif self.qtype == 'ratio' and q <= self.quantity: continue elif self.qtype == 'count' and ( q + 1000 * (q < 0) <= self.quantity + 1000 * (self.quantity < 0)): continue elif q in closest_quantities: continue elif self.incorrect_quantity is None or q < self.incorrect_quantity: self.incorrect_quantity = q if self.incorrect_quantity is None: return False closest_quantities.append(self.incorrect_quantity) if self.incorrect_mode == 3: # 3: incorrect range self.incorrect_qrange = choice([ r for r, q in self.quantifiers[self.qtype] if q == self.quantity and r != self.qrange ]) self.incorrect_quantity = self.quantity elif self.incorrect_mode == 4: # 4: incorrect quantity self.incorrect_qrange = self.qrange self.incorrect_quantity = choice([ q for r, q in self.quantifiers[self.qtype] if r == self.qrange and q != self.quantity ]) elif self.incorrect_mode == 5: # 5: incorrect quantifier of same type self.incorrect_qrange, self.incorrect_quantity = choice( self.quantifiers[self.qtype]) if Quantifier.tautological(qtype=self.qtype, qrange1=self.qrange, quantity1=self.quantity, qrange2=self.incorrect_qrange, quantity2=self.incorrect_quantity): continue elif (self.qtype, self.incorrect_qrange, self.incorrect_quantity ) in self.tautological_quantifiers: # always true, so never incorrect continue elif self.zero_quantification and ( self.qtype, self.incorrect_qrange, self. incorrect_quantity) in self.zero_included_quantifiers: # always true if zero, so never incorrect continue elif not self.zero_quantification and ( self.qtype, self.incorrect_qrange, self.incorrect_quantity ) in Quantifier.zero_negated_quantifiers: # always true unless zero, so never incorrect continue elif self.all_quantification and ( self.qtype, self.incorrect_qrange, self. incorrect_quantity) in self.all_included_quantifiers: # always true if all, so never incorrect continue elif not self.all_quantification and ( self.qtype, self.incorrect_qrange, self. incorrect_quantity) in self.all_negated_quantifiers: # always true unless all, so never incorrect continue break else: return False return True
def sample_values(self, mode, predication): if not super(SelectorCaptioner, self).sample_values(mode=mode, predication=predication): return False for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): self.predtype, self.value = choice(self.selectors) if self.predtype in ('size-two', 'size-max') and not self.logical_contradiction and predication.blocked(predicate='shape'): continue elif self.predtype in ('shade-two', 'shade-max') and not self.logical_contradiction and predication.blocked(predicate='color'): continue break for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): scope_predication = predication.copy() if not self.scope_captioner.sample_values(mode=mode, predication=scope_predication): continue elif self.predtype in ('size-two', 'size-max') and ((not predication.redundant(predicate='shape') and not scope_predication.redundant(predicate='shape')) or (not self.logical_redundancy and predication.redundant(predicate='shape') and scope_predication.redundant(predicate='shape')) or (not self.logical_contradiction and scope_predication.blocked(predicate='shape'))): continue elif self.predtype in ('shade-two', 'shade-max') and ((not predication.redundant(predicate='color') and not scope_predication.redundant(predicate='color')) or (not self.logical_redundancy and predication.redundant(predicate='color') and scope_predication.redundant(predicate='color')) or (not self.logical_contradiction and scope_predication.blocked(predicate='color'))): continue predication.predicates.update(scope_predication.predicates) predication.blocked_preds.update(scope_predication.blocked_preds) break else: return False self.incorrect_mode = util.sample(self.incorrect_distribution) self.incorrect_predtype = self.predtype self.incorrect_value = self.value if self.incorrect_mode == 0: # 0: incorrect selectors for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): self.incorrect_predtype, self.incorrect_value = choice(self.selectors) if self.incorrect_predtype == self.predtype and self.incorrect_value == self.value: continue elif self.incorrect_predtype in ('size-two', 'size-max') and not self.logical_contradiction and (predication.blocked(predicate='shape') or scope_predication.blocked(predicate='shape')): continue elif self.incorrect_predtype in ('shade-two', 'shade-max')and not self.logical_contradiction and (predication.blocked(predicate='color') or scope_predication.blocked(predicate='color')): continue break else: return False # if not self.scope_captioner.sample_values(mode=mode, predication=predication): # return False if self.predtype in Selector.comparison_selectors or self.incorrect_predtype in Selector.comparison_selectors: comp_predication = predication.copy(reset=True) if not self.comparison_captioner.sample_values(mode=mode, predication=comp_predication): return False if self.predtype in ('size-two', 'size-max') or self.incorrect_predtype in ('size-two', 'size-max'): predication.apply(predicate='shape') if not self.logical_redundancy or (not self.logical_contradiction and scope_predication.redundant(predicate='shape') and self.incorrect_predtype in ('size-two', 'size-max')): predication.block(predicate='shape') elif self.predtype in ('shade-two', 'shade-max') or self.incorrect_predtype in ('shade-two', 'shade-max'): predication.apply(predicate='color') if not self.logical_redundancy or (not self.logical_contradiction and scope_predication.redundant(predicate='color') and self.incorrect_predtype in ('shade-two', 'shade-max')): predication.block(predicate='color') return True
def sample_values(self, mode, predication): assert predication.empty() if not super(QuantifierCaptioner, self).sample_values( mode=mode, predication=predication): return False self.incorrect_mode = util.sample(self.incorrect_distribution) for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): self.qtype, self.qrange, self.quantity = choice(self.quantifiers) if self.incorrect_mode == 0 and ( self.qtype, self.qrange, self.quantity) in Quantifier.zero_quantifiers: continue elif self.incorrect_mode in (0, 1) and ( self.qtype, self.qrange, self.quantity) in Quantifier.tautological_quantifiers: # always true in whatever way restrictor/body is changed continue break else: return False predication = predication.copy() if self.incorrect_mode == 0: # 0: incorrect restrictor # incorrect after correct if (self.qtype, self.qrange, self.quantity) in Quantifier.zero_quantifiers: # always incorrect body for zero quantification, since we need an incorrect body for a correct caption if not self.body_captioner.sample_values( mode=mode, predication=predication): return False else: if not self.body_captioner.sample_values( mode=mode, predication=predication): return False if not self.restrictor_captioner.sample_values( mode=mode, predication=predication): return False else: if not self.restrictor_captioner.sample_values( mode=mode, predication=predication): return False if (self.qtype, self.qrange, self.quantity) in Quantifier.zero_quantifiers: # always incorrect body for zero quantification, since we need an incorrect body for a correct caption if not self.body_captioner.sample_values( mode=mode, predication=predication): return False else: if not self.body_captioner.sample_values( mode=mode, predication=predication): return False if self.incorrect_mode == 2: # 2: incorrect quantifier self.incorrect_quantifiers = [ (qtype, qrange, quantity) for qtype, qrange, quantity in self.quantifiers if qtype != self.qtype or qrange != self.qrange or quantity != self.quantity ] return True
def sample_values(self, mode, predication): if not super(RegularTypeCaptioner, self).sample_values( mode=mode, predication=predication): return False self.valid_attributes = list() is_hypernym = 0 if len(self.shapes) > 1: if (self.logical_redundancy or not predication.redundant(predicate='shape')) and ( self.logical_contradiction or not predication.blocked(predicate='shape')): self.valid_attributes.append('shape') else: is_hypernym = 1 if len(self.colors) > 1: if (self.logical_redundancy or not predication.redundant(predicate='color')) and ( self.logical_contradiction or not predication.blocked(predicate='color')): self.valid_attributes.append('color') else: is_hypernym = 1 if len(self.textures) > 1: if (self.logical_redundancy or not predication.redundant(predicate='texture')) and ( self.logical_contradiction or not predication.blocked(predicate='texture')): self.valid_attributes.append('texture') else: is_hypernym = 1 if not self.logical_tautology and predication.tautological( predicates=self.valid_attributes): return False for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): self.incorrect_mode = util.sample(self.incorrect_distribution) if self.incorrect_mode == 0 and ( 'shape' not in self.valid_attributes or (not self.logical_contradiction and predication.redundant(predicate='shape'))): continue elif self.incorrect_mode == 1 and ( 'color' not in self.valid_attributes or (not self.logical_contradiction and predication.redundant(predicate='color'))): continue elif self.incorrect_mode == 2 and ( 'texture' not in self.valid_attributes or (not self.logical_contradiction and predication.redundant(predicate='texture'))): continue elif self.incorrect_mode == 3 and ( len(self.valid_attributes) == 0 or (not self.logical_contradiction and all( predication.redundant(predicate=attribute) for attribute in self.valid_attributes))): continue break else: return False if not self.logical_contradiction and self.incorrect_mode == 3: # since otherwise an incorrect predicate might contradict parts of the predication for attribute in list(self.valid_attributes): if predication.redundant(predicate=attribute): self.valid_attributes.remove(attribute) is_hypernym = 1 # attribute set is already smaller assert len(self.valid_attributes) > 0 self.hypernym = random() < self.hypernym_rate shuffle(self.valid_attributes) if self.hypernym: for _ in range(self.__class__.MAX_SAMPLE_ATTEMPTS): self.attributes = choice([ list(comb) for n in range( 1, len(self.valid_attributes) + int(is_hypernym)) for comb in combinations(self.valid_attributes, n) ]) if not self.logical_tautology and predication.tautological( predicates=self.attributes): continue elif self.incorrect_mode == 0 and 'shape' not in self.attributes: continue elif self.incorrect_mode == 1 and 'color' not in self.attributes: continue elif self.incorrect_mode == 2 and 'texture' not in self.attributes: continue break else: return False else: self.attributes = list(self.valid_attributes) if self.incorrect_mode == 0: self.attributes.remove('shape') self.attributes.insert(0, 'shape') elif self.incorrect_mode == 1: self.attributes.remove('color') self.attributes.insert(0, 'color') elif self.incorrect_mode == 2: self.attributes.remove('texture') self.attributes.insert(0, 'texture') if self.existing_attribute_rate == 0.0: self.existing_attribute = False elif self.existing_attribute_rate == 1.0: self.existing_attribute = True else: self.existing_attribute = random() < self.existing_attribute_rate assert len(self.attributes) > 0 for predtype in self.attributes: predication.apply(predicate=predtype) if self.incorrect_mode == 0: predication.block(predicate='shape') elif self.incorrect_mode == 1: predication.block(predicate='color') elif self.incorrect_mode == 2: predication.block(predicate='texture') elif self.incorrect_mode == 3: for predtype in self.attributes: predication.block(predicate=predtype) return True