def _prepare_base_model(self, base_model): print('=> base model: {}'.format(base_model)) if base_model == 'mobilenetv2': from archs.mobilenet_v2 import mobilenet_v2, InvertedResidual self.base_model = mobilenet_v2(True) self.base_model.last_layer_name = 'classifier' self.input_size = 224 self.input_mean = [0.485, 0.456, 0.406] self.input_std = [0.229, 0.224, 0.225] if self.is_shift: from ops.temporal_shift import TemporalShift for m in self.base_model.modules(): if isinstance(m, InvertedResidual) and len(m.conv) == 8 and m.use_res_connect: print('Adding temporal shift... {}'.format(m.use_res_connect)) m.conv[0] = TemporalShift(m.conv[0], n_segment=self.num_segments, n_div=self.shift_div) else: raise ValueError('Unknown base model: {}'.format(base_model))
def _prepare_base_model(self, base_model): print('=> base model: {}'.format(base_model)) if 'resnet' in base_model: self.base_model = getattr( torchvision.models, base_model)(True if self.pretrain == 'imagenet' else False) if self.is_shift: print('Adding temporal shift...') from ops.temporal_shift import make_temporal_shift make_temporal_shift(self.base_model, self.num_segments, n_div=self.shift_div, place=self.shift_place, temporal_pool=self.temporal_pool) if self.non_local: print('Adding non-local module...') from ops.non_local import make_non_local make_non_local(self.base_model, self.num_segments) self.base_model.last_layer_name = 'fc' self.input_size = 224 self.input_mean = [0.485, 0.456, 0.406] self.input_std = [0.229, 0.224, 0.225] self.base_model.avgpool = nn.AdaptiveAvgPool2d(1) if self.modality == 'Flow': self.input_mean = [0.5] self.input_std = [np.mean(self.input_std)] elif self.modality == 'RGBDiff': self.input_mean = [0.485, 0.456, 0.406 ] + [0] * 3 * self.new_length self.input_std = self.input_std + [ np.mean(self.input_std) * 2 ] * 3 * self.new_length elif base_model == 'mobilenetv2': from archs.mobilenet_v2 import mobilenet_v2, InvertedResidual self.base_model = mobilenet_v2(True if self.pretrain == 'imagenet' else False) self.base_model.last_layer_name = 'classifier' self.input_size = 224 self.input_mean = [0.485, 0.456, 0.406] self.input_std = [0.229, 0.224, 0.225] self.base_model.avgpool = nn.AdaptiveAvgPool2d(1) if self.is_shift: from ops.temporal_shift import TemporalShift for m in self.base_model.modules(): if isinstance(m, InvertedResidual) and len( m.conv) == 8 and m.use_res_connect: if self.print_spec: print('Adding temporal shift... {}'.format( m.use_res_connect)) m.conv[0] = TemporalShift(m.conv[0], n_segment=self.num_segments, n_div=self.shift_div) if self.modality == 'Flow': self.input_mean = [0.5] self.input_std = [np.mean(self.input_std)] elif self.modality == 'RGBDiff': self.input_mean = [0.485, 0.456, 0.406 ] + [0] * 3 * self.new_length self.input_std = self.input_std + [ np.mean(self.input_std) * 2 ] * 3 * self.new_length elif base_model == 'BNInception': from archs.bn_inception import bninception self.base_model = bninception(pretrained=self.pretrain) self.input_size = self.base_model.input_size self.input_mean = self.base_model.mean self.input_std = self.base_model.std self.base_model.last_layer_name = 'fc' if self.modality == 'Flow': self.input_mean = [128] elif self.modality == 'RGBDiff': self.input_mean = self.input_mean * (1 + self.new_length) if self.is_shift: print('Adding temporal shift...') self.base_model.build_temporal_ops( self.num_segments, is_temporal_shift=self.shift_place, shift_div=self.shift_div) else: raise ValueError('Unknown base model: {}'.format(base_model))
def _prepare_base_model(self, base_model): print('=> base model: {}'.format(base_model)) if 'resnet' in base_model: from archs.Resnet_ann_v3 import __resnet__ self.base_model = __resnet__( base_model, True if self.pretrain == 'imagenet' else False, self.extra_temporal_modeling, self.num_segments, self.num_class) if self.is_shift: #summary(self.base_model,(3,256,256)) #print(self.base_model) print('Adding temporal shift...') from ops.temporal_shift import make_temporal_shift make_temporal_shift( self.base_model, self.num_segments, n_div=self.shift_div, place=self.shift_place, temporal_pool=self.temporal_pool, concat=self.concat, prune_list=self.prune_list[0] if self.is_prune in ['input', 'inout'] else {}, prune=True if self.is_prune in ['input', 'inout'] else False) if self.is_prune in ['output', 'inout']: print('prune from conv1 and conv2...') from ops.prune import make_prune_conv make_prune_conv(self.base_model, place=self.shift_place) if self.non_local: print('Adding non-local module...') from ops.non_local import make_non_local make_non_local(self.base_model, self.num_segments) self.base_model.last_layer_name = 'fc' self.input_size = 224 self.input_mean = [0.485, 0.456, 0.406] self.input_std = [0.229, 0.224, 0.225] self.base_model.avgpool = nn.AdaptiveAvgPool2d(1) if self.modality == 'Flow': self.input_mean = [0.5] self.input_std = [np.mean(self.input_std)] elif self.modality == 'RGBDiff': self.input_mean = [0.485, 0.456, 0.406 ] + [0] * 3 * self.new_length self.input_std = self.input_std + [ np.mean(self.input_std) * 2 ] * 3 * self.new_length elif 'efficientnet' in base_model: from efficientnet_pytorch import EfficientNet self.base_model = EfficientNet.from_pretrained( base_model, temporal_modeling=self.extra_temporal_modeling, segment=self.num_segments, num_class=self.num_class) if self.is_shift: print('Adding temporal shift...') from ops.temporal_shift import make_efficientnet_shift make_efficientnet_shift(self.base_model, self.num_segments, n_div=self.shift_div, place=self.shift_place, temporal_pool=self.temporal_pool, concat=self.concat) if self.non_local: print('Adding non-local module...') from ops.non_local import make_non_local make_non_local(self.base_model, self.num_segments) self.base_model.dropout = '_dropout' self.base_model.last_layer_name = '_fc' self.input_size = 224 self.input_mean = [0.485, 0.456, 0.406] self.input_std = [0.229, 0.224, 0.225] if self.modality == 'Flow': self.input_mean = [0.5] self.input_std = [np.mean(self.input_std)] elif self.modality == 'RGBDiff': self.input_mean = [0.485, 0.456, 0.406 ] + [0] * 3 * self.new_length self.input_std = self.input_std + [ np.mean(self.input_std) * 2 ] * 3 * self.new_length elif base_model == 'mobilenetv2': from archs.mobilenet_v2 import mobilenet_v2, InvertedResidual self.base_model = mobilenet_v2(True if self.pretrain == 'imagenet' else False) self.base_model.last_layer_name = 'classifier' self.input_size = 224 self.input_mean = [0.485, 0.456, 0.406] self.input_std = [0.229, 0.224, 0.225] self.base_model.avgpool = nn.AdaptiveAvgPool2d(1) if self.is_shift: from ops.temporal_shift import TemporalShift for m in self.base_model.modules(): if isinstance(m, InvertedResidual) and len( m.conv) == 8 and m.use_res_connect: if self.print_spec: print('Adding temporal shift... {}'.format( m.use_res_connect)) m.conv[0] = TemporalShift(m.conv[0], n_segment=self.num_segments, n_div=self.shift_div) if self.modality == 'Flow': self.input_mean = [0.5] self.input_std = [np.mean(self.input_std)] elif self.modality == 'RGBDiff': self.input_mean = [0.485, 0.456, 0.406 ] + [0] * 3 * self.new_length self.input_std = self.input_std + [ np.mean(self.input_std) * 2 ] * 3 * self.new_length elif base_model == 'BNInception': from archs.bn_inception import bninception self.base_model = bninception(pretrained=self.pretrain) self.input_size = self.base_model.input_size self.input_mean = self.base_model.mean self.input_std = self.base_model.std self.base_model.last_layer_name = 'fc' if self.modality == 'Flow': self.input_mean = [128] elif self.modality == 'RGBDiff': self.input_mean = self.input_mean * (1 + self.new_length) if self.is_shift: print('Adding temporal shift...') self.base_model.build_temporal_ops( self.num_segments, is_temporal_shift=self.shift_place, shift_div=self.shift_div) else: raise ValueError('Unknown base model: {}'.format(base_model))
def _prepare_base_model(self, base_model): print('=> base model: {}'.format(base_model)) if 'resnet' in base_model: # for 0.5 res18 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> self.base_model = getattr( torchvision.models, base_model)(True if self.pretrain == 'imagenet' else False) # if '18' in base_model: # from .half_res18 import resnet18 # self.base_model = resnet18(pretrained=False) # else: # self.base_model = getattr(torchvision.models, base_model)( # True if self.pretrain == 'imagenet' else False) # for 0.5 res18 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< if self.is_shift: print('Adding temporal shift...') from ops.temporal_shift import make_temporal_shift # from ops.spatial_transform import make_spatial_transform # make_spatial_transform(self.base_model) make_temporal_shift(self.base_model, self.num_segments, n_div=self.shift_div, place=self.shift_place, temporal_pool=self.temporal_pool, soft=self.is_softshift, init_mode=self.shift_init_mode) if self.non_local: print('Adding non-local module...') from ops.non_local import make_non_local make_non_local(self.base_model, self.num_segments) self.base_model.last_layer_name = 'fc' self.input_size = 224 self.input_mean = [0.485, 0.456, 0.406] self.input_std = [0.229, 0.224, 0.225] self.base_model.avgpool = nn.AdaptiveAvgPool2d(1) if self.modality == 'Flow': self.input_mean = [0.5] self.input_std = [np.mean(self.input_std)] elif self.modality == 'RGBDiff': self.input_mean = [0.485, 0.456, 0.406 ] + [0] * 3 * self.new_length self.input_std = self.input_std + [ np.mean(self.input_std) * 2 ] * 3 * self.new_length elif 'repvgg' in base_model: from archs.repvgg import repvgg_A0, repvgg_B1g2 # imagenet pretrained, deploy MODE if 'A0' in base_model: # compare with res18 self.base_model = repvgg_A0( True if self.pretrain == 'imagenet' else False, deploy=self.deploy) elif 'B1g2' in base_model: # compare with res50 self.base_model = repvgg_B1g2( True if self.pretrain == 'imagenet' else False, deploy=self.deploy) if self.is_shift: print('Adding temporal shift...') from ops.temporal_shift import make_temporal_shift make_temporal_shift(self.base_model, self.num_segments, n_div=self.shift_div, place=self.shift_place, temporal_pool=self.temporal_pool, deploy=self.deploy, soft=self.is_softshift, init_mode=self.shift_init_mode) self.base_model.last_layer_name = 'linear' self.input_size = 224 self.input_mean = [0.485, 0.456, 0.406] self.input_std = [0.229, 0.224, 0.225] self.base_model.gap = nn.AdaptiveAvgPool2d(1) if self.modality == 'Flow': self.input_mean = [0.5] self.input_std = [np.mean(self.input_std)] elif self.modality == 'RGBDiff': self.input_mean = [0.485, 0.456, 0.406 ] + [0] * 3 * self.new_length self.input_std = self.input_std + [ np.mean(self.input_std) * 2 ] * 3 * self.new_length elif base_model == 'mobilenetv2': from archs.mobilenet_v2 import mobilenet_v2, InvertedResidual self.base_model = mobilenet_v2(True if self.pretrain == 'imagenet' else False) # import pdb; pdb.set_trace() self.base_model.last_layer_name = 'classifier' self.input_size = 224 self.input_mean = [0.485, 0.456, 0.406] self.input_std = [0.229, 0.224, 0.225] self.base_model.avgpool = nn.AdaptiveAvgPool2d(1) if self.is_shift: from ops.temporal_shift import TemporalShift for m in self.base_model.modules(): if isinstance(m, InvertedResidual) and len( m.conv) == 8 and m.use_res_connect: if self.print_spec: print('Adding temporal shift... {}'.format( m.use_res_connect)) m.conv[0] = TemporalShift( m.conv[0], n_segment=self.num_segments, n_div=self.shift_div, soft=self.is_softshift, init_mode=self.shift_init_mode) if self.modality == 'Flow': self.input_mean = [0.5] self.input_std = [np.mean(self.input_std)] elif self.modality == 'RGBDiff': self.input_mean = [0.485, 0.456, 0.406 ] + [0] * 3 * self.new_length self.input_std = self.input_std + [ np.mean(self.input_std) * 2 ] * 3 * self.new_length elif base_model == 'BNInception': from archs.bn_inception import bninception self.base_model = bninception(pretrained=self.pretrain) self.input_size = self.base_model.input_size self.input_mean = self.base_model.mean self.input_std = self.base_model.std self.base_model.last_layer_name = 'fc' if self.modality == 'Flow': self.input_mean = [128] elif self.modality == 'RGBDiff': self.input_mean = self.input_mean * (1 + self.new_length) if self.is_shift: print('Adding temporal shift...') self.base_model.build_temporal_ops( self.num_segments, is_temporal_shift=self.shift_place, shift_div=self.shift_div) else: raise ValueError('Unknown base model: {}'.format(base_model))