def __init__(self, protofile, caffemodel): super(CaffeNet, self).__init__() self.seen = 0 self.num_classes = 1 self.is_pretrained = True if not caffemodel is None: self.is_pretrained = True self.anchors = [0.625,0.750, 0.625,0.750, 0.625,0.750, \ 0.625,0.750, 0.625,0.750, 1.000,1.200, \ 1.000,1.200, 1.000,1.200, 1.000,1.200, \ 1.600,1.920, 2.560,3.072, 4.096,4.915, \ 6.554,7.864, 10.486,12.583] #self.anchors = [1.3221, 1.73145, 3.19275, 4.00944, 5.05587, 8.09892, 9.47112, 4.84053, 11.2364, 10.0071] self.num_anchors = len(self.anchors) / 2 self.width = 480 self.height = 320 self.loss = RegionLoss(self.num_classes, self.anchors, self.num_anchors) self.net_info = parse_prototxt(protofile) self.models = self.create_network(self.net_info) self.modelList = nn.ModuleList() if self.is_pretrained: self.load_weigths_from_caffe(protofile, caffemodel) for name, model in self.models.items(): self.modelList.append(model)
def __init__(self): super(Resnet, self).__init__() self.seen = 0 self.num_classes = 20 self.anchors = [ 1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52 ] self.num_anchors = len(self.anchors) / 2 num_output = (5 + self.num_classes) * self.num_anchors self.width = 160 self.height = 160 self.loss = RegionLoss(self.num_classes, self.anchors, self.num_anchors) self.model = ResNet(Bottleneck, [3, 4, 6, 3])
train_loader = torch.utils.data.DataLoader( train_dataset, batch_size=cfg.TRAIN.BATCH_SIZE, shuffle=True, num_workers=cfg.DATA_LOADER.NUM_WORKERS, drop_last=True, pin_memory=True) test_loader = torch.utils.data.DataLoader( test_dataset, batch_size=cfg.TRAIN.BATCH_SIZE, shuffle=False, num_workers=cfg.DATA_LOADER.NUM_WORKERS, drop_last=False, pin_memory=True) loss_module = RegionLoss(cfg).cuda() # loads from core module the optimization module train = getattr(sys.modules[__name__], 'train_ucf24_jhmdb21') test = getattr(sys.modules[__name__], 'test_ucf24_jhmdb21') ####### Training and Testing Schedule # --------------------------------------------------------------- if cfg.TRAIN.EVALUATE: logging('evaluating ...') test(cfg, 0, model, test_loader) else: for epoch in range(cfg.TRAIN.BEGIN_EPOCH, cfg.TRAIN.END_EPOCH + 1): # Adjust learning rate lr_new = adjust_learning_rate(optimizer, epoch, cfg)
def __init__(self): super(TinyYoloNet, self).__init__() self.seen = 0 self.num_classes = 20 self.anchors = [ 1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52 ] self.num_anchors = len(self.anchors) / 2 num_output = (5 + self.num_classes) * self.num_anchors self.width = 160 self.height = 160 self.loss = RegionLoss(self.num_classes, self.anchors, self.num_anchors) self.cnn = nn.Sequential( OrderedDict([ # conv1 ('conv1', nn.Conv2d(3, 16, 3, 1, 1, bias=False)), ('bn1', nn.BatchNorm2d(16)), ('leaky1', nn.LeakyReLU(0.1, inplace=True)), ('pool1', nn.MaxPool2d(2, 2)), # conv2 ('conv2', nn.Conv2d(16, 32, 3, 1, 1, bias=False)), ('bn2', nn.BatchNorm2d(32)), ('leaky2', nn.LeakyReLU(0.1, inplace=True)), ('pool2', nn.MaxPool2d(2, 2)), # conv3 ('conv3', nn.Conv2d(32, 64, 3, 1, 1, bias=False)), ('bn3', nn.BatchNorm2d(64)), ('leaky3', nn.LeakyReLU(0.1, inplace=True)), ('pool3', nn.MaxPool2d(2, 2)), # conv4 ('conv4', nn.Conv2d(64, 128, 3, 1, 1, bias=False)), ('bn4', nn.BatchNorm2d(128)), ('leaky4', nn.LeakyReLU(0.1, inplace=True)), ('pool4', nn.MaxPool2d(2, 2)), # conv5 ('conv5', nn.Conv2d(128, 256, 3, 1, 1, bias=False)), ('bn5', nn.BatchNorm2d(256)), ('leaky5', nn.LeakyReLU(0.1, inplace=True)), ('pool5', nn.MaxPool2d(2, 2)), # conv6 ('conv6', nn.Conv2d(256, 512, 3, 1, 1, bias=False)), ('bn6', nn.BatchNorm2d(512)), ('leaky6', nn.LeakyReLU(0.1, inplace=True)), ('pool6', MaxPoolStride1()), # conv7 ('conv7', nn.Conv2d(512, 1024, 3, 1, 1, bias=False)), ('bn7', nn.BatchNorm2d(1024)), ('leaky7', nn.LeakyReLU(0.1, inplace=True)), # conv8 ('conv8', nn.Conv2d(1024, 1024, 3, 1, 1, bias=False)), ('bn8', nn.BatchNorm2d(1024)), ('leaky8', nn.LeakyReLU(0.1, inplace=True)), # output ('output', nn.Conv2d(1024, num_output, 1, 1, 0)), ]))
def create_network(self, blocks): models = nn.ModuleList() prev_filters = 3 out_filters = [] conv_id = 0 for block in blocks: if block['type'] == 'net': prev_filters = int(block['channels']) continue elif block['type'] == 'convolutional': conv_id = conv_id + 1 batch_normalize = int(block['batch_normalize']) filters = int(block['filters']) kernel_size = int(block['size']) stride = int(block['stride']) is_pad = int(block['pad']) pad = (kernel_size - 1) // 2 if is_pad else 0 activation = block['activation'] model = nn.Sequential() if batch_normalize: model.add_module( 'conv{0}'.format(conv_id), nn.Conv2d(prev_filters, filters, kernel_size, stride, pad, bias=False)) model.add_module('bn{0}'.format(conv_id), nn.BatchNorm2d(filters)) #model.add_module('bn{0}'.format(conv_id), BN2d(filters)) else: model.add_module( 'conv{0}'.format(conv_id), nn.Conv2d(prev_filters, filters, kernel_size, stride, pad)) if activation == 'leaky': model.add_module('leaky{0}'.format(conv_id), nn.LeakyReLU(0.1, inplace=True)) elif activation == 'relu': model.add_module('relu{0}'.format(conv_id), nn.ReLU(inplace=True)) prev_filters = filters out_filters.append(prev_filters) models.append(model) elif block['type'] == 'maxpool': pool_size = int(block['size']) stride = int(block['stride']) if stride > 1: model = nn.MaxPool2d(pool_size, stride) else: model = MaxPoolStride1() out_filters.append(prev_filters) models.append(model) elif block['type'] == 'avgpool': model = GlobalAvgPool2d() out_filters.append(prev_filters) models.append(model) elif block['type'] == 'softmax': model = nn.Softmax() out_filters.append(prev_filters) models.append(model) elif block['type'] == 'cost': if block['_type'] == 'sse': model = nn.MSELoss(size_average=True) elif block['_type'] == 'L1': model = nn.L1Loss(size_average=True) elif block['_type'] == 'smooth': model = nn.SmoothL1Loss(size_average=True) out_filters.append(1) models.append(model) elif block['type'] == 'reorg': stride = int(block['stride']) prev_filters = stride * stride * prev_filters out_filters.append(prev_filters) models.append(Reorg(stride)) elif block['type'] == 'route': layers = block['layers'].split(',') ind = len(models) layers = [ int(i) if int(i) > 0 else int(i) + ind for i in layers ] if len(layers) == 1: prev_filters = out_filters[layers[0]] elif len(layers) == 2: assert (layers[0] == ind - 1) prev_filters = out_filters[layers[0]] + out_filters[ layers[1]] out_filters.append(prev_filters) models.append(EmptyModule()) elif block['type'] == 'shortcut': ind = len(models) prev_filters = out_filters[ind - 1] out_filters.append(prev_filters) models.append(EmptyModule()) elif block['type'] == 'connected': filters = int(block['output']) if block['activation'] == 'linear': model = nn.Linear(prev_filters, filters) elif block['activation'] == 'leaky': model = nn.Sequential(nn.Linear(prev_filters, filters), nn.LeakyReLU(0.1, inplace=True)) elif block['activation'] == 'relu': model = nn.Sequential(nn.Linear(prev_filters, filters), nn.ReLU(inplace=True)) prev_filters = filters out_filters.append(prev_filters) models.append(model) elif block['type'] == 'region': loss = RegionLoss() anchors = block['anchors'].split(',') loss.anchors = [float(i) for i in anchors] loss.num_classes = int(block['classes']) loss.num_anchors = int(block['num']) loss.anchor_step = len(loss.anchors) / loss.num_anchors loss.object_scale = float(block['object_scale']) loss.noobject_scale = float(block['noobject_scale']) loss.class_scale = float(block['class_scale']) loss.coord_scale = float(block['coord_scale']) out_filters.append(prev_filters) models.append(loss) else: print('unknown type %s' % (block['type'])) return models