示例#1
0
# Load a pretrainied model and reset final fully connected layer.
#
def load_network_path(network, save_path):
    network.load_state_dict(torch.load(save_path))
    return network


model_structure = ft_net(len(class_names))
target = 'experiment_Result/p1'
path = target + '/ft_ResNet50/net_39.pth'
print('origin_with: ' + target)
model = load_network_path(model_structure, path)

model_from = ft_net(len(class_names))

nw.dup_net6(model_from, model, 'model.conv1', 'model.bn1', 'xx', 'xx',
            'model.fc', 'classifier')  #C1+FC remain

if use_gpu:
    model = model.cuda()
    #nn.DataParallel(model, device_ids=[2,3]).cuda()
criterion = nn.CrossEntropyLoss()

ignored_params = list(map(id, model.model.layer2.parameters())) + list(map(id, model.model.layer3.parameters())) + \
                 list(map(id, model.model.layer4.parameters())) + list(map(id, model.model.fc.parameters())) \
                 + list(map(id, model.classifier.parameters())) + list(map(id, model.model.layer1.parameters()))
base_params = filter(lambda p: id(p) not in ignored_params, model.parameters())
print(base_params)
# Observe that all parameters are being optimized
optimizer_ft = optim.SGD([{
    'params': base_params,
    'lr': 0.001
示例#2
0
# Load a pretrainied model and reset final fully connected layer.
#
def load_network_path(network, save_path):
    network.load_state_dict(torch.load(save_path))
    return network


model_structure = ft_net(len(class_names))
target = 'experiment_Result/p2'
path = target + '/ft_ResNet50/net_39.pth'
print('origin_with: ' + target)
model = load_network_path(model_structure, path)

model_from = ft_net(len(class_names))

nw.dup_net6(model_from, model, 'model.conv1', 'model.bn1', 'model.layer1',
            'xx', 'model.fc', 'classifier')  #C1L1L2+FC

if use_gpu:
    model = model.cuda()
    #nn.DataParallel(model, device_ids=[2,3]).cuda()
criterion = nn.CrossEntropyLoss()

ignored_params = list(map(id, model.model.layer2.parameters())) + list(map(id, model.model.layer3.parameters())) + \
                 list(map(id, model.model.layer4.parameters())) + list(map(id, model.model.fc.parameters())) \
                 + list(map(id, model.classifier.parameters())) + list(map(id, model.model.layer1.parameters()))
base_params = filter(lambda p: id(p) not in ignored_params, model.parameters())
print(base_params)
# Observe that all parameters are being optimized
optimizer_ft = optim.SGD([{
    'params': base_params,
    'lr': 0.001