From 71ebfa39ef4fd6fe36cdab944e181cb9d8be0f3b Mon Sep 17 00:00:00 2001 From: captain-pool Date: Sun, 21 Jul 2019 12:12:16 +0530 Subject: [PATCH 1/6] Knowledge Distillation. Intial Commit. --- E3_Distill_ESRGAN/config/config.yaml | 1 + E3_Distill_ESRGAN/config/settings.py | 0 E3_Distill_ESRGAN/lib/model.py | 2 + E3_Distill_ESRGAN/lib/models/__init__.py | 0 E3_Distill_ESRGAN/lib/models/abstract.py | 14 +++++++ E3_Distill_ESRGAN/lib/settings.py | 51 ++++++++++++++++++++++++ E3_Distill_ESRGAN/lib/train.py | 1 + E3_Distill_ESRGAN/lib/utils.py | 1 + E3_Distill_ESRGAN/main.py | 2 + E3_Distill_ESRGAN/train.sh | 1 + 10 files changed, 73 insertions(+) create mode 100644 E3_Distill_ESRGAN/config/config.yaml create mode 100644 E3_Distill_ESRGAN/config/settings.py create mode 100644 E3_Distill_ESRGAN/lib/model.py create mode 100644 E3_Distill_ESRGAN/lib/models/__init__.py create mode 100644 E3_Distill_ESRGAN/lib/models/abstract.py create mode 100644 E3_Distill_ESRGAN/lib/settings.py create mode 100644 E3_Distill_ESRGAN/lib/train.py create mode 100644 E3_Distill_ESRGAN/lib/utils.py create mode 100644 E3_Distill_ESRGAN/main.py create mode 100644 E3_Distill_ESRGAN/train.sh diff --git a/E3_Distill_ESRGAN/config/config.yaml b/E3_Distill_ESRGAN/config/config.yaml new file mode 100644 index 0000000..7084ed5 --- /dev/null +++ b/E3_Distill_ESRGAN/config/config.yaml @@ -0,0 +1 @@ +# Config File for Distillation diff --git a/E3_Distill_ESRGAN/config/settings.py b/E3_Distill_ESRGAN/config/settings.py new file mode 100644 index 0000000..e69de29 diff --git a/E3_Distill_ESRGAN/lib/model.py b/E3_Distill_ESRGAN/lib/model.py new file mode 100644 index 0000000..f3b21f0 --- /dev/null +++ b/E3_Distill_ESRGAN/lib/model.py @@ -0,0 +1,2 @@ +from lib import models +from lib.models.abstract import Registry diff --git a/E3_Distill_ESRGAN/lib/models/__init__.py b/E3_Distill_ESRGAN/lib/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/E3_Distill_ESRGAN/lib/models/abstract.py b/E3_Distill_ESRGAN/lib/models/abstract.py new file mode 100644 index 0000000..c925018 --- /dev/null +++ b/E3_Distill_ESRGAN/lib/models/abstract.py @@ -0,0 +1,14 @@ +from tensorflow.python import keras + +class Registry(type): + models = {} + def __init__(cls, name, bases, attrs): + if name.lower() != "models": + Registry.models[cls.__name__.lower()] = cls + +class Models(keras.models.Model, metaclass=Registry): + def __init__(self, *args, **kwargs): + super(Models, self).__init__() + self.init(*args, **kwargs) + def init(self, *args, **kwargs): + pass diff --git a/E3_Distill_ESRGAN/lib/settings.py b/E3_Distill_ESRGAN/lib/settings.py new file mode 100644 index 0000000..310d52d --- /dev/null +++ b/E3_Distill_ESRGAN/lib/settings.py @@ -0,0 +1,51 @@ +import os +import yaml + + +def singleton(cls): + instances = {} + + def getinstance(*args, **kwargs): + if cls not in instances: + instances[cls] = cls(*args, **kwargs) + return instances[cls] + return getinstance + + +@singleton +class Settings(object): + def __init__(self, filename="config.yaml"): + self.__path = os.path.abspath(filename) + + @property + def path(self): + return os.path.dirname(self.__path) + + def __getitem__(self, index): + with open(self.__path, "r") as file_: + return yaml.load(file_.read())[index] + + def get(self, index, default=None): + with open(self.__path, "r") as file_: + return yaml.load(file_.read()).get(index, default) + + +class Stats(object): + def __init__(self, filename="stats.yaml"): + if os.path.exists(filename): + with open(filename, "r") as file_: + self.__data = yaml.load(file_.read()) + else: + self.__data = {} + self.file = filename + + def get(self, index, default=None): + self.__data.get(index, default) + + def __getitem__(self, index): + return self.__data[index] + + def __setitem__(self, index, data): + self.__data[index] = data + with open(self.file, "w") as file_: + yaml.dump(self.__data, file_, default_flow_style=False) diff --git a/E3_Distill_ESRGAN/lib/train.py b/E3_Distill_ESRGAN/lib/train.py new file mode 100644 index 0000000..6f7305f --- /dev/null +++ b/E3_Distill_ESRGAN/lib/train.py @@ -0,0 +1 @@ +import tensorflow as tf diff --git a/E3_Distill_ESRGAN/lib/utils.py b/E3_Distill_ESRGAN/lib/utils.py new file mode 100644 index 0000000..6f7305f --- /dev/null +++ b/E3_Distill_ESRGAN/lib/utils.py @@ -0,0 +1 @@ +import tensorflow as tf diff --git a/E3_Distill_ESRGAN/main.py b/E3_Distill_ESRGAN/main.py new file mode 100644 index 0000000..cb2623d --- /dev/null +++ b/E3_Distill_ESRGAN/main.py @@ -0,0 +1,2 @@ +import tensorflow as tf +from settings import Settings diff --git a/E3_Distill_ESRGAN/train.sh b/E3_Distill_ESRGAN/train.sh new file mode 100644 index 0000000..36ac368 --- /dev/null +++ b/E3_Distill_ESRGAN/train.sh @@ -0,0 +1 @@ +#! /bin/bash From 41b9095d5034de83da2ffb3138c0020597004921 Mon Sep 17 00:00:00 2001 From: captain-pool Date: Sun, 21 Jul 2019 12:53:40 +0530 Subject: [PATCH 2/6] Added Dummy Config File --- E3_Distill_ESRGAN/config/config.yaml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/E3_Distill_ESRGAN/config/config.yaml b/E3_Distill_ESRGAN/config/config.yaml index 7084ed5..4643c5f 100644 --- a/E3_Distill_ESRGAN/config/config.yaml +++ b/E3_Distill_ESRGAN/config/config.yaml @@ -1 +1,16 @@ # Config File for Distillation + +student_network: "name1" +student_networks_config: + name1: + param1: 1 + param2: "two" + paramN: "N" + name2: + param1: 1 + param2: "two" + paramN: "N" + name3: + param1: 1 + param2: "two" + paramN: "N" From b360d5b2c1e7f3b97a604d3d3b8df1073c8c6553 Mon Sep 17 00:00:00 2001 From: captain-pool Date: Tue, 23 Jul 2019 23:50:15 +0530 Subject: [PATCH 3/6] Added Dependency to ESRGAN --- E3_Distill_ESRGAN/config/settings.py | 0 E3_Distill_ESRGAN/lib/models/__init__.py | 1 + E3_Distill_ESRGAN/lib/models/abstract.py | 2 +- E3_Distill_ESRGAN/lib/settings.py | 21 +++++------ E3_Distill_ESRGAN/lib/utils.py | 7 +++- E3_Distill_ESRGAN/main.py | 45 +++++++++++++++++++++++- 6 files changed, 63 insertions(+), 13 deletions(-) delete mode 100644 E3_Distill_ESRGAN/config/settings.py diff --git a/E3_Distill_ESRGAN/config/settings.py b/E3_Distill_ESRGAN/config/settings.py deleted file mode 100644 index e69de29..0000000 diff --git a/E3_Distill_ESRGAN/lib/models/__init__.py b/E3_Distill_ESRGAN/lib/models/__init__.py index e69de29..8b13789 100644 --- a/E3_Distill_ESRGAN/lib/models/__init__.py +++ b/E3_Distill_ESRGAN/lib/models/__init__.py @@ -0,0 +1 @@ + diff --git a/E3_Distill_ESRGAN/lib/models/abstract.py b/E3_Distill_ESRGAN/lib/models/abstract.py index c925018..05bb261 100644 --- a/E3_Distill_ESRGAN/lib/models/abstract.py +++ b/E3_Distill_ESRGAN/lib/models/abstract.py @@ -1,11 +1,11 @@ from tensorflow.python import keras - class Registry(type): models = {} def __init__(cls, name, bases, attrs): if name.lower() != "models": Registry.models[cls.__name__.lower()] = cls +# Abstract class with Auto Registration of Kernels class Models(keras.models.Model, metaclass=Registry): def __init__(self, *args, **kwargs): super(Models, self).__init__() diff --git a/E3_Distill_ESRGAN/lib/settings.py b/E3_Distill_ESRGAN/lib/settings.py index 310d52d..9ae30df 100644 --- a/E3_Distill_ESRGAN/lib/settings.py +++ b/E3_Distill_ESRGAN/lib/settings.py @@ -1,20 +1,21 @@ import os import yaml - def singleton(cls): instances = {} - def getinstance(*args, **kwargs): - if cls not in instances: - instances[cls] = cls(*args, **kwargs) - return instances[cls] + distill_config = kwargs.get("student", "") + key = cls.__name__ + if distill_config: + key = "%s_student" % (cls.__name__) + if key not in instances: + instances[key] = cls(*args, **kwargs) + return instances[key] return getinstance - @singleton class Settings(object): - def __init__(self, filename="config.yaml"): + def __init__(self, filename="config.yaml", student=False): self.__path = os.path.abspath(filename) @property @@ -23,18 +24,18 @@ def path(self): def __getitem__(self, index): with open(self.__path, "r") as file_: - return yaml.load(file_.read())[index] + return yaml.load(file_.read(), Loader=yaml.FullLoader)[index] def get(self, index, default=None): with open(self.__path, "r") as file_: - return yaml.load(file_.read()).get(index, default) + return yaml.load(file_.read(), Loader=yaml.FullLoader).get(index, default) class Stats(object): def __init__(self, filename="stats.yaml"): if os.path.exists(filename): with open(filename, "r") as file_: - self.__data = yaml.load(file_.read()) + self.__data = yaml.load(file_.read(), Loader=yaml.FullLoader) else: self.__data = {} self.file = filename diff --git a/E3_Distill_ESRGAN/lib/utils.py b/E3_Distill_ESRGAN/lib/utils.py index 6f7305f..d9d8d14 100644 --- a/E3_Distill_ESRGAN/lib/utils.py +++ b/E3_Distill_ESRGAN/lib/utils.py @@ -1 +1,6 @@ -import tensorflow as tf +import os +import sys +# Fetching Ra Loss from project ESRGAN +sys.path.insert(0, os.path.abspath("..")) +from E2_ESRGAN.lib.utils import RelativisticAverageLoss +sys.path.pop(0) diff --git a/E3_Distill_ESRGAN/main.py b/E3_Distill_ESRGAN/main.py index cb2623d..413546c 100644 --- a/E3_Distill_ESRGAN/main.py +++ b/E3_Distill_ESRGAN/main.py @@ -1,2 +1,45 @@ +from absl import logging +import argparse +from lib.models import teacher +from lib import model +from lib import utils +from lib import settings import tensorflow as tf -from settings import Settings +""" + Compressing GANs using Knowledge Distillation. + Teacher GAN: ESRGAN (https://github.com/captain-pool/E2_ESRGAN) + + Citation: + @article{DBLP:journals/corr/abs-1902-00159, + author = {Angeline Aguinaldo and + Ping{-}Yeh Chiang and + Alexander Gain and + Ameya Patil and + Kolten Pearson and + Soheil Feizi}, + title = {Compressing GANs using Knowledge Distillation}, + journal = {CoRR}, + volume = {abs/1902.00159}, + year = {2019}, + url = {http://arxiv.org/abs/1902.00159}, + archivePrefix = {arXiv}, + eprint = {1902.00159}, + timestamp = {Tue, 21 May 2019 18:03:39 +0200}, + biburl = {https://dblp.org/rec/bib/journals/corr/abs-1902-00159}, + bibsource = {dblp computer science bibliography, https://dblp.org} + } +""" +def main(**kwargs): + student_settings = settings.Settings("../E2_ESRGAN/config.yaml", student=True) + teacher_settings = settings.Settings("config/config.yaml", student=False) + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--logdir", default=None, help="Path to log directory") + parser.add_argument("--modeldir", default=None, help="directory to store checkpoints and SavedModel") + parser.add_argument("--verbose", "-v", action="count", default=0, help="Increases Verbosity. Repeat to increase more") + FLAGS, unparsed = parser.parse_known_args() + log_levels = [logging.WARNING, logging.INFO, logging.DEBUG] + log_level = log_levels[min(FLAGS.verbose, len(log_levels)-1)] + logging.set_verbosity(log_level) + main(**vars(FLAGS)) From d9f1e55105105d1998d8c9f924341c1560390137 Mon Sep 17 00:00:00 2001 From: captain-pool Date: Tue, 23 Jul 2019 23:54:24 +0530 Subject: [PATCH 4/6] Fixed Import Bugs --- E3_Distill_ESRGAN/lib/model.py | 2 -- E3_Distill_ESRGAN/libs/model.py | 2 ++ E3_Distill_ESRGAN/{lib => libs}/models/__init__.py | 0 E3_Distill_ESRGAN/{lib => libs}/models/abstract.py | 0 E3_Distill_ESRGAN/libs/models/teacher.py | 5 +++++ E3_Distill_ESRGAN/{lib => libs}/settings.py | 0 E3_Distill_ESRGAN/{lib => libs}/train.py | 0 E3_Distill_ESRGAN/{lib => libs}/utils.py | 1 - E3_Distill_ESRGAN/main.py | 8 ++++---- 9 files changed, 11 insertions(+), 7 deletions(-) delete mode 100644 E3_Distill_ESRGAN/lib/model.py create mode 100644 E3_Distill_ESRGAN/libs/model.py rename E3_Distill_ESRGAN/{lib => libs}/models/__init__.py (100%) rename E3_Distill_ESRGAN/{lib => libs}/models/abstract.py (100%) create mode 100644 E3_Distill_ESRGAN/libs/models/teacher.py rename E3_Distill_ESRGAN/{lib => libs}/settings.py (100%) rename E3_Distill_ESRGAN/{lib => libs}/train.py (100%) rename E3_Distill_ESRGAN/{lib => libs}/utils.py (90%) diff --git a/E3_Distill_ESRGAN/lib/model.py b/E3_Distill_ESRGAN/lib/model.py deleted file mode 100644 index f3b21f0..0000000 --- a/E3_Distill_ESRGAN/lib/model.py +++ /dev/null @@ -1,2 +0,0 @@ -from lib import models -from lib.models.abstract import Registry diff --git a/E3_Distill_ESRGAN/libs/model.py b/E3_Distill_ESRGAN/libs/model.py new file mode 100644 index 0000000..46d658b --- /dev/null +++ b/E3_Distill_ESRGAN/libs/model.py @@ -0,0 +1,2 @@ +from libs import models +from libs.models.abstract import Registry diff --git a/E3_Distill_ESRGAN/lib/models/__init__.py b/E3_Distill_ESRGAN/libs/models/__init__.py similarity index 100% rename from E3_Distill_ESRGAN/lib/models/__init__.py rename to E3_Distill_ESRGAN/libs/models/__init__.py diff --git a/E3_Distill_ESRGAN/lib/models/abstract.py b/E3_Distill_ESRGAN/libs/models/abstract.py similarity index 100% rename from E3_Distill_ESRGAN/lib/models/abstract.py rename to E3_Distill_ESRGAN/libs/models/abstract.py diff --git a/E3_Distill_ESRGAN/libs/models/teacher.py b/E3_Distill_ESRGAN/libs/models/teacher.py new file mode 100644 index 0000000..7745439 --- /dev/null +++ b/E3_Distill_ESRGAN/libs/models/teacher.py @@ -0,0 +1,5 @@ +import os +import sys +# Fetching Generator from ESRGAN +sys.path.insert(0, "../E2_ESRGAN") +from lib.model import RRDBNet diff --git a/E3_Distill_ESRGAN/lib/settings.py b/E3_Distill_ESRGAN/libs/settings.py similarity index 100% rename from E3_Distill_ESRGAN/lib/settings.py rename to E3_Distill_ESRGAN/libs/settings.py diff --git a/E3_Distill_ESRGAN/lib/train.py b/E3_Distill_ESRGAN/libs/train.py similarity index 100% rename from E3_Distill_ESRGAN/lib/train.py rename to E3_Distill_ESRGAN/libs/train.py diff --git a/E3_Distill_ESRGAN/lib/utils.py b/E3_Distill_ESRGAN/libs/utils.py similarity index 90% rename from E3_Distill_ESRGAN/lib/utils.py rename to E3_Distill_ESRGAN/libs/utils.py index d9d8d14..be3fd7e 100644 --- a/E3_Distill_ESRGAN/lib/utils.py +++ b/E3_Distill_ESRGAN/libs/utils.py @@ -3,4 +3,3 @@ # Fetching Ra Loss from project ESRGAN sys.path.insert(0, os.path.abspath("..")) from E2_ESRGAN.lib.utils import RelativisticAverageLoss -sys.path.pop(0) diff --git a/E3_Distill_ESRGAN/main.py b/E3_Distill_ESRGAN/main.py index 413546c..b5214f8 100644 --- a/E3_Distill_ESRGAN/main.py +++ b/E3_Distill_ESRGAN/main.py @@ -1,9 +1,9 @@ from absl import logging import argparse -from lib.models import teacher -from lib import model -from lib import utils -from lib import settings +from libs.models import teacher +from libs import model +from libs import utils +from libs import settings import tensorflow as tf """ Compressing GANs using Knowledge Distillation. From 7c8407ce54e296d4ad3ebb2d7cc632dcca3aea0b Mon Sep 17 00:00:00 2001 From: Adrish Dey Date: Wed, 24 Jul 2019 00:02:37 +0530 Subject: [PATCH 5/6] Fixed Citation --- E3_Distill_ESRGAN/main.py | 40 +++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/E3_Distill_ESRGAN/main.py b/E3_Distill_ESRGAN/main.py index b5214f8..dd71703 100644 --- a/E3_Distill_ESRGAN/main.py +++ b/E3_Distill_ESRGAN/main.py @@ -8,26 +8,26 @@ """ Compressing GANs using Knowledge Distillation. Teacher GAN: ESRGAN (https://github.com/captain-pool/E2_ESRGAN) - - Citation: - @article{DBLP:journals/corr/abs-1902-00159, - author = {Angeline Aguinaldo and - Ping{-}Yeh Chiang and - Alexander Gain and - Ameya Patil and - Kolten Pearson and - Soheil Feizi}, - title = {Compressing GANs using Knowledge Distillation}, - journal = {CoRR}, - volume = {abs/1902.00159}, - year = {2019}, - url = {http://arxiv.org/abs/1902.00159}, - archivePrefix = {arXiv}, - eprint = {1902.00159}, - timestamp = {Tue, 21 May 2019 18:03:39 +0200}, - biburl = {https://dblp.org/rec/bib/journals/corr/abs-1902-00159}, - bibsource = {dblp computer science bibliography, https://dblp.org} - } + +Citation: + @article{DBLP:journals/corr/abs-1902-00159, + author = {Angeline Aguinaldo and + Ping{-}Yeh Chiang and + Alexander Gain and + Ameya Patil and + Kolten Pearson and + Soheil Feizi}, + title = {Compressing GANs using Knowledge Distillation}, + journal = {CoRR}, + volume = {abs/1902.00159}, + year = {2019}, + url = {http://arxiv.org/abs/1902.00159}, + archivePrefix = {arXiv}, + eprint = {1902.00159}, + timestamp = {Tue, 21 May 2019 18:03:39 +0200}, + biburl = {https://dblp.org/rec/bib/journals/corr/abs-1902-00159}, + bibsource = {dblp computer science bibliography, https://dblp.org} + } """ def main(**kwargs): student_settings = settings.Settings("../E2_ESRGAN/config.yaml", student=True) From 4d6d8c33195232f35a6e0d2906b9455399ecb718 Mon Sep 17 00:00:00 2001 From: Adrish Dey Date: Wed, 24 Jul 2019 00:03:36 +0530 Subject: [PATCH 6/6] Updated Docstring --- E3_Distill_ESRGAN/libs/models/abstract.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/E3_Distill_ESRGAN/libs/models/abstract.py b/E3_Distill_ESRGAN/libs/models/abstract.py index 05bb261..de73523 100644 --- a/E3_Distill_ESRGAN/libs/models/abstract.py +++ b/E3_Distill_ESRGAN/libs/models/abstract.py @@ -5,7 +5,7 @@ def __init__(cls, name, bases, attrs): if name.lower() != "models": Registry.models[cls.__name__.lower()] = cls -# Abstract class with Auto Registration of Kernels +# Abstract class for Auto Registration of Kernels class Models(keras.models.Model, metaclass=Registry): def __init__(self, *args, **kwargs): super(Models, self).__init__()