Exemplo n.º 1
0
def data_generator(annotation_lines, batch_size, input_shape, anchors, num_classes,teacher):
    '''data generator for fit_generator'''
    n = len(annotation_lines)
    i = 0
    while True:
        #print("data"+str(i) )
        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=False)
            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)
        m_true = teacher.predict(image_data)
        #l_true =  [ np.zeros( shape=( batch_size ,416//{0:32, 1:16, 2:8}[l], 416//{0:32, 1:16, 2:8}[l], 9//3, 20+5) ) for l in range(3) ]

        anchor_mask = [[6,7,8], [3,4,5], [0,1,2]] if len(m_true)==3 else [[3,4,5], [1,2,3]] 

        for l in range( len(m_true) ) : 

            pred_xy, pred_wh , pred_conf , pred_class = numpy_yolo_head( m_true[l] ,anchors[anchor_mask[l]], input_shape )
            pred_detect = np.concatenate([pred_xy, pred_wh , pred_conf , pred_class ],axis=-1)

            #print("inside")
            box = np.where(y_true[l][...,4] > 0.5 )
            box = np.transpose(box)
            for k in range(len(box)):
                m_true[l][tuple(box[k])] = pred_detect[tuple(box[k])] 

        yield image_data, y_true ,m_true
Exemplo n.º 2
0
def data_generator(annotation_lines, batch_size, input_shape, anchors, num_classes,model):
    '''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)
       # print(image_data.shape)
        y_true = model.predict(image_data)
       # print("d")
      #  print(y_true[0].shape)
      #  print(y_true[1].shape)
      #  print(y_true[2].shape)
       # y_true[0] = y_true[0].reshape(y_true[0].shape[0], y_true[0].shape[1], y_true[0].shape[2], 3 , y_true[0].shape[3]//3 ) 
       # y_true[1] = y_true[1].reshape(y_true[1].shape[0], y_true[1].shape[1], y_true[1].shape[2], 3 , y_true[1].shape[3]//3 ) 
       # y_true[2] = y_true[2].reshape(y_true[2].shape[0], y_true[2].shape[1], y_true[2].shape[2], 3 , y_true[2].shape[3]//3 ) 

        yield [image_data, *y_true]#, np.zeros(batch_size)
Exemplo n.º 3
0
def data_generator(annotation_lines, batch_size, input_shape, anchors,
                   num_classes, teacher):
    '''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)

        box = teacher.predict(image_data)
        #np.savez('temp_logits.npz', 0 = box[0] , 1=box[1] , 2=box[2] )
        #box = np.load('temp_logits.npz')[()]
        #y_true = box

        y_true = box
        yield [image_data, *y_true], np.zeros(batch_size)
Exemplo n.º 4
0
def data_generator(annotation_line, batch_size, input_shape, anchors,
                   num_classes):
    """
    训练数据生成器
    :param annotation_line: 图片名与标注
    :param batch_size: 批大小
    :param input_shape: 输入尺寸
    :param anchors: 锚框坐标
    :param num_classes: 类别数
    :return: 生成器,输出每个batch的image data和y_true
    """
    n = len(annotation_line)
    if n == 0 or batch_size <= 0:
        return None
    else:
        i = 0
        while True:
            image_data = []
            box_data = []
            for batch in range(batch_size):
                if i == 0:
                    np.random.shuffle(annotation_line)  # 对图片进行乱序
                # 对每张图片进行数据增强,并分开图片地址和标签
                image, box = get_random_data(annotation_line[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)
Exemplo n.º 5
0
def data_generator(annotation_lines, batch_size, input_shape, anchors,
                   num_classes, teacher):
    '''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)
        m_true = teacher.predict(image_data)

        anchor_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]
                       ] if len(m_true) == 3 else [[3, 4, 5], [1, 2, 3]]

        for l in range(len(m_true)):
            anchors_tensor = np.reshape(
                anchors[anchor_mask[l]],
                [1, 1, 1, len(anchors[anchor_mask[l]]), 2])

            grid_shape = m_true[l].shape[1:3]  # height, width
            grid_shape
            grid_y = np.tile(
                np.reshape(np.arange(0, stop=grid_shape[0]), [-1, 1, 1, 1]),
                [1, grid_shape[1], 1, 1])
            grid_x = np.tile(
                np.reshape(np.arange(0, stop=grid_shape[1]), [1, -1, 1, 1]),
                [grid_shape[0], 1, 1, 1])
            grid = np.concatenate([grid_x, grid_y], axis=-1)

            #print(l)
            m_true[l][..., :2] = (sigmoid(m_true[l][..., :2]) +
                                  grid) / np.array(grid_shape[::-1])
            m_true[l][..., 2:4] = np.exp(
                m_true[l][..., 2:4]) * anchors_tensor / np.array(
                    input_shape[::-1])
            m_true[l][..., 4] = sigmoid(m_true[l][..., 4])
            m_true[l][..., 5:] = sigmoid(m_true[l][..., 5:])

            #print("inside")
            box = np.where(y_true[l][..., 4] > 0.5)
            box = np.transpose(box)
            for i in range(len(box)):
                y_true[l][tuple(box[i])] = m_true[l][tuple(box[i])]

        yield [image_data, *y_true], np.zeros(batch_size)
def data_generator(annotation_lines, batch_size, input_shape, anchors,
                   num_classes, model):
    '''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)
        m_true = model.predict(image_data)

        for l in range(len(m_true)):
            #print(l)
            m_true[l][..., :2] = sigmoid(m_true[l][..., :2])
            m_true[l][..., 2:4] = np.exp(m_true[l][..., 2:4])
            m_true[l][..., 4] = sigmoid(m_true[l][..., 4])
            m_true[l][..., 5:] = sigmoid(m_true[l][..., 5:])
            #print("inside")
            #print(m_true[l].shape)
            box = np.where(m_true[l][:, :, :, :, 4] > 0.3)
            for i in range(len(box[0])):
                s = np.array(box)
                #print( "({},{},{},{})".format(s[0,i],s[1,i],s[2,i],s[3,i]) )
                #print( m_true[l][s[0,i],s[1,i],s[2,i],s[3,i],0:5] )
                #objprob = m_true[l][s[0,i],s[1,i],s[2,i],s[3,i],5:25]
                #print( objprob )
                #obnum =  np.argmax( objprob )
                y_true[l][s[0, i], s[1, i], s[2, i],
                          s[3, i]] = m_true[l][s[0, i], s[1, i], s[2, i], s[3,
                                                                            i]]
                #print("-------------------------------------------------------")

        yield [image_data, *y_true]  #, np.zeros(batch_size)
Exemplo n.º 7
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)
        y_true = preprocess_true_boxes(box_data, input_shape, anchors, num_classes)
        yield [image_data, *y_true], np.zeros(batch_size)
Exemplo n.º 8
0
def data_generator(annotation_lines, batch_size, input_shape, anchors,
                   num_classes):
    '''data generator for fit_generator'''

    num_anchors = len(anchors)
    image_input = Input(shape=(416, 416, 3))
    model = yolo_body(image_input, num_anchors // 3, num_classes)
    model.load_weights("model_data/trained_weights_final.h5")

    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)
        # print(image_data.shape)
        y_true = model.predict(image_data)
        # print("d")
        #  print(y_true[0].shape)
        #  print(y_true[1].shape)
        #  print(y_true[2].shape)
        # y_true[0] = y_true[0].reshape(y_true[0].shape[0], y_true[0].shape[1], y_true[0].shape[2], 3 , y_true[0].shape[3]//3 )
        # y_true[1] = y_true[1].reshape(y_true[1].shape[0], y_true[1].shape[1], y_true[1].shape[2], 3 , y_true[1].shape[3]//3 )
        # y_true[2] = y_true[2].reshape(y_true[2].shape[0], y_true[2].shape[1], y_true[2].shape[2], 3 , y_true[2].shape[3]//3 )

        yield [image_data, *y_true]  #, np.zeros(batch_size)
def data_generator(annotation_lines, batch_size, input_shape, anchors, num_classes,teacher):
    '''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)
        m_true = teacher.predict(image_data)
         
        h, w = input_shape
        num_anchors = len(anchors)
        
        l_true =  [ np.zeros( shape=( batch_size ,416//{0:32, 1:16, 2:8}[l], 416//{0:32, 1:16, 2:8}[l], 9//3, 20+5) ) for l in range(3) ]

        #print(len(y_true))
        #print(len(m_true))
        #print(len(l_true))
        anchor_mask = [[6,7,8], [3,4,5], [0,1,2]] if len(m_true)==3 else [[3,4,5], [1,2,3]] 

        for l in range( len(m_true) ) : 
            '''
            pred_output = tf.Variable(m_true[l]) 
            anchor_mask = [[6,7,8], [3,4,5], [0,1,2]] if len(m_true)==3 else [[3,4,5], [1,2,3]] 
            pred_xy, pred_wh , pred_conf , pred_class = yolo_head( pred_output ,anchors[anchor_mask[l]], num_classes, input_shape, calc_loss=False)
            pred_model = K.concatenate([pred_xy, pred_wh, pred_conf ,pred_class  ])

            with tf.Session() as sess:
                    init = tf.global_variables_initializer()
                    sess.run(init)
                    
                    pred_model = pred_model.eval()
            '''
            anchors_tensor = np.reshape( anchors[anchor_mask[l]] , [1, 1, 1, len( anchors[anchor_mask[l]] ) , 2] )

            grid_shape = m_true[l].shape[1:3] # height, width
            grid_shape
            grid_y = np.tile(np.reshape(np.arange(0, stop=grid_shape[0]), [-1, 1, 1, 1]),
                [1, grid_shape[1], 1, 1])
            grid_x = np.tile(np.reshape(np.arange(0, stop=grid_shape[1]), [1, -1, 1, 1]),
                [grid_shape[0], 1, 1, 1])
            grid = np.concatenate([grid_x, grid_y],axis=-1)

            #print(l)
            m_true[l][...,:2] = (sigmoid(m_true[l][...,:2]) + grid ) / np.array( grid_shape[::-1] )
            m_true[l][..., 2:4] = np.exp(m_true[l][..., 2:4]) * anchors_tensor  / np.array( input_shape[::-1] )
            m_true[l][..., 4] = sigmoid(m_true[l][..., 4])
            m_true[l][..., 5:] = sigmoid(m_true[l][..., 5:])
            
            #print("inside")
            box = np.where(y_true[l][...,4] > 0.5 )
            box = np.transpose(box)

            for i in range(len(box)):
                l_true[l][tuple(box[i])] = m_true[l][tuple(box[i])] #pred_model[tuple(box[i])]
        
        
        yield [image_data, *y_true , *l_true ], np.zeros(batch_size)