-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path030_train_cellpose.py
49 lines (39 loc) · 1.14 KB
/
030_train_cellpose.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 27 22:28:32 2024
@author: ian
"""
import os
from cellpose import models, io, train
import logging
logging.basicConfig(level=logging.INFO)
patch_dir = "patch"
label_dir = "patch_label"
model_path = "models"
patches = []
labels = []
print("Loading patches and labels for training")
def read_matching_images(patch_name, label_name):
patch_path = os.path.join(patch_dir, patch_name)
label_path = os.path.join(label_dir, label_name)
patch = io.imread(patch_path)
label = io.imread(label_path)
return patch, label
for label_name in os.listdir(label_dir):
patch_name = label_name.split("_label")[0] + ".tif"
patch, label = read_matching_images(patch_name, label_name)
print(f"Reading {patch_name} matched to {label_name}")
patches.append(patch)
labels.append(label)
model = models.CellposeModel(gpu=True, model_type="cyto3")
n = 800
os.makedirs("models", exist_ok=True)
model_path = train.train_seg(
model.net,
train_data=patches,
train_labels=labels,
channels=[0, 0],
n_epochs=n,
model_name=f"psr_{n}epochs_{len(labels)}patches",
)