Diff of /class_runner.py [000000] .. [5021a4]

Switch to unified view

a b/class_runner.py
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Wed Sep  2 15:54:42 2020
4
5
@author: Billy
6
"""
7
import CellDetector
8
import DataPreparation
9
import unet
10
import os
11
12
#Example Script using all of the created classes:
13
14
#assemble all svs into a folder and save the name of the folder as svs_loc
15
svs_loc = "C:\\Users\\Billy\\Downloads\\Data"
16
17
#save the model checkpoint folder, so that it can be loaded when the unet is initialised
18
model_loc = 'N:\\8_(384, 384, 3)\\cp.ckpt'
19
20
#initialise your data prep object with the svs_loc
21
cd = DataPreparation.DataPreparation(svs_loc)
22
23
#initialise your unet model with the location of your .cpkt file, 
24
#which will be on the same level as all of the wieghts and model parameters
25
unet = unet.uNet_segmentor(model_loc,window = 384, first_conv=8)
26
27
#this method automatically crops and covnerts all svs into png images
28
cd.AutocropAll()
29
30
#this will create subfolders, with converted, cropped version of the svs files.
31
#These subfolders will contain png files corresponding to images from each svs file.
32
#loop through these subfolders and analyse them, like so:
33
34
#firstly the pngs were saved in a sibling folder to svs_loc.
35
#this process saves the locations of the pngs as png_loc:
36
dir_ = os.path.split(svs_loc)[0]
37
png_loc = os.path.join(dir_, "Prepared_SVS")
38
39
#using this location, create a list of subfolders:
40
subfolders = [x[0] for x in os.walk(png_loc)][1:]
41
42
43
44
# The next part makes predictions for every image that was generated by cd.AutocropAll(),
45
#and generates a dataset for each subfolder.
46
#
47
#It is useful at this stage to go into each subfolder and check the cropping is correct on all photos. Ideally,
48
#the png images would be cropped such that only the epithelium is contained. Using your operating system's
49
#photo manipulation app, rotate/straighten the photo and crop, to cut as much stroma and background out as possible.
50
for subfolder in subfolders:
51
    subfolder_loc = os.path.join(dir_,subfolder)
52
    for file in os.listdir(subfolder):
53
        file_loc = os.path.join(subfolder_loc,file)
54
        unet.predict(file_loc)
55
        
56
    cd = CellDetector.CellDetector(subfolder_loc, cell_only = True, isOpt = True)
57
    cd.predictAll(subfolder_loc, isOpt=True, cell_only=False)
58
    data = cd.dataCleaner(subfolder_loc, cell_only=True)
59
    
60
    
61
    
62
# if you wish to train the model, you need to provide the location of the training images 
63
# and their corresponding ground truths, like so:
64
65
image_loc = "N:\\Bill_Mcgough\\Correct Labelling\\Labels\\ImageData"
66
label_loc = "N:\\Bill_Mcgough\\Correct Labelling\\Labels\\MatlabLabelData"
67
unet.learn_from_matlab(image_loc, label_loc, photo_per_mask=65, batch = 10, epochs = 5000, colour = True)
68