def forward(self, x: paddle.Tensor) -> Tuple[paddle.Tensor, paddle.Tensor]:
        confidences = []
        locations = []
        start_layer_index = 0
        header_index = 0
        end_layer_index = 0
        for end_layer_index in self.source_layer_indexes:
            if isinstance(end_layer_index, GraphPath):
                path = end_layer_index
                end_layer_index = end_layer_index.s0
                added_layer = None
            elif isinstance(end_layer_index, tuple):
                added_layer = end_layer_index[1]
                end_layer_index = end_layer_index[0]
                path = None
            else:
                added_layer = None
                path = None
            for layer in self.base_net[start_layer_index:end_layer_index]:
                x = layer(x)
            if added_layer:
                y = added_layer(x)
            else:
                y = x
            if path:
                sub = getattr(self.base_net[end_layer_index], path.name)
                for layer in sub[:path.s1]:
                    x = layer(x)
                y = x
                for layer in sub[path.s1:]:
                    x = layer(x)
                end_layer_index += 1
            start_layer_index = end_layer_index
            confidence, location = self.compute_header(header_index, y)
            header_index += 1
            confidences.append(confidence)
            locations.append(location)

        for layer in self.base_net[end_layer_index:]:
            x = layer(x)

        for layer in self.extras:
            x = layer(x)
            confidence, location = self.compute_header(header_index, x)
            header_index += 1
            confidences.append(confidence)
            locations.append(location)

        confidences = paddle.concat(confidences, 1)
        locations = paddle.concat(locations, 1)

        if self.is_test:
            confidences = F.softmax(confidences, 2)
            boxes = box_utils.convert_locations_to_boxes(
                locations, self.priors, self.config.center_variance,
                self.config.size_variance)
            boxes = box_utils.center_form_to_corner_form(boxes)
            return confidences, boxes
        else:
            return confidences, locations
Пример #2
0
def test(loader, net, criterion, device, writer=None, epoch=0):
    net.eval()
    running_loss = 0.0
    running_regression_loss = 0.0
    running_classification_loss = 0.0
    num = 0
    ToAbs = ToAbsoluteCoords()

    for _, data in enumerate(loader):
        images, boxes, labels = data
        images = images.to(device)
        boxes = boxes.to(device)
        labels = labels.to(device)
        num += 1

        with torch.no_grad():
            confidence, locations = net(images)
            if writer is not None and num % 2 == 1:
                normalized_image = ((images[0] + 1.0) / 2).to("cpu")
                image_locations = locations[0].to("cpu")
                image_confidences = confidence[0].to("cpu")

                # Warning:  Hardcoding for mobilenet config for now
                config = mobilenetv1_ssd_config
                # Filter locations by confidences for class 1
                # TODO(dga):  This should be more general and should be a function
                # shared with the live demo at least...
                confident_boxes_mask = image_confidences[:, 1] > 0.2
                display_boxes = box_utils.convert_locations_to_boxes(
                    image_locations, config.priors, config.center_variance,
                    config.size_variance)
                display_boxes = display_boxes[confident_boxes_mask]
                display_boxes = box_utils.center_form_to_corner_form(
                    display_boxes)
                permuted_image = normalized_image.permute(1, 2,
                                                          0)  # CHW to HWC
                _, boxesAbs, _ = ToAbs(permuted_image, display_boxes)
                writer.add_image_with_boxes('Test Image', normalized_image,
                                            boxesAbs, epoch)

            regression_loss, classification_loss = criterion(
                confidence, locations, labels, boxes)
            loss = regression_loss + classification_loss
            if writer is not None:
                # TODO:  Move this to log an average instead of every box
                writer.add_scalar('Test_Loss', loss, epoch)
                writer.add_scalar('Test_Regression_Loss', regression_loss,
                                  epoch)
                writer.add_scalar('Test_Classification_Loss',
                                  classification_loss, epoch)

        running_loss += loss.item()
        running_regression_loss += regression_loss.item()
        running_classification_loss += classification_loss.item()
    return running_loss / num, running_regression_loss / num, running_classification_loss / num
Пример #3
0
def train(loader,
          net,
          criterion,
          optimizer,
          device,
          debug_steps=100,
          epoch=-1,
          writer=None,
          target_transform=None):
    net.train(True)
    running_loss = 0.0
    running_regression_loss = 0.0
    running_classification_loss = 0.0
    ToAbs = ToAbsoluteCoords()
    for i, data in enumerate(loader):
        images, boxes, labels = data
        #         if writer is not None:
        #             normalized_image = (images[0] + 1.0)/2

        #             # Warning:  Hardcoding for mobilenet config for now
        #             config = mobilenetv1_ssd_config
        #             display_boxes = box_utils.convert_locations_to_boxes(
        #                 boxes, config.priors, config.center_variance, config.size_variance
        #             )
        #             display_boxes = box_utils.center_form_to_corner_form(display_boxes)
        # #            print("display_boxes_rel: ",display_boxes[0])
        # #            print("Display boxes shape:", display_boxes.shape)
        #             permuted_image = images[0].permute(1, 2, 0) # CHW to HWC
        # #            print("Permuted images shape", permuted_image.shape)
        #             _, boxesAbs, _ = ToAbs(permuted_image, display_boxes[0])
        #             writer.add_image_with_boxes('Training Image', normalized_image, boxesAbs, i)
        # #            print("Boxes0: ", boxesAbs)
        images = images.to(device)
        boxes = boxes.to(device)
        labels = labels.to(device)

        optimizer.zero_grad()
        confidence, locations = net(images)
        regression_loss, classification_loss = criterion(
            confidence, locations, labels, boxes)  # TODO CHANGE BOXES
        loss = regression_loss + classification_loss
        loss.backward()
        optimizer.step()
        if writer is not None and i % 29 == 0:
            images = images.to("cpu")
            boxes = boxes.to("cpu")  # This is a batch of boxes
            labels = labels.to("cpu")
            locations = locations.to("cpu")

            # Extract the relevant bbox data for images[0], the one we will display in TB:
            normalized_image = (images[0] + 1.0) / 2
            image_confidence = confidence[0]
            image_locations = locations[0]
            image_boxes = boxes[0]  # The boxes just for images[0]

            # Warning:  Hardcoding for mobilenet config for now
            config = mobilenetv1_ssd_config
            gt_display_boxes = box_utils.convert_locations_to_boxes(
                image_boxes, config.priors, config.center_variance,
                config.size_variance)
            pred_display_boxes = box_utils.convert_locations_to_boxes(
                image_locations, config.priors, config.center_variance,
                config.size_variance)
            gt_display_boxes = box_utils.center_form_to_corner_form(
                gt_display_boxes)

            confident_boxes_mask = image_confidence[:, 1] > 0.2
            pred_display_boxes = box_utils.convert_locations_to_boxes(
                image_locations, config.priors, config.center_variance,
                config.size_variance)
            pred_display_boxes = pred_display_boxes[confident_boxes_mask]
            pred_display_boxes = box_utils.center_form_to_corner_form(
                pred_display_boxes)

            #            print("display_boxes_rel: ",display_boxes[0])
            #            print("Display boxes shape:", display_boxes.shape)
            permuted_image = normalized_image.permute(1, 2, 0)  # CHW to HWC
            #            print("Permuted images shape", permuted_image.shape)
            _, gt_boxesAbs, _ = ToAbs(permuted_image, gt_display_boxes)
            _, pred_boxesAbs, _ = ToAbs(permuted_image, pred_display_boxes)
            writer.add_image_with_boxes('Training Image', normalized_image,
                                        gt_boxesAbs, epoch)
            writer.add_image_with_boxes('Predicted Image', normalized_image,
                                        pred_boxesAbs, epoch)


#            print("Boxes0: ", boxesAbs)

        running_loss += loss.item()
        running_regression_loss += regression_loss.item()
        running_classification_loss += classification_loss.item()
        if i % debug_steps == 0:
            avg_loss = running_loss / debug_steps
            avg_reg_loss = running_regression_loss / debug_steps
            avg_clf_loss = running_classification_loss / debug_steps
            logging.info(f"Epoch: {epoch}, Step: {i}, " +
                         f"Average Loss: {avg_loss:.4f}, " +
                         f"Average Regression Loss {avg_reg_loss:.4f}, " +
                         f"Average Classification Loss: {avg_clf_loss:.4f}")
            if writer is not None:
                writer.add_scalar('Training_Loss', avg_loss, epoch)
                writer.add_scalar('Training_Regression_Loss', avg_reg_loss,
                                  epoch)
                writer.add_scalar('Training_Classification_Loss', avg_clf_loss,
                                  epoch)
            running_loss = 0.0
            running_regression_loss = 0.0
            running_classification_loss = 0.0