|
| 1 | +# %% Imports and paths. |
| 2 | +import timm |
| 3 | +import torch |
| 4 | +import torchview |
| 5 | + |
| 6 | +from viscy.light.engine import ContrastiveModule |
| 7 | +from viscy.representation.contrastive import ContrastiveEncoder, UNeXt2Stem |
| 8 | + |
| 9 | +# %load_ext autoreload |
| 10 | +# %autoreload 2 |
| 11 | +# %% Initialize the model and log the graph. |
| 12 | +contra_model = ContrastiveEncoder( |
| 13 | + backbone="convnext_tiny" |
| 14 | +) # other options: convnext_tiny resnet50 |
| 15 | +print(contra_model) |
| 16 | +model_graph = torchview.draw_graph( |
| 17 | + contra_model, |
| 18 | + torch.randn(1, 2, 15, 224, 224), |
| 19 | + depth=3, # adjust depth to zoom in. |
| 20 | + device="cpu", |
| 21 | +) |
| 22 | +# Print the image of the model. |
| 23 | +model_graph.resize_graph(scale=2.5) |
| 24 | +model_graph.visual_graph |
| 25 | + |
| 26 | +# %% Initialize a resent50 model and log the graph. |
| 27 | +contra_model = ContrastiveEncoder( |
| 28 | + backbone="resnet50", in_stack_depth=16, stem_kernel_size=(4, 3, 3) |
| 29 | +) # note that the resnet first layer takes 64 channels (so we can't have multiples of 3) |
| 30 | +print(contra_model) |
| 31 | +model_graph = torchview.draw_graph( |
| 32 | + contra_model, |
| 33 | + torch.randn(1, 2, 16, 224, 224), |
| 34 | + depth=3, # adjust depth to zoom in. |
| 35 | + device="cpu", |
| 36 | +) |
| 37 | +# Print the image of the model. |
| 38 | +model_graph.resize_graph(scale=2.5) |
| 39 | +model_graph.visual_graph |
| 40 | + |
| 41 | + |
| 42 | +# %% Initiatlize the lightning module and view the model. |
| 43 | +contrastive_module = ContrastiveModule() |
| 44 | +print(contrastive_module.encoder) |
| 45 | + |
| 46 | +# %% |
| 47 | +model_graph = torchview.draw_graph( |
| 48 | + contrastive_module.encoder, |
| 49 | + torch.randn(1, 2, 15, 200, 200), |
| 50 | + depth=3, # adjust depth to zoom in. |
| 51 | + device="cpu", |
| 52 | +) |
| 53 | +# Print the image of the model. |
| 54 | +model_graph.visual_graph |
| 55 | + |
| 56 | +# %% Playground |
| 57 | + |
| 58 | +available_models = timm.list_models(pretrained=True) |
| 59 | + |
| 60 | +stem = UNeXt2Stem( |
| 61 | + in_channels=2, out_channels=96, kernel_size=(5, 2, 2), in_stack_depth=15 |
| 62 | +) |
| 63 | +print(stem) |
| 64 | +stem_graph = torchview.draw_graph( |
| 65 | + stem, |
| 66 | + torch.randn(1, 2, 15, 256, 256), |
| 67 | + depth=2, # adjust depth to zoom in. |
| 68 | + device="cpu", |
| 69 | +) |
| 70 | +# Print the image of the model. |
| 71 | +stem_graph.visual_graph |
| 72 | +# %% |
| 73 | +encoder = timm.create_model( |
| 74 | + "convnext_tiny", |
| 75 | + pretrained=True, |
| 76 | + features_only=False, |
| 77 | + num_classes=200, |
| 78 | +) |
| 79 | + |
| 80 | +print(encoder) |
| 81 | + |
| 82 | +# %% |
| 83 | + |
| 84 | +encoder.stem = stem |
| 85 | + |
| 86 | +model_graph = torchview.draw_graph( |
| 87 | + encoder, |
| 88 | + torch.randn(1, 2, 15, 256, 256), |
| 89 | + depth=2, # adjust depth to zoom in. |
| 90 | + device="cpu", |
| 91 | +) |
| 92 | +# Print the image of the model. |
| 93 | +model_graph.visual_graph |
| 94 | +# %% |
| 95 | +encoder.stem = torch.nn.Identity() |
| 96 | + |
| 97 | +encoder_graph = torchview.draw_graph( |
| 98 | + encoder, |
| 99 | + torch.randn(1, 96, 128, 128), |
| 100 | + depth=2, # adjust depth to zoom in. |
| 101 | + device="cpu", |
| 102 | +) |
| 103 | +# Print the image of the model. |
| 104 | +encoder_graph.visual_graph |
| 105 | + |
| 106 | +# %% |
0 commit comments