XXXXRT666
commited on
Commit
·
926ab7d
1
Parent(s):
640cb29
- .gitattributes +5 -0
- .gitignore +12 -0
- README.md +3 -3
- dataset-224.zip +3 -0
- dataset-384.zip +3 -0
- test_labels.csv +3 -0
- train_labels.csv +3 -0
- train_vit.py +126 -0
.gitattributes
CHANGED
@@ -56,3 +56,8 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
56 |
# Video files - compressed
|
57 |
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
58 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
56 |
# Video files - compressed
|
57 |
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
58 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
59 |
+
README.md filter=lfs diff=lfs merge=lfs -text
|
60 |
+
processed_dataset filter=lfs diff=lfs merge=lfs -text
|
61 |
+
test_labels.csv filter=lfs diff=lfs merge=lfs -text
|
62 |
+
train_labels.csv filter=lfs diff=lfs merge=lfs -text
|
63 |
+
|
.gitignore
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
test
|
2 |
+
train
|
3 |
+
logs
|
4 |
+
.DS_Store
|
5 |
+
test/*
|
6 |
+
train/*
|
7 |
+
dataset-224
|
8 |
+
dataset-384
|
9 |
+
dataset-224/*
|
10 |
+
dataset-384/*
|
11 |
+
train_lable.zip
|
12 |
+
test_lable.zip
|
README.md
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d8d7a46d41a1a37fe4f0a5f637bf55c649310185329127d8a2204632e480be17
|
3 |
+
size 24
|
dataset-224.zip
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5dbaf986e1523eb8b94f3030ae256775bcfe6473cd9cb6929c299e3337b66355
|
3 |
+
size 3110989729
|
dataset-384.zip
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:932c66d2e656d85e1799b49bbf9f49511988a7a091a81650796380ff498caf32
|
3 |
+
size 8499981466
|
test_labels.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a7e2868130702fede9a9d1e7163a898675b80a3a9ade62b697c6245af4fbb83e
|
3 |
+
size 136934
|
train_labels.csv
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:de31cdbbe45a5a99999d53614a8a734b0452ca3bf2c2361f853813ea5ad5e2c5
|
3 |
+
size 1230577
|
train_vit.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datasets import Dataset, DatasetDict, load_dataset, load_from_disk
|
2 |
+
from transformers import ViTForImageClassification, ViTImageProcessor, Trainer, TrainingArguments
|
3 |
+
from PIL import Image
|
4 |
+
from torch.optim import AdamW
|
5 |
+
from torch.optim.lr_scheduler import StepLR
|
6 |
+
import torch
|
7 |
+
import numpy as np
|
8 |
+
from sklearn.metrics import f1_score, accuracy_score, recall_score, precision_score
|
9 |
+
import os
|
10 |
+
|
11 |
+
|
12 |
+
MODEL_NAME = "/Users/XXXXRT/vit_pretrain/vit-base-patch16-384"
|
13 |
+
SIZE = "base"
|
14 |
+
PATCH = 16
|
15 |
+
IMAGE_SIZE = 384
|
16 |
+
BATCH_SIZE = 8
|
17 |
+
OPTIMIZER = "AdamW"
|
18 |
+
SCHEDULER = "StepLR"
|
19 |
+
|
20 |
+
IMAGE_PATH = '/Users/XXXXRT/ISIC-2019'
|
21 |
+
TRAIN_CSV_PATH = '/Users/XXXXRT/ISIC-2019/train_labels.csv'
|
22 |
+
TEST_CSV_PATH = '/Users/XXXXRT/ISIC-2019/test_labels.csv'
|
23 |
+
|
24 |
+
processed_dataset_path = f"/Users/XXXXRT/ISIC-2019/dataset-{IMAGE_SIZE}"
|
25 |
+
processed_dataset_path = f"/Volumes/T9 APFS/ML Dataset/dataset-{IMAGE_SIZE}"
|
26 |
+
|
27 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
28 |
+
device = torch.device("mps")
|
29 |
+
|
30 |
+
processor = ViTImageProcessor.from_pretrained(MODEL_NAME)
|
31 |
+
|
32 |
+
def preprocess_image_train(example):
|
33 |
+
image = Image.open(os.path.join(IMAGE_PATH,'train', example["image"])).convert("RGB")
|
34 |
+
example["pixel_values"] = processor(images=image, return_tensors="pt")["pixel_values"].squeeze(0).numpy()
|
35 |
+
|
36 |
+
labels = example.copy()
|
37 |
+
example["labels"] = np.array([labels["MEL"], labels["NV"], labels["BCC"], labels["AK"], labels["BKL"], labels["DF"], labels["VASC"], labels["SCC"], labels["UNK"]], dtype=np.float32)
|
38 |
+
|
39 |
+
return example
|
40 |
+
|
41 |
+
def preprocess_image_test(example):
|
42 |
+
image = Image.open(os.path.join(IMAGE_PATH,'test', example["image"])).convert("RGB")
|
43 |
+
example["pixel_values"] = processor(images=image, return_tensors="pt")["pixel_values"].squeeze(0).numpy()
|
44 |
+
|
45 |
+
labels = example.copy()
|
46 |
+
example["labels"] = np.array([labels["MEL"], labels["NV"], labels["BCC"], labels["AK"], labels["BKL"], labels["DF"], labels["VASC"], labels["SCC"], labels["UNK"]], dtype=np.float32)
|
47 |
+
|
48 |
+
return example
|
49 |
+
|
50 |
+
if os.path.exists(processed_dataset_path):
|
51 |
+
dataset = load_from_disk(processed_dataset_path)
|
52 |
+
print("LOADED")
|
53 |
+
else:
|
54 |
+
train_dataset = load_dataset('csv', data_files=TRAIN_CSV_PATH)["train"]
|
55 |
+
test_dataset = load_dataset('csv', data_files=TEST_CSV_PATH)["train"]
|
56 |
+
|
57 |
+
train_dataset = train_dataset.map(preprocess_image_train, batched=False, num_proc=2)
|
58 |
+
test_dataset = test_dataset.map(preprocess_image_test, batched=False, num_proc=2)
|
59 |
+
|
60 |
+
dataset = DatasetDict({
|
61 |
+
'train': train_dataset,
|
62 |
+
'test': test_dataset
|
63 |
+
})
|
64 |
+
dataset.save_to_disk(processed_dataset_path,num_proc=2)
|
65 |
+
print(f"SAVED TO {processed_dataset_path}")
|
66 |
+
|
67 |
+
train_dataset = dataset['train']
|
68 |
+
test_dataset = dataset['test']
|
69 |
+
|
70 |
+
num_labels = 9
|
71 |
+
|
72 |
+
model = ViTForImageClassification.from_pretrained(
|
73 |
+
MODEL_NAME,
|
74 |
+
num_labels=num_labels,
|
75 |
+
problem_type="multi_label_classification"
|
76 |
+
).to(device)
|
77 |
+
|
78 |
+
|
79 |
+
training_args = TrainingArguments(
|
80 |
+
output_dir=f"/Users/XXXXRT/ISIC-2019/logs/vit-{SIZE}-patch{PATCH}-{IMAGE_SIZE}-bs{BATCH_SIZE}-{OPTIMIZER}-{SCHEDULER}-lables-{num_labels}",
|
81 |
+
evaluation_strategy="epoch",
|
82 |
+
learning_rate=5e-5,
|
83 |
+
per_device_train_batch_size=BATCH_SIZE,
|
84 |
+
per_device_eval_batch_size=BATCH_SIZE,
|
85 |
+
num_train_epochs=5,
|
86 |
+
save_strategy="epoch",
|
87 |
+
logging_dir=f"/Users/XXXXRT/ISIC-2019/logs/vit-{SIZE}-patch{PATCH}-{IMAGE_SIZE}-bs{BATCH_SIZE}-{OPTIMIZER}-{SCHEDULER}-lables-{num_labels}/logs",
|
88 |
+
logging_steps=50,
|
89 |
+
report_to="tensorboard"
|
90 |
+
)
|
91 |
+
|
92 |
+
|
93 |
+
def compute_metrics(pred):
|
94 |
+
logits, labels = pred
|
95 |
+
predictions = (logits >= 0.5).astype(int)
|
96 |
+
f1 = f1_score(labels, predictions, average="macro")
|
97 |
+
accuracy = accuracy_score(labels, predictions)
|
98 |
+
recall = recall_score(labels, predictions, average="macro")
|
99 |
+
precision = precision_score(labels, predictions, average="macro")
|
100 |
+
return {
|
101 |
+
"accuracy": accuracy,
|
102 |
+
"f1": f1,
|
103 |
+
"recall": recall,
|
104 |
+
"precision": precision,
|
105 |
+
}
|
106 |
+
|
107 |
+
|
108 |
+
learning_rate = 5e-5
|
109 |
+
weight_decay = 0.01
|
110 |
+
step_size = 100
|
111 |
+
gamma = 0.1
|
112 |
+
optimizer = AdamW(model.parameters(), lr=learning_rate, weight_decay=weight_decay)
|
113 |
+
scheduler = StepLR(optimizer, step_size=step_size, gamma=gamma)
|
114 |
+
|
115 |
+
|
116 |
+
trainer = Trainer(
|
117 |
+
model=model,
|
118 |
+
args=training_args,
|
119 |
+
train_dataset=train_dataset,
|
120 |
+
eval_dataset=test_dataset,
|
121 |
+
compute_metrics=compute_metrics,
|
122 |
+
optimizers=(optimizer, scheduler)
|
123 |
+
)
|
124 |
+
|
125 |
+
|
126 |
+
trainer.train()
|