Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
jizhang02 committed Sep 24, 2024
1 parent 86b3116 commit a69e198
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 8 deletions.
2 changes: 1 addition & 1 deletion deep_learning_from_scratch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19237,7 +19237,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.6"
},
"vscode": {
"interpreter": {
Expand Down
189 changes: 182 additions & 7 deletions dnn_marathon.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2304,32 +2304,207 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Keras 3 examples"
"interatcitve selection"
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "181bd79b64604e1f9a6729fa4ff46611",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Dropdown(description='Model Type:', index=1, options=('mar_base', 'mar_large', 'mar_huge'), value='mar_large')"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.0.1\n"
"Selected model: mar_base\n",
"Selected model: mar_huge\n"
]
}
],
"source": [
"import keras\n",
"print(keras.__version__)"
"import ipywidgets as widgets\n",
"from IPython.display import display\n",
"\n",
"# 创建下拉菜单\n",
"model_type = widgets.Dropdown(\n",
" options=['mar_base', 'mar_large', 'mar_huge'],\n",
" value='mar_large', # 默认选择值\n",
" description='Model Type:',\n",
")\n",
"\n",
"# 显示下拉菜单\n",
"display(model_type)\n",
"\n",
"# 定义一个更新函数,用来打印更新后的值\n",
"def on_change(change):\n",
" print(f\"Selected model: {model_type.value}\")\n",
"\n",
"# 当用户更改下拉菜单的选项时触发更新\n",
"model_type.observe(on_change, names='value')\n",
"\n",
"# 定义另一个函数,用于后续使用model_type\n",
"def use_model_type():\n",
" return model_type.value\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'mar_huge'"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 调用函数以显示当前选择的模型类型\n",
"use_model_type()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Self Supervised Learning\n",
"refercence:https://github.com/lightly-ai/lightly?tab=readme-ov-file \n",
" https://docs.lightly.ai/self-supervised-learning/index.html"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"loss: 5.42013\n",
"loss: 5.51289\n",
"loss: 5.45845\n",
"loss: 5.45596\n",
"loss: 5.40158\n",
"loss: 5.40549\n",
"loss: 5.41606\n",
"loss: 5.42258\n",
"loss: 5.51112\n",
"loss: 5.36894\n"
]
}
],
"source": [
"import torchvision\n",
"import torch\n",
"from lightly.loss import NTXentLoss\n",
"from lightly.models.modules.heads import SimCLRProjectionHead\n",
"from lightly.transforms.simclr_transform import SimCLRTransform\n",
"from lightly.data import LightlyDataset\n",
"\n",
"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
"\n",
"# The following transform will return two augmented images per input image.\n",
"transform = SimCLRTransform()\n",
"\n",
"# Create a dataset from your image folder.\n",
"dataset = LightlyDataset(\n",
" input_dir='/wholebody_2D/ct/',\n",
" transform=transform,\n",
")\n",
"\n",
"# Build a PyTorch dataloader.\n",
"dataloader = torch.utils.data.DataLoader(\n",
" dataset, # Pass the dataset to the dataloader.\n",
" batch_size=128, # A large batch size helps with learning.\n",
" shuffle=True, # Shuffling is important!\n",
")\n",
"\n",
"# use a resnet backbone\n",
"resnet = torchvision.models.resnet18()\n",
"resnet = torch.nn.Sequential(*list(resnet.children())[:-1])\n",
"\n",
"# build a SimCLR model\n",
"class SimCLR(torch.nn.Module):\n",
" def __init__(self, backbone, hidden_dim, out_dim):\n",
" super().__init__()\n",
" self.backbone = backbone\n",
" self.projection_head = SimCLRProjectionHead(hidden_dim, hidden_dim, out_dim)\n",
"\n",
" def forward(self, x):\n",
" h = self.backbone(x).flatten(start_dim=1)\n",
" z = self.projection_head(h)\n",
" return z\n",
"\n",
"model = SimCLR(resnet, hidden_dim=512, out_dim=128).to(device)\n",
"\n",
"# use a criterion for self-supervised learning\n",
"# (normalized temperature-scaled cross entropy loss)\n",
"criterion = NTXentLoss(temperature=0.5)\n",
"\n",
"# get a PyTorch optimizer\n",
"optimizer = torch.optim.SGD(model.parameters(), lr=0.1, weight_decay=1e-5)\n",
"\n",
"max_epochs = 10\n",
"for epoch in range(max_epochs):\n",
" for (x0, x1), _, _ in dataloader:\n",
"\n",
" x0 = x0.to(device)\n",
" x1 = x1.to(device)\n",
"\n",
" z0 = model(x0)\n",
" z1 = model(x1)\n",
"\n",
" loss = criterion(z0, z1)\n",
" loss.backward()\n",
"\n",
" optimizer.step()\n",
" optimizer.zero_grad()\n",
" print(f\"loss: {loss.item():.5f}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### MONAI framework"
"### Keras 3 examples"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.0.0\n"
]
}
],
"source": [
"import keras\n",
"print(keras.__version__)"
]
}
],
Expand All @@ -2349,7 +2524,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.6"
},
"vscode": {
"interpreter": {
Expand Down

0 comments on commit a69e198

Please sign in to comment.