前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C-SATS工程副总裁教你如何用TensorFlow分类图像 part2

C-SATS工程副总裁教你如何用TensorFlow分类图像 part2

作者头像
AiTechYun
发布2018-03-06 11:13:11
7840
发布2018-03-06 11:13:11
举报
文章被收录于专栏:ATYUN订阅号ATYUN订阅号

我们都知道,适当的训练对于将来有效的分类来说至关重要,为了训练工作,我们需要大量准确标记的数据。在第一部分中,我通过下载3000个预先标记的图像跳过了这个挑战。然后我向你展示了在TensorFlow中如何使用带标签的数据训练分类器。在这一部分,我们将使用新的数据集进行训练,并且我将介绍数据可视化工具TensorBoard,以便更好地理解,调试和优化我们的TensorFlow代码。

  • 第一部分:http://www.atyun.com/13211_c-sats工程副总裁教你如何用TensorFlow分类图像-part1.html

鉴于我在医疗技术公司C-SATS担任工程副总裁的工作,我希望建立一个与手术有关的分类器。缝合似乎会很不错。它立即投入使用,并且我知道如何识别它。如果机器能够看到缝合正在发生,它可以自动识别缝合手术过程的步骤(或阶段),例如吻合术。并且,因为外科缝线的针和线比较独特,甚至外行也能辨认出来。

我的目标是训练一台识别医学视频中缝合的机器。

我可以访问数十亿帧的手术视频,其中许多包含缝合。但是这样还会面临标记的问题。幸运的是,C-SATS拥有一批经验丰富的注释师,他们是做这件事的专家。我的源数据是JSON中的视频文件和注释。

注释样本如下:

代码语言:javascript
复制
[
    {
        "annotations": [
            {
                "endSeconds": 2115.215,
                "label": "suturing",
                "startSeconds": 2319.541
            },
            {
                "endSeconds": 2976.301,
                "label": "suturing",
                "startSeconds": 2528.884
            }
        ],
        "durationSeconds": 2975,
        "videoId": 5
    },
    {
        "annotations": [
        // ...etc...

我写了一个Python脚本来使用JSON注释来决定从视频文件中抓取哪些帧。ffmpeg做实际的抓取。我决定每秒最多抓取一帧,然后我将视频秒的总数除以四,得到10k秒(10k帧)。在找出要抓取的秒数之后,我进行了一个快速测试,看看缝合注释内是否有特定的秒(isWithinSuturingSegment())。下面是grab.py的代码

代码语言:javascript
复制
#!/usr/bin/python
 
# Grab frames from videos with ffmpeg. Use multiple cores.
# Minimum resolution is 1 second--this is a shortcut to get less frames.
 
# (C)2017 Adam Monsen. License: AGPL v3 or later.
 
import json
import subprocess
from multiprocessing import Pool
import os
 
frameList = []
 
def isWithinSuturingSegment(annotations, timepointSeconds):
    for annotation in annotations:
        startSeconds = annotation['startSeconds']
        endSeconds = annotation['endSeconds']
        if timepointSeconds > startSeconds and timepointSeconds < endSeconds:
            return True
    return False
 
with open('available-suturing-segments.json') as f:
    j = json.load(f)
 
    for video in j:
        videoId = video['videoId']
        videoDuration = video['durationSeconds']
 
        # generate many ffmpeg frame-grabbing commands
        start = 1
        stop = videoDuration
        step = 4 # Reduce to grab more frames
        for timepointSeconds in xrange(start, stop, step):
            inputFilename = '/home/adam/Downloads/suturing-videos/{}.mp4'.format
(videoId)
            outputFilename = '{}-{}.jpg'.format(video['videoId'], 
timepointSeconds)
            if isWithinSuturingSegment(video['annotations'], timepointSeconds):
                outputFilename = 'suturing/{}'.format(outputFilename)
            else:
                outputFilename = 'not-suturing/{}'.format(outputFilename)
            outputFilename = '/home/adam/local/{}'.format(outputFilename)
 
            commandString = 'ffmpeg -loglevel quiet -ss {} -i {} -frames:v 1 {}'.
format(
                timepointSeconds, inputFilename, outputFilename)
 
            frameList.append({
                'outputFilename': outputFilename,
                'commandString': commandString,
            })
 
def grabFrame(f):
    if os.path.isfile(f['outputFilename']):
        print 'already completed {}'.format(f['outputFilename'])
    else:
        print 'processing {}'.format(f['outputFilename'])
        subprocess.check_call(f['commandString'].split())
 
p = Pool(4) # for my 4-core laptop
p.map(grabFrame, frameList)

现在我们像上次那样,再次对这些模型进行再训练。

使用这个脚本来剪出10k帧用了大约10分钟,然后花费一个小时左右,进行再训练识别缝合达到90%的准确性。我使用了训练集之外的新数据进行了抽查,我试过的每一帧都被正确识别(平均置信度分数为88%,中位数置信度分数为91%)。

下面是我的抽查结果。

因图片血腥无法上传可访问网址查看(https://opensource.com/article/17/12/how-to-tensorboard)

如何使用TensorBoard

深度学习与任何其他类型的软件一样,很难可视化内部的原理和交互。幸好我们可以使用TensorBoard。

第一部分的Retrain.py自动生成文件TensorBoard以用于生成表示再训练期间发生了什么的图。

要安装TensorBoard,运行retrain.py后在容器中运行下面代码

代码语言:javascript
复制
pip install tensorboard
tensorboard --logdir /tmp/retrain_logs

如果看到下面的输出,就使用浏览器浏览器打开下方网址。

代码语言:javascript
复制
Starting TensorBoard 41 on port 6006
(You can navigate to http://172.17.0.2:6006)

你会看到这样的东西(点击下方视频链接):

  • http://imgcdn.atyun.com/2017/12/short-TensorBoard-screencast-1.mp4?_=1

我希望这个视频能够帮到你。在再训练时,我发现在“SCALARS”选项下可以看到,当我们执行更多的训练步骤时或交叉熵减少时准确性如何提高。这就是是我们想要了解的。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-12-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 ATYUN订阅号 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 如何使用TensorBoard
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档