Example #1
0
def main(argv=None):

    if FLAGS.mode == 'train':
        if FLAGS.network == None:
            raise ValueError('Please supply a network(graph) file.')

        if FLAGS.ckpt == None:
            raise ValueError('Please supply a ckpt dir.')

        if FLAGS.data_dir == None:
            raise ValueError('Please supply a data_dir.')

        if FLAGS.continue_train != True and tf.gfile.Exists(FLAGS.ckpt):
            tf.gfile.DeleteRecursively(FLAGS.ckpt)

        if tf.gfile.Exists(FLAGS.ckpt) == False:
            tf.gfile.MakeDirs(FLAGS.ckpt)

        # WRITE META-DATA
        FLAGS.num_of_classes = _num_of_folders(FLAGS.data_dir)
        with open(os.path.join(FLAGS.ckpt, "flags.txt"), "w") as text_file:
            flags = json.dumps(FLAGS.__dict__["__flags"])
            text_file.write(flags)

        train(FLAGS.data_dir)

    elif FLAGS.mode == 'eval':
        if FLAGS.ckpt == None:
            raise ValueError('Please supply a ckpt dir.')

        if FLAGS.data_dir == None:
            raise ValueError('Please supply a data_dir.')

        if FLAGS.ckpt == FLAGS.event_dir:
            FLAGS.event_dir = os.path.join(FLAGS.ckpt, 'event')
            #raise ValueError('event_dir cannot be same with ckpt dir!!!')

        # READ META-DATA
        with open(os.path.join(FLAGS.ckpt, "flags.txt"), "r") as text_file:
            txt = text_file.read()
            flags = json.loads(txt)
            FLAGS.num_of_classes = flags['num_of_classes']
            FLAGS.image_size = flags['image_size']
            FLAGS.image_crop_size = flags['image_crop_size']
            FLAGS.grayscale = flags['grayscale']
            FLAGS.use_fp16 = flags['use_fp16']
            FLAGS.network = flags['network']
            FLAGS.learning_rate = flags['learning_rate']

        evaluate(FLAGS.data_dir)

    elif FLAGS.mode == 'export':
        if FLAGS.ckpt == None:
            raise ValueError('Please supply a ckpt dir.')

        # READ META-DATA
        with open(os.path.join(FLAGS.ckpt, "flags.txt"), "r") as text_file:
            txt = text_file.read()
            flags = json.loads(txt)
            FLAGS.num_of_classes = flags['num_of_classes']
            FLAGS.image_size = flags['image_size']
            FLAGS.image_crop_size = flags['image_crop_size']
            FLAGS.grayscale = flags['grayscale']
            FLAGS.use_fp16 = flags['use_fp16']
            FLAGS.network = flags['network']
            FLAGS.learning_rate = flags['learning_rate']

        export()

    elif FLAGS.mode == 'single':
        if FLAGS.ckpt == None:
            raise ValueError('Please supply a ckpt dir.')

        if FLAGS.image == None:
            raise ValueError('Please supply a image file.')

        # READ META-DATA
        with open(os.path.join(FLAGS.ckpt, "flags.txt"), "r") as text_file:
            txt = text_file.read()
            flags = json.loads(txt)
            FLAGS.num_of_classes = flags['num_of_classes']
            FLAGS.image_size = flags['image_size']
            FLAGS.image_crop_size = flags['image_crop_size']
            FLAGS.grayscale = flags['grayscale']
            FLAGS.use_fp16 = flags['use_fp16']
            FLAGS.network = flags['network']
            FLAGS.learning_rate = flags['learning_rate']

        evaluate_single()

    elif FLAGS.mode == 'single_with_pb':
        if FLAGS.pb == None:
            raise ValueError('Please supply a pb file.')

        if FLAGS.image == None:
            raise ValueError('Please supply a image file.')

        single_with_pb()

    elif FLAGS.mode == 'eval_with_pb':
        if FLAGS.pb == None:
            raise ValueError('Please supply a pb file.')

        if FLAGS.data_dir == None:
            raise ValueError('Please supply a data_dir.')

        evaluate_with_pb(FLAGS.pb, FLAGS.data_dir)

    elif FLAGS.mode == 'resize':
        if FLAGS.data_dir == None:
            raise ValueError('Please supply a data_dir.')

        if FLAGS.out_dir == None:
            raise ValueError('Please supply a out_dir.')

        if tf.gfile.Exists(FLAGS.out_dir):
            tf.gfile.DeleteRecursively(FLAGS.out_dir)

        tf.gfile.MakeDirs(FLAGS.out_dir)

        convert_count = CNN.convert_img_to_square_with_pad(
            FLAGS.data_dir, FLAGS.out_dir)
        print('Complete Succefuly.')
        print('Output Dir: {}'.format(FLAGS.out_dir))
        print('Total Count: {}'.format(convert_count))

    elif FLAGS.mode == 'map_export':
        if FLAGS.pb == None:
            raise ValueError('Please supply pb.')
        if tf.gfile.Exists(FLAGS.pb) == False:
            raise ValueError('Pb not exists.')

        if FLAGS.out_node == None:
            raise ValueError('Please supply out_node.')

        if FLAGS.data_dir == None:
            raise ValueError('Please supply data_dir.')
        if tf.gfile.Exists(FLAGS.data_dir) == False:
            raise ValueError('data_dir not exists.')

        if FLAGS.map_dir == None:
            raise ValueError('Please supply map_dir.')
        if tf.gfile.Exists(FLAGS.map_dir) == False:
            raise ValueError('map_dir not exists.')

        map_export(FLAGS.pb, FLAGS.out_node, FLAGS.data_dir, FLAGS.map_dir)

    elif FLAGS.mode == 'map_inference':
        if FLAGS.pb == None:
            raise ValueError('Please supply pb.')
        if tf.gfile.Exists(FLAGS.pb) == False:
            raise ValueError('Pb not exists.')

        if FLAGS.out_node == None:
            raise ValueError('Please supply out_node.')

        if FLAGS.image == None:
            raise ValueError('Please supply image.')
        if tf.gfile.Exists(FLAGS.image) == False:
            raise ValueError('image not exists.')

        if FLAGS.map_dir == None:
            raise ValueError('Please supply map_dir.')
        if tf.gfile.Exists(FLAGS.map_dir) == False:
            raise ValueError('map_dir not exists.')

        if FLAGS.label == None:
            print(bcolors.WARNING + 'WARNING: Label is not supplied!!' +
                  bcolors.ENDC)
            #raise ValueError('Please supply label.')

        map_inference(FLAGS.pb, FLAGS.out_node, FLAGS.image, FLAGS.map_dir,
                      FLAGS.label)

    elif FLAGS.mode == 'map_distance':
        if FLAGS.pb == None:
            raise ValueError('Please supply pb.')
        if tf.gfile.Exists(FLAGS.pb) == False:
            raise ValueError('Pb not exists.')

        if FLAGS.out_node == None:
            raise ValueError('Please supply out_node.')

        if FLAGS.image == None:
            raise ValueError('Please supply image.')
        if tf.gfile.Exists(FLAGS.image) == False:
            raise ValueError('image not exists.')

        if FLAGS.map_dir == None:
            raise ValueError('Please supply map_dir.')
        if tf.gfile.Exists(FLAGS.map_dir) == False:
            raise ValueError('map_dir not exists.')

        if FLAGS.label == None:
            raise ValueError('Please supply label.')

        if FLAGS.target_label == None:
            raise ValueError('Please supply target_label.')

        map_distance(FLAGS.pb, FLAGS.out_node, FLAGS.image, FLAGS.map_dir,
                     FLAGS.label, FLAGS.target_label)

    elif FLAGS.mode == 'auto':
        if FLAGS.pb == None:
            raise ValueError('Please supply pb.')
        if tf.gfile.Exists(FLAGS.pb) == False:
            raise ValueError('Pb not exists.')

        if FLAGS.data_dir == None:
            raise ValueError('Please supply data_dir.')
        if tf.gfile.Exists(FLAGS.data_dir) == False:
            raise ValueError('data_dir not exists.')

        if FLAGS.out_dir == None:
            raise ValueError('Please supply out_dir.')
        if tf.gfile.Exists(FLAGS.out_dir):
            tf.gfile.DeleteRecursively(FLAGS.out_dir)

        tf.gfile.MakeDirs(FLAGS.out_dir)

        auto_classifier(FLAGS.pb, FLAGS.data_dir, FLAGS.out_dir)
Example #2
0
def main(argv=None):

    if FLAGS.mode == 'train':
        if FLAGS.network == None:
            raise ValueError('Please supply a network(graph) file.')

        if FLAGS.ckpt == None:
            raise ValueError('Please supply a ckpt dir.')

        if FLAGS.data_dir == None:
            raise ValueError('Please supply a data_dir.')

        if FLAGS.continue_train != True and tf.gfile.Exists(FLAGS.ckpt):
            tf.gfile.DeleteRecursively(FLAGS.ckpt)

        if tf.gfile.Exists(FLAGS.ckpt) == False:
            tf.gfile.MakeDirs(FLAGS.ckpt)

        # WRITE META-DATA
#        FLAGS.num_of_classes = _num_of_folders(FLAGS.data_dir)
#       with open(os.path.join(FLAGS.ckpt, "flags.txt"), "w") as text_file:
#          flags = json.dumps(FLAGS.__dict__["flags"])
#         text_file.write(flags)

        train()

    elif FLAGS.mode == 'eval':
        if FLAGS.ckpt == None:
            raise ValueError('Please supply a ckpt dir.')

        if FLAGS.data_dir == None:
            raise ValueError('Please supply a data_dir.')

        if FLAGS.ckpt == FLAGS.event_dir:
            raise ValueError('event_dir cannot be same with ckpt dir!!!')

            # READ META-DATA
            #         with open(os.path.join(FLAGS.ckpt, "flags.txt"), "r") as text_file:
            #             txt = text_file.read()
            #             flags = json.loads(txt)
            FLAGS.num_of_classes = 2
            FLAGS.image_size = 100
            FLAGS.image_crop_size = 80
            FLAGS.grayscale = True
            FLAGS.use_fp16 = False
            FLAGS.network = 'nn_default'
            FLAGS.learning_rate = 0.005

        evaluate()

    elif FLAGS.mode == 'export':
        if FLAGS.ckpt == None:
            raise ValueError('Please supply a ckpt dir.')

        # READ META-DATA
#         with open(os.path.join(FLAGS.ckpt, "flags.txt"), "r") as text_file:
#             txt = text_file.read()
#             flags = json.loads(txt)
#             FLAGS.num_of_classes = flags['num_of_classes']
#             FLAGS.image_size = flags['image_size']
#             FLAGS.image_crop_size = flags['image_crop_size']
#             FLAGS.grayscale = flags['grayscale']
#             FLAGS.use_fp16 = flags['use_fp16']
#             FLAGS.network = flags['network']
#             FLAGS.learning_rate = flags['learning_rate']

        export()

    elif FLAGS.mode == 'export-serving':
        if FLAGS.ckpt == None:
            raise ValueError('Please supply a ckpt dir.')

        # READ META-DATA
        with open(os.path.join(FLAGS.ckpt, "flags.txt"), "r") as text_file:
            txt = text_file.read()
            flags = json.loads(txt)
            FLAGS.num_of_classes = flags['num_of_classes']
            FLAGS.image_size = flags['image_size']
            FLAGS.image_crop_size = flags['image_crop_size']
            FLAGS.grayscale = flags['grayscale']
            FLAGS.use_fp16 = flags['use_fp16']
            FLAGS.network = flags['network']
            FLAGS.learning_rate = flags['learning_rate']

        export_serving()

    elif FLAGS.mode == 'single':
        if FLAGS.ckpt == None:
            raise ValueError('Please supply a ckpt dir.')

        if FLAGS.image == None:
            raise ValueError('Please supply a image file.')

        # READ META-DATA


#         with open(os.path.join(FLAGS.ckpt, "flags.txt"), "r") as text_file:
#             txt = text_file.read()
#             flags = json.loads(txt)
#             FLAGS.num_of_classes = flags['num_of_classes']
#             FLAGS.image_size = flags['image_size']
#             FLAGS.image_crop_size = flags['image_crop_size']
#             FLAGS.grayscale = flags['grayscale']
#             FLAGS.use_fp16 = flags['use_fp16']
#             FLAGS.network = flags['network']
#             FLAGS.learning_rate = flags['learning_rate']

        evaluate_single()

    elif FLAGS.mode == 'pb':
        if FLAGS.pb == None:
            raise ValueError('Please supply a pb file.')

        if FLAGS.image == None:
            raise ValueError('Please supply a image file.')

        evaluate_with_pb()

    elif FLAGS.mode == 'resize':
        if FLAGS.data_dir == None:
            raise ValueError('Please supply a data_dir.')

        if FLAGS.out_dir == None:
            raise ValueError('Please supply a out_dir.')

        if tf.gfile.Exists(FLAGS.out_dir):
            tf.gfile.DeleteRecursively(FLAGS.out_dir)

        tf.gfile.MakeDirs(FLAGS.out_dir)

        convert_count = CNN.convert_img_to_square_with_pad(
            FLAGS.data_dir, FLAGS.out_dir)
        print('Complete Succefuly.')
        print('Output Dir: {}'.format(FLAGS.out_dir))
        print('Total Count: {}'.format(convert_count))

    elif FLAGS.mode == 'map-export':
        if FLAGS.pb == None:
            raise ValueError('Please supply pb.')
        if tf.gfile.Exists(FLAGS.pb) == False:
            raise ValueError('Pb not exists.')

        if FLAGS.out_node == None:
            raise ValueError('Please supply out_node.')

        if FLAGS.data_dir == None:
            raise ValueError('Please supply data_dir.')
        if tf.gfile.Exists(FLAGS.data_dir) == False:
            raise ValueError('data_dir not exists.')

        if FLAGS.map == None:
            raise ValueError('Please supply map.')
        if tf.gfile.Exists(FLAGS.map) == True:
            tf.gfile.Remove(FLAGS.map)

        map_export(FLAGS.pb, FLAGS.out_node, FLAGS.data_dir, FLAGS.map)

    elif FLAGS.mode == 'map-inference':
        if FLAGS.pb == None:
            raise ValueError('Please supply pb.')
        if tf.gfile.Exists(FLAGS.pb) == False:
            raise ValueError('Pb not exists.')

        if FLAGS.out_node == None:
            raise ValueError('Please supply out_node.')

        if FLAGS.image == None:
            raise ValueError('Please supply image.')
        if tf.gfile.Exists(FLAGS.image) == False:
            raise ValueError('image not exists.')

        if FLAGS.map == None:
            raise ValueError('Please supply map.')
        if tf.gfile.Exists(FLAGS.map) == False:
            raise ValueError('Map not exists.')

        map_inference(FLAGS.pb, FLAGS.out_node, FLAGS.image, FLAGS.map)