diff --git a/.gitignore b/.gitignore index 5a6aeae..6bd7b4b 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,8 @@ Session.vim *~ # auto-generated tag files tags + + ### Node template # Logs logs @@ -83,6 +85,9 @@ Icon # Thumbnails ._* +# Shell + + # Files that might appear in the root of a volume .DocumentRevisions-V100 .fseventsd @@ -244,3 +249,19 @@ nohup.out *.pbtxt *.record bin/ +*.jpg +*.jpeg +*.png + +# envs variable +upload_model.sh +generate_color_classifier_dataset.sh +get_object_by_fabric.sh +create_od_train_record.sh +create_bottom_tfrecord.sh +create_full_tfrecord.sh +create_top_tfrecord.sh +create_top_full_tfrecord.sh +get_object_by_fabric.sh +get_object_by_color.sh + diff --git a/requirements.txt b/requirements.txt index c9a8c63..5aa1329 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,7 @@ +0.0.1==0.0.1 +absl-py==0.1.11 appnope==0.1.0 +astor==0.6.2 bleach==1.5.0 boto3==1.5.6 botocore==1.8.20 @@ -8,6 +11,8 @@ decorator==4.1.2 docutils==0.14 entrypoints==0.2.3 enum34==1.1.6 +gast==0.2.0 +grpcio==1.10.0 html5lib==0.9999999 ipykernel==4.6.1 ipython==6.2.1 @@ -19,26 +24,29 @@ jsonschema==2.6.0 jupyter-client==5.1.0 jupyter-core==4.3.0 lxml==4.0.0 -Markdown==2.6.9 +Markdown==2.6.11 MarkupSafe==1.0 matplotlib==2.0.2 mistune==0.7.4 nbconvert==5.3.1 nbformat==4.4.0 +nltk==3.2.4 notebook==5.1.0 -numpy==1.13.3 +numpy==1.14.2 olefile==0.44 opencv-python==3.3.0.10 +pandas==0.22.0 pandocfilters==1.4.2 parso==0.1.0 pexpect==4.2.1 pickleshare==0.7.4 Pillow==4.2.1 +preprocessing==0.1.13 prompt-toolkit==1.0.15 -protobuf==3.4.0 +protobuf==3.5.2 ptyprocess==0.5.2 Pygments==2.2.0 -pymongo==3.6.0 +pymongo==3.6.1 pyparsing==2.2.0 python-dateutil==2.6.1 pytz==2017.2 @@ -46,12 +54,18 @@ pyzmq==16.0.2 s3transfer==0.1.12 simplegeneric==0.8.1 six==1.11.0 -stylelens-dataset==0.0.4 +sphinx-rtd-theme==0.2.4 +stylelens-admin==0.0.3 +stylelens-dataset==0.0.31 +stylelens-s3==0.0.2 +tensorboard==1.6.0 tensorflow==1.3.0 +tensorflow-gpu==1.6.0 tensorflow-tensorboard==0.1.7 +termcolor==1.1.0 terminado==0.6 testpath==0.3.1 tornado==4.5.2 traitlets==4.3.2 wcwidth==0.1.7 -Werkzeug==0.12.2 +Werkzeug==0.14.1 diff --git a/stylelens/attr_classification/category_model/eval_category_classification.sh b/stylelens/attr_classification/category_model/eval_category_classification.sh new file mode 100755 index 0000000..4525984 --- /dev/null +++ b/stylelens/attr_classification/category_model/eval_category_classification.sh @@ -0,0 +1,15 @@ +#! /bin/bash +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../../tensorflow:`pwd`/../../../tensorflow/slim: +export TRAIN_DIR_PATH='/home/lion/attr_dataset/category_model/model/train1' +export EVAL_DIR_PATH='/home/lion/attr_dataset/category_model/model/eval1' +export DATASET_DIR_PATH='/home/lion/attr_dataset/category_model/data/dataset1' + +python /home/lion/bl-magi/tensorflow/slim/eval_image_classifier.py \ + --alsologtostderr\ + --eval_dir=$EVAL_DIR_PATH\ + --checkpoint_path=$TRAIN_DIR_PATH\ + --dataset_dir=$DATASET_DIR_PATH\ + --dataset_name=category\ + --dataset_split_name=validation\ + --model_name=inception_v3 diff --git a/stylelens/attr_classification/category_model/test_model/test.sh b/stylelens/attr_classification/category_model/test_model/test.sh new file mode 100755 index 0000000..3d2379c --- /dev/null +++ b/stylelens/attr_classification/category_model/test_model/test.sh @@ -0,0 +1,19 @@ +#! /bin/bash +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../../tensorflow:`pwd`/../../../tensorflow/slim: +export MODEL_PATH='/home/lion/attr_dataset/category_model/model/train1/model.ckpt-769143' +export LABEL_PATH='/home/lion/attr_dataset/category_model/data/dataset/labels.txt' +export DATA_PATH='/home/lion/bl-magi/stylelens/attr_classification/category_model/test_images1/' + +python test_category.py \ + --model_path=$MODEL_PATH \ + --model_name=inception_v3 \ + --label_path=$LABEL_PATH \ + --data_path=$DATA_PATH + + + + + + + diff --git a/stylelens/attr_classification/category_model/test_model/test_category.py b/stylelens/attr_classification/category_model/test_model/test_category.py new file mode 100644 index 0000000..4b84266 --- /dev/null +++ b/stylelens/attr_classification/category_model/test_model/test_category.py @@ -0,0 +1,85 @@ +import glob +import os,re,sys +import argparse +import importlib +import cv2 + +import tensorflow as tf +from preprocessing import inception_preprocessing + +slim = tf.contrib.slim + +# prefix image size +image_size = 300 + +def run(args): + data_path = args.data_path + label_path = args.label_path + + model_path = args.model_path + model_name = args.model_name + model_scope = model_name +'_arg_scope' + + inception = importlib.import_module('nets.'+model_name) + + + with tf.Graph().as_default(): + with slim.arg_scope(getattr(inception,model_scope)()): + + + files = glob.glob(data_path+os.path.sep+"*.jpg") + file_list = list() + + for idx,f in enumerate(files): + f_string = tf.gfile.FastGFile(f, 'rb').read() + test_img = tf.image.decode_jpeg(f_string, channels=3) + processed_image = inception_preprocessing.preprocess_image(test_img, image_size, image_size, is_training=False) + #processed_images = tf.expand_dims(processed_image, 0) + file_list.append(os.path.basename(f)) + if(idx == 0): + processed_images = [processed_image] + else: + processed_images.append(processed_image) + processed_images = tf.stack(processed_images,axis=0) + + with open(label_path,'r') as rdata: + names = dict() + for row in rdata: + strip_row = row.strip() + split_row = strip_row.split(":") + if(len(split_row) == 2): + names[int(split_row[0])]=split_row[1] + + print(names) + + logits, _ = getattr(inception,model_name)(processed_images, num_classes=14, is_training=False) + probabilities = tf.nn.softmax(logits) + init_fn = slim.assign_from_checkpoint_fn(model_path, slim.get_model_variables('InceptionV3')) + + with tf.Session() as sess: + init_fn(sess) + + + np_image, probabilities = sess.run([processed_images, probabilities]) + + + print("\n======== DATA RESULT =======\n") + print("filename\t"+"\t".join(names.values())) + + for idx,iter in enumerate(probabilities): + print(file_list[idx]+'\t' +'\t'.join([str(round(i,2)) for i in iter])) + + +if __name__ == '__main__': + + parser = argparse.ArgumentParser() + parser.add_argument("--data_path",help="the path to test images") + parser.add_argument("--model_path") + parser.add_argument("--model_name") + parser.add_argument("--label_path") + if(len(sys.argv) != 5): + parser.print_help() + parser.exit() + args = parser.parse_args() + run(args) + diff --git a/stylelens/attr_classification/category_model/train_category_classification.sh b/stylelens/attr_classification/category_model/train_category_classification.sh new file mode 100755 index 0000000..929789e --- /dev/null +++ b/stylelens/attr_classification/category_model/train_category_classification.sh @@ -0,0 +1,15 @@ +#! /bin/bash +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../../tensorflow:`pwd`/../../../tensorflow/slim: +export TRAIN_DIR_PATH='/home/lion/attr_dataset/category_model/model/train1' +export DATASET_DIR_PATH='/home/lion/attr_dataset/category_model/data/dataset1' + + +python /home/lion/bl-magi/tensorflow/slim/train_image_classifier.py \ + --train_dir=$TRAIN_DIR_PATH \ + --dataset_dir=$DATASET_DIR_PATH \ + --dataset_name=category \ + --dataset_split_name=train \ + --num_clones=7 \ + --batch_size=42\ + --model_name=inception_v3 diff --git a/stylelens/attr_classification/color_model/eval_color_classification.sh b/stylelens/attr_classification/color_model/eval_color_classification.sh new file mode 100755 index 0000000..8cf3a36 --- /dev/null +++ b/stylelens/attr_classification/color_model/eval_color_classification.sh @@ -0,0 +1,15 @@ +#! /bin/bash +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../../tensorflow:`pwd`/../../../tensorflow/slim: +export TRAIN_DIR_PATH='/home/lion/attr_dataset/color_model/model/train1' +export EVAL_DIR_PATH='/home/lion/attr_dataset/color_model/model/eval1' +export DATASET_DIR_PATH='/home/lion/attr_dataset/color_model/data/dataset1' + +python /home/lion/bl-magi/tensorflow/slim/eval_image_classifier.py \ + --alsologtostderr\ + --eval_dir=$EVAL_DIR_PATH\ + --checkpoint_path=$TRAIN_DIR_PATH\ + --dataset_dir=$DATASET_DIR_PATH\ + --dataset_name=color\ + --dataset_split_name=validation\ + --model_name=inception_v3 diff --git a/stylelens/attr_classification/color_model/eval_color_classification_nocheckpoint.sh b/stylelens/attr_classification/color_model/eval_color_classification_nocheckpoint.sh new file mode 100755 index 0000000..de14c6e --- /dev/null +++ b/stylelens/attr_classification/color_model/eval_color_classification_nocheckpoint.sh @@ -0,0 +1,15 @@ +#! /bin/bash +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../../tensorflow:`pwd`/../../../tensorflow/slim: +export TRAIN_DIR_PATH='/home/lion/attr_dataset/color_model/model/train2' +export EVAL_DIR_PATH='/home/lion/attr_dataset/color_model/model/eval2' +export DATASET_DIR_PATH='/home/lion/attr_dataset/color_model/data/dataset1' + +python /home/lion/bl-magi/tensorflow/slim/eval_image_classifier.py \ + --alsologtostderr\ + --eval_dir=$EVAL_DIR_PATH\ + --checkpoint_path=$TRAIN_DIR_PATH\ + --dataset_dir=$DATASET_DIR_PATH\ + --dataset_name=color\ + --dataset_split_name=validation\ + --model_name=inception_v3 diff --git a/stylelens/attr_classification/color_model/train_color_classification.sh b/stylelens/attr_classification/color_model/train_color_classification.sh new file mode 100755 index 0000000..f0c546d --- /dev/null +++ b/stylelens/attr_classification/color_model/train_color_classification.sh @@ -0,0 +1,19 @@ +#! /bin/bash +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../../tensorflow:`pwd`/../../../tensorflow/slim: +export TRAIN_DIR_PATH='/home/lion/attr_dataset/color_model/model/train1' +export DATASET_DIR_PATH='/home/lion/attr_dataset/color_model/data/dataset1' +export CHECKPOINT_PATH='/home/lion/attr_dataset/color_model/data/checkpoints/inception_v3.ckpt' + + +python /home/lion/bl-magi/tensorflow/slim/train_image_classifier.py \ + --train_dir=$TRAIN_DIR_PATH \ + --dataset_dir=$DATASET_DIR_PATH \ + --dataset_name=color \ + --dataset_split_name=train \ + --num_clones=6 \ + --batch_size=192 \ + --model_name=inception_v3 \ + --checkpoint_path=/home/lion/attr_dataset/color_model/data/checkpoints1/inception_v3.ckpt \ + --checkpoint_exclude_scopes=InceptionV3/Logits,InceptionV3/AuxLogits \ + --trainable_scopes=InceptionV3/Logits,InceptionV3/AuxLogits diff --git a/stylelens/attr_classification/color_model/train_color_classification_nocheckpoint.sh b/stylelens/attr_classification/color_model/train_color_classification_nocheckpoint.sh new file mode 100755 index 0000000..2724fad --- /dev/null +++ b/stylelens/attr_classification/color_model/train_color_classification_nocheckpoint.sh @@ -0,0 +1,17 @@ +#! /bin/bash +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../../tensorflow:`pwd`/../../../tensorflow/slim: +export TRAIN_DIR_PATH='/home/lion/attr_dataset/color_model/model/train2' +export DATASET_DIR_PATH='/home/lion/attr_dataset/color_model/data/dataset1' + + +python /home/lion/bl-magi/tensorflow/slim/train_image_classifier.py \ + --train_dir=$TRAIN_DIR_PATH \ + --dataset_dir=$DATASET_DIR_PATH \ + --dataset_name=color \ + --dataset_split_name=train \ + --num_clones=2 \ + --batch_size=30 \ + --learning_rate=0.01 \ + --model_name=inception_v3 \ + --max_num_of_steps=1000000 diff --git a/stylelens/attr_classification/fabric_model/eval_fabric_classification.sh b/stylelens/attr_classification/fabric_model/eval_fabric_classification.sh new file mode 100755 index 0000000..8cedbee --- /dev/null +++ b/stylelens/attr_classification/fabric_model/eval_fabric_classification.sh @@ -0,0 +1,15 @@ +#! /bin/bash +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../tensorflow/slim: +export TRAIN_DIR_PATH='/home/lion/attr_dataset/fabric_model/model/train' +export EVAL_DIR_PATH='/home/lion/attr_dataset/fabric_model/model/eval' +export DATASET_DIR_PATH='/home/lion/attr_dataset/fabric_model/data/dataset' + +python /home/lion/bl-magi/tensorflow/slim/eval_image_classifier.py \ + --alsologtostderr\ + --eval_dir=$EVAL_DIR_PATH\ + --checkpoint_path=$TRAIN_DIR_PATH\ + --dataset_dir=$DATASET_DIR_PATH\ + --dataset_name=fabric\ + --dataset_split_name=validation\ + --model_name=inception_v3 diff --git a/stylelens/attr_classification/fabric_model/train_fabric_classification.sh b/stylelens/attr_classification/fabric_model/train_fabric_classification.sh new file mode 100755 index 0000000..946d1a0 --- /dev/null +++ b/stylelens/attr_classification/fabric_model/train_fabric_classification.sh @@ -0,0 +1,16 @@ +#! /bin/bash +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../tensorflow/slim: +export TRAIN_DIR_PATH='/home/lion/attr_dataset/fabric_model/model/train' +export DATASET_DIR_PATH='/home/lion/attr_dataset/fabric_model/data/dataset' + +python /home/lion/bl-magi/tensorflow/slim/train_image_classifier.py \ + --train_dir=$TRAIN_DIR_PATH\ + --dataset_dir=$DATASET_DIR_PATH\ + --dataset_name=fabric\ + --dataset_split_name=train\ + --num_clones 7 \ + --model_name=inception_v3\ + --checkpoint_path=/home/lion/attr_dataset/fabric_model/data/checkpoints/inception_v3.ckpt\ + --checkpoint_exclude_scopes=InceptionV3/Logits,InceptionV3/AuxLogits\ + --trainable_scopes=InceptionV3/Logits,InceptionV3/AuxLogits diff --git a/stylelens/attr_classification/freeze_graph/bl-text-classifier b/stylelens/attr_classification/freeze_graph/bl-text-classifier new file mode 160000 index 0000000..718490f --- /dev/null +++ b/stylelens/attr_classification/freeze_graph/bl-text-classifier @@ -0,0 +1 @@ +Subproject commit 718490fe99076047b0d34436645aca07e5e583d4 diff --git a/stylelens/attr_classification/freeze_graph/freeze_graph.py b/stylelens/attr_classification/freeze_graph/freeze_graph.py new file mode 100644 index 0000000..0ddf092 --- /dev/null +++ b/stylelens/attr_classification/freeze_graph/freeze_graph.py @@ -0,0 +1,350 @@ +# Copyright 2015 The TensorFlow Authors. 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"""Converts checkpoint variables into Const ops in a standalone GraphDef file. + +This script is designed to take a GraphDef proto, a SaverDef proto, and a set of +variable values stored in a checkpoint file, and output a GraphDef with all of +the variable ops converted into const ops containing the values of the +variables. + +It's useful to do this when we need to load a single file in C++, especially in +environments like mobile or embedded where we may not have access to the +RestoreTensor ops and file loading calls that they rely on. + +An example of command-line usage is: +bazel build tensorflow/python/tools:freeze_graph && \ +bazel-bin/tensorflow/python/tools/freeze_graph \ +--input_graph=some_graph_def.pb \ +--input_checkpoint=model.ckpt-8361242 \ +--output_graph=/tmp/frozen_graph.pb --output_node_names=softmax + +You can also look at freeze_graph_test.py for an example of how to use it. + +""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import argparse +import sys + +from google.protobuf import text_format + +from tensorflow.core.framework import graph_pb2 +from tensorflow.core.protobuf import saver_pb2 +from tensorflow.core.protobuf.meta_graph_pb2 import MetaGraphDef +from tensorflow.python import pywrap_tensorflow +from tensorflow.python.client import session +from tensorflow.python.framework import graph_util +from tensorflow.python.framework import importer +from tensorflow.python.platform import app +from tensorflow.python.platform import gfile +from tensorflow.python.saved_model import loader +from tensorflow.python.saved_model import tag_constants +from tensorflow.python.tools import saved_model_utils +from tensorflow.python.training import saver as saver_lib + +FLAGS = None + + +def freeze_graph_with_def_protos(input_graph_def, + input_saver_def, + input_checkpoint, + output_node_names, + restore_op_name, + filename_tensor_name, + output_graph, + clear_devices, + initializer_nodes, + variable_names_whitelist="", + variable_names_blacklist="", + input_meta_graph_def=None, + input_saved_model_dir=None, + saved_model_tags=None): + """Converts all variables in a graph and checkpoint into constants.""" + del restore_op_name, filename_tensor_name # Unused by updated loading code. + + # 'input_checkpoint' may be a prefix if we're using Saver V2 format + if (not input_saved_model_dir and + not saver_lib.checkpoint_exists(input_checkpoint)): + print("Input checkpoint '" + input_checkpoint + "' doesn't exist!") + return -1 + + if not output_node_names: + print("You need to supply the name of a node to --output_node_names.") + return -1 + + # Remove all the explicit device specifications for this node. This helps to + # make the graph more portable. + if clear_devices: + if input_meta_graph_def: + for node in input_meta_graph_def.graph_def.node: + node.device = "" + elif input_graph_def: + for node in input_graph_def.node: + node.device = "" + + if input_graph_def: + _ = importer.import_graph_def(input_graph_def, name="") + with session.Session() as sess: + if input_saver_def: + saver = saver_lib.Saver(saver_def=input_saver_def) + saver.restore(sess, input_checkpoint) + elif input_meta_graph_def: + restorer = saver_lib.import_meta_graph( + input_meta_graph_def, clear_devices=True) + restorer.restore(sess, input_checkpoint) + if initializer_nodes: + sess.run(initializer_nodes.split(",")) + elif input_saved_model_dir: + if saved_model_tags is None: + saved_model_tags = [] + loader.load(sess, saved_model_tags, input_saved_model_dir) + else: + var_list = {} + reader = pywrap_tensorflow.NewCheckpointReader(input_checkpoint) + var_to_shape_map = reader.get_variable_to_shape_map() + for key in var_to_shape_map: + try: + tensor = sess.graph.get_tensor_by_name(key + ":0") + except KeyError: + # This tensor doesn't exist in the graph (for example it's + # 'global_step' or a similar housekeeping element) so skip it. + continue + var_list[key] = tensor + saver = saver_lib.Saver(var_list=var_list) + saver.restore(sess, input_checkpoint) + if initializer_nodes: + sess.run(initializer_nodes.split(",")) + + variable_names_whitelist = (variable_names_whitelist.split(",") + if variable_names_whitelist else None) + variable_names_blacklist = (variable_names_blacklist.split(",") + if variable_names_blacklist else None) + + if input_meta_graph_def: + output_graph_def = graph_util.convert_variables_to_constants( + sess, + input_meta_graph_def.graph_def, + output_node_names.split(","), + variable_names_whitelist=variable_names_whitelist, + variable_names_blacklist=variable_names_blacklist) + else: + output_graph_def = graph_util.convert_variables_to_constants( + sess, + input_graph_def, + output_node_names.split(","), + variable_names_whitelist=variable_names_whitelist, + variable_names_blacklist=variable_names_blacklist) + + # Write GraphDef to file if output path has been given. + if output_graph: + with gfile.GFile(output_graph, "wb") as f: + f.write(output_graph_def.SerializeToString()) + + return output_graph_def + + +def _parse_input_graph_proto(input_graph, input_binary): + """Parser input tensorflow graph into GraphDef proto.""" + if not gfile.Exists(input_graph): + print("Input graph file '" + input_graph + "' does not exist!") + return -1 + input_graph_def = graph_pb2.GraphDef() + mode = "rb" if input_binary else "r" + with gfile.FastGFile(input_graph, mode) as f: + if input_binary: + input_graph_def.ParseFromString(f.read()) + else: + text_format.Merge(f.read(), input_graph_def) + return input_graph_def + + +def _parse_input_meta_graph_proto(input_graph, input_binary): + """Parser input tensorflow graph into MetaGraphDef proto.""" + if not gfile.Exists(input_graph): + print("Input meta graph file '" + input_graph + "' does not exist!") + return -1 + input_meta_graph_def = MetaGraphDef() + mode = "rb" if input_binary else "r" + with gfile.FastGFile(input_graph, mode) as f: + if input_binary: + input_meta_graph_def.ParseFromString(f.read()) + else: + text_format.Merge(f.read(), input_meta_graph_def) + print("Loaded meta graph file '" + input_graph) + return input_meta_graph_def + + +def _parse_input_saver_proto(input_saver, input_binary): + """Parser input tensorflow Saver into SaverDef proto.""" + if not gfile.Exists(input_saver): + print("Input saver file '" + input_saver + "' does not exist!") + return -1 + mode = "rb" if input_binary else "r" + with gfile.FastGFile(input_saver, mode) as f: + saver_def = saver_pb2.SaverDef() + if input_binary: + saver_def.ParseFromString(f.read()) + else: + text_format.Merge(f.read(), saver_def) + return saver_def + + +def freeze_graph(input_graph, + input_saver, + input_binary, + input_checkpoint, + output_node_names, + restore_op_name, + filename_tensor_name, + output_graph, + clear_devices, + initializer_nodes, + variable_names_whitelist="", + variable_names_blacklist="", + input_meta_graph=None, + input_saved_model_dir=None, + saved_model_tags=tag_constants.SERVING): + """Converts all variables in a graph and checkpoint into constants.""" + input_graph_def = None + if input_saved_model_dir: + input_graph_def = saved_model_utils.get_meta_graph_def( + input_saved_model_dir, saved_model_tags).graph_def + elif input_graph: + input_graph_def = _parse_input_graph_proto(input_graph, input_binary) + input_meta_graph_def = None + if input_meta_graph: + input_meta_graph_def = _parse_input_meta_graph_proto( + input_meta_graph, input_binary) + input_saver_def = None + if input_saver: + input_saver_def = _parse_input_saver_proto(input_saver, input_binary) + freeze_graph_with_def_protos( + input_graph_def, input_saver_def, input_checkpoint, output_node_names, + restore_op_name, filename_tensor_name, output_graph, clear_devices, + initializer_nodes, variable_names_whitelist, variable_names_blacklist, + input_meta_graph_def, input_saved_model_dir, saved_model_tags.split(",")) + + +def main(unused_args): + freeze_graph(FLAGS.input_graph, FLAGS.input_saver, FLAGS.input_binary, + FLAGS.input_checkpoint, FLAGS.output_node_names, + FLAGS.restore_op_name, FLAGS.filename_tensor_name, + FLAGS.output_graph, FLAGS.clear_devices, FLAGS.initializer_nodes, + FLAGS.variable_names_whitelist, FLAGS.variable_names_blacklist, + FLAGS.input_meta_graph, FLAGS.input_saved_model_dir, + FLAGS.saved_model_tags) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.register("type", "bool", lambda v: v.lower() == "true") + parser.add_argument( + "--input_graph", + type=str, + default="", + help="TensorFlow \'GraphDef\' file to load.") + parser.add_argument( + "--input_saver", + type=str, + default="", + help="TensorFlow saver file to load.") + parser.add_argument( + "--input_checkpoint", + type=str, + default="", + help="TensorFlow variables file to load.") + parser.add_argument( + "--output_graph", + type=str, + default="", + help="Output \'GraphDef\' file name.") + parser.add_argument( + "--input_binary", + nargs="?", + const=True, + type="bool", + default=False, + help="Whether the input files are in binary format.") + parser.add_argument( + "--output_node_names", + type=str, + default="", + help="The name of the output nodes, comma separated.") + parser.add_argument( + "--restore_op_name", + type=str, + default="save/restore_all", + help="""\ + The name of the master restore operator. Deprecated, unused by updated \ + loading code. + """) + parser.add_argument( + "--filename_tensor_name", + type=str, + default="save/Const:0", + help="""\ + The name of the tensor holding the save path. Deprecated, unused by \ + updated loading code. + """) + parser.add_argument( + "--clear_devices", + nargs="?", + const=True, + type="bool", + default=True, + help="Whether to remove device specifications.") + parser.add_argument( + "--initializer_nodes", + type=str, + default="", + help="Comma separated list of initializer nodes to run before freezing.") + parser.add_argument( + "--variable_names_whitelist", + type=str, + default="", + help="""\ + Comma separated list of variables to convert to constants. If specified, \ + only those variables will be converted to constants.\ + """) + parser.add_argument( + "--variable_names_blacklist", + type=str, + default="", + help="""\ + Comma separated list of variables to skip converting to constants.\ + """) + parser.add_argument( + "--input_meta_graph", + type=str, + default="", + help="TensorFlow \'MetaGraphDef\' file to load.") + parser.add_argument( + "--input_saved_model_dir", + type=str, + default="", + help="Path to the dir with TensorFlow \'SavedModel\' file and variables.") + parser.add_argument( + "--saved_model_tags", + type=str, + default="serve", + help="""\ + Group of tag(s) of the MetaGraphDef to load, in string format,\ + separated by \',\'. For tag-set contains multiple tags, all tags \ + must be passed in.\ + """) + FLAGS, unparsed = parser.parse_known_args() + app.run(main=main, argv=[sys.argv[0]] + unparsed) diff --git a/stylelens/attr_classification/freeze_graph/frozen_graph.sh b/stylelens/attr_classification/freeze_graph/frozen_graph.sh new file mode 100755 index 0000000..c052823 --- /dev/null +++ b/stylelens/attr_classification/freeze_graph/frozen_graph.sh @@ -0,0 +1,13 @@ +#! /bin/bash + +source activate bl-magi +export PYTHONPATH='pwd'/../../tensorflow:'pwd'/../../tensorflow/slim +echo $PYTHONPATH +export MODEL_BASE_PATH='/home/lion/attr_dataset/category_model' + +python freeze_graph.py \ + --input_graph=category_inception_v3_inf_graph.pb \ + --input_checkpoint=/home/lion/attr_dataset/category_model/model/train1/model.ckpt-786881 \ + --input_binary=True \ + --output_graph=category_inference_graph.pb \ + --output_node_names=InceptionV3/Predictions/Reshape_1 diff --git a/stylelens/attr_classification/freeze_graph/upload_model.py b/stylelens/attr_classification/freeze_graph/upload_model.py new file mode 100644 index 0000000..c9a150c --- /dev/null +++ b/stylelens/attr_classification/freeze_graph/upload_model.py @@ -0,0 +1,23 @@ +import os +from stylelens_s3.s3 import S3 + +AWS_ACCESS_KEY = os.environ['AWS_ACCESS_KEY'] +AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY'] +RELEASE_MODE = os.environ['RELEASE_MODE'] +FROZEN_GRAPH_PATH = '/home/lion/bl-magi/stylelens/object_detection/frozen_graph/top_full_output_inference_graph_100893.pb' +LABLE_MAP_PATH = '/home/lion/attr_dataset/color_model/data/dataset1/labels.txt' + +AWS_BUCKET = 'bluelens-style-model' +AWS_BUCKET_FOLDER = 'classification' +MODEL_FILE = 'frozen_inference_graph.pb' + +storage = S3(AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY) + +key = os.path.join(AWS_BUCKET_FOLDER, 'prod', 'top_full', 'top_full_model.ckpt-100893', MODEL_FILE) +label_key = os.path.join(AWS_BUCKET_FOLDER, 'color', 'dev', 'labels.txt') + +try: + storage.upload_file_to_bucket(AWS_BUCKET, FROZEN_GRAPH_PATH, key) + storage.upload_file_to_bucket(AWS_BUCKET, LABLE_MAP_PATH, label_key) +except: + print('upload error') diff --git a/stylelens/attr_classification/freeze_graph/upload_model.sh b/stylelens/attr_classification/freeze_graph/upload_model.sh new file mode 100755 index 0000000..2e03533 --- /dev/null +++ b/stylelens/attr_classification/freeze_graph/upload_model.sh @@ -0,0 +1,9 @@ +#source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../../tensorflow:`pwd`/../../../tensorflow/slim +export AWS_SECRET_ACCESS_KEY="7NFxpVlVQjABUXv3j3BCvayc6sgRwwXzHMl+iSJ8" +export AWS_ACCESS_KEY="AKIAIWSVDY3WRWCMFRJQ" +export RELEASE_MODE="prod" + +echo $PYTHONPATH + +python upload_model.py \ diff --git a/stylelens/dataset/deepfashion/attr_model/build_image_data.py b/stylelens/dataset/deepfashion/attr_model/build_image_data.py new file mode 100644 index 0000000..2669f6a --- /dev/null +++ b/stylelens/dataset/deepfashion/attr_model/build_image_data.py @@ -0,0 +1,384 @@ +# 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. +# ============================================================================== +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +from datetime import datetime +import os +import random +import sys +import threading + +import numpy as np +import tensorflow as tf + +tf.app.flags.DEFINE_string('train_directory', '', + 'Training data directory') +tf.app.flags.DEFINE_string('validation_directory', '', + 'Validation data directory') +tf.app.flags.DEFINE_string('output_directory', '', + 'Output data directory') + +tf.app.flags.DEFINE_integer('train_shards', 2, + 'Number of shards in training TFRecord files.') +tf.app.flags.DEFINE_integer('validation_shards', 2, + 'Number of shards in validation TFRecord files.') + +tf.app.flags.DEFINE_integer('num_threads', 2, + 'Number of threads to preprocess the images.') + +# The labels file contains a list of valid labels are held in this file. +# Assumes that the file contains entries as such: +# dog +# cat +# flower +# where each line corresponds to a label. We map each label contained in +# the file to an integer corresponding to the line number starting from 0. +tf.app.flags.DEFINE_string('labels_file', '', 'Labels file') + + +FLAGS = tf.app.flags.FLAGS + + +def _int64_feature(value): + """Wrapper for inserting int64 features into Example proto.""" + if not isinstance(value, list): + value = [value] + return tf.train.Feature(int64_list=tf.train.Int64List(value=value)) + + +def _bytes_feature(value): + """Wrapper for inserting bytes features into Example proto.""" + return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) + + +def _convert_to_example(filename, image_buffer, label, text, height, width): + """Build an Example proto for an example. + + Args: + filename: string, path to an image file, e.g., '/path/to/example.JPG' + image_buffer: string, JPEG encoding of RGB image + label: integer, identifier for the ground truth for the network + text: string, unique human-readable, e.g. 'dog' + height: integer, image height in pixels + width: integer, image width in pixels + Returns: + Example proto + """ + + colorspace = 'RGB' + channels = 3 + image_format = 'JPEG' + + example = tf.train.Example(features=tf.train.Features(feature={ + 'image/height': _int64_feature(height), + 'image/width': _int64_feature(width), + 'image/colorspace': _bytes_feature(tf.compat.as_bytes(colorspace)), + 'image/channels': _int64_feature(channels), + 'image/class/label': _int64_feature(label), + 'image/class/text': _bytes_feature(tf.compat.as_bytes(text)), + 'image/format': _bytes_feature(tf.compat.as_bytes(image_format)), + 'image/filename': _bytes_feature(tf.compat.as_bytes(os.path.basename(filename))), + 'image/encoded': _bytes_feature(tf.compat.as_bytes(image_buffer))})) + return example + + +class ImageCoder(object): + """Helper class that provides TensorFlow image coding utilities.""" + + def __init__(self): + # Create a single Session to run all image coding calls. + self._sess = tf.Session() + + # Initializes function that converts PNG to JPEG data. + self._png_data = tf.placeholder(dtype=tf.string) + image = tf.image.decode_png(self._png_data, channels=3) + self._png_to_jpeg = tf.image.encode_jpeg(image, format='rgb', quality=100) + + # Initializes function that decodes RGB JPEG data. + self._decode_jpeg_data = tf.placeholder(dtype=tf.string) + self._decode_jpeg = tf.image.decode_jpeg(self._decode_jpeg_data, channels=3) + + def png_to_jpeg(self, image_data): + return self._sess.run(self._png_to_jpeg, + feed_dict={self._png_data: image_data}) + + def decode_jpeg(self, image_data): + image = self._sess.run(self._decode_jpeg, + feed_dict={self._decode_jpeg_data: image_data}) + assert len(image.shape) == 3 + assert image.shape[2] == 3 + return image + + +def _is_png(filename): + """Determine if a file contains a PNG format image. + + Args: + filename: string, path of the image file. + + Returns: + boolean indicating if the image is a PNG. + """ + return filename.endswith('.png') + + +def _process_image(filename, coder): + """Process a single image file. + + Args: + filename: string, path to an image file e.g., '/path/to/example.JPG'. + coder: instance of ImageCoder to provide TensorFlow image coding utils. + Returns: + image_buffer: string, JPEG encoding of RGB image. + height: integer, image height in pixels. + width: integer, image width in pixels. + """ + # Read the image file. + with tf.gfile.FastGFile(filename, 'rb') as f: + image_data = f.read() + + # Convert any PNG to JPEG's for consistency. + if _is_png(filename): + print('Converting PNG to JPEG for %s' % filename) + image_data = coder.png_to_jpeg(image_data) + + # Decode the RGB JPEG. + image = coder.decode_jpeg(image_data) + + # Check that image converted to RGB + assert len(image.shape) == 3 + height = image.shape[0] + width = image.shape[1] + assert image.shape[2] == 3 + + return image_data, height, width + + +def _process_image_files_batch(coder, thread_index, ranges, name, filenames, + texts, labels, num_shards): + """Processes and saves list of images as TFRecord in 1 thread. + + Args: + coder: instance of ImageCoder to provide TensorFlow image coding utils. + thread_index: integer, unique batch to run index is within [0, len(ranges)). + ranges: list of pairs of integers specifying ranges of each batches to + analyze in parallel. + name: string, unique identifier specifying the data set + filenames: list of strings; each string is a path to an image file + texts: list of strings; each string is human readable, e.g. 'dog' + labels: list of integer; each integer identifies the ground truth + num_shards: integer number of shards for this data set. + """ + # Each thread produces N shards where N = int(num_shards / num_threads). + # For instance, if num_shards = 128, and the num_threads = 2, then the first + # thread would produce shards [0, 64). + num_threads = len(ranges) + assert not num_shards % num_threads + num_shards_per_batch = int(num_shards / num_threads) + + shard_ranges = np.linspace(ranges[thread_index][0], + ranges[thread_index][1], + num_shards_per_batch + 1).astype(int) + num_files_in_thread = ranges[thread_index][1] - ranges[thread_index][0] + + counter = 0 + for s in range(num_shards_per_batch): + # Generate a sharded version of the file name, e.g. 'train-00002-of-00010' + shard = thread_index * num_shards_per_batch + s + output_filename = '%s-%.5d-of-%.5d' % (name, shard, num_shards) + output_file = os.path.join(FLAGS.output_directory, output_filename) + writer = tf.python_io.TFRecordWriter(output_file) + + shard_counter = 0 + files_in_shard = np.arange(shard_ranges[s], shard_ranges[s + 1], dtype=int) + for i in files_in_shard: + filename = filenames[i] + label = labels[i] + text = texts[i] + + try: + image_buffer, height, width = _process_image(filename, coder) + except Exception as e: + print(e) + print('SKIPPED: Unexpected error while decoding %s.' % filename) + continue + + example = _convert_to_example(filename, image_buffer, label, + text, height, width) + writer.write(example.SerializeToString()) + shard_counter += 1 + counter += 1 + + if not counter % 1000: + print('%s [thread %d]: Processed %d of %d images in thread batch.' % + (datetime.now(), thread_index, counter, num_files_in_thread)) + sys.stdout.flush() + + writer.close() + print('%s [thread %d]: Wrote %d images to %s' % + (datetime.now(), thread_index, shard_counter, output_file)) + sys.stdout.flush() + shard_counter = 0 + print('%s [thread %d]: Wrote %d images to %d shards.' % + (datetime.now(), thread_index, counter, num_files_in_thread)) + sys.stdout.flush() + + +def _process_image_files(name, filenames, texts, labels, num_shards): + """Process and save list of images as TFRecord of Example protos. + + Args: + name: string, unique identifier specifying the data set + filenames: list of strings; each string is a path to an image file + texts: list of strings; each string is human readable, e.g. 'dog' + labels: list of integer; each integer identifies the ground truth + num_shards: integer number of shards for this data set. + """ + assert len(filenames) == len(texts) + assert len(filenames) == len(labels) + + # Break all images into batches with a [ranges[i][0], ranges[i][1]]. + spacing = np.linspace(0, len(filenames), FLAGS.num_threads + 1).astype(np.int) + ranges = [] + for i in range(len(spacing) - 1): + ranges.append([spacing[i], spacing[i + 1]]) + + # Launch a thread for each batch. + print('Launching %d threads for spacings: %s' % (FLAGS.num_threads, ranges)) + sys.stdout.flush() + + # Create a mechanism for monitoring when all threads are finished. + coord = tf.train.Coordinator() + + # Create a generic TensorFlow-based utility for converting all image codings. + coder = ImageCoder() + + threads = [] + for thread_index in range(len(ranges)): + args = (coder, thread_index, ranges, name, filenames, + texts, labels, num_shards) + t = threading.Thread(target=_process_image_files_batch, args=args) + t.start() + threads.append(t) + + # Wait for all the threads to terminate. + coord.join(threads) + print('%s: Finished writing all %d images in data set.' % + (datetime.now(), len(filenames))) + sys.stdout.flush() + + +def _find_image_files(data_dir, labels_file): + """Build a list of all images files and labels in the data set. + + Args: + data_dir: string, path to the root directory of images. + + Assumes that the image data set resides in JPEG files located in + the following directory structure. + + data_dir/dog/another-image.JPEG + data_dir/dog/my-image.jpg + + where 'dog' is the label associated with these images. + + labels_file: string, path to the labels file. + + The list of valid labels are held in this file. Assumes that the file + contains entries as such: + dog + cat + flower + where each line corresponds to a label. We map each label contained in + the file to an integer starting with the integer 0 corresponding to the + label contained in the first line. + + Returns: + filenames: list of strings; each string is a path to an image file. + texts: list of strings; each string is the class, e.g. 'dog' + labels: list of integer; each integer identifies the ground truth. + """ + print('Determining list of input files and labels from %s.' % data_dir) + unique_labels = [l.strip() for l in tf.gfile.FastGFile( + labels_file, 'r').readlines()] + + labels = [] + filenames = [] + texts = [] + + # Leave label index 0 empty as a background class. + label_index = 1 + # Construct the list of JPEG files and labels. + for text in unique_labels: + jpeg_file_path = '%s/%s/*' % (data_dir, text) + matching_files = tf.gfile.Glob(jpeg_file_path) + + labels.extend([label_index] * len(matching_files)) + texts.extend([text] * len(matching_files)) + filenames.extend(matching_files) + + if not label_index % 100: + print('Finished finding files in %d of %d classes.' % ( + label_index, len(labels))) + label_index += 1 + + # Shuffle the ordering of all image files in order to guarantee + # random ordering of the images with respect to label in the + # saved TFRecord files. Make the randomization repeatable. + shuffled_index = list(range(len(filenames))) + random.seed(12345) + random.shuffle(shuffled_index) + + filenames = [filenames[i] for i in shuffled_index] + texts = [texts[i] for i in shuffled_index] + labels = [labels[i] for i in shuffled_index] + + print('Found %d JPEG files across %d labels inside %s.' % + (len(filenames), len(unique_labels), data_dir)) + return filenames, texts, labels + + +def _process_dataset(name, directory, num_shards, labels_file): + """Process a complete data set and save it as a TFRecord. + + Args: + name: string, unique identifier specifying the data set. + directory: string, root path to the data set. + num_shards: integer number of shards for this data set. + labels_file: string, path to the labels file. + """ + filenames, texts, labels = _find_image_files(directory, labels_file) + _process_image_files(name, filenames, texts, labels, num_shards) + + +def main(unused_argv): + assert not FLAGS.train_shards % FLAGS.num_threads, ( + 'Please make the FLAGS.num_threads commensurate with FLAGS.train_shards') + assert not FLAGS.validation_shards % FLAGS.num_threads, ( + 'Please make the FLAGS.num_threads commensurate with ' + 'FLAGS.validation_shards') + print('Saving results to %s' % FLAGS.output_directory) + + # Run it! + _process_dataset('validation', FLAGS.validation_directory, + FLAGS.validation_shards, FLAGS.labels_file) + _process_dataset('train', FLAGS.train_directory, + FLAGS.train_shards, FLAGS.labels_file) + + +if __name__ == '__main__': + tf.app.run() diff --git a/stylelens/dataset/deepfashion/attr_model/build_image_data.sh b/stylelens/dataset/deepfashion/attr_model/build_image_data.sh new file mode 100755 index 0000000..8e71d4d --- /dev/null +++ b/stylelens/dataset/deepfashion/attr_model/build_image_data.sh @@ -0,0 +1,10 @@ +#source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../../slim + +echo $PYTHONPATH + +python build_image_data.py \ + --train_directory=/home/lion/attr_dataset/fabric_dataset/TRAIN_DIR\ + --validation_directory=/home/lion/attr_dataset/fabric_dataset/VALIDATION_DIR\ + --output_directory=/home/lion/attr_dataset/fabric_model/data\ + --labels_file=/home/lion/attr_dataset/fabric_model/data/fabric_label.txt diff --git a/stylelens/dataset/deepfashion/attr_model/fabric_label.txt b/stylelens/dataset/deepfashion/attr_model/fabric_label.txt new file mode 100644 index 0000000..3d5ba98 --- /dev/null +++ b/stylelens/dataset/deepfashion/attr_model/fabric_label.txt @@ -0,0 +1,218 @@ +acid +acid_wash +applique +bead +beaded +beaded_chiffon +beaded_sheer +bejeweled +bleach +bleached +bleached_denim +brocade +burnout +cable +cable_knit +cable-knit +canvas +chambray +chambray_drawstring +chenille +chiffon +chiffon_lace +chiffon_layered +chiffon_shirt +chino +chunky +chunky_knit +classic_cotton +classic_denim +classic_knit +classic_woven +clean +clean_wash +cloud +cloud_wash +coated +corduroy +cotton +cotton_drawstring +cotton_knit +cotton-blend +crepe +crepe_woven +crinkled +crochet +crochet_embroidered +crochet_knit +crochet_lace +crochet_mesh +crochet_overlay +crocheted +crocheted_lace +cuffed_denim +cutout_lace +damask +denim +denim_drawstring +denim_shirt +denim_utility +dip-dye +dip-dyed +distressed +dye +elasticized +embellished +embroidered +embroidered_gauze +embroidered_lace +embroidered_mesh +embroidered_woven +embroidery +eyelash +eyelash_knit +eyelash_lace +eyelet +faded +fair +fair_isle +faux +faux_fur +faux_leather +faux_shearling +faux_suede +feather +floral_knit +floral_lace +floral_mesh +foulard +frayed +french +french_terry +fur +fuzzy +fuzzy_knit +gauze +gauzy +gem +georgette +gingham +glass +glitter +heathered +heathered_knit +herringbone +jacquard +knit +knit_lace +lace +lace_layered +lace_mesh +lace_overlay +lace_panel +lace_paneled +lace_pleated +lace_print +lace_sheer +lace-paneled +lacy +lattice +layered +leather +leather_paneled +leather_quilted +leather-paneled +led +loop +loose +loose-knit +mesh +mesh_overlay +mesh_panel +mesh_paneled +mesh-paneled +metallic +mineral +mineral_wash +neon +neoprene +nets +netted +nylon +oil +organza +origami +overlay +panel +paneled +patched +patchwork +perforated +pima +pintuck +pintuck_pleated +pintucked +plaid +plaid_shirt +pleat +pleated +pleated_woven +pointelle +ponte +print_satin +print_scuba +purl +quilted +rhinestone +rib +rib-knit +ribbed +ribbed-knit +ripped +ruched +ruffle +ruffled +sateen +satin +scuba +seam +seamless +seersucker +semi-sheer +sequin +sequined +shaggy +shearling +sheer +sheer-paneled +shirred +shredded +sleek +slick +slub +slub-knit +sparkling +stone +stone_washed +stones +stretch +stretch-knit +studded +suede +tapestry +tartan +terry +textured +textured_woven +tie-dye +tiered +tile +tulle +tweed +twill +velvet +velveteen +waffle +wash +washed +woven diff --git a/stylelens/dataset/deepfashion/attr_model/get_object_by_color.py b/stylelens/dataset/deepfashion/attr_model/get_object_by_color.py new file mode 100644 index 0000000..77183dd --- /dev/null +++ b/stylelens/dataset/deepfashion/attr_model/get_object_by_color.py @@ -0,0 +1,95 @@ +from __future__ import print_function +from stylelens_dataset.colors import Colors +from pprint import pprint +import os +import string +import time +import tensorflow as tf +import urllib.request as urllib +# import redis +# from bluelens_log import Logging + +api_instance = Colors() + +flags = tf.app.flags +flags.DEFINE_string('color_dataset_path', '', 'Path to color_dataset_path') +FLAGS = flags.FLAGS + +''' +REDIS_SERVER = os.environ['REDIS_SERVER'] +REDIS_PASSWORD = os.environ['REDIS_PASSWORD'] +options = { + 'REDIS_SERVER': REDIS_SERVER, + 'REDIS_PASSWORD': REDIS_PASSWORD +} +log = Logging(options, tag='generate-color-classifier-dataset') +rconn = redis.StrictRedis(REDIS_SERVER, decode_responses=True, port=6379, password=REDIS_PASSWORD) +''' + +def get_colors(classes): + color_dataset_path = FLAGS.color_dataset_path + + try: + for clazz in classes: + color_name = clazz + + + os.chdir(color_dataset_path) + try: + os.mkdir(color_name) + except FileExistsError: + pass + os.chdir(color_name) + + pprint('get_colors API... color : ' + color_name) + + offset = 0 + limit = 100 + i = 0 + while True: + + colors = api_instance.get_colors_by_name(color_name, offset=offset, + limit=limit) + + for color in colors: + _id = str(color['_id']) + download_image_from_url(color['main_image'], _id + '.jpg') + # pprint(color_object_list) + + if limit > len(colors): + break + else: + offset += limit + i += 1 + pprint('get_colors API... color : ' + color_name + ' / ' + str(i)) + + pprint('get_colors API... color : ' + color_name + ' ALL Done !') + + except Exception as e: + print("Exception when calling get_colors_by_name: %s\n" % e) + +def get_color_classes(): + try: + classes = api_instance.get_classes() + return classes + except Exception as e: + print("Exception when calling get_color_classes: %s\n" % e) + return None + +def download_image_from_url(url, filename): + try: + urllib.urlretrieve(url, filename) + except urllib.HTTPError: + pass + + +def main(_): + # log.info('Start generate-color-classifier-dataset') + #classes = get_color_classes() + color_list = ['Black', 'Gray', 'Purple', 'Blue', 'Brown','Green', 'Orange', 'Red', 'Pink', 'Yellow','White', 'Navy', 'Beige'] + classes = color_list + #if classes: + get_colors(classes) + +if __name__ == '__main__': + tf.app.run() diff --git a/stylelens/dataset/deepfashion/attr_model/get_object_by_fabric.py b/stylelens/dataset/deepfashion/attr_model/get_object_by_fabric.py new file mode 100644 index 0000000..68f344d --- /dev/null +++ b/stylelens/dataset/deepfashion/attr_model/get_object_by_fabric.py @@ -0,0 +1,75 @@ +from __future__ import print_function +from stylelens_dataset.objects import Objects +from pprint import pprint +import os +import string +import time +import tensorflow as tf +import urllib.request as urllib + +ATTR_CLOTH_FILE = './list_attr_cloth_fabric.txt' + +api_instance = Objects() + +flags = tf.app.flags +flags.DEFINE_string('fabric_dataset_path', '', 'Path to fabric_dataset_path') +FLAGS = flags.FLAGS + +def get_attribute_clothes(): + attr_cloth = open(ATTR_CLOTH_FILE, 'r') + attribute_clothes = [] + for pair in attr_cloth.readlines(): + map = pair.strip().rsplit(' ', 1) + attr = {} + attr['name'] = map[0].strip() + attr['type'] = map[1] + attribute_clothes.append(attr) + return attribute_clothes + + +def download_image_from_url(url, filename): + try: + urllib.urlretrieve(url, filename) + except urllib.HTTPError: + pass + + +def main(_): + fabric_dataset_path = FLAGS.fabric_dataset_path + # time.sleep(7200) + try: + + attrs = get_attribute_clothes() + for attr in attrs: + print(attr['name']) + attr_name = attr['name'].replace(" ", "_") + + offset = 0 + limit = 100 + + os.chdir(fabric_dataset_path) + try: + os.mkdir(attr_name) + except FileExistsError: + pass + os.chdir(attr_name) + + + while True: + res = api_instance.get_objects_by_fabric(attr['name'], offset=offset, limit=limit) + # save image by fabric attr + for idx in range(len(res)): + url = res[idx]['url'] + _id = str(res[idx]['_id']) + filename = _id + '.jpg' + download_image_from_url(url, str(filename)) + if limit > len(res): + break + else: + offset = offset + limit + + except Exception as e: + print("Exception when calling get_images_by_source: %s\n" % e) + +if __name__ == '__main__': + tf.app.run() diff --git a/stylelens/dataset/deepfashion/attr_model/list_attr_cloth_fabric.txt b/stylelens/dataset/deepfashion/attr_model/list_attr_cloth_fabric.txt new file mode 100644 index 0000000..f401f95 --- /dev/null +++ b/stylelens/dataset/deepfashion/attr_model/list_attr_cloth_fabric.txt @@ -0,0 +1,218 @@ +acid 2 +acid wash 2 +applique 2 +bead 2 +beaded 2 +beaded chiffon 2 +beaded sheer 2 +bejeweled 2 +bleach 2 +bleached 2 +bleached denim 2 +brocade 2 +burnout 2 +cable 2 +cable knit 2 +cable-knit 2 +canvas 2 +chambray 2 +chambray drawstring 2 +chenille 2 +chiffon 2 +chiffon lace 2 +chiffon layered 2 +chiffon shirt 2 +chino 2 +chunky 2 +chunky knit 2 +classic cotton 2 +classic denim 2 +classic knit 2 +classic woven 2 +clean 2 +clean wash 2 +cloud 2 +cloud wash 2 +coated 2 +corduroy 2 +cotton 2 +cotton drawstring 2 +cotton knit 2 +cotton-blend 2 +crepe 2 +crepe woven 2 +crinkled 2 +crochet 2 +crochet embroidered 2 +crochet knit 2 +crochet lace 2 +crochet mesh 2 +crochet overlay 2 +crocheted 2 +crocheted lace 2 +cuffed denim 2 +cutout lace 2 +damask 2 +denim 2 +denim drawstring 2 +denim shirt 2 +denim utility 2 +dip-dye 2 +dip-dyed 2 +distressed 2 +dye 2 +elasticized 2 +embellished 2 +embroidered 2 +embroidered gauze 2 +embroidered lace 2 +embroidered mesh 2 +embroidered woven 2 +embroidery 2 +eyelash 2 +eyelash knit 2 +eyelash lace 2 +eyelet 2 +faded 2 +fair 2 +fair isle 2 +faux 2 +faux fur 2 +faux leather 2 +faux shearling 2 +faux suede 2 +feather 2 +floral knit 2 +floral lace 2 +floral mesh 2 +foulard 2 +frayed 2 +french 2 +french terry 2 +fur 2 +fuzzy 2 +fuzzy knit 2 +gauze 2 +gauzy 2 +gem 2 +georgette 2 +gingham 2 +glass 2 +glitter 2 +heathered 2 +heathered knit 2 +herringbone 2 +jacquard 2 +knit 2 +knit lace 2 +lace 2 +lace layered 2 +lace mesh 2 +lace overlay 2 +lace panel 2 +lace paneled 2 +lace pleated 2 +lace print 2 +lace sheer 2 +lace-paneled 2 +lacy 2 +lattice 2 +layered 2 +leather 2 +leather paneled 2 +leather quilted 2 +leather-paneled 2 +led 2 +loop 2 +loose 2 +loose-knit 2 +mesh 2 +mesh overlay 2 +mesh panel 2 +mesh paneled 2 +mesh-paneled 2 +metallic 2 +mineral 2 +mineral wash 2 +neon 2 +neoprene 2 +nets 2 +netted 2 +nylon 2 +oil 2 +organza 2 +origami 2 +overlay 2 +panel 2 +paneled 2 +patched 2 +patchwork 2 +perforated 2 +pima 2 +pintuck 2 +pintuck pleated 2 +pintucked 2 +plaid 2 +plaid shirt 2 +pleat 2 +pleated 2 +pleated woven 2 +pointelle 2 +ponte 2 +print satin 2 +print scuba 2 +purl 2 +quilted 2 +rhinestone 2 +rib 2 +rib-knit 2 +ribbed 2 +ribbed-knit 2 +ripped 2 +ruched 2 +ruffle 2 +ruffled 2 +sateen 2 +satin 2 +scuba 2 +seam 2 +seamless 2 +seersucker 2 +semi-sheer 2 +sequin 2 +sequined 2 +shaggy 2 +shearling 2 +sheer 2 +sheer-paneled 2 +shirred 2 +shredded 2 +sleek 2 +slick 2 +slub 2 +slub-knit 2 +sparkling 2 +stone 2 +stone washed 2 +stones 2 +stretch 2 +stretch-knit 2 +studded 2 +suede 2 +tapestry 2 +tartan 2 +terry 2 +textured 2 +textured woven 2 +tie-dye 2 +tiered 2 +tile 2 +tulle 2 +tweed 2 +twill 2 +velvet 2 +velveteen 2 +waffle 2 +wash 2 +washed 2 +woven 2 diff --git a/stylelens/dataset/deepfashion/od_model_3class/create_tf_record_from_db.py b/stylelens/dataset/deepfashion/od_model_3class/create_tf_record_from_db.py new file mode 100644 index 0000000..8be94d3 --- /dev/null +++ b/stylelens/dataset/deepfashion/od_model_3class/create_tf_record_from_db.py @@ -0,0 +1,216 @@ +r"""Convert raw PASCAL dataset to TFRecord for object_detection. + +Example usage: + ./create_pascal_tf_record --data_dir=/home/user/VOCdevkit \ + --folder=merged \ + --output_path=/home/user/pascal.record +""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import hashlib +import io +# import logging +import os +import random +# import re + +import PIL.Image +import tensorflow as tf + +from os import listdir +# from os.path import isdir, isfile, join + +from object_detection.utils import dataset_util +# from object_detection.utils import label_map_util + +from stylelens_dataset.images import Images +# from stylelens_dataset.objects import Objects + + +flags = tf.app.flags +flags.DEFINE_string('data_dir', '', 'Root directory to raw PASCAL VOC dataset.') +flags.DEFINE_string('set', 'train', 'Convert training set, validation set or ' + 'merged set.') +flags.DEFINE_string('annotations_dir', 'Annotations', + '(Relative) path to annotations directory.') +flags.DEFINE_string('folder', 'merged', 'Desired challenge folder.') +flags.DEFINE_string('train_output_path', '', 'Path to train output TFRecord') +flags.DEFINE_string('eval_output_path', '', 'Path to eval output TFRecord') +flags.DEFINE_string('label_map_path', 'data/pascal_label_map.pbtxt', + 'Path to label map proto') +flags.DEFINE_boolean('ignore_difficult_instances', False, 'Whether to ignore ' + 'difficult instances') +FLAGS = flags.FLAGS +SETS = ['train', 'val', 'trainval', 'test'] + + +def dict_to_tf_example(data, image_subdirectory='JPEGImages'): + """ + Convert XML derived dict to tf.Example proto. + + Notice that this function normalizes the bounding box coordinates provided + by the raw data. + + Args: + data: dict holding PASCAL XML fields for a single image (obtained by + running dataset_util.recursive_parse_xml_to_dict) + dataset_directory: Path to root directory holding PASCAL dataset + label_map_dict: A map from string label names to integers ids. + ignore_difficult_instances: Whether to skip difficult instances in the + dataset (default: False). + 1image_subdirectory: String specifying subdirectory within the + PASCAL dataset directory holding the actual image data. + + Returns: + example: The converted tf.Example. + + Raises: + ValueError: if the image pointed to by data['filename'] + is not a valid JPEG + """ + + full_path = os.path.join('/home/lion/dataset', data['file']) + with tf.gfile.GFile(full_path, 'rb') as fid: + encoded_jpg = fid.read() + encoded_jpg_io = io.BytesIO(encoded_jpg) + image = PIL.Image.open(encoded_jpg_io) + if image.format != 'JPEG': + raise ValueError('Image format not JPEG') + key = hashlib.sha256(encoded_jpg).hexdigest() + + xmin = [] + ymin = [] + xmax = [] + ymax = [] + poses = [] + classes = [] + classes_text = [] + + width = int(data['width']) + height = int(data['height']) + xmin.append(float(data['bbox']['x1']) / width) + xmax.append(float(data['bbox']['x2']) / width) + + if int(data['category_class']) == 2: #excepyion about bottom + ymin.append(float(data['bbox']['y1']+8) / height) + else: + ymin.append(float(data['bbox']['y1']) / height) + + if int(data['category_class']) == 1: #exception about top + ymax.append(float(data['bbox']['y2']-8) / height) + else: + ymax.append(float(data['bbox']['y2']) / height) + + classes_text.append(data['category_name'].encode('utf8')) + classes.append(int(data['category_class'])) + difficult = [0] + truncated = [0] + poses.append('Frontal'.encode('utf8')) + + 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( + data['file'].encode('utf8')), + 'image/source_id': dataset_util.bytes_feature( + str(data['_id']).encode('utf8')), + 'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')), + 'image/encoded': dataset_util.bytes_feature(encoded_jpg), + 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), + 'image/object/bbox/xmin': dataset_util.float_list_feature(xmin), + 'image/object/bbox/xmax': dataset_util.float_list_feature(xmax), + 'image/object/bbox/ymin': dataset_util.float_list_feature(ymin), + 'image/object/bbox/ymax': dataset_util.float_list_feature(ymax), + 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), + 'image/object/class/label': dataset_util.int64_list_feature(classes), + 'image/object/difficult': dataset_util.int64_list_feature(difficult), + 'image/object/truncated': dataset_util.int64_list_feature(truncated), + 'image/object/view': dataset_util.bytes_list_feature(poses), + })) + return example + +def read_from_db(dataset_api, category_class): + + offset = 0 + limit = 50 + idx = 0 + images = [] + valid_images = [] + + while True: + try: + res = dataset_api.get_images_by_category_class(category_class, offset=offset, limit=limit) + images.extend(res) + + if limit > len(res): + print("done") + break + + else: + offset = offset + len(res) + except Exception as e: + print(str(e)) + """ + get is_valid dataset + + """ + + for image in images: + if 'is_valid' in image: + if image['is_valid']: + idx += 1 + valid_images.append(image) + + random.shuffle(valid_images) + + if category_class == str(3): + cut_idx = 22454 + train_images = valid_images[0:cut_idx] + eval_images = valid_images[cut_idx:-1] + print(cut_idx) + else: + cut_idx = 32000 + train_images = valid_images[0:cut_idx] + eval_images = valid_images[cut_idx:40001] + + return (train_images, eval_images) + + +def main(_): + dataset_api = Images() + train_writer = tf.python_io.TFRecordWriter(FLAGS.train_output_path) + eval_writer = tf.python_io.TFRecordWriter(FLAGS.eval_output_path) + + train_images = [] + eval_images = [] + + for i in range(1, 4): + category_class = str(i) + print(category_class) + + (category_train_images, category_eval_images) = read_from_db(dataset_api, + category_class) + train_images.extend(category_train_images) + eval_images.extend(category_eval_images) + print("category_class: {} read from db done!".format(category_class)) + + random.shuffle(train_images) + random.shuffle(eval_images) + + for image in train_images: + tf_data = dict_to_tf_example(image) + train_writer.write(tf_data.SerializeToString()) + + for image in eval_images: + tf_data = dict_to_tf_example(image) + eval_writer.write(tf_data.SerializeToString()) + + print("make train/eval TFRecord done!") + train_writer.close() + eval_writer.close() + + +if __name__ == '__main__': + tf.app.run() diff --git a/stylelens/dataset/deepfashion/od_model_separate/create_3class_tfrecord_from_db.py b/stylelens/dataset/deepfashion/od_model_separate/create_3class_tfrecord_from_db.py new file mode 100644 index 0000000..1c8e485 --- /dev/null +++ b/stylelens/dataset/deepfashion/od_model_separate/create_3class_tfrecord_from_db.py @@ -0,0 +1,193 @@ +r"""Convert raw PASCAL dataset to TFRecord for object_detection. + +Example usage: + ./create_pascal_tf_record --data_dir=/home/user/VOCdevkit \ + --folder=merged \ + --output_path=/home/user/pascal.record +""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import hashlib +import io +# import logging +import os +import random +# import re + +import PIL.Image +import tensorflow as tf + +from os import listdir +# from os.path import isdir, isfile, join + +from object_detection.utils import dataset_util +# from object_detection.utils import label_map_util + +from stylelens_dataset.images import Images +# from stylelens_dataset.objects import Objects + + +flags = tf.app.flags +flags.DEFINE_string('data_dir', '', 'Root directory to raw PASCAL VOC dataset.') +flags.DEFINE_string('category_class', '', 'top, bottom, full.') +flags.DEFINE_string('set', 'train', 'Convert training set, validation set or ' + 'merged set.') +flags.DEFINE_string('annotations_dir', 'Annotations', + '(Relative) path to annotations directory.') +flags.DEFINE_string('folder', 'merged', 'Desired challenge folder.') +flags.DEFINE_string('train_output_path', '', 'Path to train output TFRecord') +flags.DEFINE_string('eval_output_path', '', 'Path to eval output TFRecord') +flags.DEFINE_string('label_map_path', 'data/pascal_label_map.pbtxt', + 'Path to label map proto') +flags.DEFINE_boolean('ignore_difficult_instances', False, 'Whether to ignore ' + 'difficult instances') +FLAGS = flags.FLAGS +SETS = ['train', 'val', 'trainval', 'test'] + + +def dict_to_tf_example(data, image_subdirectory='JPEGImages'): + """ + Convert XML derived dict to tf.Example proto. + + Notice that this function normalizes the bounding box coordinates provided + by the raw data. + + Args: + data: dict holding PASCAL XML fields for a single image (obtained by + running dataset_util.recursive_parse_xml_to_dict) + dataset_directory: Path to root directory holding PASCAL dataset + label_map_dict: A map from string label names to integers ids. + ignore_difficult_instances: Whether to skip difficult instances in the + dataset (default: False). + 1image_subdirectory: String specifying subdirectory within the + PASCAL dataset directory holding the actual image data. + + Returns: + example: The converted tf.Example. + + Raises: + ValueError: if the image pointed to by data['filename'] + is not a valid JPEG + """ + full_path = os.path.join('/home/lion/dataset', data['file']) + with tf.gfile.GFile(full_path, 'rb') as fid: + encoded_jpg = fid.read() + encoded_jpg_io = io.BytesIO(encoded_jpg) + image = PIL.Image.open(encoded_jpg_io) + if image.format != 'JPEG': + raise ValueError('Image format not JPEG') + key = hashlib.sha256(encoded_jpg).hexdigest() + + xmin = [] + ymin = [] + xmax = [] + ymax = [] + poses = [] + classes = [] + classes_text = [] + + width = int(data['width']) + height = int(data['height']) + + xmin.append(float(data['bbox']['x1'] / width)) + ymin.append(float(data['bbox']['y1'] / height)) + xmax.append(float(data['bbox']['x2'] / width)) + ymax.append(float(data['bbox']['y2'] / height)) + classes_text.append(data['category_name'].encode('utf8')) + # classes.append(int(data['category_class'])) + classes.append(int(1)) + difficult = [0] + truncated = [0] + poses.append('Frontal'.encode('utf8')) + + 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( + data['file'].encode('utf8')), + 'image/source_id': dataset_util.bytes_feature( + str(data['_id']).encode('utf8')), + 'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')), + 'image/encoded': dataset_util.bytes_feature(encoded_jpg), + 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), + 'image/object/bbox/xmin': dataset_util.float_list_feature(xmin), + 'image/object/bbox/xmax': dataset_util.float_list_feature(xmax), + 'image/object/bbox/ymin': dataset_util.float_list_feature(ymin), + 'image/object/bbox/ymax': dataset_util.float_list_feature(ymax), + 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), + 'image/object/class/label': dataset_util.int64_list_feature(classes), + 'image/object/difficult': dataset_util.int64_list_feature(difficult), + 'image/object/truncated': dataset_util.int64_list_feature(truncated), + 'image/object/view': dataset_util.bytes_list_feature(poses), + })) + return example + +def read_from_db(dataset_api, category_class): + offset = 0 + limit = 50 + images = [] + + while True: + try: + res = dataset_api.get_images_by_category_class(category_class, offset=offset, limit=limit) + images.extend(res) + print(len(images)) + if limit > len(res): + print("done") + break + + else: + offset = offset + len(res) + except Exception as e: + print(str(e)) + + random.shuffle(images) + cut_idx = len(images) + cut_idx = int(cut_idx*0.9) + + train_images = images[0:cut_idx] + eval_images = images[cut_idx:-1] + + return (train_images, eval_images) + + +def main(_): + dataset_api = Images() + train_writer = tf.python_io.TFRecordWriter(FLAGS.train_output_path) + eval_writer = tf.python_io.TFRecordWriter(FLAGS.eval_output_path) + + train_images = [] + eval_images = [] + + category_class = str(FLAGS.category_class) + (train_images, eval_images) = read_from_db(dataset_api,category_class) + print("category_class: {} read from db done!".format(category_class)) + + random.shuffle(train_images) + random.shuffle(eval_images) + + print("----images_num-------") + print(len(train_images)) + print(len(eval_images)) + print("---------------------") + for image in train_images: + if 'is_valid' in image: + if image['is_valid']: + tf_data = dict_to_tf_example(image) + train_writer.write(tf_data.SerializeToString()) + + for image in eval_images: + if 'is_valid' in image: + if image['is_valid']: + tf_data = dict_to_tf_example(image) + eval_writer.write(tf_data.SerializeToString()) + + print("make train/eval TFRecord done!") + train_writer.close() + eval_writer.close() + + +if __name__ == '__main__': + tf.app.run() diff --git a/stylelens/dataset/deepfashion/od_model_top_full/create_tf_record_from_db.py b/stylelens/dataset/deepfashion/od_model_top_full/create_tf_record_from_db.py new file mode 100644 index 0000000..ac806bb --- /dev/null +++ b/stylelens/dataset/deepfashion/od_model_top_full/create_tf_record_from_db.py @@ -0,0 +1,227 @@ +r"""Convert raw PASCAL dataset to TFRecord for object_detection. + +Example usage: + ./create_pascal_tf_record --data_dir=/home/user/VOCdevkit \ + --folder=merged \ + --output_path=/home/user/pascal.record +""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import hashlib +import io +# import logging +import os +import random +# import re + +import PIL.Image +import tensorflow as tf + +from os import listdir +# from os.path import isdir, isfile, join + +from object_detection.utils import dataset_util +# from object_detection.utils import label_map_util + +from stylelens_dataset.images import Images +# from stylelens_dataset.objects import Objects + + +flags = tf.app.flags +flags.DEFINE_string('data_dir', '', 'Root directory to raw PASCAL VOC dataset.') +flags.DEFINE_string('set', 'train', 'Convert training set, validation set or ' + 'merged set.') +flags.DEFINE_string('annotations_dir', 'Annotations', + '(Relative) path to annotations directory.') +flags.DEFINE_string('folder', 'merged', 'Desired challenge folder.') +flags.DEFINE_string('train_output_path', '', 'Path to train output TFRecord') +flags.DEFINE_string('eval_output_path', '', 'Path to eval output TFRecord') +flags.DEFINE_string('label_map_path', 'data/pascal_label_map.pbtxt', + 'Path to label map proto') +flags.DEFINE_boolean('ignore_difficult_instances', False, 'Whether to ignore ' + 'difficult instances') +FLAGS = flags.FLAGS +SETS = ['train', 'val', 'trainval', 'test'] + + +def dict_to_tf_example(data, image_subdirectory='JPEGImages'): + """ + Convert XML derived dict to tf.Example proto. + + Notice that this function normalizes the bounding box coordinates provided + by the raw data. + + Args: + data: dict holding PASCAL XML fields for a single image (obtained by + running dataset_util.recursive_parse_xml_to_dict) + dataset_directory: Path to root directory holding PASCAL dataset + label_map_dict: A map from string label names to integers ids. + ignore_difficult_instances: Whether to skip difficult instances in the + dataset (default: False). + 1image_subdirectory: String specifying subdirectory within the + PASCAL dataset directory holding the actual image data. + + Returns: + example: The converted tf.Example. + + Raises: + ValueError: if the image pointed to by data['filename'] + is not a valid JPEG + """ + + full_path = os.path.join('/home/lion/dataset', data['file']) + with tf.gfile.GFile(full_path, 'rb') as fid: + encoded_jpg = fid.read() + encoded_jpg_io = io.BytesIO(encoded_jpg) + image = PIL.Image.open(encoded_jpg_io) + if image.format != 'JPEG': + raise ValueError('Image format not JPEG') + key = hashlib.sha256(encoded_jpg).hexdigest() + + xmin = [] + ymin = [] + xmax = [] + ymax = [] + poses = [] + classes = [] + classes_text = [] + + width = int(data['width']) + height = int(data['height']) + xmin.append(float(data['bbox']['x1']) / width) + xmax.append(float(data['bbox']['x2']) / width) + + if int(data['category_class']) == 2: #excepyion about bottom + ymin.append(float(data['bbox']['y1']+8) / height) + else: + ymin.append(float(data['bbox']['y1']) / height) + + if int(data['category_class']) == 1: #exception about top + ymax.append(float(data['bbox']['y2']-8) / height) + else: + ymax.append(float(data['bbox']['y2']) / height) + + classes_text.append(data['category_name'].encode('utf8')) + if data['category_class'] == str(1): + classes.append(int(data['category_class'])) + if data['category_class'] == str(3): + classes.append(int(2)) + difficult = [0] + truncated = [0] + poses.append('Frontal'.encode('utf8')) + + 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( + data['file'].encode('utf8')), + 'image/source_id': dataset_util.bytes_feature( + str(data['_id']).encode('utf8')), + 'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')), + 'image/encoded': dataset_util.bytes_feature(encoded_jpg), + 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), + 'image/object/bbox/xmin': dataset_util.float_list_feature(xmin), + 'image/object/bbox/xmax': dataset_util.float_list_feature(xmax), + 'image/object/bbox/ymin': dataset_util.float_list_feature(ymin), + 'image/object/bbox/ymax': dataset_util.float_list_feature(ymax), + 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), + 'image/object/class/label': dataset_util.int64_list_feature(classes), + 'image/object/difficult': dataset_util.int64_list_feature(difficult), + 'image/object/truncated': dataset_util.int64_list_feature(truncated), + 'image/object/view': dataset_util.bytes_list_feature(poses), + })) + return example + +def read_from_db(dataset_api, category_class): + + offset = 0 + limit = 50 + idx = 0 + images = [] + valid_images = [] + + while True: + try: + res = dataset_api.get_images_by_category_class(category_class, offset=offset, limit=limit) + images.extend(res) + + if limit > len(res): + print("done") + break + + else: + offset = offset + len(res) + except Exception as e: + print(str(e)) + """ + get is_valid dataset + + """ + + for image in images: + if 'is_valid' in image: + if image['is_valid']: + idx += 1 + valid_images.append(image) + + random.shuffle(valid_images) + + if category_class == str(3): + cut_idx = 22454 + train_images = valid_images[0:cut_idx] + eval_images = valid_images[cut_idx:-1] + print(cut_idx) + else: + cut_idx = 48000 + train_images = valid_images[0:cut_idx] + eval_images = valid_images[cut_idx:60001] + + return (train_images, eval_images) + + +def main(_): + dataset_api = Images() + train_writer = tf.python_io.TFRecordWriter(FLAGS.train_output_path) + eval_writer = tf.python_io.TFRecordWriter(FLAGS.eval_output_path) + + train_images = [] + eval_images = [] + + # top / full + category_class = str(1) + print(category_class) + (category_train_images, category_eval_images) = read_from_db(dataset_api,category_class) + train_images.extend(category_train_images) + eval_images.extend(category_eval_images) + print("category_class: {} read from db done!".format(category_class)) + + category_class = str(3) + print(category_class) + (category_train_images, category_eval_images) = read_from_db(dataset_api,category_class) + train_images.extend(category_train_images) + eval_images.extend(category_eval_images) + print("category_class: {} read from db done!".format(category_class)) + + + random.shuffle(train_images) + random.shuffle(train_images) + random.shuffle(eval_images) + random.shuffle(eval_images) + + for image in train_images: + tf_data = dict_to_tf_example(image) + train_writer.write(tf_data.SerializeToString()) + + for image in eval_images: + tf_data = dict_to_tf_example(image) + eval_writer.write(tf_data.SerializeToString()) + + print("make train/eval TFRecord done!") + train_writer.close() + eval_writer.close() + + +if __name__ == '__main__': + tf.app.run() diff --git a/stylelens/object_detection/create_od_train_record.sh b/stylelens/object_detection/create_od_train_record.sh deleted file mode 100755 index 373e738..0000000 --- a/stylelens/object_detection/create_od_train_record.sh +++ /dev/null @@ -1,12 +0,0 @@ -#source activate bl-magi -export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../../slim -export DB_DATASET_HOST="35.187.193.199" -export DB_DATASET_NAME="bl-db-dataset" -export DB_DATASET_PORT="27017" -#export DATASET_PATH='gs://bluelens-style-model/object_detection' - -echo $PYTHONPATH - -python create_tf_record_from_db.py \ - --train_output_path=./train.record \ - --eval_output_path=./eval.record diff --git a/stylelens/object_detection/create_tf_record_from_db1.py b/stylelens/object_detection/create_tf_record/base_create_tf_record_from_db.py similarity index 100% rename from stylelens/object_detection/create_tf_record_from_db1.py rename to stylelens/object_detection/create_tf_record/base_create_tf_record_from_db.py diff --git a/stylelens/object_detection/create_3class_train_record.sh b/stylelens/object_detection/create_tf_record/create_3class_train_record.sh similarity index 81% rename from stylelens/object_detection/create_3class_train_record.sh rename to stylelens/object_detection/create_tf_record/create_3class_train_record.sh index 13c8e20..cb32923 100755 --- a/stylelens/object_detection/create_3class_train_record.sh +++ b/stylelens/object_detection/create_tf_record/create_3class_train_record.sh @@ -1,7 +1,7 @@ #!/bin/bash source activate bl-magi -export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../../slim +export PYTHONPATH=$PYTHONPATH:`pwd`/../../../tensorflow:`pwd`/../../../slim #export DATASET_PATH='gs://bluelens-style-model/object_detection' export DATASET_PATH='/dataset/deepfashion3' diff --git a/stylelens/object_detection/create_3class_val_record.sh b/stylelens/object_detection/create_tf_record/create_3class_val_record.sh similarity index 82% rename from stylelens/object_detection/create_3class_val_record.sh rename to stylelens/object_detection/create_tf_record/create_3class_val_record.sh index fc42ba0..6bd5a4d 100755 --- a/stylelens/object_detection/create_3class_val_record.sh +++ b/stylelens/object_detection/create_tf_record/create_3class_val_record.sh @@ -1,7 +1,7 @@ #!/bin/bash source activate bl-magi -export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../../slim +export PYTHONPATH=$PYTHONPATH:`pwd`/../../../tensorflow:`pwd`/../../../slim #export MODEL_DATASET_PATH='gs://bluelens-style-model/object_detection' export DATASET_PATH='/dataset/deepfashion3' diff --git a/stylelens/object_detection/create_tf_record.py b/stylelens/object_detection/create_tf_record/create_tf_record.py similarity index 100% rename from stylelens/object_detection/create_tf_record.py rename to stylelens/object_detection/create_tf_record/create_tf_record.py diff --git a/stylelens/object_detection/create_tf_record_from_db.py b/stylelens/object_detection/create_tf_record/create_tf_record_from_db.py similarity index 97% rename from stylelens/object_detection/create_tf_record_from_db.py rename to stylelens/object_detection/create_tf_record/create_tf_record_from_db.py index 6f6ff5f..be6c9a6 100644 --- a/stylelens/object_detection/create_tf_record_from_db.py +++ b/stylelens/object_detection/create_tf_record/create_tf_record_from_db.py @@ -71,7 +71,7 @@ def dict_to_tf_example(data, image_subdirectory='JPEGImages'): is not a valid JPEG """ - full_path = os.path.join('/dataset', data['file']) + full_path = os.path.join('/home/lion/dataset', data['file']) with tf.gfile.GFile(full_path, 'rb') as fid: encoded_jpg = fid.read() encoded_jpg_io = io.BytesIO(encoded_jpg) @@ -96,13 +96,8 @@ def dict_to_tf_example(data, image_subdirectory='JPEGImages'): ymax.append(float(data['bbox']['y2']) / height) classes_text.append(data['category_name'].encode('utf8')) classes.append(int(data['category_class'])) -<<<<<<< Updated upstream - difficult = int([0]) - truncated = int([0]) -======= difficult = [0] truncated = [0] ->>>>>>> Stashed changes poses.append('Frontal'.encode('utf8')) example = tf.train.Example(features=tf.train.Features(feature={ diff --git a/stylelens/object_detection/create_train_record.sh b/stylelens/object_detection/create_tf_record/create_train_record.sh similarity index 82% rename from stylelens/object_detection/create_train_record.sh rename to stylelens/object_detection/create_tf_record/create_train_record.sh index 201b930..006d01f 100755 --- a/stylelens/object_detection/create_train_record.sh +++ b/stylelens/object_detection/create_tf_record/create_train_record.sh @@ -1,7 +1,7 @@ #!/bin/bash source activate bl-magi -export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../../slim +export PYTHONPATH=$PYTHONPATH:`pwd`/../../../tensorflow:`pwd`/../../../slim #export MODEL_BASE_PATH='gs://bluelens-style-model/object_detection' export MODEL_BASE_PATH='/dataset/deepfashion' diff --git a/stylelens/object_detection/create_val_record.sh b/stylelens/object_detection/create_tf_record/create_val_record.sh similarity index 82% rename from stylelens/object_detection/create_val_record.sh rename to stylelens/object_detection/create_tf_record/create_val_record.sh index aca5be3..2abcb3e 100755 --- a/stylelens/object_detection/create_val_record.sh +++ b/stylelens/object_detection/create_tf_record/create_val_record.sh @@ -1,7 +1,7 @@ #!/bin/bash source activate bl-magi -export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../../slim +export PYTHONPATH=$PYTHONPATH:`pwd`/../../../tensorflow:`pwd`/../../../slim #export MODEL_BASE_PATH='gs://bluelens-style-model/object_detection' export MODEL_BASE_PATH='/dataset/deepfashion' diff --git a/stylelens/object_detection/eval.sh b/stylelens/object_detection/eval.sh index 51db04b..501bb4a 100755 --- a/stylelens/object_detection/eval.sh +++ b/stylelens/object_detection/eval.sh @@ -3,10 +3,10 @@ source activate bl-magi export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../../tensorflow/slim:'pwd'/../../tensorflow/object_detection #export MODEL_BASE_PATH='gs://bluelens-style-model/object_detection' -export MODEL_BASE_PATH='/home/lion/dataset/deepfashion3' +export MODEL_BASE_PATH='/home/lion/dataset/object_3class/3class_model' python ./eval.py \ --logtostderr \ --pipeline_config_path=$MODEL_BASE_PATH/models/model/ssd_inception_v2_3class.config \ - --checkpoint_dir=$MODEL_BASE_PATH/models/model/train \ - --eval_dir=$MODEL_BASE_PATH/models/model/eval + --checkpoint_dir=$MODEL_BASE_PATH/models/model/train1 \ + --eval_dir=$MODEL_BASE_PATH/models/model/eval1 diff --git a/stylelens/object_detection/eval_full.sh b/stylelens/object_detection/eval_full.sh new file mode 100755 index 0000000..aa9d517 --- /dev/null +++ b/stylelens/object_detection/eval_full.sh @@ -0,0 +1,11 @@ +#! /bin/bash + +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../../tensorflow/slim:'pwd'/../../tensorflow/object_detection +export MODEL_BASE_PATH='/home/lion/dataset/object_3class/full_model' + +python ./eval.py \ + --logtostderr \ + --pipeline_config_path=$MODEL_BASE_PATH/models/model/ssd_inception_v2_3class.config \ + --checkpoint_dir=$MODEL_BASE_PATH/models/model/train1 \ + --eval_dir=$MODEL_BASE_PATH/models/model/eval1 diff --git a/stylelens/object_detection/eval_top.sh b/stylelens/object_detection/eval_top.sh new file mode 100755 index 0000000..f2c688d --- /dev/null +++ b/stylelens/object_detection/eval_top.sh @@ -0,0 +1,11 @@ +#! /bin/bash + +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../../tensorflow/slim:'pwd'/../../tensorflow/object_detection +export MODEL_BASE_PATH='/home/lion/dataset/object_3class/top_model' + +python ./eval.py \ + --logtostderr \ + --pipeline_config_path=$MODEL_BASE_PATH/models/model/ssd_inception_v2_3class.config \ + --checkpoint_dir=$MODEL_BASE_PATH/models/model/train2 \ + --eval_dir=$MODEL_BASE_PATH/models/model/eval2 diff --git a/stylelens/object_detection/eval_top_full.sh b/stylelens/object_detection/eval_top_full.sh new file mode 100755 index 0000000..73cf869 --- /dev/null +++ b/stylelens/object_detection/eval_top_full.sh @@ -0,0 +1,11 @@ +#! /bin/bash + +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../../tensorflow/slim:'pwd'/../../tensorflow/object_detection +export MODEL_BASE_PATH='/home/lion/dataset/object_3class/top_full_model' + +python ./eval.py \ + --logtostderr \ + --pipeline_config_path=$MODEL_BASE_PATH/models/model/ssd_inception_v2_3class.config \ + --checkpoint_dir=$MODEL_BASE_PATH/models/model/train \ + --eval_dir=$MODEL_BASE_PATH/models/model/eval diff --git a/stylelens/object_detection/ssd_inception_v2_3class.config b/stylelens/object_detection/ssd_inception_v2_3class.config index ce0ea0b..0ee37d0 100644 --- a/stylelens/object_detection/ssd_inception_v2_3class.config +++ b/stylelens/object_detection/ssd_inception_v2_3class.config @@ -155,7 +155,7 @@ train_config: { # empirically found to be sufficient enough to train the pets dataset. This # effectively bypasses the learning rate schedule (the learning rate will # never decay). Remove the below line to train indefinitely. - num_steps: 200000 + # num_steps: 200000 data_augmentation_options { random_horizontal_flip { } diff --git a/stylelens/object_detection/test.py b/stylelens/object_detection/test.py deleted file mode 100644 index 1a24495..0000000 --- a/stylelens/object_detection/test.py +++ /dev/null @@ -1,37 +0,0 @@ -import tensorflow as tf -import os - - -def read_tfrecord(path): - class_label1 = 0 - class_label2 = 0 - class_label3 = 0 - idx = 0 - for serialized_example in tf.python_io.tf_record_iterator(path): - idx += 1 - example = tf.train.Example() - example.ParseFromString(serialized_example) - filename = example.features.feature["image/filename"].bytes_list.value - label = example.features.feature["image/object/class/label"].int64_list.value - - if(label[0] == 1): - class_label1 += 1 - elif(label[0] == 2): - class_label2 += 1 - elif(label[0] == 3): - class_label3 += 1 - - print("idx: {}, filename: {}, label: {}".format(idx, filename, label)) - - print("label num") - print("label 1: {}, label 2: {}, label 3: {}".format(class_label1, class_label2, class_label3)) - - - -current_path = os.getcwd() -train_input_file = os.path.join(current_path, "train.record") -test_input_file = os.path.join(current_path, "eval.record") - -read_tfrecord(train_input_file) -#read_tfrecord(test_input_file) - diff --git a/stylelens/object_detection/train.sh b/stylelens/object_detection/train.sh index df79884..c231b19 100755 --- a/stylelens/object_detection/train.sh +++ b/stylelens/object_detection/train.sh @@ -1,12 +1,12 @@ #! /bin/bash source activate bl-magi -#export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../../slim export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../../tensorflow/slim:'pwd'/../../tensorflow/object_detection #export MODEL_BASE_PATH='gs://bluelens-style-model/object_detection' -export MODEL_BASE_PATH='/home/lion/dataset/deepfashion3' +export MODEL_BASE_PATH='/home/lion/dataset/object_3class/3class_model' python ./train.py \ --logtostderr \ + --num_clones 7 \ --pipeline_config_path=$MODEL_BASE_PATH/models/model/ssd_inception_v2_3class.config \ - --train_dir=$MODEL_BASE_PATH/models/model/train + --train_dir=$MODEL_BASE_PATH/models/model/train1 diff --git a/stylelens/object_detection/train_full.sh b/stylelens/object_detection/train_full.sh new file mode 100755 index 0000000..eaf047f --- /dev/null +++ b/stylelens/object_detection/train_full.sh @@ -0,0 +1,11 @@ +#! /bin/bash + +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../../tensorflow/slim:'pwd'/../../tensorflow/object_detection +export MODEL_BASE_PATH='/home/lion/dataset/object_3class/full_model' + +python ./train.py \ + --logtostderr \ + --num_clones 7 \ + --pipeline_config_path=$MODEL_BASE_PATH/models/model/ssd_inception_v2_3class.config \ + --train_dir=$MODEL_BASE_PATH/models/model/train1 diff --git a/stylelens/object_detection/train_top.sh b/stylelens/object_detection/train_top.sh new file mode 100755 index 0000000..81d85a5 --- /dev/null +++ b/stylelens/object_detection/train_top.sh @@ -0,0 +1,11 @@ +#! /bin/bash + +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../../tensorflow/slim:'pwd'/../../tensorflow/object_detection +export MODEL_BASE_PATH='/home/lion/dataset/object_3class/top_model' + +python ./train.py \ + --logtostderr \ + --num_clones 7 \ + --pipeline_config_path=$MODEL_BASE_PATH/models/model/ssd_inception_v2_3class.config \ + --train_dir=$MODEL_BASE_PATH/models/model/train2 diff --git a/stylelens/object_detection/train_top_full.sh b/stylelens/object_detection/train_top_full.sh new file mode 100755 index 0000000..31b4319 --- /dev/null +++ b/stylelens/object_detection/train_top_full.sh @@ -0,0 +1,11 @@ +#! /bin/bash + +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../../tensorflow/slim:'pwd'/../../tensorflow/object_detection +export MODEL_BASE_PATH='/home/lion/dataset/object_3class/top_full_model' + +python ./train.py \ + --logtostderr \ + --num_clones 7 \ + --pipeline_config_path=$MODEL_BASE_PATH/models/model/ssd_inception_v2_3class.config \ + --train_dir=$MODEL_BASE_PATH/models/model/train diff --git a/tensorflow/export.sh b/tensorflow/export.sh new file mode 100755 index 0000000..99df03e --- /dev/null +++ b/tensorflow/export.sh @@ -0,0 +1,12 @@ +#! /bin/bash +source activate bl-magi +export PYTHONPATH=$PYTHONPATH:./slim:'pwd'/object_detection +echo $PYTHONPATH +export MODEL_BASE_PATH='/home/lion/dataset/object_3class/top_full_model' +export TRAIN_PATH=$MODEL_BASE_PATH/models/model/train/model.ckpt-$1 + +python object_detection/export_inference_graph.py \ + --input_type image_tensor \ + --pipeline_config_path $MODEL_BASE_PATH/models/model/ssd_inception_v2_3class.config \ + --trained_checkpoint_prefix ${TRAIN_PATH} \ + --output_directory /home/lion/bl-magi/stylelens/object_detection/frozen_graph/top_full_output_inference_graph_$1.pb diff --git a/tensorflow/slim/convert_tf_category.sh b/tensorflow/slim/convert_tf_category.sh new file mode 100755 index 0000000..45c9bdd --- /dev/null +++ b/tensorflow/slim/convert_tf_category.sh @@ -0,0 +1,9 @@ +#source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../slim +export DATASET_PATH=/home/lion/attr_dataset/category_model/data/dataset1 + +echo $PYTHONPATH + +python download_and_convert_data.py \ + --dataset_name=category\ + --dataset_dir=$DATASET_PATH diff --git a/tensorflow/slim/convert_tf_color.sh b/tensorflow/slim/convert_tf_color.sh new file mode 100755 index 0000000..d516692 --- /dev/null +++ b/tensorflow/slim/convert_tf_color.sh @@ -0,0 +1,9 @@ +#source activate bl-magi +export PYTHONPATH=$PYTHONPATH:`pwd`/../../tensorflow:`pwd`/../slim +export DATASET_PATH=/home/lion/attr_dataset/color_model/data/dataset + +echo $PYTHONPATH + +python download_and_convert_data.py \ + --dataset_name=color\ + --dataset_dir=$DATASET_PATH diff --git a/tensorflow/slim/datasets/category.py b/tensorflow/slim/datasets/category.py new file mode 100644 index 0000000..3fb560b --- /dev/null +++ b/tensorflow/slim/datasets/category.py @@ -0,0 +1,98 @@ +# Copyright 2016 The TensorFlow Authors. 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. +# ============================================================================== +"""Provides data for the flowers dataset. + +The dataset scripts used to create the dataset can be found at: +tensorflow/models/research/slim/datasets/download_and_convert_flowers.py +""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import os +import tensorflow as tf + +from datasets import dataset_utils + +slim = tf.contrib.slim + +_FILE_PATTERN = 'category_%s_*.tfrecord' + +SPLITS_TO_SIZES = {'train': 236827, 'validation': 57206} + +_NUM_CLASSES = 14 + +_ITEMS_TO_DESCRIPTIONS = { + 'image': 'A category image of varying size.', + 'label': 'A single integer between 0 and 4', +} + + +def get_split(split_name, dataset_dir, file_pattern=None, reader=None): + """Gets a dataset tuple with instructions for reading flowers. + + Args: + split_name: A train/validation split name. + dataset_dir: The base directory of the dataset sources. + file_pattern: The file pattern to use when matching the dataset sources. + It is assumed that the pattern contains a '%s' string so that the split + name can be inserted. + reader: The TensorFlow reader type. + + Returns: + A `Dataset` namedtuple. + + Raises: + ValueError: if `split_name` is not a valid train/validation split. + """ + if split_name not in SPLITS_TO_SIZES: + raise ValueError('split name %s was not recognized.' % split_name) + + if not file_pattern: + file_pattern = _FILE_PATTERN + file_pattern = os.path.join(dataset_dir, file_pattern % split_name) + + # Allowing None in the signature so that dataset_factory can use the default. + if reader is None: + reader = tf.TFRecordReader + + keys_to_features = { + 'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''), + 'image/format': tf.FixedLenFeature((), tf.string, default_value='png'), + 'image/class/label': tf.FixedLenFeature( + [], tf.int64, default_value=tf.zeros([], dtype=tf.int64)), + } + + items_to_handlers = { + 'image': slim.tfexample_decoder.Image(), + 'label': slim.tfexample_decoder.Tensor('image/class/label'), + } + + decoder = slim.tfexample_decoder.TFExampleDecoder( + keys_to_features, items_to_handlers) + + labels_to_names = None + if dataset_utils.has_labels(dataset_dir): + labels_to_names = dataset_utils.read_label_file(dataset_dir) + + return slim.dataset.Dataset( + data_sources=file_pattern, + reader=reader, + decoder=decoder, + num_samples=SPLITS_TO_SIZES[split_name], + items_to_descriptions=_ITEMS_TO_DESCRIPTIONS, + num_classes=_NUM_CLASSES, + labels_to_names=labels_to_names) diff --git a/tensorflow/slim/datasets/color.py b/tensorflow/slim/datasets/color.py new file mode 100644 index 0000000..c7f2692 --- /dev/null +++ b/tensorflow/slim/datasets/color.py @@ -0,0 +1,98 @@ +# Copyright 2016 The TensorFlow Authors. 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. +# ============================================================================== +"""Provides data for the flowers dataset. + +The dataset scripts used to create the dataset can be found at: +tensorflow/models/research/slim/datasets/download_and_convert_flowers.py +""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import os +import tensorflow as tf + +from datasets import dataset_utils + +slim = tf.contrib.slim + +_FILE_PATTERN = 'color_%s_*.tfrecord' + +SPLITS_TO_SIZES = {'train': 96627, 'validation': 10736} + +_NUM_CLASSES = 12 + +_ITEMS_TO_DESCRIPTIONS = { + 'image': 'A color image of varying size.', + 'label': 'A single integer between 0 and 4', +} + + +def get_split(split_name, dataset_dir, file_pattern=None, reader=None): + """Gets a dataset tuple with instructions for reading flowers. + + Args: + split_name: A train/validation split name. + dataset_dir: The base directory of the dataset sources. + file_pattern: The file pattern to use when matching the dataset sources. + It is assumed that the pattern contains a '%s' string so that the split + name can be inserted. + reader: The TensorFlow reader type. + + Returns: + A `Dataset` namedtuple. + + Raises: + ValueError: if `split_name` is not a valid train/validation split. + """ + if split_name not in SPLITS_TO_SIZES: + raise ValueError('split name %s was not recognized.' % split_name) + + if not file_pattern: + file_pattern = _FILE_PATTERN + file_pattern = os.path.join(dataset_dir, file_pattern % split_name) + + # Allowing None in the signature so that dataset_factory can use the default. + if reader is None: + reader = tf.TFRecordReader + + keys_to_features = { + 'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''), + 'image/format': tf.FixedLenFeature((), tf.string, default_value='png'), + 'image/class/label': tf.FixedLenFeature( + [], tf.int64, default_value=tf.zeros([], dtype=tf.int64)), + } + + items_to_handlers = { + 'image': slim.tfexample_decoder.Image(), + 'label': slim.tfexample_decoder.Tensor('image/class/label'), + } + + decoder = slim.tfexample_decoder.TFExampleDecoder( + keys_to_features, items_to_handlers) + + labels_to_names = None + if dataset_utils.has_labels(dataset_dir): + labels_to_names = dataset_utils.read_label_file(dataset_dir) + + return slim.dataset.Dataset( + data_sources=file_pattern, + reader=reader, + decoder=decoder, + num_samples=SPLITS_TO_SIZES[split_name], + items_to_descriptions=_ITEMS_TO_DESCRIPTIONS, + num_classes=_NUM_CLASSES, + labels_to_names=labels_to_names) diff --git a/tensorflow/slim/datasets/dataset_factory.py b/tensorflow/slim/datasets/dataset_factory.py index 141079a..eed5200 100644 --- a/tensorflow/slim/datasets/dataset_factory.py +++ b/tensorflow/slim/datasets/dataset_factory.py @@ -22,12 +22,22 @@ from datasets import flowers from datasets import imagenet from datasets import mnist +#---------add--------------- +from datasets import fabric +from datasets import color +from datasets import category +#-------------------------- datasets_map = { 'cifar10': cifar10, 'flowers': flowers, 'imagenet': imagenet, 'mnist': mnist, + #-------add-------- + 'fabric': fabric, + 'color': color, + 'category': category, + #----------------- } diff --git a/tensorflow/slim/datasets/download_and_convert_category.py b/tensorflow/slim/datasets/download_and_convert_category.py new file mode 100644 index 0000000..1d00331 --- /dev/null +++ b/tensorflow/slim/datasets/download_and_convert_category.py @@ -0,0 +1,205 @@ +# Copyright 2016 The TensorFlow Authors. 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"""Downloads and converts Flowers data to TFRecords of TF-Example protos. + +This module downloads the Flowers data, uncompresses it, reads the files +that make up the Flowers data and creates two TFRecord datasets: one for train +and one for test. Each TFRecord dataset is comprised of a set of TF-Example +protocol buffers, each of which contain a single image and label. + +The script should take about a minute to run. + +""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import math +import os +import random +import sys + +import tensorflow as tf + +from datasets import dataset_utils + + +# The number of images in the validation set. +_NUM_VALIDATION = 57206 + +# Seed for repeatability. +_RANDOM_SEED = 12 + +# The number of shards per dataset split. +_NUM_SHARDS = 225 + + +class ImageReader(object): + """Helper class that provides TensorFlow image coding utilities.""" + + def __init__(self): + # Initializes function that decodes RGB JPEG data. + self._decode_jpeg_data = tf.placeholder(dtype=tf.string) + self._decode_jpeg = tf.image.decode_jpeg(self._decode_jpeg_data, channels=3) + + def read_image_dims(self, sess, image_data): + image = self.decode_jpeg(sess, image_data) + return image.shape[0], image.shape[1] + + def decode_jpeg(self, sess, image_data): + image = sess.run(self._decode_jpeg, + feed_dict={self._decode_jpeg_data: image_data}) + assert len(image.shape) == 3 + assert image.shape[2] == 3 + return image + + +def _get_filenames_and_classes(dataset_dir): + """Returns a list of filenames and inferred class names. + + Args: + dataset_dir: A directory containing a set of subdirectories representing + class names. Each subdirectory should contain PNG or JPG encoded images. + + Returns: + A list of image file paths, relative to `dataset_dir` and the list of + subdirectories, representing class names. + """ + category_root = os.path.join(dataset_dir, 'images') + directories = [] + class_names = [] + + for filename in os.listdir(category_root): + path = os.path.join(category_root, 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, sorted(class_names) + + +def _get_dataset_filename(dataset_dir, split_name, shard_id): + output_filename = 'category_%s_%05d-of-%05d.tfrecord' % ( + split_name, shard_id, _NUM_SHARDS) + return os.path.join(dataset_dir, output_filename) + + +def _convert_dataset(split_name, filenames, class_names_to_ids, dataset_dir): + """Converts the given filenames to a TFRecord dataset. + + Args: + split_name: The name of the dataset, either 'train' or 'validation'. + filenames: A list of absolute paths to png or jpg images. + class_names_to_ids: A dictionary from class names (strings) to ids + (integers). + dataset_dir: The directory where the converted datasets are stored. + """ + assert split_name in ['train', 'validation'] + + num_per_shard = int(math.ceil(len(filenames) / float(_NUM_SHARDS))) + + with tf.Graph().as_default(): + image_reader = ImageReader() + + with tf.Session('') as sess: + + for shard_id in range(_NUM_SHARDS): + 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): + sys.stdout.write('\r>> Converting image %d/%d shard %d' % ( + i+1, len(filenames), shard_id)) + sys.stdout.flush() + + # Read the filename: + image_data = tf.gfile.FastGFile(filenames[i], 'rb').read() + height, width = image_reader.read_image_dims(sess, image_data) + class_name = os.path.basename(os.path.dirname(filenames[i])) + class_id = class_names_to_ids[class_name] + example = dataset_utils.image_to_tfexample(image_data, b'jpg', height, width, class_id) + tfrecord_writer.write(example.SerializeToString()) + + sys.stdout.write('\n') + sys.stdout.flush() + + +def _clean_up_temporary_files(dataset_dir): + """Removes temporary files used to create the dataset. + + Args: + dataset_dir: The directory where the temporary files are stored. + """ + filename = _DATA_URL.split('/')[-1] + filepath = os.path.join(dataset_dir, filename) + tf.gfile.Remove(filepath) + + #tmp_dir = os.path.join(dataset_dir, 'images') + tf.gfile.DeleteRecursively(tmp_dir) + + +def _dataset_exists(dataset_dir): + for split_name in ['train', 'validation']: + for shard_id in range(_NUM_SHARDS): + output_filename = _get_dataset_filename( + dataset_dir, split_name, shard_id) + if not tf.gfile.Exists(output_filename): + return False + return True + + +def run(dataset_dir): + """Runs the download and conversion operation. + + Args: + dataset_dir: The dataset directory where the dataset is stored. + """ + if not tf.gfile.Exists(dataset_dir): + tf.gfile.MakeDirs(dataset_dir) + + if _dataset_exists(dataset_dir): + print('Dataset files already exist. Exiting without re-creating them.') + return + + #dataset_utils.download_and_uncompress_tarball(_DATA_URL, dataset_dir) + photo_filenames, class_names = _get_filenames_and_classes(dataset_dir) + class_names_to_ids = dict(zip(class_names, range(len(class_names)))) + + # Divide into train and test: + random.seed(_RANDOM_SEED) + random.shuffle(photo_filenames) + training_filenames = photo_filenames[_NUM_VALIDATION:] + validation_filenames = photo_filenames[:_NUM_VALIDATION] + + # First, convert the training and validation sets. + _convert_dataset('train', training_filenames, class_names_to_ids, dataset_dir) + _convert_dataset('validation', validation_filenames, class_names_to_ids, dataset_dir) + + # Finally, write the labels file: + labels_to_class_names = dict(zip(range(len(class_names)), class_names)) + dataset_utils.write_label_file(labels_to_class_names, dataset_dir) + + #_clean_up_temporary_files(dataset_dir) + print('\nFinished converting the Categories dataset!') diff --git a/tensorflow/slim/datasets/download_and_convert_color.py b/tensorflow/slim/datasets/download_and_convert_color.py new file mode 100644 index 0000000..7eee1b6 --- /dev/null +++ b/tensorflow/slim/datasets/download_and_convert_color.py @@ -0,0 +1,205 @@ +# Copyright 2016 The TensorFlow Authors. 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"""Downloads and converts Flowers data to TFRecords of TF-Example protos. + +This module downloads the Flowers data, uncompresses it, reads the files +that make up the Flowers data and creates two TFRecord datasets: one for train +and one for test. Each TFRecord dataset is comprised of a set of TF-Example +protocol buffers, each of which contain a single image and label. + +The script should take about a minute to run. + +""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import math +import os +import random +import sys + +import tensorflow as tf + +from datasets import dataset_utils + + +# The number of images in the validation set. +_NUM_VALIDATION = 10736 + +# Seed for repeatability. +_RANDOM_SEED = 5 + +# The number of shards per dataset split. +_NUM_SHARDS = 95 + + +class ImageReader(object): + """Helper class that provides TensorFlow image coding utilities.""" + + def __init__(self): + # Initializes function that decodes RGB JPEG data. + self._decode_jpeg_data = tf.placeholder(dtype=tf.string) + self._decode_jpeg = tf.image.decode_jpeg(self._decode_jpeg_data, channels=3) + + def read_image_dims(self, sess, image_data): + image = self.decode_jpeg(sess, image_data) + return image.shape[0], image.shape[1] + + def decode_jpeg(self, sess, image_data): + image = sess.run(self._decode_jpeg, + feed_dict={self._decode_jpeg_data: image_data}) + assert len(image.shape) == 3 + assert image.shape[2] == 3 + return image + + +def _get_filenames_and_classes(dataset_dir): + """Returns a list of filenames and inferred class names. + + Args: + dataset_dir: A directory containing a set of subdirectories representing + class names. Each subdirectory should contain PNG or JPG encoded images. + + Returns: + A list of image file paths, relative to `dataset_dir` and the list of + subdirectories, representing class names. + """ + color_root = os.path.join(dataset_dir, 'images') + directories = [] + class_names = [] + + for filename in os.listdir(color_root): + path = os.path.join(color_root, 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, sorted(class_names) + + +def _get_dataset_filename(dataset_dir, split_name, shard_id): + output_filename = 'color_%s_%05d-of-%05d.tfrecord' % ( + split_name, shard_id, _NUM_SHARDS) + return os.path.join(dataset_dir, output_filename) + + +def _convert_dataset(split_name, filenames, class_names_to_ids, dataset_dir): + """Converts the given filenames to a TFRecord dataset. + + Args: + split_name: The name of the dataset, either 'train' or 'validation'. + filenames: A list of absolute paths to png or jpg images. + class_names_to_ids: A dictionary from class names (strings) to ids + (integers). + dataset_dir: The directory where the converted datasets are stored. + """ + assert split_name in ['train', 'validation'] + + num_per_shard = int(math.ceil(len(filenames) / float(_NUM_SHARDS))) + + with tf.Graph().as_default(): + image_reader = ImageReader() + + with tf.Session('') as sess: + + for shard_id in range(_NUM_SHARDS): + 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): + sys.stdout.write('\r>> Converting image %d/%d shard %d' % ( + i+1, len(filenames), shard_id)) + sys.stdout.flush() + + # Read the filename: + image_data = tf.gfile.FastGFile(filenames[i], 'rb').read() + height, width = image_reader.read_image_dims(sess, image_data) + class_name = os.path.basename(os.path.dirname(filenames[i])) + class_id = class_names_to_ids[class_name] + example = dataset_utils.image_to_tfexample(image_data, b'jpg', height, width, class_id) + tfrecord_writer.write(example.SerializeToString()) + + sys.stdout.write('\n') + sys.stdout.flush() + + +def _clean_up_temporary_files(dataset_dir): + """Removes temporary files used to create the dataset. + + Args: + dataset_dir: The directory where the temporary files are stored. + """ + filename = _DATA_URL.split('/')[-1] + filepath = os.path.join(dataset_dir, filename) + tf.gfile.Remove(filepath) + + #tmp_dir = os.path.join(dataset_dir, 'images') + tf.gfile.DeleteRecursively(tmp_dir) + + +def _dataset_exists(dataset_dir): + for split_name in ['train', 'validation']: + for shard_id in range(_NUM_SHARDS): + output_filename = _get_dataset_filename( + dataset_dir, split_name, shard_id) + if not tf.gfile.Exists(output_filename): + return False + return True + + +def run(dataset_dir): + """Runs the download and conversion operation. + + Args: + dataset_dir: The dataset directory where the dataset is stored. + """ + if not tf.gfile.Exists(dataset_dir): + tf.gfile.MakeDirs(dataset_dir) + + if _dataset_exists(dataset_dir): + print('Dataset files already exist. Exiting without re-creating them.') + return + + #dataset_utils.download_and_uncompress_tarball(_DATA_URL, dataset_dir) + photo_filenames, class_names = _get_filenames_and_classes(dataset_dir) + class_names_to_ids = dict(zip(class_names, range(len(class_names)))) + + # Divide into train and test: + random.seed(_RANDOM_SEED) + random.shuffle(photo_filenames) + training_filenames = photo_filenames[_NUM_VALIDATION:] + validation_filenames = photo_filenames[:_NUM_VALIDATION] + + # First, convert the training and validation sets. + _convert_dataset('train', training_filenames, class_names_to_ids, dataset_dir) + _convert_dataset('validation', validation_filenames, class_names_to_ids, dataset_dir) + + # Finally, write the labels file: + labels_to_class_names = dict(zip(range(len(class_names)), class_names)) + dataset_utils.write_label_file(labels_to_class_names, dataset_dir) + + #_clean_up_temporary_files(dataset_dir) + print('\nFinished converting the Colors dataset!') diff --git a/tensorflow/slim/datasets/download_and_convert_fabric.py b/tensorflow/slim/datasets/download_and_convert_fabric.py new file mode 100644 index 0000000..89c37b9 --- /dev/null +++ b/tensorflow/slim/datasets/download_and_convert_fabric.py @@ -0,0 +1,212 @@ +# Copyright 2016 The TensorFlow Authors. 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"""Downloads and converts Flowers data to TFRecords of TF-Example protos. + +This module downloads the Flowers data, uncompresses it, reads the files +that make up the Flowers data and creates two TFRecord datasets: one for train +and one for test. Each TFRecord dataset is comprised of a set of TF-Example +protocol buffers, each of which contain a single image and label. + +The script should take about a minute to run. + +""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import math +import os +import random +import sys + +import tensorflow as tf + +from datasets import dataset_utils + +# The URL where the Flowers data can be downloaded. +#_DATA_URL = 'http://download.tensorflow.org/example_images/flower_photos.tgz' + +# The number of images in the validation set. +_NUM_VALIDATION = 35948 + +# Seed for repeatability. +_RANDOM_SEED = 0 + +# The number of shards per dataset split. +_NUM_SHARDS = 5 + + +class ImageReader(object): + """Helper class that provides TensorFlow image coding utilities.""" + + def __init__(self): + # Initializes function that decodes RGB JPEG data. + self._decode_jpeg_data = tf.placeholder(dtype=tf.string) + self._decode_jpeg = tf.image.decode_jpeg(self._decode_jpeg_data, channels=3) + + def read_image_dims(self, sess, image_data): + image = self.decode_jpeg(sess, image_data) + return image.shape[0], image.shape[1] + + def decode_jpeg(self, sess, image_data): + image = sess.run(self._decode_jpeg, + feed_dict={self._decode_jpeg_data: image_data}) + assert len(image.shape) == 3 + assert image.shape[2] == 3 + return image + + +def _get_filenames_and_classes(dataset_dir): + """Returns a list of filenames and inferred class names. + + Args: + dataset_dir: A directory containing a set of subdirectories representing + class names. Each subdirectory should contain PNG or JPG encoded images. + + Returns: + A list of image file paths, relative to `dataset_dir` and the list of + subdirectories, representing class names. + """ + fabric_root = os.path.join(dataset_dir, 'images') + directories = [] + class_names = [] + + for filename in os.listdir(fabric_root): + path = os.path.join(fabric_root, 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, sorted(class_names) + + +def _get_dataset_filename(dataset_dir, split_name, shard_id): + output_filename = 'fabric_%s_%05d-of-%05d.tfrecord' % ( + split_name, shard_id, _NUM_SHARDS) + return os.path.join(dataset_dir, output_filename) + + +def _convert_dataset(split_name, filenames, class_names_to_ids, dataset_dir): + """Converts the given filenames to a TFRecord dataset. + + Args: + split_name: The name of the dataset, either 'train' or 'validation'. + filenames: A list of absolute paths to png or jpg images. + class_names_to_ids: A dictionary from class names (strings) to ids + (integers). + dataset_dir: The directory where the converted datasets are stored. + """ + assert split_name in ['train', 'validation'] + + num_per_shard = int(math.ceil(len(filenames) / float(_NUM_SHARDS))) + + with tf.Graph().as_default(): + image_reader = ImageReader() + + with tf.Session('') as sess: + + for shard_id in range(_NUM_SHARDS): + 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): + sys.stdout.write('\r>> Converting image %d/%d shard %d' % ( + i+1, len(filenames), shard_id)) + sys.stdout.flush() + + # Read the filename: + image_data = tf.gfile.FastGFile(filenames[i], 'rb').read() + height, width = image_reader.read_image_dims(sess, image_data) + + class_name = os.path.basename(os.path.dirname(filenames[i])) + class_id = class_names_to_ids[class_name] + + example = dataset_utils.image_to_tfexample( + image_data, b'jpg', height, width, class_id) + tfrecord_writer.write(example.SerializeToString()) + + sys.stdout.write('\n') + sys.stdout.flush() + + +def _clean_up_temporary_files(dataset_dir): + """Removes temporary files used to create the dataset. + + Args: + dataset_dir: The directory where the temporary files are stored. + """ + filename = _DATA_URL.split('/')[-1] + filepath = os.path.join(dataset_dir, filename) + tf.gfile.Remove(filepath) + + tmp_dir = os.path.join(dataset_dir, 'images') + tf.gfile.DeleteRecursively(tmp_dir) + + +def _dataset_exists(dataset_dir): + for split_name in ['train', 'validation']: + for shard_id in range(_NUM_SHARDS): + output_filename = _get_dataset_filename( + dataset_dir, split_name, shard_id) + if not tf.gfile.Exists(output_filename): + return False + return True + + +def run(dataset_dir): + """Runs the download and conversion operation. + + Args: + dataset_dir: The dataset directory where the dataset is stored. + """ + if not tf.gfile.Exists(dataset_dir): + tf.gfile.MakeDirs(dataset_dir) + + if _dataset_exists(dataset_dir): + print('Dataset files already exist. Exiting without re-creating them.') + return + + #dataset_utils.download_and_uncompress_tarball(_DATA_URL, dataset_dir) + photo_filenames, class_names = _get_filenames_and_classes(dataset_dir) + class_names_to_ids = dict(zip(class_names, range(len(class_names)))) + + # Divide into train and test: + random.seed(_RANDOM_SEED) + random.shuffle(photo_filenames) + training_filenames = photo_filenames[_NUM_VALIDATION:] + validation_filenames = photo_filenames[:_NUM_VALIDATION] + + # First, convert the training and validation sets. + _convert_dataset('train', training_filenames, class_names_to_ids, + dataset_dir) + _convert_dataset('validation', validation_filenames, class_names_to_ids, + dataset_dir) + + # Finally, write the labels file: + labels_to_class_names = dict(zip(range(len(class_names)), class_names)) + dataset_utils.write_label_file(labels_to_class_names, dataset_dir) + + #_clean_up_temporary_files(dataset_dir) + print('\nFinished converting the Flowers dataset!') diff --git a/tensorflow/slim/datasets/fabric.py b/tensorflow/slim/datasets/fabric.py new file mode 100644 index 0000000..c9f3ae2 --- /dev/null +++ b/tensorflow/slim/datasets/fabric.py @@ -0,0 +1,98 @@ +# Copyright 2016 The TensorFlow Authors. 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. +# ============================================================================== +"""Provides data for the flowers dataset. + +The dataset scripts used to create the dataset can be found at: +tensorflow/models/research/slim/datasets/download_and_convert_flowers.py +""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import os +import tensorflow as tf + +from datasets import dataset_utils + +slim = tf.contrib.slim + +_FILE_PATTERN = 'fabric_%s_*.tfrecord' + +SPLITS_TO_SIZES = {'train': 143362, 'validation': 35948} + +_NUM_CLASSES = 218 + +_ITEMS_TO_DESCRIPTIONS = { + 'image': 'A color image of varying size.', + 'label': 'A single integer between 0 and 4', +} + + +def get_split(split_name, dataset_dir, file_pattern=None, reader=None): + """Gets a dataset tuple with instructions for reading flowers. + + Args: + split_name: A train/validation split name. + dataset_dir: The base directory of the dataset sources. + file_pattern: The file pattern to use when matching the dataset sources. + It is assumed that the pattern contains a '%s' string so that the split + name can be inserted. + reader: The TensorFlow reader type. + + Returns: + A `Dataset` namedtuple. + + Raises: + ValueError: if `split_name` is not a valid train/validation split. + """ + if split_name not in SPLITS_TO_SIZES: + raise ValueError('split name %s was not recognized.' % split_name) + + if not file_pattern: + file_pattern = _FILE_PATTERN + file_pattern = os.path.join(dataset_dir, file_pattern % split_name) + + # Allowing None in the signature so that dataset_factory can use the default. + if reader is None: + reader = tf.TFRecordReader + + keys_to_features = { + 'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''), + 'image/format': tf.FixedLenFeature((), tf.string, default_value='png'), + 'image/class/label': tf.FixedLenFeature( + [], tf.int64, default_value=tf.zeros([], dtype=tf.int64)), + } + + items_to_handlers = { + 'image': slim.tfexample_decoder.Image(), + 'label': slim.tfexample_decoder.Tensor('image/class/label'), + } + + decoder = slim.tfexample_decoder.TFExampleDecoder( + keys_to_features, items_to_handlers) + + labels_to_names = None + if dataset_utils.has_labels(dataset_dir): + labels_to_names = dataset_utils.read_label_file(dataset_dir) + + return slim.dataset.Dataset( + data_sources=file_pattern, + reader=reader, + decoder=decoder, + num_samples=SPLITS_TO_SIZES[split_name], + items_to_descriptions=_ITEMS_TO_DESCRIPTIONS, + num_classes=_NUM_CLASSES, + labels_to_names=labels_to_names) diff --git a/tensorflow/slim/download_and_convert_data.py b/tensorflow/slim/download_and_convert_data.py index 924a3a4..aeebd89 100644 --- a/tensorflow/slim/download_and_convert_data.py +++ b/tensorflow/slim/download_and_convert_data.py @@ -39,6 +39,11 @@ from datasets import download_and_convert_cifar10 from datasets import download_and_convert_flowers from datasets import download_and_convert_mnist +#-------------------add-------------------------- +from datasets import download_and_convert_fabric +from datasets import download_and_convert_color +from datasets import download_and_convert_category +#----------------------------------------------- FLAGS = tf.app.flags.FLAGS @@ -65,6 +70,14 @@ def main(_): download_and_convert_flowers.run(FLAGS.dataset_dir) elif FLAGS.dataset_name == 'mnist': download_and_convert_mnist.run(FLAGS.dataset_dir) + #-------------------add------------------------- + elif FLAGS.dataset_name == 'fabric': + download_and_convert_fabric.run(FLAGS.dataset_dir) + elif FLAGS.dataset_name == 'color': + download_and_convert_color.run(FLAGS.dataset_dir) + elif FLAGS.dataset_name == 'category': + download_and_convert_category.run(FLAGS.dataset_dir) + #----------------------------------------------- else: raise ValueError( 'dataset_name [%s] was not recognized.' % FLAGS.dataset_name) diff --git a/tensorflow/slim/eval_image_classifier.py b/tensorflow/slim/eval_image_classifier.py index 82d10d9..82add5e 100644 --- a/tensorflow/slim/eval_image_classifier.py +++ b/tensorflow/slim/eval_image_classifier.py @@ -20,15 +20,17 @@ import math import tensorflow as tf +import os from datasets import dataset_factory from nets import nets_factory from preprocessing import preprocessing_factory +os.environ['CUDA_VISIBLE_DEVICES'] = '-1' slim = tf.contrib.slim tf.app.flags.DEFINE_integer( - 'batch_size', 100, 'The number of samples in each batch.') + 'batch_size', 42, 'The number of samples in each batch.') tf.app.flags.DEFINE_integer( 'max_num_batches', None, @@ -38,25 +40,25 @@ 'master', '', 'The address of the TensorFlow master to use.') tf.app.flags.DEFINE_string( - 'checkpoint_path', '/tmp/tfmodel/', + 'checkpoint_path', '', 'The directory where the model was written to or an absolute path to a ' 'checkpoint file.') tf.app.flags.DEFINE_string( - 'eval_dir', '/tmp/tfmodel/', 'Directory where the results are saved to.') + 'eval_dir', '', 'Directory where the results are saved to.') tf.app.flags.DEFINE_integer( 'num_preprocessing_threads', 4, 'The number of threads used to create the batches.') tf.app.flags.DEFINE_string( - 'dataset_name', 'imagenet', 'The name of the dataset to load.') + 'dataset_name', '', 'The name of the dataset to load.') tf.app.flags.DEFINE_string( - 'dataset_split_name', 'test', 'The name of the train/test split.') + 'dataset_split_name', '', 'The name of the train/test split.') tf.app.flags.DEFINE_string( - 'dataset_dir', None, 'The directory where the dataset files are stored.') + 'dataset_dir', '', 'The directory where the dataset files are stored.') tf.app.flags.DEFINE_integer( 'labels_offset', 0, @@ -150,6 +152,11 @@ def main(_): predictions = tf.argmax(logits, 1) labels = tf.squeeze(labels) + print("---------------------------") + print(predictions) + print(labels) + print("---------------------------") + # Define the metrics: names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({ 'Accuracy': slim.metrics.streaming_accuracy(predictions, labels), diff --git a/tensorflow/slim/export_inference_graph.py b/tensorflow/slim/export_inference_graph.py index 56f28a5..a3a7fc6 100644 --- a/tensorflow/slim/export_inference_graph.py +++ b/tensorflow/slim/export_inference_graph.py @@ -81,7 +81,7 @@ def with the variables inlined as constants using: 'Batch size for the exported model. Defaulted to "None" so batch size can ' 'be specified at model runtime.') -tf.app.flags.DEFINE_string('dataset_name', 'imagenet', +tf.app.flags.DEFINE_string('dataset_name', 'category', 'The name of the dataset to use with the model.') tf.app.flags.DEFINE_integer( diff --git a/tensorflow/slim/train_image_classifier.py b/tensorflow/slim/train_image_classifier.py index bd7280d..57d8b1b 100644 --- a/tensorflow/slim/train_image_classifier.py +++ b/tensorflow/slim/train_image_classifier.py @@ -34,7 +34,7 @@ 'train_dir', '/tmp/tfmodel/', 'Directory where checkpoints and event logs are written to.') -tf.app.flags.DEFINE_integer('num_clones', 1, +tf.app.flags.DEFINE_integer('num_clones', 7, 'Number of model clones to deploy.') tf.app.flags.DEFINE_boolean('clone_on_cpu', False, @@ -144,7 +144,7 @@ 'learning_rate_decay_factor', 0.94, 'Learning rate decay factor.') tf.app.flags.DEFINE_float( - 'num_epochs_per_decay', 2.0, + 'num_epochs_per_decay', 10.0, 'Number of epochs after which learning rate decays.') tf.app.flags.DEFINE_bool( @@ -165,13 +165,13 @@ ####################### tf.app.flags.DEFINE_string( - 'dataset_name', 'imagenet', 'The name of the dataset to load.') + 'dataset_name', '', 'The name of the dataset to load.') tf.app.flags.DEFINE_string( 'dataset_split_name', 'train', 'The name of the train/test split.') tf.app.flags.DEFINE_string( - 'dataset_dir', None, 'The directory where the dataset files are stored.') + 'dataset_dir', '', 'The directory where the dataset files are stored.') tf.app.flags.DEFINE_integer( 'labels_offset', 0, @@ -187,12 +187,12 @@ 'as `None`, then the model_name flag is used.') tf.app.flags.DEFINE_integer( - 'batch_size', 32, 'The number of samples in each batch.') + 'batch_size', 192, 'The number of samples in each batch.') tf.app.flags.DEFINE_integer( 'train_image_size', None, 'Train image size') -tf.app.flags.DEFINE_integer('max_number_of_steps', None, +tf.app.flags.DEFINE_integer('max_number_of_steps', '10000000', 'The maximum number of training steps.') ##################### @@ -552,6 +552,9 @@ def clone_fn(batch_queue): # Merge all summaries together. summary_op = tf.summary.merge(list(summaries), name='summary_op') + session_config = tf.ConfigProto(allow_soft_placement = True, + log_device_placement = False) + session_config.gpu_options.allow_growth = True ########################### # Kicks off the training. # @@ -561,6 +564,7 @@ def clone_fn(batch_queue): logdir=FLAGS.train_dir, master=FLAGS.master, is_chief=(FLAGS.task == 0), + session_config = session_config, init_fn=_get_init_fn(), summary_op=summary_op, number_of_steps=FLAGS.max_number_of_steps,