cos_img = yc[1][0][0].data.numpy()
sin_img = yc[2][0][0].data.numpy()
width_img = yc[3][0][0].data.numpy()

plt.subplot(231)
plt.title('depth_input')
plt.imshow(depth_img)
plt.subplot(232)
plt.title('rgb_input')
plt.imshow(rgb_img)
plt.subplot(233)
plt.title('pos_input')
plt.imshow(pos_img)
plt.subplot(234)
plt.title('cos_input')
plt.imshow(cos_img)
plt.subplot(235)
plt.title('sin_input')
plt.imshow(sin_img)
plt.subplot(236)
plt.title('width_input')
plt.imshow(width_img)
plt.show()

#实例化一个网络
net = GGCNN(4)

#将输入传递到网络并计算输出
pos, cos, sin, width = net.forward(xc)

#待解决:目前的输入还未经过裁剪处理,结合当前的网络参数还不能获得理想的输出,所以暂时还不能进行训练
    img.crop((left, top), (left + 300, top + 300))

    a = img.img

    a_points = grasps_pre[0].as_gr.points.astype(np.uint8)  #预测出的抓取
    b_points = grasps_true.points
    color1 = (255, 255, 0)
    color2 = (255, 0, 0)
    for i in range(3):
        img = cv2.line(a, tuple(a_points[i]), tuple(a_points[i + 1]),
                       color1 if i % 2 == 0 else color2, 1)
    img = cv2.line(a, tuple(a_points[3]), tuple(a_points[0]), color2, 1)

    color1 = (0, 0, 0)
    color2 = (0, 255, 0)

    for b_point in b_points:
        for i in range(3):
            img = cv2.line(a, tuple(b_point[i]), tuple(b_point[i + 1]),
                           color1 if i % 2 == 0 else color2, 1)
        img = cv2.line(a, tuple(b_point[3]), tuple(b_point[0]), color2, 1)
    #cv2.imshow('img',a)
    cv2.imwrite('img.png', a)
    #cv2.waitKey(1000)


if __name__ == '__main__':
    net = GGCNN(4)
    run(net)
    torch.save(net, 'trained_models/model_v')
Beispiel #3
0
from ggcnn import GGCNN

batch_size = 16

#准备数据集
cornell_data = Cornell('../cornell', output_size=300)
dataset = torch.utils.data.DataLoader(cornell_data, batch_size=batch_size)

#从数据集中读取一个样本
for x, y, _ in dataset:
    xc = x
    yc = y
    break

#实例化一个网络
net = GGCNN(4)

#定义一个优化器
optimizer = optim.Adam(net.parameters())

#设置GPU设备
device = torch.device("cuda:0")

net = net.to(device)

x = xc.to(device)
y = [yy.to(device) for yy in yc]

#动态显示每次优化过后的预测结果
plt.ion()
plt.show()
                result = 0
                for grasp_pre in grasps_pre:
                    if max_iou(grasp_pre, grasps_true) > 0.25:
                        result = 1
                        break

            if result:
                val_result['correct'] += 1
            else:
                val_result['failed'] += 1
    acc = val_result['correct'] / (val_result['correct'] +
                                   val_result['failed'])
    val_result['acc'] = acc
    print(time.ctime())
    print('Correct: {}/{}, val_loss: {:0.4f}, acc: {:0.4f}'.format(
        val_result['correct'], val_result['correct'] + val_result['failed'],
        val_result['loss'].numpy()[0], acc))
    return val_result


if __name__ == '__main__':
    use_gpu = True
    paddle.set_device('gpu:0') if use_gpu else paddle.set_device('cpu')
    net = GGCNN(include_depth + include_rgb * 3)
    paddle.summary(net, (1, 4, 300, 300))
    for epoch_num in range(epoch_nums):
        train_result = train(net, epoch_num, train_batches)
        print('validating...')
        val_result = eval_net(net, val_batches)
        fluid.dygraph.save_dygraph(net.state_dict(), "save_dir/params")