{ "cells": [ { "cell_type": "code", "execution_count": 7, "id": "2b925313", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using device: cuda\n", "Loading dataset...\n", "Number of labels: 60\n", "Label names: ['airplane', 'airport', 'bare soil', 'baseball diamond', 'basketball court', 'beach', 'bridge', 'buildings', 'cars', 'chaparral']...\n", "Total samples: 109161\n", "Train: 87328, Val: 10916, Test: 10917\n", "\n", "Starting training...\n", "\n", "Epoch 1/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:40<00:00, 6.82it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 15.95it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0363, Train Acc: 0.9280\n", "Val Loss: 0.0217, Val Acc: 0.9609\n", "Best model saved with validation accuracy: 0.9609\n", "\n", "Epoch 2/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:31<00:00, 6.97it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.42it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0240, Train Acc: 0.9566\n", "Val Loss: 0.0193, Val Acc: 0.9644\n", "Best model saved with validation accuracy: 0.9644\n", "\n", "Epoch 3/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:26<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.69it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0211, Train Acc: 0.9614\n", "Val Loss: 0.0179, Val Acc: 0.9663\n", "Best model saved with validation accuracy: 0.9663\n", "\n", "Epoch 4/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:26<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.75it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0188, Train Acc: 0.9653\n", "Val Loss: 0.0164, Val Acc: 0.9702\n", "Best model saved with validation accuracy: 0.9702\n", "\n", "Epoch 5/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:26<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.68it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0180, Train Acc: 0.9666\n", "Val Loss: 0.0163, Val Acc: 0.9701\n", "\n", "Epoch 6/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:24<00:00, 7.09it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.93it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0171, Train Acc: 0.9682\n", "Val Loss: 0.0158, Val Acc: 0.9696\n", "\n", "Epoch 7/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:24<00:00, 7.10it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.60it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0160, Train Acc: 0.9700\n", "Val Loss: 0.0157, Val Acc: 0.9727\n", "Best model saved with validation accuracy: 0.9727\n", "\n", "Epoch 8/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.08it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.78it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0154, Train Acc: 0.9708\n", "Val Loss: 0.0159, Val Acc: 0.9700\n", "\n", "Epoch 9/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:33<00:00, 6.94it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 16.25it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0146, Train Acc: 0.9722\n", "Val Loss: 0.0151, Val Acc: 0.9718\n", "\n", "Epoch 10/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:39<00:00, 6.83it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 15.87it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0143, Train Acc: 0.9726\n", "Val Loss: 0.0151, Val Acc: 0.9728\n", "Best model saved with validation accuracy: 0.9728\n", "\n", "Epoch 11/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:41<00:00, 6.79it/s]\n", "Validation: 100%|██████████| 342/342 [00:22<00:00, 15.40it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0139, Train Acc: 0.9736\n", "Val Loss: 0.0149, Val Acc: 0.9740\n", "Best model saved with validation accuracy: 0.9740\n", "\n", "Epoch 12/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:32<00:00, 6.96it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.79it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0135, Train Acc: 0.9742\n", "Val Loss: 0.0146, Val Acc: 0.9746\n", "Best model saved with validation accuracy: 0.9746\n", "\n", "Epoch 13/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.09it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 17.08it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0130, Train Acc: 0.9751\n", "Val Loss: 0.0151, Val Acc: 0.9739\n", "\n", "Epoch 14/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:21<00:00, 7.16it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.99it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0127, Train Acc: 0.9756\n", "Val Loss: 0.0158, Val Acc: 0.9751\n", "Best model saved with validation accuracy: 0.9751\n", "\n", "Epoch 15/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:26<00:00, 7.06it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.70it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0123, Train Acc: 0.9761\n", "Val Loss: 0.0150, Val Acc: 0.9738\n", "\n", "Epoch 16/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:26<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.85it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0122, Train Acc: 0.9764\n", "Val Loss: 0.0151, Val Acc: 0.9749\n", "\n", "Epoch 17/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.92it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0119, Train Acc: 0.9768\n", "Val Loss: 0.0160, Val Acc: 0.9728\n", "\n", "Epoch 18/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:23<00:00, 7.12it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.90it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0117, Train Acc: 0.9770\n", "Val Loss: 0.0157, Val Acc: 0.9728\n", "\n", "Epoch 19/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:23<00:00, 7.12it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.66it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0115, Train Acc: 0.9775\n", "Val Loss: 0.0158, Val Acc: 0.9768\n", "Best model saved with validation accuracy: 0.9768\n", "\n", "Epoch 20/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:23<00:00, 7.12it/s]\n", "Validation: 100%|██████████| 342/342 [00:19<00:00, 17.30it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0111, Train Acc: 0.9784\n", "Val Loss: 0.0147, Val Acc: 0.9767\n", "\n", "Epoch 21/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:22<00:00, 7.14it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.95it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0109, Train Acc: 0.9787\n", "Val Loss: 0.0155, Val Acc: 0.9766\n", "\n", "Epoch 22/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:23<00:00, 7.12it/s]\n", "Validation: 100%|██████████| 342/342 [00:19<00:00, 17.34it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0109, Train Acc: 0.9789\n", "Val Loss: 0.0147, Val Acc: 0.9767\n", "\n", "Epoch 23/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:21<00:00, 7.16it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.98it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0104, Train Acc: 0.9795\n", "Val Loss: 0.0152, Val Acc: 0.9774\n", "Best model saved with validation accuracy: 0.9774\n", "\n", "Epoch 24/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:20<00:00, 7.17it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.97it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0103, Train Acc: 0.9798\n", "Val Loss: 0.0175, Val Acc: 0.9745\n", "\n", "Epoch 25/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:21<00:00, 7.15it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 17.05it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0105, Train Acc: 0.9795\n", "Val Loss: 0.0160, Val Acc: 0.9770\n", "\n", "Epoch 26/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:26<00:00, 7.06it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.86it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0097, Train Acc: 0.9808\n", "Val Loss: 0.0159, Val Acc: 0.9776\n", "Best model saved with validation accuracy: 0.9776\n", "\n", "Epoch 27/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.69it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0101, Train Acc: 0.9802\n", "Val Loss: 0.0156, Val Acc: 0.9774\n", "\n", "Epoch 28/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:26<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.89it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0097, Train Acc: 0.9809\n", "Val Loss: 0.0163, Val Acc: 0.9763\n", "\n", "Epoch 29/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:23<00:00, 7.12it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.72it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0096, Train Acc: 0.9811\n", "Val Loss: 0.0154, Val Acc: 0.9766\n", "\n", "Epoch 30/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.79it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0094, Train Acc: 0.9814\n", "Val Loss: 0.0162, Val Acc: 0.9781\n", "Best model saved with validation accuracy: 0.9781\n", "\n", "Epoch 31/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.08it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.71it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0094, Train Acc: 0.9814\n", "Val Loss: 0.0156, Val Acc: 0.9781\n", "\n", "Epoch 32/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.86it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0091, Train Acc: 0.9820\n", "Val Loss: 0.0176, Val Acc: 0.9743\n", "\n", "Epoch 33/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:26<00:00, 7.06it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.82it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0086, Train Acc: 0.9828\n", "Val Loss: 0.0170, Val Acc: 0.9789\n", "Best model saved with validation accuracy: 0.9789\n", "\n", "Epoch 34/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:26<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.85it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0085, Train Acc: 0.9832\n", "Val Loss: 0.0160, Val Acc: 0.9785\n", "\n", "Epoch 35/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.74it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0088, Train Acc: 0.9825\n", "Val Loss: 0.0175, Val Acc: 0.9790\n", "Best model saved with validation accuracy: 0.9790\n", "\n", "Epoch 36/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.70it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0084, Train Acc: 0.9834\n", "Val Loss: 0.0162, Val Acc: 0.9798\n", "Best model saved with validation accuracy: 0.9798\n", "\n", "Epoch 37/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.76it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0086, Train Acc: 0.9829\n", "Val Loss: 0.0154, Val Acc: 0.9774\n", "\n", "Epoch 38/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:26<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.79it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0081, Train Acc: 0.9838\n", "Val Loss: 0.0165, Val Acc: 0.9792\n", "\n", "Epoch 39/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 17.04it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0078, Train Acc: 0.9844\n", "Val Loss: 0.0176, Val Acc: 0.9797\n", "\n", "Epoch 40/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.08it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.76it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0078, Train Acc: 0.9843\n", "Val Loss: 0.0167, Val Acc: 0.9795\n", "\n", "Epoch 41/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.74it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0078, Train Acc: 0.9845\n", "Val Loss: 0.0172, Val Acc: 0.9806\n", "Best model saved with validation accuracy: 0.9806\n", "\n", "Epoch 42/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.08it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 17.09it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0075, Train Acc: 0.9849\n", "Val Loss: 0.0168, Val Acc: 0.9801\n", "\n", "Epoch 43/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:23<00:00, 7.12it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.88it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0075, Train Acc: 0.9852\n", "Val Loss: 0.0171, Val Acc: 0.9803\n", "\n", "Epoch 44/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.93it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0072, Train Acc: 0.9858\n", "Val Loss: 0.0168, Val Acc: 0.9816\n", "Best model saved with validation accuracy: 0.9816\n", "\n", "Epoch 45/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.74it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0068, Train Acc: 0.9864\n", "Val Loss: 0.0169, Val Acc: 0.9801\n", "\n", "Epoch 46/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.71it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0070, Train Acc: 0.9862\n", "Val Loss: 0.0180, Val Acc: 0.9807\n", "\n", "Epoch 47/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.08it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 17.03it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0071, Train Acc: 0.9861\n", "Val Loss: 0.0184, Val Acc: 0.9792\n", "\n", "Epoch 48/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:26<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.75it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0071, Train Acc: 0.9861\n", "Val Loss: 0.0171, Val Acc: 0.9809\n", "\n", "Epoch 49/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.08it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.96it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0065, Train Acc: 0.9871\n", "Val Loss: 0.0182, Val Acc: 0.9809\n", "\n", "Epoch 50/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:23<00:00, 7.12it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.70it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0065, Train Acc: 0.9873\n", "Val Loss: 0.0180, Val Acc: 0.9812\n", "\n", "Epoch 51/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:24<00:00, 7.10it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 17.04it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0053, Train Acc: 0.9894\n", "Val Loss: 0.0182, Val Acc: 0.9825\n", "Best model saved with validation accuracy: 0.9825\n", "\n", "Epoch 52/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:22<00:00, 7.13it/s]\n", "Validation: 100%|██████████| 342/342 [00:19<00:00, 17.50it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0047, Train Acc: 0.9906\n", "Val Loss: 0.0196, Val Acc: 0.9824\n", "\n", "Epoch 53/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:22<00:00, 7.13it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.68it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0045, Train Acc: 0.9911\n", "Val Loss: 0.0195, Val Acc: 0.9830\n", "Best model saved with validation accuracy: 0.9830\n", "\n", "Epoch 54/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:23<00:00, 7.11it/s]\n", "Validation: 100%|██████████| 342/342 [00:19<00:00, 17.29it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0043, Train Acc: 0.9915\n", "Val Loss: 0.0198, Val Acc: 0.9828\n", "\n", "Epoch 55/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:23<00:00, 7.12it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.92it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0041, Train Acc: 0.9920\n", "Val Loss: 0.0210, Val Acc: 0.9827\n", "\n", "Epoch 56/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:23<00:00, 7.11it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 17.08it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0040, Train Acc: 0.9923\n", "Val Loss: 0.0212, Val Acc: 0.9832\n", "Best model saved with validation accuracy: 0.9832\n", "\n", "Epoch 57/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:23<00:00, 7.11it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.74it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0038, Train Acc: 0.9928\n", "Val Loss: 0.0215, Val Acc: 0.9832\n", "\n", "Epoch 58/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:26<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.83it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0037, Train Acc: 0.9931\n", "Val Loss: 0.0220, Val Acc: 0.9832\n", "\n", "Epoch 59/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:24<00:00, 7.09it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.85it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0036, Train Acc: 0.9934\n", "Val Loss: 0.0220, Val Acc: 0.9834\n", "Best model saved with validation accuracy: 0.9834\n", "\n", "Epoch 60/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.08it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.79it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0036, Train Acc: 0.9935\n", "Val Loss: 0.0227, Val Acc: 0.9831\n", "\n", "Epoch 61/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:26<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.66it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0033, Train Acc: 0.9940\n", "Val Loss: 0.0239, Val Acc: 0.9830\n", "\n", "Epoch 62/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.90it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0032, Train Acc: 0.9942\n", "Val Loss: 0.0236, Val Acc: 0.9832\n", "\n", "Epoch 63/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:24<00:00, 7.10it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.76it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0031, Train Acc: 0.9945\n", "Val Loss: 0.0240, Val Acc: 0.9832\n", "\n", "Epoch 64/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.97it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0030, Train Acc: 0.9947\n", "Val Loss: 0.0244, Val Acc: 0.9834\n", "\n", "Epoch 65/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:24<00:00, 7.10it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 17.05it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0029, Train Acc: 0.9949\n", "Val Loss: 0.0245, Val Acc: 0.9829\n", "\n", "Epoch 66/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:20<00:00, 7.17it/s]\n", "Validation: 100%|██████████| 342/342 [00:19<00:00, 17.29it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0024, Train Acc: 0.9959\n", "Val Loss: 0.0263, Val Acc: 0.9837\n", "Best model saved with validation accuracy: 0.9837\n", "\n", "Epoch 67/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:20<00:00, 7.17it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 17.04it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0023, Train Acc: 0.9963\n", "Val Loss: 0.0268, Val Acc: 0.9838\n", "Best model saved with validation accuracy: 0.9838\n", "\n", "Epoch 68/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:21<00:00, 7.16it/s]\n", "Validation: 100%|██████████| 342/342 [00:19<00:00, 17.17it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0021, Train Acc: 0.9965\n", "Val Loss: 0.0273, Val Acc: 0.9839\n", "Best model saved with validation accuracy: 0.9839\n", "\n", "Epoch 69/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:26<00:00, 7.05it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.82it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0021, Train Acc: 0.9966\n", "Val Loss: 0.0275, Val Acc: 0.9837\n", "\n", "Epoch 70/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:34<00:00, 6.92it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 15.90it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0019, Train Acc: 0.9969\n", "Val Loss: 0.0286, Val Acc: 0.9837\n", "\n", "Epoch 71/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:35<00:00, 6.90it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 16.21it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0019, Train Acc: 0.9970\n", "Val Loss: 0.0283, Val Acc: 0.9839\n", "Best model saved with validation accuracy: 0.9839\n", "\n", "Epoch 72/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:35<00:00, 6.91it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.72it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0018, Train Acc: 0.9971\n", "Val Loss: 0.0289, Val Acc: 0.9838\n", "\n", "Epoch 73/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:33<00:00, 6.94it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 15.99it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0018, Train Acc: 0.9972\n", "Val Loss: 0.0291, Val Acc: 0.9836\n", "\n", "Epoch 74/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:32<00:00, 6.95it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.40it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0018, Train Acc: 0.9973\n", "Val Loss: 0.0288, Val Acc: 0.9837\n", "\n", "Epoch 75/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:26<00:00, 7.06it/s]\n", "Validation: 100%|██████████| 342/342 [00:19<00:00, 17.28it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0015, Train Acc: 0.9976\n", "Val Loss: 0.0292, Val Acc: 0.9844\n", "Best model saved with validation accuracy: 0.9844\n", "\n", "Epoch 76/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:31<00:00, 6.98it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 16.10it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0014, Train Acc: 0.9978\n", "Val Loss: 0.0305, Val Acc: 0.9842\n", "\n", "Epoch 77/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:31<00:00, 6.97it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.58it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0013, Train Acc: 0.9979\n", "Val Loss: 0.0307, Val Acc: 0.9842\n", "\n", "Epoch 78/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:32<00:00, 6.95it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.47it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0013, Train Acc: 0.9980\n", "Val Loss: 0.0308, Val Acc: 0.9840\n", "\n", "Epoch 79/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:29<00:00, 7.00it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 15.92it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0013, Train Acc: 0.9980\n", "Val Loss: 0.0315, Val Acc: 0.9842\n", "\n", "Epoch 80/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:34<00:00, 6.91it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 15.98it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0013, Train Acc: 0.9981\n", "Val Loss: 0.0316, Val Acc: 0.9841\n", "\n", "Epoch 81/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:33<00:00, 6.93it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.64it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0012, Train Acc: 0.9982\n", "Val Loss: 0.0318, Val Acc: 0.9843\n", "\n", "Epoch 82/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:31<00:00, 6.96it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.44it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0011, Train Acc: 0.9983\n", "Val Loss: 0.0325, Val Acc: 0.9843\n", "\n", "Epoch 83/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:36<00:00, 6.88it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 16.12it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0011, Train Acc: 0.9984\n", "Val Loss: 0.0327, Val Acc: 0.9844\n", "\n", "Epoch 84/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:29<00:00, 7.01it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 16.24it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0011, Train Acc: 0.9985\n", "Val Loss: 0.0333, Val Acc: 0.9843\n", "\n", "Epoch 85/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:32<00:00, 6.96it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.43it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0010, Train Acc: 0.9985\n", "Val Loss: 0.0332, Val Acc: 0.9844\n", "Best model saved with validation accuracy: 0.9844\n", "\n", "Epoch 86/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:27<00:00, 7.04it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.36it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0010, Train Acc: 0.9985\n", "Val Loss: 0.0331, Val Acc: 0.9846\n", "Best model saved with validation accuracy: 0.9846\n", "\n", "Epoch 87/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:31<00:00, 6.97it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.31it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0010, Train Acc: 0.9986\n", "Val Loss: 0.0332, Val Acc: 0.9845\n", "\n", "Epoch 88/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:31<00:00, 6.97it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 16.15it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0010, Train Acc: 0.9986\n", "Val Loss: 0.0337, Val Acc: 0.9844\n", "\n", "Epoch 89/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:34<00:00, 6.91it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 16.02it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0010, Train Acc: 0.9986\n", "Val Loss: 0.0342, Val Acc: 0.9846\n", "Best model saved with validation accuracy: 0.9846\n", "\n", "Epoch 90/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:35<00:00, 6.91it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 16.03it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0010, Train Acc: 0.9986\n", "Val Loss: 0.0341, Val Acc: 0.9844\n", "\n", "Epoch 91/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:33<00:00, 6.94it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 16.08it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0010, Train Acc: 0.9986\n", "Val Loss: 0.0347, Val Acc: 0.9844\n", "\n", "Epoch 92/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:34<00:00, 6.91it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 16.10it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0009, Train Acc: 0.9987\n", "Val Loss: 0.0345, Val Acc: 0.9846\n", "\n", "Epoch 93/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:27<00:00, 7.04it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.87it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0009, Train Acc: 0.9988\n", "Val Loss: 0.0346, Val Acc: 0.9845\n", "\n", "Epoch 94/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:21<00:00, 7.16it/s]\n", "Validation: 100%|██████████| 342/342 [00:19<00:00, 17.26it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0009, Train Acc: 0.9988\n", "Val Loss: 0.0352, Val Acc: 0.9845\n", "\n", "Epoch 95/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:23<00:00, 7.11it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.66it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0009, Train Acc: 0.9988\n", "Val Loss: 0.0356, Val Acc: 0.9844\n", "\n", "Epoch 96/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:22<00:00, 7.13it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.88it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0009, Train Acc: 0.9988\n", "Val Loss: 0.0359, Val Acc: 0.9844\n", "\n", "Epoch 97/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:22<00:00, 7.13it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.97it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0008, Train Acc: 0.9988\n", "Val Loss: 0.0357, Val Acc: 0.9844\n", "\n", "Epoch 98/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:25<00:00, 7.07it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.38it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0008, Train Acc: 0.9988\n", "Val Loss: 0.0359, Val Acc: 0.9845\n", "\n", "Epoch 99/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:35<00:00, 6.90it/s]\n", "Validation: 100%|██████████| 342/342 [00:21<00:00, 16.09it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0008, Train Acc: 0.9989\n", "Val Loss: 0.0355, Val Acc: 0.9845\n", "\n", "Epoch 100/100\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Training: 100%|██████████| 2729/2729 [06:36<00:00, 6.89it/s]\n", "Validation: 100%|██████████| 342/342 [00:20<00:00, 16.43it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Train Loss: 0.0008, Train Acc: 0.9989\n", "Val Loss: 0.0355, Val Acc: 0.9845\n", "\n", "Loading best model for testing...\n", "\n", "Evaluating on test set...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Validation: 100%|██████████| 342/342 [00:21<00:00, 15.69it/s]\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Test Loss: 0.0375, Test Acc: 0.9843\n", "\n", "Calculating metrics...\n", "Calculating ECE and Brier Score...\n", "Generating confusion matrices...\n", "Generating ROC curves...\n", "Generating PR curves...\n", "Generating calibration curve...\n", "\n", "Number of trainable parameters: 26,460,468\n", "Generating misclassification analysis...\n", "Analyzing confused label pairs...\n", "\n", "================================================================================\n", "Training and evaluation complete!\n", "Best validation accuracy: 0.9846\n", "Test accuracy: 0.9843\n", "All results and figures saved.\n", "================================================================================\n" ] } ], "source": [ "import torch\n", "import torch.nn as nn\n", "import torch.nn.functional as F\n", "from torch.utils.data import Dataset, DataLoader\n", "import torchvision.transforms as transforms\n", "from torchvision.models import resnet50\n", "import pandas as pd\n", "import numpy as np\n", "from PIL import Image\n", "import os\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import (accuracy_score, precision_recall_fscore_support, \n", " confusion_matrix, roc_auc_score, average_precision_score,\n", " cohen_kappa_score, matthews_corrcoef, roc_curve, \n", " precision_recall_curve, label_ranking_average_precision_score)\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "from tqdm import tqdm\n", "import warnings\n", "warnings.filterwarnings('ignore')\n", "\n", "# Set random seeds for reproducibility\n", "torch.manual_seed(42)\n", "np.random.seed(42)\n", "\n", "# ==================== Dataset Class ====================\n", "class MultiLabelDataset(Dataset):\n", " def __init__(self, image_paths, labels, transform=None):\n", " self.image_paths = image_paths\n", " self.labels = labels\n", " self.transform = transform\n", " \n", " def __len__(self):\n", " return len(self.image_paths)\n", " \n", " def __getitem__(self, idx):\n", " img_path = self.image_paths[idx]\n", " image = Image.open(img_path).convert('RGB')\n", " \n", " if self.transform:\n", " image = self.transform(image)\n", " \n", " label = torch.FloatTensor(self.labels[idx])\n", " return image, label\n", "\n", "# ==================== Data Loading ====================\n", "def load_dataset(root_dir, labels_dir):\n", " \"\"\"Load dataset from folder structure and CSV labels\"\"\"\n", " all_image_paths = []\n", " all_labels = []\n", " \n", " # Get label names from first CSV file\n", " csv_files = [f for f in os.listdir(labels_dir) if f.endswith('.csv')]\n", " first_csv = pd.read_csv(os.path.join(labels_dir, csv_files[0]))\n", " label_columns = first_csv.columns[1:].tolist() # Skip 'image' column\n", " num_classes = len(label_columns)\n", " \n", " print(f\"Number of labels: {num_classes}\")\n", " print(f\"Label names: {label_columns[:10]}...\") # Print first 10\n", " \n", " # Load all CSV files\n", " for csv_file in csv_files:\n", " csv_path = os.path.join(labels_dir, csv_file)\n", " df = pd.read_csv(csv_path)\n", " \n", " class_name = csv_file.replace('.csv', '')\n", " class_folder = os.path.join(root_dir, class_name)\n", " \n", " if not os.path.exists(class_folder):\n", " continue\n", " \n", " for _, row in df.iterrows():\n", " img_name = row['image']\n", " img_path = os.path.join(class_folder, img_name)\n", " \n", " if os.path.exists(img_path):\n", " all_image_paths.append(img_path)\n", " labels = row[1:].values.astype(np.float32)\n", " all_labels.append(labels)\n", " \n", " return all_image_paths, np.array(all_labels), label_columns\n", "\n", "# ==================== Novel Components ====================\n", "\n", "class ScalePredictor(nn.Module):\n", " \"\"\"Predicts importance weights for different scale levels\"\"\"\n", " def __init__(self, in_channels, num_scales=4):\n", " super().__init__()\n", " self.num_scales = num_scales\n", " self.global_pool = nn.AdaptiveAvgPool2d(1)\n", " self.fc = nn.Sequential(\n", " nn.Linear(in_channels, in_channels // 4),\n", " nn.ReLU(),\n", " nn.Linear(in_channels // 4, num_scales),\n", " nn.Softmax(dim=1)\n", " )\n", " \n", " def forward(self, x):\n", " b, c, h, w = x.shape\n", " pooled = self.global_pool(x).view(b, c)\n", " weights = self.fc(pooled)\n", " return weights\n", "\n", "class DynamicScaleAwareFPN(nn.Module):\n", " \"\"\"Dynamic Scale-Aware Feature Pyramid\"\"\"\n", " def __init__(self, in_channels_list, out_channels=256):\n", " super().__init__()\n", " self.num_scales = len(in_channels_list)\n", " \n", " # Lateral connections\n", " self.lateral_convs = nn.ModuleList([\n", " nn.Conv2d(in_ch, out_channels, 1) for in_ch in in_channels_list\n", " ])\n", " \n", " # Scale predictor\n", " self.scale_predictor = ScalePredictor(in_channels_list[-1], self.num_scales)\n", " \n", " def forward(self, features):\n", " # features: list of feature maps from different scales\n", " # Get scale weights\n", " scale_weights = self.scale_predictor(features[-1])\n", " \n", " # Apply lateral connections\n", " lateral_features = [conv(feat) for conv, feat in zip(self.lateral_convs, features)]\n", " \n", " # Weighted combination\n", " # Upsample all to the largest size\n", " target_size = lateral_features[0].shape[2:]\n", " upsampled = []\n", " for feat in lateral_features:\n", " if feat.shape[2:] != target_size:\n", " feat = F.interpolate(feat, size=target_size, mode='bilinear', align_corners=False)\n", " upsampled.append(feat)\n", " \n", " # Weight and combine\n", " weighted_features = []\n", " for i, feat in enumerate(upsampled):\n", " weighted_features.append(feat * scale_weights[:, i].view(-1, 1, 1, 1))\n", " \n", " output = sum(weighted_features)\n", " return output, scale_weights\n", "\n", "class GraphAttentionLayer(nn.Module):\n", " \"\"\"Graph Attention for label correlation\"\"\"\n", " def __init__(self, in_features, out_features, num_labels):\n", " super().__init__()\n", " self.in_features = in_features\n", " self.out_features = out_features\n", " self.num_labels = num_labels\n", " \n", " self.W = nn.Linear(in_features, out_features, bias=False)\n", " self.a = nn.Linear(2 * out_features, 1, bias=False)\n", " self.leakyrelu = nn.LeakyReLU(0.2)\n", " \n", " def forward(self, x, adj=None):\n", " # x: [batch, num_labels, in_features]\n", " batch_size = x.size(0)\n", "\n", " Wx = self.W(x) # [batch, num_labels, out_features]\n", "\n", " # Compute attention coefficients\n", " a_input = self._prepare_attentional_mechanism_input(Wx)\n", " # a_input: [batch, num_labels, num_labels, 2*out_features]\n", "\n", " e = self.leakyrelu(self.a(a_input)) # [batch, num_labels, num_labels, 1]\n", " e = e.squeeze(-1) # [batch, num_labels, num_labels]\n", "\n", " attention = F.softmax(e, dim=2) # [batch, num_labels, num_labels]\n", "\n", " # Apply attention\n", " h_prime = torch.bmm(attention, Wx) # [batch, num_labels, out_features]\n", "\n", " return h_prime, attention\n", " \n", " def _prepare_attentional_mechanism_input(self, Wx):\n", " num_labels = Wx.size(1)\n", "\n", " # Wx: [batch, num_labels, out_features]\n", " # We need to create pairs: [batch, num_labels, num_labels, 2*out_features]\n", "\n", " # Repeat for all pairs\n", " Wx_i = Wx.unsqueeze(2).repeat(1, 1, num_labels, 1) # [batch, num_labels, num_labels, out_features]\n", " Wx_j = Wx.unsqueeze(1).repeat(1, num_labels, 1, 1) # [batch, num_labels, num_labels, out_features]\n", "\n", " # Concatenate pairs\n", " all_combinations = torch.cat([Wx_i, Wx_j], dim=3) # [batch, num_labels, num_labels, 2*out_features]\n", "\n", " return all_combinations\n", "\n", "class CrossLabelSemanticGraph(nn.Module):\n", " \"\"\"Cross-Label Semantic Correlation Graph\"\"\"\n", " def __init__(self, feature_dim, num_labels, num_heads=4):\n", " super().__init__()\n", " self.num_labels = num_labels\n", " self.num_heads = num_heads\n", " \n", " self.gat_layers = nn.ModuleList([\n", " GraphAttentionLayer(feature_dim, feature_dim, num_labels)\n", " for _ in range(num_heads)\n", " ])\n", " \n", " self.output_projection = nn.Linear(feature_dim * num_heads, feature_dim)\n", " \n", " def forward(self, label_features):\n", " # label_features: [batch, num_labels, feature_dim]\n", " \n", " multi_head_outputs = []\n", " attentions = []\n", " \n", " for gat in self.gat_layers:\n", " out, att = gat(label_features)\n", " multi_head_outputs.append(out)\n", " attentions.append(att)\n", " \n", " # Concatenate multi-head outputs\n", " concatenated = torch.cat(multi_head_outputs, dim=2)\n", " output = self.output_projection(concatenated)\n", " \n", " return output, attentions\n", "\n", "class DeformableAttention(nn.Module):\n", " \"\"\"Memory-efficient Deformable Attention with spatial downsampling\"\"\"\n", " def __init__(self, dim, num_heads=8, max_tokens=196):\n", " super().__init__()\n", " self.num_heads = num_heads\n", " self.scale = (dim // num_heads) ** -0.5\n", " self.max_tokens = max_tokens # Reduce to 14x14 = 196 tokens max\n", "\n", " self.qkv = nn.Linear(dim, dim * 3)\n", " self.proj = nn.Linear(dim, dim)\n", "\n", " def forward(self, x):\n", " B, N, C = x.shape\n", "\n", " # Spatially downsample if needed to reduce memory\n", " if N > self.max_tokens:\n", " # Reshape to 2D, apply adaptive pooling, reshape back\n", " # Assuming square spatial dimensions\n", " h = w = int(N ** 0.5)\n", " target_h = target_w = int(self.max_tokens ** 0.5)\n", "\n", " x_2d = x.transpose(1, 2).reshape(B, C, h, w)\n", " x_2d = F.adaptive_avg_pool2d(x_2d, (target_h, target_w))\n", " x = x_2d.reshape(B, C, -1).transpose(1, 2)\n", " N = x.shape[1]\n", "\n", " qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)\n", " q, k, v = qkv[0], qkv[1], qkv[2]\n", "\n", " attn = (q @ k.transpose(-2, -1)) * self.scale\n", " attn = attn.softmax(dim=-1)\n", "\n", " x = (attn @ v).transpose(1, 2).reshape(B, N, C)\n", " x = self.proj(x)\n", " return x\n", "\n", "class SpectralSpatialFusion(nn.Module):\n", " \"\"\"Spectral-Spatial Decoupled Multi-Head Classification\"\"\"\n", " def __init__(self, feature_dim, num_labels):\n", " super().__init__()\n", " \n", " # Spectral branch\n", " self.spectral_branch = nn.Sequential(\n", " nn.Linear(feature_dim, feature_dim // 2),\n", " nn.ReLU(),\n", " nn.Dropout(0.3),\n", " nn.Linear(feature_dim // 2, num_labels)\n", " )\n", " \n", " # Spatial branch\n", " self.spatial_branch = nn.Sequential(\n", " nn.Linear(feature_dim, feature_dim // 2),\n", " nn.ReLU(),\n", " nn.Dropout(0.3),\n", " nn.Linear(feature_dim // 2, num_labels)\n", " )\n", " \n", " # Fusion gate\n", " self.fusion_gate = nn.Sequential(\n", " nn.Linear(feature_dim, num_labels),\n", " nn.Sigmoid()\n", " )\n", " \n", " def forward(self, features):\n", " spectral_out = self.spectral_branch(features)\n", " spatial_out = self.spatial_branch(features)\n", " \n", " gate = self.fusion_gate(features)\n", " \n", " fused_output = gate * spectral_out + (1 - gate) * spatial_out\n", " \n", " return fused_output, spectral_out, spatial_out, gate\n", "\n", "class UncertaintyHead(nn.Module):\n", " \"\"\"Evidential uncertainty estimation\"\"\"\n", " def __init__(self, feature_dim, num_labels):\n", " super().__init__()\n", " self.fc = nn.Linear(feature_dim, num_labels)\n", " \n", " def forward(self, x):\n", " evidence = F.softplus(self.fc(x))\n", " alpha = evidence + 1\n", " uncertainty = num_labels / torch.sum(alpha, dim=1, keepdim=True)\n", " return evidence, uncertainty\n", "\n", "# ==================== Main AMSI-Net Architecture ====================\n", "class AMSINet(nn.Module):\n", " def __init__(self, num_labels, pretrained=True):\n", " super().__init__()\n", " self.num_labels = num_labels\n", " \n", " # Backbone: ResNet50 as feature extractor\n", " backbone = resnet50(pretrained=pretrained)\n", " self.conv1 = backbone.conv1\n", " self.bn1 = backbone.bn1\n", " self.relu = backbone.relu\n", " self.maxpool = backbone.maxpool\n", " \n", " self.layer1 = backbone.layer1 # 256 channels\n", " self.layer2 = backbone.layer2 # 512 channels\n", " self.layer3 = backbone.layer3 # 1024 channels\n", " self.layer4 = backbone.layer4 # 2048 channels\n", " \n", " # Dynamic Scale-Aware FPN\n", " self.dsafp = DynamicScaleAwareFPN([256, 512, 1024, 2048], out_channels=256)\n", " \n", " # Deformable Attention\n", " self.deformable_attn = DeformableAttention(dim=256, num_heads=8)\n", " \n", " # Global pooling\n", " self.global_pool = nn.AdaptiveAvgPool2d(1)\n", " \n", " # Cross-Label Semantic Correlation Graph\n", " self.clscg = CrossLabelSemanticGraph(feature_dim=256, num_labels=num_labels, num_heads=4)\n", " \n", " # Create label embeddings\n", " self.label_embeddings = nn.Parameter(torch.randn(num_labels, 256))\n", " \n", " # Spectral-Spatial Fusion\n", " self.spectral_spatial_fusion = SpectralSpatialFusion(256, num_labels)\n", " \n", " # Uncertainty Head\n", " self.uncertainty_head = UncertaintyHead(256, num_labels)\n", " \n", " def forward(self, x):\n", " # Backbone feature extraction\n", " x = self.conv1(x)\n", " x = self.bn1(x)\n", " x = self.relu(x)\n", " x = self.maxpool(x)\n", " \n", " # Multi-scale features\n", " c2 = self.layer1(x)\n", " c3 = self.layer2(c2)\n", " c4 = self.layer3(c3)\n", " c5 = self.layer4(c4)\n", " \n", " # Dynamic FPN\n", " fpn_features, scale_weights = self.dsafp([c2, c3, c4, c5])\n", " \n", " # Flatten for attention\n", " b, c, h, w = fpn_features.shape\n", " fpn_flat = fpn_features.view(b, c, -1).permute(0, 2, 1) # [B, H*W, C]\n", " \n", " # Deformable attention\n", " attn_features = self.deformable_attn(fpn_flat)\n", " \n", " # Global pooling\n", " attn_features = attn_features.mean(dim=1) # [B, C]\n", " \n", " # Prepare label-specific features\n", " batch_size = x.size(0)\n", " label_features = self.label_embeddings.unsqueeze(0).expand(batch_size, -1, -1)\n", " \n", " # Add global context to each label\n", " global_context = attn_features.unsqueeze(1).expand(-1, self.num_labels, -1)\n", " label_features = label_features + global_context\n", " \n", " # Cross-Label Graph reasoning\n", " graph_features, graph_attentions = self.clscg(label_features)\n", " \n", " # Aggregate graph features\n", " aggregated_features = graph_features.mean(dim=1) # [B, C]\n", " \n", " # Spectral-Spatial Fusion\n", " output, spectral_out, spatial_out, fusion_gate = self.spectral_spatial_fusion(aggregated_features)\n", " \n", " # Uncertainty estimation\n", " evidence, uncertainty = self.uncertainty_head(aggregated_features)\n", " \n", " return {\n", " 'logits': output,\n", " 'spectral_logits': spectral_out,\n", " 'spatial_logits': spatial_out,\n", " 'fusion_gate': fusion_gate,\n", " 'uncertainty': uncertainty,\n", " 'scale_weights': scale_weights,\n", " 'graph_attentions': graph_attentions\n", " }\n", "\n", "# ==================== Loss Functions ====================\n", "class AsymmetricLoss(nn.Module):\n", " \"\"\"Asymmetric Loss for multi-label classification\"\"\"\n", " def __init__(self, gamma_neg=4, gamma_pos=1, clip=0.05):\n", " super().__init__()\n", " self.gamma_neg = gamma_neg\n", " self.gamma_pos = gamma_pos\n", " self.clip = clip\n", " \n", " def forward(self, x, y):\n", " # x: logits, y: targets\n", " xs_pos = torch.sigmoid(x)\n", " xs_neg = 1 - xs_pos\n", " \n", " # Asymmetric Clipping\n", " if self.clip is not None and self.clip > 0:\n", " xs_neg = (xs_neg + self.clip).clamp(max=1)\n", " \n", " # Basic CE calculation\n", " los_pos = y * torch.log(xs_pos.clamp(min=1e-8))\n", " los_neg = (1 - y) * torch.log(xs_neg.clamp(min=1e-8))\n", " \n", " # Asymmetric Focusing\n", " pt0 = xs_pos * y\n", " pt1 = xs_neg * (1 - y)\n", " pt = pt0 + pt1\n", " one_sided_gamma = self.gamma_pos * y + self.gamma_neg * (1 - y)\n", " one_sided_w = torch.pow(1 - pt, one_sided_gamma)\n", " \n", " loss = -one_sided_w * (los_pos + los_neg)\n", " \n", " return loss.mean()\n", "\n", "class CombinedLoss(nn.Module):\n", " \"\"\"Combined loss with multiple components\"\"\"\n", " def __init__(self, num_labels):\n", " super().__init__()\n", " self.asymmetric_loss = AsymmetricLoss()\n", " self.num_labels = num_labels\n", " \n", " def forward(self, outputs, targets):\n", " # Main classification loss\n", " cls_loss = self.asymmetric_loss(outputs['logits'], targets)\n", " \n", " # Spectral and spatial consistency\n", " spectral_loss = self.asymmetric_loss(outputs['spectral_logits'], targets)\n", " spatial_loss = self.asymmetric_loss(outputs['spatial_logits'], targets)\n", " \n", " # Total loss\n", " total_loss = cls_loss + 0.3 * (spectral_loss + spatial_loss)\n", " \n", " return total_loss\n", "\n", "# ==================== Training Functions ====================\n", "def train_epoch(model, dataloader, criterion, optimizer, device, accumulation_steps=2):\n", " model.train()\n", " total_loss = 0\n", " all_preds = []\n", " all_targets = []\n", "\n", " optimizer.zero_grad()\n", "\n", " for batch_idx, (images, targets) in enumerate(tqdm(dataloader, desc=\"Training\")):\n", " images, targets = images.to(device), targets.to(device)\n", "\n", " outputs = model(images)\n", " loss = criterion(outputs, targets)\n", "\n", " # Gradient accumulation\n", " loss = loss / accumulation_steps\n", " loss.backward()\n", "\n", " if (batch_idx + 1) % accumulation_steps == 0:\n", " optimizer.step()\n", " optimizer.zero_grad()\n", "\n", " total_loss += loss.item() * accumulation_steps\n", "\n", " preds = torch.sigmoid(outputs['logits'])\n", " all_preds.append(preds.detach().cpu().numpy())\n", " all_targets.append(targets.cpu().numpy())\n", "\n", " # Clear cache periodically\n", " if batch_idx % 100 == 0:\n", " torch.cuda.empty_cache()\n", "\n", " # Final optimizer step for remaining gradients\n", " optimizer.step()\n", " optimizer.zero_grad()\n", "\n", " all_preds = np.vstack(all_preds)\n", " all_targets = np.vstack(all_targets)\n", "\n", " # Calculate accuracy (using threshold 0.5)\n", " pred_labels = (all_preds > 0.5).astype(int)\n", " accuracy = (pred_labels == all_targets).mean()\n", "\n", " return total_loss / len(dataloader), accuracy\n", "\n", "def validate(model, dataloader, criterion, device):\n", " model.eval()\n", " total_loss = 0\n", " all_preds = []\n", " all_targets = []\n", "\n", " with torch.no_grad():\n", " for batch_idx, (images, targets) in enumerate(tqdm(dataloader, desc=\"Validation\")):\n", " images, targets = images.to(device), targets.to(device)\n", "\n", " outputs = model(images)\n", " loss = criterion(outputs, targets)\n", "\n", " total_loss += loss.item()\n", "\n", " preds = torch.sigmoid(outputs['logits'])\n", " all_preds.append(preds.cpu().numpy())\n", " all_targets.append(targets.cpu().numpy())\n", "\n", " # Clear cache periodically\n", " if batch_idx % 100 == 0:\n", " torch.cuda.empty_cache()\n", "\n", " all_preds = np.vstack(all_preds)\n", " all_targets = np.vstack(all_targets)\n", "\n", " pred_labels = (all_preds > 0.5).astype(int)\n", " accuracy = (pred_labels == all_targets).mean()\n", "\n", " return total_loss / len(dataloader), accuracy, all_preds, all_targets\n", "\n", "# ==================== Metrics Calculation ====================\n", "def calculate_metrics(y_true, y_pred, y_scores, label_names):\n", " \"\"\"Calculate comprehensive metrics\"\"\"\n", " metrics = {}\n", " num_labels = y_true.shape[1]\n", " \n", " # Per-class metrics\n", " precision, recall, f1, _ = precision_recall_fscore_support(y_true, y_pred, average=None, zero_division=0)\n", " \n", " for i, label in enumerate(label_names):\n", " tn = ((1 - y_true[:, i]) * (1 - y_pred[:, i])).sum()\n", " fp = ((1 - y_true[:, i]) * y_pred[:, i]).sum()\n", " fn = (y_true[:, i] * (1 - y_pred[:, i])).sum()\n", " tp = (y_true[:, i] * y_pred[:, i]).sum()\n", " \n", " specificity = tn / (tn + fp) if (tn + fp) > 0 else 0\n", " accuracy = (tp + tn) / (tp + tn + fp + fn) if (tp + tn + fp + fn) > 0 else 0\n", " \n", " metrics[f'{label}_accuracy'] = accuracy\n", " metrics[f'{label}_precision'] = precision[i]\n", " metrics[f'{label}_recall'] = recall[i]\n", " metrics[f'{label}_f1'] = f1[i]\n", " metrics[f'{label}_specificity'] = specificity\n", " \n", " # MCC\n", " mcc = matthews_corrcoef(y_true[:, i], y_pred[:, i])\n", " metrics[f'{label}_mcc'] = mcc\n", " \n", " # Overall metrics\n", " metrics['micro_precision'], metrics['micro_recall'], metrics['micro_f1'], _ = \\\n", " precision_recall_fscore_support(y_true, y_pred, average='micro', zero_division=0)\n", " \n", " metrics['macro_precision'], metrics['macro_recall'], metrics['macro_f1'], _ = \\\n", " precision_recall_fscore_support(y_true, y_pred, average='macro', zero_division=0)\n", " \n", " metrics['weighted_precision'], metrics['weighted_recall'], metrics['weighted_f1'], _ = \\\n", " precision_recall_fscore_support(y_true, y_pred, average='weighted', zero_division=0)\n", " \n", " # Hamming metrics\n", " metrics['hamming_accuracy'] = 1 - (np.sum(y_pred != y_true) / (y_true.shape[0] * y_true.shape[1]))\n", " \n", " # ROC-AUC\n", " try:\n", " metrics['micro_roc_auc'] = roc_auc_score(y_true, y_scores, average='micro')\n", " metrics['macro_roc_auc'] = roc_auc_score(y_true, y_scores, average='macro')\n", " except:\n", " metrics['micro_roc_auc'] = 0\n", " metrics['macro_roc_auc'] = 0\n", " \n", " # Average Precision\n", " try:\n", " metrics['micro_map'] = average_precision_score(y_true, y_scores, average='micro')\n", " metrics['macro_map'] = average_precision_score(y_true, y_scores, average='macro')\n", " except:\n", " metrics['micro_map'] = 0\n", " metrics['macro_map'] = 0\n", " \n", " # LRAP\n", " try:\n", " metrics['lrap'] = label_ranking_average_precision_score(y_true, y_scores)\n", " except:\n", " metrics['lrap'] = 0\n", " \n", " return metrics\n", "\n", "def calculate_ece(y_true, y_scores, n_bins=10):\n", " \"\"\"Calculate Expected Calibration Error\"\"\"\n", " bin_boundaries = np.linspace(0, 1, n_bins + 1)\n", " ece = 0\n", " \n", " for i in range(n_bins):\n", " bin_lower = bin_boundaries[i]\n", " bin_upper = bin_boundaries[i + 1]\n", " \n", " in_bin = (y_scores > bin_lower) & (y_scores <= bin_upper)\n", " prop_in_bin = in_bin.mean()\n", " \n", " if prop_in_bin > 0:\n", " accuracy_in_bin = (y_true[in_bin] == (y_scores[in_bin] > 0.5)).mean()\n", " avg_confidence_in_bin = y_scores[in_bin].mean()\n", " ece += np.abs(avg_confidence_in_bin - accuracy_in_bin) * prop_in_bin\n", " \n", " return ece\n", "\n", "def calculate_brier_score(y_true, y_scores):\n", " \"\"\"Calculate Brier Score\"\"\"\n", " return np.mean((y_scores - y_true) ** 2)\n", "\n", "# ==================== Main Execution ====================\n", "if __name__ == \"__main__\":\n", " # Configuration\n", " ROOT_DIR = \"/home/aiserver/Desktop/Remote-Sensing-Datasets/MLRSNet-master/image\"\n", " LABELS_DIR = \"/home/aiserver/Desktop/Remote-Sensing-Datasets/MLRSNet-master/labels\"\n", " BATCH_SIZE = 32 # Reduced from 32 to save memory\n", " NUM_EPOCHS = 100\n", " LEARNING_RATE = 1e-4\n", " DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n", "\n", " # Enable memory optimization\n", " torch.backends.cudnn.benchmark = True\n", " if torch.cuda.is_available():\n", " torch.cuda.empty_cache()\n", " \n", " print(f\"Using device: {DEVICE}\")\n", " \n", " # Data transforms\n", " train_transform = transforms.Compose([\n", " transforms.Resize((224, 224)),\n", " transforms.RandomHorizontalFlip(),\n", " transforms.RandomRotation(15),\n", " transforms.ColorJitter(brightness=0.2, contrast=0.2),\n", " transforms.ToTensor(),\n", " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n", " ])\n", " \n", " val_transform = transforms.Compose([\n", " transforms.Resize((224, 224)),\n", " transforms.ToTensor(),\n", " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])\n", " ])\n", " \n", " # Load dataset\n", " print(\"Loading dataset...\")\n", " image_paths, labels, label_names = load_dataset(ROOT_DIR, LABELS_DIR)\n", " print(f\"Total samples: {len(image_paths)}\")\n", " \n", " # Split dataset: 80% train, 10% val, 10% test\n", " train_paths, temp_paths, train_labels, temp_labels = train_test_split(\n", " image_paths, labels, test_size=0.2, random_state=42\n", " )\n", " val_paths, test_paths, val_labels, test_labels = train_test_split(\n", " temp_paths, temp_labels, test_size=0.5, random_state=42\n", " )\n", " \n", " print(f\"Train: {len(train_paths)}, Val: {len(val_paths)}, Test: {len(test_paths)}\")\n", " \n", " # Create datasets\n", " train_dataset = MultiLabelDataset(train_paths, train_labels, train_transform)\n", " val_dataset = MultiLabelDataset(val_paths, val_labels, val_transform)\n", " test_dataset = MultiLabelDataset(test_paths, test_labels, val_transform)\n", " \n", " # Create dataloaders\n", " train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=4)\n", " val_loader = DataLoader(val_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4)\n", " test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4)\n", " \n", " # Initialize model\n", " num_labels = labels.shape[1]\n", " model = AMSINet(num_labels=num_labels, pretrained=True).to(DEVICE)\n", " \n", " # Loss and optimizer\n", " criterion = CombinedLoss(num_labels)\n", " optimizer = torch.optim.AdamW(model.parameters(), lr=LEARNING_RATE, weight_decay=1e-4)\n", " scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', factor=0.5, patience=5)\n", "\n", " # Training\n", " print(\"\\nStarting training...\")\n", " best_val_acc = 0\n", " patience = 15\n", " patience_counter = 0\n", " train_losses, train_accs = [], []\n", " val_losses, val_accs = [], []\n", "\n", " for epoch in range(NUM_EPOCHS):\n", " print(f\"\\nEpoch {epoch+1}/{NUM_EPOCHS}\")\n", "\n", " train_loss, train_acc = train_epoch(model, train_loader, criterion, optimizer, DEVICE)\n", " val_loss, val_acc, val_preds, val_targets = validate(model, val_loader, criterion, DEVICE)\n", " \n", " train_losses.append(train_loss)\n", " train_accs.append(train_acc)\n", " val_losses.append(val_loss)\n", " val_accs.append(val_acc)\n", " \n", " print(f\"Train Loss: {train_loss:.4f}, Train Acc: {train_acc:.4f}\")\n", " print(f\"Val Loss: {val_loss:.4f}, Val Acc: {val_acc:.4f}\")\n", " \n", " scheduler.step(val_acc)\n", " \n", " # Save best model\n", " if val_acc > best_val_acc:\n", " best_val_acc = val_acc\n", " torch.save(model.state_dict(), 'best_model.pth')\n", " print(f\"Best model saved with validation accuracy: {best_val_acc:.4f}\")\n", " patience_counter = 0\n", " else:\n", " patience_counter += 1\n", " \n", " # Early stopping\n", " if patience_counter >= patience:\n", " print(f\"Early stopping at epoch {epoch+1}\")\n", " break\n", " \n", " # Plot training curves\n", " plt.figure(figsize=(12, 5), dpi=600)\n", " \n", " plt.subplot(1, 2, 1)\n", " plt.plot(train_losses, label='Train Loss', linewidth=2)\n", " plt.plot(val_losses, label='Val Loss', linewidth=2)\n", " plt.xlabel('Epoch', fontweight='bold', fontsize=12)\n", " plt.ylabel('Loss', fontweight='bold', fontsize=12)\n", " plt.title('Training and Validation Loss', fontweight='bold', fontsize=14)\n", " plt.legend(fontsize=10)\n", " plt.grid(True, alpha=0.3)\n", " \n", " plt.subplot(1, 2, 2)\n", " plt.plot(train_accs, label='Train Accuracy', linewidth=2)\n", " plt.plot(val_accs, label='Val Accuracy', linewidth=2)\n", " plt.xlabel('Epoch', fontweight='bold', fontsize=12)\n", " plt.ylabel('Accuracy', fontweight='bold', fontsize=12)\n", " plt.title('Training and Validation Accuracy', fontweight='bold', fontsize=14)\n", " plt.legend(fontsize=10)\n", " plt.grid(True, alpha=0.3)\n", " \n", " plt.tight_layout()\n", " plt.savefig('training_curves.png', dpi=600, bbox_inches='tight')\n", " plt.close()\n", " \n", " # Load best model for testing\n", " print(\"\\nLoading best model for testing...\")\n", " model.load_state_dict(torch.load('best_model.pth'))\n", " \n", " # Testing\n", " print(\"\\nEvaluating on test set...\")\n", " test_loss, test_acc, test_preds, test_targets = validate(model, test_loader, criterion, DEVICE)\n", " print(f\"Test Loss: {test_loss:.4f}, Test Acc: {test_acc:.4f}\")\n", " \n", " # Convert predictions to labels\n", " test_pred_labels = (test_preds > 0.5).astype(int)\n", " \n", " # Calculate comprehensive metrics\n", " print(\"\\nCalculating metrics...\")\n", " metrics = calculate_metrics(test_targets, test_pred_labels, test_preds, label_names)\n", " \n", " # Save detailed results\n", " with open('results.txt', 'w') as f:\n", " f.write(\"=\" * 80 + \"\\n\")\n", " f.write(\"AMSI-Net Test Results\\n\")\n", " f.write(\"=\" * 80 + \"\\n\\n\")\n", " \n", " f.write(\"Overall Metrics:\\n\")\n", " f.write(\"-\" * 80 + \"\\n\")\n", " f.write(f\"Test Accuracy: {test_acc:.4f}\\n\")\n", " f.write(f\"Micro Precision: {metrics['micro_precision']:.4f}\\n\")\n", " f.write(f\"Micro Recall: {metrics['micro_recall']:.4f}\\n\")\n", " f.write(f\"Micro F1: {metrics['micro_f1']:.4f}\\n\")\n", " f.write(f\"Macro Precision: {metrics['macro_precision']:.4f}\\n\")\n", " f.write(f\"Macro Recall: {metrics['macro_recall']:.4f}\\n\")\n", " f.write(f\"Macro F1: {metrics['macro_f1']:.4f}\\n\")\n", " f.write(f\"Weighted Precision: {metrics['weighted_precision']:.4f}\\n\")\n", " f.write(f\"Weighted Recall: {metrics['weighted_recall']:.4f}\\n\")\n", " f.write(f\"Weighted F1: {metrics['weighted_f1']:.4f}\\n\")\n", " f.write(f\"Hamming Accuracy: {metrics['hamming_accuracy']:.4f}\\n\")\n", " f.write(f\"Micro ROC-AUC: {metrics['micro_roc_auc']:.4f}\\n\")\n", " f.write(f\"Macro ROC-AUC: {metrics['macro_roc_auc']:.4f}\\n\")\n", " f.write(f\"Micro mAP: {metrics['micro_map']:.4f}\\n\")\n", " f.write(f\"Macro mAP: {metrics['macro_map']:.4f}\\n\")\n", " f.write(f\"LRAP: {metrics['lrap']:.4f}\\n\\n\")\n", " \n", " # Per-class metrics\n", " f.write(\"Per-Class Metrics:\\n\")\n", " f.write(\"-\" * 80 + \"\\n\")\n", " f.write(f\"{'Label':<25} {'Acc':>8} {'Prec':>8} {'Rec':>8} {'F1':>8} {'Spec':>8} {'MCC':>8}\\n\")\n", " f.write(\"-\" * 80 + \"\\n\")\n", " \n", " for label in label_names:\n", " f.write(f\"{label:<25} \"\n", " f\"{metrics[f'{label}_accuracy']:>8.4f} \"\n", " f\"{metrics[f'{label}_precision']:>8.4f} \"\n", " f\"{metrics[f'{label}_recall']:>8.4f} \"\n", " f\"{metrics[f'{label}_f1']:>8.4f} \"\n", " f\"{metrics[f'{label}_specificity']:>8.4f} \"\n", " f\"{metrics[f'{label}_mcc']:>8.4f}\\n\")\n", " \n", " # ECE and Brier Score per class\n", " print(\"Calculating ECE and Brier Score...\")\n", " ece_scores = []\n", " brier_scores = []\n", " \n", " for i in range(num_labels):\n", " ece = calculate_ece(test_targets[:, i], test_preds[:, i])\n", " brier = calculate_brier_score(test_targets[:, i], test_preds[:, i])\n", " ece_scores.append(ece)\n", " brier_scores.append(brier)\n", " \n", " with open('results.txt', 'a') as f:\n", " f.write(\"\\n\\nCalibration Metrics:\\n\")\n", " f.write(\"-\" * 80 + \"\\n\")\n", " f.write(f\"{'Label':<25} {'ECE':>10} {'Brier':>10}\\n\")\n", " f.write(\"-\" * 80 + \"\\n\")\n", " for i, label in enumerate(label_names):\n", " f.write(f\"{label:<25} {ece_scores[i]:>10.4f} {brier_scores[i]:>10.4f}\\n\")\n", " \n", " # Confusion matrices for each label\n", " print(\"Generating confusion matrices...\")\n", " fig, axes = plt.subplots(int(np.ceil(num_labels/5)), 5, figsize=(25, 5*int(np.ceil(num_labels/5))), dpi=600)\n", " axes = axes.flatten() if num_labels > 1 else [axes]\n", " \n", " for i, label in enumerate(label_names[:min(num_labels, len(axes))]):\n", " cm = confusion_matrix(test_targets[:, i], test_pred_labels[:, i])\n", " sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', ax=axes[i], \n", " annot_kws={'size': 14, 'weight': 'bold'},\n", " cbar_kws={'label': 'Count'})\n", " axes[i].set_title(label, fontweight='bold', fontsize=12)\n", " axes[i].set_xlabel('Predicted', fontweight='bold', fontsize=10)\n", " axes[i].set_ylabel('Actual', fontweight='bold', fontsize=10)\n", " \n", " # Hide unused subplots\n", " for i in range(num_labels, len(axes)):\n", " axes[i].axis('off')\n", " \n", " plt.tight_layout()\n", " plt.savefig('confusion_matrices.png', dpi=600, bbox_inches='tight')\n", " plt.close()\n", " \n", " # ROC Curves\n", " print(\"Generating ROC curves...\")\n", " plt.figure(figsize=(15, 10), dpi=600)\n", " \n", " for i, label in enumerate(label_names[:min(20, num_labels)]): # Plot first 20 to avoid clutter\n", " try:\n", " fpr, tpr, _ = roc_curve(test_targets[:, i], test_preds[:, i])\n", " roc_auc = roc_auc_score(test_targets[:, i], test_preds[:, i])\n", " plt.plot(fpr, tpr, linewidth=2, label=f'{label} (AUC={roc_auc:.3f})', alpha=0.7)\n", " except:\n", " continue\n", " \n", " plt.plot([0, 1], [0, 1], 'k--', linewidth=2, label='Random')\n", " plt.xlabel('False Positive Rate', fontweight='bold', fontsize=14)\n", " plt.ylabel('True Positive Rate', fontweight='bold', fontsize=14)\n", " plt.title('ROC Curves', fontweight='bold', fontsize=16)\n", " plt.legend(loc='lower right', fontsize=8)\n", " plt.grid(True, alpha=0.3)\n", " plt.tight_layout()\n", " plt.savefig('roc_curves.png', dpi=600, bbox_inches='tight')\n", " plt.close()\n", " \n", " # Precision-Recall Curves\n", " print(\"Generating PR curves...\")\n", " plt.figure(figsize=(15, 10), dpi=600)\n", " \n", " for i, label in enumerate(label_names[:min(20, num_labels)]):\n", " try:\n", " precision, recall, _ = precision_recall_curve(test_targets[:, i], test_preds[:, i])\n", " ap = average_precision_score(test_targets[:, i], test_preds[:, i])\n", " plt.plot(recall, precision, linewidth=2, label=f'{label} (AP={ap:.3f})', alpha=0.7)\n", " except:\n", " continue\n", " \n", " plt.xlabel('Recall', fontweight='bold', fontsize=14)\n", " plt.ylabel('Precision', fontweight='bold', fontsize=14)\n", " plt.title('Precision-Recall Curves', fontweight='bold', fontsize=16)\n", " plt.legend(loc='best', fontsize=8)\n", " plt.grid(True, alpha=0.3)\n", " plt.tight_layout()\n", " plt.savefig('pr_curves.png', dpi=600, bbox_inches='tight')\n", " plt.close()\n", " \n", " # Calibration curve\n", " print(\"Generating calibration curve...\")\n", " plt.figure(figsize=(10, 8), dpi=600)\n", " \n", " n_bins = 10\n", " for i in range(min(5, num_labels)): # Show first 5 labels\n", " fraction_of_positives, mean_predicted_value = [], []\n", " \n", " for j in range(n_bins):\n", " bin_lower = j / n_bins\n", " bin_upper = (j + 1) / n_bins\n", " \n", " in_bin = (test_preds[:, i] > bin_lower) & (test_preds[:, i] <= bin_upper)\n", " \n", " if in_bin.sum() > 0:\n", " fraction_of_positives.append(test_targets[:, i][in_bin].mean())\n", " mean_predicted_value.append(test_preds[:, i][in_bin].mean())\n", " \n", " plt.plot(mean_predicted_value, fraction_of_positives, 'o-', \n", " linewidth=2, markersize=8, label=label_names[i], alpha=0.7)\n", " \n", " plt.plot([0, 1], [0, 1], 'k--', linewidth=2, label='Perfect Calibration')\n", " plt.xlabel('Mean Predicted Probability', fontweight='bold', fontsize=14)\n", " plt.ylabel('Fraction of Positives', fontweight='bold', fontsize=14)\n", " plt.title('Calibration Curve', fontweight='bold', fontsize=16)\n", " plt.legend(fontsize=10)\n", " plt.grid(True, alpha=0.3)\n", " plt.tight_layout()\n", " plt.savefig('calibration_curve.png', dpi=600, bbox_inches='tight')\n", " plt.close()\n", " \n", " # Calculate number of parameters and FLOPs (approximation)\n", " def count_parameters(model):\n", " return sum(p.numel() for p in model.parameters() if p.requires_grad)\n", " \n", " num_params = count_parameters(model)\n", " print(f\"\\nNumber of trainable parameters: {num_params:,}\")\n", " \n", " with open('results.txt', 'a') as f:\n", " f.write(f\"\\n\\nModel Statistics:\\n\")\n", " f.write(\"-\" * 80 + \"\\n\")\n", " f.write(f\"Trainable Parameters: {num_params:,}\\n\")\n", " \n", " # Misclassification analysis\n", " print(\"Generating misclassification analysis...\")\n", " misclass_per_label = []\n", " for i in range(num_labels):\n", " misclass = (test_pred_labels[:, i] != test_targets[:, i]).sum()\n", " misclass_per_label.append(misclass)\n", " \n", " plt.figure(figsize=(20, 6), dpi=600)\n", " plt.bar(range(min(30, num_labels)), misclass_per_label[:30], color='coral', edgecolor='black')\n", " plt.xlabel('Label Index', fontweight='bold', fontsize=14)\n", " plt.ylabel('Number of Misclassifications', fontweight='bold', fontsize=14)\n", " plt.title('Misclassification Analysis', fontweight='bold', fontsize=16)\n", " plt.xticks(range(min(30, num_labels)), [label_names[i][:10] for i in range(min(30, num_labels))], \n", " rotation=45, ha='right', fontweight='bold')\n", " plt.grid(True, alpha=0.3, axis='y')\n", " plt.tight_layout()\n", " plt.savefig('misclassification_analysis.png', dpi=600, bbox_inches='tight')\n", " plt.close()\n", " \n", " # Confusion pairs analysis\n", " print(\"Analyzing confused label pairs...\")\n", " confusion_pairs = []\n", " \n", " for i in range(num_labels):\n", " for j in range(i+1, num_labels):\n", " # Cases where both are wrong in opposite directions\n", " confusion_count = ((test_targets[:, i] == 1) & (test_pred_labels[:, i] == 0) & \n", " (test_targets[:, j] == 0) & (test_pred_labels[:, j] == 1)).sum()\n", " confusion_count += ((test_targets[:, i] == 0) & (test_pred_labels[:, i] == 1) & \n", " (test_targets[:, j] == 1) & (test_pred_labels[:, j] == 0)).sum()\n", " \n", " if confusion_count > 0:\n", " confusion_pairs.append((label_names[i], label_names[j], confusion_count))\n", " \n", " # Sort and get top 10\n", " confusion_pairs.sort(key=lambda x: x[2], reverse=True)\n", " top_confused = confusion_pairs[:10]\n", " \n", " if top_confused:\n", " plt.figure(figsize=(12, 8), dpi=600)\n", " labels = [f\"{p[0][:8]}\\nvs\\n{p[1][:8]}\" for p in top_confused]\n", " counts = [p[2] for p in top_confused]\n", " \n", " plt.barh(range(len(top_confused)), counts, color='steelblue', edgecolor='black')\n", " plt.yticks(range(len(top_confused)), labels, fontweight='bold', fontsize=10)\n", " plt.xlabel('Confusion Count', fontweight='bold', fontsize=14)\n", " plt.title('Top 10 Most Confused Label Pairs', fontweight='bold', fontsize=16)\n", " plt.grid(True, alpha=0.3, axis='x')\n", " plt.tight_layout()\n", " plt.savefig('confused_pairs.png', dpi=600, bbox_inches='tight')\n", " plt.close()\n", " \n", " print(\"\\n\" + \"=\"*80)\n", " print(\"Training and evaluation complete!\")\n", " print(f\"Best validation accuracy: {best_val_acc:.4f}\")\n", " print(f\"Test accuracy: {test_acc:.4f}\")\n", " print(\"All results and figures saved.\")\n", " print(\"=\"*80)" ] }, { "cell_type": "code", "execution_count": null, "id": "93ce28ef", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "pytor", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.11" } }, "nbformat": 4, "nbformat_minor": 5 }