Python 3.x `TypeError:不支持*:';操作';和';int';`。凯拉斯张量流
我正在尝试基于这里的代码实现yolov3模型() 这段代码是用tensor flow 1.x编写的,我正试图将其转换为2.0。我正在尝试进行所有需要的更改,如从Python 3.x `TypeError:不支持*:';操作';和';int';`。凯拉斯张量流,python-3.x,windows,tensorflow,keras,yolo,Python 3.x,Windows,Tensorflow,Keras,Yolo,我正在尝试基于这里的代码实现yolov3模型() 这段代码是用tensor flow 1.x编写的,我正试图将其转换为2.0。我正在尝试进行所有需要的更改,如从tf.Print到tf.Print等,但我被卡在第192行返回损耗*self.grid\u scale。我得到的错误是TypeError:*:“Operation”和“int”的操作数类型不受支持。。 如何获取loss中的值。目前我正在恢复类型操作。我正在尝试获取操作中的值。 我已经尝试过急切地执行,.eval,但仍然无法获得操作的值 完
tf.Print到tf.Print
等,但我被卡在第192行返回损耗*self.grid\u scale
。我得到的错误是TypeError:*:“Operation”和“int”的操作数类型不受支持。
。
如何获取loss
中的值。目前我正在恢复类型操作。我正在尝试获取操作中的值。
我已经尝试过急切地执行,.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行,在
运行文件('C:/Users/Sree/Documents/keras-yolo3-master/train.py',wdir='C:/Users/Sree/Documents/keras-yolo3-master')
文件
“C:\Users\Sree\Anaconda3\envs\tensorflow2\lib\site packages\spyder\u kernels\customize\spyderrcustomize.py”,
第827行,在runfile中
execfile(文件名、命名空间)
文件
“C:\Users\Sree\Anaconda3\envs\tensorflow2\lib\site packages\spyder\u kernels\customize\spyderrcustomize.py”,
第110行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
文件“C:/Users/Sree/Documents/keras-yolo3-master/train.py”,第行
279,在
主(args)
文件“C:/Users/Sree/Documents/keras-yolo3-master/train.py”,第行
总的来说是241
等级比例=配置['train']['class\u scale']
文件“C:/Users/Sree/Documents/keras-yolo3-master/train.py”,第行
131,在创建_模型中
等级刻度=等级刻度
文件“C:\Users\Sree\Documents\keras-yolo3-master\yolo.py”,第320行,
在create_yolov3_模型中
等级(刻度)([输入图像、预测值、真值、真值框])
文件
“C:\Users\Sree\Anaconda3\envs\tensorflow2\lib\site packages\keras\backend\tensorflow\u backend.py”,
第75行,符号包装
返回函数(*args,**kwargs)
文件
“C:\Users\Sree\Anaconda3\envs\tensorflow2\lib\site packages\keras\engine\base\u layer.py”,
第489行,在呼叫中
输出=自调用(输入,**kwargs)
文件“C:\Users\Sree\Documents\keras-yolo3-master\yolo.py”,第206行,
随时待命
返回损耗*self.grid\u刻度
TypeError:不支持*:“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
def YoloLoss(锚点,类=80,忽略阈值=0.5):
def yolo_损失(y_正确,y_预测):
# 1. 转换所有pred输出
#y_pred:(批次尺寸、网格、网格、锚固件,(x、y、w、h、obj、cls))
pred_盒,pred_对象,pred_类,pred_xywh=yolo_盒(
y_pred、锚、类)
pred_xy=pred_xywh[…,0:2]
pred_wh=pred_xywh[…,2:4]
# 2. 转换所有真实输出
#y_true:(批次尺寸、网格、网格、锚固件,(x1、y1、x2、y2、obj、cls))
true\u框,true\u obj,true\u class\u idx=tf.split(
y_真,(4,1,1),轴=-1)
true_xy=(true_box[…,0:2]+true_box[…,2:4])/2
真盒[…,2:4]-真盒[…,0:2]
#给小盒子增加重量
方框损失量表=2-真值[万用表[…,0]*真值[万用表[…,1]
# 3. 反演pred-box方程
网格大小=tf.形状(y_真)[1]
网格=tf.meshgrid(tf.range(网格大小),tf.range(网格大小))
网格=tf.expand_dims(tf.stack(网格,轴=-1,轴=2)
true_xy=true_xy*tf.cast(网格大小,tf.float32)-\
tf.cast(网格,tf.float32)
true\u wh=tf.math.log(true\u wh/anchors)
true\u wh=tf.where(tf.math.is\u inf(true\u wh),
tf.zero_like(真wh,真wh)
# 4. 计算所有遮罩
对象掩码=tf.挤压(真对象,-1)
#当iou超过阈值时忽略假阳性
最佳iou=tf.map\u fn(
lambda x:tf.reduce\u max(广播iou(x[0],tf.boolean\u掩码(
x[1],tf.cast(x[2],tf.bool))),轴=-1),
(pred_盒、true_盒、obj_面具),
tf.32)
忽略屏蔽=tf.cast(最佳iou(批次、1)
xy_损失=tf。减少_总和(xy_损失,轴=(1,2,3))
wh_损失=tf.减少wh_总和(wh_损失,轴=(1,2,3))
obj_损失=tf。减少总和(obj_损失,轴=(1,2,3))
类别损失=tf。减少总和(类别损失,轴=(1,2,3))
返回xy_损失+wh_损失+obj_损失+类别损失
返回yolo_损失
有关Tensorflow 2.0中的详细代码,您可以按照此操作,您需要像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
def YoloLoss(锚点,类=80,忽略阈值=0.5):
def yolo_损失(y_正确,y_预测):
# 1. 转换所有pred输出
#y_pred:(批次尺寸、网格、网格、锚固件,(x、y、w、h、obj、cls))
pred_盒,pred_对象,pred_类,pred_xywh=yolo_盒(
y_pred、锚、类)
pred_xy=pred_xywh[…,0:2]
pred_wh=pred_xywh[…,2:4]
# 2. 转换所有真实输出