Skip to content

Instantly share code, notes, and snippets.

@andreped
Created May 22, 2023 08:27
Show Gist options
  • Select an option

  • Save andreped/134a8df7f3f0ea34dd690e76a33462a6 to your computer and use it in GitHub Desktop.

Select an option

Save andreped/134a8df7f3f0ea34dd690e76a33462a6 to your computer and use it in GitHub Desktop.
torchstain-macenko-test.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMtC8uHAdAPEBJRr8YHFLCy",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/andreped/134a8df7f3f0ea34dd690e76a33462a6/torchstain-macenko-test.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ri50qeSVhxmy",
"outputId": "11da57f6-6707-4a4c-9ec9-2b6c20fd1829"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
"Collecting torchstain[torch]\n",
" Downloading torchstain-1.3.0-py3-none-any.whl (28 kB)\n",
"Requirement already satisfied: torchvision in /usr/local/lib/python3.10/dist-packages (0.15.2+cu118)\n",
"Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from torchstain[torch]) (1.22.4)\n",
"Requirement already satisfied: torch in /usr/local/lib/python3.10/dist-packages (from torchstain[torch]) (2.0.1+cu118)\n",
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from torchvision) (2.27.1)\n",
"Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /usr/local/lib/python3.10/dist-packages (from torchvision) (8.4.0)\n",
"Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch->torchstain[torch]) (3.12.0)\n",
"Requirement already satisfied: typing-extensions in /usr/local/lib/python3.10/dist-packages (from torch->torchstain[torch]) (4.5.0)\n",
"Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch->torchstain[torch]) (1.11.1)\n",
"Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch->torchstain[torch]) (3.1)\n",
"Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch->torchstain[torch]) (3.1.2)\n",
"Requirement already satisfied: triton==2.0.0 in /usr/local/lib/python3.10/dist-packages (from torch->torchstain[torch]) (2.0.0)\n",
"Requirement already satisfied: cmake in /usr/local/lib/python3.10/dist-packages (from triton==2.0.0->torch->torchstain[torch]) (3.25.2)\n",
"Requirement already satisfied: lit in /usr/local/lib/python3.10/dist-packages (from triton==2.0.0->torch->torchstain[torch]) (16.0.5)\n",
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->torchvision) (1.26.15)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->torchvision) (2022.12.7)\n",
"Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests->torchvision) (2.0.12)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->torchvision) (3.4)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch->torchstain[torch]) (2.1.2)\n",
"Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch->torchstain[torch]) (1.3.0)\n",
"Installing collected packages: torchstain\n",
"Successfully installed torchstain-1.3.0\n",
"--2023-05-22 08:27:32-- https://user-images.githubusercontent.com/105635526/239820548-7ae8d9f2-6947-4720-927c-8f193ae051fa.jpg\n",
"Resolving user-images.githubusercontent.com (user-images.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...\n",
"Connecting to user-images.githubusercontent.com (user-images.githubusercontent.com)|185.199.108.133|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 27141 (27K) [image/jpeg]\n",
"Saving to: ‘239820548-7ae8d9f2-6947-4720-927c-8f193ae051fa.jpg’\n",
"\n",
"239820548-7ae8d9f2- 100%[===================>] 26.50K --.-KB/s in 0.002s \n",
"\n",
"2023-05-22 08:27:32 (17.0 MB/s) - ‘239820548-7ae8d9f2-6947-4720-927c-8f193ae051fa.jpg’ saved [27141/27141]\n",
"\n",
"Cloning into 'torchstain'...\n",
"remote: Enumerating objects: 1021, done.\u001b[K\n",
"remote: Counting objects: 100% (243/243), done.\u001b[K\n",
"remote: Compressing objects: 100% (120/120), done.\u001b[K\n",
"remote: Total 1021 (delta 137), reused 165 (delta 118), pack-reused 778\u001b[K\n",
"Receiving objects: 100% (1021/1021), 2.53 MiB | 24.68 MiB/s, done.\n",
"Resolving deltas: 100% (519/519), done.\n"
]
}
],
"source": [
"!pip install torchstain[torch] torchvision\n",
"!wget https://user-images.githubusercontent.com/105635526/239820548-7ae8d9f2-6947-4720-927c-8f193ae051fa.jpg\n",
"!git clone https://github.com/EIDOSLAB/torchstain.git"
]
},
{
"cell_type": "code",
"source": [
"!ls -haltr"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Mx4XEVGMi0on",
"outputId": "a1683e14-e047-4043-a237-2e1e5619c7b5"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"total 48K\n",
"drwxr-xr-x 4 root root 4.0K May 18 13:37 .config\n",
"drwxr-xr-x 1 root root 4.0K May 18 13:38 sample_data\n",
"-rw-r--r-- 1 root root 27K May 22 06:27 239820548-7ae8d9f2-6947-4720-927c-8f193ae051fa.jpg\n",
"drwxr-xr-x 1 root root 4.0K May 22 08:26 ..\n",
"drwxr-xr-x 1 root root 4.0K May 22 08:27 .\n",
"drwxr-xr-x 7 root root 4.0K May 22 08:27 torchstain\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import torch\n",
"from torchvision import transforms\n",
"import torchstain\n",
"import cv2\n",
"import os\n",
"\n",
"\n",
"for target_path in [\"./239820548-7ae8d9f2-6947-4720-927c-8f193ae051fa.jpg\", \"./torchstain/data/target.png\"]:\n",
" \n",
" target = cv2.cvtColor(cv2.imread(target_path), cv2.COLOR_BGR2RGB)\n",
" to_transform = cv2.cvtColor(cv2.imread(\"./239820548-7ae8d9f2-6947-4720-927c-8f193ae051fa.jpg\"), cv2.COLOR_BGR2RGB)\n",
"\n",
" T = transforms.Compose([\n",
" transforms.ToTensor(),\n",
" transforms.Lambda(lambda x: x*255)\n",
" ])\n",
"\n",
" torch_normalizer = torchstain.normalizers.MacenkoNormalizer(backend='torch')\n",
" torch_normalizer.fit(T(target))\n",
"\n",
" t_to_transform = T(to_transform)\n",
" norm, H, E = torch_normalizer.normalize(I=t_to_transform, stains=True)\n"
],
"metadata": {
"id": "qT0dKLldh5q_"
},
"execution_count": 3,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment