{ "cells": [ { "cell_type": "code", "execution_count": 18, "id": "7091802b42f15ff3", "metadata": { "collapsed": false, "tags": [], "ExecuteTime": { "end_time": "2023-08-20T19:00:25.870983100Z", "start_time": "2023-08-20T19:00:25.811377600Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.9.17\n" ] } ], "source": [ "from platform import python_version\n", "print(python_version())" ] }, { "cell_type": "code", "execution_count": 19, "id": "initial_id", "metadata": { "collapsed": false, "tags": [], "ExecuteTime": { "end_time": "2023-08-20T19:00:25.959582500Z", "start_time": "2023-08-20T19:00:25.821371200Z" } }, "outputs": [], "source": [ "import argparse\n", "import time\n", "import os\n", "import sys\n", "import json\n", "import shutil\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import itertools\n", "import torch\n", "from torch.autograd import Variable\n", "from sklearn.metrics import confusion_matrix\n", "from torch.nn import functional as F" ] }, { "cell_type": "code", "execution_count": 20, "outputs": [], "source": [ "from generate_c3d_model import generate_model\n", "from target_transforms import ClassLabel\n", "from train import train_epoch\n", "from datasets.nv import NV\n", "from spatial_transforms import *\n", "from temporal_transforms import *\n", "from utils import *" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-08-20T19:00:25.960586600Z", "start_time": "2023-08-20T19:00:25.834767500Z" } }, "id": "6afa73e7e42f093" }, { "cell_type": "code", "execution_count": 21, "outputs": [], "source": [ "from logger.logger import get_logger\n", "logger = get_logger(__name__)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-08-20T19:00:25.960586600Z", "start_time": "2023-08-20T19:00:25.850811500Z" } }, "id": "d4931d40281f629" }, { "cell_type": "code", "execution_count": 22, "id": "4667ed32b4c9104b", "metadata": { "collapsed": false, "tags": [], "ExecuteTime": { "end_time": "2023-08-20T19:00:25.961579100Z", "start_time": "2023-08-20T19:00:25.866978900Z" } }, "outputs": [], "source": [ "arch = '{}'.format('c3d')\n", "n_epochs = 35\n", "n_classes = 27\n", "sample_size = 112\n", "sample_duration = 19\n", "ft_portion = \"last_layer\"\n", "downsample = 2\n", "scale_step = 0.84089641525\n", "scales = [1.0]\n", "for i in range(1, 5):\n", " scales.append(scales[-1] * scale_step)" ] }, { "cell_type": "code", "execution_count": 23, "id": "787ecfb4a99aff7c", "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-08-20T19:00:25.962582200Z", "start_time": "2023-08-20T19:00:25.880989900Z" } }, "outputs": [], "source": [ "def plot_cm(cm, classes, normalize = True):\n", " import seaborn as sns\n", " if normalize:\n", " cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n", " print(\"Normalized confusion matrix\")\n", " else:\n", " print('Confusion matrix, without normalization')\n", "\n", " ax= plt.subplot()\n", " sns.heatmap(cm, annot=False, ax = ax); #annot=True to annotate cells\n", "\n", " # labels, title and ticks\n", " ax.set_xlabel('Predicted labels');ax.set_ylabel('True labels'); \n", " plt.xticks(rotation='vertical')\n", " plt.yticks(rotation='horizontal')" ] }, { "cell_type": "code", "execution_count": 24, "id": "928ce7d00fa83416", "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-08-20T19:00:25.962582200Z", "start_time": "2023-08-20T19:00:25.897508300Z" } }, "outputs": [], "source": [ "def calculate_accuracy(outputs, targets, topk=(1,)):\n", " maxk = max(topk)\n", " batch_size = targets.size(0)\n", " _, pred = outputs.topk(maxk, 1, True, True)\n", " pred = pred.t()\n", " correct = pred.eq(targets.view(1, -1).expand_as(pred))\n", " ret = []\n", " for k in topk:\n", " correct_k = correct[:k].float().sum().item()\n", " ret.append(correct_k / batch_size)\n", "\n", " return ret" ] }, { "cell_type": "code", "execution_count": 25, "outputs": [ { "data": { "text/plain": "" }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.manual_seed(1)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-08-20T19:00:25.963581100Z", "start_time": "2023-08-20T19:00:25.911509600Z" } }, "id": "9ca636566f332603" }, { "cell_type": "code", "execution_count": 26, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "generate_c3d_model 2023-08-20 22:00:25,927 INFO Torch version: 1.13.1\n", "generate_c3d_model 2023-08-20 22:00:25,928 INFO Is CUDA enabled? True\n", "generate_c3d_model 2023-08-20 22:00:26,395 INFO Total number of trainable parameters: 48692379\n", "generate_c3d_model 2023-08-20 22:00:26,396 INFO Converting the pretrained model to RGB+D init model\n", "generate_c3d_model 2023-08-20 22:00:26,415 INFO Done. RGB-D model ready.\n" ] } ], "source": [ "model, parameters = generate_model(n_classes, sample_size, sample_duration, ft_portion)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-08-20T19:00:26.448812500Z", "start_time": "2023-08-20T19:00:25.928049600Z" } }, "id": "b21677097b3c23b" }, { "cell_type": "code", "execution_count": 27, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "DataParallel(\n", " (module): C3D(\n", " (group1): Sequential(\n", " (0): Conv3d(4, 64, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n", " (1): BatchNorm3d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (2): ReLU()\n", " (3): MaxPool3d(kernel_size=(2, 2, 2), stride=(1, 2, 2), padding=0, dilation=1, ceil_mode=False)\n", " )\n", " (group2): Sequential(\n", " (0): Conv3d(64, 128, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n", " (1): BatchNorm3d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (2): ReLU()\n", " (3): MaxPool3d(kernel_size=(2, 2, 2), stride=(2, 2, 2), padding=0, dilation=1, ceil_mode=False)\n", " )\n", " (group3): Sequential(\n", " (0): Conv3d(128, 256, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n", " (1): BatchNorm3d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (2): ReLU()\n", " (3): Conv3d(256, 256, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n", " (4): BatchNorm3d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (5): ReLU()\n", " (6): MaxPool3d(kernel_size=(2, 2, 2), stride=(2, 2, 2), padding=0, dilation=1, ceil_mode=False)\n", " )\n", " (group4): Sequential(\n", " (0): Conv3d(256, 512, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n", " (1): BatchNorm3d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (2): ReLU()\n", " (3): Conv3d(512, 512, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n", " (4): BatchNorm3d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (5): ReLU()\n", " (6): MaxPool3d(kernel_size=(2, 2, 2), stride=(2, 2, 2), padding=0, dilation=1, ceil_mode=False)\n", " )\n", " (group5): Sequential(\n", " (0): Conv3d(512, 512, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n", " (1): BatchNorm3d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (2): ReLU()\n", " (3): Conv3d(512, 512, kernel_size=(3, 3, 3), stride=(1, 1, 1), padding=(1, 1, 1))\n", " (4): BatchNorm3d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", " (5): ReLU()\n", " (6): MaxPool3d(kernel_size=(1, 2, 2), stride=(2, 2, 2), padding=(0, 1, 1), dilation=1, ceil_mode=False)\n", " )\n", " (fc1): Sequential(\n", " (0): Linear(in_features=8192, out_features=2048, bias=True)\n", " (1): ReLU()\n", " (2): Dropout(p=0.5, inplace=False)\n", " )\n", " (fc2): Sequential(\n", " (0): Linear(in_features=2048, out_features=2048, bias=True)\n", " (1): ReLU()\n", " (2): Dropout(p=0.5, inplace=False)\n", " )\n", " (fc): Sequential(\n", " (0): Linear(in_features=2048, out_features=27, bias=True)\n", " )\n", " )\n", ")\n", "Total number of trainable parameters: 48694107\n" ] } ], "source": [ "print(model)\n", "pytorch_total_params = sum(p.numel() for p in model.parameters() if\n", " p.requires_grad)\n", "print(\"Total number of trainable parameters: \", pytorch_total_params)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-08-20T19:00:26.449813900Z", "start_time": "2023-08-20T19:00:26.429671700Z" } }, "id": "40086c402cf2261e" }, { "cell_type": "code", "execution_count": 28, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "loading checkpoint _checkpoint.pth\n" ] }, { "data": { "text/plain": "" }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "resume_path = \"_checkpoint.pth\"\n", "print('loading checkpoint {}'.format(resume_path))\n", "checkpoint = torch.load(resume_path)\n", "begin_epoch = checkpoint['epoch']\n", "model.load_state_dict(checkpoint['state_dict'])" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-08-20T19:00:28.311462600Z", "start_time": "2023-08-20T19:00:26.444683600Z" } }, "id": "c7eeef76181abb66" }, { "cell_type": "code", "execution_count": 29, "outputs": [], "source": [ "crop_method = MultiScaleRandomCrop(scales, sample_size)\n", "norm_method = Normalize([0, 0, 0], [1, 1, 1])" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-08-20T19:00:28.326549300Z", "start_time": "2023-08-20T19:00:28.312466100Z" } }, "id": "f6ffc34b60e02c9a" }, { "cell_type": "code", "execution_count": 30, "outputs": [], "source": [ "spatial_transform = Compose([\n", " Scale(112),\n", " CenterCrop(112),\n", " ToTensor(1), norm_method\n", " ])\n", "temporal_transform = TemporalRandomCrop(sample_duration, downsample)\n", "target_transform = ClassLabel()" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-08-20T19:00:28.385798700Z", "start_time": "2023-08-20T19:00:28.327554100Z" } }, "id": "52fb95971e0be922" }, { "cell_type": "code", "execution_count": 31, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[INFO]: NV Dataset - validation is loading...\n", "dataset loading [0/482]\n" ] } ], "source": [ "test_data = NV(\n", " './nvGesture_v1',\n", " './annotation_nvGesture_v1/nvall_but_None.json',\n", " 'validation',\n", " spatial_transform=spatial_transform,\n", " temporal_transform=temporal_transform,\n", " target_transform=target_transform,\n", " sample_duration=sample_duration,\n", " modality=\"RGB-D\")" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-08-20T19:00:28.467110200Z", "start_time": "2023-08-20T19:00:28.345004100Z" } }, "id": "2e5ebec39ab2cc37" }, { "cell_type": "code", "execution_count": 32, "outputs": [], "source": [ "test_loader = torch.utils.data.DataLoader(\n", " test_data,\n", " batch_size=10,\n", " shuffle=True,\n", " num_workers=12,\n", " pin_memory=True)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-08-20T19:00:28.509818100Z", "start_time": "2023-08-20T19:00:28.469111900Z" } }, "id": "6a39ee355104b365" }, { "cell_type": "code", "execution_count": 33, "outputs": [], "source": [ "torch.cuda.empty_cache()" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2023-08-20T19:00:28.511340500Z", "start_time": "2023-08-20T19:00:28.483809100Z" } }, "id": "21527c9cef9a68b9" }, { "cell_type": "code", "execution_count": null, "id": "746588d6f3626a2a", "metadata": { "collapsed": false, "is_executing": true, "ExecuteTime": { "start_time": "2023-08-20T19:00:28.506822100Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "run\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "C:\\Users\\zxasv\\AppData\\Local\\Temp\\ipykernel_17088\\3359315552.py:20: UserWarning: Implicit dimension choice for softmax has been deprecated. Change the call to include dim=X as an argument.\n", " outputs = F.softmax(outputs)\n" ] } ], "source": [ "recorder = []\n", "print('run')\n", "model.eval()\n", "\n", "batch_time = AverageMeter()\n", "top1 = AverageMeter()\n", "top5 = AverageMeter()\n", "precisions = AverageMeter() #\n", "recalls = AverageMeter()\n", "\n", "y_true = []\n", "y_pred = []\n", "end_time = time.time()\n", "for i, (inputs, targets) in enumerate(test_loader):\n", " # targets = targets.cuda()\n", " with torch.no_grad():\n", " inputs = Variable(inputs)\n", " targets = Variable(targets)\n", " outputs = model(inputs)\n", " outputs = F.softmax(outputs)\n", " recorder.append(outputs.data.cpu().numpy().copy())\n", " y_true.extend(targets.cpu().numpy().tolist())\n", " y_pred.extend(outputs.argmax(1).cpu().numpy().tolist())\n", "\n", " if outputs.size(1) <= 4:\n", "\n", " prec1= calculate_accuracy(outputs, targets, topk=(1,))\n", " precision = calculate_precision(outputs, targets) #\n", " recall = calculate_recall(outputs,targets)\n", "\n", " top1.update(prec1[0], inputs.size(0))\n", " precisions.update(precision, inputs.size(0))\n", " recalls.update(recall,inputs.size(0))\n", "\n", " batch_time.update(time.time() - end_time)\n", " end_time = time.time()\n", "\n", " \n", " \n", " print('[{0}/{1}]\\t'\n", " 'Time {batch_time.val:.5f} ({batch_time.avg:.5f})\\t'\n", " 'prec@1 {top1.avg:.5f} \\t'\n", " 'precision {precision.val:.5f} ({precision.avg:.5f})\\t'\n", " 'recall {recall.val:.5f} ({recall.avg:.5f})'.format(\n", " i + 1,\n", " len(test_loader),\n", " batch_time=batch_time,\n", " top1 =top1,\n", " precision = precisions,\n", " recall = recalls))\n", " else:\n", "\n", " prec1, prec5 = calculate_accuracy(outputs, targets, topk=(1,5))\n", " precision = calculate_precision(outputs, targets) #\n", " recall = calculate_recall(outputs,targets)\n", "\n", "\n", " top1.update(prec1, inputs.size(0))\n", " top5.update(prec5, inputs.size(0))\n", " precisions.update(precision, inputs.size(0))\n", " recalls.update(recall,inputs.size(0))\n", "\n", " batch_time.update(time.time() - end_time)\n", " end_time = time.time()\n", " print('[{0}/{1}]\\t'\n", " 'Time {batch_time.val:.5f} ({batch_time.avg:.5f})\\t'\n", " 'prec@1 {top1.avg:.5f} prec@5 {top5.avg:.5f}\\t'\n", " 'precision {precision.val:.5f} ({precision.avg:.5f})\\t'\n", " 'recall {recall.val:.5f} ({recall.avg:.5f})'.format(\n", " i + 1,\n", " len(test_loader),\n", " batch_time=batch_time,\n", " top1 =top1,\n", " top5=top5,\n", " precision = precisions,\n", " recall = recalls))\n", "test_logger.log({\n", " 'top1': top1.avg,\n", " 'top5': top5.avg,\n", " 'precision':precisions.avg,\n", " 'recall':recalls.avg\n", " })\n", "\n", "print('-----Evaluation is finished------')\n", "print('Overall Prec@1 {:.05f}% Prec@5 {:.05f}%'.format(top1.avg, top5.avg))\n" ] }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [], "metadata": { "collapsed": false, "is_executing": true }, "id": "6eebd67c82beea45" } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.17" } }, "nbformat": 4, "nbformat_minor": 5 }