示例#1
0
def display(tfrecord_dir):
    print('Loading dataset "%s"' % tfrecord_dir)
    tfutil.init_tf({'gpu_options.allow_growth': True})
    dset = dataset.TFRecordDataset(tfrecord_dir,
                                   max_label_size='full',
                                   repeat=False,
                                   shuffle_mb=0)
    tfutil.init_uninited_vars()

    idx = 0
    while True:
        try:
            images, labels = dset.get_minibatch_np(1)
        except tf.errors.OutOfRangeError:
            break
        if idx == 0:
            print('Displaying images')
            import cv2  # pip install opencv-python
            cv2.namedWindow('dataset_tool')
            print('Press SPACE or ENTER to advance, ESC to exit')
        print('\nidx = %-8d\nlabel = %s' % (idx, labels[0].tolist()))
        cv2.imshow('dataset_tool', images[0].transpose(
            1, 2, 0)[:, :, ::-1])  # CHW => HWC, RGB => BGR
        idx += 1
        if cv2.waitKey() == 27:
            break
    print('\nDisplayed %d images.' % idx)
def generate(network_pkl, out_dir):
    if os.path.exists(out_dir):
        raise ValueError('{} already exists'.format(out_dir))
    misc.init_output_logging()
    np.random.seed(config.random_seed)
    tfutil.init_tf(config.tf_config)
    with tf.device('/gpu:0'):
        G, D, Gs = misc.load_pkl(network_pkl)
    training_set = dataset.load_dataset(data_dir=config.data_dir,
                                        verbose=True,
                                        **config.dataset)
    # grid_size, grid_reals, grid_labels, grid_latents = train.setup_snapshot_image_grid(G, training_set, **config.grid)
    number_of_images = 1000
    grid_labels = np.zeros([number_of_images, training_set.label_size],
                           dtype=training_set.label_dtype)
    grid_latents = misc.random_latents(number_of_images, G)
    total_kimg = config.train.total_kimg
    sched = train.TrainingSchedule(total_kimg * 1000, training_set,
                                   **config.sched)
    grid_fakes = Gs.run(grid_latents,
                        grid_labels,
                        minibatch_size=sched.minibatch // config.num_gpus)
    os.makedirs(out_dir)
    # print(np.min(grid_fakes), np.mean(grid_fakes), np.max(grid_fakes))
    # misc.save_image_grid(grid_fakes, 'fakes.png', drange=[-1,1], grid_size=grid_size)
    for i, img in enumerate(grid_fakes):
        img = img.transpose((1, 2, 0))
        img = np.clip(img, -1, 1)
        img = (1 + img) / 2
        img = skimage.img_as_ubyte(img)
        imageio.imwrite(os.path.join(out_dir, '{}.png'.format(i)),
                        img[..., :3])
        if img.shape[-1] > 3:
            np.save(os.path.join(out_dir, '{}.npy'.format(i)), img)
示例#3
0
def extract(tfrecord_dir, output_dir):
    print('Loading dataset "%s"' % tfrecord_dir)
    tfutil.init_tf({'gpu_options.allow_growth': True})
    dset = dataset.TFRecordDataset(tfrecord_dir,
                                   max_label_size=0,
                                   repeat=False,
                                   shuffle_mb=0)
    tfutil.init_uninited_vars()

    print('Extracting images to "%s"' % output_dir)
    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)
    idx = 0
    while True:
        if idx % 10 == 0:
            print('%d\r' % idx, end='', flush=True)
        try:
            images, labels = dset.get_minibatch_np(1)
        except tf.errors.OutOfRangeError:
            break
        if images.shape[1] == 1:
            img = PIL.Image.fromarray(images[0][0], 'L')
        else:
            img = PIL.Image.fromarray(images[0].transpose(1, 2, 0), 'RGB')
        img.save(os.path.join(output_dir, 'img%08d.png' % idx))
        idx += 1
    print('Extracted %d images.' % idx)
示例#4
0
def main():
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    parser = argparse.ArgumentParser(
        description='converter to create pytorch models')
    #parser.add_argument('--id', type=int, required=True,
    #        help='number of model to convert')
    parser.add_argument('--id',
                        type=str,
                        required=True,
                        help='number of model to convert')
    parser.add_argument('--outdir', default=None)
    args = parser.parse_args()

    # Configuration
    snapshot = None  # Default, implies last snapshot

    # Get parameters from checkpoint
    tfutil.init_tf()
    directory = misc.locate_result_subdir(args.id)
    print('Loading snapshot from %s' % directory)
    G, D, Gs = misc.load_network_pkl(args.id, snapshot)
    print(G)
    print(D)
    print(Gs)

    # import pdb; pdb.set_trace()

    # model = from_tf_parameters(Gs.variables)
    model = from_tf_parameters(Gs.vars)
    if args.outdir is None:
        args.outdir = directory
    filename = os.path.join(args.outdir, 'generator.pth')
    print('Saving pytorch model as %s' % filename)
    torch.save(model.state_dict(), filename)
示例#5
0
def hello():
    tfutil.init_tf(config.tf_config)
    with tf.device('/gpu:0'):
        G, D, Gs = misc.load_pkl(resume_network_pkl)

    imsize = Gs.output_shape[-1]
    selected_textures = misc.random_latents(1, Gs)
    selected_shapes = get_random_mask(1)
    selected_colors = get_random_color(1)
    fake_images = Gs.run(selected_textures, selected_colors, selected_shapes)

    return "DCGAN endpoint -> /predict "
示例#6
0
def predict():

    tfutil.init_tf(config.tf_config)
    with tf.device('/gpu:0'):
        G, D, Gs = misc.load_pkl(resume_network_pkl)
    imsize = Gs.output_shape[-1]

    random_masks = []
    temp = Image.open(request.files['image']).convert('L')
    temp = temp.resize((imsize, imsize))
    temp = (np.float32(temp) - 127.5) / 127.5
    temp = temp.reshape((1, 1, imsize, imsize))
    random_masks.append(temp)
    masks = np.vstack(random_masks)
    #masks = get_random_mask(1)

    ctemp = []
    ctemp.append(float(request.form['R']))
    ctemp.append(float(request.form['G']))
    ctemp.append(float(request.form['B']))
    colors = np.array([ctemp], dtype=object)
    #colors = get_random_color(1)

    texid = -1
    selected_textures = None
    if request.form['texflag'] == "true":
        selected_textures = misc.random_latents(1, Gs)
        texture_list.append(selected_textures[0])
        texid = len(texture_list) - 1
    else:
        selected_textures = np.array(
            [texture_list[int(request.form['texid'])]], dtype=object)
        texid = int(request.form['texid'])
    #selected_textures = misc.random_latents(1, Gs)

    fake_images = Gs.run(selected_textures, colors, masks)
    fake_images = convert_to_image(fake_images)
    matplotlib.image.imsave('localtemp.png', fake_images[0])

    conv_image = Image.open('localtemp.png')
    buffered = io.BytesIO()
    conv_image.save(buffered, format="PNG")
    img_str = base64.b64encode(buffered.getvalue())
    #jsonify({"image": str(img_str), "id": texid})
    return jsonify({
        "image": str(img_str)[2:-1],
        "id": texid
    })
示例#7
0
def compare(tfrecord_dir_a, tfrecord_dir_b, ignore_labels):
  max_label_size = 0 if ignore_labels else 'full'
  print('Loading dataset "%s"' % tfrecord_dir_a)
  tfutil.init_tf({'gpu_options.allow_growth': True})
  dset_a = dataset.TFRecordDataset(
      tfrecord_dir_a,
      max_label_size=max_label_size,
      repeat=False,
      shuffle_mb=0)
  print('Loading dataset "%s"' % tfrecord_dir_b)
  dset_b = dataset.TFRecordDataset(
      tfrecord_dir_b,
      max_label_size=max_label_size,
      repeat=False,
      shuffle_mb=0)
  tfutil.init_uninited_vars()

  print('Comparing datasets')
  idx = 0
  identical_images = 0
  identical_labels = 0
  while True:
    if idx % 100 == 0:
      print('%d\r' % idx, end='', flush=True)
    try:
      images_a, labels_a = dset_a.get_minibatch_np(1)
    except tf.errors.OutOfRangeError:
      images_a, labels_a = None, None
    try:
      images_b, labels_b = dset_b.get_minibatch_np(1)
    except tf.errors.OutOfRangeError:
      images_b, labels_b = None, None
    if images_a is None or images_b is None:
      if images_a is not None or images_b is not None:
        print('Datasets contain different number of images')
      break
    if images_a.shape == images_b.shape and np.all(images_a == images_b):
      identical_images += 1
    else:
      print('Image %d is different' % idx)
    if labels_a.shape == labels_b.shape and np.all(labels_a == labels_b):
      identical_labels += 1
    else:
      print('Label %d is different' % idx)
    idx += 1
  print('Identical images: %d / %d' % (identical_images, idx))
  if not ignore_labels:
    print('Identical labels: %d / %d' % (identical_labels, idx))
 def setup_before_metric():
     misc.init_output_logging()
     np.random.seed(config.random_seed)
     print('Initializing TensorFlow...')
     os.environ.update(config.env)
     tfutil.init_tf(config.tf_config)
     try:
         yield
     except Exception as e:
         print(e)
         raise e
     finally:
         sess = tf.get_default_session()
         if sess is not None:
             sess.close()
         tf.reset_default_graph()
示例#9
0
def extract(tfrecord_dir, output_dir):
    print('Loading dataset "%s"' % tfrecord_dir)
    tfutil.init_tf({'gpu_options.allow_growth': True})
    dset = dataset.TFRecordDataset(tfrecord_dir,
                                   max_label_size=0,
                                   repeat=False,
                                   shuffle_mb=0)
    tfutil.init_uninited_vars()

    ### shifting kernel
    fc_x = 0.5
    fc_y = 0.5
    im_size = 128
    kernel_loc = 2.*np.pi*fc_x * np.arange(im_size).reshape((1, im_size, 1)) + \
        2.*np.pi*fc_y * np.arange(im_size).reshape((im_size, 1, 1))
    kernel_cos = np.cos(kernel_loc)

    print('Extracting images to "%s"' % output_dir)
    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)
    idx = 0
    while True:
        if idx % 10 == 0:
            print('%d\r' % idx, end='', flush=True)
        try:
            images, labels = dset.get_minibatch_np(1)
        except tf.errors.OutOfRangeError:
            break
        if images.shape[1] == 1:
            img = PIL.Image.fromarray(images[0][0], 'L')
        else:
            img = PIL.Image.fromarray(images[0].transpose(1, 2, 0), 'RGB')

        ### shifting the image
        #img = np.asarray(img)
        #img_t = 2.* (img.astype(np.float32) / 255.) - 1.
        #img_sh = img_t * kernel_cos
        #img = (img_sh + 1.) / 2. * 255.
        #img = np.rint(img).clip(0, 255).astype(np.uint8)
        #img = PIL.Image.fromarray(img)

        img.save(os.path.join(output_dir, 'img%08d.png' % idx))
        idx += 1
    print('Extracted %d images.' % idx)
def compare(tfrecord_dir_a, tfrecord_dir_b, ignore_labels):
    max_label_size = 0 if ignore_labels else 'full'
    print('Loading dataset "%s"' % tfrecord_dir_a)
    tfutil.init_tf({'gpu_options.allow_growth': True})
    dset_a = dataset.TFRecordDataset(tfrecord_dir_a, max_label_size=max_label_size, repeat=False, shuffle_mb=0)
    print('Loading dataset "%s"' % tfrecord_dir_b)
    dset_b = dataset.TFRecordDataset(tfrecord_dir_b, max_label_size=max_label_size, repeat=False, shuffle_mb=0)
    tfutil.init_uninited_vars()
    
    print('Comparing datasets')
    idx = 0
    identical_images = 0
    identical_labels = 0
    while True:
        if idx % 100 == 0:
            print('%d\r' % idx, end='', flush=True)
        try:
            images_a, labels_a = dset_a.get_minibatch_np(1)
        except tf.errors.OutOfRangeError:
            images_a, labels_a = None, None
        try:
            images_b, labels_b = dset_b.get_minibatch_np(1)
        except tf.errors.OutOfRangeError:
            images_b, labels_b = None, None
        if images_a is None or images_b is None:
            if images_a is not None or images_b is not None:
                print('Datasets contain different number of images')
            break
        if images_a.shape == images_b.shape and np.all(images_a == images_b):
            identical_images += 1
        else:
            print('Image %d is different' % idx)
        if labels_a.shape == labels_b.shape and np.all(labels_a == labels_b):
            identical_labels += 1
        else:
            print('Label %d is different' % idx)
        idx += 1
    print('Identical images: %d / %d' % (identical_images, idx))
    if not ignore_labels:
        print('Identical labels: %d / %d' % (identical_labels, idx))
def display(tfrecord_dir):
    print('Loading dataset "%s"' % tfrecord_dir)
    tfutil.init_tf({'gpu_options.allow_growth': True})
    dset = dataset.TFRecordDataset(tfrecord_dir, max_label_size='full', repeat=False, shuffle_mb=0)
    tfutil.init_uninited_vars()
    
    idx = 0
    while True:
        try:
            images, labels = dset.get_minibatch_np(1)
        except tf.errors.OutOfRangeError:
            break
        if idx == 0:
            print('Displaying images')
            import cv2 # pip install opencv-python
            cv2.namedWindow('dataset_tool')
            print('Press SPACE or ENTER to advance, ESC to exit')
        print('\nidx = %-8d\nlabel = %s' % (idx, labels[0].tolist()))
        cv2.imshow('dataset_tool', images[0].transpose(1, 2, 0)[:, :, ::-1]) # CHW => HWC, RGB => BGR
        idx += 1
        if cv2.waitKey() == 27:
            break
    print('\nDisplayed %d images.' % idx)
def extract(tfrecord_dir, output_dir):
    print('Loading dataset "%s"' % tfrecord_dir)
    tfutil.init_tf({'gpu_options.allow_growth': True})
    dset = dataset.TFRecordDataset(tfrecord_dir, max_label_size=0, repeat=False, shuffle_mb=0)
    tfutil.init_uninited_vars()
    
    print('Extracting images to "%s"' % output_dir)
    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)
    idx = 0
    while True:
        if idx % 10 == 0:
            print('%d\r' % idx, end='', flush=True)
        try:
            images, labels = dset.get_minibatch_np(1)
        except tf.errors.OutOfRangeError:
            break
        if images.shape[1] == 1:
            img = PIL.Image.fromarray(images[0][0], 'L')
        else:
            img = PIL.Image.fromarray(images[0].transpose(1, 2, 0), 'RGB')
        img.save(os.path.join(output_dir, 'img%08d.png' % idx))
        idx += 1
    print('Extracted %d images.' % idx)
示例#13
0
                    (G, D, Gs, E),
                    os.path.join(
                        result_subdir,
                        'network-snapshot-%06d.pkl' % (cur_nimg // 1000)))

            # Record start time of the next tick.
            tick_start_time = time.time()

    # Write final results.
    misc.save_pkl((G, D, Gs, E),
                  os.path.join(result_subdir, 'network-final.pkl'))
    summary_log.close()
    open(os.path.join(result_subdir, '_training-done.txt'), 'wt').close()


#----------------------------------------------------------------------------
# Main entry point.
# Calls the function indicated in config.py.

if __name__ == "__main__":
    misc.init_output_logging()
    np.random.seed(config.random_seed)
    print('Initializing TensorFlow...')
    os.environ.update(config.env)
    tfutil.init_tf(config.tf_config)
    print('Running %s()...' % config.train['func'])
    tfutil.call_func_by_name(**config.train)
    print('Exiting...')

#----------------------------------------------------------------------------
                                         args.generated_images_dir)

if os.path.exists(args.aligned_dir) == False:
    os.mkdir(args.aligned_dir)

# initialize TensorFlow
print('Initializing TensorFlow...')
env = EasyDict()  # Environment variables, set by the main program in train.py.
env.TF_CPP_MIN_LOG_LEVEL = '1'  # Print warnings and errors, but disable debug info.
env.CUDA_VISIBLE_DEVICES = args.gpu  # Unspecified (default) = Use all available GPUs. List of ints = CUDA device numbers to use. change to '0' if first GPU is better
os.environ.update(env)
tf_config = EasyDict()  # TensorFlow session config, set by tfutil.init_tf().
tf_config[
    'graph_options.place_pruned_graph'] = True  # False (default) = Check that all ops are available on the designated device.
tf_config['gpu_options.allow_growth'] = True
tfutil.init_tf(tf_config)

if args.use_aligned == 1:
    landmarks_detector = LandmarksDetector(args.landmarks_model_path)
    aligned_face_path = None
    ALIGNED_IMAGES_DIR = args.aligned_dir
    for img_name in os.listdir(args.src_dir):
        print('Aligning %s ...' % img_name)
        try:
            raw_img_path = os.path.join(args.src_dir, img_name)
            fn = face_img_name = '%s_%02d.png' % (
                os.path.splitext(img_name)[0], 1)
            if os.path.isfile(fn):
                continue
            print('Getting landmarks...')
            ld = landmarks_detector.get_landmarks(raw_img_path)
示例#15
0
    def __init__(self, api, save_dir):
        ########################
        # INITIALIZE MODEL:
        ########################
       
        misc.init_output_logging()
        numpy.random.seed(config.random_seed)
        print('Initializing TensorFlow...')
        os.environ.update(config.env)
        tfutil.init_tf(config.tf_config)
        #-----------------
        network_pkl = misc.locate_network_pkl(14, None)
        print('Loading network from "%s"...' % network_pkl)
        self.G, self.D, self.Gs = misc.load_network_pkl(14, None)    
        self.random_state = numpy.random.RandomState()

        ########################
        # INITIALIZE API INFORMATION:
        ########################
        # Azure storage information
        self.table_account_name = ''        # NEEDS TO BE COMPLETED WITH AZURE ACCOUNT
        self.table_account_key = ''         # NEEDS TO BE COMPLETED WITH AZURE ACCOUNT
        self.block_blob_service = BlockBlobService(self.table_account_name, self.table_account_key) 
        self.https_prefix = ''              # NEEDS TO BE COMPLETED WITH AZURE ACCOUNT
        self.table_service = TableService(account_name=self.table_account_name, account_key=self.table_account_key)
        self.container_name = ''            # NEEDS TO BE COMPLETED WITH AZURE ACCOUNT

        # Microsoft face detection 
        self.msft_face_detection_key = ''  # NEEDS TO BE COMPLETED WITH MSFT API ACCOUNT
        self.msft_face_detection_url = ''  # NEEDS TO BE COMPLETED WITH MSFT API ACCOUNT
        self.msft_face_attributes = 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'
        self.msft_headers = {'Ocp-Apim-Subscription-Key': self.msft_face_detection_key}
        self.msft_params = {
            'returnFaceId': 'true',
            'returnFaceLandmarks': 'false',
            'returnFaceAttributes': self.msft_face_attributes
        }

        # FacePlusPlus face detection 
        self.faceplusplus_http_url = 'https://api-us.faceplusplus.com/facepp/v3/detect'
        self.faceplusplus_key = ""          # NEEDS TO BE COMPLETED WITH FACE++ API ACCOUNT
        self.faceplusplus_secret = ""       # NEEDS TO BE COMPLETED WITH FACE++ API ACCOUNT

        # IBM Watson face detection
        self.IBM_visual_recognition = VisualRecognitionV3(
            version='2018-03-19',
            iam_api_key='',                 # NEEDS TO BE COMPLETED WITH IBM API ACCOUNT
            url = 'https://gateway.watsonplatform.net/visual-recognition/api'
        )
        
        # Amazon AWS Rekognition face detection:
        self.amazon_face_detection_id = ''  # NEEDS TO BE COMPLETED WITH AMAZON API ACCOUNT
        self.amazon_face_detection_key = '' # NEEDS TO BE COMPLETED WITH AMAZON API ACCOUNT
        self.amazon_client = boto3.client('rekognition','us-east-1',aws_access_key_id=self.amazon_face_detection_id,aws_secret_access_key=self.amazon_face_detection_key)

        # SightEngine:
        self.SEclient = SightengineClient('', '')   # NEEDS TO BE COMPLETED WITH SE API ACCOUNT

        ########################
        # SET WHICH FACE API TO USE:
        ########################
        self.faceAPI = api  # or "FacePlusePlus" or "IBM" or "Google" or "Amazon" or "SE"

        self.save_dir = save_dir+'\\images'
        self.raw_results = save_dir+'\\raw_results.txt'

        f = open(self.raw_results, 'a')
        f.write("ImageLocation,Race_Int,Gender_Int,Race,Gender,Face_Detected,Gender_Prediction,Gender_Correct\n")
        f.close()

        self.image_count=0
示例#16
0
                misc.save_pkl(
                    (G, D, Gs),
                    os.path.join(
                        result_subdir,
                        'network-snapshot-%06d.pkl' % (cur_nimg // 1000)))

            # Record start time of the next tick.
            tick_start_time = time.time()

    # Write final results.
    misc.save_pkl((G, D, Gs), os.path.join(result_subdir, 'network-final.pkl'))
    summary_log.close()
    open(os.path.join(result_subdir, '_training-done.txt'), 'wt').close()


#----------------------------------------------------------------------------
# Main entry point.
# Calls the function indicated in config.py.

if __name__ == "__main__":
    misc.init_output_logging()
    np.random.seed(config_test.random_seed)
    print('Initializing TensorFlow...')
    os.environ.update(config_test.env)
    tfutil.init_tf(config_test.tf_config)
    print('Running %s()...' % config_test.train['func'])
    tfutil.call_func_by_name(**config_test.train)
    print('Exiting...')

#----------------------------------------------------------------------------
            # Save snapshots.
            if cur_tick % image_snapshot_ticks == 0 or done:
                grid_fakes = Gs.run(grid_latents, grid_labels, minibatch_size=sched.minibatch//config.num_gpus)
                misc.save_image_grid(grid_fakes, os.path.join(result_subdir, 'fakes%06d.png' % (cur_nimg // 1000)), drange=drange_net, grid_size=grid_size)
            if cur_tick % network_snapshot_ticks == 0 or done:
                misc.save_pkl((G, D, Gs), os.path.join(result_subdir, 'network-snapshot-%06d.pkl' % (cur_nimg // 1000)))

            # Record start time of the next tick.
            tick_start_time = time.time()

    # Write final results.
    misc.save_pkl((G, D, Gs), os.path.join(result_subdir, 'network-final.pkl'))
    summary_log.close()
    open(os.path.join(result_subdir, '_training-done.txt'), 'wt').close()

#----------------------------------------------------------------------------
# Main entry point.
# Calls the function indicated in config.py.

if __name__ == "__main__":
    misc.init_output_logging()
    np.random.seed(config.random_seed)
    print('Initializing TensorFlow...')
    os.environ.update(config.env)
    tfutil.init_tf(config.tf_config)
    print('Running %s()...' % config.train['func'])
    tfutil.call_func_by_name(**config.train)
    print('Exiting...')

#----------------------------------------------------------------------------