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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [
|
| 8 |
+
{
|
| 9 |
+
"name": "stderr",
|
| 10 |
+
"output_type": "stream",
|
| 11 |
+
"text": [
|
| 12 |
+
"/root/miniconda3/envs/torch/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
| 13 |
+
" from .autonotebook import tqdm as notebook_tqdm\n",
|
| 14 |
+
"/root/miniconda3/envs/torch/lib/python3.9/site-packages/diffusers/models/cross_attention.py:30: FutureWarning: Importing from cross_attention is deprecated. Please import from diffusers.models.attention_processor instead.\n",
|
| 15 |
+
" deprecate(\n"
|
| 16 |
+
]
|
| 17 |
+
}
|
| 18 |
+
],
|
| 19 |
+
"source": [
|
| 20 |
+
"from transformers import pipeline\n",
|
| 21 |
+
"import torch\n",
|
| 22 |
+
"from optimum.onnxruntime import ORTModelForImageClassification\n",
|
| 23 |
+
"from transformers import AutoImageProcessor\n",
|
| 24 |
+
"\n",
|
| 25 |
+
"from models import ResNetFN"
|
| 26 |
+
]
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"cell_type": "code",
|
| 30 |
+
"execution_count": 2,
|
| 31 |
+
"metadata": {},
|
| 32 |
+
"outputs": [
|
| 33 |
+
{
|
| 34 |
+
"name": "stderr",
|
| 35 |
+
"output_type": "stream",
|
| 36 |
+
"text": [
|
| 37 |
+
"Could not find image processor class in the image processor config or the model config. Loading based on pattern matching with the model's feature extractor configuration.\n",
|
| 38 |
+
"Some weights of the model checkpoint at microsoft/resnet-50 were not used when initializing ResNetModel: ['classifier.1.bias', 'classifier.1.weight']\n",
|
| 39 |
+
"- This IS expected if you are initializing ResNetModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
|
| 40 |
+
"- This IS NOT expected if you are initializing ResNetModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
|
| 41 |
+
]
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"data": {
|
| 45 |
+
"text/plain": [
|
| 46 |
+
"<All keys matched successfully>"
|
| 47 |
+
]
|
| 48 |
+
},
|
| 49 |
+
"execution_count": 2,
|
| 50 |
+
"metadata": {},
|
| 51 |
+
"output_type": "execute_result"
|
| 52 |
+
}
|
| 53 |
+
],
|
| 54 |
+
"source": [
|
| 55 |
+
"processor = AutoImageProcessor.from_pretrained(\"microsoft/resnet-50\")\n",
|
| 56 |
+
"model = ResNetFN()\n",
|
| 57 |
+
"state_dict = torch.load('/opt/data/private/graduation/autopilot/outputs/checkpoint-6321/pytorch_model.bin')\n",
|
| 58 |
+
"model.load_state_dict(state_dict)"
|
| 59 |
+
]
|
| 60 |
+
},
|
| 61 |
+
{
|
| 62 |
+
"cell_type": "code",
|
| 63 |
+
"execution_count": 3,
|
| 64 |
+
"metadata": {},
|
| 65 |
+
"outputs": [
|
| 66 |
+
{
|
| 67 |
+
"name": "stderr",
|
| 68 |
+
"output_type": "stream",
|
| 69 |
+
"text": [
|
| 70 |
+
"Could not find image processor class in the image processor config or the model config. Loading based on pattern matching with the model's feature extractor configuration.\n"
|
| 71 |
+
]
|
| 72 |
+
}
|
| 73 |
+
],
|
| 74 |
+
"source": [
|
| 75 |
+
"from datasets import load_dataset\n",
|
| 76 |
+
"from transformers import AutoImageProcessor\n",
|
| 77 |
+
"from torch.utils.data import DataLoader\n",
|
| 78 |
+
"import evaluate\n",
|
| 79 |
+
"from tqdm import tqdm\n",
|
| 80 |
+
"from torchvision.transforms import (\n",
|
| 81 |
+
" CenterCrop,\n",
|
| 82 |
+
" Compose,\n",
|
| 83 |
+
" Normalize,\n",
|
| 84 |
+
" RandomHorizontalFlip,\n",
|
| 85 |
+
" RandomResizedCrop,\n",
|
| 86 |
+
" Resize,\n",
|
| 87 |
+
" ToTensor,\n",
|
| 88 |
+
")\n",
|
| 89 |
+
"\n",
|
| 90 |
+
"image_processor = AutoImageProcessor.from_pretrained('microsoft/resnet-50')\n",
|
| 91 |
+
"size = image_processor.size[\"shortest_edge\"]\n",
|
| 92 |
+
"normalize = Normalize(mean=image_processor.image_mean, std=image_processor.image_std)\n",
|
| 93 |
+
"val_transforms = Compose(\n",
|
| 94 |
+
" [\n",
|
| 95 |
+
" Resize(size),\n",
|
| 96 |
+
" CenterCrop(size),\n",
|
| 97 |
+
" ToTensor(),\n",
|
| 98 |
+
" normalize,\n",
|
| 99 |
+
" ]\n",
|
| 100 |
+
")"
|
| 101 |
+
]
|
| 102 |
+
},
|
| 103 |
+
{
|
| 104 |
+
"cell_type": "code",
|
| 105 |
+
"execution_count": 4,
|
| 106 |
+
"metadata": {},
|
| 107 |
+
"outputs": [
|
| 108 |
+
{
|
| 109 |
+
"name": "stderr",
|
| 110 |
+
"output_type": "stream",
|
| 111 |
+
"text": [
|
| 112 |
+
"Resolving data files: 100%|ββββββββββ| 1805/1805 [00:00<00:00, 11224.96it/s]\n",
|
| 113 |
+
"Found cached dataset imagefolder (/root/.cache/huggingface/datasets/imagefolder/default-5ead7e559c62c602/0.0.0/37fbb85cc714a338bea574ac6c7d0b5be5aff46c1862c1989b20e0771199e93f)\n"
|
| 114 |
+
]
|
| 115 |
+
}
|
| 116 |
+
],
|
| 117 |
+
"source": [
|
| 118 |
+
"def collate_fn(examples):\n",
|
| 119 |
+
" pixel_values = torch.stack([example[\"pixel_values\"] for example in examples])\n",
|
| 120 |
+
" labels = torch.tensor([example[\"label\"] for example in examples])\n",
|
| 121 |
+
" return {\"pixel_values\": pixel_values, \"labels\": labels}\n",
|
| 122 |
+
"\n",
|
| 123 |
+
"def preprocess_val(example_batch):\n",
|
| 124 |
+
" \"\"\"Apply _val_transforms across a batch.\"\"\"\n",
|
| 125 |
+
" example_batch[\"pixel_values\"] = [val_transforms(image.convert(\"RGB\")) for image in example_batch[\"image\"]]\n",
|
| 126 |
+
" return example_batch\n",
|
| 127 |
+
"\n",
|
| 128 |
+
"eval_dataset = load_dataset('imagefolder', data_dir='/opt/data/private/graduation/autopilot/data/test', split='train').with_transform(preprocess_val)\n",
|
| 129 |
+
"eval_dataloader = DataLoader(eval_dataset, collate_fn=collate_fn, batch_size=1)"
|
| 130 |
+
]
|
| 131 |
+
},
|
| 132 |
+
{
|
| 133 |
+
"cell_type": "code",
|
| 134 |
+
"execution_count": null,
|
| 135 |
+
"metadata": {},
|
| 136 |
+
"outputs": [],
|
| 137 |
+
"source": [
|
| 138 |
+
"metric = evaluate.load(\"accuracy\")\n",
|
| 139 |
+
"\n",
|
| 140 |
+
"model = model.cuda()\n",
|
| 141 |
+
"model = model.cpu()\n",
|
| 142 |
+
"model.eval()\n",
|
| 143 |
+
"for step, batch in tqdm(enumerate(eval_dataloader)):\n",
|
| 144 |
+
" # for key in batch.keys():\n",
|
| 145 |
+
" # batch[key] = batch[key].cuda()\n",
|
| 146 |
+
" with torch.no_grad():\n",
|
| 147 |
+
" outputs = model(**batch)\n",
|
| 148 |
+
" predictions = outputs.logits.argmax(dim=-1)\n",
|
| 149 |
+
" references = batch[\"labels\"]\n",
|
| 150 |
+
" metric.add_batch(\n",
|
| 151 |
+
" predictions=predictions,\n",
|
| 152 |
+
" references=references,\n",
|
| 153 |
+
" )\n",
|
| 154 |
+
"eval_metric = metric.compute()"
|
| 155 |
+
]
|
| 156 |
+
},
|
| 157 |
+
{
|
| 158 |
+
"cell_type": "code",
|
| 159 |
+
"execution_count": 5,
|
| 160 |
+
"metadata": {},
|
| 161 |
+
"outputs": [
|
| 162 |
+
{
|
| 163 |
+
"name": "stderr",
|
| 164 |
+
"output_type": "stream",
|
| 165 |
+
"text": [
|
| 166 |
+
"/root/miniconda3/envs/torch/lib/python3.9/site-packages/transformers/models/convnext/feature_extraction_convnext.py:28: FutureWarning: The class ConvNextFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please use ConvNextImageProcessor instead.\n",
|
| 167 |
+
" warnings.warn(\n"
|
| 168 |
+
]
|
| 169 |
+
}
|
| 170 |
+
],
|
| 171 |
+
"source": [
|
| 172 |
+
"from optimum.onnxruntime import ORTModelForImageClassification\n",
|
| 173 |
+
"\n",
|
| 174 |
+
"save_directory = '/opt/data/private/graduation/autopilot/quantization/onnx'\n",
|
| 175 |
+
"model_onnx = ORTModelForImageClassification.from_pretrained(save_directory, file_name=\"model_quantized.onnx\")"
|
| 176 |
+
]
|
| 177 |
+
},
|
| 178 |
+
{
|
| 179 |
+
"cell_type": "code",
|
| 180 |
+
"execution_count": null,
|
| 181 |
+
"metadata": {},
|
| 182 |
+
"outputs": [],
|
| 183 |
+
"source": [
|
| 184 |
+
"metric = evaluate.load(\"accuracy\")\n",
|
| 185 |
+
"model_onnx = model.cuda()\n",
|
| 186 |
+
"# model_onnx.eval()\n",
|
| 187 |
+
"\n",
|
| 188 |
+
"for step, batch in tqdm(enumerate(eval_dataloader)):\n",
|
| 189 |
+
" for key in batch.keys():\n",
|
| 190 |
+
" batch[key] = batch[key].cuda()\n",
|
| 191 |
+
" with torch.no_grad():\n",
|
| 192 |
+
" outputs = model_onnx(**batch)\n",
|
| 193 |
+
" predictions = outputs.logits.argmax(dim=-1)\n",
|
| 194 |
+
" references = batch[\"labels\"]\n",
|
| 195 |
+
" metric.add_batch(\n",
|
| 196 |
+
" predictions=predictions,\n",
|
| 197 |
+
" references=references,\n",
|
| 198 |
+
" )\n",
|
| 199 |
+
"eval_metric_onnx = metric.compute()"
|
| 200 |
+
]
|
| 201 |
+
}
|
| 202 |
+
],
|
| 203 |
+
"metadata": {
|
| 204 |
+
"interpreter": {
|
| 205 |
+
"hash": "6f64be793538f7fe230f350828c9baf03d97c4df0981f52e8388f53f367f4a42"
|
| 206 |
+
},
|
| 207 |
+
"kernelspec": {
|
| 208 |
+
"display_name": "Python 3.9.16 ('torch')",
|
| 209 |
+
"language": "python",
|
| 210 |
+
"name": "python3"
|
| 211 |
+
},
|
| 212 |
+
"language_info": {
|
| 213 |
+
"codemirror_mode": {
|
| 214 |
+
"name": "ipython",
|
| 215 |
+
"version": 3
|
| 216 |
+
},
|
| 217 |
+
"file_extension": ".py",
|
| 218 |
+
"mimetype": "text/x-python",
|
| 219 |
+
"name": "python",
|
| 220 |
+
"nbconvert_exporter": "python",
|
| 221 |
+
"pygments_lexer": "ipython3",
|
| 222 |
+
"version": "3.9.16"
|
| 223 |
+
},
|
| 224 |
+
"orig_nbformat": 4
|
| 225 |
+
},
|
| 226 |
+
"nbformat": 4,
|
| 227 |
+
"nbformat_minor": 2
|
| 228 |
+
}
|
models.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch.nn as nn
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from transformers import AutoFeatureExtractor, AutoModel
|
| 5 |
+
from transformers.modeling_outputs import ImageClassifierOutput
|
| 6 |
+
|
| 7 |
+
class ResNetFN(nn.Module):
|
| 8 |
+
def __init__(self):
|
| 9 |
+
super(ResNetFN, self).__init__()
|
| 10 |
+
self.resnet = AutoModel.from_pretrained('microsoft/resnet-50')
|
| 11 |
+
self.fc1 = nn.Linear(2048, 512)
|
| 12 |
+
self.fc2 = nn.Linear(512, 2)
|
| 13 |
+
|
| 14 |
+
def forward(self, pixel_values, labels=None):
|
| 15 |
+
x1 = self.resnet(pixel_values=pixel_values)
|
| 16 |
+
x2 = F.relu(self.fc1(x1.pooler_output.squeeze(-1).squeeze(-1)))
|
| 17 |
+
x3 = self.fc2(x2)
|
| 18 |
+
loss_func = nn.BCEWithLogitsLoss()
|
| 19 |
+
loss = None
|
| 20 |
+
if labels != None:
|
| 21 |
+
onehot_labels = F.one_hot(labels, num_classes=2)
|
| 22 |
+
loss = loss_func(x3, onehot_labels.float())
|
| 23 |
+
return ImageClassifierOutput(loss=loss, logits=x3)
|