Exemple #1
0
def GenerateBatch(BasePath, DirNamesTrain, TrainLabels, ImageSize,
                  MiniBatchSize):
    """
	Inputs: 
	BasePath - Path to CIFAR10 folder without "/" at the end
	DirNamesTrain - Variable with Subfolder paths to train files
	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 images
	LabelBatch - Batch of one-hot encoded labels 
	"""
    I1Batch = []
    LabelBatch = []

    ImageNum = 0
    while ImageNum < MiniBatchSize:
        # Generate random image
        RandIdx = random.randint(0, len(DirNamesTrain) - 1)

        RandImageName = BasePath + os.sep + DirNamesTrain[RandIdx] + '.png'
        ImageNum += 1
        ##########################################################
        # Add any standardization or data augmentation here!
        ##########################################################
        #Flipping Augmentation
        I1 = np.float32(cv2.imread(RandImageName))
        I1 = iu.StandardizeInputs(I1)
        Label = convertToOneHot(TrainLabels[RandIdx], 10)
        # Append All Images and Mask
        I1Batch.append(I1)
        LabelBatch.append(Label)

        I1Flipped = np.float32(cv2.flip(cv2.imread(RandImageName), 1))
        I1Flipped = iu.StandardizeInputs(I1Flipped)
        I1Batch.append(I1Flipped)
        LabelBatch.append(Label)

        I1noise = np.float32(
            random_noise(cv2.imread(RandImageName), mode='gaussian', var=0.01))
        I1noise = iu.StandardizeInputs(I1noise)
        I1Batch.append(I1noise)
        LabelBatch.append(Label)

    return I1Batch, LabelBatch
def ReadImages(ImageSize, DataPath):
    """
    Inputs: 
    ImageSize - Size of the Image
    DataPath - Paths of all images where testing will be run on
    Outputs:
    I1Combined - I1 image after any standardization and/or cropping/resizing to ImageSize
    I1 - Original I1 image for visualization purposes only
    """
    
    ImageName = DataPath
    
    I1 = cv2.imread(ImageName)
    
    if(I1 is None):
        # OpenCV returns empty list if image is not read! 
        print('ERROR: Image I1 cannot be read')
        sys.exit()
        
    ##########################################################################
    # Add any standardization or cropping/resizing if used in Training here!
    ##########################################################################

    I1S = iu.StandardizeInputs(np.float32(I1))

    I1Combined = np.expand_dims(I1S, axis=0)

    return I1Combined, I1
def ReadImages(ImageSize, DataPath):
    """
    Inputs: 
    ImageSize - Size of the Image
    DataPath - Paths of all images where testing will be run on
    Outputs:
    I1Combined - I1 image after standardization and cropping/resizing to ImageSize
    I1 - Original I1 image for visualization purposes only
    """
    
    ImageName = DataPath
    
    I1 = cv2.imread(ImageName)
    
    if(I1 is None):
        # OpenCV returns empty list if image is not read! Like WTF!
        print('ERROR: Image I1 cannot be read')
        sys.exit()
        
        
    # Always get a random crop to fit the size of the network
    # I1 = iu.RandomCrop(I1, ImageSize)

    # Resize Image to fit size of the network
    # I1 = iu.Resize(I1, ImageSize)
        
    # 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
    I1S = iu.StandardizeInputs(np.float32(I1))

    I1Combined = np.expand_dims(I1S, axis=0)

    return I1Combined, I1
Exemple #4
0
def GenerateBatch(IBuffer, PatchSize):
    """
    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 = []

    # Generate random image
    if(np.shape(IBuffer)[1]>=246):
        IBuffer = np.hsplit(IBuffer, 2)
        I = IBuffer[0]
    else:
        I = IBuffer
    
    # Homography and Patch generation 
    IPatch = I
    # IOriginal, IPatch, AllPts, Mask = GenerateRandPatch(I, PatchSize, Vis=False)
    
    # Normalize Dataset
    # https://stackoverflow.com/questions/42275815/should-i-substract-imagenet-pretrained-inception-v3-model-mean-value-at-inceptio
    IS = iu.StandardizeInputs(np.float32(IPatch))
    
    # Append All Images and Mask
    IBatch.append(IS)

    # IBatch is the Original Image I1 Batch
    return IBatch
Exemple #5
0
def GenerateBatch(DirNamesTrain, TrainLabels, ImageSize, MiniBatchSize,
                  PerEpochCounter):
    """
    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
    """
    I1Batch = []
    LabelBatch = []

    ImageNum = 0
    # RandIdxAll = range(PerEpochCounter*MiniBatchSize,(PerEpochCounter+1)*MiniBatchSize)
    # count = 0
    while ImageNum < MiniBatchSize:
        # Generate random image
        RandIdx = random.randint(0, len(DirNamesTrain) - 1)
        # RandIdx = RandIdxAll[count]

        RandImageName = DirNamesTrain[RandIdx] + '.png'
        RandImageNameWithoutExt = DirNamesTrain[RandIdx]
        RandImageNum = RandImageNameWithoutExt.split('/')
        CurrPath = '/'.join(map(str, RandImageNum[0:-1])) + '/'
        RandImageNum = int(RandImageNum[-1])
        ImageNum += 1

        I1 = cv2.imread(RandImageName)

        # Always get a random crop to fit the size of the network
        # I1 = iu.RandomCrop(I1, ImageSize)

        # Apply a random perturbation
        # PerturbNum = random.randint(0, 5)
        # I1 = PerturbImage(I1, PerturbNum)

        # 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
        I1S = iu.StandardizeInputs(np.float32(I1))
        Label = np.ones(
            OutSize)  #[0:8,0:8#convertToOneHot(TrainLabels[RandIdx], 10)

        # Append All Images and Mask
        I1Batch.append(I1S)
        LabelBatch.append(Label)
        # count += 1

        # print(np.shape(LabelBatch))
        # z = input('z')

    return I1Batch, LabelBatch
Exemple #6
0
def GenerateBatch(TrainNames, PatchSize, MiniBatchSize, BasePath):
    """
    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 = []
    IOrgBatch = []
    AllPtsBatch = []
    IPatchBatch = []
    MaskBatch = []

    ImageNum = 0
    while ImageNum < MiniBatchSize:
        # Generate random image
        RandIdx = random.randint(0, len(TrainNames)-1)        
        RandImageName = BasePath + os.sep + TrainNames[RandIdx] 
        ImageNum += 1
        IBuffer = cv2.imread(RandImageName)
        if(np.shape(IBuffer)[1]>346):
            IBuffer = np.hsplit(IBuffer, 2)
            I = IBuffer[0]
        else:
            I = IBuffer

        # Homography and Patch generation 
        IOriginal, IPatch, AllPts, Mask = GenerateRandPatch(I, PatchSize, Vis=False)

        # Normalize Dataset
        # https://stackoverflow.com/questions/42275815/should-i-substract-imagenet-pretrained-inception-v3-model-mean-value-at-inceptio
        IS = iu.StandardizeInputs(np.float32(IPatch))

        # Append All Images and Mask
        IBatch.append(IS)
        IOrgBatch.append(I)
        AllPtsBatch.append(AllPts)
        IPatchBatch.append(IPatch)
        MaskBatch.append(Mask)

        
    # IBatch is the Original Image I1 Batch
    # IPatchBatch is I1 cropped to patch Size Batch
    # AllPtsBatch is the patch corners in I1 Batch
    # MaskBatch is the active region of I1Patch in I1 Batch
    return IBatch, IOrgBatch, AllPtsBatch, IPatchBatch, MaskBatch
def GenerateBatch(IBuffer, Rho, PatchSize, CropType, Vis=False):
    """
    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 = []
    AllPtsBatch = []
    PerturbPtsBatch = []
    H4PtColBatch = []
    MaskBatch = []

    # Generate random image
    if (np.shape(IBuffer)[1] > 346):
        IBuffer = np.hsplit(IBuffer, 2)
        I1 = IBuffer[0]
    else:
        I1 = IBuffer

    # Homography and Patch generation
    IOriginal, I1Patch, I2Patch, AllPts, PerturbPts,\
    H4PtCol, Mask = GenerateRandPatch(I1, Rho, PatchSize, CropType, Vis=Vis) # Rand Patch will take the whole image as it doesn't have a choice
    ICombo = np.dstack((I1Patch, I2Patch))

    # Normalize Dataset
    # https://stackoverflow.com/questions/42275815/should-i-substract-imagenet-pretrained-inception-v3-model-mean-value-at-inceptio
    IS = iu.StandardizeInputs(np.float32(ICombo))

    # Append All Images and Mask
    IBatch.append(IS)
    I1Batch.append(I1Patch)
    I2Batch.append(I2Patch)
    AllPtsBatch.append(AllPts)
    PerturbPtsBatch.append(PerturbPts)
    H4PtColBatch.append(H4PtCol)
    MaskBatch.append(MaskBatch)

    # IBatch is the Original Image I1 Batch
    return IBatch, I1Batch, I2Batch, AllPtsBatch, PerturbPtsBatch, H4PtColBatch, MaskBatch
Exemple #8
0
    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
Exemple #9
0
    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 ReadImages(ImageSize, DataPath):
    """
    Inputs: 
    ImageSize - Size of the Image
    DataPath - Paths of all images where testing will be run on
    Outputs:
    I1Combined - I1 image after any standardization and/or cropping/resizing to ImageSize
    I1 - Original I1 image for visualization purposes only
    """

    ImageName = DataPath
    Image = cv2.imread(ImageName)
    Gray = cv2.cvtColor(Image, cv2.COLOR_BGR2GRAY)

    if (Image is None):
        # OpenCV returns empty list if image is not read!
        print('ERROR: Image cannot be read')
        sys.exit()

    Tau = min(ImageSize[0], ImageSize[1]) / 3
    # generate label
    Label = np.random.randint(2 * Tau, size=8) - Tau

    # get corner correspondences
    Corners, Warped = iu.getCorrespondence(Image.shape, Label, ImageSize)

    # get forward and backward homographies
    CroppedPatch, WarpedPatch = iu.getWarpingPair(Gray, ImageSize, Corners,
                                                  Warped)
    I1 = np.float32(np.stack([CroppedPatch, WarpedPatch], -1))

    #cv2.imshow("Image", Image)
    #cv2.imshow("Warped Patch", WarpedPatch)
    #cv2.imshow("Cropped Patch", CroppedPatch)

    I1S = iu.StandardizeInputs(np.float32(I1))

    I1Combined = np.expand_dims(I1S, axis=0)

    return I1Combined, Image, Label, Corners
Exemple #11
0
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