From 6e6b9b5cd6f27f626616844e1ec31088515de9e8 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sun, 22 Dec 2019 23:17:28 -0800 Subject: [PATCH] Change model, still broken --- model.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/model.py b/model.py index 79304a9..bb90caf 100644 --- a/model.py +++ b/model.py @@ -42,6 +42,8 @@ class SimpleCNN(object): for i in range(2)]) 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. ious = input_output_tensor(self.iou) @@ -57,7 +59,7 @@ class SimpleCNN(object): 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') # 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 # for the maximum IOU. This is used for penalizing @@ -65,7 +67,7 @@ class SimpleCNN(object): 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') # 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. coord_cost = input_output_tensor( @@ -79,7 +81,7 @@ class SimpleCNN(object): # The cost of incorrect class guesses, per square. class_cost = tf.map_fn(lambda output: 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. coord_weight = 5 @@ -92,15 +94,15 @@ class SimpleCNN(object): noobj_cost= noobj_weight * confidence_cost # Cost per box, selecting only "responsible" entries. box_cost = ( - obj_cost* cast(eq_max_box, 'float32') + - noobj_cost* cast(eq_min_box, 'float32') + obj_cost* tf.cast(eq_max_box, tf.float32) + + noobj_cost* tf.cast(eq_min_box, tf.float32) ) # 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 - cost = sum(sum(sum(box_cost))) + sum(sum(square_cost)) + cost = sum(box_cost) + sum(square_cost) return cost def build_model(self): @@ -118,7 +120,7 @@ class SimpleCNN(object): self.model.add(Flatten()) self.model.add(Dense(units=self.squares*self.squares*(self.boxes*5+self.classes))) 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): self.model.add(MaxPooling2D(*args, **kwargs)) @@ -143,7 +145,7 @@ def image_generator(source, n): inputs = [] outputs = [] -for (inp, out) in image_generator('data/test', 100): +for (inp, out) in image_generator('data/test', 200): inputs.append(inp) outputs.append(out)