深度学习框架tensorflow学习与应用——代码笔记9(未完成)

09-1 测试训练好的模型

1.guithub下载tensorflow-master.zip文件:

https://github.com/tensorflow/tensorflow

解压找到…\tensorflow-master\tensorflow\examples\image_retraining下的retrain.py文件

结果发现.py文件不在该路径下。

下载hub-master.zip文件,解压:

https://github.com/tensorflow/hub

retrain.py文件在…\hub-master\examples\image_retraining路径下。

2.准备数据集

可以到下面网址下载:

http://www.robots.ox.ac.uk/~vgg/data/

在这里插入图片描述

3.打开anaconda3 prompt输入命令:

python D:/python_data/hub-master/examples/image_retraining/retrain.py --bottleneck_dir D:/python_data/bottleneck --how_many_training_steps 200 --model_dir D:/python_model/inception_model/ --output_graph D:/output_graph.pb --output_labels D:/output_labels.txt --image_dir D:/python_data/images/

报错如下:

*INFO:tensorflow:Looking for images in ‘cat’
I1022 16:02:56.827849 17108 retrain.py:182] Looking for images in ‘cat’
INFO:tensorflow:Looking for images in ‘dog’
I1022 16:02:56.859763 17108 retrain.py:182] Looking for images in ‘dog’
INFO:tensorflow:Looking for images in ‘flower’
I1022 16:02:56.892650 17108 retrain.py:182] Looking for images in ‘flower’
I1022 16:02:56.930548 17108 resolver.py:79] Using C:\Users\SSC\AppData\Local\Temp\tfhub_modules to cache modules.
I1022 16:02:56.933573 17108 resolver.py:400] Downloading TF-Hub Module ‘https://tfhub.dev/google/imagenet/inception_v3/feature_vector/3’.
Traceback (most recent call last):
File “D:\ProgramData\Anaconda3\lib\urllib\request.py”, line 1317, in do_open
encode_chunked=req.has_header(‘Transfer-encoding’))
File “D:\ProgramData\Anaconda3\lib\http\client.py”, line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File “D:\ProgramData\Anaconda3\lib\http\client.py”, line 1275, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File “D:\ProgramData\Anaconda3\lib\http\client.py”, line 1224, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File “D:\ProgramData\Anaconda3\lib\http\client.py”, line 1016, in _send_output
self.send(msg)
File “D:\ProgramData\Anaconda3\lib\http\client.py”, line 956, in send
self.connect()
File “D:\ProgramData\Anaconda3\lib\http\client.py”, line 1384, in connect
super().connect()
File “D:\ProgramData\Anaconda3\lib\http\client.py”, line 928, in connect
(self.host,self.port), self.timeout, self.source_address)
File “D:\ProgramData\Anaconda3\lib\socket.py”, line 727, in create_connection
raise err
File “D:\ProgramData\Anaconda3\lib\socket.py”, line 716, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “D:/python_data/hub-master/examples/image_retraining/retrain.py”, line 1350, in
tf.compat.v1.app.run(main=main, argv=[sys.argv[0]] + unparsed)
File “D:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\platform\app.py”, line 40, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File “D:\ProgramData\Anaconda3\lib\site-packages\absl\app.py”, line 299, in run
_run_main(main, args)
File “D:\ProgramData\Anaconda3\lib\site-packages\absl\app.py”, line 250, in _run_main
sys.exit(main(argv))
File “D:/python_data/hub-master/examples/image_retraining/retrain.py”, line 1022, in main
module_spec = hub.load_module_spec(FLAGS.tfhub_module)
File “D:\ProgramData\Anaconda3\lib\site-packages\tensorflow_hub\module.py”, line 60, in load_module_spec
path = registry.resolver(path)
File “D:\ProgramData\Anaconda3\lib\site-packages\tensorflow_hub\registry.py”, line 42, in call
return impl(*args, **kwargs)
File “D:\ProgramData\Anaconda3\lib\site-packages\tensorflow_hub\compressed_module_resolver.py”, line 103, in call
self._lock_file_timeout_sec())
File “D:\ProgramData\Anaconda3\lib\site-packages\tensorflow_hub\resolver.py”, line 402, in atomic_download
download_fn(handle, tmp_dir)
File “D:\ProgramData\Anaconda3\lib\site-packages\tensorflow_hub\compressed_module_resolver.py”, line 98, in download
response = url_opener.open(request)
File “D:\ProgramData\Anaconda3\lib\urllib\request.py”, line 525, in open
response = self._open(req, data)
File “D:\ProgramData\Anaconda3\lib\urllib\request.py”, line 543, in _open
‘_open’, req)
File “D:\ProgramData\Anaconda3\lib\urllib\request.py”, line 503, in _call_chain
result = func(args)
File “D:\ProgramData\Anaconda3\lib\urllib\request.py”, line 1360, in https_open
context=self._context, check_hostname=self._check_hostname)
File “D:\ProgramData\Anaconda3\lib\urllib\request.py”, line 1319, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试 失败。>

无法生成相关文件!!!
output_graph.pb
output_labels.txt
labels.txt

9-1 测试训练好的模型(未完成)

import tensorflow as tf
import os
import numpy as np
import re
from PIL import Image
import matplotlib.pyplot as plt

lines = tf.gfile.GFile('retrain/output_labels.txt').readlines()
uid_to_human = {}
#一行一行读取数据
for uid,line in enumerate(lines) :
    #去掉换行符
    line=line.strip('\n')
    uid_to_human[uid] = line

def id_to_string(node_id):
    if node_id not in uid_to_human:
        return ''
    return uid_to_human[node_id]


#创建一个图来存放google训练好的模型
with tf.gfile.FastGFile('retrain/output_graph.pb', 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    tf.import_graph_def(graph_def, name='')


with tf.Session() as sess:
    softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
    #遍历目录
    for root,dirs,files in os.walk('retrain/images/'):
        for file in files:
            #载入图片
            image_data = tf.gfile.FastGFile(os.path.join(root,file), 'rb').read()
            predictions = sess.run(softmax_tensor,{'DecodeJpeg/contents:0': image_data})#图片格式是jpg格式
            predictions = np.squeeze(predictions)#把结果转为1维数据

            #打印图片路径及名称
            image_path = os.path.join(root,file)
            print(image_path)
            #显示图片
            img=Image.open(image_path)
            plt.imshow(img)
            plt.axis('off')
            plt.show()

            #排序
            top_k = predictions.argsort()[::-1]
            print(top_k)
            for node_id in top_k:     
                #获取分类名称
                human_string = id_to_string(node_id)
                #获取该分类的置信度
                score = predictions[node_id]
                print('%s (score = %.5f)' % (human_string, score))
            print()

9-3 生成tfrecord(未完成)

下载models-master.zip:

https://github.com/tensorflow/models

将…\models-master\research下slim文件夹复制到代码同级目录下。


# coding: utf-8


import tensorflow as tf
import os
import random
import math
import sys


# In[3]:

#验证集数量
_NUM_TEST = 500
#随机种子
_RANDOM_SEED = 0
#数据块
_NUM_SHARDS = 5
#数据集路径
DATASET_DIR = "./slim/images/"
#标签文件名字
LABELS_FILENAME = "./slim/images/labels.txt"

#定义tfrecord文件的路径+名字
def _get_dataset_filename(dataset_dir, split_name, shard_id):
    output_filename = 'image_%s_%05d-of-%05d.tfrecord' % (split_name, shard_id, _NUM_SHARDS)
    return os.path.join(dataset_dir, output_filename)

#判断tfrecord文件是否存在
def _dataset_exists(dataset_dir):
    for split_name in ['train', 'test']:
        for shard_id in range(_NUM_SHARDS):
            #定义tfrecord文件的路径+名字
            output_filename = _get_dataset_filename(dataset_dir, split_name, shard_id)
        if not tf.gfile.Exists(output_filename):
            return False
    return True

#获取所有文件以及分类
def _get_filenames_and_classes(dataset_dir):
    #数据目录
    directories = []
    #分类名称
    class_names = []
    for filename in os.listdir(dataset_dir):
        #合并文件路径
        path = os.path.join(dataset_dir, filename)
        #判断该路径是否为目录
        if os.path.isdir(path):
            #加入数据目录
            directories.append(path)
            #加入类别名称
            class_names.append(filename)

    photo_filenames = []
    #循环每个分类的文件夹
    for directory in directories:
        for filename in os.listdir(directory):
            path = os.path.join(directory, filename)
            #把图片加入图片列表
            photo_filenames.append(path)

    return photo_filenames, class_names

def int64_feature(values):
    if not isinstance(values, (tuple, list)):
        values = [values]
    return tf.train.Feature(int64_list=tf.train.Int64List(value=values))

def bytes_feature(values):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[values]))

def image_to_tfexample(image_data, image_format, class_id):
    #Abstract base class for protocol messages.
    return tf.train.Example(features=tf.train.Features(feature={
      'image/encoded': bytes_feature(image_data),
      'image/format': bytes_feature(image_format),
      'image/class/label': int64_feature(class_id),
    }))

def write_label_file(labels_to_class_names, dataset_dir,filename=LABELS_FILENAME):
    labels_filename = os.path.join(dataset_dir, filename)
    with tf.gfile.Open(labels_filename, 'w') as f:
        for label in labels_to_class_names:
            class_name = labels_to_class_names[label]
            f.write('%d:%s\n' % (label, class_name))

#把数据转为TFRecord格式
def _convert_dataset(split_name, filenames, class_names_to_ids, dataset_dir):
    assert split_name in ['train', 'test']
    #计算每个数据块有多少数据
    num_per_shard = int(len(filenames) / _NUM_SHARDS)
    with tf.Graph().as_default():
        with tf.Session() as sess:
            for shard_id in range(_NUM_SHARDS):
                #定义tfrecord文件的路径+名字
                output_filename = _get_dataset_filename(dataset_dir, split_name, shard_id)
                with tf.python_io.TFRecordWriter(output_filename) as tfrecord_writer:
                    #每一个数据块开始的位置
                    start_ndx = shard_id * num_per_shard
                    #每一个数据块最后的位置
                    end_ndx = min((shard_id+1) * num_per_shard, len(filenames))
                    for i in range(start_ndx, end_ndx):
                        try:
                            sys.stdout.write('\r>> Converting image %d/%d shard %d' % (i+1, len(filenames), shard_id))
                            sys.stdout.flush()
                            #读取图片
                            image_data = tf.gfile.FastGFile(filenames[i], 'r').read()
                            #获得图片的类别名称
                            class_name = os.path.basename(os.path.dirname(filenames[i]))
                            #找到类别名称对应的id
                            class_id = class_names_to_ids[class_name]
                            #生成tfrecord文件
                            example = image_to_tfexample(image_data, b'jpg', class_id)
                            tfrecord_writer.write(example.SerializeToString())
                        except IOError as e:
                            print("Could not read:",filenames[i])
                            print("Error:",e)
                            print("Skip it\n")

    sys.stdout.write('\n')
    sys.stdout.flush()


if __name__ == '__main__':
    #判断tfrecord文件是否存在
    if _dataset_exists(DATASET_DIR):
        print('tfcecord文件已存在')
    else:
        #获得所有图片以及分类
        photo_filenames, class_names = _get_filenames_and_classes(DATASET_DIR)
        #把分类转为字典格式,类似于{'house': 3, 'flower': 1, 'plane': 4, 'guitar': 2, 'animal': 0}
        class_names_to_ids = dict(zip(class_names, range(len(class_names))))

        #把数据切分为训练集和测试集
        random.seed(_RANDOM_SEED)
        random.shuffle(photo_filenames)
        training_filenames = photo_filenames[_NUM_TEST:]
        testing_filenames = photo_filenames[:_NUM_TEST]

        #数据转换
        _convert_dataset('train', training_filenames, class_names_to_ids, DATASET_DIR)
        _convert_dataset('test', testing_filenames, class_names_to_ids, DATASET_DIR)

        #输出labels文件
        labels_to_class_names = dict(zip(range(len(class_names)), class_names))
        write_label_file(labels_to_class_names, DATASET_DIR)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值