def vgg16_bn(num_classes=1000, pretrained='imagenet'): """VGG 16-layer model (configuration "D") with batch normalization """ model = models.vgg16_bn(pretrained=False) if pretrained is not None: settings = pretrained_settings['vgg16_bn'][pretrained] model = load_pretrained(model, num_classes, settings) return model
def vgg16(pre): return children(vgg16_bn(pre))[0] def vgg19(pre): return children(vgg19_bn(pre))[0]
# One final thing , class names -> known objects on Mars. Total = 25 class_names = { 'apxs', 'apxs cal target', 'chemcam cal target', 'chemin inlet open', 'drill', 'drill holes', 'drt front', 'drt side', 'ground', 'horizon', 'inlet', 'mahli', 'mahli cal target', 'mastcam', 'mastcam cal target', 'observation tray', 'portion box', 'portion tube', 'portion tube opening', 'rems uv sensor', 'rover rear deck', 'scoop', 'sun', 'turret', 'wheel' } device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # Part-2 - Data processing done. # Modelling begins. # ----------------------------------------------------------------------- # loading the pre-trained model vgg_model = models.vgg16_bn(pretrained=True) # Freeze model weights for param in vgg_model.parameters(): param.requires_grad = False # checking if GPU is available # if torch.cuda.is_available(): # model = vgg_model.cuda() # print(vgg_model) # Modify the last layer. number_features = vgg_model.classifier[6].in_features features = list(vgg_model.classifier.children())[:-1] # remove last layer features.extend([torch.nn.Linear(number_features, len(class_names))]) vgg_model.classifier = torch.nn.Sequential(*features)
shape_dict = {axis: shape[axes_dict[axis]] for axis in czi.axes} return axes_dict, shape_dict def build_index(axes, ix_select): idx = [ix_select.get(ax, 0) for ax in axes] return tuple(idx) def gram_matrix(x): n, c, h, w = x.size() x = x.view(n, c, -1) return (x @ x.transpose(1, 2)) / (c * h * w) vgg_m = vgg16_bn(True).features.cuda().eval() requires_grad(vgg_m, False) blocks = [ i - 1 for i, o in enumerate(children(vgg_m)) if isinstance(o, nn.MaxPool2d) ] class FeatureLoss(nn.Module): def __init__(self, m_feat, layer_ids, layer_wgts, base_loss=F.l1_loss): super().__init__() self.base_loss = base_loss self.m_feat = m_feat self.loss_features = [self.m_feat[i] for i in layer_ids] self.hooks = hook_outputs(self.loss_features, detach=False) self.wgts = layer_wgts
def main(): path = 'D://驾驶行为//imgs//train//' path1 = 'D://驾驶行为//imgs//test_set//' batch_size = 16 viz = visdom.Visdom() cifar_train = DataLoader(datasets.ImageFolder( path, transform=transforms.Compose([ transforms.Resize((224, 224)), transforms.RandomCrop((180, 180)), transforms.RandomRotation(20), transforms.ColorJitter(brightness=0.5, contrast=0.5, hue=0.5), transforms.RandomGrayscale(0.4), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ])), shuffle=True, batch_size=batch_size) val_train = DataLoader(datasets.ImageFolder( path, transform=transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ])), shuffle=False, batch_size=batch_size, num_workers=8) cifar_test = DataLoader(datasets.ImageFolder( path1, transform=transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ])), shuffle=False, batch_size=batch_size, num_workers=8) device = torch.device('cuda') trained_model = vgg16_bn(pretrained=True) trained_model.classifier = nn.Sequential( nn.Linear(in_features=25088, out_features=4096, bias=True), nn.ReLU(inplace=True), nn.Dropout(p=0.5, inplace=False), nn.Linear(in_features=4096, out_features=4096, bias=True), nn.ReLU(inplace=True), nn.Dropout(p=0.5, inplace=False), nn.Linear(in_features=4096, out_features=10, bias=True)) model = trained_model.to(device) print(model) criteon = nn.CrossEntropyLoss().to(device) optimizer = optim.Adam(model.parameters(), lr=0.00001, weight_decay=0.0005) best_acc, best_epoch = 0, 0 # model.load_state_dict(torch.load('best_checkpoint_transfered_vgg16_L2-77.model')) global_step = 0 for epoch in range(20): if (epoch + 1) % 5 == 0: for p in optimizer.param_groups: p['lr'] *= 0.1 model.train() total_loss = 0 for batchidx, (x, label) in enumerate(cifar_train): # [b, 3, 32, 32] # [b] x, lable = x.to(device), label.to(device) logits = model(x) # logits: [b, 10] # label: [b] # loss: tensor scalar loss = criteon(logits, lable) total_loss += loss.item() # backprop optimizer.zero_grad() loss.backward() optimizer.step() viz.line([loss.item()], [global_step], win='loss', update='append') global_step += 1 print(epoch, 'loss:', total_loss / len(cifar_train)) model.eval() with torch.no_grad(): # test total_correct = 0 total_num = 0 for x, label in val_train: # [b, 3, 32, 32] # [b] x, label = x.to(device), label.to(device) # [b, 10] logits = model(x) # [b] pred = logits.argmax(dim=1) # [b] vs [b] => scalar tensor correct = torch.eq(pred, label).float().sum().item() total_correct += correct total_num += x.size(0) # print(correct) acc = total_correct / total_num print("train acc :", acc) model.eval() with torch.no_grad(): # test total_correct = 0 total_num = 0 for x, label in cifar_test: # [b, 3, 32, 32] # [b] x, label = x.to(device), label.to(device) # [b, 10] logits = model(x) # [b] pred = logits.argmax(dim=1) # [b] vs [b] => scalar tensor correct = torch.eq(pred, label).float().sum().item() total_correct += correct total_num += x.size(0) # print(correct) acc = total_correct / total_num if acc > best_acc: best_epoch = epoch best_acc = acc if epoch == 0: continue torch.save( model.state_dict(), 'temp/best_checkpoint_transfered_vgg16_bn-epoch-' + str(epoch) + '-.model') print(epoch, 'test acc:', acc) print('best epoch', best_epoch, 'best acc', best_acc)
def main(): args = get_arguments() # configuration CONFIG = Dict(yaml.safe_load(open(args.config))) # writer if CONFIG.writer_flag: writer = SummaryWriter(CONFIG.result_path) else: writer = None """ DataLoader """ train_data = PartAffordanceDataset(CONFIG.labeled_data, config=CONFIG, transform=transforms.Compose([ CenterCrop(CONFIG), ToTensor(), Normalize() ])) test_data = PartAffordanceDataset(CONFIG.test_data, config=CONFIG, transform=transforms.Compose([ CenterCrop(CONFIG), ToTensor(), Normalize() ])) train_loader = DataLoader(train_data, batch_size=CONFIG.batch_size, shuffle=True, num_workers=CONFIG.num_workers) test_loader = DataLoader(test_data, batch_size=CONFIG.batch_size, shuffle=False, num_workers=CONFIG.num_workers) model = models.vgg16_bn(pretrained=True) for param in model.features.parameters(): param.requires_grad = False model.classifier[6] = nn.Linear(in_features=4096, out_features=7, bias=True) model.to(args.device) """ optimizer, criterion """ optimizer = optim.Adam(model.classifier.parameters(), lr=CONFIG.learning_rate) criterion = nn.BCEWithLogitsLoss() losses_train = [] losses_val = [] class_accuracy_val = [] accuracy_val = [] best_accuracy = 0.0 for epoch in tqdm.tqdm(range(CONFIG.max_epoch)): poly_lr_scheduler(optimizer, CONFIG.learning_rate, epoch, max_iter=CONFIG.max_epoch, power=CONFIG.poly_power) epoch_loss = 0.0 for sample in train_loader: loss_train = full_train(model, sample, criterion, optimizer, args.device) epoch_loss += loss_train losses_train.append(epoch_loss / len(train_loader)) # validation loss_val, class_accuracy, accuracy = eval_model( model, test_loader, criterion, CONFIG, args.device) losses_val.append(loss_val) class_accuracy_val.append(class_accuracy) accuracy_val.append(accuracy) if best_accuracy < accuracy_val[-1]: best_accuracy = accuracy_val[-1] torch.save(model.state_dict(), CONFIG.result_path + '/best_accuracy_model.prm') if epoch % 50 == 0 and epoch != 0: torch.save( model.state_dict(), CONFIG.result_path + '/epoch_{}_model.prm'.format(epoch)) if writer is not None: writer.add_scalars("loss", { 'loss_train': losses_train[-1], 'loss_val': losses_val[-1] }, epoch) writer.add_scalar("accuracy", accuracy_val[-1], epoch) writer.add_scalars( "class_accuracy", { 'accuracy of class 1': class_accuracy_val[-1][0], 'accuracy of class 2': class_accuracy_val[-1][1], 'accuracy of class 3': class_accuracy_val[-1][2], 'accuracy of class 4': class_accuracy_val[-1][3], 'accuracy of class 5': class_accuracy_val[-1][4], 'accuracy of class 6': class_accuracy_val[-1][5], 'accuracy of class 7': class_accuracy_val[-1][6], }, epoch) print( 'epoch: {}\tloss_train: {:.5f}\tloss_val: {:.5f}\taccuracy: {:.5f}' .format(epoch, losses_train[-1], losses_val[-1], accuracy_val[-1])) torch.save(model.state_dict(), CONFIG.result_path + '/final_model.prm')
def vgg16(pre): return children(vgg16_bn(pre))[0] @_fastai_model('Vgg-19 with batch norm added', 'Very Deep Convolutional Networks for Large-Scale Image Recognition',
def __init__(self): self.vgg16_bn = models.vgg16_bn(pretrained=True) self.vgg16_bn.classifier = None
def get_model(self): if self.args.architecture in ['resnet34', 'resnet50', 'resnet101']: if self.args.architecture == 'resnet34': model = torchmodels.resnet34(pretrained=True) if self.args.architecture == 'resnet50': model = torchmodels.resnet50(pretrained=True) if self.args.architecture == 'resnet101': model = torchmodels.resnet101(pretrained=True) num_classes = self.args.num_classes num_ftrs = model.fc.in_features model.fc = nn.Linear(num_ftrs, num_classes) if self.args.architecture in [ 'densenet121', 'densenet169', 'densenet201', 'densenet161' ]: if self.args.architecture == 'densenet121': model = torchmodels.densenet121(pretrained=True) if self.args.architecture == 'densenet169': model = torchmodels.densenet169(pretrained=True) if self.args.architecture == 'densenet201': model = torchmodels.densenet201(pretrained=True) if self.args.architecture == 'densenet161': model = torchmodels.densenet161(pretrained=True) num_classes = self.args.num_classes num_ftrs = model.classifier.in_features model.classifier = nn.Linear(num_ftrs, num_classes) if self.args.architecture in ['vgg11_ad', 'vgg11_bn_ad']: if self.args.architecture == 'vgg11_ad': model = torchmodels.vgg11(pretrained=True) if self.args.architecture in [ 'vgg11', 'vgg11_bn', 'vgg13', 'vgg13_bn', 'vgg16', 'vgg16_bn', 'vgg19_bn', 'vgg19' ]: if self.args.architecture == 'vgg11': model = torchmodels.vgg11(pretrained=True) if self.args.architecture == 'vgg11_bn': model = torchmodels.vgg11_bn(pretrained=True) if self.args.architecture == 'vgg13': model = torchmodels.vgg13(pretrained=True) if self.args.architecture == 'vgg13_bn': model = torchmodels.vgg13_bn(pretrained=True) if self.args.architecture == 'vgg16': model = torchmodels.vgg16(pretrained=True) if self.args.architecture == 'vgg16_bn': model = torchmodels.vgg16_bn(pretrained=True) if self.args.architecture == 'vgg19_bn': model = torchmodels.vgg19_bn(pretrained=True) if self.args.architecture == 'vgg19': model = torchmodels.vgg19(pretrained=True) num_classes = self.args.num_classes in_features = model.classifier[6].in_features n_module = nn.Linear(in_features, num_classes) n_classifier = list(model.classifier.children())[:-1] n_classifier.append(n_module) model.classifier = nn.Sequential(*n_classifier) if self.args.cuda: model.cuda() return model
num_workers=workers) loss_fuction_BCE = nn.BCELoss() loss_fuction_MSE = nn.MSELoss() gOptimizer = optim.Adam(generator.parameters(), lr=0.0002, betas=(0.5, 0.999)) dOptimizer = optim.Adam(discriminator.parameters(), lr=0.0002, betas=(0.5, 0.999)) errG = [] errD = [] image_loss_list = [] adversarial_loss_list = [] perception_loss_list = [] VGGnet = models.vgg16_bn(pretrained=True).features.to(device) VGGnet.eval() for param in VGGnet.parameters(): param.requires_grad = False #-------------------------------------# for epoch in range(EPOCHS): #for batch in range(0, len(dimageList), BATCH): for i, data in enumerate(dataloader): if i > 5: break highOriginal = data[0].to(device) lowOriginal = F.interpolate(highOriginal, size=(imagesizeLR, imagesizeLR), mode='nearest')
def __init__(self, conf): super(VGGEncBN, self).__init__() self.depth = conf.depth self.pretr = conf.pretr vgg16_model = models.vgg16_bn(pretrained=self.pretr) feat_list = list(vgg16_model.features) #self.conv_pre = nn.Conv2d(3,3,1,1,0,bias=True) if self.depth > 0: # [224x224] self.conv1_1 = feat_list[0] self.bnor1_1 = feat_list[1] self.relu1_1 = feat_list[2] self.conv1_2 = feat_list[3] self.bnor1_2 = feat_list[4] self.relu1_2 = feat_list[5] if self.depth > 1: # [112x112] self.maxp2_1 = feat_list[6] self.conv2_1 = feat_list[7] self.bnor2_1 = feat_list[8] self.relu2_1 = feat_list[9] self.conv2_2 = feat_list[10] self.bnor2_2 = feat_list[11] self.relu2_2 = feat_list[12] if self.depth > 2: # [56x56] self.maxp3_1 = feat_list[13] self.conv3_1 = feat_list[14] self.bnor3_1 = feat_list[15] self.relu3_1 = feat_list[16] self.conv3_2 = feat_list[17] self.bnor3_2 = feat_list[18] self.relu3_2 = feat_list[19] self.conv3_3 = feat_list[20] self.bnor3_3 = feat_list[21] self.relu3_3 = feat_list[22] if self.depth > 3: # [28x28] self.maxp4_1 = feat_list[23] self.conv4_1 = feat_list[24] self.bnor4_1 = feat_list[25] self.relu4_1 = feat_list[26] self.conv4_2 = feat_list[27] self.bnor4_2 = feat_list[28] self.relu4_2 = feat_list[29] self.conv4_3 = feat_list[30] self.bnor4_3 = feat_list[31] self.relu4_3 = feat_list[32] if self.depth > 4: # [14x14] self.maxp5_1 = feat_list[33] self.conv5_1 = feat_list[34] self.bnor5_1 = feat_list[35] self.relu5_1 = feat_list[36] self.conv5_2 = feat_list[37] self.bnor5_2 = feat_list[38] self.relu5_2 = feat_list[39] self.conv5_3 = feat_list[40] self.bnor5_3 = feat_list[41] self.relu5_3 = feat_list[42]
:return:a list containing frequency matrix for each conv layer """ frequency_matrix = [] for mod in net.modules(): if isinstance(mod, torch.nn.Conv2d): weight_matrix = conv_to_matrix(mod) frequency_matrix += [cv2.dct(weight_matrix.T)] return frequency_matrix if __name__ == "__main__": device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print("using: " + torch.cuda.get_device_name(torch.cuda.current_device()) + " of capacity: " + str(torch.cuda.get_device_capability(torch.cuda.current_device()))) net = models.vgg16_bn() net.load_state_dict(torch.load('../vgg16_bn-6c64b313.pth')) net.to(device) freq_matrix = conv_dct(net) def power_res(weight): """ calculate the magnitude of parameter :param weight: parameter itself :return: magnitude of parameter, that is lg(abs(weight))+10 """ if weight < 0: weight = -weight if weight > 1: return 10
torch.cuda.empty_cache() images_so_far += size if images_so_far >= num_images: break print(title[0]) vgg.train(mode=was_training) def eval_model(vgg, criterion): test_batches = len(dataloaders[TEST]) for i, data in enumerate(dataloaders[TEST]): vgg.train(False) vgg.eval() inputs, labels = data inputs, labels = Variable(inputs, requires_grad=True), Variable(labels, requires_grad=True) outputs = vgg(inputs) _, preds = torch.max(outputs.data, 1) del inputs, labels, outputs, preds torch.cuda.empty_cache() vgg16 = models.vgg16_bn() vgg16.load_state_dict(torch.load("vgg16_bn.pth")) for param in vgg16.features.parameters(): param.require_grad = False num_features = vgg16.classifier[6].in_features features = list(vgg16.classifier.children())[:-1] features.extend([nn.Linear(num_features, len(class_names))]) vgg16.classifier = nn.Sequential(*features) criterion = nn.CrossEntropyLoss() optimizer_ft = optim.SGD(vgg16.parameters(), lr=0.001, momentum=0.9) exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1) vgg16.load_state_dict(torch.load('VGG16_v2-OCT_Retina_half_dataset.pt', map_location={'cuda:0': 'cpu'})) eval_model(vgg16, criterion) visualize_model(vgg16, num_images=1)
t = data.valid_ds[0][1].data #데이터에서 검증데이터셋 - x:0인덱스 y인덱스[1] 고화질 데이터를 얻어옴 t = torch.stack([t,t]) # 배치사아지 2개짜리 배치를 가상적으로 만들었다 def gram_matrix(x): n,c,h,w = x.size() #n 데이터 개수 (배치), c채널, h세로, w가로 얻어옵니다 x = x.view(n, c, -1) # x.view --> reshape해줌 -1을 하면 h하고 w 곱한 값이 자동으로 들어감 return (x @ x.transpose(1,2))/(c*h*w) #1번 축하고 2번축을 transpose 하고 메트릭스 해줍니다 ---> 3채널 각각 값 9개의 값이 나옴 -->이런 값이 비슷하게 나오면 스타일을 비슷하게 생성할 수 있음 gram_matrix(t) #gram_matrix를 통해서 값을 얻음 base_loss = F.l1_loss #손실함수 뺀다음에 절대값 취한다음에 평균 구한 값 vgg_m = vgg16_bn(True).features.cuda().eval() #vgg16 사용해서 features값 컨블루션만 사용하겠다는 말 그래서 쿠다 gpu사용하고 eval()사용하겠다 requires_grad(vgg_m, False) #학습은 안시킬거니까 grad 사용할 필요없다 false blocks = [i-1 for i,o in enumerate(children(vgg_m)) if isinstance(o,nn.MaxPool2d)] #사이즈가 줄어드는 순간 직전에 MaxPool2d 잡아냄 blocks, [vgg_m[i] for i in blocks] #그거게 관련된 것들을 보여줌 class FeatureLoss(nn.Module): def __init__(self, m_feat, layer_ids, layer_wgts): super().__init__() self.m_feat = m_feat self.loss_features = [self.m_feat[i] for i in layer_ids] self.hooks = hook_outputs(self.loss_features, detach=False) self.wgts = layer_wgts self.metric_names = ['pixel', ] + [f'feat_{i}' for i in range(len(layer_ids)) ] + [f'gram_{i}' for i in range(len(layer_ids))]
def get_pretrained_model(parentdir, model_name, class_num, train_on_gpu, multi_gpu): """Retrieve a pre-trained model from torchvision Params ------- model_name (str): name of the model (currently only accepts vgg16 and resnet50) Return -------- model (PyTorch model): cnn """ if model_name == 'squeezenet1_0': # model = models.squeezenet1_0(pretrained=True) from squeezenet import squeezenet1_0 model = squeezenet1_0(pretrained=True) # class CustomLayer(nn.Module): # def __init__(self,layer_idx=None,in_channels=1,out_channels=1,kernel_size=1,sampling_factor=1,optimize=True): # super().__init__() # self.in_channels = in_channels # self.out_channels = out_channels # self.kernel_size = kernel_size # self.sampling_factor = sampling_factor # self.layer_idx = layer_idx # def forward(self,x): # x = x.squeeze() # return x model.classifier[-1] = nn.Sequential( nn.AdaptiveAvgPool2d(output_size=(1, 1)), CustomLayer(), nn.Linear(1000, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1)) # model = nn.Sequential(model, # nn.Sequential(nn.Linear(1000, 256), nn.ReLU(), nn.Dropout(0.2), # nn.Linear(256, class_num), nn.LogSoftmax(dim=1)) # ) elif model_name == 'vgg16': model = models.vgg16(pretrained=True) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False n_inputs = model.classifier[-1].in_features # Add on classifier model.classifier[-1] = nn.Sequential(nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1)) elif model_name == 'vgg16_bn': model = models.vgg16_bn(pretrained=True) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False n_inputs = model.classifier[-1].in_features # Add on classifier model.classifier[-1] = nn.Sequential(nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1)) elif model_name == 'vgg19': model = models.vgg19(pretrained=True) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False n_inputs = model.classifier[-1].in_features # Add on classifier model.classifier[-1] = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) elif model_name == 'vgg19_bn': model = models.vgg19_bn(pretrained=True) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False n_inputs = model.classifier[-1].in_features # Add on classifier model.classifier[-1] = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) elif model_name == 'resnet18': model = models.resnet18(pretrained=True) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.fc.in_features model.fc = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Softmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.Softmax(dim=1) ) elif model_name == 'resnet50': model = models.resnet50(pretrained=True) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.fc.in_features model.fc = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) elif model_name == 'resnet101': model = models.resnet101(pretrained=True) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.fc.in_features model.fc = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) elif model_name == 'resnet152': model = models.resnet152(pretrained=True) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.fc.in_features model.fc = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) elif model_name == 'inception_v3': from Inception_Networks import inception_v3 model = inception_v3(pretrained=True) # InceptionV3 = torch.load('models/inception_v3.pth') # model = InceptionV3['model'] # del InceptionV3 # import pretrainedmodels # import ssl # ssl._create_default_https_context = ssl._create_unverified_context # model = pretrainedmodels.__dict__['inceptionv3'](num_classes=1000, pretrained='imagenet') # ssl._create_default_https_context = ssl._create_stdlib_context # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.fc.in_features model.fc = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) # model = nn.Sequential(model, # nn.Sequential(nn.Linear(1000, 256), nn.ReLU(), nn.Dropout(0.2), # nn.Linear(256, class_num), nn.LogSoftmax(dim=1)) # ) elif model_name == 'inceptionresnetv2': from inceptionresnetv2 import inceptionresnetv2 model = inceptionresnetv2(parentdir, num_classes=1000, pretrained='imagenet') # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.last_linear.in_features model.last_linear = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) elif model_name == 'xception': from xception import xception model = xception(pretrained=True) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.fc.in_features model.fc = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) elif model_name == 'chexnet': from chexnet import chexnet model = chexnet(parentdir) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.module.densenet121.classifier[0].in_features model.module.densenet121.classifier = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) if model_name == 'nasnetalarge': import pretrainedmodels import ssl ssl._create_default_https_context = ssl._create_unverified_context model = pretrainedmodels.__dict__[model_name](num_classes=1000, pretrained='imagenet') ssl._create_default_https_context = ssl._create_stdlib_context # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.last_linear.in_features model.last_linear = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) if model_name == 'pnasnet5large': import pretrainedmodels import ssl ssl._create_default_https_context = ssl._create_unverified_context model = pretrainedmodels.__dict__[model_name](num_classes=1000, pretrained='imagenet') ssl._create_default_https_context = ssl._create_stdlib_context # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.last_linear.in_features model.last_linear = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) if model_name == 'densenet121': model = models.densenet121(pretrained=True) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.classifier.in_features model.classifier = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) if model_name == 'densenet201': model = models.densenet201(pretrained=True) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.classifier.in_features model.classifier = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) if model_name == 'shufflenet': model = models.shufflenet_v2_x1_0(pretrained=True) # model = torch.hub.load('pytorch/vision:v0.6.0', 'shufflenet_v2_x1_0', pretrained=True) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.fc.in_features model.fc = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) if model_name == 'googlenet': model = models.googlenet(pretrained=True) # model = torch.hub.load('pytorch/vision:v0.6.0', 'shufflenet_v2_x1_0', pretrained=True) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.fc.in_features model.fc = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) if model_name == 'mobilenet_v2': model = models.mobilenet_v2(pretrained=True) # model = torch.hub.load('pytorch/vision:v0.6.0', 'shufflenet_v2_x1_0', pretrained=True) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.classifier[-1].in_features model.classifier[-1] = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) if model_name == 'nasnetamobile': import pretrainedmodels import ssl ssl._create_default_https_context = ssl._create_unverified_context model = pretrainedmodels.__dict__[model_name](num_classes=1000, pretrained='imagenet') ssl._create_default_https_context = ssl._create_stdlib_context # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.last_linear.in_features model.last_linear = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) if model_name == 'alexnet': model = models.alexnet(pretrained=True) # model = torch.hub.load('pytorch/vision:v0.6.0', 'shufflenet_v2_x1_0', pretrained=True) # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.classifier[-1].in_features model.classifier[-1] = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) if model_name == 'darknet53': from darknet53 import darknet53 model = darknet53(1000) checkpoint = parentdir + 'models/darknet53.pth.tar' checkpoint = torch.load(checkpoint) model.load_state_dict(checkpoint['state_dict']) del checkpoint # # Freeze early layers # for param in model.parameters(): # param.requires_grad = False # Add on classifier n_inputs = model.fc.in_features model.fc = nn.Sequential( nn.Linear(n_inputs, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, class_num), nn.LogSoftmax(dim=1) # nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1) ) elif model_name == 'efficientnet_b0': from efficientnet_pytorch import EfficientNet model = EfficientNet.from_pretrained('efficientnet-b0') n_inputs = model._fc.in_features model._fc = nn.Sequential(nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1)) model._swish = Identity() torch.nn.init.xavier_uniform_(model._fc[0].weight) model._fc[0].bias.data.fill_(0.01) # model = nn.Sequential(*list(model.children())[:-1]) elif model_name == 'efficientnet_b1': from efficientnet_pytorch import EfficientNet model = EfficientNet.from_pretrained('efficientnet-b1') n_inputs = model._fc.in_features model._fc = nn.Sequential(nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1)) model._swish = Identity() torch.nn.init.xavier_uniform_(model._fc[0].weight) model._fc[0].bias.data.fill_(0.01) elif model_name == 'efficientnet_b2': from efficientnet_pytorch import EfficientNet model = EfficientNet.from_pretrained('efficientnet-b2') n_inputs = model._fc.in_features model._fc = nn.Sequential(nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1)) model._swish = Identity() torch.nn.init.xavier_uniform_(model._fc[0].weight) model._fc[0].bias.data.fill_(0.01) elif model_name == 'efficientnet_b3': from efficientnet_pytorch import EfficientNet model = EfficientNet.from_pretrained('efficientnet-b3') n_inputs = model._fc.in_features model._fc = nn.Sequential(nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1)) model._swish = Identity() torch.nn.init.xavier_uniform_(model._fc[0].weight) model._fc[0].bias.data.fill_(0.01) elif model_name == 'efficientnet_b4': from efficientnet_pytorch import EfficientNet model = EfficientNet.from_pretrained('efficientnet-b4') n_inputs = model._fc.in_features model._fc = nn.Sequential(nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1)) model._swish = Identity() torch.nn.init.xavier_uniform_(model._fc[0].weight) model._fc[0].bias.data.fill_(0.01) elif model_name == 'efficientnet_b5': from efficientnet_pytorch import EfficientNet model = EfficientNet.from_pretrained('efficientnet-b5') n_inputs = model._fc.in_features model._fc = nn.Sequential(nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1)) model._swish = Identity() torch.nn.init.xavier_uniform_(model._fc[0].weight) model._fc[0].bias.data.fill_(0.01) elif model_name == 'efficientnet_b6': from efficientnet_pytorch import EfficientNet model = EfficientNet.from_pretrained('efficientnet-b6') n_inputs = model._fc.in_features model._fc = nn.Sequential(nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1)) model._swish = Identity() torch.nn.init.xavier_uniform_(model._fc[0].weight) model._fc[0].bias.data.fill_(0.01) elif model_name == 'efficientnet_b7': from efficientnet_pytorch import EfficientNet model = EfficientNet.from_pretrained('efficientnet-b7') n_inputs = model._fc.in_features model._fc = nn.Sequential(nn.Linear(n_inputs, class_num), nn.LogSoftmax(dim=1)) model._swish = Identity() torch.nn.init.xavier_uniform_(model._fc[0].weight) model._fc[0].bias.data.fill_(0.01) # Move to gpu and parallelize if train_on_gpu: model = model.to('cuda') if multi_gpu: model = nn.DataParallel(model) return model
import torch import torchvision.models as models from torch import nn from collections import OrderedDict resnet18 = models.resnet18(pretrained=True) alexnet = models.alexnet(pretrained=True) vgg16 = models.vgg16_bn(pretrained=True) models_container = {'resnet': resnet18, 'alexnet': alexnet, 'vgg': vgg16} def get_pretrained_model(arch, hidden_units, output_size): #Load the pretrained model from pytorch model = models_container[arch] # Freeze training for all layers for param in model.features.parameters(): param.require_grad = False # Newly created modules have require_grad=True by default num_features = model.classifier[0].in_features classifier = nn.Sequential(OrderedDict([ ('fc1', nn.Linear(num_features, hidden_units)), ('relu1', nn.ReLU()), ('drop1', nn.Dropout()), ('fc2', nn.Linear(hidden_units, hidden_units)), ('relu2', nn.ReLU()), ('drop2', nn.Dropout()), ('fc3', nn.Linear(hidden_units, output_size)), ])) model.classifier = classifier
def load_neural_network(self): if self.network_name == "vgg16": self.net = models.vgg16_bn(pretrained=False) num_ftrs = self.net.classifier[6].in_features self.net.classifier[6] = nn.Linear( num_ftrs, self.settings["number_of_classes"]) self.net.load_state_dict( torch.load(os.path.join(self.models_path, "VGG16_best.pth"))) elif self.network_name == "densenet": self.net = models.densenet161(pretrained=False) num_ftrs = self.net.classifier.in_features self.net.classifier = nn.Linear(num_ftrs, self.settings["number_of_classes"]) self.net.load_state_dict( torch.load(os.path.join(self.models_path, 'DenseNet_best.pth'))) elif self.network_name == "xception": self.net = pretrainedmodels.xception() num_ftrs = self.net.last_linear.in_features self.net.last_linear = nn.Linear( num_ftrs, self.settings["number_of_classes"]) self.net.load_state_dict( torch.load(os.path.join(self.models_path, 'Xception_best.pth'))) elif self.network_name == "wide_resnet50": self.net = models.wide_resnet50_2(pretrained=True) num_ftrs = self.net.fc.in_features self.net.fc = nn.Linear(num_ftrs, self.settings["number_of_classes"]) self.net.load_state_dict( torch.load( os.path.join(self.models_path, 'ResNetWide_best.pth'))) elif self.network_name == "resnext": self.net = models.resnext50_32x4d(pretrained=True) num_ftrs = self.net.fc.in_features self.net.fc = nn.Linear(num_ftrs, self.settings["number_of_classes"]) self.net.load_state_dict( torch.load(os.path.join(self.models_path, 'ResNext_best.pth'))) elif self.network_name == "inceptionresnetv2": self.net = models.wide_resnet50_2(pretrained=True) num_ftrs = self.net.fc.in_features self.net.fc = nn.Linear(num_ftrs, self.settings["number_of_classes"]) self.net.load_state_dict( torch.load( os.path.join(self.models_path, 'InceptionResNetv2_best.pth'))) elif self.network_name == "inceptionv4": self.net = pretrainedmodels.inceptionv4() self.net.avg_pool = nn.AvgPool2d(kernel_size=2, stride=2) num_ftrs = self.net.last_linear.in_features self.net.last_linear = nn.Linear( num_ftrs, self.settings["number_of_classes"]) self.net.load_state_dict( torch.load( os.path.join(self.models_path, 'InceptionV4_best.pth'))) else: self.net = None print("unknown neural network architecture") self.net.eval()
def __init__(self): super(SegNet_ConvLSTM,self).__init__() self.vgg16_bn = models.vgg16_bn(pretrained=True).features self.relu = nn.ReLU(inplace=True) self.index_MaxPool = nn.MaxPool2d(kernel_size=2, stride=2,return_indices=True) self.index_UnPool = nn.MaxUnpool2d(kernel_size=2, stride=2) # net struct self.conv1_block = nn.Sequential(self.vgg16_bn[0], # conv2d(3,64,(3,3)) self.vgg16_bn[1], # bn(64,eps=1e-05,momentum=0.1,affine=True) self.vgg16_bn[2], # relu(in_place) self.vgg16_bn[3], # conv2d(3,64,(3,3)) self.vgg16_bn[4], # bn(64,eps=1e-05,momentum=0.1,affine=True) self.vgg16_bn[5] # relu(in_place) ) self.conv2_block = nn.Sequential(self.vgg16_bn[7], self.vgg16_bn[8], self.vgg16_bn[9], self.vgg16_bn[10], self.vgg16_bn[11], self.vgg16_bn[12] ) self.conv3_block = nn.Sequential(self.vgg16_bn[14], self.vgg16_bn[15], self.vgg16_bn[16], self.vgg16_bn[17], self.vgg16_bn[18], self.vgg16_bn[19], self.vgg16_bn[20], self.vgg16_bn[21], self.vgg16_bn[22] ) self.conv4_block = nn.Sequential(self.vgg16_bn[24], self.vgg16_bn[25], self.vgg16_bn[26], self.vgg16_bn[27], self.vgg16_bn[28], self.vgg16_bn[29], self.vgg16_bn[30], self.vgg16_bn[31], self.vgg16_bn[32] ) self.conv5_block = nn.Sequential(self.vgg16_bn[34], self.vgg16_bn[35], self.vgg16_bn[36], self.vgg16_bn[37], self.vgg16_bn[38], self.vgg16_bn[39], self.vgg16_bn[40], self.vgg16_bn[41], self.vgg16_bn[42] ) self.upconv5_block = nn.Sequential( nn.Conv2d(512, 512, kernel_size=(3, 3), padding=(1,1)), nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True), self.relu, nn.Conv2d(512, 512, kernel_size=(3, 3), padding=(1,1)), nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True), self.relu, nn.Conv2d(512, 512, kernel_size=(3, 3), padding=(1, 1)), nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True), self.relu, ) self.upconv4_block = nn.Sequential( nn.Conv2d(512, 512, kernel_size=(3, 3), padding=(1,1)), nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True), self.relu, nn.Conv2d(512, 512, kernel_size=(3, 3), padding=(1,1)), nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True), self.relu, nn.Conv2d(512, 256, kernel_size=(3, 3), padding=(1, 1)), nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True), self.relu, ) self.upconv3_block = nn.Sequential( nn.Conv2d(256, 256, kernel_size=(3, 3), padding=(1,1)), nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True), self.relu, nn.Conv2d(256, 256, kernel_size=(3, 3), padding=(1,1)), nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True), self.relu, nn.Conv2d(256, 128, kernel_size=(3, 3), padding=(1, 1)), nn.BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True), self.relu, ) self.upconv2_block = nn.Sequential( nn.Conv2d(128, 128, kernel_size=(3, 3), padding=(1,1)), nn.BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True), self.relu, nn.Conv2d(128, 64, kernel_size=(3, 3), padding=(1,1)), nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True), self.relu ) self.upconv1_block = nn.Sequential( nn.Conv2d(64, 64, kernel_size=(3, 3), padding=(1,1)), nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True), self.relu, nn.Conv2d(64, class_num, kernel_size=(3, 3), padding=(1,1)), ) self.convlstm = ConvLSTM(input_size=(4,8), input_dim=512, hidden_dim=[512, 512], kernel_size=(3,3), num_layers=2, batch_first=False, bias=True, return_all_layers=False)
def train_PalmLocNet(train_loader, test_x, test_y): if os.path.exists(args.MODELFOLDER + 'train_params_best.pth'): print('reload the last best model parameters') vgg = models.vgg16_bn(pretrained=False) pal = vggPalmLocNet(vgg) if torch.cuda.is_available(): pal.load_state_dict( {k.replace('module.', ''): v for k, v in torch.load(args.MODELFOLDER + 'train_params_best.pth').items()}) else: pal.load_state_dict( {k.replace('module.', ''): v for k, v in torch.load(args.MODELFOLDER + 'train_params_best.pth', map_location='cpu').items()}) # print(vgg) # print(pal) if torch.cuda.device_count() > 1: print('lets use', torch.cuda.device_count(), 'GPUs') palnet = nn.DataParallel(pal) else: palnet = pal palnet.to(device) else: print('It is the first time to train the model!') vgg = models.vgg16_bn(pretrained=False) if torch.cuda.is_available(): vgg.load_state_dict( {k.replace('module.', ''): v for k, v in torch.load(args.MODELFOLDER + 'vgg16_bn-6c64b313.pth').items()}) else: vgg.load_state_dict( {k.replace('module.', ''): v for k, v in torch.load(args.MODELFOLDER + 'vgg16_bn-6c64b313.pth', map_location='cpu').items()}) pal = vggPalmLocNet(vgg) if torch.cuda.device_count() > 1: print('lets use', torch.cuda.device_count(), 'GPUs') palnet = nn.DataParallel(pal) else: palnet = pal palnet.to(device) optimizer = torch.optim.Adam(palnet.parameters(), lr=args.LR) loss_func = Myloss() for epoch in range(args.EPOCH): for step, (x, y) in enumerate(train_loader): b_x = x.to(device) b_y = y.to(device) / 480 output = palnet(b_x) loss = loss_func(output, b_y) optimizer.zero_grad() # 将上一步梯度值清零 loss.backward() # 求此刻各参数的梯度值 optimizer.step() # 更新参数 if step % 100 == 0: palnet.eval() test_output = palnet(test_x) print('test_output[0]', test_output[0]) print('test_y[0]', test_y[0]) test_loss_func = Myloss() test_GIoU = test_loss_func.MyGIoU(test_output, test_y).sum() / (test_y.shape[0]) test_locMSEloss = ((test_output - test_y).pow(2).sum() / (4 * test_y.shape[0])) test_loss = test_loss_func(test_output, test_y) print('Epoch', epoch, '\n' 'train loss: %.4f' % loss.data.cpu().numpy(), '\n' 'test GIoU: %.4f' % test_GIoU, '\n' 'test locMSEloss: %.4f' % test_locMSEloss, '\n' 'total test loss: %.4f' % test_loss) palnet.train() # 检查是否有模型文件夹,没有就自行创建一个 if not os.path.isdir(args.MODELFOLDER): os.makedirs(args.MODELFOLDER) # 保存训练loss最小的模型参数(按周期记步) if epoch == 0: if os.path.exists(args.MODELFOLDER + 'train_params_best.pth'): print('exist the train_params_best.pth!') pass else: print('first make the train_params_best.pth') torch.save(palnet.state_dict(), args.MODELFOLDER + 'train_params_best.pth') best_loss = loss print('best_loss in epoch 0:', best_loss) else: if loss < best_loss: torch.save(palnet.state_dict(), args.MODELFOLDER + 'train_params_best.pth') print('save the best trained model in epoch', epoch) best_loss = loss print('new best_loss:', best_loss) else: print('no better in this epoch', epoch)
def train(local_rank,args): img_transforms = Compose([ CenterCrop((224, 224)), ToTensor(), Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)), ]) aud_transforms = nn.Sequential( Normalize(mean=[0], std=[12.0]) ) #classes = ['BoxingSpeedBag','PlayingCello', 'PlayingDaf'] classes = ['BlowDryHair', 'Typing', 'BandMarching', 'BoxingSpeedBag', 'PlayingCello', 'PlayingDaf', 'PlayingDhol','PlayingFlute', 'PlayingSitar', 'BrushingTeeth'] classes_num = len(classes) torch.cuda.set_device(local_rank) #### Set the dataset rank = args.nr * args.local_rank + local_rank print('rank',rank,'local_rank',local_rank) seed = rank+args.seed print('seed',seed) init_seeds(seed) train_dataset = UCF101Frame_Sound_Dataset(classes, args.train_path ,img_transform = img_transforms,aud_transform = aud_transforms) val_dataset = UCF101Frame_Sound_Dataset_test(classes, args.test_path,img_transform = img_transforms,aud_transform = aud_transforms) train_sampler = torch.utils.data.distributed.DistributedSampler( train_dataset, num_replicas=args.world_size, rank=rank, shuffle=True ) train_labels = train_dataset.all_labels print('true labels',Counter(train_labels)) ##generate noise label if args.y_noise_generate == True: result_path = 'results/'+args.model+'_SMP' + '/noise_type_{}_noise_percent{}_seed{}_epochs{}'.format(args.noise_type,args.noise_percent,args.seed,args.epoches) if args.noise_type == 'symmetry': corruption_matrix = uniform_mix_C(args.noise_percent,classes_num) elif args.noise_type == 'asymmetry_balance': corruption_matrix = flip_labels_C_balanced(args.noise_percent, classes_num,args.seed) elif args.noise_type == 'asymmetry': corruption_matrix = flip_labels_C(args.noise_percent, classes_num,args.seed) elif args.noise_type == 'asymmetry2': corruption_matrix = flip_labels_C_two(args.noise_percent, classes_num,args.seed) print('corruption_matrix',corruption_matrix) np.random.seed(args.seed) for i in range(len(train_labels)): train_dataset.all_labels[i] = np.random.choice(classes_num, p=corruption_matrix[train_labels[i]]) print('noise labels', Counter(train_dataset.all_labels)) else: print('No noise labels') result_path = 'results/'+args.model + '/NoNoise_seed{}_epochs{}'.format(args.seed,args.epoches) train_dataloader = DataLoaderX(train_dataset, batch_size=args.batch_size, shuffle=False, num_workers = 8,pin_memory=True, sampler=train_sampler) val_dataloader = DataLoaderX(val_dataset, batch_size=args.batch_size, shuffle=True, num_workers=8, pin_memory=True,sampler=None) ###set the distribution dist.init_process_group( backend='nccl', init_method='env://', world_size=args.world_size, rank=rank ) if args.model == 'L3-Net': model = L_3(classes_num).cuda() elif args.model == 'Vision-Subnet': model = ImageSubNet(classes_num).cuda() elif args.model == 'VGG16': model = models.vgg11_bn(pretrained=False).cuda() elif args.model == 'VGG16-Pretrained': model = models.vgg16_bn(pretrained=True).cuda() else: raise Exception('Please check the model name!!') model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model) #实现多卡BN model = nn.parallel.DistributedDataParallel(model, device_ids=[local_rank], output_device=local_rank) crossEntropy = nn.CrossEntropyLoss().cuda() #optim = Adam(model.parameters(), lr = 2.5e-6 , weight_decay = 1e-5,amsgrad=True) optim = SGD(model.parameters(), lr = args.lr, momentum=0.9) #每16个周期降低6% scheduler = lr_scheduler.StepLR(optim,16,0.94) scaler = torch.cuda.amp.GradScaler() total_batch = len(train_dataloader.dataset) / args.batch_size / args.world_size train_loss_sequence = [] train_acc_sequence = [] val_loss_sequence = [] val_acc_sequence = [] max_val_acc = 0 epochs_no_improve = 0 frame_level_pred, video_level_label = get_vedio_dict(classes,args.splite_path) if os.path.exists(result_path) is not True: os.makedirs(result_path+'/model/') os.makedirs(result_path + '/plot/') os.makedirs(result_path + '/log/') for epoch in range(args.epoches): if epoch >= args.SMP_start_epoch : #torch.cuda.empty_cache() model.train() all_class_combine_feature = get_all_class_combine_feature(local_rank,train_dataset, model, classes_num, args.m) prototypes_of_all_classe = get_prototypes_of_all_classe(all_class_combine_feature, classes_num, args.p) #torch.cuda.empty_cache() model.train() batch_loss_sequence = [] batch_acc_sequence = [] for batch_idx, (img, aud, label) in enumerate(train_dataloader): optim.zero_grad() img = img.float() img = img.cuda() label = label.cuda() with torch.cuda.amp.autocast(): if args.model_type == 'multimodal': aud = aud.cuda() out,img_f,aud_f = model(img, aud) elif args.model_type == 'Uni-Vision-modal': out = model(img) else: print('Please check the modal type!!') if epoch >= args.SMP_start_epoch: pseudo_label = get_pseudo_label(prototypes_of_all_classe,classes_num,img_f,aud_f,args.p) pseudo_label = torch.from_numpy(pseudo_label) pseudo_label = pseudo_label.cuda() alpha = soft_alpha(epoch) loss = (1 - alpha) * crossEntropy(out, label) + alpha * crossEntropy(out, pseudo_label) else: loss = crossEntropy(out, label) scaler.scale(loss).backward() scaler.step(optim) scaler.update() _, predict = out.max(1) correct = (predict.data == label.data).sum() * 1.0 batch_acc = correct / len(label) batch_loss_sequence.append(loss.cpu().detach().numpy()) batch_acc_sequence.append(batch_acc.cpu().detach().numpy()) if (batch_idx + 1) % 5 == 0: print(args.model+'# Epoch %3d: batch_idx/ total_batch %3d/%3d: train_loss: %.6f batch_acc: %.6f' % (epoch + 1, batch_idx, total_batch, loss,batch_acc)) scheduler.step() # adjust lr if (epoch + 1) % 1 == 0: if local_rank == 0: val_loss, val_acc = test(model,val_dataloader,frame_level_pred, video_level_label,args) train_loss_sequence.append(np.mean(batch_loss_sequence)) train_acc_sequence .append(np.mean(batch_acc_sequence)) val_loss_sequence.append(float(val_loss)) val_acc_sequence.append(float(val_acc)) if val_acc > max_val_acc: epochs_no_improve = 0 max_val_acc = val_acc #torch.save(model.state_dict(),result_path+'/model/best.model') else: epochs_no_improve += 1 if epochs_no_improve == args.n_epochs_stop: print('Early Stopping!') break print('# Epoch %3d: train_loss: %.6f val_loss: %.6f val_acc: %.6f best: %.6f' % (epoch + 1, loss, val_loss, val_acc, max_val_acc)) if rank == 0: print('train_loss_sequence',train_loss_sequence) np.save(result_path+'/log/train_loss', train_loss_sequence) print('train_acc_sequence', train_acc_sequence) np.save(result_path + '/log/train_acc', train_acc_sequence) print('val_loss_sequence', val_loss_sequence) np.save(result_path + '/log/val_loss', val_loss_sequence) print('val_acc_sequence', val_acc_sequence) np.save(result_path + '/log/val_acc', val_acc_sequence) plot_process(result_path + '/plot/train_val.jpg',list(range(len(train_loss_sequence))),train_loss_sequence,train_acc_sequence,val_loss_sequence,val_acc_sequence) sys.exit()
def train_cnn(MODEL_NAME, PRETRAINED, FREEZE, EPOCHS, BATCH_SIZE, N_LABELS, OPTIMIZERS, PATH_TO_IMAGES, LR, WEIGHT_DECAY, LR_DECAY_STEPS, DEBUG_MODE, CHECKPOINT_PATH='', DISTILLATE_WITH=''): """ Train torchvision model to NIH data given high level hyperparameters. Args: MODEL_NAME: model name PRETRAINED: if model pretrained FREEZE: model layer frozen or not EPOCHS: epochs iteration BATCH_SIZE: number of batch data per training N_LABELS: number of class labels OPTIMIZERS: optimizers used PATH_TO_IMAGES: path to NIH images LR: learning rate WEIGHT_DECAY: weight decay parameter for SGD LR_DECAY_STEPS: how many steps before LR decayed and dropped DEBUG_MODE: if true then no log will be created CHECKPOINT_PATH: load checkpoint path DISTILLATE_WITH: distillate the model with Returns: # preds: torchvision model predictions on test fold with ground truth for comparison # aucs: AUCs for each train,test tuple """ if not os.path.exists('results'): os.makedirs('results') # use imagenet mean,std for normalization mean = [0.485, 0.456, 0.406] std = [0.229, 0.224, 0.225] normalize = transforms.Normalize(mean=mean, std=std) # define torchvision transforms data_transforms = { 'train': transforms.Compose([ transforms.Resize(256), transforms.TenCrop(224), transforms.Lambda(lambda crops: torch.stack( [normalize(transforms.ToTensor()(crop)) for crop in crops])) ]), 'val': transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean, std) ]), 'test': transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean, std) ]) } # create train/val dataloaders transformed_datasets = { x: datasets.ImageFolder(os.path.join(PATH_TO_IMAGES, x), data_transforms[x]) for x in ['train', 'val', 'test'] } dataloaders = { x: torch.utils.data.DataLoader(transformed_datasets[x], batch_size=BATCH_SIZE, shuffle=True, num_workers=0) for x in ['train', 'val'] } # please do not attempt to train without GPU as will take excessively long if not use_gpu: raise ValueError("Error, requires GPU") # Check model used import modified_densenet import modified_alexnet model = ( models.densenet121( pretrained=PRETRAINED) if MODEL_NAME == 'densenet' else modified_densenet.densenet121(type=MODEL_NAME, pretrained=PRETRAINED) if MODEL_NAME == 'va-densenet' or MODEL_NAME == 'reva-densenet' or MODEL_NAME == 'fp-densenet' or MODEL_NAME == 'start-densenet' or MODEL_NAME == 'every-densenet' or MODEL_NAME == 'sedensenet' or MODEL_NAME == 'triplelossdensenet' else models.alexnet( pretrained=PRETRAINED) if MODEL_NAME == 'alexnet' else modified_alexnet.alexnet(type=MODEL_NAME, pretrained=PRETRAINED) if MODEL_NAME == 'va-alexnet' or MODEL_NAME == 'reva-alexnet' or MODEL_NAME == 'fp-alexnet' or MODEL_NAME == 'start-alexnet' else models.resnet152(pretrained=PRETRAINED) if MODEL_NAME == 'resnet' else models.vgg16( pretrained=PRETRAINED) if MODEL_NAME == 'VGG' else models.vgg16_bn( pretrained=PRETRAINED) if MODEL_NAME == 'VGG_Bn' else '') # get num_ftrs based on model name num_ftrs = (model.classifier.in_features if MODEL_NAME == 'densenet' or MODEL_NAME == 'va-densenet' or MODEL_NAME == 'reva-densenet' or MODEL_NAME == 'fp-densenet' or MODEL_NAME == 'start-densenet' or MODEL_NAME == 'every-densenet' or MODEL_NAME == 'sedensenet' or MODEL_NAME == 'triplelossdensenet' else model.classifier[6].in_features if MODEL_NAME == 'alexnet' or MODEL_NAME == 'va-alexnet' or MODEL_NAME == 'reva-alexnet' or MODEL_NAME == 'fp-alexnet' or MODEL_NAME == 'start-alexnet' or MODEL_NAME == 'VGG' or MODEL_NAME == 'VGG_Bn' else model.fc.in_features if MODEL_NAME == 'resnet' else model.fc3.in_features if MODEL_NAME == 'small_va' else '') # change classifier class to N_LABELS if (MODEL_NAME == 'densenet' or MODEL_NAME == 'va-densenet' or MODEL_NAME == 'reva-densenet' or MODEL_NAME == 'fp-densenet' or MODEL_NAME == 'start-densenet' or MODEL_NAME == 'every-densenet' or MODEL_NAME == 'sedensenet' or MODEL_NAME == 'triplelossdensenet'): model.classifier = nn.Linear(num_ftrs, N_LABELS) elif (MODEL_NAME == 'alexnet' or MODEL_NAME == 'va-alexnet' or MODEL_NAME == 'va-alexnet' or MODEL_NAME == 'reva-alexnet' or MODEL_NAME == 'fp-alexnet' or MODEL_NAME == 'start-alexnet' or MODEL_NAME == 'VGG' or MODEL_NAME == 'VGG_Bn'): model.classifier[6] = nn.Linear(num_ftrs, N_LABELS) elif (MODEL_NAME == 'resnet'): model.fc = nn.Linear(num_ftrs, N_LABELS) else: raise ValueError("Error model name") if CHECKPOINT_PATH != '': model = load_checkpoint(model, CHECKPOINT_PATH) # show params to learn if FREEZE: for name, param in model.named_parameters(): attention_pattern = re.compile( r'^(conv2d1x1|valinear|transconv|start|every|se_).+$') classifier_pattern = re.compile( r'^(classifier(?!\.\d)|classifier\.6|fc).+$') if attention_pattern.match(name): param.requires_grad = True elif classifier_pattern.match(name) and CHECKPOINT_PATH == '': param.requires_grad = True else: param.requires_grad = False if FREEZE: print('Params to learn:') for name, param in model.named_parameters(): if param.requires_grad == True: print(name) print('==================================') # Distillate distillate_time = '' if DISTILLATE_WITH != '': print(f'Distillate with {DISTILLATE_WITH}') distillate_time = datetime.datetime.now().isoformat() model_distillate = models.densenet121(pretrained=PRETRAINED) num_ftrs_distillate = model_distillate.classifier.in_features model_distillate.classifier = nn.Linear(num_ftrs_distillate, N_LABELS) model_distillate = load_checkpoint(model_distillate, DISTILLATE_WITH) print('Loaded checkpoint for distillation') model_distillate = model_distillate.cuda() loading_bar = '' dataloaders_length = len(dataloaders['train']) + len( dataloaders['val']) for i in range(dataloaders_length): loading_bar += '-' for phase in ['train', 'val']: for data in dataloaders[phase]: loading_bar = f'={loading_bar}' loading_bar = loading_bar[:dataloaders_length] print(f'Distillating: {loading_bar}', end='\r') inputs, labels = data if phase == 'train': for i in range(10): inp = inputs.clone()[:, i] inp = inp.cuda() labels = labels.cuda() outputs = model_distillate(inp).cpu().data.numpy() if len(outputs) != BATCH_SIZE: outputs_padding = np.zeros((BATCH_SIZE, N_LABELS)) outputs_padding[:outputs.shape[0], :outputs. shape[1]] = outputs outputs = outputs_padding if Path(f'results_distillation/d-{distillate_time}.npy' ).exists(): loaded_np = np.load( f'results_distillation/d-{distillate_time}.npy' ) outputs = np.append(loaded_np, [outputs], axis=0) else: outputs = [outputs] np.save( f'results_distillation/d-{distillate_time}.npy', outputs) else: inputs = inputs.cuda() labels = labels.cuda() outputs = model_distillate(inputs).cpu().data.numpy() if len(outputs) != BATCH_SIZE: outputs_padding = np.zeros((BATCH_SIZE, N_LABELS)) outputs_padding[:outputs.shape[0], :outputs. shape[1]] = outputs outputs = outputs_padding loaded_np = np.load( f'results_distillation/d-{distillate_time}.npy') outputs = np.append(loaded_np, [outputs], axis=0) np.save(f'results_distillation/d-{distillate_time}.npy', outputs) print('') # put model on GPU model = model.cuda() model.name = MODEL_NAME # define criterion, optimizer for training if distillate_time != '': criterion = nn.MSELoss() else: # class_weight_value = [ # 1 - (1 / 77), # 1 - (3 / 77), # 1 - (12 / 77), # 1 - (19 / 77), # 1 - (3 / 77), # 1 - (33 / 77), # 1 - (6 / 77) # ] # class_weight = torch.FloatTensor(class_weight_value).cuda() # criterion = nn.CrossEntropyLoss(weight=class_weight) criterion = nn.CrossEntropyLoss() # Check if SGD or Adam optimizer = (optim.SGD(model.parameters(), lr=LR, momentum=0.9) if OPTIMIZERS == 'SGD' else optim.Adam(model.parameters(), lr=LR) if OPTIMIZERS == 'Adam' else '') scheduler = lr_scheduler.StepLR(optimizer, step_size=LR_DECAY_STEPS, gamma=0.1) dataset_sizes = {x: len(transformed_datasets[x]) for x in ['train', 'val']} # train model model, best_epoch = train_model(model, criterion, optimizer, optim_name=OPTIMIZERS, LR=LR, num_epochs=EPOCHS, dataloaders=dataloaders, dataset_sizes=dataset_sizes, weight_decay=WEIGHT_DECAY, scheduler=scheduler, debug_mode=DEBUG_MODE, pretrained=PRETRAINED, freeze=FREEZE, checkpoint=CHECKPOINT_PATH, distillate_time=distillate_time) print("Finished Training")
def __init__(self, num_layers): self._net = models.vgg16_bn(pretrained=True).features[:num_layers] self._preprocessor = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ])
def vgg16(pre): return children(vgg16_bn(pre))[0]
def createModel(model, feature_extract): #Create a variable to store the NN NN = None #Initialize the network #Load in VGG11 if (model == 1): NN = models.vgg11(pretrained=True) set_parameter_requires_grad(NN, feature_extract) NN.classifier[6] = nn.Linear(4096, 196) #Load in VGG11_BN elif (model == 2): NN = models.vgg11_bn(pretrained=True) set_parameter_requires_grad(NN, feature_extract) NN.classifier[6] = nn.Linear(4096, 196) #Load in VGG16 elif (model == 3): NN = models.vgg16(pretrained=True) set_parameter_requires_grad(NN, feature_extract) NN.classifier[6] = nn.Linear(4096, 196) #Load in VGG16_BN elif (model == 4): NN = models.vgg16_bn(pretrained=True) set_parameter_requires_grad(NN, feature_extract) NN.classifier[6] = nn.Linear(4096, 196) #Load in Resnet18 elif (model == 5): NN = models.resnet18(pretrained=True) set_parameter_requires_grad(NN, feature_extract) NN.fc = nn.Linear(512, 196) #Load in Resnet50 elif (model == 6): NN = models.resnet50(pretrained=True) set_parameter_requires_grad(NN, feature_extract) NN.fc = nn.Linear(2048, 196) #Load in Resnet152 elif (model == 7): NN = models.resnet152(pretrained=True) set_parameter_requires_grad(NN, feature_extract) NN.fc = nn.Linear(2048, 196) #Load in DenseNet161 elif (model == 8): NN = models.densenet161(pretrained=True) set_parameter_requires_grad(NN, feature_extract) NN.fc = nn.Linear(1024, 196) #Load in ALexNet elif (model == 9): NN = models.alexnet(pretrained=True) set_parameter_requires_grad(NN, feature_extract) NN.classifier[6] = nn.Linear(4096, 196) else: print("Error! Model options are as follows:") print("1 -> VGG11") print("2 -> VGG11_BN") print("3 -> VGG16") print("4 -> VGG16_BN") print("5 -> Resnet18") print("6 -> Resnet50") print("7 -> Resnet152") print("8 -> Densenet161") print("9 -> Alexnet") exit() return NN
'shuffle': True, 'collate_fn': my_collate, 'num_workers': 6 } # Generators training_set = Dataset(partition['train'], labels) training_generator = data.DataLoader(training_set, **params2) validation_set = Dataset(partition['validation'], labels) validation_generator = data.DataLoader(validation_set, **params2) vgg_epochs = 3 # using the vgg16 pretrained model vgg16 = models.vgg16_bn(pretrained=True).cuda() # Freeze model weights -- don't freeze because it's pretrained on # natural images #for param in vgg16.parameters(): # param.requires_grad = False # 1. replace the first convolution layer with another one with 4 filters out_channels = vgg16.features[0].out_channels layers = list(vgg16.features.children())[1:] # Remove the first layer newlayer = nn.Conv2d(4, out_channels, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)) layers.insert(0, newlayer) # add the newlayer to the front
for param in resnet.parameters(): param.require_grad = False del resnet.fc # Remove last layer resnet.fc = nn.Sequential(nn.Linear(2048, 1024), nn.ReLU(), nn.Dropout(p=0.2), nn.Linear(1024, 1024), nn.ReLU(), nn.Dropout(p=0.2), nn.Linear(1024, 264)) # 264 classes resnet.fc resnet # resnet - head + top layer (sequential) """## VGG""" vgg = models.vgg16_bn(pretrained=True) # define model and weights print(vgg) # as shown by the output above, the fully connected layer (head/top) of VGG is called 'classifier' # in Resnet, the last layer is called 'fc' vgg.classifier num_features = vgg.classifier[6].in_features num_features """### Change top layer""" for param in vgg.features.parameters(): param.require_grad = False
def Model(self): if self.arch == 'alexnet': model = models.alexnet(num_classes=self.num_classes) if self.arch == 'vgg11': model = models.vgg11(num_classes=self.num_classes) if self.arch == 'vgg13': model = models.vgg13(num_classes=self.num_classes) if self.arch == 'vgg16': model = models.vgg16(num_classes=self.num_classes) if self.arch == 'vgg19': model = models.vgg19(num_classes=self.num_classes) if self.arch == 'vgg11_bn': model = models.vgg11_bn(num_classes=self.num_classes) if self.arch == 'vgg13_bn': model = models.vgg13_bn(num_classes=self.num_classes) if self.arch == 'vgg16_bn': model = models.vgg16_bn(num_classes=self.num_classes) if self.arch == 'vgg19_bn': model = models.vgg19_bn(num_classes=self.num_classes) if self.arch == 'resnet18': model = models.resnet18(num_classes=self.num_classes) if self.arch == 'resnet34': model = models.resnet34(num_classes=self.num_classes) if self.arch == 'resnet50': model = models.resnet50(num_classes=self.num_classes) if self.arch == 'resnet101': model = models.resnet101(num_classes=self.num_classes) if self.arch == 'resnet152': model = models.resnet152(num_classes=self.num_classes) if self.arch == 'squeezenet1_0': model = models.squeezenet1_0(num_classes=self.num_classes) if self.arch == 'squeezenet1_1': model = models.squeezenet1_1(num_classes=self.num_classes) if self.arch == 'densenet121': model = models.densenet121(num_classes=self.num_classes) if self.arch == 'densenet161': model = models.densenet161(num_classes=self.num_classes) if self.arch == 'densenet169': model = models.densenet169(num_classes=self.num_classes) if self.arch == 'densenet201': model = models.densenet201(num_classes=self.num_classes) if self.arch == 'inception_v1': # parameters 'aux_logits' maybe will make the model not work model = models.googlenet(num_classes=self.num_classes) if self.arch == 'inception_v3': # parameters 'aux_logits' maybe will make the model not work model = models.inception_v3(num_classes=self.num_classes) if self.arch == 'shufflenet_v2_x0_5': model = models.shufflenet_v2_x0_5(num_classes=self.num_classes) if self.arch == 'shufflenet_v2_x1_0': model = models.shufflenet_v2_x1_0(num_classes=self.num_classes) if self.arch == 'shufflenet_v2_x1_5': model = models.shufflenet_v2_x1_5(num_classes=self.num_classes) if self.arch == 'shufflenet_v2_x2_0': model = models.shufflenet_v2_x2_0(num_classes=self.num_classes) if self.arch == 'mobilenet_v2': model = models.mobilenet_v2(num_classes=self.num_classes) if self.arch == 'resnext50_32x4d': model = models.resnext50_32x4d(num_classes=self.num_classes) if self.arch == 'resnext101_32x4d': model = models.resnext101_32x4d(num_classes=self.num_classes) if self.arch == 'wide_resnet50_2': model = models.wide_resnet50_2(num_classes=self.num_classes) if self.arch == 'wide_resnet101_2': model = models.wide_resnet101_2(num_classes=self.num_classes) if self.arch == 'mnasnet1_0': model = models.mnasnet1_0(num_classes=self.num_classes) model = torch.nn.DataParallel(model, device_ids=self.gups).cuda() return model
class FCN(nn.Module): vgg16 = models.vgg16_bn(pretrained=True) def __init__(self, num_classes): super().__init__() # 特征提取的5个模块 self.stage1 = self.vgg16.features[:7] self.stage2 = self.vgg16.features[7:14] self.stage3 = self.vgg16.features[14:24] self.stage4 = self.vgg16.features[24:34] self.stage5 = self.vgg16.features[34:] # 两个改变通道数的过渡卷积 self.conv1 = nn.Conv2d(512, 256, 1) self.conv2 = nn.Conv2d(256, num_classes, 1) # 反卷积 self.upsample_2x_1 = nn.ConvTranspose2d(512, 512, 4, 2, 1, bias=False) self.upsample_2x_1.weight.data = bilinear_kernel(512, 512, 4) self.upsample_2x_2 = nn.ConvTranspose2d(256, 256, 4, 2, 1, bias=False) self.upsample_2x_2.weight.data = bilinear_kernel(256, 256, 4) self.upsample_8x = nn.ConvTranspose2d(num_classes, num_classes, 16, 8, 4, bias=False) self.upsample_8x.weight.data = bilinear_kernel(num_classes, num_classes, 16) pass def forward(self, x): ''' 卷积公式:output = ((input + 2 * padding - kernel) / stride) + 1 池化公式:output = ((input + 2 * padding - dilation * (kernel - 1) - 1) / stride) + 1 $$L_{out}=floor((L_{in} + 2padding - dilation(kernel_size - 1) - 1)/stride + 1$$ ''' # 特征提取 pool1 = self.stage1(x) # (bn, 3, 352, 480) -> (bn, 64, 176, 240) pool2 = self.stage2(pool1) # (bn, 64, 176, 240) -> (bn, 128,88, 120) pool3 = self.stage3(pool2) # (bn, 128, 88, 120) -> (bn, 256,44, 60) pool4 = self.stage4(pool3) # (bn, 256, 44, 60) -> (bn, 512,22, 30) pool5 = self.stage5(pool4) # (bn, 512, 22, 30) -> (bn, 512,11, 15) # pool5 2x上采样得到add1 pool5_2x = self.upsample_2x_1( pool5) # (bn, 512,11, 15) -> (bn, 512,22, 30) add1 = pool4 + pool5_2x # (bn, 512,22, 30) -> (bn, 512,22, 30) # add1 2x上采样得到add2 add1 = self.conv1(add1) # (bn, 512, 22, 30) -> (bn, 256,22, 30) add1_2x = self.upsample_2x_2( add1) # (bn, 256,22, 30) -> (bn, 256, 44, 60) add2 = pool3 + add1_2x # (bn, 256,44, 60) -> (bn, 256,44, 60) # add2通过过度卷积改变通道数后 8x上采样得到分数图 add2 = self.conv2(add2) # (bn, 256,44, 60) -> (bn, 12,44, 60) y = self.upsample_8x(add2) # (bn, 12,44, 60) -> (bn, 12,352, 480) return y
def __init__(self, pretrain=True): super(Encoder, self).__init__() self.vgg_backbone = nn.Sequential( *list(models.vgg16_bn(pretrained=pretrain).children())[:-1])
fname = annotation[0] image_names.append(fname) num_bboxes = (len(annotation) - 1) // 5 for b in range(num_bboxes): x1 = int(annotation[5 * b + 1]) y1 = int(annotation[5 * b + 2]) x2 = int(annotation[5 * b + 3]) y2 = int(annotation[5 * b + 4]) cls_label = int(annotation[5 * b + 5]) cls_name = cls_names[cls_label] targets[(fname, cls_name)].append([x1, y1, x2, y2]) feature_extractor = vgg16_bn().features yolo_model = YOLO_v1(nms_thresh=0.5, prob_thresh=0.1, _model='vgg', _device='cuda').cuda() yolo_model.load_state_dict(torch.load('./yolo_v1.ptr')) yolo_model.eval() for fname in tqdm(image_names): path = os.path.join(image_dir, fname) image = cv2.imread(path) boxes, class_names, probs = yolo_model.detect(image) plot_img = visualize_boxes(image, boxes, class_names, probs) saveD = os.path.join('./debug', 'test')
def __init__(self): super(CNNEncoder, self).__init__() features = list(models.vgg16_bn(pretrained=False).features) self.layer1 = nn.Sequential(nn.Conv2d(4, 64, kernel_size=3, padding=1)) self.features = nn.ModuleList(features)[1:] # .eval()
for_testing=True,bn_normalize=True) else: target_images = get_target_images(target_path, name_to_id.keys(), for_testing=True,means=means) #test multiple trained nets for model_name in trained_model_names: print model_name # load net net = TDID() #load a previously trained model if use_batch_norm: vgg16_bn = models.vgg16_bn(pretrained=False) net.features = torch.nn.Sequential(*list(vgg16_bn.features.children())[:-1]) net.features.eval() elif use_torch_vgg: vgg16 = models.vgg16(pretrained=False) net.features = torch.nn.Sequential(*list(vgg16.features.children())[:-1]) network.load_net(trained_model_path + model_name+'.h5', net) print('load model successfully!') net.cuda() net.eval() # evaluation test_net(model_name, net, dataloader, name_to_id, target_images,chosen_ids, max_per_target=max_per_target, thresh=thresh, vis=vis,
if resume: print("loading weight from checkpoints/best.pth") net.load_state_dict(torch.load('checkpoints/best.pth')) else: print('loading pre-trained model ......') if use_resnet: # 对应自己网络结构的加载官方预训练模型参数 offiNet = officalModel.resnet50(pretrained=True) # 官方模型 new_state_dict = offiNet.state_dict() dd = net.state_dict() for k in new_state_dict.keys(): print(k) if k in dd.keys() and not k.startswith('fc'): dd[k] = new_state_dict[k] net.load_state_dict(dd) else: vgg = officalModel.vgg16_bn(pretrained=True) new_state_dict = vgg.state_dict() dd = net.state_dict() for k in new_state_dict.keys(): print(k) if k in dd.keys() and k.startswith('features'): print('yes') dd[k] = new_state_dict[k] net.load_state_dict(dd) if use_gpu: print('this computer has gpu %d and current is %s' % (torch.cuda.device_count(), torch.cuda.current_device())) net.cuda() # ---------------------损失函数---------------------