Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: cientgu/GIQA
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: IDLabMedia/GIQA
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 1 commit
  • 3 files changed
  • 1 contributor

Commits on Sep 26, 2024

  1. Copy the full SHA
    dc1e6ea View commit details
Showing with 103 additions and 31 deletions.
  1. 0 __init__.py
  2. +15 −12 get_gmm.py
  3. +88 −19 gmm_score.py
Empty file added __init__.py
Empty file.
27 changes: 15 additions & 12 deletions get_gmm.py
Original file line number Diff line number Diff line change
@@ -11,23 +11,26 @@
# If not tqdm is not available, provide a mock version of it
def tqdm(x): return x

from inception import InceptionV3
from .inception import InceptionV3

parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('--act_path', type=str, default=None, help='input act path')
parser.add_argument('--kernel_number', type=int, default=5, help='number of gmm kernels')
parser.add_argument('--gmm_path', type=str, default=None, help='output gmm path')



if __name__ == '__main__':
args = parser.parse_args()

act = pickle.load(open(args.act_path, "rb"))
def get_gmm(act_path, kernel_number, gmm_path):
act = pickle.load(open(act_path, "rb"))
print(act.shape)
# gact = mixture.GaussianMixture(n_components=args.kernel_number, covariance_type='diag', max_iter=1000)
print("this is full")
gact = mixture.GaussianMixture(n_components=args.kernel_number, covariance_type='full')
gact = mixture.GaussianMixture(n_components=kernel_number, covariance_type='full')
gact.fit(act)
pickle.dump(gact,open(args.gmm_path, "wb+"))
pickle.dump(gact, open(gmm_path, "wb+"))

if __name__ == '__main__':
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('--act_path', type=str, default=None, help='input act path')
parser.add_argument('--kernel_number', type=int, default=5, help='number of gmm kernels')
parser.add_argument('--gmm_path', type=str, default=None, help='output gmm path')
args = parser.parse_args()

get_gmm(args.act_path, args.kernel_number, args.gmm_path)


107 changes: 88 additions & 19 deletions gmm_score.py
Original file line number Diff line number Diff line change
@@ -20,24 +20,8 @@
# If not tqdm is not available, provide a mock version of it
def tqdm(x): return x

from inception import InceptionV3

parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('path', type=str, nargs=1,
help=('Path to the generated images or '
'to .npz statistic files'))
parser.add_argument('--batch-size', type=int, default=50,
help='Batch size to use')
parser.add_argument('--dims', type=int, default=2048,
choices=list(InceptionV3.BLOCK_INDEX_BY_DIM),
help=('Dimensionality of Inception features to use. '
'By default, uses pool3 features'))
parser.add_argument('-c', '--gpu', default='', type=str,
help='GPU to use (leave blank for CPU only)')
parser.add_argument('--pca_path', type=str, default=None)
# "/mnt/blob/code/image-judge/gaussian/pca_stat/pca_all_95.pkl"
parser.add_argument('--gmm_path', type=str, default="/mnt/blob/code/image-judge/gaussian/pca_stat/stat_cat/act95_7")
parser.add_argument('--output_file', type=str, default="/mnt/blob/datasets/generation_results/score_results/try_out.txt")
from .inception import InceptionV3


def imread(filename):
return np.asarray(Image.open(filename).convert('RGB'), dtype=np.uint8)[..., :3]
@@ -59,7 +43,6 @@ def get_activations(files, model, batch_size, dims, cuda, verbose, pca_path, gmm
n_batches = len(files) // batch_size
n_used_imgs = n_batches * batch_size

pred_arr = np.empty((n_used_imgs, dims))

pca_gmm_path = gmm_path
pca_gmm = pickle.load(open(gmm_path, "rb"))
@@ -103,6 +86,75 @@ def get_activations(files, model, batch_size, dims, cuda, verbose, pca_path, gmm
return pred_arr


import pickle

class RenameUnpickler(pickle.Unpickler):
def find_class(self, module, name):
renamed_module = module
if module == "sklearn.mixture.gaussian_mixture":
renamed_module = "sklearn.mixture._gaussian_mixture"

return super(RenameUnpickler, self).find_class(renamed_module, name)


def renamed_load(file_obj):
return RenameUnpickler(file_obj).load()

def load_models(inception_dims, useCuda, pca_path, gmm_path):
block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[inception_dims]
model = InceptionV3([block_idx])
if useCuda:
model.cuda()


pca_gmm_path = gmm_path
#pca_gmm = pickle.load(open(gmm_path, "rb"))
pca_gmm = renamed_load(open(gmm_path, "rb"))

if pca_path != None:
pca = pickle.load(open(pca_path, "rb"))
else:
pca = None

return model, pca, pca_gmm

def get_activation_preloaded_model(files, model, pca_gmm, pca=None, inception_dims=2048, useCuda=True, read_files=True):
if not isinstance(files, list):
files = [files]
singleFile = True
else:
singleFile = False

model.eval()
batch_size = len(files)

if read_files:
images = np.array([imread(str(f)).astype(np.float32) for f in files])
else:
images = np.array(files).astype(np.float32)

# Reshape to (n_images, 3, height, width)
#print(images.shape)
images = images.transpose((0, 3, 1, 2))
images /= 255

batch = torch.from_numpy(images).type(torch.FloatTensor)
if useCuda:
batch = batch.cuda()
pred = model(batch)[0]

if pca != None:
pred = pca.transform(pred.cpu()[:,:,0,0])
prop = pca_gmm.score_samples(pred)
else:
prop = pca_gmm.score_samples(pred[:,:,0,0].cpu().numpy())

if singleFile:
prop = prop[0]

return prop


def calculate_activation_statistics(files, model, batch_size, dims, cuda, pca_path, gmm_path, output_file):
verbose = False
act = get_activations(files, model, batch_size, dims, cuda, verbose, pca_path, gmm_path, output_file)
@@ -146,6 +198,23 @@ def calculate_fid_given_paths(paths, batch_size, cuda, dims, pca_path, gmm_path,


if __name__ == '__main__':

parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('path', type=str, nargs=1,
help=('Path to the generated images or '
'to .npz statistic files'))
parser.add_argument('--batch-size', type=int, default=50,
help='Batch size to use')
parser.add_argument('--dims', type=int, default=2048,
choices=list(InceptionV3.BLOCK_INDEX_BY_DIM),
help=('Dimensionality of Inception features to use. '
'By default, uses pool3 features'))
parser.add_argument('-c', '--gpu', default='', type=str,
help='GPU to use (leave blank for CPU only)')
parser.add_argument('--pca_path', type=str, default=None)
# "/mnt/blob/code/image-judge/gaussian/pca_stat/pca_all_95.pkl"
parser.add_argument('--gmm_path', type=str, default="/mnt/blob/code/image-judge/gaussian/pca_stat/stat_cat/act95_7")
parser.add_argument('--output_file', type=str, default="/mnt/blob/datasets/generation_results/score_results/try_out.txt")
args = parser.parse_args()
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu