def __getitem__(self, index): ImageName = self.img_files[index] I = cv2.imread(ImageName) I = cv2.resize(I, (640, 640), interpolation=cv2.INTER_AREA) #if(not np.shape(I2)): # return #cv2.imshow("I", I) #cv2.waitKey() I, ImageSize = iu.CenterCropFactor(I, self.Factor) IS = iu.StandardizeInputs(np.float32(I)) Image = np.transpose(IS, (2, 0, 1)) MaskName = ImageName.replace("img", "mask") #Image = cv2.imread(ImageName) #RandImagePairName = self.img_files[index+1] #Image, ImageSize = iu.CenterCropFactor(Image, self.Factor) #Image = iu.StandardizeInputs(np.float32(Image)) mask = cv2.imread(MaskName) if (not np.shape(mask)): mask = np.zeros(ImageSize, dtype=np.int) mask = cv2.resize(mask, (640, 640), interpolation=cv2.INTER_AREA) else: mask = cv2.resize(mask, (640, 640), interpolation=cv2.INTER_AREA) #cv2.imshow("mask", mask) #cv2.waitKey() mask, _ = iu.CenterCropFactor(mask, self.Factor) mask = np.float32(mask[:, :, 0]) / 255.0 mask = np.expand_dims(mask, axis=2) mask = np.dstack((mask, 1.0 - mask)) #transform = transforms.Compose([transforms.ToTensor(), \ # transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))]) return Image, mask
def __getitem__(self, index): ImageName = self.img_files[index] #ImagePairName = self.img_files[index+1] I = cv2.imread(ImageName) #I2 = cv2.imread(ImagePairName) #if(not np.shape(I2)): # return I, ImageSize = iu.CenterCropFactor(I, self.Factor) #I2, _ = iu.CenterCropFactor(I2, self.Factor) #ICombined = np.dstack((I1, I2)) # 沿第3维组合 IS = iu.StandardizeInputs(np.float32(I)) Image = np.transpose(IS, (2, 0, 1)) MaskName = ImageName.replace("img", "mask") #Image = cv2.imread(ImageName) #RandImagePairName = self.img_files[index+1] #Image, ImageSize = iu.CenterCropFactor(Image, self.Factor) #Image = iu.StandardizeInputs(np.float32(Image)) mask = cv2.imread(MaskName) if (not np.shape(mask)): mask = np.zeros(ImageSize, dtype=np.int) #Mask2Name = ImagePairName.replace("img", "mask") #mask2 = cv2.imread(Mask2Name) #if (not np.shape(mask2)): # mask2 = np.zeros(ImageSize, dtype=np.int) mask, _ = iu.CenterCropFactor(mask, self.Factor) mask = np.float32(mask[:, :, 0]) / 255.0 mask = np.expand_dims(mask, axis=2) mask = np.dstack((mask, 1.0 - mask)) #transform = transforms.Compose([transforms.ToTensor(), \ # transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))]) return Image, mask
def SetupAll(BasePath, LearningRate): """ Inputs: BasePath is the base path where Images are saved without "/" at the end Outputs: DirNames - Full path to all image files without extension Train/Val/Test - Idxs of all the images to be used for training/validation (held-out testing in this case)/testing Ratios - Ratios is a list of fraction of data used for [Train, Val, Test] CheckPointPath - Path to save checkpoints/model OptimizerParams - List of all OptimizerParams: depends on Optimizer SaveCheckPoint - Save checkpoint every SaveCheckPoint iteration in every epoch, checkpoint saved automatically after every epoch ImageSize - Size of the image NumTrain/Val/TestSamples - length(Train/Val/Test) NumTestRunsPerEpoch - Number of passes of Val data with MiniBatchSize Train/Val/TestLabels - Labels corresponding to Train/Val/Test """ # Setup DirNames DirNamesPath = BasePath + os.sep + 'DirNames.txt' LabelNamesPath = BasePath + os.sep + 'Labels.txt' TrainPath = BasePath + os.sep + 'Train.txt' DirNames, TrainNames, TrainLabels = ReadDirNames(DirNamesPath, LabelNamesPath, TrainPath) # Setup Neural Net Params # List of all OptimizerParams: depends on Optimizer # For ADAM Optimizer: [LearningRate, Beta1, Beta2, Epsilion] UseDefaultFlag = 0 # Set to 0 to use your own params, do not change default parameters if UseDefaultFlag: # Default Parameters OptimizerParams = [1e-3, 0.9, 0.999, 1e-8] else: # Custom Parameters OptimizerParams = [LearningRate, 0.9, 0.999, 1e-8] # Save checkpoint every SaveCheckPoint iteration in every epoch, checkpoint saved automatically after every epoch SaveCheckPoint = 100 # Image Input Shape Factor = 3 I = cv2.imread(BasePath + os.sep + TrainNames[0]) _, ImageSize = iu.CenterCropFactor(I, Factor) NumTrainSamples = len(TrainNames) return TrainNames, TrainLabels, OptimizerParams, SaveCheckPoint, Factor, ImageSize, NumTrainSamples
def GenerateBatch(TrainNames, TrainLabels, Factor, ImageSize, MiniBatchSize, BasePath, MaxFrameDiff): """ Inputs: DirNames - Full path to all image files without extension NOTE that Train can be replaced by Val/Test for generating batch corresponding to validation (held-out testing in this case)/testing TrainLabels - Labels corresponding to Train NOTE that TrainLabels can be replaced by Val/TestLabels for generating batch corresponding to validation (held-out testing in this case)/testing ImageSize - Size of the Image MiniBatchSize is the size of the MiniBatch Outputs: I1Batch - Batch of I1 images after standardization and cropping/resizing to ImageSize HomeVecBatch - Batch of Homing Vector labels """ IBatch = [] I1Batch = [] I2Batch = [] LabelBatch = [] ImageNum = 0 while ImageNum < MiniBatchSize: # Generate random image RandIdx = random.randint(0, len(TrainNames) - MaxFrameDiff) RandFrameDiff = random.randint(1, MaxFrameDiff) RandImageName = TrainNames[RandIdx] # Create File Number in same folder with RandFrameDiff RandImagePairName = RandImageName.split( os.sep)[0] + os.sep + 'events' + os.sep + 'event_' + str( int(re.split('_|.png', RandImageName)[-2]) + RandFrameDiff) I2 = cv2.imread(BasePath + os.sep + RandImagePairName + '.png') if (not np.shape(I2) ): # OpenCV returns empty matrix if no image is found! continue # Retry if RandImagePair is not valid! I1 = cv2.imread(BasePath + os.sep + RandImageName) ImageNum += 1 I1, _ = iu.CenterCropFactor(I1, Factor) I2, _ = iu.CenterCropFactor(I2, Factor) ICombined = np.dstack((I1, I2)) # Standardize Inputs as given by Inception v3 paper # MAYBE: Find Mean of Dataset or use from ImageNet # MAYBE: Normalize Dataset # https://stackoverflow.com/questions/42275815/should-i-substract-imagenet-pretrained-inception-v3-model-mean-value-at-inceptio IS = iu.StandardizeInputs(np.float32(ICombined)) Label1 = cv2.imread(BasePath + os.sep + TrainLabels[RandIdx]) Label1Name = TrainLabels[RandIdx] Label2Name = Label1Name.split( os.sep)[0] + os.sep + 'masks' + os.sep + 'mask_' + '%08d.png' % ( int(re.split('_|.png', RandImageName)[-2]) + RandFrameDiff ) # 08 Label2 = cv2.imread(BasePath + os.sep + Label2Name) LabelCropped, _ = iu.CenterCropFactor( Label1 | Label2, Factor) # Label Mask is the logical OR of both Masks LabelCropped = np.float32(LabelCropped[:, :, 0]) / 255.0 LabelCropped = np.expand_dims(LabelCropped, axis=3) LabelCropped = np.dstack((LabelCropped, 1.0 - LabelCropped)) # Append All Images and Mask IBatch.append(IS) I1Batch.append(I1) I2Batch.append(I2) LabelBatch.append(LabelCropped) return IBatch, I1Batch, I2Batch, LabelBatch