def download_model(saving_path='.'): # inception net # model = models.Inception3() # model.load_state_dict(model_zoo.load_url(model_urls['inception_v3_google'], model_dir=saving_path, progress=True)) # resnet model = models.ResNet(_Bottleneck, [3, 8, 36, 3]) model.load_state_dict(model_zoo.load_url(model_urls['resnet152'], model_dir=saving_path, progress=True)) # save_model(model, 'resnet152.pkl', saving_path) # alex net model = models.AlexNet() model.load_state_dict(model_zoo.load_url(model_urls['alexnet'], model_dir=saving_path, progress=True)) # save_model(model, 'alexnet.pkl', saving_path) # vgg model = models.VGG(_vgg_make_layers(_vgg_cfg['E'], batch_norm=True), init_weights=False) model.load_state_dict(model_zoo.load_url(model_urls['vgg19_bn'], model_dir=saving_path, progress=True)) # save_model(model, 'vgg19.pkl', saving_path) # squeeze net model = models.SqueezeNet(version=1.1) model.load_state_dict(model_zoo.load_url(model_urls['squeezenet1_1'], model_dir=saving_path, progress=True)) # save_model(model, 'squeezenet1_1.pkl', saving_path) # dense net model = models.DenseNet(num_init_features=64, growth_rate=32, block_config=(6, 12, 48, 32)) pattern = re.compile( r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$') state_dict = model_zoo.load_url(model_urls['densenet201'], model_dir=saving_path, progress=True) for key in list(state_dict.keys()): res = pattern.match(key) if res: new_key = res.group(1) + res.group(2) state_dict[new_key] = state_dict[key] del state_dict[key] model.load_state_dict(state_dict) # save_model(model, 'densenet201.pkl', saving_path) # googlenet kwargs = dict() kwargs['transform_input'] = True kwargs['aux_logits'] = False # if kwargs['aux_logits']: # warnings.warn('auxiliary heads in the pretrained googlenet model are NOT pretrained, ' # 'so make sure to train them') original_aux_logits = kwargs['aux_logits'] kwargs['aux_logits'] = True kwargs['init_weights'] = False model = models.GoogLeNet(**kwargs) model.load_state_dict(model_zoo.load_url(model_urls['googlenet'])) if not original_aux_logits: model.aux_logits = False del model.aux1, model.aux2 # save_model(model, 'googlenet.pkl', saving_path) # resnext model = models.resnext101_32x8d(pretrained=False) model.load_state_dict(model_zoo.load_url(model_urls['resnext101_32x8d'], model_dir=saving_path, progress=True))
def test_alex_net(): alex = models.AlexNet() graph = wandb.wandb_torch.TorchGraph.hook_torch(alex) output = alex.forward(dummy_torch_tensor((2, 3, 224, 224))) grads = torch.ones(2, 1000) output.backward(grads) graph = wandb.Graph.transform(graph) assert len(graph["nodes"]) == 20 assert graph["nodes"][0][ 'class_name'] == "Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))" assert graph["nodes"][0]['name'] == "features.0"
def test_alex_net(): alex = models.AlexNet() graph = wandb.wandb_torch.TorchGraph.hook_torch(alex) output = alex.forward(dummy_torch_tensor((2, 3, 224, 224))) grads = torch.ones(2, 1000) output.backward(grads) graph = graph.to_json() # This was failing in CI with 21 nodes?!? print(graph["nodes"]) assert len(graph["nodes"]) >= 20 assert graph["nodes"][0]['class_name'] == "Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))" assert graph["nodes"][0]['name'] == "features.0"
def _init_modules(self): alex = models.AlexNet() resnet = models.resnet18() if self.pretrained: print("Loading pretrained weights from %s" % (self.model_path)) state_dict = torch.load(self.model_path) alex.load_state_dict({ k: v for k, v in state_dict.items() if k in alex.state_dict() }) state_dict = torch.load( 'data/pretrained_model/resnet18-5c106cde.pth') resnet.load_state_dict({ k: v for k, v in state_dict.items() if k in resnet.state_dict() }) # use last layer self.RCNN_base = nn.Sequential(*list(alex.features._modules.values())) #dont't use classifier alex.classifier = nn.Sequential( *list(alex.classifier._modules.values())[:-1]) # fix conv layers # Fix the layers before Fire5: for layer in range(6): for p in self.RCNN_base[layer].parameters(): p.requires_grad = False # top # self.RCNN_top = self.RCNN_top = resnet.layer4 #[1:] # not using the last maxpool layer self.RCNN_cls_score = nn.Linear(512, self.n_classes) if self.class_agnostic: self.RCNN_bbox_pred = nn.Linear(512, 4) else: self.RCNN_bbox_pred = nn.Linear(512, 4 * self.n_classes) def set_bn_fix(m): classname = m.__class__.__name__ if classname.find('BatchNorm') != -1: for p in m.parameters(): p.requires_grad = False self.RCNN_base.apply(set_bn_fix) self.RCNN_top.apply(set_bn_fix)
def load_model(model_name): global MODEL_NAME # Detect if we have a GPU available device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") if model_name == 'ResNet': model = models.resnet152(pretrained=False) model.load_state_dict(torch.load(os.path.join(PRETRAINED_DEEP_MODEL_DIR, MODEL_NAME[model_name]), map_location=device)) elif model_name == 'AlexNet': model = models.AlexNet() model.load_state_dict(torch.load(os.path.join(PRETRAINED_DEEP_MODEL_DIR, MODEL_NAME[model_name]), map_location=device)) elif model_name == 'VGG': model = models.VGG(_vgg_make_layers(_vgg_cfg['E'], batch_norm=True), init_weights=False) model.load_state_dict(torch.load(os.path.join(PRETRAINED_DEEP_MODEL_DIR, MODEL_NAME[model_name]), map_location=device)) elif model_name == 'DenseNet': model = models.DenseNet(num_init_features=64, growth_rate=32, block_config=(6, 12, 48, 32)) pattern = re.compile( r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$') state_dict = torch.load(os.path.join(PRETRAINED_DEEP_MODEL_DIR, MODEL_NAME[model_name]), map_location=device) for key in list(state_dict.keys()): res = pattern.match(key) if res: new_key = res.group(1) + res.group(2) state_dict[new_key] = state_dict[key] del state_dict[key] model.load_state_dict(state_dict) elif model_name == 'GoogleNet': # googlenet kwargs = dict() kwargs['transform_input'] = True kwargs['aux_logits'] = False # if kwargs['aux_logits']: # warnings.warn('auxiliary heads in the pretrained googlenet model are NOT pretrained, ' # 'so make sure to train them') original_aux_logits = kwargs['aux_logits'] kwargs['aux_logits'] = True kwargs['init_weights'] = False model = models.GoogLeNet(**kwargs) model.load_state_dict(torch.load(os.path.join(PRETRAINED_DEEP_MODEL_DIR, MODEL_NAME[model_name]), map_location=device)) if not original_aux_logits: model.aux_logits = False del model.aux1, model.aux2 elif model_name == 'ResNext101': model = models.resnext101_32x8d(pretrained=False) model.load_state_dict(torch.load(os.path.join(PRETRAINED_DEEP_MODEL_DIR, MODEL_NAME[model_name]), map_location=device)) else: raise ValueError("Model name must be one of ['VGG', 'ResNet', 'DenseNet', 'AlexNet', 'GoogleNet', 'ResNext101']") return model
def save(): cnn = CNN() #定義CNN的名稱為cnn CNN1 = models.AlexNet() k = 0 h = list(cnn.parameters()) bk = False acc = 0 count = 0 right = 0 LastLoss = 100 for epoch in range(90): LR = 0.0005 #0.00025 #0.0015 if LastLoss > 3: LR = 0.001 elif LastLoss < 3 and LastLoss > 0.5: LR = 0.001 elif LastLoss <= 0.5: LR = 0.0005 print(LR) print(epoch) if bk: break for i, b_x in enumerate(train_loader): LR = LR #*abs(math.cos((math.pi)-(i*5*math.pi/180))) print(i * 5) optimizer = torch.optim.Adam(cnn.parameters(), lr=LR) #優化器 在此選擇Adam 學習率=0.001 loss_func = nn.CrossEntropyLoss() #損失函數 output = cnn(b_x['image'])[0] #將照片丟給cnn運算 loss = loss_func(output, b_x['label']) #計算損失誤差 optimizer.zero_grad() #將梯度歸零 loss.backward() #反向求函數偏導數 optimizer.step() #更新權重 Recognition_result = torch.max(output, 1)[1] #取得當前判斷結果 #ans=str(int(b_x['label']))#取得答案 #count+=1 #if Recognition_result==ans: # right+=1 #print("訓練第 "+str(count)+" 次的結果="+str(Recognition_result),"正確答案="+ans,"ACC=%.3f"%(right/count)) print(Recognition_result, b_x['label']) print(loss.data.numpy()) LastLoss = loss.data.numpy() torch.save(cnn.state_dict(), 'reallynet1.pt') #訓練完成後將權重打包 print("DONE")
def loading_model(path): checkpoint = torch.load(path) if checkpoint['pretrained_model']=='vgg11': model = models.vgg11(pretrained=True) model.classifier = checkpoint['classifier'] elif checkpoint['pretrained_model']=='AlexNet': model = models.AlexNet(pretrained=True) model.classifier = checkpoint['classifier'] else: model = models.resnet18(pretrained=True) model.fc = checkpoint['classifier'] learning_rate = checkpoint['learning_rate'] model.epochs = checkpoint['epochs'] model.optimizer = checkpoint['optimizer'] model.class_to_idx = checkpoint['class_to_idx'] model.load_state_dict(checkpoint['model']) criterion = checkpoint['loss'] return model
def outputsummary(): """ layer input_shape output_shape nb_params weight_shape """ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') vgg = models.AlexNet().to(device) #x = torch.randn(1,3,224,224).cuda()#change 12 to the channel number of network input #y = vgg(x) r_summary = summary(vgg, (3, 3, 224, 224), batch_size=3) #for layer in r_summary: #print(r_summary[i]["input_shape"]) #print(layer,r_summary[layer]['output_shape'],r_summary[layer]['nb_params'],) return r_summary
def load_model(name, num_classes, input_height=64, input_width=64, num_of_channels=3): if name == 'COAPModNet': net = COAPModNet(num_classes=num_classes) elif name == 'COAPNet': net = COAPNet(num_classes=num_classes) elif name == 'SimpleNet': net = SimpleNet(num_classes=num_classes) elif name == 'AlexNet': net = models.AlexNet(num_classes=num_classes) elif name == 'PlanktonNet': net = PlanktonNet(num_classes=num_classes) elif name == 'ResNet18': net = models.resnet18(num_of_channels, num_classes) elif name == 'ResNet34': net = models.resnet34(num_of_channels, num_classes) elif name == 'ResNet50': net = models.resnet50(num_of_channels, num_classes) elif name == 'ResNet101': net = models.resnet101(num_of_channels, num_classes) elif name == 'ResNet152': net = models.resnet152(num_of_channels, num_classes) elif name == 'VGGNet11': net = models.vgg11(num_classes=num_classes) elif name == 'VGGNet13': net = models.vgg13(num_classes=num_classes) elif name == 'VGGNet16': net = models.vgg16(num_classes=num_classes) elif name == 'VGGNet19': net = models.vgg19(num_classes=num_classes) elif name == 'ResNext50': net = models.resnext50_32x4d(num_classes=num_classes) elif name == 'ResNext101': net = models.resnext101_32x8d(num_classes=num_classes) elif name == 'GoogLeNet': net = models.GoogLeNet(num_classes=num_classes) return net
def test(**kwargs): opt.parse(kwargs) # configure model model = getattr(models, opt.model)().eval() AlexNet = Pre_models.AlexNet(pretrained=True) # 读取参数 pretrained_dict = AlexNet.state_dict() model_dict = model.state_dict() # 将pretrained_dict里不属于model_dict的键剔除掉 pretrained_dict = { k: v for k, v in pretrained_dict.items() if k in model_dict } # 更新现有的model_dict model_dict.update(pretrained_dict) # 加载我们真正需要的state_dict model.load_state_dict(model_dict) # data train_data = CAG(opt.test_data_root, test=True) test_dataloader = DataLoader(train_data, batch_size=opt.batch_size, shuffle=False, num_workers=opt.num_workers) results = [] for ii, (data, path) in enumerate(test_dataloader): input = t.autograd.Variable(data, volatile=True) if opt.use_gpu: input = input.cuda() score = model(input) probability = t.nn.functional.softmax(score)[:, 0].data.tolist() # label = score.max(dim = 1)[1].data.tolist() batch_results = [(path_, probability_) for path_, probability_ in zip(path, probability)] results += batch_results write_csv(results, opt.result_file) return results
def make_model(self, name): if name == 'vgg16': # model = models.vgg16(pretrained=True).features # print(model) # 其实就是定位到第28层,对照着上面的key看就可以理解 model = models.vgg16(pretrained=True).features[:28] # print(model) elif name == 'alexnet': model = models.AlexNet().features elif name == 'densenet121': model = models.densenet121(pretrained=True).features # print(model) elif name == 'resnet50': resnet = models.resnet50(pretrained=True) modules = list(resnet.children())[:-1] # delete the last fc layer. model = nn.Sequential(*modules) # print(model) else: raise KeyError model = model.eval() # 一定要有这行,不然运算速度会变慢(要求梯度)而且会影响结果 if torch.cuda.is_available(): model.cuda() # 将模型从CPU发送到GPU,如果没有GPU则删除该行 return model
train_dataset = datasets.ImageFolder(train_dir, transform = train_transforms) valid_dataset = datasets.ImageFolder(valid_dir, transform = data_transforms) test_dataset = datasets.ImageFolder(test_dir, transform = data_transforms) trainloader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True) validloader = torch.utils.data.DataLoader(valid_dataset, batch_size=64, shuffle=True) testloader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=True) if dev == 'GPU': device = torch.device('cuda') else: device = torch.device('cpu') if architecture == 'AlexNet': model = models.AlexNet(pretrained=True) classifier = nn.Sequential(OrderedDict([('inp', nn.Linear(256,int(hidden_layers))), ('dropout', nn.Dropout(p=0.4)), ('relu', nn.ReLU()), ('out', nn.Linear(int(hidden_layers),102)), ('softmax', nn.LogSoftmax(dim=1))])) for i in model.parameters(): i.requires_grad = False model.classifier = classifier criterion = nn.NLLLoss() optimizer = optim.Adam(model.classifier.parameters(), lr = float(learning_rate)) elif architecture == 'vgg11': model = models.vgg11(pretrained=True) classifier = nn.Sequential(OrderedDict([('inp', nn.Linear(25088,int(hidden_layers))), ('dropout', nn.Dropout(p=0.4)),
'gaussian_blur', 'snow', 'shear' ]) parser.add_argument('--difficulty', '-d', type=int, default=1, choices=[1, 2, 3]) # Acceleration parser.add_argument('--ngpu', type=int, default=1, help='0 = CPU.') args = parser.parse_args() print(args) # /////////////// Model Setup /////////////// if args.model_name == 'alexnet': net = models.AlexNet() net.load_state_dict( model_zoo.load_url( 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth', # model_dir='/share/data/lang/users/dan/.torch/models')) model_dir='/share/data/vision-greg2/pytorch_models/alexnet')) args.test_bs = 6 elif args.model_name == 'squeezenet1.0': net = models.SqueezeNet(version=1.0) net.load_state_dict( model_zoo.load_url( 'https://download.pytorch.org/models/squeezenet1_0-a815701f.pth', # model_dir='/share/data/lang/users/dan/.torch/models')) model_dir='/share/data/vision-greg2/pytorch_models/squeezenet')) args.test_bs = 6
import torch import torchvision.models as models import torch.nn as nn from torch.optim import SGD, optimizer from torch.optim.lr_scheduler import ReduceLROnPlateau from src.dataset_ants_bees import train_dataset, val_dataset from src.dataset_ants_bees import train_loader, val_loader import numpy as np from sklearn.metrics import accuracy_score # Define NN net = models.AlexNet(pre_trained=True) if __name__ == '__main__': print(net)
import torch import torch.nn as nn from torchsummary import summary from torchvision import models from pthflops import count_ops parser = argparse.ArgumentParser() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") net = models.AlexNet().to(device) summary(net, (3, 64, 64)) inp = torch.rand(1, 3, 224, 224).to(device) count_ops(net, inp)
def main(): wandb.init() histogram_small_literal = wandb.Histogram(np_histogram=([1, 2, 4], [3, 10, 20, 0])) histogram_large_random = wandb.Histogram( numpy.random.randint(255, size=(1000))) numpy_array = numpy.random.rand(1000) torch_tensor = torch.rand(1000, 1000) data_frame = pandas.DataFrame(data=numpy.random.rand(1000), columns=['col']) tensorflow_variable_single = tensorflow.Variable(543.01, tensorflow.float32) tensorflow_variable_multi = tensorflow.Variable([[2, 3], [7, 11]], tensorflow.int32) plot_scatter = go.Figure( # plotly data=go.Scatter(x=[0, 1, 2]), layout=go.Layout(title=go.layout.Title(text="A Bar Chart"))) image_data = numpy.zeros((28, 28)) image_cool = wandb.Image(image_data, caption="Cool zeros") image_nice = wandb.Image(image_data, caption="Nice zeros") image_random = wandb.Image(numpy.random.randint(255, size=(28, 28, 3))) image_pil = wandb.Image(PIL.Image.new("L", (28, 28))) plt.plot([1, 2, 3, 4]) plt.ylabel('some interesting numbers') image_matplotlib_plot = wandb.Image(plt) matplotlib_plot = plt audio_data = numpy.random.uniform(-1, 1, 44100) sample_rate = 44100 caption1 = "This is what a dog sounds like" caption2 = "This is what a chicken sounds like" # test with all captions audio1 = wandb.Audio(audio_data, sample_rate=sample_rate, caption=caption1) audio2 = wandb.Audio(audio_data, sample_rate=sample_rate, caption=caption2) # test with no captions audio3 = wandb.Audio(audio_data, sample_rate=sample_rate) audio4 = wandb.Audio(audio_data, sample_rate=sample_rate) # test with some captions audio5 = wandb.Audio(audio_data, sample_rate=sample_rate) audio6 = wandb.Audio(audio_data, sample_rate=sample_rate, caption=caption2) html = wandb.Html("<html><body><h1>Hello</h1></body></html>") table_default_columns = wandb.Table() table_default_columns.add_data("Some awesome text", "Positive", "Negative") table_custom_columns = wandb.Table(["Foo", "Bar"]) table_custom_columns.add_data("So", "Cool") table_custom_columns.add_data("&", "Rad") #plot_figure = matplotlib.pyplot.plt.figure() #c1 = matplotlib.pyplot.plt.Circle((0.2, 0.5), 0.2, color='r') #ax = matplotlib.pyplot.plt.gca() #ax.add_patch(c1) #matplotlib.pyplot.plt.axis('scaled') # pytorch model graph alex = models.AlexNet() graph = wandb.wandb_torch.TorchGraph.hook_torch(alex) alex.forward(dummy_torch_tensor((2, 3, 224, 224))) with tensorflow.Session().as_default() as sess: sess.run(tensorflow.global_variables_initializer()) wandb.run.summary.update({ 'histogram-small-literal-summary': histogram_small_literal, 'histogram-large-random-summary': histogram_large_random, 'numpy-array-summary': numpy_array, 'torch-tensor-summary': torch_tensor, 'data-frame-summary': data_frame, 'image-cool-summary': image_cool, 'image-nice-summary': image_nice, 'image-random-summary': image_random, 'image-pil-summary': image_pil, 'image-plot-summary': image_matplotlib_plot, 'image-list-summary': [image_cool, image_nice, image_random, image_pil], # Doesn't work, because something has happened to the MPL object (MPL may # be doing magical scope stuff). If you log it right after creating it, # it works fine. # 'matplotlib-plot': matplotlib_plot, 'audio1-summary': audio1, 'audio2-summary': audio2, 'audio3-summary': audio3, 'audio4-summary': audio4, 'audio5-summary': audio5, 'audio6-summary': audio6, 'audio-list-summary': [audio1, audio2, audio3, audio4, audio5, audio6], 'html-summary': html, 'table-default-columns-summary': table_default_columns, 'table-custom-columns-summary': table_custom_columns, 'plot-scatter-summary': plot_scatter, #'plot_figure': plot_figure, 'tensorflow-variable-single-summary': tensorflow_variable_single, 'tensorflow-variable-multi-summary': tensorflow_variable_multi, 'graph-summary': graph, }) for _ in range(10): wandb.run.history.add({ 'string': 'string', 'histogram-small-literal': histogram_small_literal, 'histogram-large-random': histogram_large_random, 'numpy-array': numpy_array, 'torch-tensor': torch_tensor, #'data-frame': data_frame, # not supported yet 'image-cool': image_cool, 'image-nice': image_nice, 'image-random': image_random, 'image-pil': image_pil, 'image-plot': image_matplotlib_plot, 'image-list': [image_cool, image_nice, image_random, image_pil], # 'matplotlib-plot': matplotlib_plot, 'audio1': audio1, 'audio2': audio2, 'audio3': audio3, 'audio4': audio4, 'audio5': audio5, 'audio6': audio6, 'audio-list': [audio1, audio2, audio3, audio4, audio5, audio6], 'html': html, 'table-default-columns': table_default_columns, 'table-custom-columns': table_custom_columns, 'plot-scatter': plot_scatter, #'plot_figure': plot_figure, 'tensorflow-variable-single': tensorflow_variable_single, 'tensorflow-variable-multi': tensorflow_variable_multi, #'graph': graph, }) wandb.run.summary.update({ 'histogram-small-literal-summary': histogram_small_literal, 'histogram-large-random-summary': histogram_large_random, 'numpy-array-summary': numpy_array, 'torch-tensor-summary': torch_tensor, 'data-frame-summary': data_frame, 'image-cool-summary': image_cool, 'image-nice-summary': image_nice, 'image-random-summary': image_random, 'image-pil-summary': image_pil, 'image-plot-summary': image_matplotlib_plot, 'image-list-summary': [image_cool, image_nice, image_random, image_pil], # 'matplotlib-plot': matplotlib_plot, 'audio1-summary': audio1, 'audio2-summary': audio2, 'audio3-summary': audio3, 'audio4-summary': audio4, 'audio5-summary': audio5, 'audio6-summary': audio6, 'audio-list-summary': [audio1, audio2, audio3, audio4, audio5, audio6], 'html-summary': html, 'table-default-columns-summary': table_default_columns, 'table-custom-columns-summary': table_custom_columns, 'plot-scatter-summary': plot_scatter, #'plot_figure': plot_figure, 'tensorflow-variable-single-summary': tensorflow_variable_single, 'tensorflow-variable-multi-summary': tensorflow_variable_multi, 'graph-summary': graph, })
iter_data = iter(trainloader) images, labels = next(iter_data) hog_dim = images.size()[1] # ---- log-------------------- log = dict() it_num = 0 running_loss = 0.0 print("Training Start...\n") log["hog_dim"] = hog_dim # net = model.getNetwork(config["Architecture"]["network"],num_cls).cuda() net = models.AlexNet(num_cls).cuda() print(net) optimizer = optim.Adam(net.parameters(), lr=learning_rate) sumEpoch = 0 accuracy_history = dict() loss_history = [] # ------------ Run ------------------ timeMemo.reset() for epoch in range(epochs): transData=transformFunction(fontData) # ryuutils.example.showExamplePIL(transData,8,4,shuffle=True) trainData=ryutorch.transform.pil2Tensor(transData)
def __init__(self, num_classes): super(AlexNet, self).__init__() self.alex_net = models.AlexNet(num_classes=num_classes) self.base_net = self.alex_net.features self.pooling = nn.AvgPool2d(3) self.fc = nn.Linear(1024, num_classes)
def alex_net(num_classes): return models.AlexNet(num_classes)
import torch from torchvision import models model = models.AlexNet() model.eval() # import urllib # url, filename = ("https://github.com/pytorch/hub/raw/master/dog.jpg", "dog.jpg") # try: urllib.URLopener().retrieve(url, filename) # except: urllib.request.urlretrieve(url, filename) filename = "dog.jpg" from PIL import Image from torchvision import transforms input_image = Image.open(filename) preprocess = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) input_tensor = preprocess(input_image) input_batch = input_tensor.unsqueeze( 0) # create a mini-batch as expected by the model # move the input and model to GPU for speed if available if torch.cuda.is_available(): input_batch = input_batch.to('cuda') model.to('cuda')
from typing import Any bb = BasicBlock(10, 10) mnasnet_dict = {"alpha": 1.0, "num_classes": 1000} @pytest.mark.parametrize( "modulepath, classname, cfg, passthrough_args, passthrough_kwargs, expected", [ pytest.param( "models.alexnet", "AlexNet", {}, [], {}, models.AlexNet(), id="AlexNetConf", ), pytest.param( "models.resnet", "ResNet", {"layers": [2, 2, 2, 2]}, [], {"block": Bottleneck}, models.ResNet(block=Bottleneck, layers=[2, 2, 2, 2]), id="ResNetConf", ), pytest.param( "models.densenet", "DenseNet", {},
from utils.AverageMeter import AverageMeter from utils.metric import accuracy, update_class_acc import utils.get_data as gd os.environ["CUDA_VISIBLE_DEVICES"] = "4" """Pretrain the basic classifors and features extracter """ torch.manual_seed(1) # Parameters EPOCH = 5 BATCH_SIZE = 48 LR = 0.0001 # learning rate # Resnet50 inception = models.AlexNet().cuda() optimizer = torch.optim.Adam(inception.parameters(), lr=LR) loss_func = nn.CrossEntropyLoss() for epoch in range(EPOCH): print("Epoch {}".format(epoch)) for step, sample in enumerate(gd.train_loader): # gives batch data # b_x = x.view(BATCH_SIZE, 224,224) # reshape x to (batch, time_step, input_size) s = Variable(sample[0].cuda()) output = inception(s) # output loss = loss_func(output, sample[1].cuda()) # cross entropy loss optimizer.zero_grad() # clear gradients for this training step loss.backward() # backpropagation, compute gradients optimizer.step() # apply gradients print("Iter {}".format(step)) # save model
def ceal(du, dl, dtest, configuration_path): with open(configuration_path, 'r') as cfg_path: cfg = yaml.safe_load(cfg_path) model = cfg['config']['model'] labeled_category = cfg['config']['labeled_category'] if model == 'AlexNet': model_ft = models.AlexNet(pretrained=True) for param in model_ft.parameters(): param.requires_grad = False model_ft.classifier[6] = nn.Linear(4096, len(labeled_category)) elif model == 'ResNet': model_ft = models.resnet18(pretrained=True) for param in model_ft.parameters(): param.requires_grad = False num_ftrs = model_ft.fc.in_features model_ft.fc = nn.Linear(num_ftrs, len(labeled_category)) elif model == 'DenseNet': model_ft = models.densenet161(pretrained=True) for param in model_ft.parameters(): param.requires_grad = False num_ftrs = model_ft.classifier.in_features model_ft.classifier = nn.Linear(num_ftrs, len(labeled_category)) else: print("Model is not defined yet.") return device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model_ft = model_ft.to(device) criterion = nn.CrossEntropyLoss() optimizer_ft = optim.Adam(model_ft.parameters(), lr=0.001, betas=(0.9, 0.999)) num_epochs = cfg['config']['epochs'] max_iteration = cfg['config']['max_iteration'] uncertain_samples = [] high_confidence_samples = [] for iteration in range(max_iteration): if len(high_confidence_samples) != 0: dl, dtest = make_labeled_dataloader(configuration_path) model_ft = train(model_ft, device, dl, dtest, num_epochs, criterion, optimizer_ft) if len(high_confidence_samples) != 0: for sample in high_confidence_samples: filename = sample[0] label = labeled_category[sample[1]] shutil.move( os.path.join(labeled_dataset_path, label, filename.split(os.sep)[-1]), filename) du = make_unlabeled_dataloader(configuration_path) high_confidence_samples.clear() pred_prob = predict(model_ft, device, du) k = cfg['ceal']['k'] criteria = cfg['ceal']['criteria'] uncert_samp_idx, _ = get_uncertain_samples(pred_prob=pred_prob, k=k, criteria=criteria) scheduling_path = cfg['config']['labeling_scheduling_path'] if not os.path.isdir(scheduling_path): os.makedirs(scheduling_path, exist_ok=True) for idx in uncert_samp_idx: filename = du.dataset[idx]['filename'] uncertain_samples.append(filename) shutil.copy(filename, scheduling_path + '/' + filename.split(os.sep)[-1]) labeling(scheduling_path, labeled_category) delta_0 = cfg['ceal']['delta_0'] hcs_idx, hcs_labels = get_high_confidence_samples(pred_prob=pred_prob, delta=delta_0) for idx, label in zip(hcs_idx, hcs_labels): filename = du.dataset[idx]['filename'] high_confidence_samples.append([filename, label]) labeled_dataset_path = cfg['config']['labeled_data_path'] for sample in high_confidence_samples: filename = sample[0] label = labeled_category[sample[1]] shutil.move( filename, os.path.join(labeled_dataset_path, label, filename.split(os.sep)[-1])) for sample in uncertain_samples: os.remove(sample) uncertain_samples.clear() for sample in high_confidence_samples: filename = sample[0] label = labeled_category[sample[1]] shutil.move( os.path.join(labeled_dataset_path, label, filename.split(os.sep)[-1]), filename)
def cal_elapse(nn_name, img_file, use_gpu=True): """ calculate time elapse :param nn_name: :param img_file: :param use_gpu :return: """ import torchvision.models as models image = resize(io.imread(img_file), (224, 224), mode='constant') image[:, :, 0] -= 131.45376586914062 image[:, :, 1] -= 103.98748016357422 image[:, :, 2] -= 91.46234893798828 image = np.transpose(image, [2, 0, 1]) input = torch.from_numpy(image).unsqueeze(0).float() device = torch.device( "cuda:0" if torch.cuda.is_available() and use_gpu else "cpu") if nn_name == 'AlexNet': alex_net = models.AlexNet(num_classes=1) input = input.to(device) alex_net = alex_net.to(device) start = time.time() alex_net.forward(input) end = time.time() return end - start elif nn_name == 'ResNet18': resnet18 = models.resnet18(num_classes=1) input = input.to(device) resnet18 = resnet18.to(device) start = time.time() resnet18.forward(input) end = time.time() return end - start elif nn_name == 'ResNet50': resnet50 = models.resnet50(num_classes=1) input = input.to(device) resnet50 = resnet50.to(device) start = time.time() resnet50.forward(input) end = time.time() return end - start elif nn_name == 'ResNet101': resnet101 = models.resnet101(num_classes=1) input = input.to(device) resnet101 = resnet101.to(device) start = time.time() resnet101.forward(input) end = time.time() return end - start elif nn_name == 'ResNet152': resnet152 = models.resnet152(num_classes=1) input = input.to(device) resnet152 = resnet152.to(device) start = time.time() resnet152.forward(input) end = time.time() return end - start elif nn_name == 'HMT-Net': hmt_net = HMTNet() hmt_net.load_state_dict(torch.load('./model/hmt-net.pth')) input = input.to(device) hmt_net = hmt_net.to(device) start = time.time() hmt_net.forward(input) end = time.time() return end - start else: print('Invalid NN Name!!') sys.exit(0)
def load_alexnet(self, model_path): """加载AlexNet预训练模型;""" model = models.AlexNet() model.load_state_dict(torch.load(model_path)) return model.features