Diff of /examples/tutorial.ipynb [000000] .. [3ee609]

Switch to unified view

a b/examples/tutorial.ipynb
1
{
2
 "cells": [
3
  {
4
   "cell_type": "markdown",
5
   "metadata": {},
6
   "source": [
7
    "# MEDIGAN Quick start\n",
8
    "Quick introduction on how to choose the right model and generate images"
9
   ]
10
  },
11
  {
12
   "cell_type": "code",
13
   "execution_count": null,
14
   "metadata": {},
15
   "outputs": [],
16
   "source": [
17
    "pip install medigan"
18
   ]
19
  },
20
  {
21
   "cell_type": "markdown",
22
   "metadata": {},
23
   "source": [
24
    "Import medigan and initialize Generators\n"
25
   ]
26
  },
27
  {
28
   "cell_type": "code",
29
   "execution_count": null,
30
   "metadata": {},
31
   "outputs": [],
32
   "source": [
33
    "from medigan import Generators\n",
34
    "generators = Generators()"
35
   ]
36
  },
37
  {
38
   "cell_type": "markdown",
39
   "metadata": {},
40
   "source": [
41
    "Generate 10 samples using one of the medigan models\n"
42
   ]
43
  },
44
  {
45
   "cell_type": "code",
46
   "execution_count": null,
47
   "metadata": {},
48
   "outputs": [],
49
   "source": [
50
    "generators.generate(model_id=\"00001_DCGAN_MMG_CALC_ROI\", num_samples=10)"
51
   ]
52
  },
53
  {
54
   "cell_type": "markdown",
55
   "metadata": {},
56
   "source": [
57
    "Get the model's generate method and run it to generate 3 samples\n"
58
   ]
59
  },
60
  {
61
   "cell_type": "code",
62
   "execution_count": null,
63
   "metadata": {},
64
   "outputs": [],
65
   "source": [
66
    "gen_function = generators.get_generate_function(model_id=\"00001_DCGAN_MMG_CALC_ROI\", \n",
67
    "                                                num_samples=3)\n",
68
    "gen_function()"
69
   ]
70
  },
71
  {
72
   "cell_type": "markdown",
73
   "metadata": {},
74
   "source": [
75
    "Create a list of search terms and find the models that have these terms in their config.\n"
76
   ]
77
  },
78
  {
79
   "cell_type": "code",
80
   "execution_count": null,
81
   "metadata": {},
82
   "outputs": [],
83
   "source": [
84
    "values_list = ['dcgan', 'Mammography', 'inbreast']\n",
85
    "models = generators.find_matching_models_by_values(values=values_list, \n",
86
    "                                                    target_values_operator='AND', \n",
87
    "                                                    are_keys_also_matched=True, \n",
88
    "                                                    is_case_sensitive=False)\n",
89
    "print(f'Found models: {models}')"
90
   ]
91
  },
92
  {
93
   "cell_type": "markdown",
94
   "metadata": {},
95
   "source": [
96
    "Create a list of search terms, find a model and generate\n"
97
   ]
98
  },
99
  {
100
   "cell_type": "code",
101
   "execution_count": null,
102
   "metadata": {},
103
   "outputs": [],
104
   "source": [
105
    "values_list = ['dcgan', 'mMg', 'ClF', 'modalities', 'inbreast']\n",
106
    "generators.find_model_and_generate(values=values_list, \n",
107
    "                                    target_values_operator='AND', \n",
108
    "                                    are_keys_also_matched=True, \n",
109
    "                                    is_case_sensitive=False, \n",
110
    "                                    num_samples=5)"
111
   ]
112
  },
113
  {
114
   "cell_type": "markdown",
115
   "metadata": {},
116
   "source": [
117
    "Rank the models by a performance metric and return ranked list of models\n"
118
   ]
119
  },
120
  {
121
   "cell_type": "code",
122
   "execution_count": null,
123
   "metadata": {},
124
   "outputs": [],
125
   "source": [
126
    "ranked_models = generators.rank_models_by_performance(metric=\"SSIM\", \n",
127
    "                                                        order=\"asc\")\n",
128
    "print(ranked_models)"
129
   ]
130
  },
131
  {
132
   "cell_type": "markdown",
133
   "metadata": {},
134
   "source": [
135
    "Find the models, then rank them by a performance metric and return ranked list of models\n"
136
   ]
137
  },
138
  {
139
   "cell_type": "code",
140
   "execution_count": null,
141
   "metadata": {},
142
   "outputs": [],
143
   "source": [
144
    "ranked_models = generators.find_models_and_rank(values=values_list, \n",
145
    "                                                target_values_operator='AND',\n",
146
    "                                                are_keys_also_matched=True,\n",
147
    "                                                is_case_sensitive=False, \n",
148
    "                                                metric=\"SSIM\", \n",
149
    "                                                order=\"asc\")\n",
150
    "print(ranked_models)"
151
   ]
152
  },
153
  {
154
   "cell_type": "markdown",
155
   "metadata": {},
156
   "source": [
157
    "Find the models, then rank them, and then generate samples with the best ranked model.\n"
158
   ]
159
  },
160
  {
161
   "cell_type": "code",
162
   "execution_count": null,
163
   "metadata": {},
164
   "outputs": [],
165
   "source": [
166
    "generators.find_models_rank_and_generate(values=values_list, \n",
167
    "                                        target_values_operator='AND',\n",
168
    "                                        are_keys_also_matched=True,\n",
169
    "                                        is_case_sensitive=False, \n",
170
    "                                        metric=\"SSIM\", \n",
171
    "                                        order=\"asc\", \n",
172
    "                                        num_samples=5)"
173
   ]
174
  },
175
  {
176
   "cell_type": "markdown",
177
   "metadata": {},
178
   "source": [
179
    "Find all models that contain a specific key-value pair in their model config.\n"
180
   ]
181
  },
182
  {
183
   "cell_type": "code",
184
   "execution_count": null,
185
   "metadata": {},
186
   "outputs": [],
187
   "source": [
188
    "key = \"modality\"\n",
189
    "value = \"Full-Field Mammography\"\n",
190
    "found_models = generators.get_models_by_key_value_pair(key1=key, \n",
191
    "                                                        value1=value, \n",
192
    "                                                        is_case_sensitive=False)\n",
193
    "print(found_models)"
194
   ]
195
  }
196
 ],
197
 "metadata": {
198
  "interpreter": {
199
   "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
200
  },
201
  "kernelspec": {
202
   "display_name": "Python 3.9.10 64-bit",
203
   "language": "python",
204
   "name": "python3"
205
  },
206
  "language_info": {
207
   "codemirror_mode": {
208
    "name": "ipython",
209
    "version": 3
210
   },
211
   "file_extension": ".py",
212
   "mimetype": "text/x-python",
213
   "name": "python",
214
   "nbconvert_exporter": "python",
215
   "pygments_lexer": "ipython3",
216
   "version": "3.8.12"
217
  },
218
  "orig_nbformat": 4
219
 },
220
 "nbformat": 4,
221
 "nbformat_minor": 2
222
}