def forward(self, x, inp_dim, num_classes, confidence):
     x = x.data
     global CUDA
     prediction = x
     prediction = predict_transform(prediction, inp_dim, self.anchors,
                                    num_classes, confidence, CUDA)
     return prediction
Exemple #2
0
    def forward(self, x, CUDA):
        modules = self.blocks[1:]
        outputs = {}  #cache outputs for route layer

        write = 0

        for i, module in enumerate(modules):

            module_type = (module["type"])

            if module_type == "convolutional" or module_type == "upsample" or module_type == "maxpool":
                x = self.module_list[i](x)

            elif module_type == "route":
                layers = module["layers"]
                layers = [int(a) for a in layers]

                if (layers[0]) > 0:
                    layers[0] = layers[0] - i

                if len(layers) == 1:
                    x = outputs[i + (layers[0])]

                else:
                    if (layers[1]) > 0:
                        layers[1] = layers[1] - i

                    map1 = outputs[i + layers[0]]
                    map2 = outputs[i + layers[1]]

                    x = torch.cat((map1, map2), 1)

            elif module_type == "shortcut":
                from_ = int(module["from"])
                x = outputs[i - 1] + outputs[i + from_]

            elif module_type == "yolo":

                anchors = self.module_list[i][0].anchors
                #get input dimensions
                inp_dim = int(self.net_info["height"])

                #get # of classes
                num_classes = int(module["classes"])

                #Transform
                x = x.data
                x = predict_transform(x, inp_dim, anchors, num_classes, CUDA)
                if not write:
                    detections = x
                    write = 1

                else:
                    detections = torch.cat((detections, x), 1)

            outputs[i] = x

        return detections
    def forward(self, x, CUDA):
        modules = self.blocks[1:]
        outputs = {}  # We cache the outputs for the route layer

        write = 0
        for i, module in enumerate(modules):
            module_type = (module["type"])

            # 1) Convolutional&&Upsample Layers Type
            if module_type == "convolutional" or module_type == "upsample":
                x = self.module_list[i](x)

            # 3) Route Layers Type
            elif module_type == "route":
                layers = module["layers"]
                layers = [int(a) for a in layers]

                if (layers[0]) > 0:
                    layers[0] = layers[0] - i

                if len(layers) == 1:
                    x = outputs[i + (layers[0])]

                else:
                    if (layers[1]) > 0:
                        layers[1] = layers[1] - i

                    map1 = outputs[i + layers[0]]
                    map2 = outputs[i + layers[1]]
                    x = torch.cat((map1, map2), 1)

            # 4) Residual Layers Type
            elif module_type == "shortcut":
                tail_3 = int(module["from"])
                x = outputs[i - 1] + outputs[i + tail_3]

            # 5) YOLO Layers Type
            elif module_type == 'yolo':
                anchors = self.module_list[i][0].anchors
                # Get the input dimensions
                inp_dim = int(self.net_info["height"])

                # Get the number of classes
                num_classes = int(module["classes"])

                # Transform
                x = x.data
                x = predict_transform(x, inp_dim, anchors, num_classes, CUDA)
                if not write:  # if no collector has been intialised.
                    detections = x
                    write = 1

                else:
                    detections = torch.cat((detections, x), 1)

            outputs[i] = x

        return detections
    def forward(self, x):
        modules = self.blocks[1:]
        outputs = {}  # We cache the outputs for the route layer
        write = False
        for module_idx, module in enumerate(modules):
            module_type = module['type']
            if module_type == 'convolutional' or module_type == 'upsample':
                x = self.module_list[module_idx](x)
            elif module_type == 'route':
                layers = module['layers']
                if isinstance(layers, list):
                    if layers[0] < 0:
                        layers[0] += module_idx
                    map1 = outputs[layers[0]]
                    map2 = outputs[layers[1]]

                    x = torch.cat((map1, map2), 1)

                else:
                    x = outputs[module_idx + layers]

            elif module_type == 'shortcut':
                map_from = outputs[module_idx + module['from']]
                map_prev = outputs[module_idx - 1]
                x = map_prev + map_from

            elif module_type == 'yolo':
                anchors = self.module_list[module_idx][0].anchors
                # get dim
                input_height = self.net_info['height']
                input_width = self.net_info['width']

                # get number of classes
                num_classes = module['classes']

                # transform
                x = x.data
                x = predict_transform(x,
                                      input_height,
                                      input_width,
                                      anchors,
                                      num_classes,
                                      CUDA=False)

                if not write:
                    detections = x
                    write = True

                else:
                    # so that's a detection layer
                    detections = torch.cat((detections, x), 1)

            outputs[module_idx] = x

        return detections
Exemple #5
0
    def forward(self, x):
        modules = self.blocks[1:]
        outputs = {}

        write = 0
        for i, module in enumerate(modules):
            module_type = module['type']

            if module_type == 'convolutional' or \
                module_type == 'upsample':
                x = self.module_list[i](x)

            elif module_type == 'route':
                layers = module['layers'].split(',')
                layers = [int(a) for a in layers]

                if layers[0] > 0:
                    layers[0] = layers[0] - i

                if len(layers) == 1:
                    x = outputs[i + layers[0]]
                else:
                    if layers[1] > 0:
                        layers[1] = layers[1] - i

                    map1 = outputs[i + layers[0]]
                    map2 = outputs[i + layers[1]]

                    x = torch.cat((map1, map2), 1)
            
            elif module_type == 'shortcut':
                from_ = int(module['from'])
                x = outputs[i - 1] + outputs[i + from_]

            elif module_type == 'yolo':
                anchors = self.module_list[i][0].anchors
                # get the input dimension
                input_dim = int(self.net_info['height'])
                # get the number of classes
                num_classes = int(module['classes'])

                # transform
                x = predict_transform(x, input_dim, anchors, num_classes)
                if not write:
                    detections = x
                    write = 1
                else:
                    detections = torch.cat((detections, x), 1)

            outputs[i] = x
        
        return detections
    def forward(self, x):
        outputs = []
        detections = None
        for block_id, block in enumerate(self.blocks[1:]):
            if block['type'] == 'convolutional':
                x = self.module_list[block_id](x)
            elif block['type'] == 'upsample':
                x = self.module_list[block_id](x)
            elif block['type'] == 'route':
                route_layers = block['layers'].split(',')
                start = int(route_layers[0])
                try:
                    end = int(route_layers[1])
                except:
                    end = 0
                if end > 0:
                    end = end - block_id

                assert start < 0
                assert end <= 0  # end==0只有一个start

                if end < 0:
                    map1 = outputs[block_id + start]
                    map2 = outputs[block_id + end]
                    x = torch.cat((map1, map2), 1)
                else:
                    x = outputs[block_id + start]
            elif block['type'] == 'shortcut':
                from_block_id = int(block['from'])
                x = outputs[block_id - 1] + outputs[block_id + from_block_id]
            elif block['type'] == 'yolo':
                anchors = self.module_list[block_id][0].anchors
                input_height = int(self.net_info["height"])
                input_width = int(self.net_info["width"])
                assert input_height == input_width
                input_dim = (input_height + input_width) // 2
                num_classes = int(block["classes"])
                x = utils.predict_transform(x, input_dim, anchors, num_classes)
                if detections is None:
                    detections = x
                else:
                    detections = torch.cat((detections, x), 1)
            outputs.append(x)
        return detections
Exemple #7
0
    def forward(self, x, CUDA):
        #we iterate over self.blocks[1:] instead of self.blocks since the first element of
        # self.blocks is a net block which isn't a part of the forward pass.
        modules = self.blocks[1:]
        outputs = {}  #We cache the outputs for the route layer

        write = 0  #the first detection flag
        #we now iterate over module_list which contains the modules of the network.
        for i, module in enumerate(modules):
            module_type = (module["type"])
            # print(module_type)
            if module_type == "convolutional" or module_type == "upsample":
                x = self.module_list[i](x)

            elif module_type == "roote":

                layers = module["layers"]
                layers = [int(a) for a in layers]

                if layers[0] > 0:
                    layers[0] = layers[0] - i

                if len(layers) == 1:
                    x = outputs[layers[0] + i]
                elif len(layers) > 0:
                    if (layers[1] > 0):
                        layers[1] = layers[1] - i

                    map1 = outputs[layers[0] + i]
                    map2 = outputs[layers[1] + i]
                    # print("map1 size {}".format(map1.size()))
                    # print("map2 size {}".format(map2.size()))
                    x = torch.concat((map1, map2), 1)
                    # print("conat map1 and map2 size {}".format(x.size()))

            elif module_type == "shortcut":
                from_ = int(module["from"])
                x = outputs[i - 1] + outputs[i + from_]

            elif module_type == "yolo":
                anchors = self.module_list[i][0].anchors  #????????????
                #get the input dimentions
                inp_dim = int(self.net_info["height"])

                #get the number of classes
                num_classes = int(module["classes"])

                #transform
                detect = x.data
                detect = predict_transform(detect, inp_dim, anchors,
                                           num_classes)

                if not write:
                    detections = detect
                    write = 1
                else:
                    detections = torch.cat((detections, detect), 1)

            # print(x.size())
            outputs[i] = x
        #The shape of this tensor is 1 x 10647 x 85. The first dimension is the batch size which is simply 1 because we have used a single image.
        # For each image in a batch,
        #  we have a 10647 x 85 table. The row of each of this table represents a bounding box. (4 bbox attributes, 1 objectness score, and 80 class scores

        return detections
Exemple #8
0
    def forward(self, x, CUDA):
        detections = []
        modules = self.blocks[1:]
        outputs = {}  #We cache the outputs for the route layer

        write = 0
        for i in range(len(modules)):

            module_type = (modules[i]["type"])
            if module_type == "convolutional" or module_type == "upsample" or module_type == "maxpool":

                x = self.module_list[i](x)
                outputs[i] = x

            elif module_type == "route":
                layers = modules[i]["layers"]
                layers = [int(a) for a in layers]

                if (layers[0]) > 0:
                    layers[0] = layers[0] - i

                if len(layers) == 1:
                    x = outputs[i + (layers[0])]

                else:
                    if (layers[1]) > 0:
                        layers[1] = layers[1] - i

                    map1 = outputs[i + layers[0]]
                    map2 = outputs[i + layers[1]]

                    x = torch.cat((map1, map2), 1)
                outputs[i] = x

            elif module_type == "shortcut":
                from_ = int(modules[i]["from"])
                x = outputs[i - 1] + outputs[i + from_]
                outputs[i] = x

            elif module_type == 'yolo':

                anchors = self.module_list[i][0].anchors
                #Get the input dimensions
                inp_dim = int(self.net_info["height"])

                #Get the number of classes
                num_classes = int(modules[i]["classes"])

                #Output the result
                x = x.data
                x = predict_transform(x, inp_dim, anchors, num_classes, CUDA)

                if type(x) == int:
                    continue

                if not write:
                    detections = x
                    write = 1

                else:
                    detections = torch.cat((detections, x), 1)

                outputs[i] = outputs[i - 1]

        try:
            return detections
        except:
            return 0