Ejemplo n.º 1
0
	def call(self, x):
		'''
		To compute rois from infered classification branch and location branch
		Arguments:
			x:
		Return
			roi_3dtensor:
		'''

		ishape = self.ishape
		anchor_4dtensor = self.anchor_4dtensor
		max_num_of_rois = self.num_of_rois
		nsm_iou_threshold = self.nsm_iou_threshold
		nsm_score_threshold = self.nsm_score_threshold

		clzbbe_4dtensor = x # (batch_size, h, w, 6k)
		clzbbe_3dtensor = clzbbe_4dtensor[0] # (h, w, 6k)

		roi_2dtensor = nsm(
			anchor_4dtensor=anchor_4dtensor,
			clzbbe_3dtensor=clzbbe_3dtensor,
			max_num_of_rois=max_num_of_rois,
			nsm_iou_threshold=nsm_iou_threshold,
			nsm_score_threshold=nsm_score_threshold,
			ishape=ishape)
		roi_3dtensor = tf.expand_dims(input=roi_2dtensor, axis=0) # (batch_size, num_of_rois, 4)

		return roi_3dtensor
Ejemplo n.º 2
0
def build_infer_model(ishape, resnet_settings, top_down_pyramid_size, k, total_classes, abox_2dtensor, nsm_iou_threshold, nsm_score_threshold, nsm_max_output_size):
	'''
	'''

	use_bias = True
	weight_decay = 0.0
	trainable = False
	bn_trainable = False

	input_tensor = Input(shape=ishape, name='input', dtype='float32')
	
	nets = fpn(
		input_tensor=input_tensor, 
		resnet_settings=resnet_settings, 
		top_down_pyramid_size=top_down_pyramid_size, 
		use_bias=use_bias, 
		weight_decay=weight_decay, 
		trainable=trainable, 
		bn_trainable=bn_trainable)

	tensors = []
	for i in range(len(nets)):
		tensor = nets[i]
		
		tensor = Conv2D(
			filters=k[i]*(total_classes+1+4), 
			kernel_size=[1, 1], 
			strides=[1, 1], 
			padding='same', 
			use_bias=use_bias, 
			kernel_regularizer=regularizers.l2(weight_decay), 
			trainable=trainable, 
			name='head'+str(i)+'_conv')(tensor)
		tensor = tf.reshape(tensor=tensor, shape=[tensor.shape[1]*tensor.shape[2]*k[i], total_classes+1+4]) # (h*w*k, total_classes+1+4)
		clz_tensor = tensor[:, :total_classes+1]
		clz_tensor = Activation('softmax')(clz_tensor)
		loc_tensor = tensor[:, total_classes+1:]
		tensor = tf.concat(values=[clz_tensor, loc_tensor], axis=-1) # (h*w*k, total_classes+1+4)
		tensors.append(tensor)

	tensor = tf.concat(values=tensors, axis=0) # (h1*w1*k1 + h2*w2*k2 + h3*w3*k3 + h4*w4*k4, total_classes+1+4)

	tensor, valid_outputs = nsm(
		abox_2dtensor=abox_2dtensor, 
		prediction=tensor, 
		nsm_iou_threshold=nsm_iou_threshold,
		nsm_score_threshold=nsm_score_threshold,
		nsm_max_output_size=nsm_max_output_size,
		total_classes=total_classes)
	valid_outputs = tf.expand_dims(input=valid_outputs, axis=0)

	model = Model(inputs=input_tensor, outputs=[tensor, valid_outputs])
	model.compile(optimizer=Adam(), loss=None)

	return model
Ejemplo n.º 3
0
                  start_example_index + num_of_train_examples +
                  num_of_validation_examples + num_of_test_examples),
           ishape=ishape)

for sample_order in range(num_of_test_examples):
    x, img_id = next(gen)

    # predict proposal
    batch_x = tf.expand_dims(input=x, axis=0)
    y_pred = rpn_model.predict_on_batch(batch_x)
    clzbbe_4dtensor = y_pred  # (batch_size, h, w, 6k), batch_size = 1
    clzbbe_3dtensor = clzbbe_4dtensor[0]  # (h, w, 6k)

    roi_2dtensor = nsm(anchor_4dtensor=anchor_4dtensor,
                       clzbbe_3dtensor=clzbbe_3dtensor,
                       max_num_of_rois=max_num_of_rois,
                       nsm_iou_threshold=nsm_iou_threshold,
                       nsm_score_threshold=nsm_score_threshold,
                       ishape=ishape)

    # predict object proposal
    batch_x = [
        tf.expand_dims(input=x, axis=0),
        tf.expand_dims(input=roi_2dtensor, axis=0)
    ]
    y_pred = detection_model.predict_on_batch(batch_x)
    clzbbe_tensor = y_pred[0]
    clz_2dtensor = clzbbe_tensor[:, :len(classes)]
    bbe_2dtensor = clzbbe_tensor[:, len(classes):]
    pred_bbox2d = bbe2box2d(box_2dtensor=roi_2dtensor,
                            bbe_2dtensor=bbe_2dtensor)
Ejemplo n.º 4
0
def build_infer_model(ishape, resnet_settings, k, total_classes, abox_2dtensor,
                      nsm_iou_threshold, nsm_score_threshold,
                      nsm_max_output_size):
    '''
	'''

    use_bias = True
    weight_decay = 0.0
    trainable = False
    bn_trainable = False

    input_tensor = Input(shape=ishape, name='input', dtype='uint8')
    tensor = tf.cast(x=input_tensor, dtype='float32')

    tensors = resnet(input_tensor=tensor,
                     block_settings=resnet_settings,
                     use_bias=use_bias,
                     weight_decay=weight_decay,
                     trainable=trainable,
                     bn_trainable=bn_trainable)
    tensor = tensors[-1]

    head_dims = tensor.shape[3]
    tensor = Conv2D(filters=head_dims,
                    kernel_size=[3, 3],
                    strides=[1, 1],
                    padding='same',
                    use_bias=use_bias,
                    kernel_regularizer=regularizers.l2(weight_decay),
                    trainable=trainable,
                    name='conv6')(tensor)
    tensor = BatchNormalization(trainable=bn_trainable,
                                name='conv6_bn')(tensor)
    tensor = Activation('relu')(tensor)

    tensor = Conv2D(filters=k * (total_classes + 1 + 4),
                    kernel_size=[1, 1],
                    strides=[1, 1],
                    padding='same',
                    use_bias=use_bias,
                    kernel_regularizer=regularizers.l2(weight_decay),
                    trainable=trainable,
                    name='conv7')(tensor)
    tensor = tf.reshape(tensor=tensor, shape=[-1, total_classes + 5
                                              ])  # (h*w*k, total_classes+1+4)
    clz_tensor = tensor[:, :1 + total_classes]
    clz_tensor = Activation('softmax')(clz_tensor)
    loc_tensor = tensor[:, 1 + total_classes:]
    tensor = tf.concat(values=[clz_tensor, loc_tensor],
                       axis=-1)  # (h*w*k, total_classes+1+4)

    tensor, valid_outputs = nsm(abox_2dtensor=abox_2dtensor,
                                prediction=tensor,
                                nsm_iou_threshold=nsm_iou_threshold,
                                nsm_score_threshold=nsm_score_threshold,
                                nsm_max_output_size=nsm_max_output_size,
                                total_classes=total_classes)
    valid_outputs = tf.cast(x=valid_outputs, dtype='float32')
    valid_outputs = tf.expand_dims(input=valid_outputs, axis=0)

    model = Model(inputs=input_tensor, outputs=[tensor, valid_outputs])
    model.compile(
        optimizer=Adam(),
        loss=[lambda y_true, y_pred: 0.0, lambda y_true, y_pred: 0.0])

    return model
Ejemplo n.º 5
0
	total_faces = 0
	total_pred_faces = 0
	TP = 0
	FP = 0
	FN = 0

	for batch in range(total_test_examples):
		batchx_4dtensor, batchy_2dtensor, bboxes, _ = next(gen)
		batch_loss = model.test_on_batch(batchx_4dtensor, batchy_2dtensor)
		loss[batch] = batch_loss

		prediction = model.predict_on_batch(batchx_4dtensor) # (h1*w1*k1 + h2*w2*k2 + h3*w3*k3, total_classes+1+4)
		boxclz_2dtensor, valid_outputs = nsm(
			abox_2dtensor=abox_2dtensor, 
			prediction=prediction, 
			nsm_iou_threshold=nsm_iou_threshold,
			nsm_score_threshold=nsm_score_threshold,
			nsm_max_output_size=nsm_max_output_size,
			total_classes=total_classes)
		boxclz_2dtensor = boxclz_2dtensor[:valid_outputs]
		pred_bboxes = list(boxclz_2dtensor.numpy())

		total_bboxes = len(bboxes)
		total_pred_bboxes = len(pred_bboxes)
		true_positives = 0

		for i in range(total_bboxes):
			for j in range(total_pred_bboxes):
				iou = comiou(bbox=bboxes[i], pred_bbox=pred_bboxes[j])
				if iou >= iou_thresholds[1]:
					true_positives += 1