以下都是在windows 10环境下进行安装编译
tf models最麻烦的地方就是要安装对应版本的python
而 git上的教程都很老了,安装tensorflow总是会出现各种问题
现在tf 2.x 版本 需要python3.5在以上
tensorflow models 官方安装教程
tensorflow models
下载最新的tf models,链接教程里面有源码连接
Anaconda版本:
Anaconda3-2020.02-Windows-x86_64.exe
安装完anaconda 后
要以 管理员身份运行
创建环境
conda create -n tensorflow1 pip python=3.8
网络不行的时候创建虚拟环境都会失败
自行在网上查找 anaconda 替换源 或者 添加代理的方法
C:\Users\xxx 下有个 .condarc 文件是anaconda的config文件
激活环境
conda activate tensorflow1
在**.condarc**目录下创建pip文件夹,里面创建pip.ini替换pip源
pip.ini 文件内容
[global]
index-url=https://pypi.doubanio.com/simple/
[install]
trusted-host=pypi.douban.com
disable-pip-version-check = true
timeout = 6000
这里替换的是豆瓣源 可以在网上查找其他源
然后就是按照官方教程
安装tensorflow
安装完tensorflow后开始编译tf models
按照官方的教程
在models\research路径下运行
python setup.py build
python setup.py install
最新的源码这个路径是没setup.py的,这个文件被藏在其他地方了
直接搜索然后拉到这个目录下就行了
如果install的时候提示某些包版本过高或者过低,运行
pip install a==b
a表示某个包
b表示对应的版本
如果最终提示了tensorflow版本过低
表示 ts models的源码版本过高了,找低版本的就行了
编译完后
官方教程运行 object_detection_tutorial.ipynb
这个文件也被藏起来了
直接搜索拖到当前目录下就好了
然后创建个新的虚拟环境
pip install labelimg
labelimg
运行这个软件
对图片进行标记
按照官网教程在object_detection下创建images/train 和 images/test文件夹
按照官网教程运行两个脚本
不用去下那两个脚本了
没用
直接贴代码
xml_to_csv.py
import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET
def xml_to_csv(path):
xml_list = []
for xml_file in glob.glob(path + '/*.xml'):
tree = ET.parse(xml_file)
root = tree.getroot()
for member in root.findall('object'):
value = (root.find('filename').text,
int(root.find('size')[0].text),
int(root.find('size')[1].text),
member[0].text,
int(member[4][0].text),
int(member[4][1].text),
int(member[4][2].text),
int(member[4][3].text)
)
xml_list.append(value)
column_name = ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
xml_df = pd.DataFrame(xml_list, columns=column_name)
return xml_df
def main():
# train xml to csv
cwd_path = os.getcwd();
print('cwd path: %s' % (cwd_path))
image_path = cwd_path + '\\images\\train'
print('xml path:%s' % (image_path))
xml_df = xml_to_csv(image_path)
xml_df.to_csv(cwd_path + '\\images\\train_labels.csv', index=None)
# test xml to csv
image_path = cwd_path + '\\images\\test'
print('xml path:%s' % (image_path))
xml_df = xml_to_csv(image_path)
xml_df.to_csv(cwd_path + '\\images\\test_labels.csv', index=None)
# finished
print('Successfully converted xml to csv.')
main()
generate_tfrecord.py
"""
Usage:
# From tensorflow/models/
# Create train data:
python generate_tfrecord.py --csv_input=data/train_labels.csv --output_path=train.record
# Create test data:
python generate_tfrecord.py --csv_input=data/test_labels.csv --output_path=test.record
"""
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import os
import io
import pandas as pd
import tensorflow.compat.v1 as tf
#from absl import app
#from absl import flags
from PIL import Image
from object_detection.utils import dataset_util
from collections import namedtuple, OrderedDict
flags = tf.app.flags
flags.DEFINE_string('csv_input', '', 'Path to the CSV input')
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
flags.DEFINE_string('image_dir', '', 'Path to images')
FLAGS = flags.FLAGS
# TO-DO replace this with label map
def class_text_to_int(row_label):
if row_label == 'person':
return 1
else:
None
def split(df, group):
data = namedtuple('data', ['filename', 'object'])
gb = df.groupby(group)
return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]
def create_tf_example(group, path):
with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
encoded_jpg = fid.read()
encoded_jpg_io = io.BytesIO(encoded_jpg)
image = Image.open(encoded_jpg_io)
width, height = image.size
filename = group.filename.encode('utf8')
image_format = b'jpg'
xmins = []
xmaxs = []
ymins = []
ymaxs = []
classes_text = []
classes = []
for index, row in group.object.iterrows():
xmins.append(row['xmin'] / width)
xmaxs.append(row['xmax'] / width)
ymins.append(row['ymin'] / height)
ymaxs.append(row['ymax'] / height)
classes_text.append(row['class'].encode('utf8'))
classes.append(class_text_to_int(row['class']))
tf_example = tf.train.Example(features=tf.train.Features(feature={
'image/height': dataset_util.int64_feature(height),
'image/width': dataset_util.int64_feature(width),
'image/filename': dataset_util.bytes_feature(filename),
'image/source_id': dataset_util.bytes_feature(filename),
'image/encoded': dataset_util.bytes_feature(encoded_jpg),
'image/format': dataset_util.bytes_feature(image_format),
'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
'image/object/class/label': dataset_util.int64_list_feature(classes),
}))
return tf_example
def main(_):
writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
path = os.path.join(FLAGS.image_dir)
examples = pd.read_csv(FLAGS.csv_input)
grouped = split(examples, 'filename')
for group in grouped:
tf_example = create_tf_example(group, path)
writer.write(tf_example.SerializeToString())
writer.close()
output_path = os.path.join(os.getcwd(), FLAGS.output_path)
print('Successfully created the TFRecords: {}'.format(output_path))
if __name__ == '__main__':
tf.app.run()
按照官网教程将图片和 xml 放入对应的文件夹
将脚本放在research\object_detection 下运行这两个脚本就行了
会生成 test.record 和 train.record
问题
- ImportError: cannot import name ‘center_net_pb2’ from 'object_detection.protos’
(返回到research\目录下运行 protoc --python_out=. .\object_detection\protos\center_net.proto 然后在python setup.py build 和 python setup.py install)
本文介绍如何在Windows 10环境下安装并编译TensorFlow Models,包括配置Anaconda环境、解决常见错误及运行示例脚本。
1万+

被折叠的 条评论
为什么被折叠?



