def __init__(self, Net, epoch, batchSize, imgPath, tagPath, testTagPath, testImgPath, testResult): # multiprocessing.set_start_method('spawn', True) ''' Net:PNet,RNet,ONet对应需要训练的网络名称 epoch,batchSize 批次和轮次 ''' self.writer = SummaryWriter(log_dir="./runs/{}_runs".format(Net)) self.netName = Net self.testResult = testResult self.device = deviceFun() self.modelPath = str( '/home/chinasilva/code/deeplearning_homework/project_5/model/' + self.netName + '.pth') if Net == 'PNet': self.centlossSize = 32 self.size = 12 self.net = PNet().to(self.device) elif Net == 'RNet': self.centlossSize = 128 self.size = 24 self.net = RNet().to(self.device) elif Net == 'ONet': self.centlossSize = 256 self.size = 48 self.net = ONet().to(self.device) else: raise RuntimeError('训练时,请输入正确的网络名称') self.batchSize = batchSize self.epoch = epoch self.myData = MyData(tagPath, imgPath) self.testData = MyData(testTagPath, testImgPath) self.lossFun1 = nn.MSELoss() #reduction='none' self.lossFun2 = nn.MSELoss() #reduction='none' self.lossFun3 = nn.MSELoss() #reduction='none' # self.lossFun1=nn.BCELoss() # self.lossFun2=nn.MSELoss() # self.lossFun3=nn.MSELoss() self.criterion = nn.CrossEntropyLoss() self.center_loss_layer = CenterLoss(2, self.centlossSize) if os.path.exists(self.modelPath): self.net = torch.load(self.modelPath) # self.optimizer=torch.optim.SGD(self.net.parameters(), lr=0.001,momentum=0.9,weight_decay=0.00003) # self.optimizer3 = torch.optim.SGD(self.net.parameters(),lr=0.5) # self.optimizer3 = torch.optim.Adam(self.center_loss_layer.parameters()) # self.optimizer=torch.optim.Adam(self.net.parameters()) self.optimizer = torch.optim.SGD(self.net.parameters(), lr=0.0001, momentum=0.9, weight_decay=0.0005) self.optimizer2 = Lookahead(self.optimizer, k=5, alpha=0.5) # Initialize Lookahead
def __init__(self, batchSize, epoch): self.device = deviceFun() self.batchSize = batchSize self.epoch = epoch self.myData = MyData() self.preModelLoction = PRE_MODEL_PATH self.lossFun = nn.MSELoss() self.net = MyNet(cls_num=CLASS_NUM).to(self.device) self.darkNet = DarkNet().to(self.device) self.loadModel(self.preModelLoction) self.optimizer = torch.optim.Adam( filter(lambda p: p.requires_grad != None, self.net.parameters()))
def __init__(self, testImagePath, netPath): self.device = deviceFun(cpu=False) self.pnet = torch.load(netPath + '/PNet.pth') #,map_location="cpu" self.rnet = torch.load(netPath + '/RNet.pth') self.onet = torch.load(netPath + '/ONet.pth') self.testImagePath = testImagePath self.pnetSize = 12 self.rnetSize = 24 self.onetSize = 48 self.pnet.eval() self.rnet.eval() self.onet.eval() self.imgName = ''
def __init__(self): super().__init__() self.device = deviceFun() self.net = MyNet() # self.loadModel() self.net = self.net.load_state_dict( torch.load(PRE_MODEL_PATH, map_location=self.device)) # torch.load(PRE_MODEL_PATH,) self.trainImagePath = IMG_PATH self.net.eval() self.imgName = '' self.net = torch.load(MODEL_PATH) self.trainImagePath = IMG_PATH self.net.eval() self.imgName = ''
def __init__(self, in_features, out_features, s=2.0, m=0.50, easy_margin=False): #m=0.50 super(ArcMarginProduct, self).__init__() self.device = deviceFun() self.in_features = in_features self.out_features = out_features self.s = s self.m = m self.weight = nn.Parameter(torch.FloatTensor(out_features, in_features)) nn.init.xavier_uniform_(self.weight) self.easy_margin = easy_margin self.cos_m = math.cos(m) self.sin_m = math.sin(m) self.th = math.cos(math.pi - m) self.mm = math.sin(math.pi - m) * m
def __init__(self, ratio): super(NLL_OHEM, self).__init__(None, True) self.ratio = ratio self.device = deviceFun()