from fastai.basics import *
from fastai.vision.all import *
from fastai.vision.gan import *
from fastai.callback.all import *

from monai.networks.nets import UNet

from bioMONAI.data import *
from bioMONAI.transforms import *
/home/bm/miniforge3/envs/biomonai/lib/python3.11/site-packages/monai/utils/module.py:399: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.
  pkg = __import__(module)  # top level module
Disabling PyTorch because PyTorch >= 2.1 is required but found 2.0.1
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
28-Jul-25 20:04:57 - bfio.backends - WARNING  - Java backend is not available. This could be due to a missing dependency (jpype).
bs = 128
size = 64
def get_gt(path, gt_file_name="avg50.png"): 
    def _fn(fn): return Path(path/"gt")/f"{parent_label(fn)}"/gt_file_name
    return _fn
path = '../_data/Confocal_BPAE_B/raw'

data_ops = {
    'blocks':       (BioImageBlock(cls=BioImage), BioImageBlock(cls=BioImage)),              # define a `TransformBlock` tailored for bioimaging data
    'get_items':    get_image_files,                                                               # get image files in path
    'get_x':        get_gt(Path('../_data/Confocal_BPAE_B')),                                                                  # Label item with the parent folder name
    'get_y':        lambda x: x,                                                # Label item with the grandparent folder name
    'valid_pct':    0.05,                     # split data with the grandparent folder name
    'item_tfms':    [RandCrop2D(size)], # list of item transforms
    'batch_tfms':   Normalize.from_stats(torch.tensor([0.5,0.5,0.5]), torch.tensor([0.5,0.5,0.5])),
    'bs':           bs,                                                                    # batch size
}

data = BioDataLoaders.from_source(
    path,                           # root directory for data
    show_summary=False,             # print summary of the data
    **data_ops,                     # rest of method arguments
    )

# print length of training and validation datasets
print('train images:', len(data.train_ds.items), '\nvalidation images:', len(data.valid_ds.items))
train images: 800 
validation images: 200
data.show_batch(max_n=8)

# generator = basic_generator(64, n_channels=1, n_extra_layers=1)
generator = UNet(2, 1, 1, channels=(4, 8, 16), strides=(2, 2),)
# critic    = basic_critic(64, n_channels=3, n_extra_layers=1, act_cls=partial(nn.LeakyReLU, negative_slope=0.2))
critic    = gan_critic(n_channels=1)
learn = GANLearner.wgan(data, generator, critic, opt_func = RMSProp)

learn.recorder.train_metrics=True
learn.recorder.valid_metrics=False
learn.fit_one_cycle(100, 1e-3, wd=0.)
/home/bm/miniforge3/envs/biomonai/lib/python3.11/site-packages/fastai/callback/core.py:71: UserWarning: You are shadowing an attribute (generator) that exists in the learner. Use `self.learn.generator` to avoid this
  warn(f"You are shadowing an attribute ({name}) that exists in the learner. Use `self.learn.{name}` to avoid this")
/home/bm/miniforge3/envs/biomonai/lib/python3.11/site-packages/fastai/callback/core.py:71: UserWarning: You are shadowing an attribute (critic) that exists in the learner. Use `self.learn.critic` to avoid this
  warn(f"You are shadowing an attribute ({name}) that exists in the learner. Use `self.learn.{name}` to avoid this")
/home/bm/miniforge3/envs/biomonai/lib/python3.11/site-packages/fastai/callback/core.py:71: UserWarning: You are shadowing an attribute (gen_mode) that exists in the learner. Use `self.learn.gen_mode` to avoid this
  warn(f"You are shadowing an attribute ({name}) that exists in the learner. Use `self.learn.{name}` to avoid this")
epoch train_loss gen_loss crit_loss time
0 -2555.725342 -8.855750 -3524.030029 00:05
1 -9171.391602 -44.314968 -15667.432617 00:05
2 -20479.978516 -245.935425 -44006.261719 00:05
3 -29629.142578 -499.743134 -58261.878906 00:05
4 -36985.750000 -641.925293 -70105.820312 00:05
5 -41806.429688 -745.183350 -70526.367188 00:05
6 -44930.250000 -562.076660 -67684.757812 00:05
7 -48039.218750 -867.235535 -74037.320312 00:05
8 -50323.011719 -1301.510132 -70982.398438 00:05
9 -51990.070312 -1011.221497 -72361.929688 00:05
10 -52694.460938 -1070.567871 -68849.398438 00:05
11 -54215.046875 -1278.330078 -70736.320312 00:05
12 -55039.480469 -1271.100220 -69847.812500 00:05
13 -56330.246094 -1383.572266 -75332.367188 00:05
14 -56714.425781 -1381.441528 -70363.679688 00:05
15 -56997.082031 -1558.157593 -71483.023438 00:05
16 -57394.003906 -1719.754883 -69988.320312 00:05
17 -57469.859375 -1838.876587 -70168.250000 00:05
18 -57449.664062 -2124.592529 -67045.140625 00:05
19 -57892.289062 -1979.348755 -76002.695312 00:05
20 -58208.968750 -2267.419678 -70614.460938 00:05
21 -58570.417969 -2560.939453 -72913.187500 00:05
22 -58912.085938 -2772.954346 -72357.554688 00:05
23 -58710.218750 -2990.201904 -68624.109375 00:05
24 -58385.925781 -3307.741455 -67303.929688 00:05
25 -58325.656250 -3320.586182 -68148.953125 00:05
26 -58451.281250 -3523.760498 -66745.437500 00:05
27 -58263.949219 -3617.496826 -67114.085938 00:05
28 -58247.828125 -3918.816650 -67373.429688 00:05
29 -58734.609375 -4046.113525 -71666.023438 00:05
30 -58417.695312 -4127.087891 -66409.718750 00:05
31 -58451.921875 -4379.448730 -68604.960938 00:05
32 -58119.710938 -4690.789551 -65217.980469 00:05
33 -57963.933594 -4833.750488 -66986.523438 00:05
34 -57998.644531 -4800.651855 -67332.304688 00:05
35 -58251.468750 -4936.716797 -71206.867188 00:05
36 -58448.957031 -4950.596191 -69872.226562 00:05
37 -58461.574219 -5231.733887 -68034.523438 00:05
38 -58602.554688 -5506.718262 -69505.117188 00:05
39 -59480.609375 -5747.910156 -75871.429688 00:05
40 -58995.625000 -5267.629395 -64849.859375 00:05
41 -58977.128906 -5611.252441 -65881.695312 00:05
42 -58801.718750 -5676.598145 -65844.492188 00:05
43 -58302.402344 -5836.274902 -64666.105469 00:05
44 -58347.101562 -6094.844238 -68369.851562 00:05
45 -58286.523438 -6109.770996 -67618.445312 00:05
46 -58374.449219 -6296.898438 -69571.625000 00:05
47 -58650.578125 -6659.979980 -72281.070312 00:05
48 -59117.601562 -6838.660645 -73362.617188 00:05
49 -59513.003906 -7379.285645 -73517.914062 00:05
50 -59659.074219 -6954.452637 -69732.445312 00:05
51 -59484.953125 -6999.523438 -67509.250000 00:05
52 -59320.210938 -7272.484863 -66735.820312 00:05
53 -59221.730469 -6849.975098 -70904.695312 00:05
54 -59152.105469 -7148.956543 -67752.710938 00:05
55 -58850.554688 -7220.743652 -65474.355469 00:05
56 -58561.949219 -7536.355957 -66434.351562 00:05
57 -58720.746094 -7684.017090 -70128.164062 00:05
58 -59213.441406 -7937.337402 -74009.953125 00:05
59 -59918.703125 -8084.930176 -74281.617188 00:05
60 -57586.945312 -3723.216797 -52365.136719 00:05
61 -58046.992188 -8134.827637 -70903.156250 00:05
62 -58271.789062 -9151.311523 -69963.273438 00:05
63 -58337.546875 -7649.717285 -67126.406250 00:05
64 -58684.027344 -8468.325195 -70284.281250 00:05
65 -59827.761719 -9247.046875 -78048.507812 00:05
66 -59587.492188 -7925.616699 -67480.078125 00:05
67 -59939.238281 -8443.958008 -72519.539062 00:05
68 -59456.238281 -8623.256836 -68618.023438 00:05
69 -59108.722656 -8669.090820 -64234.218750 00:05
70 -59420.562500 -8938.526367 -71713.085938 00:05
71 -59181.535156 -6871.854980 -66008.382812 00:05
72 -59059.433594 -8419.147461 -66976.046875 00:05
73 -58972.750000 -8657.824219 -68083.945312 00:05
74 -58900.992188 -8772.456055 -68560.007812 00:05
75 -58852.097656 -8931.586914 -68600.101562 00:05
76 -59197.417969 -8979.152344 -71580.406250 00:05
77 -59542.625000 -9254.100586 -73398.328125 00:05
78 -60583.347656 -9472.500977 -76918.570312 00:05
79 -59862.980469 -9991.527344 -66114.593750 00:05
80 -59912.042969 -9010.300781 -68012.812500 00:05
81 -60151.687500 -9152.114258 -71084.070312 00:05
82 -60573.488281 -9745.797852 -74115.554688 00:05
83 -61072.933594 -8934.475586 -73050.148438 00:05
84 -61386.250000 -12126.792969 -75693.921875 00:05
85 -60748.492188 -8996.679688 -64332.550781 00:05
86 -60856.929688 -9140.695312 -69207.898438 00:05
87 -61205.265625 -9348.092773 -75234.812500 00:05
88 -61060.812500 -9673.237305 -70567.179688 00:05
89 -60800.796875 -9457.715820 -68463.851562 00:05
90 -61188.296875 -9424.264648 -72567.539062 00:05
91 -60895.378906 -9552.094727 -68528.234375 00:05
92 -61207.582031 -9553.511719 -73301.593750 00:05
93 -61169.050781 -9681.491211 -69688.570312 00:05
94 -61118.429688 -9749.852539 -70681.375000 00:05
95 -62075.406250 -9744.019531 -78722.742188 00:05
96 -62198.058594 -9968.821289 -72208.546875 00:05
97 -62314.414062 -9765.531250 -72931.539062 00:05
98 -62202.933594 -9711.192383 -72671.250000 00:05
99 -62022.523438 -9778.769531 -71103.945312 00:05
learn.show_results(max_n=4, ds_idx=0)
/home/bm/miniforge3/envs/biomonai/lib/python3.11/site-packages/fastai/callback/core.py:71: UserWarning: You are shadowing an attribute (generator) that exists in the learner. Use `self.learn.generator` to avoid this
  warn(f"You are shadowing an attribute ({name}) that exists in the learner. Use `self.learn.{name}` to avoid this")
/home/bm/miniforge3/envs/biomonai/lib/python3.11/site-packages/fastai/callback/core.py:71: UserWarning: You are shadowing an attribute (critic) that exists in the learner. Use `self.learn.critic` to avoid this
  warn(f"You are shadowing an attribute ({name}) that exists in the learner. Use `self.learn.{name}` to avoid this")
/home/bm/miniforge3/envs/biomonai/lib/python3.11/site-packages/fastai/callback/core.py:71: UserWarning: You are shadowing an attribute (gen_mode) that exists in the learner. Use `self.learn.gen_mode` to avoid this
  warn(f"You are shadowing an attribute ({name}) that exists in the learner. Use `self.learn.{name}` to avoid this")