Commit 0275e2a9 authored by E144069X's avatar E144069X

Added adapations for the marseille dataset

parent 2b2e616d
......@@ -422,16 +422,21 @@ def collectAndFormatCSVs(dataset,labelDict):
return dfDict
def getLabels():
return labelDict
def getLabels(marseille=False):
if not marseille:
return labelDict
else:
return mars_labelDict
def getReversedLabels(marseille=False):
def getReversedLabels():
dict = getLabels(marseille)
revDict = {}
for key in labelDict.keys():
revDict[labelDict[key]] = key
revDict = {}
for key in dict.keys():
revDict[labelDict[key]] = key
return revDict
return revDict
def formatCUB(pathToCubFolder):
......
......@@ -287,8 +287,10 @@ class TestLoader():
if os.path.exists("../data/{}/annotations/{}_timeElapsed.csv".format(self.dataset.split("+")[0],vidName)):
timeElapsed = np.genfromtxt("../data/{}/annotations/{}_timeElapsed.csv".format(self.dataset.split("+")[0],vidName),delimiter=",")[1:][self.currFrameInd:min(self.currFrameInd+L,frameNb),1]
else:
elif len(self.dataset.split("+")) > 1:
timeElapsed = np.genfromtxt("../data/{}/annotations/{}_timeElapsed.csv".format(self.dataset.split("+")[1],vidName),delimiter=",")[1:][self.currFrameInd:min(self.currFrameInd+L,frameNb),1]
else:
timeElapsed = None
if frameInds[-1] + 1 == frameNb:
self.currFrameInd = None
......@@ -324,7 +326,9 @@ def loadFrames_and_process(frameInds,gt,timeElapsed,vidName,video,preproc):
# T x 3 x H x W
frameSeq = torch.cat(list(map(lambda x:preproc.normalizeFunc(x).unsqueeze(0),frameSeq.float())),dim=0)
return frameSeq.unsqueeze(0),torch.tensor(gt).unsqueeze(0),vidName,torch.tensor(frameInds).int(),torch.tensor(timeElapsed).float().unsqueeze(0)/maxTime
timeElapsed = torch.tensor(timeElapsed).float().unsqueeze(0)/maxTime if not timeElapsed is None else None
return frameSeq.unsqueeze(0),torch.tensor(gt).unsqueeze(0),vidName,torch.tensor(frameInds).int(),timeElapsed
def computeMask(maskTimeOnImage,imgSize):
if maskTimeOnImage:
......@@ -545,7 +549,7 @@ def getGT(vidName,dataset):
for phase in phases:
#The dictionary called here convert the label into a integer
gt[int(phase[1]):int(phase[2])+1] = formatData.getLabels()[phase[0]]
gt[int(phase[1]):int(phase[2])+1] = formatData.getLabels(datasetOfTheVideo=="marseille")[phase[0]]
np.savetxt("../data/{}/annotations/{}_targ.csv".format(datasetOfTheVideo,vidName),gt)
else:
......
......@@ -12,8 +12,8 @@ resize_image = True
log_interval = 50
tr_len = 25
val_l = 150
val_l_temp = 300
val_l = 10
val_l_temp = 10
min_phase_nb = 6
img_size = 500
......
......@@ -720,6 +720,13 @@ def plotConfusionMatrix(exp_id,model_id,epochToProcess,dataset_test,test_part_be
labels = formatData.getLabels()
labelInds = list(revDict.keys())
if dataset_test == "marseille":
revDict_gt = formatData.getReversedLabels(True)
labels_gt = formatData.getLabels(True)
labelInds_gt = list(revDict_gt.keys())
else:
revDict_gt,labels_gt,labelInds_gt = revDict,labels,labelInds
for i,resFilePath in enumerate(resFilePaths):
if i%1==0:
......@@ -742,16 +749,15 @@ def plotConfusionMatrix(exp_id,model_id,epochToProcess,dataset_test,test_part_be
if confMat[i].sum() > 0:
confMat[i] = confMat[i]/confMat[i].sum()
labelIndsPred,labelIndsGT = list(set(pred)),list(set(gt))
print(labelIndsPred,labelIndsGT)
labelsPred,labelsGT = [revDict[labelInd] for labelInd in labelIndsPred],[revDict[labelInd] for labelInd in labelIndsGT]
#labelIndsPred,labelIndsGT = list(set(pred)),list(set(gt))
#labelsPred,labelsGT = [revDict[labelInd] for labelInd in labelIndsPred],[revDict[labelInd] for labelInd in labelIndsGT]
plt.figure()
img = plt.imshow(confMat)
plt.xlabel("Predictions")
plt.xticks(np.arange(len(labelInds)),labels,rotation=45)
plt.ylabel("Ground-truth")
plt.yticks(np.arange(len(labelInds)),labels)
plt.yticks(np.arange(len(labelInds_gt)),labels_gt)
plt.colorbar(img)
plt.tight_layout()
plt.savefig("../vis/{}/confMat_{}_epoch{}_{}.png".format(exp_id,model_id,epochToProcess,videoDict[resFilePath]))
......
......@@ -145,7 +145,7 @@ def average_gradients(model):
dist.all_reduce(param.grad.data, op=dist.ReduceOp.SUM)
param.grad.data /= size
def epochSeqVal(model,log_interval,loader, epoch, args,writer,metricEarlyStop,mode="val"):
def epochSeqVal(model,log_interval,loader, epoch, args,writer,metricEarlyStop,mode="val",computeMetrics=True):
'''
Validate a model. This function computes several metrics and return the best value found until this point.
......@@ -191,7 +191,9 @@ def epochSeqVal(model,log_interval,loader, epoch, args,writer,metricEarlyStop,mo
print("\t",loader.sumL+1,"/",loader.nbImages)
if args.cuda:
data, target,frameInds,timeElapsedTensor = data.cuda(), target.cuda(),frameInds.cuda(),timeElapsedTensor.cuda()
data, target,frameInds = data.cuda(), target.cuda(),frameInds.cuda()
if not timeElapsedTensor is None:
timeElapsedTensor = timeElapsedTensor.cuda()
visualDict = model.computeVisual(data)
feat = visualDict["x"].data
......@@ -199,7 +201,8 @@ def epochSeqVal(model,log_interval,loader, epoch, args,writer,metricEarlyStop,mo
update.updateFrameDict(frameIndDict,frameInds,vidName)
if newVideo and not videoBegining:
allOutput,nbVideos = update.updateMetrics(args,model,allFeat,allTimeElapsedTensor,allTarget,precVidName,nbVideos,metrDict,outDict,targDict)
if computeMetrics:
allOutput,nbVideos = update.updateMetrics(args,model,allFeat,allTimeElapsedTensor,allTarget,precVidName,nbVideos,metrDict,outDict,targDict)
intermVarDict = update.saveIntermediateVariables(intermVarDict,args.exp_id,args.model_id,epoch,precVidName)
intermVarDict = update.catIntermediateVariables(visualDict,intermVarDict,nbVideos)
......@@ -225,7 +228,8 @@ def epochSeqVal(model,log_interval,loader, epoch, args,writer,metricEarlyStop,mo
break
if not args.debug:
allOutput,nbVideos = update.updateMetrics(args,model,allFeat,allTimeElapsedTensor,allTarget,precVidName,nbVideos,metrDict,outDict,targDict)
if computeMetrics:
allOutput,nbVideos = update.updateMetrics(args,model,allFeat,allTimeElapsedTensor,allTarget,precVidName,nbVideos,metrDict,outDict,targDict)
intermVarDict = update.saveIntermediateVariables(intermVarDict,args.exp_id,args.model_id,epoch,precVidName)
for key in outDict.keys():
......@@ -406,6 +410,30 @@ def get_OptimConstructor_And_Kwargs(optimStr,momentum):
kwargs = {'amsgrad':True}
return optimConst,kwargs
def moduleVSNoModule(startsWithModule,params):
if startsWithModule:
paramsFormated = {}
for key in params.keys():
keyFormat = "module."+key if key.find("module") == -1 else key
paramsFormated[keyFormat] = params[key]
params = paramsFormated
else:
paramsFormated = {}
for key in params.keys():
keyFormat = key.replace("module.","")
paramsFormated[keyFormat] = params[key]
params = paramsFormated
return params
def oldNamesToNewNames(params):
paramsFormated = {}
for key in params.keys():
keyFormat = key.replace("visualModel","firstModel").replace("tempModel","secondModel")
paramsFormated[keyFormat] = params[key]
params = paramsFormated
return params
def initialize_Net_And_EpochNumber(net,exp_id,model_id,cuda,start_mode,init_path,strict):
'''Initialize a network
......@@ -442,20 +470,8 @@ def initialize_Net_And_EpochNumber(net,exp_id,model_id,cuda,start_mode,init_path
#Checking if the key of the model start with "module."
startsWithModule = (list(net.state_dict().keys())[0].find("module.") != -1)
if startsWithModule:
paramsFormated = {}
for key in params.keys():
keyFormat = "module."+key if key.find("module") == -1 else key
paramsFormated[keyFormat] = params[key]
params = paramsFormated
else:
paramsFormated = {}
for key in params.keys():
keyFormat = key.replace("module.","")
paramsFormated[keyFormat] = params[key]
params = paramsFormated
params = moduleVSNoModule(startsWithModule,params)
params = oldNamesToNewNames(params)
#Removing keys corresponding to parameter which shape are different in the checkpoint and in the current model
#For example, this is necessary to load a model trained on n classes to bootstrap a model with m != n classes.
......@@ -496,6 +512,7 @@ def initialize_Net_And_EpochNumber(net,exp_id,model_id,cuda,start_mode,init_path
startEpoch = 1
return startEpoch
def getBestEpochInd_and_WorseEpochNb(start_mode,exp_id,model_id,epoch):
if start_mode == "scratch":
......@@ -592,6 +609,7 @@ def run(args):
kwargsVal['loader'] = valLoader
kwargsVal["metricEarlyStop"] = args.metric_early_stop
kwargsVal["computeMetrics"] = args.compute_metrics_during_eval
#Getting the contructor and the kwargs for the choosen optimizer
optimConst,kwargsOpti = get_OptimConstructor_And_Kwargs(args.optim,args.momentum)
......@@ -630,7 +648,9 @@ def run(args):
trainFunc(**kwargsTr)
else:
if not args.no_val:
net.load_state_dict(torch.load("../models/{}/model{}_epoch{}".format(args.exp_id_no_train,args.model_id_no_train,epoch),map_location="cpu" if not args.cuda else None))
params = oldNamesToNewNames(torch.load("../models/{}/model{}_epoch{}".format(args.exp_id_no_train,args.model_id_no_train,epoch),map_location="cpu" if not args.cuda else None))
params = moduleVSNoModule(list(net.state_dict().keys())[0].find("module.") != -1,params)
net.load_state_dict(params)
if not args.no_val:
with torch.no_grad():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment