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) tflib.init_tf() dset_a = dataset.TFRecordDataset(tfrecord_dir_a, max_label_size=max_label_size, repeat=False, shuffle=False) print('Loading dataset "%s"' % tfrecord_dir_b) dset_b = dataset.TFRecordDataset(tfrecord_dir_b, max_label_size=max_label_size, repeat=False, shuffle=False) tflib.init_uninitialized_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) images_a, labels_a = dset_a.get_minibatch_np(1) images_b, labels_b = dset_b.get_minibatch_np(1) 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) tflib.init_tf({'gpu_options.allow_growth': True}) dset = dataset.TFRecordDataset(tfrecord_dir, max_label_size='full', repeat=False, shuffle_mb=0) tflib.init_uninitialized_vars() import cv2 # pip install opencv-python idx = 0 while True: try: images, labels = dset.get_minibatch_np(1) except tf.errors.OutOfRangeError: break if idx == 0: print('Displaying images') 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 unpack(tfrecord_dir, output_dir, resolution=None): print('Loading dataset "%s"' % tfrecord_dir) tflib.init_tf() dset = dataset.TFRecordDataset(tfrecord_dir, max_label_size='full', repeat=False, shuffle=False) tflib.init_uninitialized_vars() print('Extracting images to "%s"' % output_dir) if not os.path.isdir(output_dir): os.makedirs(output_dir) idx = 0 labels = [] while True: if idx % 10 == 0: print('%d\r' % idx, end='', flush=True) images, lbls = dset.get_minibatch_np(1) if images is None: 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') if resolution is not None: img = img.resize((resolution, resolution), PIL.Image.ANTIALIAS) assert lbls.shape[0] == 1 labels.append(lbls[0]) png_fname = make_png_path(output_dir, idx) os.makedirs(os.path.dirname(png_fname), exist_ok=True) img.save(png_fname) idx += 1 np.savez(f'{output_dir}/pack_extras.npz', labels=np.array(labels, dtype=np.uint8), num_images=idx) print('Extracted %d images.' % idx)
def extract(tfrecord_dir, output_dir): print('Loading dataset "%s"' % tfrecord_dir) tflib.init_tf({'gpu_options.allow_growth': True}) dset = dataset.TFRecordDataset(tfrecord_dir, max_label_size=0, repeat=False, shuffle_mb=0) tflib.init_uninitialized_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)
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) tflib.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) tflib.init_uninitialized_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 info(tfrecord_dir): print() print('%-20s%s' % ('Dataset name:', os.path.basename(tfrecord_dir))) bytes_total = 0 bytes_max = 0 num_files = 0 for f in sorted(glob.glob(os.path.join(tfrecord_dir, '*'))): if os.path.isfile(f): fs = os.stat(f).st_size bytes_total += fs bytes_max = max(bytes_max, fs) num_files += 1 print('%-20s%.2f' % ('Total size GB:', bytes_total / (1 << 30))) print('%-20s%.2f' % ('Largest file GB:', bytes_max / (1 << 30))) print('%-20s%d' % ('Num files:', num_files)) tflib.init_tf() dset = dataset.TFRecordDataset(tfrecord_dir, max_label_size='full', repeat=False, shuffle=False) tflib.init_uninitialized_vars() print('%-20s%d' % ('Image width:', dset.shape[2])) print('%-20s%d' % ('Image height:', dset.shape[1])) print('%-20s%d' % ('Image channels:', dset.shape[0])) print('%-20s%s' % ('Image datatype:', dset.dtype)) print('%-20s%d' % ('Label size:', dset.label_size)) print('%-20s%s' % ('Label datatype:', dset.label_dtype)) num_images = 0 label_min = np.finfo(np.float64).max label_max = np.finfo(np.float64).min label_norm = 0 lod = max(dset.resolution_log2 - 2, 0) while True: print('\r%-20s%d' % ('Num images:', num_images), end='', flush=True) _images, labels = dset.get_minibatch_np(10000, lod=lod) # not accurate if labels is None: break num_images += labels.shape[0] if dset.label_size: label_min = min(label_min, np.min(labels)) label_max = max(label_max, np.max(labels)) label_norm += np.sum(np.sqrt(np.sum(np.square(labels), axis=1))) print('\r%-20s%d' % ('Num images:', num_images)) print( '%-20s%s' % ('Label range:', '%g -- %g' % (label_min, label_max) if num_images and dset.label_size else 'n/a')) print('%-20s%s' % ('Label L2 norm:', '%g' % (label_norm / num_images) if num_images and dset.label_size else 'n/a')) print()
def __init__(self, tfrecord_dir: str = None): if tfrecord_dir: self.training_set = dataset.TFRecordDataset(tfrecord_dir, shuffle_mb=0) self.labels_available = True else: self.labels_available = False self.label_names = [ 'Model', 'Color', 'Manufacturer', 'Body', 'Rotation', 'Ratio', 'Background' ]
def display(tfrecord_dir): print('Loading dataset "%s"' % tfrecord_dir) tflib.init_tf({'gpu_options.allow_growth': True}) # label_file = './datasets/car_labels_new_rotation_oversample_classifier/car_labels_new_rotation_oversample_classifier-rxx.labels' dset = dataset.TFRecordDataset(tfrecord_dir, max_label_size='full', repeat=False, shuffle_mb=0) tflib.init_uninitialized_vars() import cv2 # pip install opencv-python idx = 0 while True: try: images, labels = dset.get_minibatch_np(1) except tf.errors.OutOfRangeError: break if idx == 0: print('Displaying images') cv2.namedWindow('dataset_tool') print('Press SPACE or ENTER to advance, ESC to exit') label_list = labels[0].tolist() ''' rotation_cos = labels[:, 108] rotation_sin = labels[:, 109] angle = np.arctan2(rotation_sin, rotation_cos) print('Angle:', (angle * 360 / (2 * np.pi) + 360 + 0) % 360) print('X: ', rotation_cos) print('Y: ', rotation_sin) new_rotation_sin = rotation_sin * -1 angle = np.arctan2(new_rotation_sin, rotation_cos) print('Mirror Angle:', (angle * 360 / (2 * np.pi) + 360 + 0) % 360) print('Mirror X: ', rotation_cos) print('Mirror Y: ', new_rotation_sin) print(labels[:, 108:108+8]) ''' # rotations = labels[:, 108:108 + 8] # rotations_index = (np.argmax(rotations) + np.random.choice([-1, 1])) % 8 # new_rotation = np.zeros([8]) # new_rotation[rotations_index] = 1 print('\nidx = %-8d\nlabel = %s' % (idx, label_list)) print('background:', labels[:, -6:]) print('roation:', labels[:, 108:108 + 8]) # print('old rotation', label_list[108:108 + 8]) # print('new rotation', new_rotation) 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 convert_to_hdf5(hdf5_filename, tfrecord_dir, compress): print('Loading dataset "%s"' % tfrecord_dir) tflib.init_tf() dset = dataset.TFRecordDataset(tfrecord_dir, max_label_size='full', repeat=False, shuffle=False) tflib.init_uninitialized_vars() with HDF5Exporter(hdf5_filename, resolution=dset.shape[1], channels=dset.shape[0], compress=compress) as h5: all_labels = [] while True: images, labels = dset.get_minibatch_np(1) if images is None: break h5.add_images(images) all_labels.append(labels) all_labels = np.concatenate(all_labels) if all_labels.size: h5.add_labels(all_labels)
def _evaluate(self, classifier, Gs_kwargs, num_gpus): self._set_dataset_obj( dataset.TFRecordDataset(tfrecord_dir=self.test_dataset)) iterations = self.num_images // self.minibatch_per_gpu acc_out_list = [] with tf.device('/gpu:0'): all_rotations = tf.constant( [[1.0, 0.0], [0.7071, 0.7071], [0.0, 1.0], [-0.7071, 0.7071], [-1.0, 0.0], [-0.7071, -0.7071], [0.0, -1.0], [0.7071, -0.7071]], dtype=tf.float32) all_rotations = tf.tile(tf.expand_dims(all_rotations, axis=0), [self.minibatch_per_gpu, 1, 1]) images_placeholder = tf.placeholder( shape=classifier.input_shapes[0], dtype=tf.float32) label_placeholder = tf.placeholder(shape=[None, 2], dtype=tf.float32) images = misc.adjust_dynamic_range(images_placeholder, [0, 255], [-1, 1]) prediction = classifier.get_output_for(images) prediction = tf.tile(tf.expand_dims(prediction, axis=1), [1, 8, 1]) rotation_labels = tf.tile( tf.expand_dims(label_placeholder, axis=1), [1, 8, 1]) distance_pred = tf.norm(all_rotations - prediction, axis=-1) min_dist_pred = tf.argmin(distance_pred, axis=-1) distance_real = tf.norm(all_rotations - rotation_labels, axis=-1) min_dist_real = tf.argmin(distance_real, axis=-1) correct = tf.cast(tf.equal(min_dist_pred, min_dist_real), tf.int32) for _ in range(iterations): reals = next(self._iterate_reals(self.minibatch_per_gpu)) images, labels = reals acc = tf.reduce_sum(correct) / self.minibatch_per_gpu acc_out = tflib.run(acc, feed_dict={ images_placeholder: images, label_placeholder: labels }) acc_out_list.append(acc_out) self._report_result(np.mean(np.array(acc_out_list)))
def _evaluate(self, classifier, Gs_kwargs, num_gpus): self._set_dataset_obj( dataset.TFRecordDataset(tfrecord_dir='datasets/car_labels_new_rotation_oversample_classifier_test')) iterations = self.num_images // self.minibatch_per_gpu distance_list = [] images_placeholder = tf.placeholder(shape=classifier.input_shapes[0], dtype=tf.float32) with tf.device('/gpu:0'): for _ in range(iterations): reals = next(self._iterate_reals(self.minibatch_per_gpu)) images, labels = reals images = misc.adjust_dynamic_range(images, [0, 255], [-1, 1]) prediction = classifier.get_output_for(images_placeholder) prediction = prediction[:, 0:2] rotation_labels = labels[:, 108:108 + 2] distance = tf.norm(rotation_labels - prediction, axis=-1) prediction_out = tflib.run(distance, feed_dict={images_placeholder: images}) distance_list.append(prediction_out) self._report_result(np.mean(np.array(distance_list)))
def _evaluate(self, classifier, Gs_kwargs, num_gpus): self._set_dataset_obj( dataset.TFRecordDataset( tfrecord_dir='datasets/classifier_dataset_15_oversample_v5_test' )) dataset_object = self._get_dataset_obj() dataset_object.configure(minibatch_size=self.minibatch_per_gpu) num_correct = 0 num_total = 0 images_placeholder = tf.placeholder(shape=classifier.input_shapes[0], dtype=tf.float32) label_placeholder = tf.placeholder(shape=[None, 127], dtype=tf.float32) images_adjust = misc.adjust_dynamic_range(images_placeholder, [0, 255], [-1, 1]) prediction = classifier.get_output_for(images_adjust) one_hot_prediction = tf.one_hot(indices=tf.argmax(prediction, axis=-1), depth=self.label_size) current_label = label_placeholder[:, self.label_start:self.label_end] num_correct_pred = tf.reduce_sum(one_hot_prediction * current_label) num_total_label = tf.reduce_sum(current_label) while num_total < self.num_images: images, labels = dataset_object.get_minibatch_np( minibatch_size=self.minibatch_per_gpu) num_correct_pred_out, num_total_label_out = tflib.run( [num_correct_pred, num_total_label], feed_dict={ images_placeholder: images, label_placeholder: labels }) num_correct += num_correct_pred_out # print('correct:', num_correct) num_total += num_total_label_out # print('total:', num_total) self._report_result(num_correct / num_total)
import seaborn as sn import pandas as pd import matplotlib.pyplot as plt import result_models from dnnlib import tflib from training import dataset import tensorflow as tf from training import misc from tqdm import tqdm import numpy as np tflib.init_tf() dset = dataset.TFRecordDataset('./../../../datasets/classifier_oversample_rotation_test') dset.configure(minibatch_size=1) classifier = result_models.load_rotation_classifier_one_hot('./../../../results') x, labels = dset.get_minibatch_tf() x = tf.cast(x, tf.float32) x = misc.adjust_dynamic_range(x, [0, 255], [-1, 1]) prediction = classifier.get_output_for(x) prediction_out_list = [] label_out_list = [] iterations = 5000 for i in tqdm(range(iterations)): prediction_out, label_out = tflib.run([prediction, labels]) prediction_out_list.append(prediction_out) label_out_list.append(label_out) label_out_list = np.reshape(np.stack(label_out_list, axis=0), [iterations, 8])
import dnnlib.tflib as tflib from training import dataset import numpy as np import tensorflow as tf import training.misc as misc import matplotlib.pyplot as plt tfrecord_dir = '../../datasets/cars_v5_512' tflib.init_tf({'gpu_options.allow_growth': True}) training_set = dataset.TFRecordDataset(tfrecord_dir, max_label_size='full', repeat=False, shuffle_mb=0) tflib.init_uninitialized_vars() session = tflib.create_session(None, force_as_default=True) latent_placeholder = tf.placeholder(tf.float32, shape=(None, 512)) dlatent_placeholder = tf.placeholder(tf.float32, shape=(None, 16, 512)) label_placeholder = tf.placeholder(tf.float32, shape=(None, 127)) G, D, Gs = misc.load_pkl( '../../results/00155-stylegan2-cars_v5_512-2gpu-config-f/network-snapshot-010467.pkl' ) num_steps = 10 label_left = training_set.get_random_labels_np(1) rotation_offset = 108 rotations = label_left[:, rotation_offset:rotation_offset + 8] rotation_index = np.argmax(rotations, axis=1) new_rotation_index = ((rotation_index + np.random.choice([-1, 1])) % 8)
import numpy as np import matplotlib.pyplot as plt import dnnlib.tflib as tflib import training.misc as misc import result_models from training import dataset from tqdm import tqdm import tensorflow as tf tflib.init_tf() rotation_offset = 108 tflib.init_tf() dset = dataset.TFRecordDataset( './../../../datasets/car_labels_v7_oversample_filter') dset.configure(minibatch_size=10) batch_size = 10 dset.configure(batch_size) classifier = misc.load_pkl( '../../../classifier/models/rotation_v7/network-snapshot-000602.pkl') mean_dist_disc = [] mean_dist_gen = [] titles = [ 'Front Center', 'Front Left', 'Profile Left', 'Rear Left', 'Rear Center', 'Rear Right', 'Profile Right', 'Front Right' ]
import tensorflow as tf import dnnlib.tflib as tflib from training import misc from training import dataset tflib.init_tf() dset = dataset.TFRecordDataset('../datasets/classifier_dataset_15_test') minibatch_size = 4 dset.configure(minibatch_size=minibatch_size) images, labels = dset.get_minibatch_tf() images = misc.adjust_dynamic_range(images, [0, 255], [-1, 1]) prediction = tf.constant([[0, 1.5, 3.0, 0, 9, 2, 1, 0.5], [0, 1.5, 3.0, 0, 1, 3, 1, 0.5], [0, 1.5, 3.0, 0, 1, 2, 5, 0.5], [0, 1.5, 3.0, 0, 1, 2, 1, 1.5]]) one_hot_prediction = tf.one_hot(indices=tf.argmax(prediction, axis=-1), depth=8) current_label = labels[:, 108:108 + 8] num_correct_pred = tf.reduce_sum(one_hot_prediction * current_label) num_total_label = tf.reduce_sum(current_label) num_correct_pred_out, num_total_label_out, current_label, one_hot_prediction = tflib.run( [num_correct_pred, num_total_label, current_label, one_hot_prediction]) print()
from dnnlib import tflib from training import dataset import tensorflow as tf tfrecord_dir = '../../datasets/car_images_512' label_dir = '../../datasets/car_labels/cars_v5-rxx.labels' tflib.init_tf() d_set = dataset.TFRecordDataset(tfrecord_dir, label_file=label_dir, buffer_mb=1, shuffle_mb=0) def G_logistic_ns_pathreg(G, D, opt, training_set, minibatch_size, pl_minibatch_shrink=2, pl_decay=0.01, pl_weight=2.0): _ = opt latents = tf.random_normal([minibatch_size] + G.input_shapes[0][1:]) labels = training_set.get_random_labels_tf(minibatch_size) fake_images_out, fake_dlatents_out = G.get_output_for(latents, labels, is_training=True, return_dlatents=True) fake_scores_out = D.get_output_for(fake_images_out, labels,
import dnnlib.tflib as tflib from training import dataset import numpy as np import tensorflow as tf import training.misc as misc import matplotlib.pyplot as plt from dnnlib.tflib import tfutil import PIL.Image as Image from tqdm import tqdm tflib.init_tf() tfrecord_dir = '../../datasets/car_labels_v7_oversample_filter' training_set = dataset.TFRecordDataset(tfrecord_dir) rotation_offset = 108 minibatch_size = 4 tflib.init_uninitialized_vars() rotation_step_size = 0.02 latent_placeholder = tf.placeholder(tf.float32, shape=(None, 512)) label_placeholder = tf.placeholder(tf.float32, shape=(None, 121)) angle_placeholder = tf.placeholder(tf.float32, shape=(1)) G, D, Gs = misc.load_pkl( '../../results/00201-stylegan2-car_labels_v7_oversample_filter-2gpu-config-f-squared_euclidean_10_interpolate_50_percent_int_reg-256/network-snapshot-001290.pkl' ) distance_measure = misc.load_pkl( 'https://nvlabs-fi-cdn.nvidia.com/stylegan/networks/metrics/vgg16_zhang_perceptual.pkl' ) label_int_pl = label_placeholder[1:2] interpolation_rotation_cos = tf.expand_dims(tf.cos(angle_placeholder), axis=-1)
import numpy as np import tensorflow as tf import sys sys.path.append("../../") from training import dataset from dnnlib import tflib tfrecord_dir = '../../datasets/' + sys.argv[1] tflib.init_tf() dset = dataset.TFRecordDataset(tfrecord_dir, repeat=False, shuffle_mb=0) label = np.zeros(shape=[dset.label_size]) while True: try: images, labels = dset.get_minibatch_np(1) label += labels[0] except tf.errors.OutOfRangeError: break print(label)
import seaborn as sn import pandas as pd import matplotlib.pyplot as plt import result_models from dnnlib import tflib from training import dataset import tensorflow as tf from training import misc from tqdm import tqdm import numpy as np tflib.init_tf() dset = dataset.TFRecordDataset( '../../../datasets/classifier_oversample_color_test') minibatch_size = 10 dset.configure(minibatch_size=minibatch_size) classifier = result_models.load_color_classifier('./../../../results') x, labels = dset.get_minibatch_tf() x = tf.cast(x, tf.float32) # x = misc.adjust_dynamic_range(x, [0, 255], [-1, 1]) prediction = classifier.get_output_for(x) prediction_out_list = [] label_out_list = [] iterations = 1000 for i in tqdm(range(iterations)): prediction_out, label_out = tflib.run([prediction, labels]) prediction_out_list.append(prediction_out) label_out_list.append(label_out)
from training import misc import dnnlib.tflib as tflib from matplotlib import pyplot as plt from training import dataset import tensorflow as tf import numpy as np from tqdm import tqdm tfrecord_dir = '../../../datasets/car_labels_v5_oversample_filter' network_pkl = '../../../results/00375-conditional_label_attention/network-snapshot-000645.pkl' tflib.init_tf() dset = dataset.TFRecordDataset(tfrecord_dir, shuffle_mb=0) tflib.init_uninitialized_vars() num_images = 10 offset = 0 _G, D_original, _Gs = misc.load_pkl(network_pkl) label_placeholder = tf.placeholder(tf.float32, [None, dset.label_size]) images_placeholder = tf.placeholder(tf.float32, [None] + dset.shape) for i in tqdm(range(num_images)): select_image = i + offset while True: images, labels = dset.get_minibatch_np(1) if np.sum(labels) == 8: if select_image is 0: break select_image -= 1 images = misc.adjust_dynamic_range(images, [0, 255], [-1, 1]) fig, axs = plt.subplots(4, 3)
from training import misc import tensorflow as tf from rotation_detector.torch_lib.Dataset import * from rotation_detector.library.Plotting import * from rotation_detector.torch_lib import Model, ClassAverages from rotation_detector.yolo.yolo import cv_Yolo import os import torch from torchvision.models import vgg tflib.init_tf() tfrecord_dir = '../datasets/car_labels_v7_oversample_filter' dset = dataset.TFRecordDataset(tfrecord_dir) network_dir = '../results/00313-rotation-v7-baseline_256/network-snapshot-004919.pkl' G_, D_, Gs = misc.load_pkl(network_dir) label = dset.get_random_labels_np(1) latent = np.random.normal(size=[1, 512]) def convert_to_image(array, size=256): img = np.transpose(array, [1, 2, 0]) img = img * 127.5 + 127.5 img = np.clip(img, 0, 255).astype(np.uint8) return cv2.resize(img, (size, size)) label_placeholder = tf.placeholder(tf.float32, [None, 121])
import tensorflow as tf from dnnlib import tflib from training import dataset tflib.init_tf() dset = dataset.TFRecordDataset('../datasets/car_images_256', label_file='../datasets/car_labels/cars_v7-rxx.labels', max_label_size='full', repeat=False, shuffle_mb=0) minibatch_size = 20 dset.configure(minibatch_size=minibatch_size) rotation_offset = 108 x, labels = dset.get_minibatch_tf() zero_rotation = tf.expand_dims(tf.zeros(tf.shape(x)[0]), axis=-1) removed_labels = tf.concat([ labels[:, :rotation_offset], zero_rotation, zero_rotation, labels[:, rotation_offset + 2:] ], axis=1) condition = tf.equal(labels[:, 108], 0.7071) random_vector = tf.random_uniform([tf.shape(x)[0]]) < 0.5 remove_condition = tf.logical_and(condition, random_vector) removed_fl_fr = tf.where(remove_condition, removed_labels, labels) labels, removed_labels, removed_fl_fr, condition, random_vector, remove_condition, zero_rotation = tflib.run([labels[:, 108:110], removed_labels[:, 108:110], removed_fl_fr[:, 108:110], condition, random_vector, remove_condition, zero_rotation]) print()
from training import dataset tfrecord_dir = '../datasets/classifier_dataset_15_oversample_v5_test' label_file = None tflib.init_tf() # tfrecord_dir = '../datasets/car_images_256' # label_file = '../datasets/car_labels/cars_v5-rxx.labels' offsets = [1, 67, 12, 18, 10, 8, 5, 6] label_names = [ 'Real / Fake', 'Model', 'Color', 'Manufacturer', 'Body', 'Rotation', 'Ratio', 'Background' ] tflib.init_tf() dset = dataset.TFRecordDataset(tfrecord_dir, label_file=label_file, max_label_size='full', repeat=False, shuffle_mb=0) batch_size = 1 rotations = np.zeros(shape=[127]) _, labels = dset.get_minibatch_np(batch_size) while True: try: images, labels = dset.get_minibatch_np(1) rotations += labels[0] except tf.errors.OutOfRangeError: break
def _evaluate(self, classifier, Gs_kwargs, num_gpus): self._set_dataset_obj( dataset.TFRecordDataset(tfrecord_dir='datasets/classifier_dataset_15_test')) iterations = self.num_images // self.minibatch_per_gpu distance_list = [] images_placeholder = tf.placeholder(shape=classifier.input_shapes[0], dtype=tf.float32) num_correct_model = 0 num_correct_color = 0 num_correct_manufacturer = 0 num_correct_body = 0 num_correct_rotation = 0 num_correct_ratio = 0 num_correct_background = 0 num_label_model = 0 num_label_color = 0 num_label_manufacturer = 0 num_label_body = 0 num_label_rotation = 0 num_label_ratio = 0 num_label_background = 0 for _ in range(iterations): reals = next(self._iterate_reals(self.minibatch_per_gpu)) images, labels = reals images = misc.adjust_dynamic_range(images, [0, 255], [-1, 1]) model_pred, color_pred, manufacturer_pred, body_pred, rotation_pred, ratio_pred, background_pred = classifier.get_output_for( images_placeholder) i = 0 offsets = [1, 67, 12, 18, 10, 8, 5, 6] current_offset = offsets[i] next_offset = current_offset + offsets[i + 1] current_label = labels[:, current_offset:next_offset] num_model_pred = tf.reduce_sum(tf.one_hot(indices=tf.argmax(model_pred, axis=-1), depth=offsets[i + 1]) * current_label) num_model_label = tf.reduce_sum(current_label) i += 1 current_offset = next_offset next_offset = current_offset + offsets[i + 1] current_label = labels[:, current_offset:next_offset] num_color_pred = tf.reduce_sum(tf.one_hot(indices=tf.argmax(color_pred, axis=-1), depth=offsets[i + 1]) * current_label) num_color_label = tf.reduce_sum(current_label) i += 1 current_offset = next_offset next_offset = current_offset + offsets[i + 1] current_label = labels[:, current_offset:next_offset] num_manufacturer_pred = tf.reduce_sum(tf.one_hot(indices=tf.argmax(manufacturer_pred, axis=-1), depth=offsets[i + 1]) * current_label) num_manufacturer_label = tf.reduce_sum(current_label) i += 1 current_offset = next_offset next_offset = current_offset + offsets[i + 1] current_label = labels[:, current_offset:next_offset] num_body_pred = tf.reduce_sum(tf.one_hot(indices=tf.argmax(body_pred, axis=-1), depth=offsets[i + 1]) * current_label) num_body_label = tf.reduce_sum(current_label) i += 1 current_offset = next_offset next_offset = current_offset + offsets[i + 1] current_label = labels[:, current_offset:next_offset] num_rotation_pred = tf.reduce_sum(tf.one_hot(indices=tf.argmax(rotation_pred, axis=-1), depth=offsets[i + 1]) * current_label) num_rotation_label = tf.reduce_sum(current_label) i += 1 current_offset = next_offset next_offset = current_offset + offsets[i + 1] current_label = labels[:, current_offset:next_offset] num_ratio_pred = tf.reduce_sum(tf.one_hot(indices=tf.argmax(ratio_pred, axis=-1), depth=offsets[i + 1]) * current_label) num_ratio_label = tf.reduce_sum(current_label) i += 1 current_offset = next_offset next_offset = current_offset + offsets[i + 1] current_label = labels[:, current_offset:next_offset] num_background_pred = tf.reduce_sum(tf.one_hot(indices=tf.argmax(background_pred, axis=-1), depth=offsets[i + 1]) * current_label) num_background_label = tf.reduce_sum(current_label) output = tflib.run([ num_model_pred, num_color_pred, num_manufacturer_pred, num_body_pred, num_rotation_pred, num_ratio_pred, num_background_pred, num_model_label, num_color_label, num_manufacturer_label, num_body_label, num_rotation_label, num_ratio_label, num_background_label ], feed_dict={images_placeholder: images}) num_correct_model += output[0] num_correct_color += output[1] num_correct_manufacturer += output[2] num_correct_body += output[3] num_correct_rotation += output[4] num_correct_ratio += output[5] num_correct_background += output[6] num_label_model += output[7] num_label_color += output[8] num_label_manufacturer += output[9] num_label_body += output[10] num_label_rotation += output[11] num_label_ratio += output[12] num_label_background += output[13] model_acc = num_correct_model / num_label_model print('Model Accuracy: {}'.format(model_acc)) color_acc = num_correct_color / num_label_color print('Color Accuracy: {}'.format(color_acc)) manufacturer_acc = num_correct_manufacturer / num_label_manufacturer print('Manufacturer Accuracy: {}'.format(manufacturer_acc)) body_acc = num_correct_body / num_label_body print('Body Accuracy: {}'.format(body_acc)) rotation_acc = num_correct_rotation / num_label_rotation print('Rotation Accuracy: {}'.format(rotation_acc)) ratio_acc = num_correct_ratio / num_label_ratio print('Ratio Accuracy: {}'.format(ratio_acc)) background_acc = num_correct_background / num_label_background print('Background Accuracy: {}'.format(background_acc)) self._report_result(np.mean([ model_acc, color_acc, manufacturer_acc, body_acc, rotation_acc, ratio_acc, background_acc ]))