def decode(example): feature = tf.io.parse_single_example(example, feature_discription) data = tf.image.decode_jpeg(feature['data']) data = tf.image.convert_image_dtype(data, tf.float32) label = feature['label'] label = kbe.one_hot(label, num_classes=10) label = kbe.squeeze(label, axis=0) return data, label
def sparse_accuracy_ignoring_last_label(y_true, y_pred): nb_classes = K.int_shape(y_pred)[-1] y_pred = K.reshape(y_pred, (-1, nb_classes)) y_true = K.one_hot(tf.to_int32(K.flatten(y_true)), nb_classes + 1) unpacked = tf.unstack(y_true, axis=-1) legal_labels = ~tf.cast(unpacked[-1], tf.bool) y_true = tf.stack(unpacked[:-1], axis=-1) return K.sum(tf.to_float(legal_labels & K.equal(K.argmax(y_true, axis=-1), K.argmax(y_pred, axis=-1)))) / K.sum(tf.to_float(legal_labels))
def Mask(self, inputs, seq_len, mode='mul'): if seq_len is None: return inputs else: mask = K.one_hot(seq_len[:, 0], K.shape(inputs)[1]) mask = 1 - K.cumsum(mask, 1) for _ in range(len(inputs.shape) - 2): mask = K.expand_dims(mask, 2) if mode == 'mul': return inputs * mask if mode == 'add': return inputs - (1 - mask) * 1e12
def softmax_sparse_crossentropy_ignoring_last_label(y_true, y_pred): y_pred = K.reshape(y_pred, (-1, K.int_shape(y_pred)[-1])) log_softmax = tf.nn.log_softmax(y_pred) y_true = K.one_hot(tf.to_int32(K.flatten(y_true)), K.int_shape(y_pred)[-1]+1) unpacked = tf.unstack(y_true, axis=-1) y_true = tf.stack(unpacked[:-1], axis=-1) cross_entropy = -K.sum(y_true * log_softmax, axis=1) cross_entropy_mean = K.mean(cross_entropy) return cross_entropy_mean
def call(self, inputs, mask=None): cos_m = math.cos(self.m) sin_m = math.sin(self.m) mm = sin_m * self.m threshold = math.cos(math.pi - self.m) # features X = inputs[0] # 1-D or one-hot label works as mask Y_mask = inputs[1] # If Y_mask is not in one-hot form, transfer it to one-hot form. if Y_mask.shape[-1] == 1: Y_mask = K.cast(Y_mask, tf.int32) Y_mask = K.reshape(K.one_hot(Y_mask, self.class_num), (-1, self.class_num)) X_normed = K.l2_normalize(X, axis=1) # L2 Normalized X W_normed = K.l2_normalize(self.W, axis=0) # L2 Normalized Weights # cos(theta + m) cos_theta = K.dot(X_normed, W_normed) # 矩阵乘法 cos_theta2 = K.square(cos_theta) sin_theta2 = 1. - cos_theta2 sin_theta = K.sqrt(sin_theta2 + K.epsilon()) cos_tm = self.s * ((cos_theta * cos_m) - (sin_theta * sin_m)) # This condition controls the theta + m should in range [0, pi] # 0 <= theta + m < = pi # -m <= theta <= pi - m cond_v = cos_theta - threshold cond = K.cast(K.relu(cond_v), dtype=tf.bool) keep_val = self.s * (cos_theta - mm) cos_tm_temp = tf.where(cond, cos_tm, keep_val) # mask by label # Y_mask =+ K.epsilon() # Why??? inv_mask = 1. - Y_mask s_cos_theta = self.s * cos_theta output = K.softmax((s_cos_theta * inv_mask) + (cos_tm_temp * Y_mask)) return output
def call(self, inputs, **kwargs): if type(inputs) is list: assert len(inputs) == 2 input, mask = inputs _, hei, wid, _, _ = input.get_shape() if self.resize_masks: mask = tf.image.resize_bicubic(mask, (hei.value, wid.value)) mask = K.expand_dims(mask, -1) if input.get_shape().ndims == 3: masked = K.batch_flatten(mask * input) else: masked = mask * input else: if inputs.get_shape().ndims == 3: x = K.sqrt(K.sum(K.square(inputs), -1)) mask = K.one_hot(indices=K.argmax(x, 1), num_classes=x.get_shape().as_list()[1]) masked = K.batch_flatten(K.expand_dims(mask, -1) * inputs) else: masked = inputs return masked
def one_hot(x, num_classes): return K.one_hot(x, num_classes=num_classes)
def _make_relative_position_table(self): from_position = np.arange(self.num_memory_slots + 1)[:, None] to_position = np.arange(self.num_memory_slots + 1)[None, :] table = from_position - to_position + self.num_memory_slots return K.cast(K.one_hot(table, 2 * self.num_memory_slots + 1), tf.float32)
def yolo_loss(yolo_output, true_boxes, detectors_mask, matching_true_boxes, anchors, num_classes, rescore_confidence=False, print_loss=False): """YOLO localization loss function. Parameters ---------- yolo_output : tf.Tensor Final convolutional layer features. true_boxes : tf.Tensor Ground truth boxes tensor with shape [batch, num_true_boxes, 5] containing box x_center, y_center, width, height, and class. detectors_mask : np.ndarray 0/1 mask for detector positions where there is a matching ground truth. matching_true_boxes : np.ndarray Corresponding ground truth boxes for positive detector positions. Already adjusted for conv height and width. anchors : np.ndarray Anchor boxes for model. num_classes : int Number of object classes. rescore_confidence : bool, default=False If true then set confidence target to IOU of best predicted box with the closest matching ground truth box. print_loss : bool, default=False If True then print the loss components. Returns ------- mean_loss : float Mean localization loss across minibatch """ num_anchors = len(anchors) object_scale = 5 no_object_scale, class_scale, coordinates_scale = 1, 1, 1 pred_xy, pred_wh, pred_confidence, pred_class_prob = yolo_head( yolo_output, anchors, num_classes) # Unadjusted box predictions for loss. # TODO: Remove extra computation shared with yolo_head. yolo_output_shape = K.shape(yolo_output) feats = K.reshape(yolo_output, [ -1, yolo_output_shape[1], yolo_output_shape[2], num_anchors, num_classes + 5 ]) pred_boxes = K.concatenate((K.sigmoid(feats[..., 0:2]), feats[..., 2:4]), axis=-1) # TODO: Adjust predictions by image width/height for non-square images? # IOUs may be off due to different aspect ratio. # Expand pred x,y,w,h to allow comparison with ground truth. # batch, conv_height, conv_width, num_anchors, num_true_boxes, box_params pred_xy = K.expand_dims(pred_xy, 4) pred_wh = K.expand_dims(pred_wh, 4) pred_wh_half = pred_wh / 2. pred_mins = pred_xy - pred_wh_half pred_maxes = pred_xy + pred_wh_half true_boxes_shape = K.shape(true_boxes) # batch, conv_height, conv_width, num_anchors, num_true_boxes, box_params true_boxes = K.reshape(true_boxes, [ true_boxes_shape[0], 1, 1, 1, true_boxes_shape[1], true_boxes_shape[2] ]) true_xy = true_boxes[..., 0:2] true_wh = true_boxes[..., 2:4] # Find IOU of each predicted box with each ground truth box. true_wh_half = true_wh / 2. true_mins = true_xy - true_wh_half true_maxes = true_xy + true_wh_half intersect_mins = K.maximum(pred_mins, true_mins) intersect_maxes = K.minimum(pred_maxes, true_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_areas = intersect_wh[..., 0] * intersect_wh[..., 1] pred_areas = pred_wh[..., 0] * pred_wh[..., 1] true_areas = true_wh[..., 0] * true_wh[..., 1] union_areas = pred_areas + true_areas - intersect_areas iou_scores = intersect_areas / union_areas # Best IOUs for each location. best_ious = K.max(iou_scores, axis=4) # Best IOU scores. best_ious = K.expand_dims(best_ious) # A detector has found an object if IOU > thresh for some true box. object_detections = K.cast(best_ious > 0.6, K.dtype(best_ious)) # TODO: Darknet region training includes extra coordinate loss for early # TODO: training steps to encourage predictions to match anchor priors. # Determine confidence weights from object and no_object weights. # NOTE: YOLO does not use binary cross-entropy here. no_object_weights = (no_object_scale * (1 - object_detections) * (1 - detectors_mask)) no_objects_loss = no_object_weights * K.square(-pred_confidence) if rescore_confidence: objects_loss = (object_scale * detectors_mask * K.square(best_ious - pred_confidence)) else: objects_loss = (object_scale * detectors_mask * K.square(1 - pred_confidence)) confidence_loss = objects_loss + no_objects_loss # Classification loss for matching detections. # NOTE: YOLO does not use categorical cross-entropy loss here. matching_classes = K.cast(matching_true_boxes[..., 4], 'int32') matching_classes = K.one_hot(matching_classes, num_classes) classification_loss = (class_scale * detectors_mask * K.square(matching_classes - pred_class_prob)) # Coordinate loss for matching detection boxes. matching_boxes = matching_true_boxes[..., 0:4] coordinates_loss = (coordinates_scale * detectors_mask * K.square(matching_boxes - pred_boxes)) confidence_loss_sum = K.sum(confidence_loss) classification_loss_sum = K.sum(classification_loss) coordinates_loss_sum = K.sum(coordinates_loss) total_loss = 0.5 * (confidence_loss_sum + classification_loss_sum + coordinates_loss_sum) if print_loss: # TODO: printing Tensor values. Maybe use eval function or session? print( 'yolo_loss: {}, conf_loss: {}, class_loss: {}, box_coord_loss: {}'. format(total_loss, confidence_loss_sum, classification_loss_sum, coordinates_loss_sum)) return total_loss
def yolo_loss(y_true, y_pred, anchors, num_classes, batch_size, ignore_thresh=.5, print_loss=False): '''Return yolo_loss tensor Parameters ---------- yolo_outputs: list of tensor, the output of yolo_body or tiny_yolo_body y_true: list of array, the output of preprocess_true_boxes anchors: array, shape=(N, 2), wh num_classes: integer ignore_thresh: float, the iou threshold whether to ignore object confidence loss Returns ------- loss: tensor, shape=(1,) ''' num_layers = len(anchors) // 3 # default setting # yolo_outputs = args[:num_layers] # y_true = args[num_layers:] y_true = y_true yolo_outputs = y_pred print(y_true) print(yolo_outputs) anchor_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2] ] if num_layers == 3 else [[3, 4, 5], [1, 2, 3]] input_shape = K.cast( K.shape(yolo_outputs[0])[1:3] * 32, K.dtype(y_true[0])) grid_shapes = [ K.cast(K.shape(yolo_outputs[l])[1:3], K.dtype(y_true[0])) for l in range(num_layers) ] loss = 0 m = K.shape(yolo_outputs[0])[0] # batch size, tensor mf = K.cast(m, K.dtype(yolo_outputs[0])) for l in range(num_layers): object_mask = y_true[l][..., 4:5] true_class_probs = K.one_hot(K.cast(y_true[l][..., 5], tf.int64), num_classes) # true_class_probs = y_true[l][..., 5:] grid, raw_pred, pred_xy, pred_wh = yolo_head(yolo_outputs[l], anchors[anchor_mask[l]], num_classes, input_shape, calc_loss=True) pred_box = K.concatenate([pred_xy, pred_wh]) # Darknet raw box to calculate loss. raw_true_xy = y_true[l][..., :2] * grid_shapes[l][::-1] - grid raw_true_wh = K.log(y_true[l][..., 2:4] / anchors[anchor_mask[l]] * input_shape[::-1]) raw_true_wh = K.switch(object_mask, raw_true_wh, K.zeros_like(raw_true_wh)) # avoid log(0)=-inf box_loss_scale = 2 - y_true[l][..., 2:3] * y_true[l][..., 3:4] # Find ignore mask, iterate over each of batch. ignore_mask = tf.TensorArray(K.dtype(y_true[0]), size=1, dynamic_size=False) object_mask_bool = K.cast(object_mask, 'bool') def _loop_body(b, ignore_mask): true_box = y_true[l][ b, ..., 0: 4] # tf.boolean_mask(y_true[l][b,...,0:4], object_mask_bool[b,...,0]) iou = box_iou(pred_box[b], true_box) best_iou = K.max(iou, axis=-1) ignore_mask = ignore_mask.write( b, K.cast(best_iou < ignore_thresh, K.dtype(true_box))) return b + 1, ignore_mask def loop_body(b): true_box = y_true[l][ b, ..., 0: 4] # tf.boolean_mask(y_true[l][b,...,0:4], object_mask_bool[b,...,0]) iou = box_iou(pred_box[b], true_box) best_iou = K.max(iou, axis=-1) ignore_mask = K.cast(best_iou < ignore_thresh, K.dtype(true_box)) return ignore_mask # mにバッチサイズを入れれば動的tensorが必要なくなる # _, ignore_mask = K.control_flow_ops.while_loop(lambda b,*args: b<m, loop_body, [0, ignore_mask]) ignore_mask = [loop_body(b) for b in range(batch_size)] # ignore_mask = ignore_mask.stack() ignore_mask = K.stack(ignore_mask) ignore_mask = K.expand_dims(ignore_mask, -1) ignore_mask = K.expand_dims(ignore_mask, -1) # K.binary_crossentropy is helpful to avoid exp overflow. xy_loss = object_mask * box_loss_scale * K.binary_crossentropy( raw_true_xy, raw_pred[..., 0:2], from_logits=True) wh_loss = object_mask * box_loss_scale * 0.5 * K.square( raw_true_wh - raw_pred[..., 2:4]) confidence_loss = object_mask * K.binary_crossentropy(object_mask, raw_pred[...,4:5], from_logits=True)+ \ (1-object_mask) * K.binary_crossentropy(object_mask, raw_pred[...,4:5], from_logits=True) * ignore_mask class_loss = object_mask * K.binary_crossentropy( true_class_probs, raw_pred[..., 5:], from_logits=True) xy_loss = K.sum(xy_loss) / mf wh_loss = K.sum(wh_loss) / mf confidence_loss = K.sum(confidence_loss) / mf class_loss = K.sum(class_loss) / mf loss += xy_loss + wh_loss + confidence_loss + class_loss if print_loss: loss = tf.Print(loss, [ loss, xy_loss, wh_loss, confidence_loss, class_loss, K.sum(ignore_mask) ], message='loss: ') return loss
import json import random import pickle as pkl def normalize(r): return (r / 64) - 1 data_x = [] data_y = [] str_data = [i.replace("\n", "") for i in open("RAWDAT", "r")] for index, line in enumerate(str_data): data_x.append(np.array([normalize(float(i)) for i in line.strip("][").split(",")])) data_y.append(one_hot(index % 5, 5)) data_x_norm = data_x data_y_norm = data_y data_x = [] data_y = [] for index in range(len(data_x_norm)): rnum = random.randrange(0, len(data_x_norm)) data_x.append(data_x_norm.pop(rnum)) data_y.append(data_y_norm.pop(rnum)) data_x_train = data_x[:25] data_y_train = data_y[:25]
def loss_layer(x, anchors, num_classes=4, input_shape=(416, 416)): in_shape = tf.constant(input_shape, dtype=tf.float32) # anchors = K.constant(anchors) detectors_mask = x[:3] matching_true_boxes = x[3:6] true_box_all = x[6] ys = x[7:10] object_scale = 5 no_object_scale = 1 class_scale = 1 coordinates_scale = 1 m = K.shape(ys[0])[0] # batch size, tensor loss = 0 mf = K.cast(m, K.dtype(ys[0])) for index, y in enumerate(ys): box_xy_predict, box_wh_predict, box_confidence_predict, box_classes_predict, box_coord_predict = \ yolo_parse_output(y, anchors=anchors[(index*3):(index*3+3), :], input_shape=in_shape, num_classes=num_classes) box_xy_predict = K.expand_dims(box_xy_predict, 4) box_wh_predict = K.expand_dims(box_wh_predict, 4) pred_wh_half = box_wh_predict / 2. pred_mins = box_xy_predict - pred_wh_half pred_maxes = box_xy_predict + pred_wh_half true_box = true_box_all[:, index, :, :] true_boxes_shape = K.shape(true_box) # batch, conv_height, conv_width, num_anchors, num_true_boxes, box_params true_boxes = K.reshape(true_box, [ true_boxes_shape[0], 1, 1, 1, true_boxes_shape[1], true_boxes_shape[2] ]) true_xy = true_boxes[..., 0:2] true_wh = true_boxes[..., 2:4] # Find IOU of each predicted box with each ground truth box. true_wh_half = true_wh / 2. true_mins = true_xy - true_wh_half true_maxes = true_xy + true_wh_half intersect_mins = K.maximum(pred_mins, true_mins) intersect_maxes = K.minimum(pred_maxes, true_maxes) intersect_wh = K.maximum(intersect_maxes - intersect_mins, 0.) intersect_areas = intersect_wh[..., 0] * intersect_wh[..., 1] pred_areas = box_wh_predict[..., 0] * box_wh_predict[..., 1] true_areas = true_wh[..., 0] * true_wh[..., 1] union_areas = pred_areas + true_areas - intersect_areas iou_scores = intersect_areas / union_areas # Best IOUs for each location. best_ious = K.max(iou_scores, axis=4) # Best IOU scores. best_ious = K.expand_dims(best_ious) object_detections = K.cast(best_ious > 0.4, dtype=K.dtype(best_ious)) no_object_weights = (no_object_scale * (1 - object_detections) * (1 - detectors_mask[index])) no_objects_loss = no_object_weights * K.binary_crossentropy( detectors_mask[index], box_confidence_predict, from_logits=False) object_loss = object_scale * detectors_mask[ index] * K.binary_crossentropy(detectors_mask[index], box_confidence_predict, from_logits=False) xy_loss = (coordinates_scale * detectors_mask[index] * K.binary_crossentropy(matching_true_boxes[index][..., 0:2], box_coord_predict[..., 0:2], from_logits=False)) wh_loss = (coordinates_scale * detectors_mask[index] * K.square(matching_true_boxes[index][..., 2:4] - box_coord_predict[..., 2:4])) matching_classes = K.cast(matching_true_boxes[index][..., 4], 'int32') matching_classes = K.one_hot(matching_classes, num_classes) classification_loss = ( class_scale * detectors_mask[index] * K.binary_crossentropy( matching_classes, box_classes_predict, from_logits=False)) no_objects_loss = K.sum(no_objects_loss) object_loss = K.sum(object_loss) xy_loss = K.sum(xy_loss) wh_loss = K.sum(wh_loss) classification_loss = K.sum(classification_loss) total_loss = no_objects_loss + object_loss + xy_loss + wh_loss + classification_loss loss += total_loss losses = loss / mf return losses