[3ee609]: / examples / tutorial.ipynb

Download this file

223 lines (222 with data), 6.1 kB

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MEDIGAN Quick start\n",
    "Quick introduction on how to choose the right model and generate images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install medigan"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Import medigan and initialize Generators\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from medigan import Generators\n",
    "generators = Generators()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generate 10 samples using one of the medigan models\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "generators.generate(model_id=\"00001_DCGAN_MMG_CALC_ROI\", num_samples=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get the model's generate method and run it to generate 3 samples\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gen_function = generators.get_generate_function(model_id=\"00001_DCGAN_MMG_CALC_ROI\", \n",
    "                                                num_samples=3)\n",
    "gen_function()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a list of search terms and find the models that have these terms in their config.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "values_list = ['dcgan', 'Mammography', 'inbreast']\n",
    "models = generators.find_matching_models_by_values(values=values_list, \n",
    "                                                    target_values_operator='AND', \n",
    "                                                    are_keys_also_matched=True, \n",
    "                                                    is_case_sensitive=False)\n",
    "print(f'Found models: {models}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a list of search terms, find a model and generate\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "values_list = ['dcgan', 'mMg', 'ClF', 'modalities', 'inbreast']\n",
    "generators.find_model_and_generate(values=values_list, \n",
    "                                    target_values_operator='AND', \n",
    "                                    are_keys_also_matched=True, \n",
    "                                    is_case_sensitive=False, \n",
    "                                    num_samples=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Rank the models by a performance metric and return ranked list of models\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ranked_models = generators.rank_models_by_performance(metric=\"SSIM\", \n",
    "                                                        order=\"asc\")\n",
    "print(ranked_models)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Find the models, then rank them by a performance metric and return ranked list of models\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ranked_models = generators.find_models_and_rank(values=values_list, \n",
    "                                                target_values_operator='AND',\n",
    "                                                are_keys_also_matched=True,\n",
    "                                                is_case_sensitive=False, \n",
    "                                                metric=\"SSIM\", \n",
    "                                                order=\"asc\")\n",
    "print(ranked_models)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Find the models, then rank them, and then generate samples with the best ranked model.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "generators.find_models_rank_and_generate(values=values_list, \n",
    "                                        target_values_operator='AND',\n",
    "                                        are_keys_also_matched=True,\n",
    "                                        is_case_sensitive=False, \n",
    "                                        metric=\"SSIM\", \n",
    "                                        order=\"asc\", \n",
    "                                        num_samples=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Find all models that contain a specific key-value pair in their model config.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "key = \"modality\"\n",
    "value = \"Full-Field Mammography\"\n",
    "found_models = generators.get_models_by_key_value_pair(key1=key, \n",
    "                                                        value1=value, \n",
    "                                                        is_case_sensitive=False)\n",
    "print(found_models)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
  },
  "kernelspec": {
   "display_name": "Python 3.9.10 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}