def __init__(self,model_path=None, normalize=True): super(ResNet50Fc, self).__init__() if model_path: if os.path.exists(model_path): self.model_resnet = models.resnet50(pretrained=False) self.model_resnet.load_state_dict(torch.load(model_path)) else: raise Exception('invalid model path!') else: self.model_resnet = models.resnet50(pretrained=True) if model_path or normalize: # pretrain model is used, use ImageNet normalization self.normalize = True self.register_buffer('mean', torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1)) self.register_buffer('std', torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1)) else: self.normalize = False model_resnet = self.model_resnet self.conv1 = model_resnet.conv1 self.bn1 = model_resnet.bn1 self.relu = model_resnet.relu self.maxpool = model_resnet.maxpool self.layer1 = model_resnet.layer1 self.layer2 = model_resnet.layer2 self.layer3 = model_resnet.layer3 self.layer4 = model_resnet.layer4 self.avgpool = model_resnet.avgpool self.__in_features = model_resnet.fc.in_features
def __init__(self, num_classes=1): super(DinkNet50, self).__init__() filters = [256, 512, 1024, 2048] resnet = models.resnet50(pretrained=True) self.firstconv = resnet.conv1 self.firstbn = resnet.bn1 self.firstrelu = resnet.relu self.firstmaxpool = resnet.maxpool self.encoder1 = resnet.layer1 self.encoder2 = resnet.layer2 self.encoder3 = resnet.layer3 self.encoder4 = resnet.layer4 self.dblock = Dblock_more_dilate(2048) self.decoder4 = DecoderBlock(filters[3], filters[2]) self.decoder3 = DecoderBlock(filters[2], filters[1]) self.decoder2 = DecoderBlock(filters[1], filters[0]) self.decoder1 = DecoderBlock(filters[0], filters[0]) self.finaldeconv1 = nn.ConvTranspose2d(filters[0], 32, 4, 2, 1) self.finalrelu1 = nonlinearity self.finalconv2 = nn.Conv2d(32, 32, 3, padding=1) self.finalrelu2 = nonlinearity self.finalconv3 = nn.Conv2d(32, num_classes, 3, padding=1)
def __init__(self, class_num ): super(ft_net_middle, self).__init__() model_ft = models.resnet50(pretrained=True) # avg pooling to global pooling model_ft.avgpool = nn.AdaptiveAvgPool2d((1,1)) self.model = model_ft self.classifier = ClassBlock(2048+1024, class_num)
def __init__(self, num_classes): super(IDE, self).__init__() resnet = resnet50(pretrained=True) self.backbone = nn.Sequential( resnet.conv1, resnet.bn1, resnet.relu, resnet.maxpool, resnet.layer1, resnet.layer2, resnet.layer3, resnet.layer4, nn.AdaptiveAvgPool2d((1, 1)), ) self.classifier = nn.Sequential( nn.Linear(2048, 512), nn.BatchNorm1d(512), nn.LeakyReLU(0.1), nn.Dropout(p=0.5), nn.Linear(512, num_classes) ) nn.init.kaiming_normal_(self.classifier[0].weight, mode='fan_out') nn.init.constant_(self.classifier[0].bias, 0.) nn.init.normal_(self.classifier[1].weight, mean=1., std=0.02) nn.init.constant_(self.classifier[1].bias, 0.) nn.init.normal_(self.classifier[4].weight, std=0.001) nn.init.constant_(self.classifier[4].bias, 0.)
def resnet50(num_classes=1000, pretrained='imagenet'): """Constructs a ResNet-50 model. """ model = models.resnet50(pretrained=False) if pretrained is not None: settings = pretrained_settings['resnet50'][pretrained] model = load_pretrained(model, num_classes, settings) return model
def __init__(self, descriptor_name): super(Net, self).__init__() # if descriptor_name == 'vgg16': # self.select = ['30'] # self.vgg16 = models.vgg16(pretrained=True) # self.sequence = [] # for name, layer in self.vgg16.features._modules.items(): # self.sequence += [layer] # for name, layer in self.vgg16.classifier._modules.items(): # self.sequence += [layer] # break # self.model = nn.Sequential(*self.sequence) if descriptor_name == 'vgg16': self.select = ['30'] self.vgg16 = models.vgg16(pretrained=True) self.sequence = [] for name, layer in self.vgg16.features._modules.items(): self.sequence += [layer] for name, layer in self.vgg16.classifier._modules.items(): if name == '6': break self.sequence += [layer] layer = nn.Linear(4096, 10) # init.xavier_normal(layer.weight.data, gain = 1) self.sequence += [layer] self.model = nn.Sequential(*self.sequence) elif descriptor_name == 'vgg19': self.select = ['36'] self.vgg19 = models.vgg19(pretrained=True) self.sequence = [] for name, layer in self.vgg19.features._modules.items(): self.sequence += [layer] for name, layer in self.vgg19.classifier._modules.items(): self.sequence += [layer] break self.model = nn.Sequential(*self.sequence) elif descriptor_name == 'resnet50': self.select = ['avgpool'] self.model = models.resnet50(pretrained=True) self.model.fc = nn.Linear(2048, 10) elif descriptor_name == 'resnet101': self.select = ['avgpool'] self.model = models.resnet101(pretrained=True) elif descriptor_name == 'resnet152': self.select = ['avgpool'] self.model = models.resnet152(pretrained=True) self.model.fc = nn.Linear(2048, 10)
def __init__(self): super(ResNet50Fc, self).__init__() model_resnet50 = models.resnet50(pretrained=True) self.conv1 = model_resnet50.conv1 self.bn1 = model_resnet50.bn1 self.relu = model_resnet50.relu self.maxpool = model_resnet50.maxpool self.layer1 = model_resnet50.layer1 self.layer2 = model_resnet50.layer2 self.layer3 = model_resnet50.layer3 self.layer4 = model_resnet50.layer4 self.avgpool = model_resnet50.avgpool self.__in_features = model_resnet50.fc.in_features
def __init__(self, class_num ): super(PCB, self).__init__() self.part = 6 # We cut the pool5 to 6 parts model_ft = models.resnet50(pretrained=True) self.model = model_ft self.avgpool = nn.AdaptiveAvgPool2d((self.part,1)) self.dropout = nn.Dropout(p=0.5) # remove the final downsample self.model.layer4[0].downsample[0].stride = (1,1) self.model.layer4[0].conv2.stride = (1,1) # define 6 classifiers for i in range(self.part): name = 'classifier'+str(i) setattr(self, name, ClassBlock(2048, class_num, True, False, 256))
def get_model(num_classes, model_type='resnet50'): if model_type == 'resnet50': model = resnet50(pretrained=True).cuda() model.fc = nn.Linear(model.fc.in_features, num_classes).cuda() elif model_type == 'resnet101': model = resnet101(pretrained=True).cuda() model.fc = nn.Linear(model.fc.in_features, num_classes).cuda() elif model_type == 'resnet152': model = resnet152(pretrained=True).cuda() model.fc = nn.Linear(model.fc.in_features, num_classes).cuda() elif model_type == 'densenet121': model = densenet121(pretrained=True).cuda() model.classifier = nn.Linear(model.classifier.in_features, num_classes).cuda() elif model_type == 'densenet161': model = densenet161(pretrained=True).cuda() model.classifier = nn.Linear(model.classifier.in_features, num_classes).cuda() elif model_type == 'densenet201': model = densenet201(pretrained=True).cuda() model.classifier = nn.Linear(model.classifier.in_features, num_classes).cuda() return model
def __init__(self, requires_grad=False, pretrained=True, num=18): super(resnet, self).__init__() if(num==18): self.net = models.resnet18(pretrained=pretrained) elif(num==34): self.net = models.resnet34(pretrained=pretrained) elif(num==50): self.net = models.resnet50(pretrained=pretrained) elif(num==101): self.net = models.resnet101(pretrained=pretrained) elif(num==152): self.net = models.resnet152(pretrained=pretrained) self.N_slices = 5 self.conv1 = self.net.conv1 self.bn1 = self.net.bn1 self.relu = self.net.relu self.maxpool = self.net.maxpool self.layer1 = self.net.layer1 self.layer2 = self.net.layer2 self.layer3 = self.net.layer3 self.layer4 = self.net.layer4
# Importing libraries import torch import matplotlib.pyplot as plt import torchvision.transforms as t import cv2 as cv import torchvision.models as models # Importing the module from extractor import Extractor device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # Loading the model resnet = models.resnet50() extractor = Extractor(resnet) extractor.activate() # Visualising the filters plt.figure(figsize=(35, 35)) for index, filter in enumerate(extractor.CNN_weights[0]): plt.subplot(8, 8, index + 1) plt.imshow(filter[0, :, :].detach(), cmap='gray') plt.axis('off') plt.show() # Filter Map img = cv.cvtColor(cv.imread('Featuremaps&Filters/img.png'), cv.COLOR_BGR2RGB) img = t.Compose([ t.ToPILImage(), t.Resize((128, 128)), # t.Grayscale(),
def main(): global opt, name, logger, model, criterion, SSIM_loss, start_time, mcs_num opt = parser.parse_args() print(opt) # Tag_BatchSize name = "%s_%d" % (opt.tag, opt.batchSize) mcs_num = "%s" % (opt.num_mcs) logger = SummaryWriter("runs/" + name) cuda = opt.cuda if cuda and not torch.cuda.is_available(): raise Exception("No GPU found, please run without --cuda") seed = 1334 torch.manual_seed(seed) if cuda: torch.cuda.manual_seed(seed) cudnn.benchmark = True print("==========> Loading datasets") train_dataset = DatasetFromFolder(opt.train, transform=Compose([ ToTensor() ])) indoor_test_dataset = TestFromFolder(opt.test, transform=Compose([ ToTensor() ])) training_data_loader = DataLoader(dataset=train_dataset, num_workers=opt.threads, batch_size=opt.batchSize, pin_memory=True, shuffle=True) testing_data_loader = DataLoader(dataset=indoor_test_dataset, num_workers=opt.threads, batch_size=1, pin_memory=True, shuffle=True) print("==========> Building model") backbone = models.resnet50(pretrained=True) model = ResNet50(backbone, num_classes=15) # criterion = EMDLoss() criterion =nn.L1Loss() # optionally resume from a checkpoint if opt.resume: if os.path.isfile(opt.resume): print("=> loading checkpoint '{}'".format(opt.resume)) checkpoint = torch.load(opt.resume) opt.start_epoch = checkpoint["epoch"] + 1 model.load_state_dict(checkpoint["state_dict"]) else: print("=> no checkpoint found at '{}'".format(opt.resume)) # optionally copy weights from a checkpoint if opt.pretrained: if os.path.isfile(opt.pretrained): print("=> loading model '{}'".format(opt.pretrained)) weights = torch.load(opt.pretrained) model.load_state_dict(weights['state_dict'].state_dict()) else: print("=> no model found at '{}'".format(opt.pretrained)) print("==========> Setting GPU") if cuda: model = model.cuda() criterion = criterion.cuda() else: model = model.cpu() criterion = criterion.cpu() print("==========> Setting Optimizer") optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=opt.lr) old = -1 print("==========> Training") for epoch in range(opt.start_epoch, opt.nEpochs + 1): start_time = time.time() val = test(testing_data_loader, epoch) train(training_data_loader, optimizer, epoch) if val >= old: save_checkpoint_val_best(model, epoch, name, mcs_num) old = val
from torch import optim from torch import nn from utils import data_loader from torchvision import models import torch train_loader = data_loader(True) cuda = torch.cuda.is_available() if cuda: resnet50 = models.resnet50(num_classes=61).cuda() else: resnet50 = models.resnet50(num_classes=61) criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(params=resnet50.parameters(), lr=0.01, weight_decay=1e-4) epochs = 60 average_loss_series = [] for epoch in range(epochs): running_loss = 0.0 for i, data in enumerate(train_loader): inputs, labels = data inputs = inputs.cuda() if cuda else inputs labels = labels.cuda() if cuda else labels optimizer.zero_grad() outputs = resnet50(inputs) loss = criterion(outputs, labels) loss.backward()
import torchvision.models as models import torchvision.transforms as transforms import numpy as np import torch.nn as nn from torch.autograd import Variable import torch as torch import copy from torch.autograd.gradcheck import zero_gradients import sys, os from skimage.io import imread, imsave from PIL import Image input_file = sys.argv[1] ouput_file = sys.argv[2] model = models.resnet50(pretrained=True) model.eval() is_cuda = torch.cuda.is_available() if is_cuda: print("Using GPU") model = model.cuda() else: print("Using CPU") pre_mean = [0.485, 0.456, 0.406] pre_std = [0.229, 0.224, 0.225] post_mean = np.array([-0.485, -0.456, -0.406]) post_std = np.array([1 / 0.229, 1 / 0.224, 1 / 0.225]) add_std = [0.229 / 255, 0.224 / 255, 0.225 / 255] preprocess = transforms.Compose(
def get_resnet(): resnet50 = models.resnet50(num_classes=1000, pretrained='imagenet').cuda() return resnet50
def __init__(self): super(Extractor,self).__init__() basenet = models.resnet50(pretrained=True) self.extractor = nn.Sequential(*list(basenet.children())[:-1]) self.feat_dim = 2048
def main(): parser = argparse.ArgumentParser( description='Compute TCGA features from SimCLR embedder') parser.add_argument('--num_classes', default=2, type=int, help='Number of output classes [2]') parser.add_argument('--batch_size', default=128, type=int, help='Batch size of dataloader [128]') parser.add_argument('--num_workers', default=4, type=int, help='Number of threads for datalodaer') parser.add_argument('--gpu_index', type=int, nargs='+', default=(0, ), help='GPU ID(s) [0]') parser.add_argument('--backbone', default='resnet18', type=str, help='Embedder backbone [resnet18]') parser.add_argument('--norm_layer', default='instance', type=str, help='Normalization layer [instance]') parser.add_argument( '--magnification', default='single', type=str, help= 'Magnification to compute features. Use `tree` for multiple magnifications. Use `high` if patches are cropped for multiple resolution and only process higher level, `low` for only processing lower level.' ) parser.add_argument('--weights', default=None, type=str, help='Folder of the pretrained weights, simclr/runs/*') parser.add_argument( '--weights_high', default=None, type=str, help= 'Folder of the pretrained weights of high magnification, FOLDER < `simclr/runs/[FOLDER]`' ) parser.add_argument( '--weights_low', default=None, type=str, help= 'Folder of the pretrained weights of low magnification, FOLDER <`simclr/runs/[FOLDER]`' ) parser.add_argument('--dataset', default='TCGA-lung-single', type=str, help='Dataset folder name [TCGA-lung-single]') args = parser.parse_args() gpu_ids = tuple(args.gpu_index) os.environ['CUDA_VISIBLE_DEVICES'] = ','.join(str(x) for x in gpu_ids) if args.norm_layer == 'instance': norm = nn.InstanceNorm2d pretrain = False elif args.norm_layer == 'batch': norm = nn.BatchNorm2d if args.weights == 'ImageNet': pretrain = True else: pretrain = False if args.backbone == 'resnet18': resnet = models.resnet18(pretrained=pretrain, norm_layer=norm) num_feats = 512 if args.backbone == 'resnet34': resnet = models.resnet34(pretrained=pretrain, norm_layer=norm) num_feats = 512 if args.backbone == 'resnet50': resnet = models.resnet50(pretrained=pretrain, norm_layer=norm) num_feats = 2048 if args.backbone == 'resnet101': resnet = models.resnet101(pretrained=pretrain, norm_layer=norm) num_feats = 2048 for param in resnet.parameters(): param.requires_grad = False resnet.fc = nn.Identity() if args.magnification == 'tree' and args.weights_high != None and args.weights_low != None: i_classifier_h = mil.IClassifier(resnet, num_feats, output_class=args.num_classes).cuda() i_classifier_l = mil.IClassifier(copy.deepcopy(resnet), num_feats, output_class=args.num_classes).cuda() if args.weights_high == 'ImageNet' or args.weights_low == 'ImageNet' or args.weights == 'ImageNet': if args.norm_layer == 'batch': print('Use ImageNet features.') else: raise ValueError( 'Please use batch normalization for ImageNet feature') else: weight_path = os.path.join('simclr', 'runs', args.weights_high, 'checkpoints', 'model.pth') state_dict_weights = torch.load(weight_path) for i in range(4): state_dict_weights.popitem() state_dict_init = i_classifier_h.state_dict() new_state_dict = OrderedDict() for (k, v), (k_0, v_0) in zip(state_dict_weights.items(), state_dict_init.items()): name = k_0 new_state_dict[name] = v i_classifier_h.load_state_dict(new_state_dict, strict=False) os.makedirs(os.path.join('embedder', args.dataset), exist_ok=True) torch.save( new_state_dict, os.path.join('embedder', args.dataset, 'embedder-high.pth')) weight_path = os.path.join('simclr', 'runs', args.weights_low, 'checkpoints', 'model.pth') state_dict_weights = torch.load(weight_path) for i in range(4): state_dict_weights.popitem() state_dict_init = i_classifier_l.state_dict() new_state_dict = OrderedDict() for (k, v), (k_0, v_0) in zip(state_dict_weights.items(), state_dict_init.items()): name = k_0 new_state_dict[name] = v i_classifier_l.load_state_dict(new_state_dict, strict=False) os.makedirs(os.path.join('embedder', args.dataset), exist_ok=True) torch.save( new_state_dict, os.path.join('embedder', args.dataset, 'embedder-low.pth')) print('Use pretrained features.') elif args.magnification == 'single' or args.magnification == 'high' or args.magnification == 'low': i_classifier = mil.IClassifier(resnet, num_feats, output_class=args.num_classes).cuda() if args.weights == 'ImageNet': if args.norm_layer == 'batch': print('Use ImageNet features.') else: print('Please use batch normalization for ImageNet feature') else: if args.weights is not None: weight_path = os.path.join('simclr', 'runs', args.weights, 'checkpoints', 'model.pth') else: weight_path = glob.glob('simclr/runs/*/checkpoints/*.pth')[-1] state_dict_weights = torch.load(weight_path) for i in range(4): state_dict_weights.popitem() state_dict_init = i_classifier.state_dict() new_state_dict = OrderedDict() for (k, v), (k_0, v_0) in zip(state_dict_weights.items(), state_dict_init.items()): name = k_0 new_state_dict[name] = v i_classifier.load_state_dict(new_state_dict, strict=False) os.makedirs(os.path.join('embedder', args.dataset), exist_ok=True) torch.save(new_state_dict, os.path.join('embedder', args.dataset, 'embedder.pth')) print('Use pretrained features.') if args.magnification == 'tree' or args.magnification == 'low' or args.magnification == 'high': bags_path = os.path.join('WSI', args.dataset, 'pyramid', '*', '*') else: bags_path = os.path.join('WSI', args.dataset, 'single', '*', '*') feats_path = os.path.join('datasets', args.dataset) os.makedirs(feats_path, exist_ok=True) bags_list = glob.glob(bags_path) if args.magnification == 'tree': compute_tree_feats(args, bags_list, i_classifier_l, i_classifier_h, feats_path, 'fusion') else: compute_feats(args, bags_list, i_classifier, feats_path, args.magnification) n_classes = glob.glob( os.path.join('datasets', args.dataset, '*' + os.path.sep)) n_classes = sorted(n_classes) all_df = [] for i, item in enumerate(n_classes): bag_csvs = glob.glob(os.path.join(item, '*.csv')) bag_df = pd.DataFrame(bag_csvs) bag_df['label'] = i bag_df.to_csv(os.path.join('datasets', args.dataset, item.split(os.path.sep)[2] + '.csv'), index=False) all_df.append(bag_df) bags_path = pd.concat(all_df, axis=0, ignore_index=True) bags_path = shuffle(bags_path) bags_path.to_csv(os.path.join('datasets', args.dataset, args.dataset + '.csv'), index=False)
import torch import torch.nn as nn import torchvision from torchvision import datasets, transforms import matplotlib.pyplot as plt import time import os from torchvision.models import resnet50 NUM_TARGET_CATEGORIES = 20 # Target categories number NUM_OF_LAYERS = 116 NUM_OF_TRAINING_LAYER = 10 NUM_OF_FROZEN_LAYER = NUM_OF_LAYERS - NUM_OF_TRAINING_LAYER basemodel = resnet50(pretrained=True, progress=True) FC_input = basemodel.fc.in_features basemodel.fc = nn.Linear(FC_input, NUM_TARGET_CATEGORIES) def resnetRetrain(n_retrain_layer): trainlayer = NUM_OF_LAYERS - n_retrain_layer basemodel = resnet50(pretrained=True, progress=True) FC_input = basemodel.fc.in_features basemodel.fc = nn.Linear(FC_input, NUM_TARGET_CATEGORIES) layer_counter = 0 for gchild in basemodel.children(): for child in gchild.children(): for child in child.children():
def __init__(self): super(Res50_sentiment, self).__init__() ResNet50 = models.resnet50(pretrained=True) modules = list(ResNet50.children())[:-1] self.backbone = nn.Sequential(*modules) self.fc1 = nn.Linear(2048, 3)
def initialize_model(model_name, num_classes, resume_from=None): # Initialize these variables which will be set in this if statement. Each of these # variables is model specific. # The model (nn.Module) to return model_ft = None # The input image is expected to be (input_size, input_size) input_size = 0 # You may NOT use pretrained models!! use_pretrained = False # By default, all parameters will be trained (useful when you're starting from scratch) # Within this function you can set .requires_grad = False for various parameters, if you # don't want to learn them class Flatten(torch.nn.Module): def forward(self, x): batch_size = x.shape[0] return x.view(batch_size, -1) if model_name == "resnet18": """ Resnet18 """ model_ft = models.resnet18(pretrained=use_pretrained) num_ftrs = model_ft.fc.in_features # dropout = nn.Dropout() # flatten = Flatten() # hist = AHist.HistPool(num_bins=10) # layers = list(model_ft.children()) # layers.insert(-1, hist) # del layers[-1] # del layers[-2] # model_ft = nn.Sequential(*layers) model_ft.fc = nn.Linear(num_ftrs, num_classes) print(model_ft.children()) input_size = 224 elif model_name == "resnet50": """ Resnet50 """ model_ft = models.resnet50(pretrained=use_pretrained) num_ftrs = model_ft.fc.in_features # dropout = nn.Dropout(p=.2) # flatten = Flatten() # layers = list(model_ft.children()) # layers.insert(-1, flatten) # layers.insert(-1, dropout) # del layers[-1] # model_ft = nn.Sequential(*layers) # print(list(model_ft.children())) model_ft.fc = nn.Linear(num_ftrs, num_classes) input_size = 224 elif model_name == "resnet152": """ Resnet152 """ model_ft = models.resnet152(pretrained=use_pretrained) num_ftrs = model_ft.fc.in_features dropout = nn.Dropout() flatten = Flatten() layers = list(model_ft.children()) layers.insert(-1, flatten) layers.insert(-1, dropout) del layers[-1] model_ft = nn.Sequential(*layers) # print(list(model_ft.children())) model_ft.fc = nn.Linear(num_ftrs, num_classes) input_size = 224 elif model_name == "alexnet": """ Alexnet """ model_ft = models.alexnet(pretrained=use_pretrained) num_ftrs = model_ft.classifier[6].in_features model_ft.classifier[6] = nn.Linear(num_ftrs, num_classes) input_size = 224 elif model_name == "vgg": """ VGG11_bn """ model_ft = models.vgg11_bn(pretrained=use_pretrained) num_ftrs = model_ft.classifier[6].in_features model_ft.classifier[6] = nn.Linear(num_ftrs, num_classes) input_size = 224 elif model_name == "squeezenet": """ Squeezenet """ model_ft = models.squeezenet1_0(pretrained=use_pretrained) model_ft.classifier[1] = nn.Conv2d(512, num_classes, kernel_size=(1, 1), stride=(1, 1)) model_ft.num_classes = num_classes input_size = 224 elif model_name == "densenet": """ Densenet """ model_ft = models.densenet121(pretrained=use_pretrained) num_ftrs = model_ft.classifier.in_features model_ft.classifier = nn.Linear(num_ftrs, num_classes) input_size = 224 else: raise Exception("Invalid model name!") if resume_from is not None: print("Loading weights from %s" % resume_from) x = torch.load(resume_from) model_ft.load_state_dict(x) return model_ft, input_size
# print(x.shape) x4 = self.layer4(x3) # print(x.shape) return x1, x2, x3, x4 def resnet50(pre_trained=False, model_weight_path='./path/resnet50.pth'): model = ResNet(Bottleneck, [3, 4, 6, 3]) if pre_trained: model.load_state_dict(torch.load(model_weight_path), strict=False) return model if __name__ == '__main__': net = resnet50(pre_trained=False) for param in net.layer1.parameters(): param.requires_grad = False for p in net.layer2.parameters( ): # 将fine-tuning 的参数的 requires_grad 设置为 True p.requires_grad = False for p in net.layer3.parameters( ): # 将fine-tuning 的参数的 requires_grad 设置为 True p.requires_grad = False for p in net.layer4.parameters( ): # 将fine-tuning 的参数的 requires_grad 设置为 True p.requires_grad = False net.cuda() for name, param in net.named_parameters(): if param.requires_grad: print(name)
def visualize_model(num_images=6): plt.ion() #交互模式 data_transforms = { 'train': transforms.Compose([ transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]), 'val': transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]), } data_dir = './hymenoptera_data' #构建数据集:trainset valset, 返回值是图片路径以及类别的索引值 #在image_datasets中有两个set:trainset、valset. 每一个set中有class_to_idx{'ants': 0, 'bees': 1},有类名classes['ants', 'bees'] #有imgs[('E:/PytorchLearning/T...013035.jpg', 0), ('E:/PytorchLearning/T...c608f9.jpg', 0), ('E:/PytorchLearning/T...d8afde.jpg', 0), ('E:/PytorchLearning/T...9d3250.jpg', 0), ('E:/PytorchLearning/T...26745d.jpg', 0), ('E:/PytorchLearning/T...56588f.jpg', 0), ('E:/PytorchLearning/T...ada201.jpg', 0), ('E:/PytorchLearning/T...92cdab.jpg', 0), ('E:/PytorchLearning/T...e80de1.jpg', 0), ('E:/PytorchLearning/T...0adea2.jpg', 0), ('E:/PytorchLearning/T...8c5eea.jpg', 0), ('E:/PytorchLearning/T...e2fb6d.jpg', 0), ('E:/PytorchLearning/T...aacea6.jpg', 0), ('E:/PytorchLearning/T...84f5a4.jpg', 0), ...] image_datasets = { x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x]) for x in ['train', 'val'] } #数据加载器:加载数据集然后进行处理:batch、shuffle、sample等 dataloaders = { x: torch.utils.data.DataLoader(image_datasets[x], batch_size=4, shuffle=True) for x in ['train', 'val'] } #数据集大小以及类名 dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']} class_names = image_datasets['train'].classes device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') #创建一个跟训练时一样的模型,把训练好的模型参数加载进来 model = models.resnet50() num_ftrs = model.fc.in_features model.fc = nn.Linear(num_ftrs, 2) model.to(device) model.load_state_dict(torch.load('./model_ft.pth', map_location='cpu')) #测试模型 model.eval() images_so_far = 0 fig = plt.figure() with torch.no_grad(): #实现不需要求梯度 for data in dataloaders['val']: inputs = data[0].to(device) labels = data[1].to(device) outputs = model(inputs) _, preds = torch.max(outputs, 1) if images_so_far > num_images: break for j in range(inputs.size()[0]): images_so_far += 1 if images_so_far > num_images: break ax = plt.subplot(num_images // 2, 2, images_so_far) ax.axis('off') ax.set_title('predicted: {}/label: {}'.format( class_names[preds[j]], class_names[labels[j]])) imshow(inputs.cpu().data[j]) #关闭交互模式 plt.ioff() #显示结果图,不再关闭 plt.show()
def __init__(self): self.num_classes = 1000 self.IMG_SIZE = 224 self.resnet_model = models.resnet50(pretrained=True).cuda() random.seed(0) #sample100 = random.sample(range(1000),100) with open('imagenet_class_index.json') as f: label_data = json.load(f) labeltoclass = {} for i in xrange(1000): labeltoclass[label_data[str(i)][0]] = i ''' training_images = [] training_labels = [] for subdir, dirs, files in os.walk('/local/yuchi/train/'): for folder in dirs: if folder == "ILSVRC2012_img_train": continue for folder_subdir, folder_dirs, folder_files in os.walk(os.path.join(subdir, folder)): for file in folder_files: #if labeltoclass[folder] in sample100: training_images.append(os.path.join(folder_subdir, file)) training_labels.append(labeltoclass[folder]) self.training_images = training_images self.training_labels = training_labels ''' testing_images = [] testing_images_unsampled = [] testing_labels = [] testing_labels_unsampled = [] test_label_file = open("ILSVRC2012_validation_ground_truth.txt", "r") labels = test_label_file.readlines() mapping_file = open("ILSVRC2012_mapping.txt", "r") mapping = mapping_file.readlines() idtosynset = {} for m in mapping: temp = m.strip('\n').split(" ") idtosynset[int(temp[0])] = temp[1] for l in labels: testing_labels_unsampled.append(labeltoclass[idtosynset[int(l)]]) for subdir, dirs, files in os.walk( '/local/yuchi/dataset/ILSVRC2012/val/'): print(len(files)) for file in sorted(files): testing_images_unsampled.append(os.path.join(subdir, file)) #print(file) for x, y in zip(testing_labels_unsampled, testing_images_unsampled): #if x in sample100: testing_labels.append(x) testing_images.append(y) self.testing_images = testing_images self.testing_labels = testing_labels normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) self.train_transform = transforms.Compose([ transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize, ]) self.val_transform = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), normalize, ]) # define loss function (criterion) and pptimizer self.criterion = nn.CrossEntropyLoss().cuda() self.optimizer = torch.optim.SGD(self.resnet_model.parameters(), 0.1, momentum=0.9, weight_decay=1e-4)
# In[169]: ## TODO: Specify data loaders loaders_transfer = loaders_scratch.copy() # ### (IMPLEMENTATION) Model Architecture # # Use transfer learning to create a CNN to classify dog breed. Use the code cell below, and save your initialized model as the variable `model_transfer`. # In[170]: import torchvision.models as models import torch.nn as nn ## TODO: Specify model architecture model_transfer = models.resnet50(pretrained=True) for param in model_transfer.parameters(): param.requires_grad = False model_transfer.fc = nn.Linear(2048, 133, bias=True) fc_parameters = model_transfer.fc.parameters() for param in fc_parameters: param.requires_grad = True if use_cuda: model_transfer = model_transfer.cuda() # In[171]:
from torchvision import datasets, models, transforms import torch.nn as nn from torch.nn import functional as F import torch.optim as optim torch.__version__ #!pip install -q torch==0.4.1 torchvision import torchvision torchvision.__version__ # should be 0.2.1 input_path = "/content/gdrive/My Drive/APS360_Project/data" device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") device model = models.resnet50(pretrained=True).to(device) for param in model.parameters(): param.requires_grad = False model.fc = nn.Sequential( nn.Linear(2048, 32), #3208 nn.ReLU(inplace=True), nn.Linear(32, 2)).to(device) #nn.Linear(32, 2)) #Generate a name for the model consisting of all the hyperparameter values def get_model_name(size=320, batchSize=500, learningRate=0.0008, epoch=29): return "/content/gdrive/My Drive/APS360_Project/RainNet/Models/size{0}_bs{1}_lr{2}_epoch{3}".format(
def __init__(self, name, config): """ Initializes the ``LeNet5`` model, creates the required layers. :param name: Name of the model (taken from the configuration file). :param config: Parameters read from configuration file. :type config: ``ptp.configuration.ConfigInterface`` """ super(GenericImageEncoder, self).__init__(name, GenericImageEncoder, config) # Get key mappings. self.key_inputs = self.stream_keys["inputs"] self.key_outputs = self.stream_keys["outputs"] # Get operation modes. self.return_feature_maps = self.config["return_feature_maps"] pretrained = self.config["pretrained"] # Get model type from configuration. self.model_type = get_value_from_dictionary( "model_type", self.config, "vgg16 | densenet121 | resnet152 | resnet50".split(" | ")) if (self.model_type == 'vgg16'): # Get VGG16 self.model = models.vgg16(pretrained=pretrained) if self.return_feature_maps: # Use only the "feature encoder". self.model = self.model.features # Height of the returned features tensor (SET) self.feature_maps_height = 7 self.globals["feature_maps_height"] = self.feature_maps_height # Width of the returned features tensor (SET) self.feature_maps_width = 7 self.globals["feature_maps_width"] = self.feature_maps_width # Depth of the returned features tensor (SET) self.feature_maps_depth = 512 self.globals["feature_maps_depth"] = self.feature_maps_depth else: # Use the whole model, but cut/reshape only the last layer. self.output_size = self.globals["output_size"] # "Replace" the last layer. self.model.classifier._modules['6'] = torch.nn.Linear( 4096, self.output_size) elif (self.model_type == 'densenet121'): # Get densenet121 self.model = models.densenet121(pretrained=pretrained) if self.return_feature_maps: raise ConfigurationError( "'densenet121' doesn't support 'return_feature_maps' mode (yet)" ) # Use the whole model, but cut/reshape only the last layer. self.output_size = self.globals["output_size"] self.model.classifier = torch.nn.Linear(1024, self.output_size) elif (self.model_type == 'resnet152'): # Get resnet152 self.model = models.resnet152(pretrained=pretrained) if self.return_feature_maps: # Get all modules exluding last (avgpool) and (fc) modules = list(self.model.children())[:-2] self.model = torch.nn.Sequential(*modules) # Height of the returned features tensor (SET) self.feature_maps_height = 7 self.globals["feature_maps_height"] = self.feature_maps_height # Width of the returned features tensor (SET) self.feature_maps_width = 7 self.globals["feature_maps_width"] = self.feature_maps_width # Depth of the returned features tensor (SET) self.feature_maps_depth = 2048 self.globals["feature_maps_depth"] = self.feature_maps_depth else: # Use the whole model, but cut/reshape only the last layer. self.output_size = self.globals["output_size"] self.model.fc = torch.nn.Linear(2048, self.output_size) elif (self.model_type == 'resnet50'): # Get resnet50 self.model = models.resnet50(pretrained=pretrained) if self.return_feature_maps: # Get all modules exluding last (avgpool) and (fc) modules = list(self.model.children())[:-2] self.model = torch.nn.Sequential(*modules) # Height of the returned features tensor (SET) self.feature_maps_height = 7 self.globals["feature_maps_height"] = self.feature_maps_height # Width of the returned features tensor (SET) self.feature_maps_width = 7 self.globals["feature_maps_width"] = self.feature_maps_width # Depth of the returned features tensor (SET) self.feature_maps_depth = 2048 self.globals["feature_maps_depth"] = self.feature_maps_depth else: # Use the whole model, but cut/reshape only the last layer. self.output_size = self.globals["output_size"] self.model.fc = torch.nn.Linear(2048, self.output_size)
score = softmax(pred) pred_id = np.argmax(score) # 返回score数组中最大值的索引 stop = time.time() print('cost time', stop - start) print("预测结果:{:s}".format(labels[pred_id][0])) return labels[pred_id][0] if __name__ == "__main__": test_list = 'test.txt' # 预处理测试集(214个) test_data = Garbage_Loader(test_list, train_flag=False) test_loader = DataLoader(dataset=test_data, num_workers=2, pin_memory=True, batch_size=1) # 定义网络 model = models.resnet50(pretrained=False) fc_inputs = model.fc.in_features model.fc = nn.Sequential( nn.Dropout(0.2), nn.Linear(fc_inputs, 214) ) model = model.cuda() # 加载训练好的最佳模型 checkpoint = torch.load('model_best_checkpoint_resnet50.pth.tar') model.load_state_dict(checkpoint['state_dict']) # ------------------------------------ step 1/2 : 计算测试集的准确率 ------------------------------------ computeTestSetAccuracy(test_loader, model) # ------------------------------------ step 2/2 : 预测单个图像的结果 ------------------------------------ # predict(model, "垃圾图片库/有害垃圾_电池/img_电池_343.jpeg") # 打印模型的 state_dict
def main(): global best_loss data_dir = args.data_dir img_dir_train = os.path.join(data_dir, "img/train") img_dir_val = os.path.join(data_dir, "img/test") #use test data for validation txt_file_train = os.path.join(data_dir, "annot_train.txt") txt_file_val = os.path.join(data_dir, "annot_test.txt") # load pretrained resnet50 and modify last fully connected layer model = models.resnet50(pretrained = True) model.fc = FinalLayer() # model.fc = nn.Linear(2048, 12) # we need three different criterion for training criterion_protest = nn.BCELoss() criterion_violence = nn.MSELoss() criterion_visattr = nn.BCELoss() criterions = [criterion_protest, criterion_violence, criterion_visattr] if args.cuda and not torch.cuda.is_available(): raise Exception("No GPU Found") if args.cuda: model = model.cuda() criterions = [criterion.cuda() for criterion in criterions] optimizer = torch.optim.Adam( model.parameters(), args.lr, ) normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) train_dataset = ProtestDataset( txt_file = txt_file_train, img_dir = img_dir_train, transform = transforms.Compose([ transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize, ])) val_dataset = ProtestDataset( txt_file = txt_file_val, img_dir = img_dir_val, transform = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), normalize, ])) train_loader = DataLoader( train_dataset, num_workers = args.workers, batch_size = args.batch_size, shuffle = True ) val_loader = DataLoader( val_dataset, num_workers = args.workers, batch_size = args.batch_size) for epoch in range(args.epochs): # adjust_learning_rate(optimizer, epoch) train(train_loader, model, criterions, optimizer, epoch) loss = validate(val_loader, model, criterions, epoch) is_best = loss < best_loss if is_best: print('best model!!') best_loss = min(loss, best_loss) save_checkpoint({ 'epoch' : epoch + 1, 'state_dict' : model.state_dict(), 'best_loss' : best_loss, 'optimizer' : optimizer.state_dict(), }, is_best)
os.makedirs(output_prefix.parent, exist_ok=True) if not model_file.exists(): raise (RuntimeError("%s does not exist." % str(model_file))) # Create dataset dataset = ImageFolderDataset(image_folder, file_names, resize=256, crop=224) print('Image folder: %s' % args.image_folder) loader = data.DataLoader(dataset, batch_size=args.batch_size) print('Creating CNN instance.') cnn = resnet50(pretrained=False) resnet_dict = torch.hub.load_state_dict_from_url("", file_name=str(model_file), progress=True) cnn.load_state_dict(resnet_dict) # Remove final classifier layer del cnn.fc # Move to GPU and switch to evaluation mode cnn.cuda() cnn.train(False) # Create memmaped files res4f_feats = np.lib.format.open_memmap(str(output_prefix) + "-resnet50-res4frelu.npy",
from torchvision.models import resnet50, resnet34 import torch.nn.functional as F import torch.nn as nn from utils import local, Config if local: torch_model = resnet50(pretrained=Config['finetune']) else: torch_model = resnet34(pretrained=Config['finetune']) class Ruda_Model(nn.Module): def __init__(self): super(Ruda_Model, self).__init__() self.conv1_1 = nn.Conv2d(6, 64, 3, bias=False) self.conv1_2 = nn.Conv2d(64, 64, 3, bias=False) self.bn1_1 = nn.BatchNorm2d(64) self.bn1_2 = nn.BatchNorm2d(64) self.pool1 = nn.MaxPool2d(kernel_size=2, dilation=2) self.conv2_1 = nn.Conv2d(64, 128, 3, bias=False) self.conv2_2 = nn.Conv2d(128, 128, 3, bias=False) self.bn2_1 = nn.BatchNorm2d(128) self.bn2_2 = nn.BatchNorm2d(128) self.pool2 = nn.MaxPool2d(kernel_size=2, dilation=2) self.conv3_1 = nn.Conv2d(128, 256, 3, bias=False) self.conv3_2 = nn.Conv2d(256, 256, 3, bias=False) self.bn3_1 = nn.BatchNorm2d(256) self.bn3_2 = nn.BatchNorm2d(256)
def __init_with_imagenet(self, baseModel): model = resnet50(pretrained=True) del model.fc baseModel.copy_weight(model.state_dict())
def __init__(self): super(AppearanceEncoder, self).__init__() self.resnet = models.resnet50() self.resnet.load_state_dict(torch.load(resnet_checkpoint)) del self.resnet.fc
def __init__( self, pretrained: bool = True, resnet: int = 18, freeze: Union[str, int] = 6, head_layers: Optional[List[int]] = None, head_dropout: Optional[List[float]] = None, head_batchnorm: Optional[bool] = False, ): super(DeepImage, self).__init__() self.head_layers = head_layers if pretrained: if resnet == 18: vision_model = models.resnet18(pretrained=True) elif resnet == 34: vision_model = models.resnet34(pretrained=True) elif resnet == 50: vision_model = models.resnet50(pretrained=True) backbone_layers = list(vision_model.children())[:-1] if isinstance(freeze, str): frozen_layers = [] for layer in backbone_layers: for param in layer.parameters(): param.requires_grad = False frozen_layers.append(layer) self.backbone = nn.Sequential(*frozen_layers) if isinstance(freeze, int): assert ( freeze < 8 ), "freeze' must be less than 8 when using resnet architectures" frozen_layers = [] trainable_layers = backbone_layers[freeze:] for layer in backbone_layers[:freeze]: for param in layer.parameters(): param.requires_grad = False frozen_layers.append(layer) backbone_layers = frozen_layers + trainable_layers self.backbone = nn.Sequential(*backbone_layers) else: self.backbone = nn.Sequential( conv_layer(3, 64, 3), conv_layer(64, 128, 1, maxpool=False), conv_layer(128, 256, 1, maxpool=False), conv_layer(256, 512, 1, maxpool=False, adaptiveavgpool=True), ) # the output_dim attribute will be used as input_dim when "merging" the models self.output_dim = 512 if self.head_layers is not None: assert self.head_layers[0] == self.output_dim, ( "The output dimension from the backbone ({}) is not consistent with " "the expected input dimension ({}) of the fc-head".format( self.output_dim, self.head_layers[0] ) ) if not head_dropout: head_dropout = [0.0] * len(head_layers) self.imagehead = nn.Sequential() for i in range(1, len(head_layers)): self.imagehead.add_module( "dense_layer_{}".format(i - 1), dense_layer( head_layers[i - 1], head_layers[i], head_dropout[i - 1], head_batchnorm, ), ) self.output_dim = head_layers[-1]
self.feature_extractor = nn.Sequential( *list(base_model.children())[:-2]) tmp = OrderedDict() tmp['last_conv'] = nn.Conv2d(2048, nb_classes, 1, 1) tmp['gap'] = nn.AvgPool2d(kernel_size=7, stride=1, padding=0) self.classifier_layer = nn.Sequential(tmp) def forward(self, inputs): x = self.feature_extractor(inputs) x = self.classifier_layer(x) return x base_model = vmodels.resnet50(pretrained=True) net = Resnet_fc(base_model, 5) net.to(device) loss_function = nn.CrossEntropyLoss() opt = optim.SGD(net.parameters(), lr=0.001, momentum=0.9, weight_decay=0.001) def train_model(model, loss_function, optimizer, num_epochs=30): since = time.time() best_acc = 0.0 for epoch in range(num_epochs): print("Epoch {}/{}".format(epoch, num_epochs - 1)) print("-" * 10)
def create_res50(): model_ft = models.resnet50(pretrained=True) num_ftrs = model_ft.fc.in_features model_ft.fc = nn.Linear(num_ftrs, 3) model_ft = model_ft.cuda() return model_ft
def __init__(self, num_landmarks): super(ResNet50Int, self).__init__() self.num_landmarks = num_landmarks # Load pretrained resnet-50 model pretrained = models.resnet50(pretrained=True) # Remove last 2 layers modules = list(pretrained.children())[:-2] # load resnet with gcnet # self.encoder = resnet.resnet50(pretrained=True) # modules = nn.ModuleList([]) # Add 1st deconv block (stride = 16) modules.append( nn.Sequential( nn.ConvTranspose2d(2048, 256, kernel_size=4, stride=2, padding=1, bias=False), nn.BatchNorm2d(256), nn.ReLU(inplace=True))) # Add 2nd deconv block (stride = 32) modules.append( nn.Sequential( nn.ConvTranspose2d(256, 256, kernel_size=4, stride=2, padding=1, bias=False), nn.BatchNorm2d(256), nn.ReLU(inplace=True))) # Add 3rd deconv block (stride = 64) modules.append( nn.Sequential( nn.ConvTranspose2d(256, 256, kernel_size=4, stride=2, padding=1, bias=False), nn.BatchNorm2d(256), nn.ReLU(inplace=True))) # Add regression layer modules.append(nn.Conv2d(256, num_landmarks, 1)) self.module = nn.ModuleList(modules) # For integration self.relu = nn.ReLU(inplace=True) self.register_buffer('wx', torch.arange(64.0) * 4.0 + 2.0) self.wx = self.wx.reshape(1, 64).repeat(64, 1).reshape(64 * 64, 1) self.register_buffer('wy', torch.arange(64.0) * 4.0 + 2.0) self.wy = self.wy.reshape(64, 1).repeat(1, 64).reshape(64 * 64, 1) self.fliptest = False
def parse_feed_forward(args, input_shape): """Parses a sequential feed-forward neural network from json config.""" # parse known networks if isinstance(args, dict): if args["net"] == "resnet-cifar10": from modules.resnet_cifar import ResNet34 net = ResNet34(num_classes=10) output_shape = infer_shape([net], input_shape) print("output.shape:", output_shape) return net, output_shape if args["net"] == "resnet-cifar100": from modules.resnet_cifar import ResNet34 net = ResNet34(num_classes=100) output_shape = infer_shape([net], input_shape) print("output.shape:", output_shape) return net, output_shape if args["net"] == "resnet-clothing1M": from torchvision.models import resnet50 net = resnet50(num_classes=14) output_shape = infer_shape([net], input_shape) print("output.shape:", output_shape) return net, output_shape if args["net"] == "resnet34-clothing1M": from torchvision.models import resnet34 net = resnet34(num_classes=14) output_shape = infer_shape([net], input_shape) print("output.shape:", output_shape) return net, output_shape if args["net"] == "double-descent-cifar10-resnet18": from modules.resnet18_double_descent import make_resnet18k net = make_resnet18k(k=args["k"], num_classes=10) output_shape = infer_shape([net], input_shape) print("output.shape:", output_shape) return net, output_shape if args["net"] == "double-descent-cifar100-resnet18": from modules.resnet18_double_descent import make_resnet18k net = make_resnet18k(k=args["k"], num_classes=100) output_shape = infer_shape([net], input_shape) print("output.shape:", output_shape) return net, output_shape if args["net"] == "resnet34-imagenet": from torchvision.models import resnet34 net = resnet34(num_classes=1000) output_shape = infer_shape([net], input_shape) print("output.shape:", output_shape) return net, output_shape net = [] for cur_layer in args: layer_type = cur_layer["type"] prev_shape = infer_shape(net, input_shape) print(prev_shape) if layer_type == "fc": dim = cur_layer["dim"] assert len(prev_shape) == 2 net.append(nn.Linear(prev_shape[1], dim)) if cur_layer.get("batch_norm", False): net.append(nn.BatchNorm1d(dim)) add_activation(net, cur_layer.get("activation", "linear")) if "dropout" in cur_layer: net.append(nn.Dropout(cur_layer["dropout"])) if layer_type == "flatten": net.append(Flatten()) if layer_type == "reshape": net.append(Reshape(cur_layer["shape"])) if layer_type == "conv": assert len(prev_shape) == 4 net.append( nn.Conv2d( in_channels=prev_shape[1], out_channels=cur_layer["filters"], kernel_size=cur_layer["kernel_size"], stride=cur_layer["stride"], padding=cur_layer.get("padding", 0), ) ) if cur_layer.get("batch_norm", False): net.append(torch.nn.BatchNorm2d(num_features=cur_layer["filters"])) add_activation(net, cur_layer.get("activation", "linear")) if layer_type == "deconv": assert len(prev_shape) == 4 net.append( nn.ConvTranspose2d( in_channels=prev_shape[1], out_channels=cur_layer["filters"], kernel_size=cur_layer["kernel_size"], stride=cur_layer["stride"], padding=cur_layer.get("padding", 0), output_padding=cur_layer.get("output_padding", 0), ) ) if cur_layer.get("batch_norm", False): net.append(torch.nn.BatchNorm2d(num_features=cur_layer["filters"])) add_activation(net, cur_layer.get("activation", "linear")) if layer_type == "identity": net.append(Identity()) if layer_type == "upsampling": net.append( torch.nn.UpsamplingNearest2d(scale_factor=cur_layer["scale_factor"]) ) if layer_type == "gaussian": # this has to be the last layer net = nn.Sequential(*net) output_shape = infer_shape(net, input_shape) mu = nn.Sequential(nn.Linear(output_shape[1], cur_layer["dim"])) logvar = nn.Sequential(nn.Linear(output_shape[1], cur_layer["dim"])) output_shape = [None, cur_layer["dim"]] print("output.shape:", output_shape) return ConditionalGaussian(net, mu, logvar), output_shape if layer_type == "uniform": # this has to be the last layer net = nn.Sequential(*net) output_shape = infer_shape(net, input_shape) center = nn.Sequential(nn.Linear(output_shape[1], cur_layer["dim"])) radius = nn.Sequential(nn.Linear(output_shape[1], cur_layer["dim"])) output_shape = [None, cur_layer["dim"]] print("output.shape:", output_shape) return ConditionalUniform(net, center, radius), output_shape if layer_type == "dirac": # this has to be the last layer net = nn.Sequential(*net) output_shape = infer_shape(net, input_shape) print("output.shape:", output_shape) return ConditionalDiracDelta(net), output_shape output_shape = infer_shape(net, input_shape) print("output.shape:", output_shape) return nn.Sequential(*net), output_shape
def __init__(self, num_classes, pretrained=False): '''Creates an `PSPNet` instance for semantic segmentation. Args: num_classes: number of classes to predict. pretrained: use a pre-trained `ResNet` backbone for convolutional feature extraction. ''' super().__init__() # Backbone network we use to harvest convolutional image features from self.resnet = resnet50(pretrained=pretrained) # https://github.com/pytorch/vision/blob/c84aa9989f5256480487cafe280b521e50ddd113/torchvision/models/resnet.py#L101-L105 self.block0 = nn.Sequential(self.resnet.conv1, self.resnet.bn1, self.resnet.relu, self.resnet.maxpool) # https://github.com/pytorch/vision/blob/c84aa9989f5256480487cafe280b521e50ddd113/torchvision/models/resnet.py#L106-L109 self.block1 = self.resnet.layer1 self.block2 = self.resnet.layer2 self.block3 = self.resnet.layer3 self.block4 = self.resnet.layer4 # See https://arxiv.org/abs/1606.02147v1 section 4: Information-preserving dimensionality changes # # "When downsampling, the first 1x1 projection of the convolutional branch is performed with a stride of 2 # in both dimensions, which effectively discards 75% of the input. Increasing the filter size to 2x2 allows # to take the full input into consideration, and thus improves the information flow and accuracy." # # We can not change the kernel_size on the fly but we can change the stride instead from (2, 2) to (1, 1). assert self.block3[0].downsample[0].stride == (2, 2) assert self.block4[0].downsample[0].stride == (2, 2) self.block3[0].downsample[0].stride = (1, 1) self.block4[0].downsample[0].stride = (1, 1) # See https://arxiv.org/abs/1511.07122 and https://arxiv.org/abs/1706.05587 for dilated convolutions. # ResNets reduce spatial dimension too much for segmentation => patch in dilated convolutions. for name, module in self.block3.named_modules(): if 'conv2' in name: module.dilation = (2, 2) module.padding = (2, 2) module.stride = (1, 1) for name, module in self.block4.named_modules(): if 'conv2' in name: module.dilation = (4, 4) module.padding = (4, 4) module.stride = (1, 1) # PSPNet's pyramid: 2048 feature maps from conv net => pool into scales of {1, 2, 3, 6} self.pyramid1 = PyramidBlock(1, 2048, 2048 // 4) self.pyramid2 = PyramidBlock(2, 2048, 2048 // 4) self.pyramid3 = PyramidBlock(3, 2048, 2048 // 4) self.pyramid6 = PyramidBlock(6, 2048, 2048 // 4) # Pyramid pooling doubles feature maps via concatenation self.logits = nn.Sequential( nn.Conv2d(4096, 512, kernel_size=3, padding=1, bias=False), nn.BatchNorm2d(512), nn.ReLU(inplace=True), nn.Conv2d(512, num_classes, kernel_size=1)) self.initialize()
from os.path import join import torchvision, torch from torchvision.models import resnet50 from collections import OrderedDict ckptdir = r"E:\Cluster_Backup\torch" resnetF = resnet50(False, num_classes=8631) # Face recognition Network's output is 8631 classes in VGG-Face Dataset. #%% import pickle as pkl for pklname in ["resnet50_ft_weight.pkl", "resnet50_scratch_weight.pkl"]: data = pkl.load(open(join(ckptdir, pklname), mode="rb")) # state_dict = {k:torch.tensor(v) for k, v in data.items()} resnetF.load_state_dict(state_dict) torch.save(resnetF.state_dict(), join(ckptdir, pklname.split(".")[0]+".pt")) #%% ptname = "resnet50_scratch_weight.pt" #["resnet50_ft_weight.pkl", "resnet50_scratch_weight.pkl"] resnetF.load_state_dict(torch.load(join(ckptdir, ptname))) #%% from layer_hook_utils import get_module_names module_names, module_types, module_spec = get_module_names(resnetF, input_size=(3, 227, 227), device="cuda") #%% from grad_RF_estim import gradmap2RF_square, grad_RF_estimate resnetF.cuda().eval() for param in resnetF.parameters(): param.requires_grad_(False) unit_list = [("resnet50-face_scratch", ".ReLUrelu", 5, 57, 57), ("resnet50-face_scratch", ".layer1.Bottleneck1", 5, 28, 28), ("resnet50-face_scratch", ".layer2.Bottleneck0", 5, 14, 14), ("resnet50-face_scratch", ".layer2.Bottleneck2", 5, 14, 14), ("resnet50-face_scratch", ".layer3.Bottleneck0", 5, 7, 7), ("resnet50-face_scratch", ".layer3.Bottleneck2", 5, 7, 7),
def main(): #======================================= # 1. Load dataset #======================================= # CIFAR data data_tf = transforms.Compose([transforms.ToTensor(), transforms.Normalize([ 0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]) train_set = datasets.CIFAR10( '../../data', train=True, transform=data_tf, download=True) train_loader = torch.utils.data.DataLoader( train_set, batch_size=64, shuffle=True) test_set = datasets.CIFAR10( '../../data', train=False, transform=data_tf, download=True) valid_loader = torch.utils.data.DataLoader( test_set, batch_size=128, shuffle=False) # custom datasets # train_folders, valid_folders = get_folders( # config.train_data, config.valid_data) # train_datasets = MyDataSet(train_folders, transforms=None) # valid_datasets = MyDataSet(valid_folders, transforms=None, train=False) # train_loader = DataLoader(dataset=train_datasets, # batch_size=config.batch_size, shuffle=True) # valid_loader = DataLoader(dataset=valid_datasets, # batch_size=config.batch_size, shuffle=True) # print("Train numbers:{:d}".format(len(train_loader))) # print("Test numbers:{:d}".format(len(valid_loader))) #======================================= # 2. Define network and Load model #======================================= if config.pretrained: model = models.resnet50(num_classes=config.num_classes) checkpoint = torch.load(config.model_path) model.load_state_dict(checkpoint['model_state_dict']) print("load model success") else: model = models.resnet50(pretrained=True) # adjust last fc layer to class number channel_in = model.fc.in_features model.fc = nn.Linear(channel_in, config.num_classes) model.to(device) #======================================= # 3. Define Loss function and optimizer #======================================= criterion = nn.CrossEntropyLoss().to(device) optimizer = optim.Adam(model.parameters(), lr=config.lr, amsgrad=True, weight_decay=config.weight_decay) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1) #======================================= # 4. Train and Test the network #======================================= best_accuracy = 0. epoch = 0 resume = False # ====4.1 restart the training process==== if resume: checkpoint = torch.load(config.model_path) model.load_state_dict(checkpoint['model_state_dict']) optimizer.load_state_dict(checkpoint['optimizer_state_dict']) epoch = checkpoint["epoch"] loss = checkpoint["loss"] best_accuracy = checkpoint["best_accuracy"] # print checkpoint print('epoch:', epoch) print('best_accuracy:', best_accuracy) print("model's state_dict:") for param_tensor in model.state_dict(): print(param_tensor, '\t', model.state_dict()[param_tensor].size()) for epoch in range(1, config.epochs + 1): # ====4.2 start training==== print("======start training=====") model.train() torch.cuda.empty_cache() start = time.time() index = 1 sum_loss = 0. correct = 0. total = 0. for images, labels in train_loader: # clear the cuda cache torch.cuda.empty_cache() images = images.to(device) labels = labels.to(device, dtype=torch.long) # Forward pass outputs = model(images) loss = criterion(outputs, labels) # Backward and Optimize optimizer.zero_grad() loss.backward() optimizer.step() # print training loss and accuracy _, predicted = torch.max(outputs.data, 1) sum_loss += loss.item() total += labels.size(0) correct += (predicted == labels).sum() if index % 10 == 0: print("Iter: %d ===> Loss: %.8f | Acc: %.3f%%" % (index, sum_loss / index, 100. * correct / total)) index += 1 end = time.time() print("Epoch [%d/%d], Loss: %.8f, Time: %.1fsec!" % (epoch, config.epochs, loss.item(), (end - start))) vis.line(X=[epoch], Y=[loss.item()], win='loss', opts=dict(title='train loss'), update='append') # ====4.3 start evalidating==== model.eval() correct_prediction = 0. total = 0 with torch.no_grad(): for images, labels in valid_loader: # clear the cuda cache torch.cuda.empty_cache() images = images.to(device) labels = labels.to(device, dtype=torch.long) # print prediction outputs = model(images) _, predicted = torch.max(outputs.data, 1) # total labels number total += labels.size(0) # add correct correct_prediction += (predicted == labels).sum().item() # print("total correct number: ", correct_prediction) accuracy = 100. * correct_prediction / total print("Accuracy: %.4f%%" % accuracy) scheduler.step() if accuracy > best_accuracy: if not os.path.exists(config.checkpoint): os.mkdir(config.checkpoint) # save networks torch.save({ 'epoch': epoch, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict(), 'loss': loss, 'best_accuracy': accuracy, }, os.path.join("%s-%.3f.pth") % (config.best_models, accuracy)) print("save networks for epoch:", epoch) best_accuracy = accuracy
def resnet50(num_classes, pretrained): net = models.resnet50(pretrained) net.fc = nn.Linear(512, num_classes) return net
parser.add_argument('-r', '--saverate', default=5, help='The interval to save model states', type=int) parser.add_argument('-w', '--workers', default=4, help='Number of workers for batch processing', type=int) parser.add_argument('-n', '--notes', default="", help='Additional notes regarding the training') args = parser.parse_args() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") training_stats = dnn.read_stats(args.dataloc + "train/stats") val_stats = dnn.read_stats(args.dataloc + "val/stats") train_dataset = dnn.DroneDataset(root_dir=args.dataloc + "train/", transform=transforms.Compose([ dnn.Resize(300), dnn.Corrupt(0.2), dnn.FlipHzt(0.1), dnn.ContrastBrightness(3.0, 100, 0.1), dnn.Rotate(50.0, 0.1), dnn.RandomCrop(224), dnn.Normalize(training_stats), dnn.ToTensor()])) train_loader = DataLoader(train_dataset, batch_size=args.batch, shuffle=True, num_workers=args.workers) val_dataset = dnn.DroneDataset(root_dir=args.dataloc + "val/", transform=transforms.Compose([ dnn.Resize(224), dnn.Normalize(val_stats), dnn.ToTensor()])) val_loader = DataLoader(val_dataset, batch_size=args.batch, shuffle=True, num_workers=args.workers) network = models.resnet50(pretrained=True) #for param in network.parameters(): # param.requires_grad = False num_ftrs = network.fc.in_features network.fc = nn.Sequential(nn.Linear(num_ftrs, 1), nn.Sigmoid()) network = torch.nn.DataParallel(network).to(device) #optimizer = torch.optim.Adam(network.parameters(), lr=args.lr) optimizer = torch.optim.SGD(network.parameters(), lr=args.lr, momentum=args.momentum) #sched_optimizer = lr_scheduler.ReduceLROnPlateau(optimizer, verbose=True) criterion = nn.BCELoss() print("Model created")
style_dir = "./image/" data = dset.ImageFolder(style_dir,transform= transforms.Compose([ transforms.Scale(image_size), transforms.CenterCrop(image_size), transforms.ToTensor(), ])) print(data.class_to_idx) img_list = [] for i in data.imgs: img_list.append(i[0]) # import pretrained resnet-50 model resnet = models.resnet50(pretrained=True) class Resnet(nn.Module): def __init__(self): super(Resnet,self).__init__() self.layer0 = nn.Sequential(*list(resnet.children())[0:1]) self.layer1 = nn.Sequential(*list(resnet.children())[1:4]) self.layer2 = nn.Sequential(*list(resnet.children())[4:5]) self.layer3 = nn.Sequential(*list(resnet.children())[5:6]) #self.layer4 = nn.Sequential(*list(resnet.children())[6:7]) #self.layer5 = nn.Sequential(*list(resnet.children())[7:8]) def forward(self,x): out_0 = self.layer0(x) out_1 = self.layer1(out_0) out_2 = self.layer2(out_1)