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
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)
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)
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)