You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 13 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. [![Python Versions](https://img.shields.io/pypi/pyversions/learnware.svg?logo=python&logoColor=white)](https://pypi.org/project/learnware/#files)
  2. [![Platform](https://img.shields.io/badge/platform-linux%20%7C%20windows%20%7C%20macos-lightgrey)](https://pypi.org/project/learnware/#files)
  3. [![PypI Versions](https://img.shields.io/pypi/v/learnware)](https://pypi.org/project/learnware/#history)
  4. [![Documentation Status](https://readthedocs.org/projects/learnware/badge/?version=latest)](https://learnware.readthedocs.io/en/latest/?badge=latest)
  5. [![License](https://img.shields.io/pypi/l/learnware)](LICENSE)
  6. <div align=center>
  7. <img src="./docs/_static/img/logo/logo1.png" width="50%"/>
  8. </div>
  9. ``Learnware`` is a model sharing platform, which give a basic implementation of the learnware paradigm. A learnware is a well-performed trained machine learning model with a specification that enables it to be adequately identified to reuse according to the requirement of future users who may know nothing about the learnware in advance. The learnware paradigm can solve entangled problems in the current machine learning paradigm, like continual learning and catastrophic forgetting. It also reduces resources for training a well-performed model.
  10. # Introduction
  11. ## Framework
  12. <div align="center">
  13. <img src="./docs/_static/img/learnware_paradigm.jpg" width="70%"/>
  14. </div>
  15. Machine learning, especially the prevailing big model paradigm, has achieved great success in natural language processing and computer vision applications. However, it still faces challenges such as the requirement of a large amount of labeled training data, difficulty in adapting to changing environments, and catastrophic forgetting when refining trained models incrementally. These big models, while useful in their targeted tasks, often fail to address the above issues and struggle to generalize beyond their specific purposes.
  16. <div align="center">
  17. <img src="./docs/_static/img/learnware_market.jpg" width="70%" />
  18. </div>
  19. The learnware paradigm introduces the concept of a well-performed, trained machine learning model with a specification that allows future users, who have no prior knowledge of the learnware, to reuse it based on their requirements.
  20. Developers or owners of trained machine learning models can submit their models to a learnware market. If accepted, the market assigns a specification to the model and accommodates it. The learnware market could host thousands or millions of well-performed models from different developers, for various tasks, using diverse data, and optimizing different objectives.
  21. Instead of building a model from scratch, users can submit their requirements to the learnware market, which then identifies and deploys helpful learnware(s) based on the specifications. Users can apply the learnware directly, adapt it using their data, or exploit it in other ways to improve their model. This process is more efficient and less expensive than building a model from scratch.
  22. ## Benefits of the Learnware Paradigm
  23. | Benefit | Description |
  24. | ---- | ---- |
  25. | Lack of training data | Strong models can be built with small data by adapting well-performed learnwares. |
  26. | Lack of training skills | Ordinary users can obtain strong models by leveraging well-performed learnwares instead of building models from scratch. |
  27. | Catastrophic forgetting | Accepted learnwares are always stored in the learnware market, retaining old knowledge. |
  28. | Continual learning | The learnware market continually enriches its knowledge with constant submissions of well-performed learnwares. |
  29. | Data privacy/ proprietary | Developers only submit models, not data, preserving data privacy/proprietary. |
  30. | Unplanned tasks | Open to all legal developers, the learnware market can accommodate helpful learnwares for various tasks. |
  31. | Carbon emission | Assembling small models may offer good-enough performance, reducing interest in training large models and the carbon footprint. |
  32. # Quick Start
  33. ## Installation
  34. Learnware is currently hosted on [PyPI](https://pypi.org/). You can easily intsall ``Learnware`` according to the following steps:
  35. - For Windows and Linux users:
  36. ```bash
  37. pip install learnware
  38. ```
  39. - For macOS users:
  40. ```bash
  41. conda install -c pytorch faiss
  42. pip install learnware
  43. ```
  44. ## Prepare Learnware
  45. The Learnware Market consists of a wide range of learnwares. A valid learnware is a zipfile which
  46. is composed of the following four parts.
  47. - ``__init__.py``
  48. A python file offering interfaces for your model's fitting, predicting and fine-tuning.
  49. - ``rkme.json``
  50. A json file containing the statistical specification of your data.
  51. - ``learnware.yaml``
  52. A config file describing your model class name, type of statistical specification(e.g. Reduced Kernel Mean Embedding, ``RKMETableSpecification``), and
  53. the file name of your statistical specification file.
  54. - ``environment.yaml``
  55. A Conda environment configuration file for running the model (if the model environment is incompatible, you can rely on this for manual configuration).
  56. You can generate this file according to the following steps:
  57. - Create env config for conda:
  58. ```bash
  59. conda env export | grep -v "^prefix: " > environment.yaml
  60. ```
  61. - Recover env from config:
  62. ```bash
  63. conda env create -f environment.yaml
  64. ```
  65. We also demonstrate the detail format of learnware zipfile in [DOC link], and also please refer to [Examples](./examples/workflow_by_code/learnware_example) for concrete learnware zipfile example.
  66. ## Learnware Market Workflow
  67. Users can start an ``Learnware`` workflow according to the following steps:
  68. ### Initialize a Learnware Market
  69. The ``EasyMarket`` class implements the most basic set of functions in a ``Learnware``.
  70. You can use the following code snippet to initialize a basic ``Learnware`` named "demo":
  71. ```python
  72. import learnware
  73. from learnware.market import EasyMarket
  74. learnware.init()
  75. easy_market = EasyMarket(market_id="demo", rebuild=True)
  76. ```
  77. ### Upload Leanwares
  78. Before uploading your learnware into the ``Learnware``,
  79. create a semantic specification ``semantic_spec`` by selecting or filling in values for the predefined semantic tags
  80. to describe the features of your task and model.
  81. For example, the following code snippet demonstrates the semantic specification
  82. of a Scikit-Learn type model, which is designed for business scenario and performs classification on tabular data:
  83. ```python
  84. semantic_spec = {
  85. "Data": {"Values": ["Tabular"], "Type": "Class"},
  86. "Task": {"Values": ["Classification"], "Type": "Class"},
  87. "Library": {"Values": ["Scikit-learn"], "Type": "Class"},
  88. "Scenario": {"Values": ["Business"], "Type": "Tag"},
  89. "Description": {"Values": "", "Type": "String"},
  90. "Name": {"Values": "demo_learnware", "Type": "String"},
  91. }
  92. ```
  93. Once the semantic specification is defined,
  94. you can easily upload your learnware with a single line of code:
  95. ```python
  96. easy_market.add_learnware(zip_path, semantic_spec)
  97. ```
  98. Here, ``zip_path`` is the directory of your learnware zipfile.
  99. ### Semantic Specification Search
  100. To search for learnwares that fit your task purpose,
  101. you should also provide a semantic specification ``user_semantic`` that describes the characteristics of your task.
  102. The ``Learnware`` will perform a first-stage search based on ``user_semantic``,
  103. identifying potentially helpful leranwares whose models solve tasks similar to your requirements.
  104. ```python
  105. # construct user_info which includes semantic specification for searching learnware
  106. user_info = BaseUserInfo(id="user", semantic_spec=semantic_spec)
  107. # search_learnware performs semantic specification search if user_info doesn't include a statistical specification
  108. _, single_learnware_list, _ = easy_market.search_learnware(user_info)
  109. # single_learnware_list is the learnware list by semantic specification searching
  110. print(single_learnware_list)
  111. ```
  112. ### Statistical Specification Search
  113. If you choose to porvide your own statistical specification file ``stat.json``,
  114. the ``Learnware`` can perform a more accurate leanware selection from
  115. the learnwares returned by the previous step. This second-stage search is based on statistical information
  116. and returns one or more learnwares that are most likely to be helpful for your task.
  117. For example, the following code is designed to work with Reduced Set Kernel Embedding as a statistical specification:
  118. ```python
  119. import learnware.specification as specification
  120. user_spec = specification.RKMETableSpecification()
  121. user_spec.load(os.path.join(unzip_path, "rkme.json"))
  122. user_info = BaseUserInfo(
  123. semantic_spec=user_semantic, stat_info={"RKMETableSpecification": user_spec}
  124. )
  125. (sorted_score_list, single_learnware_list,
  126. mixture_score, mixture_learnware_list) = easy_market.search_learnware(user_info)
  127. # sorted_score_list is the learnware scores based on MMD distances, sorted in descending order
  128. print(sorted_score_list)
  129. # single_learnware_list is the learnwares sorted in descending order based on their scores
  130. print(single_learnware_list)
  131. # mixture_learnware_list is the learnwares whose mixture is helpful for your task
  132. print(mixture_learnware_list)
  133. # mixture_score is the score of the mixture of learnwares
  134. print(mixture_score)
  135. ```
  136. ### Reuse Learnwares
  137. Based on the returned list of learnwares ``mixture_learnware_list`` in the previous step,
  138. you can easily reuse them to make predictions your own data, instead of training a model from scratch.
  139. We provide two baseline methods for reusing a given list of learnwares, namely ``JobSelectorReuser`` and ``AveragingReuser``.
  140. Simply replace ``test_x`` in the code snippet below with your own testing data and start reusing learnwares!
  141. ```python
  142. # using jobselector reuser to reuse the searched learnwares to make prediction
  143. reuse_job_selector = JobSelectorReuser(learnware_list=mixture_learnware_list)
  144. job_selector_predict_y = reuse_job_selector.predict(user_data=test_x)
  145. # using averaging ensemble reuser to reuse the searched learnwares to make prediction
  146. reuse_ensemble = AveragingReuser(learnware_list=mixture_learnware_list)
  147. ensemble_predict_y = reuse_ensemble.predict(user_data=test_x)
  148. ```
  149. ## Auto Workflow Example
  150. ``Learnware`` also provides an auto workflow example, which includes preparing learnwares, upload and delete learnware from markets, search learnware with semantic specifications and statistical specifications. The users can run ``examples/workflow_by_code.py`` to try the basic workflow of ``Learnware``.
  151. # Experiments and Examples
  152. ## Environment
  153. For all experiments, we used a single linux server. Details on the specifications are listed in the table below. All processors were used for training and evaluating.
  154. | System | GPU | CPU |
  155. | ---- | ---- | ---- |
  156. | Ubuntu 20.04.4 LTS | Nvidia Tesla V100S | Intel(R) Xeon(R) Gold 6240R |
  157. ## Datasets
  158. We designed experiments on three publicly available datasets, namely Prediction Future Sales (PFS), M5 Forecasting (M5) and CIFAR 10. For the two sales forecasting data sets of PFS and M5, we divide the user data according to different stores, and train the Ridge model and LightGBM model on the corresponding data respectively. For the CIFAR10 image classification task, we first randomly pick 6 to 10 categories, and randomly select 800 to 2000 samples from each category from the categories corresponding to the training set, constituting a total of 50 different uploaders. For test users, we first randomly pick 3 to 6 categories, and randomly select 150 to 350 samples from each category from the corresponding categories from the test set, constituting a total of 20 different users.
  159. We tested the efficiency of the specification generation and the accuracy of the search and reuse model respectively. The evaluation index on PFS and M5 data is RMSE, and the evaluation index on CIFAR10 classification task is classification accuracy
  160. ## Results
  161. The time-consuming specification generation is shown in the table below:
  162. | Dataset | Data Dimensions | Specification Generation Time (s) |
  163. | ---- | ---- | ---- |
  164. | PFS | 8714274*31 | < 1.5 |
  165. | M5 | 46027957*82 | 9~15 |
  166. | CIFAR 10 | 9000\*3\*32\*32 | 7~10 |
  167. The accuracy of search and reuse is shown in the table below:
  168. | Dataset | Top-1 Performance | Job Selector Reuse | Average Ensemble Reuse |
  169. | ---- | ---- | ---- | ---- |
  170. | PFS | 1.955 +/- 2.866 | 2.175 +/- 2.847 | 1.950 +/- 2.888 |
  171. | M5 | 2.066 +/- 0.424 | 2.116 +/- 0.472 | 2.512 +/- 0.573 |
  172. | CIFAR 10 | 0.619 +/- 0.138 | 0.585 +/- 0.056 | .715 +/- 0.075 |
  173. # About
  174. ## Contributor
  175. We appreciate all contributions and thank all the contributors!
  176. TODO: Here paste the github API after publishing:
  177. [Pic after publish]()
  178. ## About us
  179. Visit [LAMDA's official website](http://www.lamda.nju.edu.cn/MainPage.ashx).