diff --git a/hub/examples/image_retraining/label_image.py b/hub/examples/image_retraining/label_image.py index 889314b1..c4799384 100644 --- a/hub/examples/image_retraining/label_image.py +++ b/hub/examples/image_retraining/label_image.py @@ -1,10 +1,13 @@ -import tensorflow as tf, sys +import tensorflow as tf +import sys from subprocess import Popen import os import wikipedia from yaml import load, SafeLoader # returns celestial_object and labels_and_scores + + def get_labels(image_data, cwd): # Loads label file, strips off carriage return label_lines = [ @@ -20,19 +23,22 @@ def get_labels(image_data, cwd): # Feed the image_data as input to the graph and get first prediction with tf.compat.v1.Session() as sess: softmax_tensor = sess.graph.get_tensor_by_name("final_result:0") - predictions = sess.run(softmax_tensor, {"DecodeJpeg/contents:0": image_data}) + predictions = sess.run( + softmax_tensor, {"DecodeJpeg/contents:0": image_data}) # Sort to show labels of first prediction in order of confidence - top_k = predictions[0].argsort()[-len(predictions[0]) :][::-1] + top_k = predictions[0].argsort()[-len(predictions[0]):][::-1] labels_and_scores = [ (label_lines[node_id], predictions[0][node_id]) for node_id in top_k ] - + # Get the predicted celestial object after classification celestial_object = label_lines[top_k[0]] return celestial_object, labels_and_scores # return title, statistics and summary -def wiki(celestial_object,cwd): + + +def wiki(celestial_object, cwd): ans = celestial_object with open(os.path.join(cwd, "display_info.yml"), "r") as stream: all_display_statistics = load(stream, Loader=SafeLoader) @@ -42,8 +48,10 @@ def wiki(celestial_object,cwd): title = None summary = None if ans in ["spiral", "elliptical"]: - title = ("Classified Celestial Object is {} Galaxy : ".format(ans.capitalize())) - summary = (wikipedia.WikipediaPage(title="{} galaxy".format(ans)).summary) + title = ("Classified Celestial Object is {} Galaxy : ".format( + ans.capitalize())) + summary = (wikipedia.WikipediaPage( + title="{} galaxy".format(ans)).summary) elif ans in [ "mercury", "venus", @@ -53,15 +61,19 @@ def wiki(celestial_object,cwd): "saturn", "uranus", "neptune", + "pluto" ]: - title = ("Classified Celestial Object is {} Planet : ".format(ans.capitalize())) + title = ("Classified Celestial Object is {} Planet : ".format( + ans.capitalize())) statistics = req_statistics.items() - summary = (wikipedia.WikipediaPage(title="{} (planet)".format(ans)).summary) + summary = (wikipedia.WikipediaPage( + title="{} (planet)".format(ans)).summary) elif ans == "moon": statistics = req_statistics.items() summary = (wikipedia.WikipediaPage(title="{}".format(ans)).summary) - title = ("Classified Celestial Object is the {} : ".format(ans.capitalize())) - return title,statistics,summary + title = ("Classified Celestial Object is the {} : ".format( + ans.capitalize())) + return title, statistics, summary if __name__ == "__main__": @@ -83,7 +95,7 @@ def wiki(celestial_object,cwd): # Summary # Popen(["python", "wiki.py"]) - title,statistics,summary = wiki(celestial_object,os.getcwd()) + title, statistics, summary = wiki(celestial_object, os.getcwd()) print("--------------------------------------------------------") print(title) print("-------------------------------------------------------- \n") diff --git a/hub/examples/image_retraining/report.txt b/hub/examples/image_retraining/report.txt new file mode 100644 index 00000000..14635c0a --- /dev/null +++ b/hub/examples/image_retraining/report.txt @@ -0,0 +1,112 @@ +TensorFlow 2.0 Upgrade Script +----------------------------- +Converted 1 files +Detected 0 issues that require attention +-------------------------------------------------------------------------------- +================================================================================ +Detailed log follows: + +================================================================================ +-------------------------------------------------------------------------------- +Processing file 'retrain.py' + outputting to 'retrain.py' +-------------------------------------------------------------------------------- + +205:4: INFO: Renamed 'tf.logging.fatal' to 'tf.compat.v1.logging.fatal' +208:4: INFO: Renamed 'tf.logging.fatal' to 'tf.compat.v1.logging.fatal' +211:4: INFO: Renamed 'tf.logging.fatal' to 'tf.compat.v1.logging.fatal' +247:7: INFO: Renamed 'tf.Session' to 'tf.compat.v1.Session' +251:18: INFO: Renamed 'tf.GraphDef' to 'tf.compat.v1.GraphDef' +354:4: INFO: Renamed 'tf.logging.fatal' to 'tf.compat.v1.logging.fatal' +550:6: INFO: Renamed 'tf.logging.fatal' to 'tf.compat.v1.logging.fatal' +639:14: INFO: Renamed 'tf.placeholder' to 'tf.compat.v1.placeholder' +646:23: INFO: Renamed 'tf.random_uniform' to 'tf.random.uniform' +654:21: INFO: Changed tf.image.resize_bilinear call to tf.image.resize(..., method=tf.image.ResizeMethod.BILINEAR). +656:24: INFO: Renamed keyword argument for tf.squeeze from squeeze_dims to axis +657:18: INFO: Renamed 'tf.random_crop' to 'tf.image.random_crop' +666:21: INFO: Renamed 'tf.random_uniform' to 'tf.random.uniform' +676:7: INFO: `name` passed to `name_scope`. Because you may be re-entering an existing scope, it is not safe to convert automatically, the v2 name_scope does not support re-entering scopes by name. + +676:7: INFO: Renamed 'tf.name_scope' to 'tf.compat.v1.name_scope' +677:11: INFO: Added keywords to args of function 'tf.reduce_mean' +678:4: INFO: tf.summary.scalar requires manual check. The TF 1.x summary API cannot be automatically migrated to TF 2.0, so symbols have been converted to tf.compat.v1.summary.* and must be migrated manually. Typical usage will only require changes to the summary writing logic, not to individual calls like scalar(). For examples of the new summary API, see the Effective TF 2.0 migration document or check the TF 2.0 TensorBoard tutorials. +678:4: INFO: Renamed 'tf.summary.scalar' to 'tf.compat.v1.summary.scalar' +679:9: INFO: `name` passed to `name_scope`. Because you may be re-entering an existing scope, it is not safe to convert automatically, the v2 name_scope does not support re-entering scopes by name. + +679:9: INFO: Renamed 'tf.name_scope' to 'tf.compat.v1.name_scope' +680:23: INFO: Added keywords to args of function 'tf.reduce_mean' +681:4: INFO: tf.summary.scalar requires manual check. The TF 1.x summary API cannot be automatically migrated to TF 2.0, so symbols have been converted to tf.compat.v1.summary.* and must be migrated manually. Typical usage will only require changes to the summary writing logic, not to individual calls like scalar(). For examples of the new summary API, see the Effective TF 2.0 migration document or check the TF 2.0 TensorBoard tutorials. +681:4: INFO: Renamed 'tf.summary.scalar' to 'tf.compat.v1.summary.scalar' +682:4: INFO: tf.summary.scalar requires manual check. The TF 1.x summary API cannot be automatically migrated to TF 2.0, so symbols have been converted to tf.compat.v1.summary.* and must be migrated manually. Typical usage will only require changes to the summary writing logic, not to individual calls like scalar(). For examples of the new summary API, see the Effective TF 2.0 migration document or check the TF 2.0 TensorBoard tutorials. +682:4: INFO: Renamed 'tf.summary.scalar' to 'tf.compat.v1.summary.scalar' +682:29: INFO: Added keywords to args of function 'tf.reduce_max' +683:4: INFO: tf.summary.scalar requires manual check. The TF 1.x summary API cannot be automatically migrated to TF 2.0, so symbols have been converted to tf.compat.v1.summary.* and must be migrated manually. Typical usage will only require changes to the summary writing logic, not to individual calls like scalar(). For examples of the new summary API, see the Effective TF 2.0 migration document or check the TF 2.0 TensorBoard tutorials. +683:4: INFO: Renamed 'tf.summary.scalar' to 'tf.compat.v1.summary.scalar' +683:29: INFO: Added keywords to args of function 'tf.reduce_min' +684:4: INFO: tf.summary.histogram requires manual check. The TF 1.x summary API cannot be automatically migrated to TF 2.0, so symbols have been converted to tf.compat.v1.summary.* and must be migrated manually. Typical usage will only require changes to the summary writing logic, not to individual calls like scalar(). For examples of the new summary API, see the Effective TF 2.0 migration document or check the TF 2.0 TensorBoard tutorials. +684:4: INFO: Renamed 'tf.summary.histogram' to 'tf.compat.v1.summary.histogram' +707:7: INFO: `name` passed to `name_scope`. Because you may be re-entering an existing scope, it is not safe to convert automatically, the v2 name_scope does not support re-entering scopes by name. + +707:7: INFO: Renamed 'tf.name_scope' to 'tf.compat.v1.name_scope' +708:23: INFO: Renamed 'tf.placeholder_with_default' to 'tf.compat.v1.placeholder_with_default' +712:25: INFO: Renamed 'tf.placeholder' to 'tf.compat.v1.placeholder' +719:7: INFO: `name` passed to `name_scope`. Because you may be re-entering an existing scope, it is not safe to convert automatically, the v2 name_scope does not support re-entering scopes by name. + +719:7: INFO: Renamed 'tf.name_scope' to 'tf.compat.v1.name_scope' +720:9: INFO: `name` passed to `name_scope`. Because you may be re-entering an existing scope, it is not safe to convert automatically, the v2 name_scope does not support re-entering scopes by name. + +720:9: INFO: Renamed 'tf.name_scope' to 'tf.compat.v1.name_scope' +721:34: INFO: Renamed 'tf.truncated_normal' to 'tf.random.truncated_normal' +723:9: INFO: `name` passed to `name_scope`. Because you may be re-entering an existing scope, it is not safe to convert automatically, the v2 name_scope does not support re-entering scopes by name. + +723:9: INFO: Renamed 'tf.name_scope' to 'tf.compat.v1.name_scope' +726:9: INFO: `name` passed to `name_scope`. Because you may be re-entering an existing scope, it is not safe to convert automatically, the v2 name_scope does not support re-entering scopes by name. + +726:9: INFO: Renamed 'tf.name_scope' to 'tf.compat.v1.name_scope' +728:6: INFO: tf.summary.histogram requires manual check. The TF 1.x summary API cannot be automatically migrated to TF 2.0, so symbols have been converted to tf.compat.v1.summary.* and must be migrated manually. Typical usage will only require changes to the summary writing logic, not to individual calls like scalar(). For examples of the new summary API, see the Effective TF 2.0 migration document or check the TF 2.0 TensorBoard tutorials. +728:6: INFO: Renamed 'tf.summary.histogram' to 'tf.compat.v1.summary.histogram' +731:2: INFO: tf.summary.histogram requires manual check. The TF 1.x summary API cannot be automatically migrated to TF 2.0, so symbols have been converted to tf.compat.v1.summary.* and must be migrated manually. Typical usage will only require changes to the summary writing logic, not to individual calls like scalar(). For examples of the new summary API, see the Effective TF 2.0 migration document or check the TF 2.0 TensorBoard tutorials. +731:2: INFO: Renamed 'tf.summary.histogram' to 'tf.compat.v1.summary.histogram' +733:7: INFO: `name` passed to `name_scope`. Because you may be re-entering an existing scope, it is not safe to convert automatically, the v2 name_scope does not support re-entering scopes by name. + +733:7: INFO: Renamed 'tf.name_scope' to 'tf.compat.v1.name_scope' +734:20: INFO: Changing labels arg of tf.nn.softmax_cross_entropy_with_logits to tf.stop_gradient(labels). Please check this transformation. + +736:9: INFO: `name` passed to `name_scope`. Because you may be re-entering an existing scope, it is not safe to convert automatically, the v2 name_scope does not support re-entering scopes by name. + +736:9: INFO: Renamed 'tf.name_scope' to 'tf.compat.v1.name_scope' +737:27: INFO: Added keywords to args of function 'tf.reduce_mean' +738:2: INFO: tf.summary.scalar requires manual check. The TF 1.x summary API cannot be automatically migrated to TF 2.0, so symbols have been converted to tf.compat.v1.summary.* and must be migrated manually. Typical usage will only require changes to the summary writing logic, not to individual calls like scalar(). For examples of the new summary API, see the Effective TF 2.0 migration document or check the TF 2.0 TensorBoard tutorials. +738:2: INFO: Renamed 'tf.summary.scalar' to 'tf.compat.v1.summary.scalar' +740:7: INFO: `name` passed to `name_scope`. Because you may be re-entering an existing scope, it is not safe to convert automatically, the v2 name_scope does not support re-entering scopes by name. + +740:7: INFO: Renamed 'tf.name_scope' to 'tf.compat.v1.name_scope' +741:17: INFO: Renamed 'tf.train.GradientDescentOptimizer' to 'tf.compat.v1.train.GradientDescentOptimizer' +759:7: INFO: `name` passed to `name_scope`. Because you may be re-entering an existing scope, it is not safe to convert automatically, the v2 name_scope does not support re-entering scopes by name. + +759:7: INFO: Renamed 'tf.name_scope' to 'tf.compat.v1.name_scope' +760:9: INFO: `name` passed to `name_scope`. Because you may be re-entering an existing scope, it is not safe to convert automatically, the v2 name_scope does not support re-entering scopes by name. + +760:9: INFO: Renamed 'tf.name_scope' to 'tf.compat.v1.name_scope' +761:19: INFO: Added keywords to args of function 'tf.argmax' +763:22: INFO: Added keywords to args of function 'tf.argmax' +764:9: INFO: `name` passed to `name_scope`. Because you may be re-entering an existing scope, it is not safe to convert automatically, the v2 name_scope does not support re-entering scopes by name. + +764:9: INFO: Renamed 'tf.name_scope' to 'tf.compat.v1.name_scope' +765:24: INFO: Added keywords to args of function 'tf.reduce_mean' +766:2: INFO: tf.summary.scalar requires manual check. The TF 1.x summary API cannot be automatically migrated to TF 2.0, so symbols have been converted to tf.compat.v1.summary.* and must be migrated manually. Typical usage will only require changes to the summary writing logic, not to individual calls like scalar(). For examples of the new summary API, see the Effective TF 2.0 migration document or check the TF 2.0 TensorBoard tutorials. +766:2: INFO: Renamed 'tf.summary.scalar' to 'tf.compat.v1.summary.scalar' +772:5: INFO: Renamed 'tf.gfile.Exists' to 'tf.io.gfile.exists' +773:4: INFO: Renamed 'tf.gfile.DeleteRecursively' to 'tf.io.gfile.rmtree' +774:2: INFO: Renamed 'tf.gfile.MakeDirs' to 'tf.io.gfile.makedirs' +797:9: INFO: Renamed 'tf.Session' to 'tf.compat.v1.Session' +821:11: INFO: tf.summary.merge_all requires manual check. The TF 1.x summary API cannot be automatically migrated to TF 2.0, so symbols have been converted to tf.compat.v1.summary.* and must be migrated manually. Typical usage will only require changes to the summary writing logic, not to individual calls like scalar(). For examples of the new summary API, see the Effective TF 2.0 migration document or check the TF 2.0 TensorBoard tutorials. +821:11: INFO: Renamed 'tf.summary.merge_all' to 'tf.compat.v1.summary.merge_all' +822:17: INFO: tf.summary.FileWriter requires manual check. The TF 1.x summary API cannot be automatically migrated to TF 2.0, so symbols have been converted to tf.compat.v1.summary.* and must be migrated manually. Typical usage will only require changes to the summary writing logic, not to individual calls like scalar(). For examples of the new summary API, see the Effective TF 2.0 migration document or check the TF 2.0 TensorBoard tutorials. +822:17: INFO: Renamed 'tf.summary.FileWriter' to 'tf.compat.v1.summary.FileWriter' +824:22: INFO: tf.summary.FileWriter requires manual check. The TF 1.x summary API cannot be automatically migrated to TF 2.0, so symbols have been converted to tf.compat.v1.summary.* and must be migrated manually. Typical usage will only require changes to the summary writing logic, not to individual calls like scalar(). For examples of the new summary API, see the Effective TF 2.0 migration document or check the TF 2.0 TensorBoard tutorials. +824:22: INFO: Renamed 'tf.summary.FileWriter' to 'tf.compat.v1.summary.FileWriter' +827:9: INFO: Renamed 'tf.global_variables_initializer' to 'tf.compat.v1.global_variables_initializer' +1062:2: INFO: Renamed 'tf.app.run' to 'tf.compat.v1.app.run' +-------------------------------------------------------------------------------- + diff --git a/hub/examples/image_retraining/retrain.py b/hub/examples/image_retraining/retrain.py index 15832e04..236972ef 100644 --- a/hub/examples/image_retraining/retrain.py +++ b/hub/examples/image_retraining/retrain.py @@ -101,888 +101,905 @@ RESIZED_INPUT_TENSOR_NAME = 'ResizeBilinear:0' MAX_NUM_IMAGES_PER_CLASS = 2 ** 27 - 1 # ~134M +tf.compat.v1.disable_eager_execution() + def create_image_lists(image_dir, testing_percentage, validation_percentage): - """Builds a list of training images from the file system. - - Analyzes the sub folders in the image directory, splits them into stable - training, testing, and validation sets, and returns a data structure - describing the lists of images for each label and their paths. - - Args: - image_dir: String path to a folder containing subfolders of images. - testing_percentage: Integer percentage of the images to reserve for tests. - validation_percentage: Integer percentage of images reserved for validation. - - Returns: - A dictionary containing an entry for each label subfolder, with images split - into training, testing, and validation sets within each label. - """ - if not gfile.Exists(image_dir): - print("Image directory '" + image_dir + "' not found.") - return None - result = {} - sub_dirs = [x[0] for x in gfile.Walk(image_dir)] - # The root directory comes first, so skip it. - is_root_dir = True - for sub_dir in sub_dirs: - if is_root_dir: - is_root_dir = False - continue - extensions = ['jpg', 'jpeg', 'JPG', 'JPEG'] - file_list = [] - dir_name = os.path.basename(sub_dir) - if dir_name == image_dir: - continue - print("Looking for images in '" + dir_name + "'") - for extension in extensions: - file_glob = os.path.join(image_dir, dir_name, '*.' + extension) - file_list.extend(gfile.Glob(file_glob)) - if not file_list: - print('No files found') - continue - if len(file_list) < 20: - print('WARNING: Folder has less than 20 images, which may cause issues.') - elif len(file_list) > MAX_NUM_IMAGES_PER_CLASS: - print('WARNING: Folder {} has more than {} images. Some images will ' - 'never be selected.'.format(dir_name, MAX_NUM_IMAGES_PER_CLASS)) - label_name = re.sub(r'[^a-z0-9]+', ' ', dir_name.lower()) - training_images = [] - testing_images = [] - validation_images = [] - for file_name in file_list: - base_name = os.path.basename(file_name) - # We want to ignore anything after '_nohash_' in the file name when - # deciding which set to put an image in, the data set creator has a way of - # grouping photos that are close variations of each other. For example - # this is used in the plant disease data set to group multiple pictures of - # the same leaf. - hash_name = re.sub(r'_nohash_.*$', '', file_name) - # This looks a bit magical, but we need to decide whether this file should - # go into the training, testing, or validation sets, and we want to keep - # existing files in the same set even if more files are subsequently - # added. - # To do that, we need a stable way of deciding based on just the file name - # itself, so we do a hash of that and then use that to generate a - # probability value that we use to assign it. - hash_name_hashed = hashlib.sha1(compat.as_bytes(hash_name)).hexdigest() - percentage_hash = ((int(hash_name_hashed, 16) % - (MAX_NUM_IMAGES_PER_CLASS + 1)) * - (100.0 / MAX_NUM_IMAGES_PER_CLASS)) - if percentage_hash < validation_percentage: - validation_images.append(base_name) - elif percentage_hash < (testing_percentage + validation_percentage): - testing_images.append(base_name) - else: - training_images.append(base_name) - result[label_name] = { - 'dir': dir_name, - 'training': training_images, - 'testing': testing_images, - 'validation': validation_images, - } - return result + """Builds a list of training images from the file system. + + Analyzes the sub folders in the image directory, splits them into stable + training, testing, and validation sets, and returns a data structure + describing the lists of images for each label and their paths. + + Args: + image_dir: String path to a folder containing subfolders of images. + testing_percentage: Integer percentage of the images to reserve for tests. + validation_percentage: Integer percentage of images reserved for validation. + + Returns: + A dictionary containing an entry for each label subfolder, with images split + into training, testing, and validation sets within each label. + """ + if not gfile.Exists(image_dir): + print("Image directory '" + image_dir + "' not found.") + return None + result = {} + sub_dirs = [x[0] for x in gfile.Walk(image_dir)] + # The root directory comes first, so skip it. + is_root_dir = True + for sub_dir in sub_dirs: + if is_root_dir: + is_root_dir = False + continue + extensions = ['jpg', 'jpeg', 'JPG', 'JPEG'] + file_list = [] + dir_name = os.path.basename(sub_dir) + if dir_name == image_dir: + continue + print("Looking for images in '" + dir_name + "'") + for extension in extensions: + file_glob = os.path.join(image_dir, dir_name, '*.' + extension) + file_list.extend(gfile.Glob(file_glob)) + if not file_list: + print('No files found') + continue + if len(file_list) < 20: + print('WARNING: Folder has less than 20 images, which may cause issues.') + elif len(file_list) > MAX_NUM_IMAGES_PER_CLASS: + print('WARNING: Folder {} has more than {} images. Some images will ' + 'never be selected.'.format(dir_name, MAX_NUM_IMAGES_PER_CLASS)) + label_name = re.sub(r'[^a-z0-9]+', ' ', dir_name.lower()) + training_images = [] + testing_images = [] + validation_images = [] + for file_name in file_list: + base_name = os.path.basename(file_name) + # We want to ignore anything after '_nohash_' in the file name when + # deciding which set to put an image in, the data set creator has a way of + # grouping photos that are close variations of each other. For example + # this is used in the plant disease data set to group multiple pictures of + # the same leaf. + hash_name = re.sub(r'_nohash_.*$', '', file_name) + # This looks a bit magical, but we need to decide whether this file should + # go into the training, testing, or validation sets, and we want to keep + # existing files in the same set even if more files are subsequently + # added. + # To do that, we need a stable way of deciding based on just the file name + # itself, so we do a hash of that and then use that to generate a + # probability value that we use to assign it. + hash_name_hashed = hashlib.sha1( + compat.as_bytes(hash_name)).hexdigest() + percentage_hash = ((int(hash_name_hashed, 16) % + (MAX_NUM_IMAGES_PER_CLASS + 1)) * + (100.0 / MAX_NUM_IMAGES_PER_CLASS)) + if percentage_hash < validation_percentage: + validation_images.append(base_name) + elif percentage_hash < (testing_percentage + validation_percentage): + testing_images.append(base_name) + else: + training_images.append(base_name) + result[label_name] = { + 'dir': dir_name, + 'training': training_images, + 'testing': testing_images, + 'validation': validation_images, + } + return result def get_image_path(image_lists, label_name, index, image_dir, category): - """"Returns a path to an image for a label at the given index. - - Args: - image_lists: Dictionary of training images for each label. - label_name: Label string we want to get an image for. - index: Int offset of the image we want. This will be moduloed by the - available number of images for the label, so it can be arbitrarily large. - image_dir: Root folder string of the subfolders containing the training - images. - category: Name string of set to pull images from - training, testing, or - validation. - - Returns: - File system path string to an image that meets the requested parameters. - - """ - if label_name not in image_lists: - tf.logging.fatal('Label does not exist %s.', label_name) - label_lists = image_lists[label_name] - if category not in label_lists: - tf.logging.fatal('Category does not exist %s.', category) - category_list = label_lists[category] - if not category_list: - tf.logging.fatal('Label %s has no images in the category %s.', - label_name, category) - mod_index = index % len(category_list) - base_name = category_list[mod_index] - sub_dir = label_lists['dir'] - full_path = os.path.join(image_dir, sub_dir, base_name) - return full_path + """"Returns a path to an image for a label at the given index. + + Args: + image_lists: Dictionary of training images for each label. + label_name: Label string we want to get an image for. + index: Int offset of the image we want. This will be moduloed by the + available number of images for the label, so it can be arbitrarily large. + image_dir: Root folder string of the subfolders containing the training + images. + category: Name string of set to pull images from - training, testing, or + validation. + + Returns: + File system path string to an image that meets the requested parameters. + + """ + if label_name not in image_lists: + tf.compat.v1.logging.fatal('Label does not exist %s.', label_name) + label_lists = image_lists[label_name] + if category not in label_lists: + tf.compat.v1.logging.fatal('Category does not exist %s.', category) + category_list = label_lists[category] + if not category_list: + tf.compat.v1.logging.fatal('Label %s has no images in the category %s.', + label_name, category) + mod_index = index % len(category_list) + base_name = category_list[mod_index] + sub_dir = label_lists['dir'] + full_path = os.path.join(image_dir, sub_dir, base_name) + return full_path def get_bottleneck_path(image_lists, label_name, index, bottleneck_dir, category): - """"Returns a path to a bottleneck file for a label at the given index. + """"Returns a path to a bottleneck file for a label at the given index. - Args: - image_lists: Dictionary of training images for each label. - label_name: Label string we want to get an image for. - index: Integer offset of the image we want. This will be moduloed by the - available number of images for the label, so it can be arbitrarily large. - bottleneck_dir: Folder string holding cached files of bottleneck values. - category: Name string of set to pull images from - training, testing, or - validation. + Args: + image_lists: Dictionary of training images for each label. + label_name: Label string we want to get an image for. + index: Integer offset of the image we want. This will be moduloed by the + available number of images for the label, so it can be arbitrarily large. + bottleneck_dir: Folder string holding cached files of bottleneck values. + category: Name string of set to pull images from - training, testing, or + validation. - Returns: - File system path string to an image that meets the requested parameters. - """ - return get_image_path(image_lists, label_name, index, bottleneck_dir, - category) + '.txt' + Returns: + File system path string to an image that meets the requested parameters. + """ + return get_image_path(image_lists, label_name, index, bottleneck_dir, + category) + '.txt' def create_inception_graph(): - """"Creates a graph from saved GraphDef file and returns a Graph object. - - Returns: - Graph holding the trained Inception network, and various tensors we'll be - manipulating. - """ - with tf.Session() as sess: - model_filename = os.path.join( - FLAGS.model_dir, 'classify_image_graph_def.pb') - with gfile.FastGFile(model_filename, 'rb') as f: - graph_def = tf.GraphDef() - graph_def.ParseFromString(f.read()) - bottleneck_tensor, jpeg_data_tensor, resized_input_tensor = ( - tf.import_graph_def(graph_def, name='', return_elements=[ - BOTTLENECK_TENSOR_NAME, JPEG_DATA_TENSOR_NAME, - RESIZED_INPUT_TENSOR_NAME])) - return sess.graph, bottleneck_tensor, jpeg_data_tensor, resized_input_tensor + """"Creates a graph from saved GraphDef file and returns a Graph object. + + Returns: + Graph holding the trained Inception network, and various tensors we'll be + manipulating. + """ + with tf.compat.v1.Session() as sess: + model_filename = os.path.join( + FLAGS.model_dir, 'classify_image_graph_def.pb') + with gfile.FastGFile(model_filename, 'rb') as f: + graph_def = tf.compat.v1.GraphDef() + graph_def.ParseFromString(f.read()) + bottleneck_tensor, jpeg_data_tensor, resized_input_tensor = ( + tf.import_graph_def(graph_def, name='', return_elements=[ + BOTTLENECK_TENSOR_NAME, JPEG_DATA_TENSOR_NAME, + RESIZED_INPUT_TENSOR_NAME])) + return sess.graph, bottleneck_tensor, jpeg_data_tensor, resized_input_tensor def run_bottleneck_on_image(sess, image_data, image_data_tensor, bottleneck_tensor): - """Runs inference on an image to extract the 'bottleneck' summary layer. + """Runs inference on an image to extract the 'bottleneck' summary layer. - Args: - sess: Current active TensorFlow Session. - image_data: String of raw JPEG data. - image_data_tensor: Input data layer in the graph. - bottleneck_tensor: Layer before the final softmax. + Args: + sess: Current active TensorFlow Session. + image_data: String of raw JPEG data. + image_data_tensor: Input data layer in the graph. + bottleneck_tensor: Layer before the final softmax. - Returns: - Numpy array of bottleneck values. - """ - bottleneck_values = sess.run( - bottleneck_tensor, - {image_data_tensor: image_data}) - bottleneck_values = np.squeeze(bottleneck_values) - return bottleneck_values + Returns: + Numpy array of bottleneck values. + """ + bottleneck_values = sess.run( + bottleneck_tensor, + {image_data_tensor: image_data}) + bottleneck_values = np.squeeze(bottleneck_values) + return bottleneck_values def maybe_download_and_extract(): - """Download and extract model tar file. - - If the pretrained model we're using doesn't already exist, this function - downloads it from the TensorFlow.org website and unpacks it into a directory. - """ - dest_directory = FLAGS.model_dir - if not os.path.exists(dest_directory): - os.makedirs(dest_directory) - filename = DATA_URL.split('/')[-1] - filepath = os.path.join(dest_directory, filename) - if not os.path.exists(filepath): - - def _progress(count, block_size, total_size): - sys.stdout.write('\r>> Downloading %s %.1f%%' % - (filename, - float(count * block_size) / float(total_size) * 100.0)) - sys.stdout.flush() - - filepath, _ = urllib.request.urlretrieve(DATA_URL, - filepath, - _progress) - print() - statinfo = os.stat(filepath) - print('Successfully downloaded', filename, statinfo.st_size, 'bytes.') - tarfile.open(filepath, 'r:gz').extractall(dest_directory) + """Download and extract model tar file. + + If the pretrained model we're using doesn't already exist, this function + downloads it from the TensorFlow.org website and unpacks it into a directory. + """ + dest_directory = FLAGS.model_dir + if not os.path.exists(dest_directory): + os.makedirs(dest_directory) + filename = DATA_URL.split('/')[-1] + filepath = os.path.join(dest_directory, filename) + if not os.path.exists(filepath): + + def _progress(count, block_size, total_size): + sys.stdout.write('\r>> Downloading %s %.1f%%' % + (filename, + float(count * block_size) / float(total_size) * 100.0)) + sys.stdout.flush() + + filepath, _ = urllib.request.urlretrieve(DATA_URL, + filepath, + _progress) + print() + statinfo = os.stat(filepath) + print('Successfully downloaded', filename, statinfo.st_size, 'bytes.') + tarfile.open(filepath, 'r:gz').extractall(dest_directory) def ensure_dir_exists(dir_name): - """Makes sure the folder exists on disk. + """Makes sure the folder exists on disk. - Args: - dir_name: Path string to the folder we want to create. - """ - if not os.path.exists(dir_name): - os.makedirs(dir_name) + Args: + dir_name: Path string to the folder we want to create. + """ + if not os.path.exists(dir_name): + os.makedirs(dir_name) -def write_list_of_floats_to_file(list_of_floats , file_path): - """Writes a given list of floats to a binary file. +def write_list_of_floats_to_file(list_of_floats, file_path): + """Writes a given list of floats to a binary file. - Args: - list_of_floats: List of floats we want to write to a file. - file_path: Path to a file where list of floats will be stored. + Args: + list_of_floats: List of floats we want to write to a file. + file_path: Path to a file where list of floats will be stored. - """ + """ - s = struct.pack('d' * BOTTLENECK_TENSOR_SIZE, *list_of_floats) - with open(file_path, 'wb') as f: - f.write(s) + s = struct.pack('d' * BOTTLENECK_TENSOR_SIZE, *list_of_floats) + with open(file_path, 'wb') as f: + f.write(s) def read_list_of_floats_from_file(file_path): - """Reads list of floats from a given file. + """Reads list of floats from a given file. - Args: - file_path: Path to a file where list of floats was stored. - Returns: - Array of bottleneck values (list of floats). + Args: + file_path: Path to a file where list of floats was stored. + Returns: + Array of bottleneck values (list of floats). - """ + """ - with open(file_path, 'rb') as f: - s = struct.unpack('d' * BOTTLENECK_TENSOR_SIZE, f.read()) - return list(s) + with open(file_path, 'rb') as f: + s = struct.unpack('d' * BOTTLENECK_TENSOR_SIZE, f.read()) + return list(s) bottleneck_path_2_bottleneck_values = {} + def create_bottleneck_file(bottleneck_path, image_lists, label_name, index, image_dir, category, sess, jpeg_data_tensor, bottleneck_tensor): - print('Creating bottleneck at ' + bottleneck_path) - image_path = get_image_path(image_lists, label_name, index, image_dir, category) - if not gfile.Exists(image_path): - tf.logging.fatal('File does not exist %s', image_path) - image_data = gfile.FastGFile(image_path, 'rb').read() - bottleneck_values = run_bottleneck_on_image(sess, image_data, jpeg_data_tensor, bottleneck_tensor) - bottleneck_string = ','.join(str(x) for x in bottleneck_values) - with open(bottleneck_path, 'w') as bottleneck_file: - bottleneck_file.write(bottleneck_string) + print('Creating bottleneck at ' + bottleneck_path) + image_path = get_image_path( + image_lists, label_name, index, image_dir, category) + if not gfile.Exists(image_path): + tf.compat.v1.logging.fatal('File does not exist %s', image_path) + image_data = gfile.FastGFile(image_path, 'rb').read() + bottleneck_values = run_bottleneck_on_image( + sess, image_data, jpeg_data_tensor, bottleneck_tensor) + bottleneck_string = ','.join(str(x) for x in bottleneck_values) + with open(bottleneck_path, 'w') as bottleneck_file: + bottleneck_file.write(bottleneck_string) + def get_or_create_bottleneck(sess, image_lists, label_name, index, image_dir, category, bottleneck_dir, jpeg_data_tensor, bottleneck_tensor): - """Retrieves or calculates bottleneck values for an image. - - If a cached version of the bottleneck data exists on-disk, return that, - otherwise calculate the data and save it to disk for future use. - - Args: - sess: The current active TensorFlow Session. - image_lists: Dictionary of training images for each label. - label_name: Label string we want to get an image for. - index: Integer offset of the image we want. This will be modulo-ed by the - available number of images for the label, so it can be arbitrarily large. - image_dir: Root folder string of the subfolders containing the training - images. - category: Name string of which set to pull images from - training, testing, - or validation. - bottleneck_dir: Folder string holding cached files of bottleneck values. - jpeg_data_tensor: The tensor to feed loaded jpeg data into. - bottleneck_tensor: The output tensor for the bottleneck values. - - Returns: - Numpy array of values produced by the bottleneck layer for the image. - """ - label_lists = image_lists[label_name] - sub_dir = label_lists['dir'] - sub_dir_path = os.path.join(bottleneck_dir, sub_dir) - ensure_dir_exists(sub_dir_path) - bottleneck_path = get_bottleneck_path(image_lists, label_name, index, bottleneck_dir, category) - if not os.path.exists(bottleneck_path): - create_bottleneck_file(bottleneck_path, image_lists, label_name, index, image_dir, category, sess, jpeg_data_tensor, bottleneck_tensor) - with open(bottleneck_path, 'r') as bottleneck_file: - bottleneck_string = bottleneck_file.read() - did_hit_error = False - try: - bottleneck_values = [float(x) for x in bottleneck_string.split(',')] - except: - print("Invalid float found, recreating bottleneck") - did_hit_error = True - if did_hit_error: - create_bottleneck_file(bottleneck_path, image_lists, label_name, index, image_dir, category, sess, jpeg_data_tensor, bottleneck_tensor) + """Retrieves or calculates bottleneck values for an image. + + If a cached version of the bottleneck data exists on-disk, return that, + otherwise calculate the data and save it to disk for future use. + + Args: + sess: The current active TensorFlow Session. + image_lists: Dictionary of training images for each label. + label_name: Label string we want to get an image for. + index: Integer offset of the image we want. This will be modulo-ed by the + available number of images for the label, so it can be arbitrarily large. + image_dir: Root folder string of the subfolders containing the training + images. + category: Name string of which set to pull images from - training, testing, + or validation. + bottleneck_dir: Folder string holding cached files of bottleneck values. + jpeg_data_tensor: The tensor to feed loaded jpeg data into. + bottleneck_tensor: The output tensor for the bottleneck values. + + Returns: + Numpy array of values produced by the bottleneck layer for the image. + """ + label_lists = image_lists[label_name] + sub_dir = label_lists['dir'] + sub_dir_path = os.path.join(bottleneck_dir, sub_dir) + ensure_dir_exists(sub_dir_path) + bottleneck_path = get_bottleneck_path( + image_lists, label_name, index, bottleneck_dir, category) + if not os.path.exists(bottleneck_path): + create_bottleneck_file(bottleneck_path, image_lists, label_name, index, + image_dir, category, sess, jpeg_data_tensor, bottleneck_tensor) with open(bottleneck_path, 'r') as bottleneck_file: - bottleneck_string = bottleneck_file.read() - # Allow exceptions to propagate here, since they shouldn't happen after a fresh creation - bottleneck_values = [float(x) for x in bottleneck_string.split(',')] - return bottleneck_values + bottleneck_string = bottleneck_file.read() + did_hit_error = False + try: + bottleneck_values = [float(x) for x in bottleneck_string.split(',')] + except: + print("Invalid float found, recreating bottleneck") + did_hit_error = True + if did_hit_error: + create_bottleneck_file(bottleneck_path, image_lists, label_name, index, + image_dir, category, sess, jpeg_data_tensor, bottleneck_tensor) + with open(bottleneck_path, 'r') as bottleneck_file: + bottleneck_string = bottleneck_file.read() + # Allow exceptions to propagate here, since they shouldn't happen after a fresh creation + bottleneck_values = [float(x) for x in bottleneck_string.split(',')] + return bottleneck_values + def cache_bottlenecks(sess, image_lists, image_dir, bottleneck_dir, jpeg_data_tensor, bottleneck_tensor): - """Ensures all the training, testing, and validation bottlenecks are cached. - - Because we're likely to read the same image multiple times (if there are no - distortions applied during training) it can speed things up a lot if we - calculate the bottleneck layer values once for each image during - preprocessing, and then just read those cached values repeatedly during - training. Here we go through all the images we've found, calculate those - values, and save them off. - - Args: - sess: The current active TensorFlow Session. - image_lists: Dictionary of training images for each label. - image_dir: Root folder string of the subfolders containing the training - images. - bottleneck_dir: Folder string holding cached files of bottleneck values. - jpeg_data_tensor: Input tensor for jpeg data from file. - bottleneck_tensor: The penultimate output layer of the graph. - - Returns: - Nothing. - """ - how_many_bottlenecks = 0 - ensure_dir_exists(bottleneck_dir) - for label_name, label_lists in image_lists.items(): - for category in ['training', 'testing', 'validation']: - category_list = label_lists[category] - for index, unused_base_name in enumerate(category_list): - get_or_create_bottleneck(sess, image_lists, label_name, index, - image_dir, category, bottleneck_dir, - jpeg_data_tensor, bottleneck_tensor) - - how_many_bottlenecks += 1 - if how_many_bottlenecks % 100 == 0: - print(str(how_many_bottlenecks) + ' bottleneck files created.') + """Ensures all the training, testing, and validation bottlenecks are cached. + + Because we're likely to read the same image multiple times (if there are no + distortions applied during training) it can speed things up a lot if we + calculate the bottleneck layer values once for each image during + preprocessing, and then just read those cached values repeatedly during + training. Here we go through all the images we've found, calculate those + values, and save them off. + + Args: + sess: The current active TensorFlow Session. + image_lists: Dictionary of training images for each label. + image_dir: Root folder string of the subfolders containing the training + images. + bottleneck_dir: Folder string holding cached files of bottleneck values. + jpeg_data_tensor: Input tensor for jpeg data from file. + bottleneck_tensor: The penultimate output layer of the graph. + + Returns: + Nothing. + """ + how_many_bottlenecks = 0 + ensure_dir_exists(bottleneck_dir) + for label_name, label_lists in image_lists.items(): + for category in ['training', 'testing', 'validation']: + category_list = label_lists[category] + for index, unused_base_name in enumerate(category_list): + get_or_create_bottleneck(sess, image_lists, label_name, index, + image_dir, category, bottleneck_dir, + jpeg_data_tensor, bottleneck_tensor) + + how_many_bottlenecks += 1 + if how_many_bottlenecks % 100 == 0: + print(str(how_many_bottlenecks) + + ' bottleneck files created.') def get_random_cached_bottlenecks(sess, image_lists, how_many, category, bottleneck_dir, image_dir, jpeg_data_tensor, bottleneck_tensor): - """Retrieves bottleneck values for cached images. - - If no distortions are being applied, this function can retrieve the cached - bottleneck values directly from disk for images. It picks a random set of - images from the specified category. - - Args: - sess: Current TensorFlow Session. - image_lists: Dictionary of training images for each label. - how_many: If positive, a random sample of this size will be chosen. - If negative, all bottlenecks will be retrieved. - category: Name string of which set to pull from - training, testing, or - validation. - bottleneck_dir: Folder string holding cached files of bottleneck values. - image_dir: Root folder string of the subfolders containing the training - images. - jpeg_data_tensor: The layer to feed jpeg image data into. - bottleneck_tensor: The bottleneck output layer of the CNN graph. - - Returns: - List of bottleneck arrays, their corresponding ground truths, and the - relevant filenames. - """ - class_count = len(image_lists.keys()) - bottlenecks = [] - ground_truths = [] - filenames = [] - if how_many >= 0: - # Retrieve a random sample of bottlenecks. + """Retrieves bottleneck values for cached images. + + If no distortions are being applied, this function can retrieve the cached + bottleneck values directly from disk for images. It picks a random set of + images from the specified category. + + Args: + sess: Current TensorFlow Session. + image_lists: Dictionary of training images for each label. + how_many: If positive, a random sample of this size will be chosen. + If negative, all bottlenecks will be retrieved. + category: Name string of which set to pull from - training, testing, or + validation. + bottleneck_dir: Folder string holding cached files of bottleneck values. + image_dir: Root folder string of the subfolders containing the training + images. + jpeg_data_tensor: The layer to feed jpeg image data into. + bottleneck_tensor: The bottleneck output layer of the CNN graph. + + Returns: + List of bottleneck arrays, their corresponding ground truths, and the + relevant filenames. + """ + class_count = len(image_lists.keys()) + bottlenecks = [] + ground_truths = [] + filenames = [] + if how_many >= 0: + # Retrieve a random sample of bottlenecks. + for unused_i in range(how_many): + label_index = random.randrange(class_count) + label_name = list(image_lists.keys())[label_index] + image_index = random.randrange(MAX_NUM_IMAGES_PER_CLASS + 1) + image_name = get_image_path(image_lists, label_name, image_index, + image_dir, category) + bottleneck = get_or_create_bottleneck(sess, image_lists, label_name, + image_index, image_dir, category, + bottleneck_dir, jpeg_data_tensor, + bottleneck_tensor) + ground_truth = np.zeros(class_count, dtype=np.float32) + ground_truth[label_index] = 1.0 + bottlenecks.append(bottleneck) + ground_truths.append(ground_truth) + filenames.append(image_name) + else: + # Retrieve all bottlenecks. + for label_index, label_name in enumerate(image_lists.keys()): + for image_index, image_name in enumerate( + image_lists[label_name][category]): + image_name = get_image_path(image_lists, label_name, image_index, + image_dir, category) + bottleneck = get_or_create_bottleneck(sess, image_lists, label_name, + image_index, image_dir, category, + bottleneck_dir, jpeg_data_tensor, + bottleneck_tensor) + ground_truth = np.zeros(class_count, dtype=np.float32) + ground_truth[label_index] = 1.0 + bottlenecks.append(bottleneck) + ground_truths.append(ground_truth) + filenames.append(image_name) + return bottlenecks, ground_truths, filenames + + +def get_random_distorted_bottlenecks( + sess, image_lists, how_many, category, image_dir, input_jpeg_tensor, + distorted_image, resized_input_tensor, bottleneck_tensor): + """Retrieves bottleneck values for training images, after distortions. + + If we're training with distortions like crops, scales, or flips, we have to + recalculate the full model for every image, and so we can't use cached + bottleneck values. Instead we find random images for the requested category, + run them through the distortion graph, and then the full graph to get the + bottleneck results for each. + + Args: + sess: Current TensorFlow Session. + image_lists: Dictionary of training images for each label. + how_many: The integer number of bottleneck values to return. + category: Name string of which set of images to fetch - training, testing, + or validation. + image_dir: Root folder string of the subfolders containing the training + images. + input_jpeg_tensor: The input layer we feed the image data to. + distorted_image: The output node of the distortion graph. + resized_input_tensor: The input node of the recognition graph. + bottleneck_tensor: The bottleneck output layer of the CNN graph. + + Returns: + List of bottleneck arrays and their corresponding ground truths. + """ + class_count = len(image_lists.keys()) + bottlenecks = [] + ground_truths = [] for unused_i in range(how_many): - label_index = random.randrange(class_count) - label_name = list(image_lists.keys())[label_index] - image_index = random.randrange(MAX_NUM_IMAGES_PER_CLASS + 1) - image_name = get_image_path(image_lists, label_name, image_index, - image_dir, category) - bottleneck = get_or_create_bottleneck(sess, image_lists, label_name, - image_index, image_dir, category, - bottleneck_dir, jpeg_data_tensor, - bottleneck_tensor) - ground_truth = np.zeros(class_count, dtype=np.float32) - ground_truth[label_index] = 1.0 - bottlenecks.append(bottleneck) - ground_truths.append(ground_truth) - filenames.append(image_name) - else: - # Retrieve all bottlenecks. - for label_index, label_name in enumerate(image_lists.keys()): - for image_index, image_name in enumerate( - image_lists[label_name][category]): - image_name = get_image_path(image_lists, label_name, image_index, - image_dir, category) - bottleneck = get_or_create_bottleneck(sess, image_lists, label_name, - image_index, image_dir, category, - bottleneck_dir, jpeg_data_tensor, - bottleneck_tensor) + label_index = random.randrange(class_count) + label_name = list(image_lists.keys())[label_index] + image_index = random.randrange(MAX_NUM_IMAGES_PER_CLASS + 1) + image_path = get_image_path(image_lists, label_name, image_index, image_dir, + category) + if not gfile.Exists(image_path): + tf.compat.v1.logging.fatal('File does not exist %s', image_path) + jpeg_data = gfile.FastGFile(image_path, 'rb').read() + # Note that we materialize the distorted_image_data as a numpy array before + # sending running inference on the image. This involves 2 memory copies and + # might be optimized in other implementations. + distorted_image_data = sess.run(distorted_image, + {input_jpeg_tensor: jpeg_data}) + bottleneck = run_bottleneck_on_image(sess, distorted_image_data, + resized_input_tensor, + bottleneck_tensor) ground_truth = np.zeros(class_count, dtype=np.float32) ground_truth[label_index] = 1.0 bottlenecks.append(bottleneck) ground_truths.append(ground_truth) - filenames.append(image_name) - return bottlenecks, ground_truths, filenames - - -def get_random_distorted_bottlenecks( - sess, image_lists, how_many, category, image_dir, input_jpeg_tensor, - distorted_image, resized_input_tensor, bottleneck_tensor): - """Retrieves bottleneck values for training images, after distortions. - - If we're training with distortions like crops, scales, or flips, we have to - recalculate the full model for every image, and so we can't use cached - bottleneck values. Instead we find random images for the requested category, - run them through the distortion graph, and then the full graph to get the - bottleneck results for each. - - Args: - sess: Current TensorFlow Session. - image_lists: Dictionary of training images for each label. - how_many: The integer number of bottleneck values to return. - category: Name string of which set of images to fetch - training, testing, - or validation. - image_dir: Root folder string of the subfolders containing the training - images. - input_jpeg_tensor: The input layer we feed the image data to. - distorted_image: The output node of the distortion graph. - resized_input_tensor: The input node of the recognition graph. - bottleneck_tensor: The bottleneck output layer of the CNN graph. - - Returns: - List of bottleneck arrays and their corresponding ground truths. - """ - class_count = len(image_lists.keys()) - bottlenecks = [] - ground_truths = [] - for unused_i in range(how_many): - label_index = random.randrange(class_count) - label_name = list(image_lists.keys())[label_index] - image_index = random.randrange(MAX_NUM_IMAGES_PER_CLASS + 1) - image_path = get_image_path(image_lists, label_name, image_index, image_dir, - category) - if not gfile.Exists(image_path): - tf.logging.fatal('File does not exist %s', image_path) - jpeg_data = gfile.FastGFile(image_path, 'rb').read() - # Note that we materialize the distorted_image_data as a numpy array before - # sending running inference on the image. This involves 2 memory copies and - # might be optimized in other implementations. - distorted_image_data = sess.run(distorted_image, - {input_jpeg_tensor: jpeg_data}) - bottleneck = run_bottleneck_on_image(sess, distorted_image_data, - resized_input_tensor, - bottleneck_tensor) - ground_truth = np.zeros(class_count, dtype=np.float32) - ground_truth[label_index] = 1.0 - bottlenecks.append(bottleneck) - ground_truths.append(ground_truth) - return bottlenecks, ground_truths + return bottlenecks, ground_truths def should_distort_images(flip_left_right, random_crop, random_scale, random_brightness): - """Whether any distortions are enabled, from the input flags. + """Whether any distortions are enabled, from the input flags. - Args: - flip_left_right: Boolean whether to randomly mirror images horizontally. - random_crop: Integer percentage setting the total margin used around the - crop box. - random_scale: Integer percentage of how much to vary the scale by. - random_brightness: Integer range to randomly multiply the pixel values by. + Args: + flip_left_right: Boolean whether to randomly mirror images horizontally. + random_crop: Integer percentage setting the total margin used around the + crop box. + random_scale: Integer percentage of how much to vary the scale by. + random_brightness: Integer range to randomly multiply the pixel values by. - Returns: - Boolean value indicating whether any distortions should be applied. - """ - return (flip_left_right or (random_crop != 0) or (random_scale != 0) or - (random_brightness != 0)) + Returns: + Boolean value indicating whether any distortions should be applied. + """ + return (flip_left_right or (random_crop != 0) or (random_scale != 0) or + (random_brightness != 0)) def add_input_distortions(flip_left_right, random_crop, random_scale, random_brightness): - """Creates the operations to apply the specified distortions. - - During training it can help to improve the results if we run the images - through simple distortions like crops, scales, and flips. These reflect the - kind of variations we expect in the real world, and so can help train the - model to cope with natural data more effectively. Here we take the supplied - parameters and construct a network of operations to apply them to an image. - - Cropping - ~~~~~~~~ - - Cropping is done by placing a bounding box at a random position in the full - image. The cropping parameter controls the size of that box relative to the - input image. If it's zero, then the box is the same size as the input and no - cropping is performed. If the value is 50%, then the crop box will be half the - width and height of the input. In a diagram it looks like this: - - < width > - +---------------------+ - | | - | width - crop% | - | < > | - | +------+ | - | | | | - | | | | - | | | | - | +------+ | - | | - | | - +---------------------+ - - Scaling - ~~~~~~~ - - Scaling is a lot like cropping, except that the bounding box is always - centered and its size varies randomly within the given range. For example if - the scale percentage is zero, then the bounding box is the same size as the - input and no scaling is applied. If it's 50%, then the bounding box will be in - a random range between half the width and height and full size. - - Args: - flip_left_right: Boolean whether to randomly mirror images horizontally. - random_crop: Integer percentage setting the total margin used around the - crop box. - random_scale: Integer percentage of how much to vary the scale by. - random_brightness: Integer range to randomly multiply the pixel values by. - graph. - - Returns: - The jpeg input layer and the distorted result tensor. - """ - - jpeg_data = tf.placeholder(tf.string, name='DistortJPGInput') - decoded_image = tf.image.decode_jpeg(jpeg_data, channels=MODEL_INPUT_DEPTH) - decoded_image_as_float = tf.cast(decoded_image, dtype=tf.float32) - decoded_image_4d = tf.expand_dims(decoded_image_as_float, 0) - margin_scale = 1.0 + (random_crop / 100.0) - resize_scale = 1.0 + (random_scale / 100.0) - margin_scale_value = tf.constant(margin_scale) - resize_scale_value = tf.random_uniform(tensor_shape.scalar(), - minval=1.0, - maxval=resize_scale) - scale_value = tf.multiply(margin_scale_value, resize_scale_value) - precrop_width = tf.multiply(scale_value, MODEL_INPUT_WIDTH) - precrop_height = tf.multiply(scale_value, MODEL_INPUT_HEIGHT) - precrop_shape = tf.stack([precrop_height, precrop_width]) - precrop_shape_as_int = tf.cast(precrop_shape, dtype=tf.int32) - precropped_image = tf.image.resize_bilinear(decoded_image_4d, - precrop_shape_as_int) - precropped_image_3d = tf.squeeze(precropped_image, squeeze_dims=[0]) - cropped_image = tf.random_crop(precropped_image_3d, - [MODEL_INPUT_HEIGHT, MODEL_INPUT_WIDTH, - MODEL_INPUT_DEPTH]) - if flip_left_right: - flipped_image = tf.image.random_flip_left_right(cropped_image) - else: - flipped_image = cropped_image - brightness_min = 1.0 - (random_brightness / 100.0) - brightness_max = 1.0 + (random_brightness / 100.0) - brightness_value = tf.random_uniform(tensor_shape.scalar(), - minval=brightness_min, - maxval=brightness_max) - brightened_image = tf.multiply(flipped_image, brightness_value) - distort_result = tf.expand_dims(brightened_image, 0, name='DistortResult') - return jpeg_data, distort_result + """Creates the operations to apply the specified distortions. + + During training it can help to improve the results if we run the images + through simple distortions like crops, scales, and flips. These reflect the + kind of variations we expect in the real world, and so can help train the + model to cope with natural data more effectively. Here we take the supplied + parameters and construct a network of operations to apply them to an image. + + Cropping + ~~~~~~~~ + + Cropping is done by placing a bounding box at a random position in the full + image. The cropping parameter controls the size of that box relative to the + input image. If it's zero, then the box is the same size as the input and no + cropping is performed. If the value is 50%, then the crop box will be half the + width and height of the input. In a diagram it looks like this: + + < width > + +---------------------+ + | | + | width - crop% | + | < > | + | +------+ | + | | | | + | | | | + | | | | + | +------+ | + | | + | | + +---------------------+ + + Scaling + ~~~~~~~ + + Scaling is a lot like cropping, except that the bounding box is always + centered and its size varies randomly within the given range. For example if + the scale percentage is zero, then the bounding box is the same size as the + input and no scaling is applied. If it's 50%, then the bounding box will be in + a random range between half the width and height and full size. + + Args: + flip_left_right: Boolean whether to randomly mirror images horizontally. + random_crop: Integer percentage setting the total margin used around the + crop box. + random_scale: Integer percentage of how much to vary the scale by. + random_brightness: Integer range to randomly multiply the pixel values by. + graph. + + Returns: + The jpeg input layer and the distorted result tensor. + """ + + jpeg_data = tf.compat.v1.placeholder(tf.string, name='DistortJPGInput') + decoded_image = tf.image.decode_jpeg(jpeg_data, channels=MODEL_INPUT_DEPTH) + decoded_image_as_float = tf.cast(decoded_image, dtype=tf.float32) + decoded_image_4d = tf.expand_dims(decoded_image_as_float, 0) + margin_scale = 1.0 + (random_crop / 100.0) + resize_scale = 1.0 + (random_scale / 100.0) + margin_scale_value = tf.constant(margin_scale) + resize_scale_value = tf.random.uniform(tensor_shape.scalar(), + minval=1.0, + maxval=resize_scale) + scale_value = tf.multiply(margin_scale_value, resize_scale_value) + precrop_width = tf.multiply(scale_value, MODEL_INPUT_WIDTH) + precrop_height = tf.multiply(scale_value, MODEL_INPUT_HEIGHT) + precrop_shape = tf.stack([precrop_height, precrop_width]) + precrop_shape_as_int = tf.cast(precrop_shape, dtype=tf.int32) + precropped_image = tf.image.resize(decoded_image_4d, + precrop_shape_as_int, method=tf.image.ResizeMethod.BILINEAR) + precropped_image_3d = tf.squeeze(precropped_image, axis=[0]) + cropped_image = tf.image.random_crop(precropped_image_3d, + [MODEL_INPUT_HEIGHT, MODEL_INPUT_WIDTH, + MODEL_INPUT_DEPTH]) + if flip_left_right: + flipped_image = tf.image.random_flip_left_right(cropped_image) + else: + flipped_image = cropped_image + brightness_min = 1.0 - (random_brightness / 100.0) + brightness_max = 1.0 + (random_brightness / 100.0) + brightness_value = tf.random.uniform(tensor_shape.scalar(), + minval=brightness_min, + maxval=brightness_max) + brightened_image = tf.multiply(flipped_image, brightness_value) + distort_result = tf.expand_dims(brightened_image, 0, name='DistortResult') + return jpeg_data, distort_result def variable_summaries(var): - """Attach a lot of summaries to a Tensor (for TensorBoard visualization).""" - with tf.name_scope('summaries'): - mean = tf.reduce_mean(var) - tf.summary.scalar('mean', mean) - with tf.name_scope('stddev'): - stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean))) - tf.summary.scalar('stddev', stddev) - tf.summary.scalar('max', tf.reduce_max(var)) - tf.summary.scalar('min', tf.reduce_min(var)) - tf.summary.histogram('histogram', var) + """Attach a lot of summaries to a Tensor (for TensorBoard visualization).""" + with tf.compat.v1.name_scope('summaries'): + mean = tf.reduce_mean(input_tensor=var) + tf.compat.v1.summary.scalar('mean', mean) + with tf.compat.v1.name_scope('stddev'): + stddev = tf.sqrt(tf.reduce_mean( + input_tensor=tf.square(var - mean))) + tf.compat.v1.summary.scalar('stddev', stddev) + tf.compat.v1.summary.scalar('max', tf.reduce_max(input_tensor=var)) + tf.compat.v1.summary.scalar('min', tf.reduce_min(input_tensor=var)) + tf.compat.v1.summary.histogram('histogram', var) def add_final_training_ops(class_count, final_tensor_name, bottleneck_tensor): - """Adds a new softmax and fully-connected layer for training. - - We need to retrain the top layer to identify our new classes, so this function - adds the right operations to the graph, along with some variables to hold the - weights, and then sets up all the gradients for the backward pass. - - The set up for the softmax and fully-connected layers is based on: - https://tensorflow.org/versions/master/tutorials/mnist/beginners/index.html - - Args: - class_count: Integer of how many categories of things we're trying to - recognize. - final_tensor_name: Name string for the new final node that produces results. - bottleneck_tensor: The output of the main CNN graph. - - Returns: - The tensors for the training and cross entropy results, and tensors for the - bottleneck input and ground truth input. - """ - with tf.name_scope('input'): - bottleneck_input = tf.placeholder_with_default( - bottleneck_tensor, shape=[None, BOTTLENECK_TENSOR_SIZE], - name='BottleneckInputPlaceholder') - - ground_truth_input = tf.placeholder(tf.float32, - [None, class_count], - name='GroundTruthInput') - - # Organizing the following ops as `final_training_ops` so they're easier - # to see in TensorBoard - layer_name = 'final_training_ops' - with tf.name_scope(layer_name): - with tf.name_scope('weights'): - layer_weights = tf.Variable(tf.truncated_normal([BOTTLENECK_TENSOR_SIZE, class_count], stddev=0.001), name='final_weights') - variable_summaries(layer_weights) - with tf.name_scope('biases'): - layer_biases = tf.Variable(tf.zeros([class_count]), name='final_biases') - variable_summaries(layer_biases) - with tf.name_scope('Wx_plus_b'): - logits = tf.matmul(bottleneck_input, layer_weights) + layer_biases - tf.summary.histogram('pre_activations', logits) - - final_tensor = tf.nn.softmax(logits, name=final_tensor_name) - tf.summary.histogram('activations', final_tensor) - - with tf.name_scope('cross_entropy'): - cross_entropy = tf.nn.softmax_cross_entropy_with_logits( - labels=ground_truth_input, logits=logits) - with tf.name_scope('total'): - cross_entropy_mean = tf.reduce_mean(cross_entropy) - tf.summary.scalar('cross_entropy', cross_entropy_mean) - - with tf.name_scope('train'): - train_step = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize( - cross_entropy_mean) - - return (train_step, cross_entropy_mean, bottleneck_input, ground_truth_input, - final_tensor) + """Adds a new softmax and fully-connected layer for training. + + We need to retrain the top layer to identify our new classes, so this function + adds the right operations to the graph, along with some variables to hold the + weights, and then sets up all the gradients for the backward pass. + + The set up for the softmax and fully-connected layers is based on: + https://tensorflow.org/versions/master/tutorials/mnist/beginners/index.html + + Args: + class_count: Integer of how many categories of things we're trying to + recognize. + final_tensor_name: Name string for the new final node that produces results. + bottleneck_tensor: The output of the main CNN graph. + + Returns: + The tensors for the training and cross entropy results, and tensors for the + bottleneck input and ground truth input. + """ + with tf.compat.v1.name_scope('input'): + bottleneck_input = tf.compat.v1.placeholder_with_default( + bottleneck_tensor, shape=[None, BOTTLENECK_TENSOR_SIZE], + name='BottleneckInputPlaceholder') + + ground_truth_input = tf.compat.v1.placeholder(tf.float32, + [None, class_count], + name='GroundTruthInput') + + # Organizing the following ops as `final_training_ops` so they're easier + # to see in TensorBoard + layer_name = 'final_training_ops' + with tf.compat.v1.name_scope(layer_name): + with tf.compat.v1.name_scope('weights'): + layer_weights = tf.Variable(tf.random.truncated_normal( + [BOTTLENECK_TENSOR_SIZE, class_count], stddev=0.001), name='final_weights') + variable_summaries(layer_weights) + with tf.compat.v1.name_scope('biases'): + layer_biases = tf.Variable( + tf.zeros([class_count]), name='final_biases') + variable_summaries(layer_biases) + with tf.compat.v1.name_scope('Wx_plus_b'): + logits = tf.matmul(bottleneck_input, layer_weights) + layer_biases + tf.compat.v1.summary.histogram('pre_activations', logits) + + final_tensor = tf.nn.softmax(logits, name=final_tensor_name) + tf.compat.v1.summary.histogram('activations', final_tensor) + + with tf.compat.v1.name_scope('cross_entropy'): + cross_entropy = tf.nn.softmax_cross_entropy_with_logits( + labels=tf.stop_gradient(ground_truth_input), logits=logits) + with tf.compat.v1.name_scope('total'): + cross_entropy_mean = tf.reduce_mean(input_tensor=cross_entropy) + tf.compat.v1.summary.scalar('cross_entropy', cross_entropy_mean) + + with tf.compat.v1.name_scope('train'): + train_step = tf.compat.v1.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize( + cross_entropy_mean) + + return (train_step, cross_entropy_mean, bottleneck_input, ground_truth_input, + final_tensor) def add_evaluation_step(result_tensor, ground_truth_tensor): - """Inserts the operations we need to evaluate the accuracy of our results. - - Args: - result_tensor: The new final node that produces results. - ground_truth_tensor: The node we feed ground truth data - into. - - Returns: - Tuple of (evaluation step, prediction). - """ - with tf.name_scope('accuracy'): - with tf.name_scope('correct_prediction'): - prediction = tf.argmax(result_tensor, 1) - correct_prediction = tf.equal( - prediction, tf.argmax(ground_truth_tensor, 1)) - with tf.name_scope('accuracy'): - evaluation_step = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) - tf.summary.scalar('accuracy', evaluation_step) - return evaluation_step, prediction + """Inserts the operations we need to evaluate the accuracy of our results. + + Args: + result_tensor: The new final node that produces results. + ground_truth_tensor: The node we feed ground truth data + into. + + Returns: + Tuple of (evaluation step, prediction). + """ + with tf.compat.v1.name_scope('accuracy'): + with tf.compat.v1.name_scope('correct_prediction'): + prediction = tf.argmax(input=result_tensor, axis=1) + correct_prediction = tf.equal( + prediction, tf.argmax(input=ground_truth_tensor, axis=1)) + with tf.compat.v1.name_scope('accuracy'): + evaluation_step = tf.reduce_mean( + input_tensor=tf.cast(correct_prediction, tf.float32)) + tf.compat.v1.summary.scalar('accuracy', evaluation_step) + return evaluation_step, prediction def main(_): - # Setup the directory we'll write summaries to for TensorBoard - if tf.gfile.Exists(FLAGS.summaries_dir): - tf.gfile.DeleteRecursively(FLAGS.summaries_dir) - tf.gfile.MakeDirs(FLAGS.summaries_dir) - - # Set up the pre-trained graph. - maybe_download_and_extract() - graph, bottleneck_tensor, jpeg_data_tensor, resized_image_tensor = ( - create_inception_graph()) - - # Look at the folder structure, and create lists of all the images. - image_lists = create_image_lists(FLAGS.image_dir, FLAGS.testing_percentage, - FLAGS.validation_percentage) - class_count = len(image_lists.keys()) - if class_count == 0: - print('No valid folders of images found at ' + FLAGS.image_dir) - return -1 - if class_count == 1: - print('Only one valid folder of images found at ' + FLAGS.image_dir + - ' - multiple classes are needed for classification.') - return -1 - - # See if the command-line flags mean we're applying any distortions. - do_distort_images = should_distort_images( - FLAGS.flip_left_right, FLAGS.random_crop, FLAGS.random_scale, - FLAGS.random_brightness) - sess = tf.Session() - - if do_distort_images: - # We will be applying distortions, so setup the operations we'll need. - distorted_jpeg_data_tensor, distorted_image_tensor = add_input_distortions( + # Setup the directory we'll write summaries to for TensorBoard + if tf.io.gfile.exists(FLAGS.summaries_dir): + tf.io.gfile.rmtree(FLAGS.summaries_dir) + tf.io.gfile.makedirs(FLAGS.summaries_dir) + + # Set up the pre-trained graph. + maybe_download_and_extract() + graph, bottleneck_tensor, jpeg_data_tensor, resized_image_tensor = ( + create_inception_graph()) + + # Look at the folder structure, and create lists of all the images. + image_lists = create_image_lists(FLAGS.image_dir, FLAGS.testing_percentage, + FLAGS.validation_percentage) + class_count = len(image_lists.keys()) + if class_count == 0: + print('No valid folders of images found at ' + FLAGS.image_dir) + return -1 + if class_count == 1: + print('Only one valid folder of images found at ' + FLAGS.image_dir + + ' - multiple classes are needed for classification.') + return -1 + + # See if the command-line flags mean we're applying any distortions. + do_distort_images = should_distort_images( FLAGS.flip_left_right, FLAGS.random_crop, FLAGS.random_scale, FLAGS.random_brightness) - else: - # We'll make sure we've calculated the 'bottleneck' image summaries and - # cached them on disk. - cache_bottlenecks(sess, image_lists, FLAGS.image_dir, FLAGS.bottleneck_dir, - jpeg_data_tensor, bottleneck_tensor) - - # Add the new layer that we'll be training. - (train_step, cross_entropy, bottleneck_input, ground_truth_input, - final_tensor) = add_final_training_ops(len(image_lists.keys()), - FLAGS.final_tensor_name, - bottleneck_tensor) - - # Create the operations we need to evaluate the accuracy of our new layer. - evaluation_step, prediction = add_evaluation_step( - final_tensor, ground_truth_input) - - # Merge all the summaries and write them out to /tmp/retrain_logs (by default) - merged = tf.summary.merge_all() - train_writer = tf.summary.FileWriter(FLAGS.summaries_dir + '/train', - sess.graph) - validation_writer = tf.summary.FileWriter(FLAGS.summaries_dir + '/validation') - - # Set up all our weights to their initial default values. - init = tf.global_variables_initializer() - sess.run(init) - - # Run the training for as many cycles as requested on the command line. - for i in range(FLAGS.how_many_training_steps): - # Get a batch of input bottleneck values, either calculated fresh every time - # with distortions applied, or from the cache stored on disk. + sess = tf.compat.v1.Session() + if do_distort_images: - train_bottlenecks, train_ground_truth = get_random_distorted_bottlenecks( - sess, image_lists, FLAGS.train_batch_size, 'training', - FLAGS.image_dir, distorted_jpeg_data_tensor, - distorted_image_tensor, resized_image_tensor, bottleneck_tensor) + # We will be applying distortions, so setup the operations we'll need. + distorted_jpeg_data_tensor, distorted_image_tensor = add_input_distortions( + FLAGS.flip_left_right, FLAGS.random_crop, FLAGS.random_scale, + FLAGS.random_brightness) else: - train_bottlenecks, train_ground_truth, _ = get_random_cached_bottlenecks( - sess, image_lists, FLAGS.train_batch_size, 'training', - FLAGS.bottleneck_dir, FLAGS.image_dir, jpeg_data_tensor, - bottleneck_tensor) - # Feed the bottlenecks and ground truth into the graph, and run a training - # step. Capture training summaries for TensorBoard with the `merged` op. - train_summary, _ = sess.run([merged, train_step], - feed_dict={bottleneck_input: train_bottlenecks, - ground_truth_input: train_ground_truth}) - train_writer.add_summary(train_summary, i) - - # Every so often, print out how well the graph is training. - is_last_step = (i + 1 == FLAGS.how_many_training_steps) - if (i % FLAGS.eval_step_interval) == 0 or is_last_step: - train_accuracy, cross_entropy_value = sess.run( - [evaluation_step, cross_entropy], - feed_dict={bottleneck_input: train_bottlenecks, - ground_truth_input: train_ground_truth}) - print('%s: Step %d: Train accuracy = %.1f%%' % (datetime.now(), i, - train_accuracy * 100)) - print('%s: Step %d: Cross entropy = %f' % (datetime.now(), i, - cross_entropy_value)) - validation_bottlenecks, validation_ground_truth, _ = ( - get_random_cached_bottlenecks( - sess, image_lists, FLAGS.validation_batch_size, 'validation', - FLAGS.bottleneck_dir, FLAGS.image_dir, jpeg_data_tensor, - bottleneck_tensor)) - # Run a validation step and capture training summaries for TensorBoard - # with the `merged` op. - validation_summary, validation_accuracy = sess.run( - [merged, evaluation_step], - feed_dict={bottleneck_input: validation_bottlenecks, - ground_truth_input: validation_ground_truth}) - validation_writer.add_summary(validation_summary, i) - print('%s: Step %d: Validation accuracy = %.1f%% (N=%d)' % - (datetime.now(), i, validation_accuracy * 100, - len(validation_bottlenecks))) - - # We've completed all our training, so run a final test evaluation on - # some new images we haven't used before. - test_bottlenecks, test_ground_truth, test_filenames = ( - get_random_cached_bottlenecks(sess, image_lists, FLAGS.test_batch_size, - 'testing', FLAGS.bottleneck_dir, - FLAGS.image_dir, jpeg_data_tensor, - bottleneck_tensor)) - test_accuracy, predictions = sess.run( - [evaluation_step, prediction], - feed_dict={bottleneck_input: test_bottlenecks, - ground_truth_input: test_ground_truth}) - print('Final test accuracy = %.1f%% (N=%d)' % ( - test_accuracy * 100, len(test_bottlenecks))) - - if FLAGS.print_misclassified_test_images: - print('=== MISCLASSIFIED TEST IMAGES ===') - for i, test_filename in enumerate(test_filenames): - if predictions[i] != test_ground_truth[i].argmax(): - print('%70s %s' % (test_filename, - list(image_lists.keys())[predictions[i]])) - - # Write out the trained graph and labels with the weights stored as constants. - output_graph_def = graph_util.convert_variables_to_constants( - sess, graph.as_graph_def(), [FLAGS.final_tensor_name]) - with gfile.FastGFile(FLAGS.output_graph, 'wb') as f: - f.write(output_graph_def.SerializeToString()) - with gfile.FastGFile(FLAGS.output_labels, 'w') as f: - f.write('\n'.join(image_lists.keys()) + '\n') + # We'll make sure we've calculated the 'bottleneck' image summaries and + # cached them on disk. + cache_bottlenecks(sess, image_lists, FLAGS.image_dir, FLAGS.bottleneck_dir, + jpeg_data_tensor, bottleneck_tensor) + + # Add the new layer that we'll be training. + (train_step, cross_entropy, bottleneck_input, ground_truth_input, + final_tensor) = add_final_training_ops(len(image_lists.keys()), + FLAGS.final_tensor_name, + bottleneck_tensor) + + # Create the operations we need to evaluate the accuracy of our new layer. + evaluation_step, prediction = add_evaluation_step( + final_tensor, ground_truth_input) + + # Merge all the summaries and write them out to /tmp/retrain_logs (by default) + merged = tf.compat.v1.summary.merge_all() + train_writer = tf.compat.v1.summary.FileWriter(FLAGS.summaries_dir + '/train', + sess.graph) + validation_writer = tf.compat.v1.summary.FileWriter( + FLAGS.summaries_dir + '/validation') + + # Set up all our weights to their initial default values. + init = tf.compat.v1.global_variables_initializer() + sess.run(init) + + # Run the training for as many cycles as requested on the command line. + for i in range(FLAGS.how_many_training_steps): + # Get a batch of input bottleneck values, either calculated fresh every time + # with distortions applied, or from the cache stored on disk. + if do_distort_images: + train_bottlenecks, train_ground_truth = get_random_distorted_bottlenecks( + sess, image_lists, FLAGS.train_batch_size, 'training', + FLAGS.image_dir, distorted_jpeg_data_tensor, + distorted_image_tensor, resized_image_tensor, bottleneck_tensor) + else: + train_bottlenecks, train_ground_truth, _ = get_random_cached_bottlenecks( + sess, image_lists, FLAGS.train_batch_size, 'training', + FLAGS.bottleneck_dir, FLAGS.image_dir, jpeg_data_tensor, + bottleneck_tensor) + # Feed the bottlenecks and ground truth into the graph, and run a training + # step. Capture training summaries for TensorBoard with the `merged` op. + train_summary, _ = sess.run([merged, train_step], + feed_dict={bottleneck_input: train_bottlenecks, + ground_truth_input: train_ground_truth}) + train_writer.add_summary(train_summary, i) + + # Every so often, print out how well the graph is training. + is_last_step = (i + 1 == FLAGS.how_many_training_steps) + if (i % FLAGS.eval_step_interval) == 0 or is_last_step: + train_accuracy, cross_entropy_value = sess.run( + [evaluation_step, cross_entropy], + feed_dict={bottleneck_input: train_bottlenecks, + ground_truth_input: train_ground_truth}) + print('%s: Step %d: Train accuracy = %.1f%%' % (datetime.now(), i, + train_accuracy * 100)) + print('%s: Step %d: Cross entropy = %f' % (datetime.now(), i, + cross_entropy_value)) + validation_bottlenecks, validation_ground_truth, _ = ( + get_random_cached_bottlenecks( + sess, image_lists, FLAGS.validation_batch_size, 'validation', + FLAGS.bottleneck_dir, FLAGS.image_dir, jpeg_data_tensor, + bottleneck_tensor)) + # Run a validation step and capture training summaries for TensorBoard + # with the `merged` op. + validation_summary, validation_accuracy = sess.run( + [merged, evaluation_step], + feed_dict={bottleneck_input: validation_bottlenecks, + ground_truth_input: validation_ground_truth}) + validation_writer.add_summary(validation_summary, i) + print('%s: Step %d: Validation accuracy = %.1f%% (N=%d)' % + (datetime.now(), i, validation_accuracy * 100, + len(validation_bottlenecks))) + + # We've completed all our training, so run a final test evaluation on + # some new images we haven't used before. + test_bottlenecks, test_ground_truth, test_filenames = ( + get_random_cached_bottlenecks(sess, image_lists, FLAGS.test_batch_size, + 'testing', FLAGS.bottleneck_dir, + FLAGS.image_dir, jpeg_data_tensor, + bottleneck_tensor)) + test_accuracy, predictions = sess.run( + [evaluation_step, prediction], + feed_dict={bottleneck_input: test_bottlenecks, + ground_truth_input: test_ground_truth}) + print('Final test accuracy = %.1f%% (N=%d)' % ( + test_accuracy * 100, len(test_bottlenecks))) + + if FLAGS.print_misclassified_test_images: + print('=== MISCLASSIFIED TEST IMAGES ===') + for i, test_filename in enumerate(test_filenames): + if predictions[i] != test_ground_truth[i].argmax(): + print('%70s %s' % (test_filename, + list(image_lists.keys())[predictions[i]])) + + # Write out the trained graph and labels with the weights stored as constants. + output_graph_def = graph_util.convert_variables_to_constants( + sess, graph.as_graph_def(), [FLAGS.final_tensor_name]) + with gfile.FastGFile(FLAGS.output_graph, 'wb') as f: + f.write(output_graph_def.SerializeToString()) + with gfile.FastGFile(FLAGS.output_labels, 'w') as f: + f.write('\n'.join(image_lists.keys()) + '\n') if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.add_argument( - '--image_dir', - type=str, - default='', - help='Path to folders of labeled images.' - ) - parser.add_argument( - '--output_graph', - type=str, - default='/tmp/output_graph.pb', - help='Where to save the trained graph.' - ) - parser.add_argument( - '--output_labels', - type=str, - default='/tmp/output_labels.txt', - help='Where to save the trained graph\'s labels.' - ) - parser.add_argument( - '--summaries_dir', - type=str, - default='/tmp/retrain_logs', - help='Where to save summary logs for TensorBoard.' - ) - parser.add_argument( - '--how_many_training_steps', - type=int, - default=4000, - help='How many training steps to run before ending.' - ) - parser.add_argument( - '--learning_rate', - type=float, - default=0.01, - help='How large a learning rate to use when training.' - ) - parser.add_argument( - '--testing_percentage', - type=int, - default=10, - help='What percentage of images to use as a test set.' - ) - parser.add_argument( - '--validation_percentage', - type=int, - default=10, - help='What percentage of images to use as a validation set.' - ) - parser.add_argument( - '--eval_step_interval', - type=int, - default=10, - help='How often to evaluate the training results.' - ) - parser.add_argument( - '--train_batch_size', - type=int, - default=100, - help='How many images to train on at a time.' - ) - parser.add_argument( - '--test_batch_size', - type=int, - default=-1, - help="""\ + parser = argparse.ArgumentParser() + parser.add_argument( + '--image_dir', + type=str, + default='', + help='Path to folders of labeled images.' + ) + parser.add_argument( + '--output_graph', + type=str, + default='/tmp/output_graph.pb', + help='Where to save the trained graph.' + ) + parser.add_argument( + '--output_labels', + type=str, + default='/tmp/output_labels.txt', + help='Where to save the trained graph\'s labels.' + ) + parser.add_argument( + '--summaries_dir', + type=str, + default='/tmp/retrain_logs', + help='Where to save summary logs for TensorBoard.' + ) + parser.add_argument( + '--how_many_training_steps', + type=int, + default=4000, + help='How many training steps to run before ending.' + ) + parser.add_argument( + '--learning_rate', + type=float, + default=0.01, + help='How large a learning rate to use when training.' + ) + parser.add_argument( + '--testing_percentage', + type=int, + default=10, + help='What percentage of images to use as a test set.' + ) + parser.add_argument( + '--validation_percentage', + type=int, + default=10, + help='What percentage of images to use as a validation set.' + ) + parser.add_argument( + '--eval_step_interval', + type=int, + default=10, + help='How often to evaluate the training results.' + ) + parser.add_argument( + '--train_batch_size', + type=int, + default=100, + help='How many images to train on at a time.' + ) + parser.add_argument( + '--test_batch_size', + type=int, + default=-1, + help="""\ How many images to test on. This test set is only used once, to evaluate the final accuracy of the model after training completes. A value of -1 causes the entire test set to be used, which leads to more stable results across runs.\ """ - ) - parser.add_argument( - '--validation_batch_size', - type=int, - default=100, - help="""\ + ) + parser.add_argument( + '--validation_batch_size', + type=int, + default=100, + help="""\ How many images to use in an evaluation batch. This validation set is used much more often than the test set, and is an early indicator of how accurate the model is during training. @@ -990,73 +1007,73 @@ def main(_): more stable results across training iterations, but may be slower on large training sets.\ """ - ) - parser.add_argument( - '--print_misclassified_test_images', - default=False, - help="""\ + ) + parser.add_argument( + '--print_misclassified_test_images', + default=False, + help="""\ Whether to print out a list of all misclassified test images.\ """, - action='store_true' - ) - parser.add_argument( - '--model_dir', - type=str, - default='/tmp/imagenet', - help="""\ + action='store_true' + ) + parser.add_argument( + '--model_dir', + type=str, + default='/tmp/imagenet', + help="""\ Path to classify_image_graph_def.pb, imagenet_synset_to_human_label_map.txt, and imagenet_2012_challenge_label_map_proto.pbtxt.\ """ - ) - parser.add_argument( - '--bottleneck_dir', - type=str, - default='/tmp/bottleneck', - help='Path to cache bottleneck layer values as files.' - ) - parser.add_argument( - '--final_tensor_name', - type=str, - default='final_result', - help="""\ + ) + parser.add_argument( + '--bottleneck_dir', + type=str, + default='/tmp/bottleneck', + help='Path to cache bottleneck layer values as files.' + ) + parser.add_argument( + '--final_tensor_name', + type=str, + default='final_result', + help="""\ The name of the output classification layer in the retrained graph.\ """ - ) - parser.add_argument( - '--flip_left_right', - default=False, - help="""\ + ) + parser.add_argument( + '--flip_left_right', + default=False, + help="""\ Whether to randomly flip half of the training images horizontally.\ """, - action='store_true' - ) - parser.add_argument( - '--random_crop', - type=int, - default=0, - help="""\ + action='store_true' + ) + parser.add_argument( + '--random_crop', + type=int, + default=0, + help="""\ A percentage determining how much of a margin to randomly crop off the training images.\ """ - ) - parser.add_argument( - '--random_scale', - type=int, - default=0, - help="""\ + ) + parser.add_argument( + '--random_scale', + type=int, + default=0, + help="""\ A percentage determining how much to randomly scale up the size of the training images by.\ """ - ) - parser.add_argument( - '--random_brightness', - type=int, - default=0, - help="""\ + ) + parser.add_argument( + '--random_brightness', + type=int, + default=0, + help="""\ A percentage determining how much to randomly multiply the training image input pixels up or down by.\ """ - ) - FLAGS, unparsed = parser.parse_known_args() - tf.app.run(main=main, argv=[sys.argv[0]] + unparsed) + ) + FLAGS, unparsed = parser.parse_known_args() + tf.compat.v1.app.run(main=main, argv=[sys.argv[0]] + unparsed) diff --git a/hub/examples/image_retraining/retrained_graph.pb b/hub/examples/image_retraining/retrained_graph.pb index bced44eb..f7ddb116 100644 Binary files a/hub/examples/image_retraining/retrained_graph.pb and b/hub/examples/image_retraining/retrained_graph.pb differ diff --git a/hub/examples/image_retraining/retrained_labels.txt b/hub/examples/image_retraining/retrained_labels.txt index f95cb5ef..f5721dc7 100644 --- a/hub/examples/image_retraining/retrained_labels.txt +++ b/hub/examples/image_retraining/retrained_labels.txt @@ -1,12 +1,13 @@ asteroids +earth +elliptical +jupiter +mars mercury -venus moon neptune -spiral +pluto saturn -jupiter -elliptical -earth +spiral uranus -mars +venus diff --git a/hub/examples/image_retraining/test_data/pluto/000.jpg b/hub/examples/image_retraining/test_data/pluto/000.jpg new file mode 100644 index 00000000..46f9c652 Binary files /dev/null and b/hub/examples/image_retraining/test_data/pluto/000.jpg differ diff --git a/hub/examples/image_retraining/test_data/pluto/001.jpg b/hub/examples/image_retraining/test_data/pluto/001.jpg new file mode 100644 index 00000000..8b807a82 Binary files /dev/null and b/hub/examples/image_retraining/test_data/pluto/001.jpg differ diff --git a/hub/examples/image_retraining/test_data/pluto/002.jpg b/hub/examples/image_retraining/test_data/pluto/002.jpg new file mode 100644 index 00000000..6ce4c8f0 Binary files /dev/null and b/hub/examples/image_retraining/test_data/pluto/002.jpg differ diff --git a/hub/examples/image_retraining/test_data/pluto/003.jpg b/hub/examples/image_retraining/test_data/pluto/003.jpg new file mode 100644 index 00000000..782a5569 Binary files /dev/null and b/hub/examples/image_retraining/test_data/pluto/003.jpg differ diff --git a/hub/examples/image_retraining/test_data/pluto/004.jpg b/hub/examples/image_retraining/test_data/pluto/004.jpg new file mode 100644 index 00000000..c3f05704 Binary files /dev/null and b/hub/examples/image_retraining/test_data/pluto/004.jpg differ diff --git a/hub/examples/image_retraining/test_data/pluto/005.jpg b/hub/examples/image_retraining/test_data/pluto/005.jpg new file mode 100644 index 00000000..6331dac1 Binary files /dev/null and b/hub/examples/image_retraining/test_data/pluto/005.jpg differ diff --git a/hub/examples/image_retraining/test_data/pluto/006.jpg b/hub/examples/image_retraining/test_data/pluto/006.jpg new file mode 100644 index 00000000..38f24190 Binary files /dev/null and b/hub/examples/image_retraining/test_data/pluto/006.jpg differ diff --git a/hub/examples/image_retraining/test_data/pluto/007.jpg b/hub/examples/image_retraining/test_data/pluto/007.jpg new file mode 100644 index 00000000..27252b9a Binary files /dev/null and b/hub/examples/image_retraining/test_data/pluto/007.jpg differ diff --git a/hub/examples/image_retraining/test_data/pluto/008.jpg b/hub/examples/image_retraining/test_data/pluto/008.jpg new file mode 100644 index 00000000..c64cfdd9 Binary files /dev/null and b/hub/examples/image_retraining/test_data/pluto/008.jpg differ diff --git a/hub/examples/image_retraining/test_data/pluto/009.jpg b/hub/examples/image_retraining/test_data/pluto/009.jpg new file mode 100644 index 00000000..6d52bc15 Binary files /dev/null and b/hub/examples/image_retraining/test_data/pluto/009.jpg differ diff --git a/hub/examples/image_retraining/test_data/pluto/010.jpg b/hub/examples/image_retraining/test_data/pluto/010.jpg new file mode 100644 index 00000000..de1d519d Binary files /dev/null and b/hub/examples/image_retraining/test_data/pluto/010.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/000.jpg b/hub/examples/image_retraining/training_data/pluto/000.jpg new file mode 100644 index 00000000..eddf77ab Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/000.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/001.jpg b/hub/examples/image_retraining/training_data/pluto/001.jpg new file mode 100644 index 00000000..878c2794 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/001.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/002.jpg b/hub/examples/image_retraining/training_data/pluto/002.jpg new file mode 100644 index 00000000..da450cfa Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/002.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/003.jpg b/hub/examples/image_retraining/training_data/pluto/003.jpg new file mode 100644 index 00000000..b4038bb7 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/003.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/004.jpg b/hub/examples/image_retraining/training_data/pluto/004.jpg new file mode 100644 index 00000000..ce790366 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/004.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/005.jpg b/hub/examples/image_retraining/training_data/pluto/005.jpg new file mode 100644 index 00000000..6ddeff8e Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/005.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/006.jpg b/hub/examples/image_retraining/training_data/pluto/006.jpg new file mode 100644 index 00000000..086d7a82 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/006.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/007.jpg b/hub/examples/image_retraining/training_data/pluto/007.jpg new file mode 100644 index 00000000..5195791c Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/007.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/008.jpg b/hub/examples/image_retraining/training_data/pluto/008.jpg new file mode 100644 index 00000000..209e6f4b Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/008.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/009.jpg b/hub/examples/image_retraining/training_data/pluto/009.jpg new file mode 100644 index 00000000..14c530ac Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/009.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/010.jpg b/hub/examples/image_retraining/training_data/pluto/010.jpg new file mode 100644 index 00000000..3a4930f0 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/010.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/011.jpg b/hub/examples/image_retraining/training_data/pluto/011.jpg new file mode 100644 index 00000000..ed3c8f07 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/011.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/012.jpg b/hub/examples/image_retraining/training_data/pluto/012.jpg new file mode 100644 index 00000000..de1d519d Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/012.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/013.jpg b/hub/examples/image_retraining/training_data/pluto/013.jpg new file mode 100644 index 00000000..77651351 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/013.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/014.jpg b/hub/examples/image_retraining/training_data/pluto/014.jpg new file mode 100644 index 00000000..f8a389a0 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/014.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/015.jpg b/hub/examples/image_retraining/training_data/pluto/015.jpg new file mode 100644 index 00000000..076159d8 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/015.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/016.jpg b/hub/examples/image_retraining/training_data/pluto/016.jpg new file mode 100644 index 00000000..a1097859 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/016.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/017.jpg b/hub/examples/image_retraining/training_data/pluto/017.jpg new file mode 100644 index 00000000..6a10ad76 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/017.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/018.jpg b/hub/examples/image_retraining/training_data/pluto/018.jpg new file mode 100644 index 00000000..8d1c01cc Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/018.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/019.jpg b/hub/examples/image_retraining/training_data/pluto/019.jpg new file mode 100644 index 00000000..15adfbd9 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/019.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/020.jpg b/hub/examples/image_retraining/training_data/pluto/020.jpg new file mode 100644 index 00000000..391f8c43 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/020.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/021.jpg b/hub/examples/image_retraining/training_data/pluto/021.jpg new file mode 100644 index 00000000..6914531e Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/021.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/022.jpg b/hub/examples/image_retraining/training_data/pluto/022.jpg new file mode 100644 index 00000000..99f64698 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/022.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/023.jpg b/hub/examples/image_retraining/training_data/pluto/023.jpg new file mode 100644 index 00000000..ef2a0d31 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/023.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/024.jpg b/hub/examples/image_retraining/training_data/pluto/024.jpg new file mode 100644 index 00000000..8d65114d Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/024.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/025.jpg b/hub/examples/image_retraining/training_data/pluto/025.jpg new file mode 100644 index 00000000..1a2d3521 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/025.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/026.jpg b/hub/examples/image_retraining/training_data/pluto/026.jpg new file mode 100644 index 00000000..b326df89 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/026.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/027.jpg b/hub/examples/image_retraining/training_data/pluto/027.jpg new file mode 100644 index 00000000..6f4f48d9 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/027.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/028.jpg b/hub/examples/image_retraining/training_data/pluto/028.jpg new file mode 100644 index 00000000..d9cd6d99 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/028.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/029.jpg b/hub/examples/image_retraining/training_data/pluto/029.jpg new file mode 100644 index 00000000..c82b7ded Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/029.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/030.jpg b/hub/examples/image_retraining/training_data/pluto/030.jpg new file mode 100644 index 00000000..f44e4732 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/030.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/031.jpg b/hub/examples/image_retraining/training_data/pluto/031.jpg new file mode 100644 index 00000000..4da9c259 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/031.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/032.jpg b/hub/examples/image_retraining/training_data/pluto/032.jpg new file mode 100644 index 00000000..aedb07b9 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/032.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/033.jpg b/hub/examples/image_retraining/training_data/pluto/033.jpg new file mode 100644 index 00000000..6a10ad76 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/033.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/034.jpg b/hub/examples/image_retraining/training_data/pluto/034.jpg new file mode 100644 index 00000000..c82b7ded Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/034.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/035.jpg b/hub/examples/image_retraining/training_data/pluto/035.jpg new file mode 100644 index 00000000..a4fa0058 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/035.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/036.jpg b/hub/examples/image_retraining/training_data/pluto/036.jpg new file mode 100644 index 00000000..3871be75 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/036.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/037.jpg b/hub/examples/image_retraining/training_data/pluto/037.jpg new file mode 100644 index 00000000..c8b57414 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/037.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/038.jpg b/hub/examples/image_retraining/training_data/pluto/038.jpg new file mode 100644 index 00000000..782a5569 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/038.jpg differ diff --git a/hub/examples/image_retraining/training_data/pluto/039.jpg b/hub/examples/image_retraining/training_data/pluto/039.jpg new file mode 100644 index 00000000..dcd76217 Binary files /dev/null and b/hub/examples/image_retraining/training_data/pluto/039.jpg differ diff --git a/hub/examples/image_retraining/training_data/uranus/46.jpg b/hub/examples/image_retraining/training_data/uranus/46.jpg new file mode 100644 index 00000000..716192da Binary files /dev/null and b/hub/examples/image_retraining/training_data/uranus/46.jpg differ diff --git a/hub/examples/image_retraining/training_data/uranus/47.jpg b/hub/examples/image_retraining/training_data/uranus/47.jpg new file mode 100644 index 00000000..a9793a60 Binary files /dev/null and b/hub/examples/image_retraining/training_data/uranus/47.jpg differ diff --git a/hub/examples/image_retraining/training_data/uranus/48.jpg b/hub/examples/image_retraining/training_data/uranus/48.jpg new file mode 100644 index 00000000..ec10ac8c Binary files /dev/null and b/hub/examples/image_retraining/training_data/uranus/48.jpg differ diff --git a/hub/examples/image_retraining/training_data/uranus/49.jpg b/hub/examples/image_retraining/training_data/uranus/49.jpg new file mode 100644 index 00000000..acf4b7b4 Binary files /dev/null and b/hub/examples/image_retraining/training_data/uranus/49.jpg differ diff --git a/hub/examples/image_retraining/training_data/uranus/50.jpg b/hub/examples/image_retraining/training_data/uranus/50.jpg new file mode 100644 index 00000000..4c574b1f Binary files /dev/null and b/hub/examples/image_retraining/training_data/uranus/50.jpg differ diff --git a/hub/examples/image_retraining/training_data/uranus/51.jpg b/hub/examples/image_retraining/training_data/uranus/51.jpg new file mode 100644 index 00000000..cec3fdf9 Binary files /dev/null and b/hub/examples/image_retraining/training_data/uranus/51.jpg differ diff --git a/hub/examples/image_retraining/training_data/uranus/52.jpg b/hub/examples/image_retraining/training_data/uranus/52.jpg new file mode 100644 index 00000000..279cf3b6 Binary files /dev/null and b/hub/examples/image_retraining/training_data/uranus/52.jpg differ diff --git a/hub/examples/image_retraining/training_data/uranus/53.jpg b/hub/examples/image_retraining/training_data/uranus/53.jpg new file mode 100644 index 00000000..454ece7c Binary files /dev/null and b/hub/examples/image_retraining/training_data/uranus/53.jpg differ diff --git a/requirements.txt b/requirements.txt index 0418904c..619e24a0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ pyyaml -tensorflow-cpu==2.9.3 +tensorflow-cpu wikipedia flask flask-wtf