Exemplo n.º 1
0
    def run(self):
        while not self.end:
            while self.batch_data_length >= self.max_data_size:
                continue
            
            batch_image_data = np.zeros((BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNEL), dtype = np.float32)
            batch_label_data = np.zeros((BATCH_SIZE, CLASSES), dtype = np.float32)
            batch_data = random.sample(self.data_list, self.batch_size)

            for index, data in enumerate(batch_data):
                image_path, label = data

                image = cv2.imread(image_path)
                image = DataAugmentation(image)
                image = cv2.resize(image, (IMAGE_WIDTH, IMAGE_HEIGHT))

                batch_image_data[index] = image.astype(np.float32)
                batch_label_data[index] = one_hot(label, CLASSES)
            
            self.batch_data_list.append([batch_image_data, batch_label_data])
            self.batch_data_length += 1

            if self.batch_data_length >= self.min_data_size:
                self.ready = True
            else:
                self.ready = False
Exemplo n.º 2
0
def makeAllSiftedDfList(basicUnitDf, iterNum=1, mu=0, sigma=0.1):
    DataAugmentationObject = DataAugmentation(basicUnitDf)
    siftedDfList = [basicUnitDf]
    for i in range(5, len(DataAugmentationObject.getFuncList()) + 1):
        siftedDfList.append(
            DataAugmentationObject.applySiftFuncWithIteration(
                i, iterNum, mu, sigma))
    return siftedDfList
Exemplo n.º 3
0
    def run(self):
        while True:
            while self.batch_data_length >= self.max_data_size:
                continue
            
            batch_image_data = []
            batch_encode_bboxes = []
            batch_encode_classes = []

            batch_indexs = random.sample(self.total_indexs, BATCH_SIZE * 2)

            for data in self.total_data_list[batch_indexs]:
                image_name, gt_bboxes, gt_classes = data
                
                image_path = TRAIN_DIR + image_name
                gt_bboxes = np.asarray(gt_bboxes, dtype = np.float32)
                gt_classes = np.asarray([CLASS_DIC[c] for c in gt_classes], dtype = np.int32)

                image = cv2.imread(image_path)
                image, gt_bboxes, gt_classes = DataAugmentation(image, gt_bboxes, gt_classes)

                image_h, image_w, image_c = image.shape
                image = cv2.resize(image, (IMAGE_WIDTH, IMAGE_HEIGHT), interpolation = cv2.INTER_CUBIC)

                gt_bboxes = gt_bboxes.astype(np.float32)
                gt_classes = np.asarray(gt_classes, dtype = np.int32)
                
                gt_bboxes /= [image_w, image_h, image_w, image_h]
                gt_bboxes *= [IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_HEIGHT]

                encode_bboxes, encode_classes = self.retina_utils.Encode(gt_bboxes, gt_classes)

                batch_image_data.append(image.astype(np.float32))
                batch_encode_bboxes.append(encode_bboxes)
                batch_encode_classes.append(encode_classes)

                if len(batch_image_data) == BATCH_SIZE:
                    break
            
            batch_image_data = np.asarray(batch_image_data, dtype = np.float32) 
            batch_encode_bboxes = np.asarray(batch_encode_bboxes, dtype = np.float32)
            batch_encode_classes = np.asarray(batch_encode_classes, dtype = np.float32)
            
            self.batch_data_list.append([batch_image_data, batch_encode_bboxes, batch_encode_classes])
            self.batch_data_length += 1

            if self.debug:
                print('[D] stack = [{}/{}]'.format(self.batch_data_length, self.max_data_size))

            if self.batch_data_length >= self.min_data_size:
                self.ready = True
            else:
                self.ready = False
Exemplo n.º 4
0
def get_data(xml_path, training, normalize = True, augment = True):
    if training:
        image_path, gt_bboxes, gt_classes = xml_read(xml_path, normalize = False)

        image = cv2.imread(image_path)
        
        if augment:
            image, gt_bboxes, gt_classes = DataAugmentation(image, gt_bboxes, gt_classes)
        
        image_h, image_w, image_c = image.shape
        image = cv2.resize(image, (IMAGE_WIDTH, IMAGE_HEIGHT), interpolation = cv2.INTER_CUBIC)

        gt_bboxes = gt_bboxes.astype(np.float32)
        gt_classes = np.asarray(gt_classes, dtype = np.int32)

        if normalize:
            gt_bboxes /= [image_w, image_h, image_w, image_h]
            gt_bboxes *= [IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_HEIGHT]

        # print(image.shape)
        # print(np.min(gt_bboxes), np.max(gt_bboxes), image.shape)

        # for bbox in gt_bboxes:
        #     print(bbox)
        #     xmin, ymin, xmax, ymax = (bbox * [IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_HEIGHT]).astype(np.int32)
        #     cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)

        # cv2.imshow('show', image)
        # cv2.waitKey(0)
    else:
        image_path, gt_bboxes, gt_classes = xml_read(xml_path, normalize = normalize)
        image = cv2.imread(image_path)

    return image, gt_bboxes, gt_classes
Exemplo n.º 5
0
def main():
    ImagePath = "./busesAug"
    AnnotationFile = "./annotationsTestAug.txt"
    OutImgPath = "./BusesOnly"
    pDV = DataViewer.CDataViewer()
    pDA = DataAugmentation.CDataAugmentation()
    OsUtils.remove(OutImgPath)
    os.mkdir(OutImgPath)
    for label in range(1, 7):
        os.mkdir(OutImgPath + "\\" + str(label))
    ImageList = pDV.LoadImages(ImagePath)
    DetectedObjects = pDV.LoadAnnotations(AnnotationFile)
    print("extracting data")
    for ImageFullPathFile in ImageList:
        FileName = os.path.basename(ImageFullPathFile)
        BoxList = DetectedObjects[FileName]
        if BoxList:
            #pDV.PlotBoxes(ImageFullPathFile,BoxList)
            pDV.SaveBoxesAsImages(ImageFullPathFile, BoxList, OutImgPath)
        pass
    pass
    print("finished extracting data")
    print("begining data augmentation")
    #pDA.rotate(OutImgPath, OutImgPath)
    #pDA.flip(OutImgPath,OutImgPath)
    print("finished data augmentation")
Exemplo n.º 6
0
    def get_data(self, json_path):
        image_path = json_path.replace('/json/', '/image/')[:-4] + 'jpg'

        image = cv2.imread(image_path)
        image = DataAugmentation(image)
        image = cv2.resize(image, (IMAGE_WIDTH, IMAGE_HEIGHT))
        image_data = image.astype(np.float32)

        labels = [
            self.class_dic[tag_name]
            for tag_name in get_json_data(json_path)['Tags']
        ]

        label_topk_data = one_hot(min(len(labels) - 1, MAX_TOP_K - 1),
                                  MAX_TOP_K)
        label_conf_data = one_hot_multi(labels, CLASSES)

        return image_data, label_topk_data, label_conf_data
Exemplo n.º 7
0
    def __init__(self, input_shape, args):

        self.batch_size = args.batch_size
        self.input_width = input_shape[0]
        self.input_height = input_shape[1]
        self.num_workers = args.num_workers
        self.buffer_size = args.buffer_size

        self.resize_function = Resizer.ResizerSimple(
            self.input_width,
            self.input_height).get_resize_func(args.resize_method)

        self.percent_of_data = args.percent_of_data
        self.max_image_size = args.max_image_size
        self.nimages_train = None
        self.nimages_val = None
        self.train_init_op = None
        self.val_init_op = None
        self.dirdata = os.path.join(args.root_of_datasets, args.dataset_name)
        self.img_extension, self.classnames = tools.process_dataset_config(
            os.path.join(self.dirdata, 'dataset_info.xml'))
        self.img_extension = '.' + self.img_extension
        self.nclasses = len(self.classnames)
        self.outdir = args.outdir
        self.write_network_input = args.write_network_input

        self.shuffle_data = args.shuffle_data

        if self.img_extension == '.jpg' or self.img_extension == '.JPEG':
            self.parse_function = parse_jpg
        elif self.img_extension == '.png':
            self.parse_function = parse_png
        else:
            raise Exception('Images format not recognized.')

        self.data_aug_opts = args.data_aug_opts

        if self.data_aug_opts.apply_data_augmentation:
            bugs_class_id = -1
            for i in range(len(self.classnames)):
                if self.classnames[i] == 'BUGS':
                    bugs_class_id = i
                    break
            data_augmenter = DataAugmentation.ClassificationDataAugmentation(
                args, self.input_width, self.input_height, bugs_class_id)
            self.data_aug_func = data_augmenter.data_augmenter
        return
Exemplo n.º 8
0
    def __init__(self, input_shape, opts, single_cell_arch):

        super(TrainDataReader,
              self).__init__(opts, opts.single_cell_opts.n_images_per_batch)

        self.single_cell_arch = single_cell_arch
        self.n_crops_per_image = opts.single_cell_opts.n_crops_per_image
        self.input_width = input_shape[0]
        self.input_height = input_shape[1]
        self.num_workers = opts.num_workers
        self.buffer_size = opts.buffer_size
        self.preprocessor = Preprocessor.Preprocessor(self.input_width,
                                                      self.input_height)

        self.nimages_train = None
        self.nimages_val = None
        self.train_init_op = None
        self.val_init_op = None

        self.outdir = opts.outdir
        self.hard_negatives_dir = os.path.join(self.outdir, 'hard_negatives')
        # self.hard_negatives_dir = '/home/xian/crodnet/experiments/2019/2019_03_12_13/hard_negatives'

        self.image_cropper = ImageCropper.ImageCropper(
            opts.image_cropper_opts, self.single_cell_arch,
            opts.single_cell_opts.n_crops_per_image)

        if self.img_extension == '.jpg' or self.img_extension == '.JPEG':
            self.parse_function = parse_jpg
        elif self.img_extension == '.png':
            self.parse_function = parse_png
        else:
            raise Exception('Images format not recognized.')

        self.data_aug_opts = opts.data_aug_opts

        if self.data_aug_opts.apply_data_augmentation:
            data_augmenter = DataAugmentation.DataAugmentation(
                opts, self.input_width, self.input_height)
            self.data_aug_func = data_augmenter.data_augmenter

        return
Exemplo n.º 9
0
def firsttime():
    #อัดเสียงและสร้างโฟล์เดอร์
    print("don't have model ...")
    print('[Make Directory] Running ... ')
    checkdir.mkpath('Model')
    print('[Make Directory] Directory Model is Created')
    mkdir_n_rec('Audio', 'Help', 'True', 'False', '"ช่วยด้วย"',
                'อะไรก็ได้ เช่น "อาบน้ำ" "น้ำไม่ไหล" เป็นต้น หรือ ไม่พูด')
    print('[Make Directory] Directory and Record Help is Created')
    mkdir_n_rec('Audio', 'Confirm1', 'True', 'False', '"ใข่"', '"ไม่ใช่"')
    print('[Make Directory] Directory and Record Confirm1 is Created')
    mkdir_n_rec('Audio', 'Confirm2', 'True', 'False', '"โอเค"', '"ไม่ต้อง"')
    print('[Make Directory] Directory and Record Confirm2 is Created')
    print('[Make Directory] Finished... ')
    #checkdir('Audio','rmSilenceHelp','True','False')

    #ตัดเสียงเงียบของไฟล์เสียงที่อัด
    print('[Remove Silence in Audio] Running ... ')
    removeSilence.rmSilence('Audio/Help', 'Audio/rmSilenceHelp')
    removeSilence.rmSilence('Audio/Confirm1', 'Audio/rmSilenceConfirm1')
    removeSilence.rmSilence('Audio/Confirm2', 'Audio/rmSilenceConfirm2')
    print('[Remove Silence in Audio] Finished ... ')

    #Normalize Audio
    print('[Normalize Audio] Running ... ')
    NormalizeSound.normalizeAudio('Audio/rmSilenceHelp',
                                  'Audio/normalizedHelp')
    NormalizeSound.normalizeAudio('Audio/rmSilenceConfirm1',
                                  'Audio/normalizedConfirm1')
    NormalizeSound.normalizeAudio('Audio/rmSilenceConfirm2',
                                  'Audio/normalizedConfirm2')
    print('[Normalize Audio] Finished ... ')

    #ปรับขนาดข้อมูลเสียงให้พอดีกับ input ของโมเดลก่อนทำ Data Augmentation เนื่องจากบางขั้นตอนต้องการไฟล์เสียงที่มีขนาดยาวขึ้นเช่น Shifting time หาก Audio มีขนาดสั้นการ Shift จะ Shift ไกลจนทำให้เสียงหาย
    print('[Split Audio] Running ... ')
    splitAudio('Audio/normalizedHelp', 'Audio/cleanHelp')
    splitAudio('Audio/normalizedConfirm1', 'Audio/cleanConfirm1')
    splitAudio('Audio/normalizedConfirm2', 'Audio/cleanConfirm2')
    print('[Split Audio] Finished ... ')

    #Data Augmentation
    print('[Data Augmentation] Running ... ')
    print('[Data Augmentation] Creating more Help Voice ... ')
    DataAugmentation.dataAugmentation('Audio/cleanHelp',
                                      'Audio/dataAugmentationHelp')
    print('[Data Augmentation] Help Voice is Created... ')
    print('[Data Augmentation] Creating more Confirm1 Voice ... ')
    DataAugmentation.dataAugmentation('Audio/cleanConfirm1',
                                      'Audio/dataAugmentationConfirm1')
    print('[Data Augmentation] Confirm1 Voice is Created... ')
    print('[Data Augmentation] Creating more Confirm2 Voice ... ')
    DataAugmentation.dataAugmentation('Audio/cleanConfirm2',
                                      'Audio/dataAugmentationConfirm2')
    print('[Data Augmentation] Confirm2 Voice is Created... ')
    print('[Data Augmentation] Finished ... ')

    #ปรับขนาดข้อมูลเสียงให้พอดีกับ input ของโมเดลอีกครั้งหลังเนื่องจากการทำ Data Augmentation ในบางขั้นตอนทำให้ขนาดของไฟล์ Audio เปลี่ยนไป
    print('[Split Audio] Running ... ')
    splitAudio('Audio/dataAugmentationHelp', 'Audio/cleanDataAug_Help')
    splitAudio('Audio/dataAugmentationConfirm1', 'Audio/cleanDataAug_Confirm1')
    splitAudio('Audio/dataAugmentationConfirm2', 'Audio/cleanDataAug_Confirm2')
    print('[Split Audio] Finished ... ')

    print('[Train Model] Running ... ')
    print('[Train Model] Training Help Model ... ')
    data_Generator('Audio/cleanDataAug_Help', 'Model/RNN_Help_Model.h5')
    print('[Train Model] Training Confirm1 Model ... ')
    data_Generator('Audio/cleanDataAug_Confirm1',
                   'Model/RNN_Confirm1_Model.h5')
    print('[Train Model] Training Confirm2 Model ... ')
    data_Generator('Audio/cleanDataAug_Confirm2',
                   'Model/RNN_Confirm2_Model.h5')
    print('[Train Model] Finished ... ')
Exemplo n.º 10
0
    def Encode(self,
               image_path,
               gt_bboxes,
               gt_classes,
               input_size,
               augment=False):
        # input_size = random.choice(self.input_sizes)
        output_sizes = input_size // self.strides

        image = cv2.imread(image_path)
        if augment:
            image, gt_bboxes, gt_classes = DataAugmentation(
                image, gt_bboxes, gt_classes)

        image_h, image_w, c = image.shape

        image = cv2.resize(image, (input_size, input_size),
                           interpolation=cv2.INTER_CUBIC)
        labels = [
            np.zeros((output_size, output_size, 3, 5 + CLASSES))
            for output_size in output_sizes
        ]

        # normalize
        gt_bboxes /= [image_w, image_h, image_w, image_h]
        gt_bboxes *= input_size

        for gt_bbox, gt_class in zip(gt_bboxes, gt_classes):
            ccwh_bbox = xyxy_to_ccwh(gt_bbox)
            onehot = smooth_one_hot(gt_class, CLASSES)

            positive_count = 0

            best_max_iou = 0.0
            best_label_index = 0
            best_anchor_index = 0

            for i in range(3):
                anchors = np.zeros((3, 4), dtype=np.float32)
                anchors[:, :2] = ccwh_bbox[:2]
                anchors[:, 2:] = self.anchors[i]

                # anchors (cx, cy, w, h -> left, top, right, bottom)
                anchors[:, :2], anchors[:, 2:] = anchors[:, :2] - anchors[:, 2:] / 2, \
                                                 anchors[:, :2] + anchors[:, 2:] / 2

                ious = compute_bboxes_IoU(anchors, gt_bbox[np.newaxis, :])[:,
                                                                           0]
                mask = ious >= IOU_THRESHOLD

                if positive_count == 0:
                    max_iou = np.max(ious)
                    max_index = np.argmax(ious)

                    if max_iou > best_max_iou:
                        best_max_iou = max_iou
                        best_label_index = i
                        best_anchor_index = max_index

                x_index, y_index = (ccwh_bbox[:2] / STRIDES[i]).astype(
                    np.int32)

                labels[i][y_index, x_index, mask, :4] = gt_bbox
                labels[i][y_index, x_index, mask, 4] = 1
                labels[i][y_index, x_index, mask, 5:] = onehot

                positive_count += np.sum(mask)

            if positive_count == 0:
                x_index, y_index = (ccwh_bbox[:2] /
                                    STRIDES[best_label_index]).astype(np.int32)
                labels[best_label_index][y_index, x_index,
                                         best_anchor_index, :4] = gt_bbox
                labels[best_label_index][y_index, x_index, best_anchor_index,
                                         4] = 1
                labels[best_label_index][y_index, x_index, best_anchor_index,
                                         5:] = onehot

        slabel_data = labels[0].reshape((-1, 5 + CLASSES))
        mlabel_data = labels[1].reshape((-1, 5 + CLASSES))
        llabel_data = labels[2].reshape((-1, 5 + CLASSES))
        label_data = np.concatenate([slabel_data, mlabel_data, llabel_data],
                                    axis=0)

        return image, label_data
Exemplo n.º 11
0
accuracy_list = []
train_time = time.time()

for iter in range(1, MAX_ITERATION + 1):
    if iter in DECAY_ITERATION:
        learning_rate /= 10.

    np.random.shuffle(train_data_list)
    batch_data_list = train_data_list[:BATCH_SIZE]

    batch_x_image_list = []
    batch_x_label_list = []

    for x_data in batch_data_list:
        image, label = x_data
        image = DataAugmentation(image)

        batch_x_image_list.append(image)
        batch_x_label_list.append(smooth_one_hot(label, CLASSES))

    batch_x_image_list = np.asarray(batch_x_image_list, dtype=np.float32)
    batch_x_label_list = np.asarray(batch_x_label_list, dtype=np.float32)

    _feed_dict = {
        x_var: batch_x_image_list,
        x_label_var: batch_x_label_list,
        is_training: True,
        learning_rate_var: learning_rate
    }

    _, loss, x_loss, l2_reg_loss, accuracy, summary = sess.run(
scriptPath = os.getcwd() + '/'

trainDatapath = '/home/liuzheng/competition/kaggle/distractedDrivers/imgs/train/'
testDatapath = '/home/liuzheng/competition/kaggle/distractedDrivers/imgs/test/'
trainFolderList = ['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9']
classNum = len(trainFolderList)

trainSavepath = '/home/liuzheng/competition/kaggle/distractedDrivers/imgs/trainAugmentation/'
testSavepath = '/home/liuzheng/competition/kaggle/distractedDrivers/imgs/testAugmentation/'

reSize = [64,64]

'''============================= Processing training data ======================================='''
for c in range(classNum):
    classDatapath = trainDatapath+trainFolderList[c]+'/'
    da.augmentTrainingImages_distractedDrivers(datapath=classDatapath, classIdx=c,\
								savepath=trainSavepath, reSize=reSize)

print('making training data list...')
mdl.makeTrainList_distractedDrivers(datapath=trainSavepath, prefix=scriptPath, listName=trainListName)

os.system(mxnetRoot+'bin/im2rec '+scriptPath+trainListName+' '+trainSavepath+' '+pcProjectpath+trainRecName)
'''=============================================================================================='''

'''============================= Processing testing data ======================================='''
da.augmentTestingImages_distractedDrivers(datapath=testDatapath, savepath=testSavepath, reSize=reSize)

print('making testing data list...')
mdl.makeTestList_distractedDrivers(datapath=testSavepath, prefix=scriptPath, listName=testListName)

os.system(mxnetRoot+'bin/im2rec '+scriptPath+testListName+' '+testSavepath+' '+pcProjectpath+testRecName)
'''=============================================================================================='''
Exemplo n.º 13
0
        return pred_bboxes, pred_classes


if __name__ == '__main__':
    total_data_list = np.load('./dataset/train.npy', allow_pickle=True)
    yolov1_utils = YOLOv1_Utils()

    for data in total_data_list:
        image_name, gt_bboxes, gt_classes = data

        image_path = ROOT_DIR + image_name
        gt_bboxes = np.asarray(gt_bboxes, dtype=np.float32)
        gt_classes = np.asarray(gt_classes, dtype=np.int32)

        image = cv2.imread(image_path)
        image, gt_bboxes, gt_classes = DataAugmentation(
            image, gt_bboxes, gt_classes)

        image_h, image_w, image_c = image.shape
        gt_bboxes /= [image_w, image_h, image_w, image_h]
        gt_bboxes *= [IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_HEIGHT]

        label_data = yolov1_utils.Encode(gt_bboxes, gt_classes)
        pred_bboxes, pred_classes = yolov1_utils.Decode(
            label_data, size=[image_w, image_h], use_nms=True)

        print(len(gt_bboxes), len(pred_bboxes),
              len(gt_bboxes) - len(pred_bboxes))

        for bbox, class_index in zip(pred_bboxes, pred_classes):
            xmin, ymin, xmax, ymax = bbox[:4].astype(np.int32)
            conf = bbox[4]
Exemplo n.º 14
0
SAMPLE = np.array(DATA["STANDUP_CHAIR"][0][:3])

"""##**Jittering**

**Hyper Parameters = SIGMA**
"""

SIGMA = 0.5

print("NO AUGMENT")
graph_numpy(SAMPLE)

fig = plt.figure(figsize=(30,10))
for ii in range(8):
    ax = fig.add_subplot(2,4,ii+1)
    rotated = (DA.DA_Jitter(SAMPLE.transpose(), SIGMA))
    ax.plot(rotated[:,0], label ='x')
    ax.plot(rotated[:,1], label ='y')
    ax.plot(rotated[:,2], label ='z')
    ax.legend(loc='best', prop={"size":16})

"""* Increasing **SIGMA** will increase the amount of jittering in the trace

Use ```preprocess_with_augemtation``` to get a list of all the traces with the desired augments:
"""

standup_chair_JITTERED = EXTK.preprocess_add_augmentation(DATA["STANDUP_CHAIR"], DA.DA_Jitter, SIGMA)

graph_numpy(standup_chair_JITTERED[0])

"""##**Time Warping**
trainFolderList = ['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9']
classNum = len(trainFolderList)

trainSavepath = '/home/liuzheng/competition/kaggle/distractedDrivers/imgs/trainAugmentation_'+colorMode + '_' + cropMode+'_'+'%03d'%(reSize[0])+'/'
if not os.path.exists(trainSavepath):
    os.mkdir(trainSavepath)
testSavepath = '/home/liuzheng/competition/kaggle/distractedDrivers/imgs/testAugmentation_'+colorMode + '_' + cropMode+'_'+'%03d'%(reSize[0])+'/'
if not os.path.exists(testSavepath):
    os.mkdir(testSavepath)


# cropSize=[224,224]
'''============================= Processing training data ======================================='''
for c in range(classNum):
    classDatapath = trainDatapath+trainFolderList[c]+'/'
    da.augmentTrainingImages(datapath=classDatapath, classIdx=c, savepath=trainSavepath, reSize=reSize,\
                            needGray=False, needMirror=False, needRotate=False)

'''=============================================================================================='''

'''============================= Processing testing data ======================================='''
da.augmentTestingImages(datapath=testDatapath, savepath=testSavepath, reSize=reSize,\
                            needGray=False, needMirror=False, needRotate=False)
'''=============================================================================================='''

#'''============================= fragment data for insufficient memory ======================================='''
#da.fragmentTrainingData(trainSavepath, imgSize=cropSize, colorMode=colorMode, fragNum=10)
#da.fragmentTestingData(testSavepath, imgSize=cropSize, colorMode=colorMode, fragNum=10)



def Training():
    #    ckpt_dir = './fb_model/'
    #    log_dir = './fb_log/'
    #    save_dir = 'fb_model/checkpoint'

    global merged, merged_unfixed
    save_dir = 'model/checkpoint'
    save_unfixed_dir = './unfixed_model/checkpoint'
    ckpt_dir = './model'
    ckpt_unfixed_dir = './unfixed_model'
    log_dir = './log/'

    if not os.path.exists(ckpt_dir):
        os.makedirs(ckpt_dir)
    if not os.path.exists(ckpt_unfixed_dir):
        os.makedirs(ckpt_unfixed_dir)

    # read data
    train_data, train_labels = rc.Load_traindata()
    test_data, test_labels = rc.Load_testdata()
    #    train_data = rc.norm_std_each(train_data)
    test_data = rc.norm_std_each(test_data)

    print('read data')

    train_size = train_data.shape[0]
    max_iteration = int(num_enpoch * (train_size // batch_size))

    # graph
    sess, LR, is_training, x_image, y_label, drop_keepProb, y_predict, loss, accuracy, opt_paras, fin_filters, is_fixed, featuremap = build_graph(
    )

    # save model
    saver = tf.train.Saver()
    saver = tf.train.Saver(write_version=saver_pb2.SaverDef.V1)

    merged_all = tf.summary.merge_all()
    # summary_writer = tf.summary.FileWriter(log_dir, sess.graph)
    writer_fixed = tf.summary.FileWriter("./log/plot_fixed", sess.graph)
    writer_unfixed = tf.summary.FileWriter("./log/plot_unfixed", sess.graph)

    sess.run(tf.global_variables_initializer())
    model_restore(saver, sess, save_dir)

    # Training
    accuracy_pre = 0
    accuracy_pre_unfixed = 0
    test_len = 100
    for iter_i in range(65000):
        # tes:
        if iter_i % test_len == 0 and iter_i >= 3000:
            accuracy_temp = 0
            for i in range(5):
                batch_x, batch_y = rc.Get_testbatch(test_data, test_labels,
                                                    2000, i)
                feed_dict_test = {
                    x_image: batch_x,
                    y_label: batch_y,
                    drop_keepProb: 1.0,
                    is_training: False,
                    is_fixed: True
                }
                accuracy_predict, merged = sess.run([accuracy, merged_all],
                                                    feed_dict=feed_dict_test)
                # if i == 0:
                #     filters = sess.run([fin_filters], feed_dict=feed_dict_test)
                #     print(filters)
                accuracy_temp += accuracy_predict
            #                print('batch accuracy: %g'% accuracy_predict)

            accuracy_predict = accuracy_temp / 5.0
            writer_fixed.add_summary(merged, iter_i)
            print('fixed test C accuracy: %g,Max accuracy: %g' %
                  (accuracy_predict, accuracy_pre))

            if accuracy_predict > accuracy_pre:
                saver.save(sess,
                           os.path.join(ckpt_dir, "model_share.ckpt"),
                           global_step=iter_i)
                accuracy_pre = accuracy_predict
                print('max accuracy: %g' % accuracy_pre)

            accuracy_temp = 0
            for i in range(5):
                batch_x, batch_y = rc.Get_testbatch(test_data, test_labels,
                                                    2000, i)
                feed_dict_test = {
                    x_image: batch_x,
                    y_label: batch_y,
                    drop_keepProb: 1.0,
                    is_training: False,
                    is_fixed: False
                }
                accuracy_predict, merged_unfixed = sess.run(
                    [accuracy, merged_all], feed_dict=feed_dict_test)
                # if i == 0:
                #     filters = sess.run([fin_filters], feed_dict=feed_dict_test)
                #     print(filters)
                accuracy_temp += accuracy_predict
            #                print('batch accuracy: %g'% accuracy_predict)

            accuracy_predict = accuracy_temp / 5.0
            writer_unfixed.add_summary(merged_unfixed, iter_i)
            print('unfixed test C accuracy: %g,Max accuracy: %g' %
                  (accuracy_predict, accuracy_pre_unfixed))

            if accuracy_predict > accuracy_pre_unfixed:
                saver.save(sess,
                           os.path.join(ckpt_unfixed_dir, "model_share.ckpt"),
                           global_step=iter_i)
                accuracy_pre_unfixed = accuracy_predict
                print('unfixed max accuracy: %g' % accuracy_pre_unfixed)

        # data agrumentation
        batch_x, batch_y = rc.Get_batchdata(train_data, train_labels,
                                            batch_size, iter_i)
        batch_x = da.data_augment(batch_x)
        batch_x = rc.norm_std_each(batch_x)

        feed_dict = {
            x_image: batch_x,
            y_label: batch_y,
            drop_keepProb: 1.0,
            is_training: True,
            is_fixed: True
        }

        if iter_i == 400:
            lr = 0.1
            sess.run(tf.assign(LR, lr))
            print('learning rate changed : 0.1')

        if iter_i == 32000:
            test_len = 50
            lr = 0.01
            sess.run(tf.assign(LR, lr))
            print('learning rate changed : 0.01')
        #
        if iter_i == 48000:
            test_len = 20
            lr = 0.001
            sess.run(tf.assign(LR, lr))
            print('learning rate changed : 0.001')

        _, lossi, train_pr = sess.run([opt_paras, loss, accuracy],
                                      feed_dict=feed_dict)

        if iter_i % 5 == 0:
            print('step %d: training loss %g : train_pr %g' %
                  (iter_i, lossi, train_pr))
    for epoch in range(total_epochs):
        print('-' * 20, 'epoch', epoch, '-' * 20)
        train_acc = []
        train_loss = []
        test_acc = []
        test_unfixed_acc = []
        # reduce learning rate
        if epoch in reduce_lr_epoch:
            lr = lr * 0.1
            print('reduce learning rate =', lr, 'now')
        # train one epoch
        for iter in range(num_train // train_batch_size):
            # get and preprocess image
            images, labels = rc.Get_batchdata(x_train, y_train, train_batch_size, iter)
            images = da.data_augment(images)
            images = rc.norm_std_each(images)
            # train_one_batch also can accept your own session
            train_feed_dict = {
                x: images,
                label: labels,
                learning_rate: lr,
                training_flag: True,
                is_fixed: True
            }
            _, lossi, acc, merged_train = sess.run([opt_paras, loss, accuracy, merged_all], feed_dict=train_feed_dict)
            train_writer.add_summary(merged_train, epoch * (num_test // test_batch_size) + iter + 1)
            train_acc.append(acc)
            train_loss.append(lossi)
            sys.stdout.write("\r>> train " + str(iter + 1) + '/' + str(num_train // train_batch_size) + ' loss ' + str(
                lossi) + ' acc ' + str(acc))
Exemplo n.º 18
0
    plt.ylabel('Binary Crossentropy')
    plt.legend()
    plt.xlim([0, max(history.epoch)])
    plt.show()


data_gen_args = dict(rotation_range=0.2,
                     width_shift_range=0.05,
                     height_shift_range=0.05,
                     shear_range=0.05,
                     zoom_range=0.05,
                     horizontal_flip=True,
                     fill_mode='nearest')
myGene = DataAugmentation.trainGenerator(2,
                                         'Data',
                                         'image',
                                         'label',
                                         data_gen_args,
                                         save_to_dir=None)

filters = [[8, 16, 32, 64, 128], [16, 32, 64, 128, 256],
           [32, 64, 128, 256, 512]]

Test_X = Train_X[-3:]
for filter in filters:
    print(filter)

    model = UNet.UNet(filters=filter)
    for use_aug in [True, False]:
        file_name = str(filter)[1:-1].replace(', ', '-')
        if use_aug:
            history = model.fit_generator(myGene,
Exemplo n.º 19
0
#set location of source file
covid_data = DM.Data_Manager('/Users/u6026797/Desktop/COVID19_country_04-21-2020.csv')
country_array = covid_data.Get_Available_Countries()

#set as pd.df as needed
#covid_data.Set_Country('Belgium')
#covid_data.Set_Province_State('NA')
#date, new_confirm = covid_data.Get_New_Confirm()
#test_df = pd.DataFrame({'new_confirm':new_counts,
#                       'date':date})
#test_df_index = test_df.set_index('date')

#%% Create transformation augmentation loop
#create output df
best_trans_df = pd.DataFrame({'Country','Region','Avg_Scaled_Error',
              'Transformation','Augmentation'})
#for country
for country in country_array:
    covid_data.Set_Country(country)
    prov_array = test.Get_Province_State
    for region in prov_array:

x = DA.Data_Augmentation(time=date,count=new_confirm,parameter=1,minimum=1,maximum=1,units=1)
x.Percent_Increase()


#%% Create model hyperparameter validation loop
best_param_df = pd.DataFrame({'Country','Avg_Scaled_Error',
              'Param1','...','ParamN'})

#%% Plots        
Exemplo n.º 20
0
print(y_test.dtype)

filename = "updated_test.p"
file = open(filename, 'rb')
X_test = pickle.load(file)

filename = "updated_train.p"
file = open(filename, 'rb')
X_train = pickle.load(file)

filename = "updated_valid.p"
file = open(filename, 'rb')
X_valid = pickle.load(file)

test = X_train[10000]
transformation = func.transform_img(test)
augmentation = func.augment_img(test)
func.show_imgs(test, transformation, augmentation)

print(X_train.shape)
print(X_train.dtype)
print(y_train.shape)
print(y_train.dtype)

print(X_valid.shape)
print(X_valid.dtype)
print(y_valid.shape)
print(y_train.dtype)

print(X_test.shape)
print(X_test.dtype)
Exemplo n.º 21
0
scriptPath = os.getcwd() + '/'

trainDatapath = '/home/liuzheng/competition/kaggle/distractedDrivers/imgs/train/'
testDatapath = '/home/liuzheng/competition/kaggle/distractedDrivers/imgs/test/'
trainFolderList = ['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9']
classNum = len(trainFolderList)

trainSavepath = '/home/liuzheng/competition/kaggle/distractedDrivers/imgs/trainAugmentation/'
testSavepath = '/home/liuzheng/competition/kaggle/distractedDrivers/imgs/testAugmentation/'

reSize = [64, 64]
'''============================= Processing training data ======================================='''
for c in range(classNum):
    classDatapath = trainDatapath + trainFolderList[c] + '/'
    da.augmentTrainingImages_distractedDrivers(datapath=classDatapath, classIdx=c,\
        savepath=trainSavepath, reSize=reSize)

print('making training data list...')
mdl.makeTrainList_distractedDrivers(datapath=trainSavepath,
                                    prefix=scriptPath,
                                    listName=trainListName)

os.system(mxnetRoot + 'bin/im2rec ' + scriptPath + trainListName + ' ' +
          trainSavepath + ' ' + pcProjectpath + trainRecName)
'''=============================================================================================='''
'''============================= Processing testing data ======================================='''
da.augmentTestingImages_distractedDrivers(datapath=testDatapath,
                                          savepath=testSavepath,
                                          reSize=reSize)

print('making testing data list...')
Exemplo n.º 22
0
    def Encode(self, xml_paths, augment=False):
        np_image_data = np.zeros(
            (BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNEL), np.float32)
        np_label_data = np.zeros((BATCH_SIZE, S, S, B, 5 + CLASSES),
                                 np.float32)

        image_paths, gt_bboxes_list, gt_classes_list = [], [], []

        for i, xml_path in enumerate(xml_paths):
            image_path, gt_bboxes, gt_classes = xml_read(xml_path,
                                                         normalize=False)

            image = cv2.imread(image_path)
            assert not image is None, "[!] cv2.imread : {}".format(image_path)

            if augment:
                image, gt_bboxes, gt_classes = DataAugmentation(
                    image, gt_bboxes, gt_classes)

            h, w, c = image.shape
            gt_bboxes = gt_bboxes.astype(np.float32)
            gt_bboxes /= [w, h, w, h]

            image = cv2.resize(image, (IMAGE_WIDTH, IMAGE_HEIGHT),
                               interpolation=cv2.INTER_LINEAR)

            image_data = image.astype(np.float32)
            label_data = np.zeros([S, S, B, 5 + CLASSES], dtype=np.float32)

            for bbox, class_index in zip(gt_bboxes, gt_classes):
                ccwh_bbox = xyxy_to_ccwh(bbox)
                cx, cy, w, h = ccwh_bbox

                ious = IoU_wh([w * IMAGE_WIDTH, h * IMAGE_HEIGHT],
                              self.decode_anchors)
                bbox_indexs = self.anchor_indexs[
                    ious >= POSITIVE_IOU_THRESHOLD]

                if len(bbox_indexs) == 0:
                    bbox_indexs = [np.argmax(ious)]

                # print(bbox_indexs)

                # get grid cell
                grid_x = int(cx * S)
                grid_y = int(cy * S)

                # get offset x, y
                grid_x_offset = (cx * S) - grid_x
                grid_y_offset = (cy * S) - grid_y

                label_data[grid_y, grid_x, bbox_indexs, :4] = [
                    grid_x_offset, grid_y_offset, w, h
                ]
                label_data[grid_y, grid_x, bbox_indexs, 4] = 1.0
                label_data[grid_y, grid_x, bbox_indexs,
                           5:] = one_hot(class_index)

            np_image_data[i] = image_data
            np_label_data[i] = label_data

        return np_image_data, np_label_data
def Get_testbatch(data, label, batch_size, index):
    batch_x_image = data[index * batch_size:(index + 1) * batch_size, ...]
    batch_y_label = label[index * batch_size:(index + 1) * batch_size, ...]
    return batch_x_image, batch_y_label


if __name__ == "__main__":
    filename = 'test_batch'
    data, labels = Load_data(filename)
    data1, labels = Get_batchdata(data, labels, 100, 0)

    plt.imshow(data1[0])
    plt.show()

    data2 = da.img_crop(data1[0])

    #    noise = np.random.normal(loc=0,scale=0.1,size=[10000,32,32,3])
    #    data += noise
    for _ in range(5):
        data2 = da.img_padding_crop(data1[0])
        plt.imshow(data2)
        plt.show()

#    mean = np.mean(data,axis=(0,1,2))
#    data[...,0] = (data[...,0] - mean[0])
#    data[...,0] = data[...,0]/np.std(data[...,0])
#
#    data[...,1] = (data[...,1] - mean[1])
#    data[...,1] = data[...,1]/np.std(data[...,1])
#