我正试图根据这里的代码实现yolov3模型(https:/github.comexperiencorkeras-yolo3blobmasteryolo.py。)
这段代码是用tensor-flow 1.x写的,我想把它转换成2.0。我正在尝试做所有需要的改变,例如 tf.Print to tf.print
等,但卡在了192行 return loss*self.grid_scale
. 我得到的错误是 TypeError: unsupported operand type(s) for *: 'Operation' and 'int'
.我如何在 loss
. 目前,我得到的类型操作回。我试图获取操作中的值,我已经尝试了 execute_eagerly,.eval
但仍然无法获得操作值。
完整的代码在上面的链接中。而主要的片段是
loss = tf.print('avg_obj \t\t:',loss, [grid_h, avg_obj],output_stream=sys.stderr)
loss = tf.print(loss, [grid_h, avg_noobj], 'avg_noobj \t\t', summarize=1000)
loss = tf.print(loss, [grid_h, avg_iou], 'avg_iou \t\t', summarize=1000)
loss = tf.print(loss, [grid_h, avg_cat], 'avg_cat \t\t', summarize=1000)
loss = tf.print(loss, [grid_h, recall50], 'recall50 \t', summarize=1000)
loss = tf.print(loss, [grid_h, recall75], 'recall75 \t', summarize=1000)
loss = tf.print(loss, [grid_h, count], 'count \t', summarize=1000)
loss = tf.print(loss, [grid_h, tf.reduce_sum(loss_xy),
tf.reduce_sum(loss_wh),
tf.reduce_sum(loss_conf),
tf.reduce_sum(loss_class)], 'loss xy, wh, conf, class: \t', summarize=1000)
return loss*self.grid_scale
以下是完整的错误跟踪
回溯(最近一次调用)。
文件"",第1行,在runfile('C:UsersSreeDocumentskeras-yolo3-mastertrain.py', wdir='C:UsersSreeDocumentskeras-yolo3-master')
文件 "C:\Users\Sree\Anaconda3\envs\tensorflow2\lib\site-packages\spyder_kernels\customize\spydercustomize.py",第827行,在运行文件execfile(filename, namespace) 中。
文件 "C:\Users\Sree\Anaconda3\envs\tensorflow2\lib\site-packages\spyder_kernels\customize\spydercustomize.py",第110行,在execfile exec(compile(f.read(), filename, 'exec'), namespace中。)
文件 "C:UsersSreeDocumentskeras-yolo3-mastertrain.py",第279行,在 主(args)
文件 "C:UsersSreeDocumentskeras-yolo3-mastertrain.py",第241行,在 主 class_scale = config['train']['class_scale'],
文件 "C:UsersSreeDocumentskeras-yolo3-mastertrain.py",第131行,在create_model中,class_scale = class_scale。
文件 "C:\Users\SreeDocuments\keras-yolo3-master\yolo.py", 第320行, in create_yolov3_model class_scale)([input_image, pred_yolo_1, true_yolo_1, true_boxes])
文件 "C:\Users\Sree\Anaconda3\envs\tensorflow2\lib\sit-packages\keras/backend\tensorflow_backend.py", 第75行, 在 symbolic_fn_wrapper return func(*args, **kwargs)中。
文件 "C:/Users/SreeAnaconda3/envs/tensorflow2/lib/site-packages/kerasengine/base_layer.py",第489行,in 召唤 output = self.call(inputs, **kwargs)
文件 "C:\Users\SreeDocuments\keras-yolo3-master\yolo.py",第206行,在调用返回损失*self.grid_scale中
TypeError: unsupported operand type(s) for *: 'Operation'和'int'
你需要像下面提到的Tensorflow 2.0中的代码一样定义损失。
def YoloLoss(anchors, classes=80, ignore_thresh=0.5):
def yolo_loss(y_true, y_pred):
# 1. transform all pred outputs
# y_pred: (batch_size, grid, grid, anchors, (x, y, w, h, obj, ...cls))
pred_box, pred_obj, pred_class, pred_xywh = yolo_boxes(
y_pred, anchors, classes)
pred_xy = pred_xywh[..., 0:2]
pred_wh = pred_xywh[..., 2:4]
# 2. transform all true outputs
# y_true: (batch_size, grid, grid, anchors, (x1, y1, x2, y2, obj, cls))
true_box, true_obj, true_class_idx = tf.split(
y_true, (4, 1, 1), axis=-1)
true_xy = (true_box[..., 0:2] + true_box[..., 2:4]) / 2
true_wh = true_box[..., 2:4] - true_box[..., 0:2]
# give higher weights to small boxes
box_loss_scale = 2 - true_wh[..., 0] * true_wh[..., 1]
# 3. inverting the pred box equations
grid_size = tf.shape(y_true)[1]
grid = tf.meshgrid(tf.range(grid_size), tf.range(grid_size))
grid = tf.expand_dims(tf.stack(grid, axis=-1), axis=2)
true_xy = true_xy * tf.cast(grid_size, tf.float32) - \
tf.cast(grid, tf.float32)
true_wh = tf.math.log(true_wh / anchors)
true_wh = tf.where(tf.math.is_inf(true_wh),
tf.zeros_like(true_wh), true_wh)
# 4. calculate all masks
obj_mask = tf.squeeze(true_obj, -1)
# ignore false positive when iou is over threshold
best_iou = tf.map_fn(
lambda x: tf.reduce_max(broadcast_iou(x[0], tf.boolean_mask(
x[1], tf.cast(x[2], tf.bool))), axis=-1),
(pred_box, true_box, obj_mask),
tf.float32)
ignore_mask = tf.cast(best_iou < ignore_thresh, tf.float32)
# 5. calculate all losses
xy_loss = obj_mask * box_loss_scale * \
tf.reduce_sum(tf.square(true_xy - pred_xy), axis=-1)
wh_loss = obj_mask * box_loss_scale * \
tf.reduce_sum(tf.square(true_wh - pred_wh), axis=-1)
obj_loss = binary_crossentropy(true_obj, pred_obj)
obj_loss = obj_mask * obj_loss + \
(1 - obj_mask) * ignore_mask * obj_loss
# TODO: use binary_crossentropy instead
class_loss = obj_mask * sparse_categorical_crossentropy(
true_class_idx, pred_class)
# 6. sum over (batch, gridx, gridy, anchors) => (batch, 1)
xy_loss = tf.reduce_sum(xy_loss, axis=(1, 2, 3))
wh_loss = tf.reduce_sum(wh_loss, axis=(1, 2, 3))
obj_loss = tf.reduce_sum(obj_loss, axis=(1, 2, 3))
class_loss = tf.reduce_sum(class_loss, axis=(1, 2, 3))
return xy_loss + wh_loss + obj_loss + class_loss
return yolo_loss
对于Tensorflow 2.0中的详细代码,你可以按照这个来做。衔接。