码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2018-06-04 22:31:35      阅读:849      评论:0      收藏:0      [点我收藏+]

标签:eve   cab   type   mission   fun   copyright   exist   txt   mat   

#!/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 files
located in the following directory structure.

 data_dir/ILSVRC2012_val_00000001.JPEG
 data_dir/ILSVRC2012_val_00000002.JPEG
 ...
 data_dir/ILSVRC2012_val_00050000.JPEG

This 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 with
these images.

This directory reorganization requires a mapping from validation image
number (i.e. suffix of the original file) to the associated label. This
is provided in the ImageNet development kit via a Matlab file.

In order to make life easier and divorce ourselves from Matlab, we instead
supply 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_import
from __future__ import division
from __future__ import print_function

import os
import sys

from six.moves import xrange  # pylint: disable=redefined-builtin


if __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 改编版

标签:eve   cab   type   mission   fun   copyright   exist   txt   mat   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!