def _init_training(self, learningRate, dataset, useDatabase): # Dataset is COCO print("Initiating training of Sanitizer MaskGenerator") print("Loading COCO") from pycocotools.coco import COCO from os import path annFile = topDir + '/annotations/instances_{}.json'.format(dataset) self.cocoPath = topDir + '/data/{}'.format(dataset) self.coco = COCO(annFile) self.personCatID = self.coco.getCatIds(catNms=['person'])[0] self.cocoImageIds = self.coco.getImgIds(catIds=self.personCatID) def is_not_crowd(imgId): annIds = self.coco.getAnnIds(imgIds=imgId, catIds=self.personCatID, iscrowd=False) annotation = self.coco.loadAnns(annIds)[0] return not annotation["iscrowd"] self.cocoImageIds = list(filter(is_not_crowd, self.cocoImageIds)) self.cocoOnDisk = path.exists(self.cocoPath) print("Coco dataset size: {}".format(len(self.cocoImageIds))) print("Coco images found on disk:", self.cocoOnDisk) # Init LMDB_helper if useDatabase: self.lmdb = LMDBHelper("a") self.lmdb.verbose = False # Init loss function and optimizer self.optimizer = torch.optim.Adam(self.maskGenerator.parameters(), lr=learningRate, amsgrad=True) self.loss_function = torch.nn.BCELoss() # Init DensePose extractor self.denseposeExtractor = DensePoseWrapper()
def _initTraining(self, learningRate, dataset, useDatabase): # Dataset is DeepFashion2 print("Initiating training of DescriptionExtractor") print("Loading DeepFashion2") from torchvision import transforms from torchvision.datasets import CocoDetection self.annFile = topDir + '/annotations/deepfashion2_{}.json'.format( dataset) self.cocoImgPath = topDir + '/data/DeepFashion2/{}'.format(dataset) self.useDatabase = useDatabase self.dataset = CocoDetection( self.cocoImgPath, self.annFile, transform=transforms.Compose([ transforms.ToTensor(), transforms.Lambda(lambda x: x.permute(1, 2, 0)), transforms.Lambda(lambda x: (x * 255).byte().numpy()), transforms.Lambda(lambda x: x[:, :, ::-1]) ])) # Init LMDB_helper if useDatabase: self.lmdb = LMDBHelper("a") self.lmdb.verbose = False self.denseposeExtractor = DensePoseWrapper() self.sanitizer = Sanitizer() self.sanitizer.load_model(topDir + "/models/Sanitizer.pth") self.uvMapper = UVMapper() # PyTorch things self.optimizer = torch.optim.Adam(self.classifier.parameters(), lr=learningRate, amsgrad=True) self.lossFunction = torch.nn.BCEWithLogitsLoss()
def _initTraining(self, learningRate, datasetName, useLMDB): self.datasetName = datasetName from DenseSense.algorithms.DensePoseWrapper import DensePoseWrapper from DenseSense.algorithms.Sanitizer import Sanitizer self.denseposeExtractor = DensePoseWrapper() self.sanitizer = Sanitizer() self.sanitizer.load_model(topDir + "/models/Sanitizer.pth") if datasetName in ActionClassifier.COCO_Datasets: print("Loading COCO dataset: " + datasetName) from pycocotools.coco import COCO from os import path annFile = topDir + '/annotations/instances_{}.json'.format( datasetName) self.cocoPath = topDir + '/data/{}'.format(datasetName) self.coco = COCO(annFile) personCatID = self.coco.getCatIds(catNms=['person'])[0] self.dataset = self.coco.getImgIds(catIds=personCatID) elif datasetName in ActionClassifier.AVA_Datasets: print("Loading AVA dataset: " + datasetName) import csv from collections import defaultdict from DenseSense.utils.YoutubeLoader import YoutubeLoader annFile = topDir + "/annotations/{}.csv".format( datasetName.replace("_predictive", "")) self.dataset = defaultdict(lambda: defaultdict(defaultdict)) with open(annFile, 'r') as csvFile: reader = csv.reader(csvFile) for row in reader: video, t, x1, y1, x2, y2, action, person = row actions = {action} if person in self.dataset[video][t]: actions = actions.union( self.dataset[video][t][person]["actions"]) self.dataset[video][t][person] = { "bbox": (x1, y1, x2, y2), "actions": actions } ordered_data = [] for key, video in self.dataset.items(): ordered_data.append((key, [])) for t, annotation in video.items(): ordered_data[-1][1].append((int(t), annotation)) ordered_data[-1][1].sort(key=lambda x: x[0]) self.dataset = ordered_data self.youtubeLoader = YoutubeLoader(verbose=False) for key, video in self.dataset: self.youtubeLoader.queue_video(key, video[0][0], video[-1][0]) self.current_video_index = 0 self.current_video_frame_index = 0 self.tracker = Tracker() else: raise Exception("Unknown dataset") self.useLMDB = useLMDB if useLMDB: self.lmdb = LMDBHelper("a", max_size=1028 * 1028 * 1028 * 32) self.lmdb.verbose = False self.optimizer = torch.optim.Adam(self._AE_model.parameters(), lr=learningRate) self.loss_function = torch.nn.BCELoss()