コード例 #1
0
ファイル: train.py プロジェクト: SpikeKing/keras-yolo3
def data_generator(annotation_lines, batch_size, input_shape, anchors, num_classes):
    '''data generator for fit_generator'''
    n = len(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        for b in range(batch_size):
            if i==0:
                np.random.shuffle(annotation_lines)
            image, box = get_random_data(annotation_lines[i], input_shape, random=True)
            image_data.append(image)
            box_data.append(box)
            i = (i+1) % n
        image_data = np.array(image_data)
        box_data = np.array(box_data)
        y_true = preprocess_true_boxes(box_data, input_shape, anchors, num_classes)
        yield [image_data, *y_true], np.zeros(batch_size)
コード例 #2
0
ファイル: train.py プロジェクト: CheMiJu/fruit_object_detect
def data_generator(annotation_lines, batch_size, input_shape, anchors, num_classes):
    '''data generator for fit_generator'''
    n = len(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        for b in range(batch_size):
            if i==0:
                np.random.shuffle(annotation_lines)
            image, box = get_random_data(annotation_lines[i], input_shape, random=True)
            image_data.append(image)
            box_data.append(box)
            i = (i+1) % n
        image_data = np.array(image_data)
        box_data = np.array(box_data)
        y_true = preprocess_true_boxes(box_data, input_shape, anchors, num_classes)
        yield [image_data, *y_true], np.zeros(batch_size)
コード例 #3
0
ファイル: train_bottleneck.py プロジェクト: bothe/keras-yolo3
def bottleneck_generator(annotation_lines, batch_size, input_shape, anchors, num_classes, bottlenecks):
    n = len(annotation_lines)
    i = 0
    while True:
        box_data = []
        b0 = np.zeros((batch_size, bottlenecks[0].shape[1], bottlenecks[0].shape[2], bottlenecks[0].shape[3]))
        b1 = np.zeros((batch_size, bottlenecks[1].shape[1], bottlenecks[1].shape[2], bottlenecks[1].shape[3]))
        b2 = np.zeros((batch_size, bottlenecks[2].shape[1], bottlenecks[2].shape[2], bottlenecks[2].shape[3]))
        for b in range(batch_size):
            _, box = get_random_data(annotation_lines[i], input_shape, random=False, proc_img=False)
            box_data.append(box)
            b0[b] = bottlenecks[0][i]
            b1[b] = bottlenecks[1][i]
            b2[b] = bottlenecks[2][i]
            i = (i + 1) % n
        box_data = np.array(box_data)
        y_true = preprocess_true_boxes(box_data, input_shape, anchors, num_classes)
        yield [b0, b1, b2, *y_true], np.zeros(batch_size)
コード例 #4
0
def bottleneck_generator(annotation_lines, batch_size, input_shape, anchors, num_classes, bottlenecks):
    n = len(annotation_lines)
    i = 0
    while True:
        box_data = []
        b0=np.zeros((batch_size,bottlenecks[0].shape[1],bottlenecks[0].shape[2],bottlenecks[0].shape[3]))
        b1=np.zeros((batch_size,bottlenecks[1].shape[1],bottlenecks[1].shape[2],bottlenecks[1].shape[3]))
        b2=np.zeros((batch_size,bottlenecks[2].shape[1],bottlenecks[2].shape[2],bottlenecks[2].shape[3]))
        for b in range(batch_size):
            _, box = get_random_data(annotation_lines[i], input_shape, random=False, proc_img=False)
            box_data.append(box)
            b0[b]=bottlenecks[0][i]
            b1[b]=bottlenecks[1][i]
            b2[b]=bottlenecks[2][i]
            i = (i+1) % n
        box_data = np.array(box_data)
        y_true = preprocess_true_boxes(box_data, input_shape, anchors, num_classes)
        yield [b0, b1, b2, *y_true], np.zeros(batch_size)
コード例 #5
0
def data_generator(annotation_lines, batch_size, input_shape, anchors,
                   num_classes):
    '''data generator for fit_generator'''
    '''数据生成器
    @param annotations_lines 训练样本,每行形如以下格式:img_file 6,1,314,262,19 40,97,121,411,4 137,36,169,109,14 180,36,216,104,14 96,39,123,103,14 
    @param batch_size 批大小,32
    @param input_shape 模型输入尺寸(416,416)
    @param anchors bounding box 锚点wh:[[10,13],  [16,30],  [33,23],  [30,61],  [62,45],  [59,119],  [116,90],  [156,198],  [373,326]]
    @param num_classes 标签类别数,20
    @return yield [image_data, *y_true], np.zeros(batch_size) => ([image_data,y1,y2,y3],loss)
                                                              => ([(32,416,416,3),(32,13,13,3,25),(32,26,26,3,25),(32,52,52,3,25),(32,)])
    '''
    n = len(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        for b in range(batch_size):
            if i == 0:  #一轮
                np.random.shuffle(annotation_lines)  #打乱排序
            #生成一个样本数据,image.shape=>(416,416,3),box.shape=>(20,5)
            #image做数据增强、归一化处理;box与image同步做变形、偏移处理,未归一化
            image, box = get_random_data(annotation_lines[i],
                                         input_shape,
                                         random=True)
            image_data.append(image)  #图像序列
            box_data.append(box)  #bounding box序列
            i = (i + 1) % n
        image_data = np.array(image_data)  #=>shape:(32,416,416,3)
        box_data = np.array(box_data)  #=>shape:(32,20,5)
        y_true = preprocess_true_boxes(box_data, input_shape, anchors,
                                       num_classes)  #=>把样本原始数据转换为训练需要的数据格式
        '''
        image_data:shape=>(32,416,416,3),模型输入图像数据
        y_true:[y1,y2,y3],               模型输入Box标签数据
            y1:shape=>(32,13,13,3,25)
            y2:shape=>(32,26,26,3,25)
            y3:shape=>(32,52,52,3,25)
        np.zeros(batch_size):            模型输出loss

        yield [image_data, *y_true], np.zeros(batch_size) => ([image_data,y1,y2,y3],loss)
                                                          => ([(32,416,416,3),(32,13,13,3,25),(32,26,26,3,25),(32,52,52,3,25),(32,)])
        '''
        yield [image_data, *y_true], np.zeros(batch_size)
コード例 #6
0
    def parse_tfrecord(self, example_proto):
        feature_description = {
            'image/encoded': tf.io.FixedLenFeature([], tf.string),
            'image/object/bbox/xmin': tf.io.VarLenFeature(tf.float32),
            'image/object/bbox/xmax': tf.io.VarLenFeature(tf.float32),
            'image/object/bbox/ymin': tf.io.VarLenFeature(tf.float32),
            'image/object/bbox/ymax': tf.io.VarLenFeature(tf.float32),
            'image/object/bbox/label': tf.io.VarLenFeature(tf.int64)
        }
        features = tf.io.parse_single_example(example_proto,
                                              feature_description)
        image = tf.image.decode_image(features['image/encoded'],
                                      channels=3,
                                      dtype=tf.float32)
        image.set_shape([None, None, 3])
        xmins = features['image/object/bbox/xmin'].values
        xmaxs = features['image/object/bbox/xmax'].values
        ymins = features['image/object/bbox/ymin'].values
        ymaxs = features['image/object/bbox/ymax'].values
        labels = features['image/object/bbox/label'].values
        image, bbox = get_random_data(image,
                                      xmins,
                                      xmaxs,
                                      ymins,
                                      ymaxs,
                                      labels,
                                      self.input_shape,
                                      train=self.mode == DATASET_MODE.TRAIN)
        y1, y2, y3 = tf.py_function(
            preprocess_true_boxes,
            [bbox, self.input_shape, self.anchors, self.num_classes],
            [tf.float32, tf.float32, tf.float32])
        y1.set_shape(
            [None, None,
             len(self.anchors) // 3, self.num_classes + 5])
        y2.set_shape(
            [None, None,
             len(self.anchors) // 3, self.num_classes + 5])
        y3.set_shape(
            [None, None,
             len(self.anchors) // 3, self.num_classes + 5])

        return image, (y1, y2, y3)
コード例 #7
0
ファイル: test.py プロジェクト: svija/K210-yolo3
    def parser(lines):
        image_data = []
        box_data = []
        for line in lines:
            image, box = get_random_data(line.numpy().decode(),
                                         input_shape,
                                         random=True)
            image_data.append(image)
            box_data.append(box)

        image_data = np.array(image_data)
        box_data = np.array(box_data)

        y_true = [
            tf.convert_to_tensor(y, tf.float32) for y in preprocess_true_boxes(
                box_data, input_shape, anchors, num_classes)
        ]
        image_data = tf.convert_to_tensor(image_data, tf.float32)
        return (image_data, *y_true)
コード例 #8
0
def data_generator(annotation_lines, batch_size, input_shape, anchors, num_classes, random=True, verbose=False):
    '''data_original generator for fit_generator'''
    n = len(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        for b in range(batch_size):
            if i==0 and random:
                np.random.shuffle(annotation_lines)
            image, box = get_random_data(annotation_lines[i], input_shape, random=random)
            image_data.append(image)
            box_data.append(box)
            i = (i+1) % n
        image_data = np.array(image_data)
        if verbose:
            print("Progress: ",i,"/",n)
        box_data = np.array(box_data)
        y_true = preprocess_true_boxes(box_data, input_shape, anchors, num_classes)
        yield [image_data, *y_true], np.zeros(batch_size)
コード例 #9
0
ファイル: ghost_train.py プロジェクト: QFaceblue/keras-yolo3
def data_generator(annotation_lines, batch_size, input_shape, num_classes):
    '''data generator for fit_generator'''
    n = len(annotation_lines)
    i = 0
    while True:
        image_data = []
        label_data = []
        for b in range(batch_size):
            if i == 0:
                np.random.shuffle(annotation_lines)
            image, box = get_random_data(annotation_lines[i],
                                         input_shape,
                                         random=True)
            image_data.append(image)
            label = keras.utils.to_categorical(box[0][-1], num_classes)
            label_data.append(label)
            i = (i + 1) % n
        image_data = np.array(image_data)
        label_data = np.array(label_data)
        yield image_data, label_data
コード例 #10
0
def data_generator(annotation_lines, batch_size, input_shape, anchors, num_classes):
    '''data generator for fit_generator'''
    n = len(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        for b in range(batch_size):
            if i==0:
                np.random.shuffle(annotation_lines)#随机打乱数据的读取顺序,有注意训练模型
            image, box = get_random_data(annotation_lines[i], input_shape, random=True)#随机获取每张图片的像素集合和检测对象的box坐标信息以及每个对象的类别
            image_data.append(image)#添加图片
            box_data.append(box)#添加box
            i = (i+1) % n
        image_data = np.array(image_data)
        box_data = np.array(box_data)
        y_true = preprocess_true_boxes(box_data, input_shape, anchors, num_classes)#获取真值,用于后面计算loss
		#图片划分为13*13,26*26,52*52三个不同规格的cell单元格,y_true记录哪个单元格负责预测对象的box的位置信息和类别

        yield [image_data, *y_true], np.zeros(batch_size) #np.zeros(batch_size)就是损失函数中那个任意给个shape符合的参数
コード例 #11
0
ファイル: train_seg.py プロジェクト: PangHua/keras-yolo3-1
def data_generator(annotation_lines,
                   batch_size,
                   input_shape,
                   anchors,
                   num_classes,
                   model_name=None):
    '''data generator for fit_generator'''
    n = len(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        seg_data = []
        for b in range(batch_size):
            if i == 0:
                np.random.shuffle(annotation_lines)
            image, box, seg = get_random_data(annotation_lines[i],
                                              input_shape,
                                              random=True,
                                              model_name=model_name)
            image_data.append(image)
            box_data.append(box)
            seg_data.append(seg)
            i = (i + 1) % n
        image_data = np.array(image_data)
        box_data = np.array(box_data)
        y_seg_data = np.array(seg_data)
        y_true = preprocess_true_boxes(box_data, input_shape, anchors,
                                       num_classes)
        #np.zeros(batch_size) -> seems like the default implementation send a dummy output.
        if model_name in ['tiny_yolo_infusion', 'yolo_infusion']:
            # y_true.append(y_seg_data)
            # yield ({'input_1': x1, 'input_2': x2}, {'output': y}) -> https://keras.io/models/model/
            yield ([image_data, *y_true], {
                'yolo_loss': np.zeros(batch_size),
                'seg_output': y_seg_data
            })
        elif model_name in ['tiny_yolo_seg', 'tiny_yolo_vgg_seg']:
            yield ([image_data], {'seg_output': y_seg_data})
        else:
            yield [image_data, *y_true], np.zeros(batch_size)
コード例 #12
0
def data_generator(annotation_lines, batch_size, input_shape, anchors, num_classes):
    '''data generator for fit_generator'''
    n = len(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        for b in range(batch_size):
            if i==0:
                np.random.shuffle(annotation_lines)
            image, box = get_random_data(annotation_lines[i], input_shape, random=True)
            augment = data_aug.Sequence([data_aug.HorizontalFlip(), data_aug.RandomRotate()], probs=0.5)
            image, tem = augment(image, box[:, :4])
            box[:len(tem), :4] = tem
            image_data.append(image)
            box_data.append(box)
            i = (i+1) % n
        image_data = np.array(image_data)
        box_data = np.array(box_data)
        y_true = preprocess_true_boxes(box_data, input_shape, anchors, num_classes)
        yield [image_data, *y_true], np.zeros(batch_size)
コード例 #13
0
        def parse(example_proto):
            feature_description = {
                'image/encoded': tf.io.FixedLenFeature([], tf.string),
                'image/object/bbox/xmin': tf.io.VarLenFeature(tf.float32),
                'image/object/bbox/xmax': tf.io.VarLenFeature(tf.float32),
                'image/object/bbox/ymin': tf.io.VarLenFeature(tf.float32),
                'image/object/bbox/ymax': tf.io.VarLenFeature(tf.float32),
                'image/object/bbox/label': tf.io.VarLenFeature(tf.int64)
            }
            features = tf.io.parse_single_example(example_proto, feature_description)
            image, bbox = get_random_data(features['image/encoded'],
                                          features['image/object/bbox/xmin'].values,
                                          features['image/object/bbox/xmax'].values,
                                          features['image/object/bbox/ymin'].values,
                                          features['image/object/bbox/ymax'].values,
                                          features['image/object/bbox/label'].values,
                                          input_shape, train=train)

            y0, y1, y2 = tf.py_function(preprocess_true_boxes, [bbox, input_shape, anchors, num_classes],
                                        [tf.float32, tf.float32, tf.float32])
            return (image, y0, y1, y2), 0
コード例 #14
0
def data_generator(annotation_lines, batch_size, input_shape, anchors,
                   num_classes):
    """
    annotation_lines:标注数据的行,每行数据包含图片路径,和框的位置信息
    在第0次时,将数据洗牌shuffle,调用get_random_data解析annotation_lines[i],
    生成图片image和标注框box,添加至各自的列表image_data和box_data中。
    """
    n = len(annotation_lines)
    np.random.shuffle(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        for b in range(batch_size):  # 4
            i %= n
            image, box = get_random_data(annotation_lines[i],
                                         input_shape,
                                         random=True)
            image_data.append(image)
            box_data.append(box)
            i += 1
        """  
        索引值递增i+1,当完成n个一轮之后,重新将i置0,再次调用shuffle洗牌数据。
        将image_data和box_data都转换为np数组,其中:
        image_data: (4, 416, 416, 3)
        box_data: (4, 20, 5) # 每个图片最多含有20个框
        """
        image_data = np.array(image_data)
        box_data = np.array(box_data)
        """
        将框的数据box_data、输入图片尺寸input_shape、anchor box列表anchors和类别数num_classes
        转换为真值y_true,其中y_true是3个预测特征的列表:
        [(4, 13, 13, 3, 7), (4, 26, 26, 3, 7), (4, 52, 52, 3, 7)]
        """
        y_true = preprocess_true_boxes(box_data, input_shape, anchors,
                                       num_classes)

        # 最终输出:图片数据image_data、真值y_true、每个图片的损失值np.zeros
        yield [image_data, *y_true], np.zeros(batch_size)
コード例 #15
0
    def data_generator(self, batch_data):
        num_classes = self.embedding.shape[0]
        embedding = np.tile(np.expand_dims(self.embedding, 0),
                            (self.batch_size, 1, 1))

        image_data = []
        box_data = []
        for data in batch_data:
            image, box = get_random_data(data, self.input_shape, random=True)
            image_data.append(image)
            box_data.append(box)
        image_data = np.array(image_data)
        box_data = np.array(box_data)
        y_true = preprocess_true_boxes(box_data, self.input_shape,
                                       self.anchors, num_classes)
        if self.final:
            anchor = np.tile(np.expand_dims(self.anchors, 0),
                             (self.batch_size, 1, 1))
            return [image_data, anchor, *y_true,
                    embedding], np.zeros(self.batch_size)
        else:
            return [image_data, *y_true, embedding], np.zeros(self.batch_size)
コード例 #16
0
def data_generator(annotation_lines, batch_size, input_shape, anchors,
                   num_classes):
    '''data generator for fit_generator'''  #去生成图像资料产生器
    n = len(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        for b in range(batch_size):
            if i == 0:
                np.random.shuffle(annotation_lines)  #图片乱序
            image, box = get_random_data(annotation_lines[i],
                                         input_shape,
                                         random=True)  #分开图片地址和标签值
            image_data.append(image)
            box_data.append(box)
            i = (i + 1) % n
        image_data = np.array(image_data)  #图片转换成矩阵
        box_data = np.array(box_data)  #BBOX转换成矩阵
        y_true = preprocess_true_boxes(box_data, input_shape, anchors,
                                       num_classes)  #真实坐标转化为YOLO输入坐标
        yield [image_data, *y_true], np.zeros(batch_size)
コード例 #17
0
def data_generator(annotation_lines, batch_size, input_shape, anchors,
                   num_classes):
    n = len(annotation_lines)
    np.random.shuffle(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        for b in range(batch_size):
            i %= n
            image, box = get_random_data(annotation_lines[i],
                                         input_shape,
                                         random=True)
            #image = cv2.resize(image, (224, 224))
            image_data.append(image)
            box_data.append(box)
            i += 1
        image_data = np.array(image_data)
        box_data = np.array(box_data)
        y_true = preprocess_true_boxes(box_data, input_shape, anchors,
                                       num_classes)
        yield [image_data, *y_true], np.zeros(batch_size)
コード例 #18
0
    def __getitem__(self, idx):
        if idx == 0:
            np.random.shuffle(self.annotation_lines)

        image_data = []
        box_data = []

        start_index = idx * self.batch_size
        end_index = (idx + 1) * self.batch_size

        for index in range(start_index, end_index):
            image, box = get_random_data(self.annotation_lines[index],
                                         self.input_shape,
                                         random=True)
            image_data.append(image)
            box_data.append(box)

        image_data = np.array(image_data)
        box_data = np.array(box_data)
        y_true = preprocess_true_boxes(box_data, self.input_shape,
                                       self.anchors, self.num_classes)

        return [image_data, *y_true], np.zeros(self.batch_size)
コード例 #19
0
        def parse(line):
            values=tf.strings.split([line]).values
            def get_data(values):
                i=0;
                xmin=[]
                ymin=[]
                xmax=[]
                ymax=[]
                label=[]
                while i<len(values):
                    xmin.append(values[i])
                    ymin.append(values[i+1])
                    xmax.append(values[i+2])
                    ymax.append(values[i+3])
                    label.append(values[i+4])
                    i+=5
                return np.array(xmin,dtype='float32'),np.array(xmax,dtype='float32'),np.array(ymin,dtype='float32'),np.array(ymax,dtype='float32'),np.array(label,dtype='float32')

            xmin,xmax,ymin,ymax,label=tf.py_function(get_data,[values[1:]],[tf.float32, tf.float32, tf.float32,tf.float32, tf.float32])
            image, bbox = get_random_data(tf.io.read_file(values[0]),xmin,xmax,ymin,ymax,label,input_shape, train=train)
            y0, y1, y2 = tf.py_function(preprocess_true_boxes, [bbox, input_shape, anchors, num_classes],
                                        [tf.float32, tf.float32, tf.float32])
            return (image, y0, y1, y2), 0
コード例 #20
0
ファイル: train.py プロジェクト: willingcptbtptp/keras-yolo3
def data_generator(annotation_lines, batch_size, input_shape, anchors,
                   num_classes):
    '''
    data generator for fit_generator
    样本生成器函数,为model.fit_generator()生成样本生成器函数
    生成器函数的输出应该为:一个形如(inputs , targets) 的tuple或者一个形如(inputs, targets,sample_weight) 的tuple。
    :param annotation_lines:图片路径名组成的list
    :param batch_size:这里是32
    :param input_shape:这里是416*416
    :param anchors: anchors组成的list
    :param num_classes:
    :return:返回一个tuple,yield类似于有记忆功能的return,没有加括号,返回多个值默认为tuple
    返回值为:([image_data, *y_true], np.zeros(batch_size)),其中[image_data, *y_true]表示输入数据,np.zeros(batch_size)表示label数据,
             因为,这里对应的model是被设置为两个输入(图像数据,真实label数据)以及一个输出(loss结果)
    '''
    n = len(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        for b in range(batch_size):
            if i == 0:
                np.random.shuffle(annotation_lines)
            # 读取指点图片的图像数据和box数据,这个label数据是已经resized到input_shape(416,416)大小的
            image, box = get_random_data(annotation_lines[i],
                                         input_shape,
                                         random=True)
            image_data.append(image)
            box_data.append(box)
            i = (i + 1) % n  # i在0-n之间循环,表示当前采集的样本序号
        image_data = np.array(
            image_data
        )  # box_data和image_data每次加入batch_size个样本,然后生成网络需要的label y_true后清空
        box_data = np.array(box_data)
        y_true = preprocess_true_boxes(box_data, input_shape, anchors,
                                       num_classes)
        yield [image_data, *y_true], np.zeros(batch_size)
コード例 #21
0
def data_generator(annotation_lines,
                   batch_size,
                   input_shape,
                   anchors,
                   num_classes,
                   random=True,
                   verbose=False):
    """
        Data generator function for fit_generator
        :param annotation_lines:
        :param batch_size:
        :param input_shape:
        :param anchors:
        :param num_classes:
        :param random:
        :param verbose:
        :return:
    """
    n = len(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        for b in range(batch_size):
            if i == 0:
                np.random.shuffle(annotation_lines)
            image, box = get_random_data(annotation_lines[i],
                                         input_shape,
                                         random=random)
            image_data.append(image)
            box_data.append(box)
            i = (i + 1) % n
        image_data = np.array(image_data)
        box_data = np.array(box_data)
        y_true = preprocess_true_boxes(box_data, input_shape, anchors,
                                       num_classes)
        yield [image_data, *y_true], np.zeros(batch_size)
コード例 #22
0
    def parse_text(self, line):
        values = tf.strings.split([line], ' ').values
        image = tf.image.decode_image(tf.io.read_file(values[0]),
                                      channels=3,
                                      dtype=tf.float32)
        image.set_shape([None, None, 3])
        reshaped_data = tf.reshape(values[1:], [-1, 5])
        xmins = tf.strings.to_number(reshaped_data[:, 0], tf.float32)
        xmaxs = tf.strings.to_number(reshaped_data[:, 2], tf.float32)
        ymins = tf.strings.to_number(reshaped_data[:, 1], tf.float32)
        ymaxs = tf.strings.to_number(reshaped_data[:, 3], tf.float32)
        labels = tf.strings.to_number(reshaped_data[:, 4], tf.int64)

        image, bbox = get_random_data(image,
                                      xmins,
                                      xmaxs,
                                      ymins,
                                      ymaxs,
                                      labels,
                                      self.input_shape,
                                      train=self.mode == DATASET_MODE.TRAIN)
        y1, y2, y3 = tf.py_function(
            preprocess_true_boxes,
            [bbox, self.input_shape, self.anchors, self.num_classes],
            [tf.float32, tf.float32, tf.float32])
        y1.set_shape(
            [None, None,
             len(self.anchors) // 3, self.num_classes + 5])
        y2.set_shape(
            [None, None,
             len(self.anchors) // 3, self.num_classes + 5])
        y3.set_shape(
            [None, None,
             len(self.anchors) // 3, self.num_classes + 5])

        return image, (y1, y2, y3)
コード例 #23
0
def data_generator(annotation_lines, batch_size, input_shape, anchors,
                   num_classes):
    n = len(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        for b in range(batch_size):
            if i == 0:
                # 随机排列图片顺序
                np.random.shuffle(annotation_lines)
            # image_data: (16, 416, 416, 3)
            # box_data: (16, 20, 5) # 每个图片最多含有20个框
            image, box = get_random_data(annotation_lines[i],
                                         input_shape,
                                         random=True)
            image_data.append(image)
            box_data.append(box)
            i = (i + 1) % n
        image_data = np.array(image_data)
        box_data = np.array(box_data)
        y_true = preprocess_true_boxes(box_data, input_shape, anchors,
                                       num_classes)
        yield [image_data, *y_true], np.zeros(batch_size)
コード例 #24
0
ファイル: train.py プロジェクト: pivapi/keras-yolo3-CPU
def data_generator(annotation_lines, batch_size, input_shape, anchors, num_classes):
    '''data generator for fit_generator
       总的来说就是生成一个batch_size的数据
    '''
    n = len(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        for b in range(batch_size):
            if i==0:
                np.random.shuffle(annotation_lines)
            # 随机处理输入数据,包括图片和bbox
            image, box = get_random_data(annotation_lines[i], input_shape, random=True)
            image_data.append(image)
            box_data.append(box)
            i = (i+1) % n
        image_data = np.array(image_data)
        box_data = np.array(box_data)
        # 这里还需要转换bbox的格式
        # 因为YOLO3里面3个feature map,需要对应地转换,将绝对坐标(原图像)转换为相对坐标
        y_true = preprocess_true_boxes(box_data, input_shape, anchors, num_classes)
        # 能够得到一个batch_size的训练数据,注意"*"号的使用
        yield [image_data, *y_true], np.zeros(batch_size)
コード例 #25
0
# -*- coding: utf-8 -*-
"""
查看数据是否有问题
"""
import cv2
from yolo3.utils import get_random_data
from matplotlib.colors import rgb_to_hsv, hsv_to_rgb

#%%  test get data function
annotation_path = 'data/data8.4/all.txt'

with open(annotation_path) as f:
        lines = f.readlines()
        for line in lines:
            img, boxes = get_random_data(line,(224,320),random=1)
            cv2.imshow('imgr',img)
            '''
            img[...,0] = preprocessing.scale(img[...,0])
            img[...,1] = preprocessing.scale(img[...,1]) 
            img[...,2] = preprocessing.scale(img[...,2]) 
            '''
            # img = rgb_to_hsv(img)
            # img[...,2]=cv2.equalizeHist(img[...,2])
            # img[...,2]=exposure.equalize_hist(img[...,2])
            
            # img = hsv_to_rgb(img)
            img[...,[0,2]] = img[...,[2,0]]
            print("##########################")
            for box in boxes:
                
                if box[4] in (0,1,2):
コード例 #26
0
ファイル: creat_random_image.py プロジェクト: wxwisgood/keras
# for i in range(1000):

    line = lines[i]
    line=line[:-1]
    a = line.split(' ')
    image_name = a[0]
    image_name1 = (image_name.split('\\')[-1]).split('.')[0]
    # print(image_name)
    image_out_name = image_out + image_name1 + '_1.jpg'
    # image=draw_ori_image(image_name, class_names, colors)
    # image.save(image_out_name)


    time1=time.time()
    image1, box = get_random_data(lines[i], input_shape, random=True)
    sum_time=sum_time+(time.time() - time1)

    # image_out_name = image_out + 'temp.jpg'
    image_out_name = image_out + image_name1 + '_2.jpg'
    # ÏÔʾ±ê×¢ÐÅÏ¢
    misc.imsave(image_out_name, image1)

    image=draw_random_image(image_out_name, class_names, colors)
    # image_out_name = image_out + image_name1 + '_2.jpg'
    # image_new.save(image_out_name)
    # # misc.imsave(image_out_name, image)
    # # image = Image.fromarray(image)
    image.save(image_out_name)
    sum_time = sum_time + (time.time() - time1)
print(sum_time)
コード例 #27
0
def data_generator(annotation_lines,
                   batch_size,
                   input_shape,
                   anchors,
                   num_classes,
                   model_name=None,
                   num_yolo_heads=None):
    '''data generator for fit_generator'''
    n = len(annotation_lines)
    i = 0
    while True:
        image_data = []
        box_data = []
        seg_data = []
        for b in range(batch_size):
            if i == 0:
                np.random.shuffle(annotation_lines)
            image, box, seg = get_random_data(
                annotation_lines[i],
                input_shape,
                random=train_config['data_augmentation'],
                model_name=model_name)
            image_data.append(image)
            box_data.append(box)
            seg_data.append(seg)
            i = (i + 1) % n
        image_data = np.array(image_data)
        box_data = np.array(box_data)
        y_seg_data = np.array(seg_data)
        y_true = preprocess_true_boxes(box_data,
                                       input_shape,
                                       anchors,
                                       num_classes,
                                       model_name=model_name,
                                       num_yolo_heads=num_yolo_heads)

        if model_name in ['tiny_yolo_infusion', 'yolo_infusion']:
            #old style
            # yield [image_data, *y_true, y_seg_data], np.zeros(batch_size)

            #new style
            # yield ({'input_1': x1, 'input_2': x2}, {'output': y}) -> https://keras.io/models/model/
            yield ([image_data, *y_true], {
                'yolo_loss': np.zeros(batch_size),
                'seg_output': y_seg_data
            })
        elif model_name in ['tiny_yolo_infusion_hydra']:
            #old style
            # y_seg_reorg = list(zip(*y_seg_data))
            # y_seg_data_1 = np.array(y_seg_reorg[0])
            ## print('y_seg_data_1',y_seg_data_1.shape)
            # y_seg_data_2 = np.array(y_seg_reorg[1])
            ## print('y_seg_data_2',y_seg_data_2.shape)
            # yield [image_data, *y_true, y_seg_data_1, y_seg_data_2 ], np.zeros(batch_size)
            #new style: not implemented yet.
            pass

        elif model_name in [
                'tiny_yolo', 'yolo', 'yolo_small_objs', 'tiny_yolo_small_objs'
        ]:
            yield [image_data, *y_true], np.zeros(
                batch_size
            )  #np.zeros(batch_size) -> seems like the default implementation send a dummy output.
        else:
            raise Exception('unknown model.')
コード例 #28
0
num_anchors = len(anchors)

yolo_model = yolo_body(Input(shape=(None, None, 3)), num_anchors // 3,
                       num_classes)
yolo_model.load_weights(model_path)

annotation_path = 'train_nuro.txt'
val_split = 0.99
with open(annotation_path) as f:
    annotation_lines = f.readlines()
num_val = int(len(annotation_lines) * val_split)
num_train = len(annotation_lines) - num_val
input_shape = (416, 416)

n = len(annotation_lines)
image, box = get_random_data(annotation_lines[0], input_shape, random=False)

image_data = []
box_data = []
image_data.append(image)
box_data.append(box)

image_data = np.array(image_data)
box_data = np.array(box_data)
batch_data = []
batch_data.append(annotation_lines[0])

y_true = preprocess_true_boxes(box_data, input_shape, anchors, num_classes,
                               batch_data)

# model = create_model(input_shape, anchors, num_classes, freeze_body=2, weights_path=model_path, grid_loss=False)
コード例 #29
0
annotation_path = 'Custom_set.txt'
classes_path = 'model_data/custom_classes.txt'
anchors_path = 'model_data/tiny_yolo_anchors.txt'
class_names = get_classes(classes_path)
num_classes = len(class_names)
anchors = get_anchors(anchors_path)

input_shape = (416, 416)  # multiple of 32, hw

val_split = 0.1
with open(annotation_path) as f:
    lines = f.readlines()
np.random.seed(10101)
np.random.shuffle(lines)
np.random.seed(None)
num_val = int(len(lines) * val_split)
num_train = len(lines) - num_val
batch_size = 32

#gen = train.data_generator_wrapper(lines[:num_train], batch_size, input_shape, anchors, num_classes)
start = timer.time()

a = get_random_data(lines[:num_train][0], input_shape, False, max_boxes=1)

end = timer.time()
print(end - start)

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