Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main():
print('Initializing...')
# TODO..
parser = argparse.ArgumentParser('Butterfly matrix recovery')
parser.add_argument('--method', type=str, choices=['none', 'butterfly', 'lowrank', 'sparse'], default='none', help='Compression method')
parser.add_argument('--steps', type=int, default=25000, help='Number of training steps')
args = parser.parse_args()
state_dict = torch.load('model_optimizer.pth')['model']
print('Loaded model')
if args.method == 'none':
print('Validating...')
print(validate(state_dict))
exit()
for layer in range(4, 5):
for block in range(2):
for conv in range(1, 3):
weight_name = f'layer{layer}.{block}.conv{conv}.weight'
weight = state_dict[weight_name]
print('Weight:', weight_name)
for mat_id in range(weight[0,0].numel()):
mat_id_x = mat_id // weight[0,0,0].numel()
mat_id_y = mat_id % weight[0,0,0].numel()
def decom_vgg16():
# the 30th layer of features is relu of conv5_3
if opt.caffe_pretrain:
model = vgg16(pretrained=False)
model.load_state_dict(torch.load(opt.caffe_pretrain_path))
else:
model = vgg16(not opt.caffe_pretrain)
features = list(model.features)[:30]
classifier = model.classifier
classifier = list(classifier)
del classifier[6]
if not opt.use_drop:
del classifier[5]
del classifier[2]
classifier = nn.Sequential(*classifier)
# freeze top4 conv
for layer in features[:10]:
for p in layer.parameters():
def load_latest_policy(out_dir, policy):
model_dir = os.path.join(out_dir, 'models')
mkdir_p(model_dir)
models = sorted(glob.glob('%s/*.pytorch' % model_dir))
if models:
policy.load_state_dict(torch.load(sorted(models)[-1]))
else:
torch.save(policy.state_dict(), '%s/%s.pytorch' % (model_dir, mk_iter_name(0)))
models = sorted(glob.glob('%s/*.pytorch' % model_dir))
policy.type(config.tt())
if config.use_cuda():
policy = policy.cuda()
return models
def blvnet_tam_backbone(depth, alpha, beta, num_frames, blending_frames=3, input_channels=3,
imagenet_blnet_pretrained=True):
layers = {
50: [3, 4, 6, 3],
101: [4, 8, 18, 3],
152: [5, 12, 30, 3]
}[depth]
model = bLVNet_TAM_BACKBONE(Bottleneck, layers, alpha, beta, num_frames,
blending_frames=blending_frames, input_channels=input_channels)
if imagenet_blnet_pretrained:
checkpoint = torch.load(model_urls['blresnet{}'.format(depth)], map_location='cpu')
print("loading weights from ImageNet-pretrained blnet, blresnet{}".format(depth),
flush=True)
# fixed parameter names in order to load the weights correctly
state_d = OrderedDict()
if input_channels != 3: # flow
print("Convert RGB model to Flow")
for key, value in checkpoint['state_dict'].items():
new_key = key.replace('module.', '')
if "conv1.weight" in key:
o_c, in_c, k_h, k_w = value.shape
else:
o_c, in_c, k_h, k_w = 0, 0, 0, 0
if k_h == 7 and k_w == 7:
# average the weights and expand to all channels
new_shape = (o_c, input_channels, k_h, k_w)
new_value = value.mean(dim=1, keepdim=True).expand(new_shape).contiguous()
t_set = OfficeImage(t_root, t_label, data_transform)
assert len(t_set) == get_dataset_length(args.target + '_shared')
t_loader = torch.utils.data.DataLoader(t_set, batch_size=args.batch_size,
shuffle=args.shuffle, num_workers=args.num_workers)
netG = ResBase50().cuda()
netF = ResClassifier(class_num=args.class_num, extract=False).cuda()
netG.eval()
netF.eval()
for epoch in range(args.epoch/2, args.epoch + 1):
if epoch % 10 != 0:
continue
netG.load_state_dict(torch.load(os.path.join(args.snapshot, "OfficeHome_HAFN_" + args.task + "_netG_" + args.post + "." + args.repeat + "_" + str(epoch) + ".pth")))
netF.load_state_dict(torch.load(os.path.join(args.snapshot, "OfficeHome_HAFN_" + args.task + "_netF_" + args.post + "." + args.repeat + "_" + str(epoch) + ".pth")))
correct = 0
tick = 0
for (imgs, labels) in t_loader:
tick += 1
imgs = Variable(imgs.cuda())
pred = netF(netG(imgs))
pred = F.softmax(pred)
pred = pred.data.cpu().numpy()
pred = pred.argmax(axis=1)
labels = labels.numpy()
correct += np.equal(labels, pred).sum()
correct = correct * 1.0 / len(t_set)
print "Epoch {0}: {1}".format(epoch, correct)
result.write("Epoch " + str(epoch) + ": " + str(correct) + "\n")
if args.visdom:
import visdom
viz = visdom.Visdom()
ssd_net = build_ssd('train', cfg['min_dim'], cfg['num_classes'])
net = ssd_net
if args.cuda:
net = torch.nn.DataParallel(ssd_net)
cudnn.benchmark = True
if args.resume:
print('Resuming training, loading {}...'.format(args.resume))
ssd_net.load_weights(args.resume)
else:
vgg_weights = torch.load(args.save_folder + args.basenet)
print('Loading base network...')
ssd_net.vgg.load_state_dict(vgg_weights)
if args.cuda:
net = net.cuda()
if not args.resume:
print('Initializing weights...')
# initialize newly added layers' weights with xavier method
ssd_net.extras.apply(weights_init)
ssd_net.loc.apply(weights_init)
ssd_net.conf.apply(weights_init)
optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=args.momentum,
weight_decay=args.weight_decay)
criterion = MultiBoxLoss(cfg['num_classes'], 0.5, True, 0, True, 3, 0.5,
def train(train_summ_batches, valid_summ_data, train_ent_batches,
np_embeddings, meta_data, num_epochs,
pt_reload=False, switch=1, print_every=1,
validate_every=1, learning_rate=0.005):
ent_batches_len = 10
hidden_size = 512
summ_encoder = None
ent_encoder = None
decoder = None
if pt_reload:
summ_encoder = torch.load('reloads/summ_encoder.pt')
ent_encoder = torch.load('reloads/ent_encoder.pt')
decoder = torch.load('reloads/decoder.pt')
print_msg('reloading of saved models done ..')
else:
summ_encoder = EncoderRNN(vocab_size, embedding_size,
hidden_size, np_embeddings)
ent_encoder = EncoderRNN(vocab_size, embedding_size,
hidden_size, np_embeddings)
decoder = AttnDecoderRNN(vocab_size, embedding_size,
hidden_size, np_embeddings)
if USE_CUDA:
summ_encoder = summ_encoder.cuda()
ent_encoder = ent_encoder.cuda()
decoder = decoder.cuda()
model = Model(
last_conv_stride=last_conv_stride,
num_stripes=num_stripes,
local_conv_out_channels=local_conv_out_channels
)
model_w = DataParallel(model)
TMO([model])
# preprocessing
preprocessor = PreProcessIm(resize_h_w=resize_h_w, scale=scale_im,
im_mean=im_mean, im_std=im_std)
# load model
map_location = (lambda storage, loc: storage)
sd = torch.load(model_weight_file, map_location=map_location)
load_state_dict(model, sd['state_dicts'][0])
print('Loaded model weight from {}'.format(model_weight_file))
extractor = ExtractFeature(model_w, TVT)
return preprocessor, extractor
def run_model(opt):
# get misc file used for the specified data format
misc = DatasetMisc(opt['dataset_type'])
# class that takes care of plotting
pose_plotter = PosePlotter(
pose_config['KEYPOINT_NAMES'],
pose_config['SKELETON_NAMES'],
pose_config['KEYPOINT_COLORS'],
pose_config['SKELETON_COLORS'])
# load checkpoint file
ckpt = torch.load(opt['load'])
stat_2d = ckpt['stat_2d']
# load the pretrained model
print("\n==================Model===================")
print("Loading Pretrained Model:")
print(" - Linear size: [{}]".format(opt['linear_size']))
print(" - Num stages: [{}]".format(opt['linear_size']))
print("==========================================\n")
pretrained_model = LinearModel(misc.NUM_KEYPOINTS_2D * 2,
misc.NUM_KEYPOINTS_3D * 3,
opt['linear_size'],
opt['num_stage'],
opt['dropout'],
opt['predict_scale'],
opt['scale_range'],
resolved_config_file = cached_path(config_file, cache_dir=cache_dir)
except EnvironmentError:
logger.error("Wrong model name, should be a valid path to a folder containing "
"a {} file and a {} file or a model name in {}".format(
WEIGHTS_NAME, CONFIG_NAME, PRETRAINED_MODEL_ARCHIVE_MAP.keys()))
raise
logger.info("loading model {} from cache at {}".format(pretrained_model_name_or_path, resolved_model_file))
# Load config
config = BigGANConfig.from_json_file(resolved_config_file)
logger.info("Model config {}".format(config))
# Instantiate model.
model = cls(config, *inputs, **kwargs)
state_dict = torch.load(resolved_model_file, map_location='cpu' if not torch.cuda.is_available() else None)
model.load_state_dict(state_dict, strict=False)
return model