分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

https://github.com/tensorflow/models/blob/master/research/slim/datasets/preprocess_imagenet_validation_data.py 改编版

发布时间:2023-09-06 01:58责任编辑:林大明关键词:httpgithub
#!/usr/bin/env python# Copyright 2016 Google Inc. All Rights Reserved.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## ????http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.# ==============================================================================r"""Process the ImageNet Challenge bounding boxes for TensorFlow model training.Associate the ImageNet 2012 Challenge validation data set with labels.The raw ImageNet validation data set is expected to reside in JPEG fileslocated in the following directory structure. data_dir/ILSVRC2012_val_00000001.JPEG data_dir/ILSVRC2012_val_00000002.JPEG ... data_dir/ILSVRC2012_val_00050000.JPEGThis script moves the files into a directory structure like such: data_dir/n01440764/ILSVRC2012_val_00000293.JPEG data_dir/n01440764/ILSVRC2012_val_00000543.JPEG ...where ‘n01440764‘ is the unique synset label associated withthese images.This directory reorganization requires a mapping from validation imagenumber (i.e. suffix of the original file) to the associated label. Thisis provided in the ImageNet development kit via a Matlab file.In order to make life easier and divorce ourselves from Matlab, we insteadsupply a custom text file that provides this mapping for us.Sample usage: ?./preprocess_imagenet_validation_data.py ILSVRC2012_img_val ??imagenet_2012_validation_synset_labels.txt"""from __future__ import absolute_importfrom __future__ import divisionfrom __future__ import print_functionimport osimport sysfrom six.moves import xrange ?# pylint: disable=redefined-builtinif __name__ == ‘__main__‘: ?if len(sys.argv) < 3: ?# sys.argv返回脚本本身的名字及给定脚本的参数. ???print(‘Invalid usage\n‘ ?????????‘usage: preprocess_imagenet_validation_data.py ‘ ?????????‘<validation data dir> <validation labels file>‘) ???sys.exit(-1) ?# System.exit(-1)是指所有程序(方法,类等)停止,系统停止运行。 ?data_dir = sys.argv[1] ?validation_labels_file = sys.argv[2] ?# Read in the 50000 synsets associated with the validation data set. ?# imagenet_2012_validation_synset_labels.txt 这个文件中有50000行类别,有重复,与50000图片是一一对应的 ?labels = [l.strip() for l in open(validation_labels_file).readlines()] ?# strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)。 ?unique_labels = set(labels) ?# set() 函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。 ?# Make all sub-directories in the validation data dir. ?for label in unique_labels: ???labeled_data_dir = os.path.join(data_dir, label) ???if not os.path.exists(labeled_data_dir): ???os.makedirs(labeled_data_dir) ?# Move all of the image to the appropriate sub-directory. ?for i in xrange(len(labels)): ?# xrange() 函数用法与 range 完全相同,所不同的是生成的不是一个数组,而是一个生成器。 ???basename = ‘ILSVRC2012_val_000%.5d.JPEG‘ % (i + 1) ???original_filename = os.path.join(data_dir, basename) ???if not os.path.exists(original_filename): ?????#print(‘Failed to find: ‘ % original_filename) ?????continue ?????#sys.exit(-1) ???new_filename = os.path.join(data_dir, labels[i], basename) ???os.rename(original_filename, new_filename)

82行的代码一加进去,就出错:

TypeError: not all arguments converted during string formatting

过程中还出现了以下错误:

Organizing the validation data into sub-directories.
Traceback (most recent call last):
?File "F:/datasets/preprocess_imagenet_validation_data.py", line 86, in <module>
???os.rename(original_filename, new_filename)
PermissionError: [WinError 32] ??????????????????????????????????: ‘F:/ILSVRC2012_img_val/ILSVRC2012_val_00032304.JPEG‘ -> ‘F:/ILSVRC2012_img_val/n02109961\\ILSVRC2012_val_00032304.JPEG‘

可能是不能够一次性重命名太多文件,反正我重新运行了 

./download_and_convert_imagenet.sh /f/ILSVRC2012_img_val_varified

preprocess_imagenet_validation_data.py这个程序可以继续重命名文件。

https://github.com/tensorflow/models/blob/master/research/slim/datasets/preprocess_imagenet_validation_data.py 改编版

原文地址:https://www.cnblogs.com/Time-LCJ/p/9135805.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved