Myosotis-Researches 0.1.6__tar.gz → 0.1.8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8/Myosotis_Researches.egg-info}/PKG-INFO +1 -1
  2. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/Myosotis_Researches.egg-info/SOURCES.txt +1 -0
  3. {myosotis_researches-0.1.6/Myosotis_Researches.egg-info → myosotis_researches-0.1.8}/PKG-INFO +1 -1
  4. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/utils/__init__.py +4 -1
  5. myosotis_researches-0.1.8/myosotis_researches/CcGAN/utils/opts.py +89 -0
  6. myosotis_researches-0.1.8/myosotis_researches/CcGAN/utils/train.py +156 -0
  7. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/setup.py +1 -1
  8. myosotis_researches-0.1.6/myosotis_researches/CcGAN/utils/train.py +0 -65
  9. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/LICENSE +0 -0
  10. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/Myosotis_Researches.egg-info/dependency_links.txt +0 -0
  11. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/Myosotis_Researches.egg-info/top_level.txt +0 -0
  12. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/README.md +0 -0
  13. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/__init__.py +0 -0
  14. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/internal/__init__.py +0 -0
  15. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/internal/install_datasets.py +0 -0
  16. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/internal/show_datasets.py +0 -0
  17. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/internal/uninstall_datasets.py +0 -0
  18. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_128/CcGAN_SAGAN.py +0 -0
  19. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_128/ResNet_class_eval.py +0 -0
  20. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_128/ResNet_embed.py +0 -0
  21. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_128/ResNet_regre_eval.py +0 -0
  22. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_128/__init__.py +0 -0
  23. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_128/autoencoder.py +0 -0
  24. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_128/cGAN_SAGAN.py +0 -0
  25. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_128/cGAN_concat_SAGAN.py +0 -0
  26. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_256/CcGAN_SAGAN.py +0 -0
  27. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_256/ResNet_class_eval.py +0 -0
  28. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_256/ResNet_embed.py +0 -0
  29. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_256/ResNet_regre_eval.py +0 -0
  30. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_256/__init__.py +0 -0
  31. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_256/autoencoder.py +0 -0
  32. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_256/cGAN_SAGAN.py +0 -0
  33. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/models_256/cGAN_concat_SAGAN.py +0 -0
  34. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128/DiffAugment_pytorch.py +0 -0
  35. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128/__init__.py +0 -0
  36. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128/eval_metrics.py +0 -0
  37. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128/opts.py +0 -0
  38. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128/pretrain_AE.py +0 -0
  39. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128/pretrain_CNN_class.py +0 -0
  40. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128/pretrain_CNN_regre.py +0 -0
  41. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128/train_ccgan.py +0 -0
  42. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128/train_cgan.py +0 -0
  43. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128/train_cgan_concat.py +0 -0
  44. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128/train_net_for_label_embed.py +0 -0
  45. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128/utils.py +0 -0
  46. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128_output_10/DiffAugment_pytorch.py +0 -0
  47. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128_output_10/__init__.py +0 -0
  48. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128_output_10/eval_metrics.py +0 -0
  49. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128_output_10/opts.py +0 -0
  50. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128_output_10/pretrain_AE.py +0 -0
  51. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128_output_10/pretrain_CNN_class.py +0 -0
  52. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128_output_10/pretrain_CNN_regre.py +0 -0
  53. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128_output_10/train_ccgan.py +0 -0
  54. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128_output_10/train_cgan.py +0 -0
  55. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128_output_10/train_cgan_concat.py +0 -0
  56. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128_output_10/train_net_for_label_embed.py +0 -0
  57. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/train_128_output_10/utils.py +0 -0
  58. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/utils/IMGs_dataset.py +0 -0
  59. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/utils/SimpleProgressBar.py +0 -0
  60. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/utils/concat_image.py +0 -0
  61. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/utils/make_h5.py +0 -0
  62. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/CcGAN/utils/print_hdf5.py +0 -0
  63. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/myosotis_researches/__init__.py +0 -0
  64. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/pyproject.toml +0 -0
  65. {myosotis_researches-0.1.6 → myosotis_researches-0.1.8}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: Myosotis-Researches
3
- Version: 0.1.6
3
+ Version: 0.1.8
4
4
  Summary: A repository for storing my progress of researches.
5
5
  Home-page: https://github.com/Zeyu-Xie/Myosotis-Researches
6
6
  Author: Zeyu Xie
@@ -57,5 +57,6 @@ myosotis_researches/CcGAN/utils/SimpleProgressBar.py
57
57
  myosotis_researches/CcGAN/utils/__init__.py
58
58
  myosotis_researches/CcGAN/utils/concat_image.py
59
59
  myosotis_researches/CcGAN/utils/make_h5.py
60
+ myosotis_researches/CcGAN/utils/opts.py
60
61
  myosotis_researches/CcGAN/utils/print_hdf5.py
61
62
  myosotis_researches/CcGAN/utils/train.py
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: Myosotis-Researches
3
- Version: 0.1.6
3
+ Version: 0.1.8
4
4
  Summary: A repository for storing my progress of researches.
5
5
  Home-page: https://github.com/Zeyu-Xie/Myosotis-Researches
6
6
  Author: Zeyu Xie
@@ -3,7 +3,8 @@ from .concat_image import concat_image
3
3
  from .make_h5 import make_h5
4
4
  from .SimpleProgressBar import SimpleProgressBar
5
5
  from .IMGs_dataset import IMGs_dataset
6
- from .train import PlotLoss, compute_entropy, predict_class_labels
6
+ from .train import PlotLoss, compute_entropy, predict_class_labels, DiffAugment
7
+ from .opts import parse_opts
7
8
 
8
9
  __all__ = [
9
10
  "print_hdf5",
@@ -14,4 +15,6 @@ __all__ = [
14
15
  "PlotLoss",
15
16
  "compute_entropy",
16
17
  "predict_class_labels",
18
+ "DiffAugment",
19
+ "parse_opts"
17
20
  ]
@@ -0,0 +1,89 @@
1
+ import argparse
2
+
3
+ def parse_opts():
4
+ parser = argparse.ArgumentParser()
5
+
6
+ ''' Overall Settings '''
7
+ parser.add_argument('--data_path', type=str, default='')
8
+ parser.add_argument('--output_dir', type=str, default='')
9
+ parser.add_argument('--eval_ckpt_path', type=str, default='')
10
+ parser.add_argument('--seed', type=int, default=2021, metavar='S', help='random seed (default: 2020)')
11
+ parser.add_argument('--num_workers', type=int, default=0)
12
+
13
+
14
+ ''' Dataset '''
15
+ ## Data split: Ra is split into a train set (the last decimal of the degree is odd) and a test set (the last decimal of the degree is even); the unique labels in two sets do not overlap.
16
+ parser.add_argument('--data_split', type=str, default='train',
17
+ choices=['all', 'train'])
18
+ parser.add_argument('--min_label', type=float, default=0.0)
19
+ parser.add_argument('--max_label', type=float, default=90.0)
20
+ parser.add_argument('--num_channels', type=int, default=3, metavar='N')
21
+ parser.add_argument('--img_size', type=int, default=128, metavar='N')
22
+ parser.add_argument('--max_num_img_per_label', type=int, default=50, metavar='N')
23
+ parser.add_argument('--max_num_img_per_label_after_replica', type=int, default=0, metavar='N')
24
+ parser.add_argument('--show_real_imgs', action='store_true', default=False)
25
+ parser.add_argument('--visualize_fake_images', action='store_true', default=False)
26
+
27
+
28
+ ''' GAN settings '''
29
+ parser.add_argument('--GAN', type=str, default='CcGAN', choices=['cGAN', 'cGAN-concat', 'CcGAN'])
30
+ parser.add_argument('--GAN_arch', type=str, default='SAGAN', choices=['SAGAN'])
31
+
32
+ # label embedding setting
33
+ parser.add_argument('--net_embed', type=str, default='ResNet34_embed') #ResNetXX_emebed
34
+ parser.add_argument('--epoch_cnn_embed', type=int, default=200) #epoch of cnn training for label embedding
35
+ parser.add_argument('--resumeepoch_cnn_embed', type=int, default=0) #epoch of cnn training for label embedding
36
+ parser.add_argument('--epoch_net_y2h', type=int, default=500)
37
+ parser.add_argument('--dim_embed', type=int, default=128) #dimension of the embedding space
38
+ parser.add_argument('--batch_size_embed', type=int, default=256, metavar='N')
39
+
40
+ parser.add_argument('--loss_type_gan', type=str, default='hinge')
41
+ parser.add_argument('--niters_gan', type=int, default=10000, help='number of iterations')
42
+ parser.add_argument('--resume_niters_gan', type=int, default=0)
43
+ parser.add_argument('--save_niters_freq', type=int, default=2000, help='frequency of saving checkpoints')
44
+ parser.add_argument('--lr_g_gan', type=float, default=1e-4, help='learning rate for generator')
45
+ parser.add_argument('--lr_d_gan', type=float, default=1e-4, help='learning rate for discriminator')
46
+ parser.add_argument('--dim_gan', type=int, default=128, help='Latent dimension of GAN')
47
+ parser.add_argument('--batch_size_disc', type=int, default=64)
48
+ parser.add_argument('--batch_size_gene', type=int, default=64)
49
+ parser.add_argument('--num_D_steps', type=int, default=4, help='number of Ds updates in one iteration')
50
+ parser.add_argument('--cGAN_num_classes', type=int, default=20, metavar='N') #bin label into cGAN_num_classes
51
+ parser.add_argument('--visualize_freq', type=int, default=2000, help='frequency of visualization')
52
+
53
+ parser.add_argument('--kernel_sigma', type=float, default=-1.0,
54
+ help='If kernel_sigma<0, then use rule-of-thumb formula to compute the sigma.')
55
+ parser.add_argument('--threshold_type', type=str, default='hard', choices=['soft', 'hard'])
56
+ parser.add_argument('--kappa', type=float, default=-1)
57
+ parser.add_argument('--nonzero_soft_weight_threshold', type=float, default=1e-3,
58
+ help='threshold for determining nonzero weights for SVDL; we neglect images with too small weights')
59
+
60
+ # DiffAugment setting
61
+ parser.add_argument('--gan_DiffAugment', action='store_true', default=False)
62
+ parser.add_argument('--gan_DiffAugment_policy', type=str, default='color,translation,cutout')
63
+
64
+
65
+ # evaluation setting
66
+ '''
67
+ Four evaluation modes:
68
+ Mode 1: eval on unique labels used for GAN training;
69
+ Mode 2. eval on all unique labels in the dataset and when computing FID use all real images in the dataset;
70
+ Mode 3. eval on all unique labels in the dataset and when computing FID only use real images for GAN training in the dataset (to test SFID's effectiveness on unseen labels);
71
+ Mode 4. eval on a interval [min_label, max_label] with num_eval_labels labels.
72
+ '''
73
+ parser.add_argument('--eval_mode', type=int, default=2)
74
+ parser.add_argument('--num_eval_labels', type=int, default=-1)
75
+ parser.add_argument('--samp_batch_size', type=int, default=200)
76
+ parser.add_argument('--nfake_per_label', type=int, default=200)
77
+ parser.add_argument('--nreal_per_label', type=int, default=-1)
78
+ parser.add_argument('--comp_FID', action='store_true', default=False)
79
+ parser.add_argument('--epoch_FID_CNN', type=int, default=200)
80
+ parser.add_argument('--FID_radius', type=float, default=0)
81
+ parser.add_argument('--FID_num_centers', type=int, default=-1)
82
+ parser.add_argument('--dump_fake_for_NIQE', action='store_true', default=False,
83
+ help='Dump fake images for computing NIQE')
84
+
85
+ args = parser.parse_args()
86
+
87
+ return args
88
+
89
+ __all__ = ["parse_opts"]
@@ -0,0 +1,156 @@
1
+ import matplotlib as mpl
2
+ import matplotlib.pyplot as plt
3
+ import numpy as np
4
+ import torch
5
+ import torch.nn.functional as F
6
+
7
+
8
+ def PlotLoss(loss, filename):
9
+ x_axis = np.arange(start=1, stop=len(loss) + 1)
10
+ plt.switch_backend("agg")
11
+ mpl.style.use("seaborn")
12
+ fig = plt.figure()
13
+ ax = plt.subplot(111)
14
+ ax.plot(x_axis, np.array(loss))
15
+ plt.xlabel("epoch")
16
+ plt.ylabel("training loss")
17
+ plt.legend()
18
+ # ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.15), shadow=True, ncol=3)
19
+ # plt.title('Training Loss')
20
+ plt.savefig(filename)
21
+
22
+
23
+ # compute entropy of class labels; labels is a numpy array
24
+ def compute_entropy(labels, base=None):
25
+ value, counts = np.unique(labels, return_counts=True)
26
+ norm_counts = counts / counts.sum()
27
+ base = np.e if base is None else base
28
+ return -(norm_counts * np.log(norm_counts) / np.log(base)).sum()
29
+
30
+
31
+ def predict_class_labels(net, images, batch_size=500, verbose=False, num_workers=0):
32
+ net = net.cuda()
33
+ net.eval()
34
+
35
+ n = len(images)
36
+ if batch_size > n:
37
+ batch_size = n
38
+ dataset_pred = IMGs_dataset(images, normalize=False)
39
+ dataloader_pred = torch.utils.data.DataLoader(
40
+ dataset_pred, batch_size=batch_size, shuffle=False, num_workers=num_workers
41
+ )
42
+
43
+ class_labels_pred = np.zeros(n + batch_size)
44
+ with torch.no_grad():
45
+ nimgs_got = 0
46
+ if verbose:
47
+ pb = SimpleProgressBar()
48
+ for batch_idx, batch_images in enumerate(dataloader_pred):
49
+ batch_images = batch_images.type(torch.float).cuda()
50
+ batch_size_curr = len(batch_images)
51
+
52
+ outputs, _ = net(batch_images)
53
+ _, batch_class_labels_pred = torch.max(outputs.data, 1)
54
+ class_labels_pred[nimgs_got : (nimgs_got + batch_size_curr)] = (
55
+ batch_class_labels_pred.detach().cpu().numpy().reshape(-1)
56
+ )
57
+
58
+ nimgs_got += batch_size_curr
59
+ if verbose:
60
+ pb.update((float(nimgs_got) / n) * 100)
61
+ # end for batch_idx
62
+ class_labels_pred = class_labels_pred[0:n]
63
+ return class_labels_pred
64
+
65
+
66
+ def DiffAugment(x, policy="", channels_first=True):
67
+ if policy:
68
+ if not channels_first:
69
+ x = x.permute(0, 3, 1, 2)
70
+ for p in policy.split(","):
71
+ for f in AUGMENT_FNS[p]:
72
+ x = f(x)
73
+ if not channels_first:
74
+ x = x.permute(0, 2, 3, 1)
75
+ x = x.contiguous()
76
+ return x
77
+
78
+
79
+ def rand_brightness(x):
80
+ x = x + (torch.rand(x.size(0), 1, 1, 1, dtype=x.dtype, device=x.device) - 0.5)
81
+ return x
82
+
83
+
84
+ def rand_saturation(x):
85
+ x_mean = x.mean(dim=1, keepdim=True)
86
+ x = (x - x_mean) * (
87
+ torch.rand(x.size(0), 1, 1, 1, dtype=x.dtype, device=x.device) * 2
88
+ ) + x_mean
89
+ return x
90
+
91
+
92
+ def rand_contrast(x):
93
+ x_mean = x.mean(dim=[1, 2, 3], keepdim=True)
94
+ x = (x - x_mean) * (
95
+ torch.rand(x.size(0), 1, 1, 1, dtype=x.dtype, device=x.device) + 0.5
96
+ ) + x_mean
97
+ return x
98
+
99
+
100
+ def rand_translation(x, ratio=0.125):
101
+ shift_x, shift_y = int(x.size(2) * ratio + 0.5), int(x.size(3) * ratio + 0.5)
102
+ translation_x = torch.randint(
103
+ -shift_x, shift_x + 1, size=[x.size(0), 1, 1], device=x.device
104
+ )
105
+ translation_y = torch.randint(
106
+ -shift_y, shift_y + 1, size=[x.size(0), 1, 1], device=x.device
107
+ )
108
+ grid_batch, grid_x, grid_y = torch.meshgrid(
109
+ torch.arange(x.size(0), dtype=torch.long, device=x.device),
110
+ torch.arange(x.size(2), dtype=torch.long, device=x.device),
111
+ torch.arange(x.size(3), dtype=torch.long, device=x.device),
112
+ )
113
+ grid_x = torch.clamp(grid_x + translation_x + 1, 0, x.size(2) + 1)
114
+ grid_y = torch.clamp(grid_y + translation_y + 1, 0, x.size(3) + 1)
115
+ x_pad = F.pad(x, [1, 1, 1, 1, 0, 0, 0, 0])
116
+ x = (
117
+ x_pad.permute(0, 2, 3, 1)
118
+ .contiguous()[grid_batch, grid_x, grid_y]
119
+ .permute(0, 3, 1, 2)
120
+ )
121
+ return x
122
+
123
+
124
+ def rand_cutout(x, ratio=0.5):
125
+ cutout_size = int(x.size(2) * ratio + 0.5), int(x.size(3) * ratio + 0.5)
126
+ offset_x = torch.randint(
127
+ 0, x.size(2) + (1 - cutout_size[0] % 2), size=[x.size(0), 1, 1], device=x.device
128
+ )
129
+ offset_y = torch.randint(
130
+ 0, x.size(3) + (1 - cutout_size[1] % 2), size=[x.size(0), 1, 1], device=x.device
131
+ )
132
+ grid_batch, grid_x, grid_y = torch.meshgrid(
133
+ torch.arange(x.size(0), dtype=torch.long, device=x.device),
134
+ torch.arange(cutout_size[0], dtype=torch.long, device=x.device),
135
+ torch.arange(cutout_size[1], dtype=torch.long, device=x.device),
136
+ )
137
+ grid_x = torch.clamp(
138
+ grid_x + offset_x - cutout_size[0] // 2, min=0, max=x.size(2) - 1
139
+ )
140
+ grid_y = torch.clamp(
141
+ grid_y + offset_y - cutout_size[1] // 2, min=0, max=x.size(3) - 1
142
+ )
143
+ mask = torch.ones(x.size(0), x.size(2), x.size(3), dtype=x.dtype, device=x.device)
144
+ mask[grid_batch, grid_x, grid_y] = 0
145
+ x = x * mask.unsqueeze(1)
146
+ return x
147
+
148
+
149
+ AUGMENT_FNS = {
150
+ "color": [rand_brightness, rand_saturation, rand_contrast],
151
+ "translation": [rand_translation],
152
+ "cutout": [rand_cutout],
153
+ }
154
+
155
+
156
+ __all__ = ["PlotLoss", "compute_entropy", "predict_class_labels", "DiffAugment"]
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="Myosotis-Researches",
5
- version="0.1.6",
5
+ version="0.1.8",
6
6
  description="A repository for storing my progress of researches.",
7
7
  long_description=open("README.md").read(),
8
8
  long_description_content_type="text/markdown",
@@ -1,65 +0,0 @@
1
- import numpy as np
2
- import torch
3
- import matplotlib.pyplot as plt
4
- import matplotlib as mpl
5
-
6
-
7
- def PlotLoss(loss, filename):
8
- x_axis = np.arange(start=1, stop=len(loss) + 1)
9
- plt.switch_backend("agg")
10
- mpl.style.use("seaborn")
11
- fig = plt.figure()
12
- ax = plt.subplot(111)
13
- ax.plot(x_axis, np.array(loss))
14
- plt.xlabel("epoch")
15
- plt.ylabel("training loss")
16
- plt.legend()
17
- # ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.15), shadow=True, ncol=3)
18
- # plt.title('Training Loss')
19
- plt.savefig(filename)
20
-
21
-
22
- # compute entropy of class labels; labels is a numpy array
23
- def compute_entropy(labels, base=None):
24
- value, counts = np.unique(labels, return_counts=True)
25
- norm_counts = counts / counts.sum()
26
- base = np.e if base is None else base
27
- return -(norm_counts * np.log(norm_counts) / np.log(base)).sum()
28
-
29
-
30
- def predict_class_labels(net, images, batch_size=500, verbose=False, num_workers=0):
31
- net = net.cuda()
32
- net.eval()
33
-
34
- n = len(images)
35
- if batch_size > n:
36
- batch_size = n
37
- dataset_pred = IMGs_dataset(images, normalize=False)
38
- dataloader_pred = torch.utils.data.DataLoader(
39
- dataset_pred, batch_size=batch_size, shuffle=False, num_workers=num_workers
40
- )
41
-
42
- class_labels_pred = np.zeros(n + batch_size)
43
- with torch.no_grad():
44
- nimgs_got = 0
45
- if verbose:
46
- pb = SimpleProgressBar()
47
- for batch_idx, batch_images in enumerate(dataloader_pred):
48
- batch_images = batch_images.type(torch.float).cuda()
49
- batch_size_curr = len(batch_images)
50
-
51
- outputs, _ = net(batch_images)
52
- _, batch_class_labels_pred = torch.max(outputs.data, 1)
53
- class_labels_pred[nimgs_got : (nimgs_got + batch_size_curr)] = (
54
- batch_class_labels_pred.detach().cpu().numpy().reshape(-1)
55
- )
56
-
57
- nimgs_got += batch_size_curr
58
- if verbose:
59
- pb.update((float(nimgs_got) / n) * 100)
60
- # end for batch_idx
61
- class_labels_pred = class_labels_pred[0:n]
62
- return class_labels_pred
63
-
64
-
65
- __all__ = ["PlotLoss", "compute_entropy", "predict_class_labels"]