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
+
"kernelspec": {
|
4 |
+
"language": "python",
|
5 |
+
"display_name": "Python 3",
|
6 |
+
"name": "python3"
|
7 |
+
},
|
8 |
+
"language_info": {
|
9 |
+
"name": "python",
|
10 |
+
"version": "3.7.12",
|
11 |
+
"mimetype": "text/x-python",
|
12 |
+
"codemirror_mode": {
|
13 |
+
"name": "ipython",
|
14 |
+
"version": 3
|
15 |
+
},
|
16 |
+
"pygments_lexer": "ipython3",
|
17 |
+
"nbconvert_exporter": "python",
|
18 |
+
"file_extension": ".py"
|
19 |
+
},
|
20 |
+
"kaggle": {
|
21 |
+
"accelerator": "gpu",
|
22 |
+
"dataSources": [
|
23 |
+
{
|
24 |
+
"sourceId": 9679350,
|
25 |
+
"sourceType": "datasetVersion",
|
26 |
+
"datasetId": 5916065
|
27 |
+
}
|
28 |
+
],
|
29 |
+
"dockerImageVersionId": 30302,
|
30 |
+
"isInternetEnabled": true,
|
31 |
+
"language": "python",
|
32 |
+
"sourceType": "notebook",
|
33 |
+
"isGpuEnabled": true
|
34 |
+
},
|
35 |
+
"colab": {
|
36 |
+
"name": "Hack49-Training",
|
37 |
+
"provenance": []
|
38 |
+
}
|
39 |
+
},
|
40 |
+
"nbformat_minor": 0,
|
41 |
+
"nbformat": 4,
|
42 |
+
"cells": [
|
43 |
+
{
|
44 |
+
"source": [
|
45 |
+
"# IMPORTANT: SOME KAGGLE DATA SOURCES ARE PRIVATE\n",
|
46 |
+
"# RUN THIS CELL IN ORDER TO IMPORT YOUR KAGGLE DATA SOURCES.\n",
|
47 |
+
"import kagglehub\n",
|
48 |
+
"kagglehub.login()\n"
|
49 |
+
],
|
50 |
+
"metadata": {
|
51 |
+
"id": "qWlTgQVSmgbJ"
|
52 |
+
},
|
53 |
+
"cell_type": "code",
|
54 |
+
"outputs": [],
|
55 |
+
"execution_count": null
|
56 |
+
},
|
57 |
+
{
|
58 |
+
"source": [
|
59 |
+
"# IMPORTANT: RUN THIS CELL IN ORDER TO IMPORT YOUR KAGGLE DATA SOURCES,\n",
|
60 |
+
"# THEN FEEL FREE TO DELETE THIS CELL.\n",
|
61 |
+
"# NOTE: THIS NOTEBOOK ENVIRONMENT DIFFERS FROM KAGGLE'S PYTHON\n",
|
62 |
+
"# ENVIRONMENT SO THERE MAY BE MISSING LIBRARIES USED BY YOUR\n",
|
63 |
+
"# NOTEBOOK.\n",
|
64 |
+
"\n",
|
65 |
+
"datajediai_hack49_alzheimer_dataset_path = kagglehub.dataset_download('datajediai/hack49-alzheimer-dataset')\n",
|
66 |
+
"\n",
|
67 |
+
"print('Data source import complete.')\n"
|
68 |
+
],
|
69 |
+
"metadata": {
|
70 |
+
"id": "1uar05ngmgbJ"
|
71 |
+
},
|
72 |
+
"cell_type": "code",
|
73 |
+
"outputs": [],
|
74 |
+
"execution_count": null
|
75 |
+
},
|
76 |
+
{
|
77 |
+
"cell_type": "code",
|
78 |
+
"source": [
|
79 |
+
"pip install boto3"
|
80 |
+
],
|
81 |
+
"metadata": {
|
82 |
+
"execution": {
|
83 |
+
"iopub.status.busy": "2024-10-21T05:31:24.709096Z",
|
84 |
+
"iopub.execute_input": "2024-10-21T05:31:24.709572Z",
|
85 |
+
"iopub.status.idle": "2024-10-21T05:31:37.120182Z",
|
86 |
+
"shell.execute_reply.started": "2024-10-21T05:31:24.709539Z",
|
87 |
+
"shell.execute_reply": "2024-10-21T05:31:37.118905Z"
|
88 |
+
},
|
89 |
+
"trusted": true,
|
90 |
+
"id": "Mm8ZSAxkmgbK",
|
91 |
+
"outputId": "e4930bdb-3291-4ae3-ffb1-2cee2f31c0d8"
|
92 |
+
},
|
93 |
+
"execution_count": null,
|
94 |
+
"outputs": [
|
95 |
+
{
|
96 |
+
"name": "stdout",
|
97 |
+
"text": "Requirement already satisfied: boto3 in /opt/conda/lib/python3.7/site-packages (1.24.93)\nRequirement already satisfied: s3transfer<0.7.0,>=0.6.0 in /opt/conda/lib/python3.7/site-packages (from boto3) (0.6.0)\nRequirement already satisfied: jmespath<2.0.0,>=0.7.1 in /opt/conda/lib/python3.7/site-packages (from boto3) (1.0.1)\nRequirement already satisfied: botocore<1.28.0,>=1.27.93 in /opt/conda/lib/python3.7/site-packages (from boto3) (1.27.93)\nRequirement already satisfied: urllib3<1.27,>=1.25.4 in /opt/conda/lib/python3.7/site-packages (from botocore<1.28.0,>=1.27.93->boto3) (1.26.12)\nRequirement already satisfied: python-dateutil<3.0.0,>=2.1 in /opt/conda/lib/python3.7/site-packages (from botocore<1.28.0,>=1.27.93->boto3) (2.8.2)\nRequirement already satisfied: six>=1.5 in /opt/conda/lib/python3.7/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.28.0,>=1.27.93->boto3) (1.15.0)\n\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n\u001b[0mNote: you may need to restart the kernel to use updated packages.\n",
|
98 |
+
"output_type": "stream"
|
99 |
+
}
|
100 |
+
]
|
101 |
+
},
|
102 |
+
{
|
103 |
+
"cell_type": "code",
|
104 |
+
"source": [
|
105 |
+
"import os\n",
|
106 |
+
"import pandas as pd\n",
|
107 |
+
"import torch\n",
|
108 |
+
"import torchaudio\n",
|
109 |
+
"import torch.nn as nn\n",
|
110 |
+
"import torch.optim as optim\n",
|
111 |
+
"# Custom collate function for DataLoader\n",
|
112 |
+
"import torch.nn.functional as F\n",
|
113 |
+
"from torch.utils.data import Dataset, DataLoader, random_split\n",
|
114 |
+
"dataset_dir = '/kaggle/input/hack49-alzheimer-dataset/Hack49-Alzheimer-Dataset'\n",
|
115 |
+
"# Custom Dataset class\n",
|
116 |
+
"class HealthAudioDataset(Dataset):\n",
|
117 |
+
" def __init__(self, root_dir, device='cpu'):\n",
|
118 |
+
" self.root_dir = root_dir\n",
|
119 |
+
" self.file_list = []\n",
|
120 |
+
" self.labels = []\n",
|
121 |
+
" self.dataframe = []\n",
|
122 |
+
" self.device = device\n",
|
123 |
+
" for label, subdir in enumerate(['Healthy', 'NotHealthy']):\n",
|
124 |
+
" subdir_path = os.path.join(root_dir, subdir)\n",
|
125 |
+
" for wav_file in os.listdir(subdir_path):\n",
|
126 |
+
" if wav_file.endswith('.wav'):\n",
|
127 |
+
" self.file_list.append(os.path.join(subdir_path, wav_file))\n",
|
128 |
+
" self.labels.append(label)\n",
|
129 |
+
" self.dataframe = pd.DataFrame({'file_path': self.file_list, 'label': self.labels})\n",
|
130 |
+
"\n",
|
131 |
+
" def __len__(self):\n",
|
132 |
+
" return len(self.file_list)\n",
|
133 |
+
"\n",
|
134 |
+
" def __getitem__(self, idx):\n",
|
135 |
+
" wav_path = self.file_list[idx]\n",
|
136 |
+
" label = self.labels[idx]\n",
|
137 |
+
" waveform, sample_rate = torchaudio.load(wav_path)\n",
|
138 |
+
" waveform = waveform.to(self.device)\n",
|
139 |
+
" if sample_rate != bundle.sample_rate:\n",
|
140 |
+
" waveform = torchaudio.functional.resample(waveform, sample_rate, bundle.sample_rate)\n",
|
141 |
+
" return waveform, label\n",
|
142 |
+
"\n",
|
143 |
+
" def get_dataframe(self):\n",
|
144 |
+
" return self.dataframe\n",
|
145 |
+
"\n",
|
146 |
+
"\n",
|
147 |
+
"class SelfAttention(nn.Module):\n",
|
148 |
+
" def __init__(self, input_dim, num_heads):\n",
|
149 |
+
" super(SelfAttention, self).__init__()\n",
|
150 |
+
" self.multihead_attn = nn.MultiheadAttention(embed_dim=input_dim, num_heads=num_heads)\n",
|
151 |
+
" self.layer_norm = nn.LayerNorm(input_dim)\n",
|
152 |
+
"\n",
|
153 |
+
" def forward(self, x):\n",
|
154 |
+
" attn_output, _ = self.multihead_attn(x, x, x)\n",
|
155 |
+
" x = x + attn_output # Add & Normalize\n",
|
156 |
+
" x = self.layer_norm(x)\n",
|
157 |
+
" return x\n",
|
158 |
+
"\n",
|
159 |
+
"class TimeSeriesClassifier(nn.Module):\n",
|
160 |
+
" def __init__(self, input_dim, num_heads, hidden_dim, output_dim):\n",
|
161 |
+
" super(TimeSeriesClassifier, self).__init__()\n",
|
162 |
+
" self.self_attention = SelfAttention(input_dim, num_heads)\n",
|
163 |
+
" self.fc1 = nn.Linear(input_dim, hidden_dim)\n",
|
164 |
+
" self.fc2 = nn.Linear(hidden_dim, output_dim)\n",
|
165 |
+
"\n",
|
166 |
+
" def forward(self, x):\n",
|
167 |
+
" # x: [batch_size, seq_len, input_dim]\n",
|
168 |
+
" x = x.permute(1, 0, 2) # Change to [seq_len, batch_size, input_dim]\n",
|
169 |
+
" x = self.self_attention(x)\n",
|
170 |
+
" x = x.permute(1, 0, 2) # Change back to [batch_size, seq_len, input_dim]\n",
|
171 |
+
" x = torch.mean(x, dim=1) # Global Average Pooling over the time dimension\n",
|
172 |
+
" x = torch.relu(self.fc1(x))\n",
|
173 |
+
" x = torch.sigmoid(self.fc2(x)) # Sigmoid for binary classification\n",
|
174 |
+
" return x\n",
|
175 |
+
"\n",
|
176 |
+
"\n",
|
177 |
+
"# Define the combined Encoder-Decoder model\n",
|
178 |
+
"class EncoderDecoder(nn.Module):\n",
|
179 |
+
" def __init__(self, encoder, decoder):\n",
|
180 |
+
" super(EncoderDecoder, self).__init__()\n",
|
181 |
+
" self.encoder = encoder\n",
|
182 |
+
" self.decoder = decoder\n",
|
183 |
+
" def forward(self, x):\n",
|
184 |
+
" emission, _ = self.encoder(x)\n",
|
185 |
+
" x = self.decoder(emission)\n",
|
186 |
+
" return x\n",
|
187 |
+
"\n",
|
188 |
+
"\n",
|
189 |
+
"\n",
|
190 |
+
"\n",
|
191 |
+
"def collate_fn(batch):\n",
|
192 |
+
" waveforms = [item[0] for item in batch]\n",
|
193 |
+
" labels = [item[1] for item in batch]\n",
|
194 |
+
"\n",
|
195 |
+
" # Find the length of the longest waveform in the batch\n",
|
196 |
+
" max_length = max(waveform.size(1) for waveform in waveforms)\n",
|
197 |
+
"\n",
|
198 |
+
" # Pad all waveforms to the length of the longest waveform\n",
|
199 |
+
" padded_waveforms = [F.pad(waveform, (0, max_length - waveform.size(1))) for waveform in waveforms]\n",
|
200 |
+
" padded_waveforms = torch.stack(padded_waveforms)\n",
|
201 |
+
"\n",
|
202 |
+
" labels = torch.tensor(labels)\n",
|
203 |
+
" return padded_waveforms, labels\n",
|
204 |
+
"\n",
|
205 |
+
"\n",
|
206 |
+
"\n",
|
207 |
+
"\n"
|
208 |
+
],
|
209 |
+
"metadata": {
|
210 |
+
"_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
|
211 |
+
"_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
|
212 |
+
"execution": {
|
213 |
+
"iopub.status.busy": "2024-10-21T05:31:37.122433Z",
|
214 |
+
"iopub.execute_input": "2024-10-21T05:31:37.122772Z",
|
215 |
+
"iopub.status.idle": "2024-10-21T05:31:37.149038Z",
|
216 |
+
"shell.execute_reply.started": "2024-10-21T05:31:37.122737Z",
|
217 |
+
"shell.execute_reply": "2024-10-21T05:31:37.148219Z"
|
218 |
+
},
|
219 |
+
"trusted": true,
|
220 |
+
"id": "slo_6EJUmgbK"
|
221 |
+
},
|
222 |
+
"execution_count": null,
|
223 |
+
"outputs": []
|
224 |
+
},
|
225 |
+
{
|
226 |
+
"cell_type": "code",
|
227 |
+
"source": [
|
228 |
+
"# Initialize everything\n",
|
229 |
+
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
|
230 |
+
"bundle = torchaudio.pipelines.WAV2VEC2_ASR_BASE_960H\n",
|
231 |
+
"encoder = bundle.get_model().to(device)\n",
|
232 |
+
"\n",
|
233 |
+
"# Example usage\n",
|
234 |
+
"batch_size = 32\n",
|
235 |
+
"seq_len = 10 # Number of time steps\n",
|
236 |
+
"input_dim = 29\n",
|
237 |
+
"hidden_dim = 15\n",
|
238 |
+
"# Creating a dummy input tensor with shape [batch_size, seq_len, input_dim]\n",
|
239 |
+
"dummy_input = torch.randn(batch_size, seq_len, input_dim)\n",
|
240 |
+
"\n",
|
241 |
+
"decoder = TimeSeriesClassifier(input_dim = input_dim,\n",
|
242 |
+
" num_heads = input_dim,\n",
|
243 |
+
" hidden_dim = hidden_dim,\n",
|
244 |
+
" output_dim = 1)\n",
|
245 |
+
"output = decoder(dummy_input)\n",
|
246 |
+
"print(output)\n",
|
247 |
+
"decoder = decoder.to(device)\n",
|
248 |
+
"model = EncoderDecoder(encoder, decoder).to(device)\n",
|
249 |
+
"\n",
|
250 |
+
"# Freeze the encoder parameters\n",
|
251 |
+
"for param in model.encoder.parameters():\n",
|
252 |
+
" param.requires_grad = False\n",
|
253 |
+
"\n",
|
254 |
+
"# Dataset and DataLoaders\n",
|
255 |
+
"\n",
|
256 |
+
"dataset = HealthAudioDataset(dataset_dir, device)\n",
|
257 |
+
"train_size = int(0.8 * len(dataset))\n",
|
258 |
+
"test_size = len(dataset) - train_size\n",
|
259 |
+
"train_dataset, test_dataset = random_split(dataset, [train_size, test_size])\n",
|
260 |
+
"\n",
|
261 |
+
"train_loader = DataLoader(train_dataset, batch_size=2, shuffle=True, collate_fn=collate_fn)\n",
|
262 |
+
"test_loader = DataLoader(test_dataset, batch_size=2, shuffle=True, collate_fn=collate_fn)\n",
|
263 |
+
"\n",
|
264 |
+
"# Print the contents of the train_loader"
|
265 |
+
],
|
266 |
+
"metadata": {
|
267 |
+
"execution": {
|
268 |
+
"iopub.status.busy": "2024-10-21T05:31:37.150151Z",
|
269 |
+
"iopub.execute_input": "2024-10-21T05:31:37.15093Z",
|
270 |
+
"iopub.status.idle": "2024-10-21T05:31:38.413704Z",
|
271 |
+
"shell.execute_reply.started": "2024-10-21T05:31:37.150901Z",
|
272 |
+
"shell.execute_reply": "2024-10-21T05:31:38.412655Z"
|
273 |
+
},
|
274 |
+
"trusted": true,
|
275 |
+
"id": "__Tb1u1wmgbK",
|
276 |
+
"outputId": "d6b97111-e800-40db-8873-fff376726885"
|
277 |
+
},
|
278 |
+
"execution_count": null,
|
279 |
+
"outputs": [
|
280 |
+
{
|
281 |
+
"name": "stdout",
|
282 |
+
"text": "tensor([[0.5726],\n [0.5791],\n [0.5756],\n [0.5718],\n [0.6160],\n [0.5886],\n [0.5634],\n [0.5896],\n [0.5754],\n [0.6070],\n [0.5597],\n [0.5673],\n [0.5942],\n [0.5967],\n [0.5815],\n [0.5582],\n [0.5873],\n [0.6225],\n [0.5830],\n [0.5879],\n [0.5958],\n [0.5540],\n [0.5812],\n [0.6039],\n [0.5825],\n [0.5635],\n [0.6145],\n [0.5483],\n [0.5770],\n [0.5952],\n [0.6086],\n [0.5806]], grad_fn=<SigmoidBackward0>)\n",
|
283 |
+
"output_type": "stream"
|
284 |
+
}
|
285 |
+
]
|
286 |
+
},
|
287 |
+
{
|
288 |
+
"cell_type": "code",
|
289 |
+
"source": [
|
290 |
+
"# Loss and optimizer\n",
|
291 |
+
"criterion = nn.BCELoss()\n",
|
292 |
+
"optimizer = optim.Adam(model.parameters(), lr=1e-4)\n",
|
293 |
+
"\n",
|
294 |
+
"def verification(model):\n",
|
295 |
+
" # Testing script\n",
|
296 |
+
" test_loss = 0.0\n",
|
297 |
+
" with torch.no_grad():\n",
|
298 |
+
" for i, (inputs_batch, labels_batch) in enumerate(test_loader):\n",
|
299 |
+
" for batch_idx in range(inputs_batch.size(0)):\n",
|
300 |
+
" inputs, labels = inputs_batch[batch_idx], labels_batch[batch_idx]\n",
|
301 |
+
" inputs = inputs.to(device)\n",
|
302 |
+
" labels = labels.to(device).float().unsqueeze(0) # Convert to float and add batch dimension\n",
|
303 |
+
" outputs = model(inputs)\n",
|
304 |
+
" labels = labels.view(outputs.shape) # Ensure labels match the shape of outputs\n",
|
305 |
+
" loss = criterion(outputs, labels)\n",
|
306 |
+
" test_loss += loss.item()\n",
|
307 |
+
" ret = test_loss / len(test_loader)\n",
|
308 |
+
" print(f'Test Loss: {ret:.4f}')\n",
|
309 |
+
" return ret\n",
|
310 |
+
"verification(model)\n",
|
311 |
+
"# save the model\n"
|
312 |
+
],
|
313 |
+
"metadata": {
|
314 |
+
"execution": {
|
315 |
+
"iopub.status.busy": "2024-10-21T05:31:38.416523Z",
|
316 |
+
"iopub.execute_input": "2024-10-21T05:31:38.417174Z",
|
317 |
+
"iopub.status.idle": "2024-10-21T05:31:39.180667Z",
|
318 |
+
"shell.execute_reply.started": "2024-10-21T05:31:38.417142Z",
|
319 |
+
"shell.execute_reply": "2024-10-21T05:31:39.179716Z"
|
320 |
+
},
|
321 |
+
"trusted": true,
|
322 |
+
"id": "fXi2w6_dmgbL",
|
323 |
+
"outputId": "de44529f-7406-42b4-ec62-c01f85b4c675"
|
324 |
+
},
|
325 |
+
"execution_count": null,
|
326 |
+
"outputs": [
|
327 |
+
{
|
328 |
+
"name": "stdout",
|
329 |
+
"text": "Test Loss: 1.6243\n",
|
330 |
+
"output_type": "stream"
|
331 |
+
},
|
332 |
+
{
|
333 |
+
"execution_count": 99,
|
334 |
+
"output_type": "execute_result",
|
335 |
+
"data": {
|
336 |
+
"text/plain": "1.6242794593175252"
|
337 |
+
},
|
338 |
+
"metadata": {}
|
339 |
+
}
|
340 |
+
]
|
341 |
+
},
|
342 |
+
{
|
343 |
+
"cell_type": "code",
|
344 |
+
"source": [
|
345 |
+
"print(f\"Training started... Using {device}\")\n",
|
346 |
+
"num_epochs = 0\n",
|
347 |
+
"while verification(model) > 0.10:\n",
|
348 |
+
" model.train()\n",
|
349 |
+
" running_loss = 0.0\n",
|
350 |
+
" for i, (inputs_batch, labels_batch) in enumerate(train_loader):\n",
|
351 |
+
"# print(f'Batch {i + 1}:')\n",
|
352 |
+
" for batch_idx in range(inputs_batch.size(0)):\n",
|
353 |
+
" inputs, labels = inputs_batch[batch_idx], labels_batch[batch_idx]\n",
|
354 |
+
" inputs = inputs.to(device)\n",
|
355 |
+
" labels = labels.to(device).float().unsqueeze(0) # Convert to float and add batch dimension\n",
|
356 |
+
"\n",
|
357 |
+
" # print(f' Input {batch_idx + 1}: {inputs.size()}, Label: {labels.size()}')\n",
|
358 |
+
" # print(f' Input data type: {inputs.dtype}, Label data type: {labels.dtype}')\n",
|
359 |
+
"\n",
|
360 |
+
" optimizer.zero_grad()\n",
|
361 |
+
" outputs = model(inputs)\n",
|
362 |
+
" # print(f' Output: {outputs.size()}')\n",
|
363 |
+
" labels = labels.view(outputs.shape) # Ensure labels match the shape of outputs\n",
|
364 |
+
" loss = criterion(outputs, labels)\n",
|
365 |
+
" loss.backward()\n",
|
366 |
+
" optimizer.step()\n",
|
367 |
+
" running_loss += loss.item()\n",
|
368 |
+
"\n",
|
369 |
+
"\n",
|
370 |
+
" print(f'Epoch [{epoch + 1}/{num_epochs}], Batch [{i + 1}], Loss: {running_loss / 100:.4f}')\n",
|
371 |
+
" epoch = epoch + 1\n",
|
372 |
+
"\n",
|
373 |
+
"print(\"Training completed.\")\n",
|
374 |
+
"# now generate the testing script\n",
|
375 |
+
"\n"
|
376 |
+
],
|
377 |
+
"metadata": {
|
378 |
+
"execution": {
|
379 |
+
"iopub.status.busy": "2024-10-21T05:39:17.441427Z",
|
380 |
+
"iopub.execute_input": "2024-10-21T05:39:17.441812Z",
|
381 |
+
"iopub.status.idle": "2024-10-21T05:39:59.551312Z",
|
382 |
+
"shell.execute_reply.started": "2024-10-21T05:39:17.44178Z",
|
383 |
+
"shell.execute_reply": "2024-10-21T05:39:59.550324Z"
|
384 |
+
},
|
385 |
+
"trusted": true,
|
386 |
+
"id": "BRtV6wDfmgbL",
|
387 |
+
"outputId": "93af8a83-48c1-41b2-e1f3-b27afa75c285"
|
388 |
+
},
|
389 |
+
"execution_count": null,
|
390 |
+
"outputs": [
|
391 |
+
{
|
392 |
+
"name": "stdout",
|
393 |
+
"text": "Training started... Using cuda\nTest Loss: 0.1040\nEpoch [283/0], Batch [12], Loss: 0.0445\nTest Loss: 0.1708\nEpoch [284/0], Batch [12], Loss: 0.0302\nTest Loss: 0.1152\nEpoch [285/0], Batch [12], Loss: 0.0298\nTest Loss: 0.1269\nEpoch [286/0], Batch [12], Loss: 0.0319\nTest Loss: 0.1675\nEpoch [287/0], Batch [12], Loss: 0.0276\nTest Loss: 0.1064\nEpoch [288/0], Batch [12], Loss: 0.0250\nTest Loss: 0.1328\nEpoch [289/0], Batch [12], Loss: 0.0220\nTest Loss: 0.1303\nEpoch [290/0], Batch [12], Loss: 0.0219\nTest Loss: 0.1164\nEpoch [291/0], Batch [12], Loss: 0.0200\nTest Loss: 0.1139\nEpoch [292/0], Batch [12], Loss: 0.0342\nTest Loss: 0.1391\nEpoch [293/0], Batch [12], Loss: 0.0213\nTest Loss: 0.1166\nEpoch [294/0], Batch [12], Loss: 0.0271\nTest Loss: 0.0904\nTraining completed.\n",
|
394 |
+
"output_type": "stream"
|
395 |
+
}
|
396 |
+
]
|
397 |
+
},
|
398 |
+
{
|
399 |
+
"cell_type": "code",
|
400 |
+
"source": [],
|
401 |
+
"metadata": {
|
402 |
+
"trusted": true,
|
403 |
+
"id": "FU8OK0LumgbL"
|
404 |
+
},
|
405 |
+
"execution_count": null,
|
406 |
+
"outputs": []
|
407 |
+
},
|
408 |
+
{
|
409 |
+
"cell_type": "code",
|
410 |
+
"source": [
|
411 |
+
"model"
|
412 |
+
],
|
413 |
+
"metadata": {
|
414 |
+
"execution": {
|
415 |
+
"iopub.status.busy": "2024-10-21T05:39:12.138603Z",
|
416 |
+
"iopub.status.idle": "2024-10-21T05:39:12.138955Z",
|
417 |
+
"shell.execute_reply.started": "2024-10-21T05:39:12.138776Z",
|
418 |
+
"shell.execute_reply": "2024-10-21T05:39:12.138793Z"
|
419 |
+
},
|
420 |
+
"trusted": true,
|
421 |
+
"id": "jIbFxLW-mgbM"
|
422 |
+
},
|
423 |
+
"execution_count": null,
|
424 |
+
"outputs": []
|
425 |
+
},
|
426 |
+
{
|
427 |
+
"cell_type": "code",
|
428 |
+
"source": [
|
429 |
+
"torch.save(model.state_dict(), 'hack49_encoder_decoder_model.pth')\n",
|
430 |
+
"import boto3\n",
|
431 |
+
"\n",
|
432 |
+
"def upload_to_s3(file_path, bucket_name, object_name, access_key, secret_key):\n",
|
433 |
+
" # Initialize a session using your AWS credentials\n",
|
434 |
+
" s3_client = boto3.client('s3',\n",
|
435 |
+
" region_name='us-east-2',\n",
|
436 |
+
" aws_access_key_id=access_key,\n",
|
437 |
+
" aws_secret_access_key=secret_key)\n",
|
438 |
+
"\n",
|
439 |
+
" try:\n",
|
440 |
+
" # Uploads the given file using a managed uploader\n",
|
441 |
+
" s3_client.upload_file(file_path, bucket_name, object_name)\n",
|
442 |
+
" print(f'Successfully uploaded {file_path} to {bucket_name}/{object_name}')\n",
|
443 |
+
" except Exception as e:\n",
|
444 |
+
" print(f'Error uploading file: {e}')\n",
|
445 |
+
"\n",
|
446 |
+
"# Example usage\n",
|
447 |
+
"file_path = 'hack49_encoder_decoder_model.pth'\n",
|
448 |
+
"bucket_name = 'my-ai-models-darcy'\n",
|
449 |
+
"# get time\n",
|
450 |
+
"import datetime\n",
|
451 |
+
"now = datetime.datetime.now()\n",
|
452 |
+
"object_name = f'hack49_encoder_decoder_model_{now.strftime(\"%Y-%m-%d_%H-%M-%S\")}.pth'\n",
|
453 |
+
"access_key = 'XXXX'\n",
|
454 |
+
"secret_key = 'XXXX'\n",
|
455 |
+
"\n",
|
456 |
+
"upload_to_s3(file_path, bucket_name, object_name, access_key, secret_key)\n"
|
457 |
+
],
|
458 |
+
"metadata": {
|
459 |
+
"execution": {
|
460 |
+
"iopub.status.busy": "2024-10-21T05:40:04.454771Z",
|
461 |
+
"iopub.execute_input": "2024-10-21T05:40:04.455706Z",
|
462 |
+
"iopub.status.idle": "2024-10-21T05:40:09.278691Z",
|
463 |
+
"shell.execute_reply.started": "2024-10-21T05:40:04.455668Z",
|
464 |
+
"shell.execute_reply": "2024-10-21T05:40:09.277704Z"
|
465 |
+
},
|
466 |
+
"trusted": true,
|
467 |
+
"id": "zDZBAM2omgbM",
|
468 |
+
"outputId": "ec41c422-578a-4a27-bfbb-c5a3d569744a"
|
469 |
+
},
|
470 |
+
"execution_count": null,
|
471 |
+
"outputs": [
|
472 |
+
{
|
473 |
+
"name": "stdout",
|
474 |
+
"text": "Successfully uploaded hack49_encoder_decoder_model.pth to my-ai-models-darcy/hack49_encoder_decoder_model_2024-10-21_05-40-05.pth\n",
|
475 |
+
"output_type": "stream"
|
476 |
+
}
|
477 |
+
]
|
478 |
+
}
|
479 |
+
]
|
480 |
+
}
|
README.md
ADDED
File without changes
|
hack49_encoder_decoder_model.pth
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a5846121d9ffd5981799137784baaa21ac6913e7de5cab7aa38162de2504c5e6
|
3 |
+
size 377681541
|