**Model trained On the AM0.1-FLUKE dataset**
Setup environment¶
Imports¶
from cellsegment.core import *
from cellsegment.inference_utils import *
from cellsegment.json_utils import *
from cellsegment.dataprep_utils import *
from cellsegment.set_directories import *
from cellsegment.markup_utils import * #hide this otherwise recurseive
import pandas
from fastai.vision import *
Define directories¶
local_datapath = '../testdata_2'
# local_datapath = '../../data/FEC/03-M100-Fluke-2019-11/'
dirs = Dirs('data') if IN_COLAB else Dirs(local_datapath)
dirs.model = dirs.model if IN_COLAB else '/home/john/github/data/FEC/03-M100-Fluke-2019-11/models'
print(dirs)
Load the Training Images¶
(if in colab)
%%bash
[[ ! -e /tools/google-cloud-sdk ]] && exit # if in colab
export fileid=1SEW0Kf1CI4e4-up4TGsDqwDwVk_QZEUf
export filename=Fluke-Train-2019-12-01.zip
## CURL ##
curl -L -c cookies.txt 'https://docs.google.com/uc?export=download&id='$fileid \
| sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1/p' > confirm.txt
curl -L -b cookies.txt -o $filename \
'https://docs.google.com/uc?export=download&id='$fileid'&confirm='$(<confirm.txt)
rm -f confirm.txt cookies.txt
unzip -u -q $filename -d data
Load exported Model¶
%%bash
[[ ! -e /tools/google-cloud-sdk ]] && exit # if in colab
switch=true
if $switch; then
export fileid=11cZWhg23QDag_3b7jcd02U8Pzq3W6U5Y
export filename=export-fluke-2019-12-01.pkl
## CURL ##
curl -L -c cookies.txt 'https://docs.google.com/uc?export=download&id='$fileid \
| sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1/p' > confirm.txt
curl -L -b cookies.txt -o $filename \
'https://docs.google.com/uc?export=download&id='$fileid'&confirm='$(<confirm.txt)
rm -f confirm.txt cookies.txt
fi
Create Learner from exported model¶
# defaults.device = 'cpu'
defaults.device = 'cuda'
if torch.cuda.is_available():
def acc_metric1(input, target):
target = target.squeeze(1)
return (input.argmax(dim=1)==target).float().mean()
def acc_metric2(input, target):
target = target.squeeze(1)
return (input.argmax(dim=1)[target>0]==target[target>0]).float().mean()
fn_model = f'{dirs.model}/export-fluke-2019-12-01.pkl'
learn = load_learner('', fn_model)
learn.model.float()
# summary(learn.model, (3, 32, 32))
print("Learner loaded")
else:
print("Learner not loaded as torch.cuda.is_available() = ", torch.cuda.is_available())
Run a prediction on a test image
img_path = Path(dirs.train)
mrk_path = Path(dirs.train+'/../markup')
mrk_path.mkdir(parents=True, exist_ok=True)
fnames = sorted(get_image_files(img_path))
fn = fnames[1]
fn
preds = run_prediction(learn, fn, tile=False, filesavedir=mrk_path)
for cls, img in list(preds.items()):
print(cls, img.shape)
# for cls,raw_pred in preds.items():
# img = np.asarray(PIL.Image.open(fn))
# show_img(img)
# save_png_p(to_np(raw_pred), f'{mrk_path}/{fn.stem}-{cls}.png')
fig, axes = plt.subplots(1, 5, figsize=(15, 3))
ax = axes.flat[0]
img = open_image(fn)
im = ax.imshow(img.data.permute(1,2,0).numpy())
ax.set_axis_off()
ax.set_title(fn.name)
titles = ['Background','Fluke-Liver','Fluke-Rumen','other']
for i, ax in enumerate(axes.flat[1:]):
arr = np.asarray(PIL.Image.open(f'{mrk_path}/{fn.stem}-{cls}.png'))
im = ax.imshow(arr)
ax.set_axis_off()
ax.set_title(titles[i])
print("Class labels")
CLASS_LABELS
Marking up an image¶
mrk_img, pred_list, jdata = markup_one_image(fn, preds)
show_img(mrk_img, figsize=(10,10))
print(fn)
pred_list
fn
# img_path = Path(dirs.train)
# mrk_path = Path('../testdata_2/Fullsize/markup')
# mrk_path.mkdir(parents=True, exist_ok=True)
#
# fnames = sorted(get_image_files(img_path))
# fnames = fnames[:3]
# classes = ['Background', 'Fluke_Liver', 'Fluke_Rumen', 'Other']
#
# fn = fnames[1]
# preds = run_prediction(learn, fn, classes=classes, tile=False, filesavedir=mrk_path)
#
# preds
# predictions, img, regions = find_prediction_blobs( to_np(preds['Fluke_Liver']))
# show_img(img, figsize=(8,8))
# regions
# # annotate_json