# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.6.9 64-bit (''env'': virtualenv)'
#     language: python
#     name: python3
# ---

# +
import os
import pprint
from pathlib import Path

import tensorflow as tf
import json
import yaml
import pandas as pd
from tqdm import tqdm

from src.data.monuseg.tf_data import get_dataset
from src.models.monuseg.train_model import eval, loss
from src.models.monuseg.models import get_model

os.environ["CUDA_VISIBLE_DEVICES"] = "3"
pp = pprint.PrettyPrinter(indent=4)

# +
path_indices = "/home/valentin/python_wkspce/2d_bispectrum_cnn/data/indices/monuseg.json"
config = "/home/valentin/python_wkspce/2d_bispectrum_cnn/src/models/monuseg/configs/bispect_nh1.yaml"
# config = "/home/valentin/python_wkspce/2d_bispectrum_cnn/src/models/monuseg/configs/unet_default.yaml"
with open(path_indices, "r") as f:
    indices_list = json.load(f)


with open(config, 'r') as f:
    params = yaml.safe_load(f)
# -

ds_test = get_dataset(id_list=indices_list[0]["test"], instance=True)
ds_test = ds_test.cache().batch(1)

params["n_harmonics"] = 8
params

model = get_model(model_name=params["model_name"],
                  output_channels=3,
                  loss=loss,
                  n_harmonics=params["n_harmonics"],
                  cosine_decay=params["cosine_decay"],
                  last_activation="softmax",
                  run_eagerly=False)




model.load_weights('/home/valentin/python_wkspce/2d_bispectrum_cnn/models/MoNuSeg/BispectUnet__rotation_True__nh_8__n_train_-1__psize_60x60__20211202-173734/weights/split_0/final')
# model.load_weights('/home/valentin/python_wkspce/2d_bispectrum_cnn/models/MoNuSeg/BispectUnet__rotation_True__nh_8__n_train_-1__psize_60x60__20211202-173734/weights/split_0/final')
# model = tf.keras.models.load_model(
#     '/home/valentin/python_wkspce/2d_bispectrum_cnn/models/test',
#     compile=False)
# model.compile(
#     loss=[loss],
#     optimizer=tf.keras.optimizers.Adam(1e-3),
#     run_eagerly=False,
# )
# eval


# +
# scores_test = eval(ds=ds_test,
#                    model=model,
#                    cropper=tf.keras.layers.Cropping2D(cropping=(20, 20)))
                   
# pp.pprint(scores_test)

# +
model_path = Path(
    "/home/valentin/python_wkspce/2d_bispectrum_cnn/models/MoNuSeg/"
    "BispectUnet__rotation_True__nh_8__n_train_-1__psize_60x60__20211202-173734"
    "/weights/")

# model_path = Path(
#     "/home/valentin/python_wkspce/2d_bispectrum_cnn/models/MoNuSeg/"
#     "Unet__rotation_True__nh_0__n_train_-1__psize_60x60__20211202-132039"
#     "/weights/")

# -

cropper = tf.keras.layers.Cropping2D(cropping=(20, 20))
scores = pd.DataFrame()
for split in tqdm(range(10)):
    model.load_weights(model_path / f"split_{split}/final")
    ds_test = get_dataset(id_list=indices_list[split]["test"],
                          instance=True).batch(1)
    scores = scores.append(
        {
            "split": split,
            **eval(ds=ds_test, model=model, cropper=cropper),
        },
        ignore_index=True,
    )


scores

scores = pd.read_csv("scores_nh8_test.csv")

scores.mean()

scores.std()

scores.to_csv("scores_nh8_test.csv")



StarcoderdataJupyterScriptsDedupFiltered