Change model, still broken

This commit is contained in:
Danila Fedorin 2019-12-22 23:17:28 -08:00
parent 84712a1740
commit 6e6b9b5cd6
1 changed files with 11 additions and 9 deletions

View File

@ -42,6 +42,8 @@ class SimpleCNN(object):
for i in range(2)]) for i in range(2)])
return tf.map_fn(per_output_tensor, output_tensor) return tf.map_fn(per_output_tensor, output_tensor)
truth_tensor = tf.transpose(truth_tensor, perm=(1,2,0))
output_tensor = tf.transpose(output_tensor, perm=(1,2,0))
# Compute per object IOU values for each square, for each box. # Compute per object IOU values for each square, for each box.
ious = input_output_tensor(self.iou) ious = input_output_tensor(self.iou)
@ -57,7 +59,7 @@ class SimpleCNN(object):
eq_min_box = tf.map_fn(lambda iou: eq_min_box = tf.map_fn(lambda iou:
tf.convert_to_tensor([equal(iou[j], min_class_ious) for j in range(2)]), ious, dtype='bool') tf.convert_to_tensor([equal(iou[j], min_class_ious) for j in range(2)]), ious, dtype='bool')
# Same as above, but per-square rather than per-box. # Same as above, but per-square rather than per-box.
eq_min_square= any(eq_min_box, axis=1) eq_min_square = any(eq_min_box, axis=1)
# Whether each box of each square is responsible # Whether each box of each square is responsible
# for the maximum IOU. This is used for penalizing # for the maximum IOU. This is used for penalizing
@ -65,7 +67,7 @@ class SimpleCNN(object):
eq_max_box = tf.map_fn(lambda iou: eq_max_box = tf.map_fn(lambda iou:
tf.convert_to_tensor([equal(iou[j], max_class_ious) for j in range(2)]), ious, dtype='bool') tf.convert_to_tensor([equal(iou[j], max_class_ious) for j in range(2)]), ious, dtype='bool')
# Same as above, but per-square. Penalizes bad class guesses. # Same as above, but per-square. Penalizes bad class guesses.
eq_max_square= any(eq_max_box, axis=1) eq_max_square = any(eq_max_box, axis=1)
# The cost of incorrect coordinate guesses per box. # The cost of incorrect coordinate guesses per box.
coord_cost = input_output_tensor( coord_cost = input_output_tensor(
@ -79,7 +81,7 @@ class SimpleCNN(object):
# The cost of incorrect class guesses, per square. # The cost of incorrect class guesses, per square.
class_cost = tf.map_fn(lambda output: class_cost = tf.map_fn(lambda output:
tf.map_fn(lambda truth: tf.map_fn(lambda truth:
tf.norm(output[2*5:2*5+6]-truth[5:12]), truth_tensor), output_tensor) sum(pow(output[2*5:2*5+6]-truth[5:12],2), axis=0), truth_tensor), output_tensor)
# Weights from the YOLO paper. # Weights from the YOLO paper.
coord_weight = 5 coord_weight = 5
@ -92,15 +94,15 @@ class SimpleCNN(object):
noobj_cost= noobj_weight * confidence_cost noobj_cost= noobj_weight * confidence_cost
# Cost per box, selecting only "responsible" entries. # Cost per box, selecting only "responsible" entries.
box_cost = ( box_cost = (
obj_cost* cast(eq_max_box, 'float32') + obj_cost* tf.cast(eq_max_box, tf.float32) +
noobj_cost* cast(eq_min_box, 'float32') noobj_cost* tf.cast(eq_min_box, tf.float32)
) )
# Cost per square, penalizing only "responsible" squares. # Cost per square, penalizing only "responsible" squares.
square_cost= class_cost * cast(eq_max_square, 'float32') square_cost = class_cost * tf.cast(eq_max_square, tf.float32)
# Total cost # Total cost
cost = sum(sum(sum(box_cost))) + sum(sum(square_cost)) cost = sum(box_cost) + sum(square_cost)
return cost return cost
def build_model(self): def build_model(self):
@ -118,7 +120,7 @@ class SimpleCNN(object):
self.model.add(Flatten()) self.model.add(Flatten())
self.model.add(Dense(units=self.squares*self.squares*(self.boxes*5+self.classes))) self.model.add(Dense(units=self.squares*self.squares*(self.boxes*5+self.classes)))
self.model.add(Reshape((64,-1))) self.model.add(Reshape((64,-1)))
self.model.compile(loss=self.loss, optimizer=Adam(lr=0.5e-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)) self.model.compile(loss=self.cost, optimizer=Adam(lr=0.5e-5, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0))
def maxpool_layer(self, *args, **kwargs): def maxpool_layer(self, *args, **kwargs):
self.model.add(MaxPooling2D(*args, **kwargs)) self.model.add(MaxPooling2D(*args, **kwargs))
@ -143,7 +145,7 @@ def image_generator(source, n):
inputs = [] inputs = []
outputs = [] outputs = []
for (inp, out) in image_generator('data/test', 100): for (inp, out) in image_generator('data/test', 200):
inputs.append(inp) inputs.append(inp)
outputs.append(out) outputs.append(out)