-
Notifications
You must be signed in to change notification settings - Fork 0
/
Baseline_EffNet.py
215 lines (130 loc) · 4.65 KB
/
Baseline_EffNet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import os
import cv2
import time
import timm
import torch
import sklearn.metrics
from PIL import Image
import numpy as np
import pandas as pd
import torch.nn as nn
from torch.optim import SGD, lr_scheduler
from torch.utils.data import DataLoader, Dataset
os.environ["CUDA_VISIBLE_DEVICES"]="1"
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
device
# In[2]:
metadata = pd.read_csv("SnakeCLEF2021_train_metadata_PROD.csv")
min_train_metadata = pd.read_csv("SnakeCLEF2021_min-train_metadata_PROD.csv")
print(len(metadata), len(min_train_metadata))
# In[3]:
metadata.head(5)
# In[4]:
train_metadata = min_train_metadata
val_metadata = metadata[metadata['subset'] == 'val']
print(len(train_metadata), len(val_metadata))
len(min_train_metadata.binomial.unique())
# In[5]:
N_CLASSES = 772
class TrainDataset(Dataset):
def __init__(self, df, transform=None):
self.df = df
self.transform = transform
def __len__(self):
return len(self.df)
def __getitem__(self, idx):
file_path = self.df['image_path'].values[idx]
label = self.df['class_id'].values[idx]
image = cv2.imread(file_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
if self.transform:
augmented = self.transform(image=image)
image = augmented['image']
return image, label
# In[6]:
from efficientnet_pytorch import EfficientNet
model = EfficientNet.from_pretrained('efficientnet-b0')
model._fc = nn.Linear(model._fc.in_features, N_CLASSES)
# In[7]:
HEIGHT = 224
WIDTH = 224
from albumentations import Compose, Normalize, Resize, HorizontalFlip, VerticalFlip
from albumentations.pytorch import ToTensorV2
from albumentations import RandomCrop, HorizontalFlip, VerticalFlip, RandomBrightnessContrast, CenterCrop, PadIfNeeded, RandomResizedCrop
def get_transforms(*, data):
assert data in ('train', 'valid')
if data == 'train':
return Compose([
RandomResizedCrop(WIDTH, HEIGHT, scale=(0.8, 1.0)),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
RandomBrightnessContrast(p=0.2),
Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225],
),
ToTensorV2(),
])
elif data == 'valid':
return Compose([
Resize(WIDTH, HEIGHT),
Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225],
),
ToTensorV2(),
])
# In[8]:
train_dataset = TrainDataset(train_metadata, transform=get_transforms(data='train'))
valid_dataset = TrainDataset(val_metadata, transform=get_transforms(data='valid'))
# In[9]:
BATCH_SIZE = 64
EPOCHS = 50
WORKERS = 8
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=WORKERS)
valid_loader = DataLoader(valid_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=WORKERS)
# In[10]:
from sklearn.metrics import f1_score, accuracy_score
import tqdm
n_epochs = EPOCHS
lr = 0.01
optimizer = SGD(model.parameters(), lr=lr, momentum=0.9)
scheduler = lr_scheduler.StepLR(optimizer, step_size=5)
criterion = nn.CrossEntropyLoss()
model.to(device)
for epoch in range(n_epochs):
start_time = time.time()
model.train()
avg_loss = 0.
optimizer.zero_grad()
for i, (images, labels) in tqdm.tqdm(enumerate(train_loader)):
images = images.to(device)
labels = labels.to(device)
y_preds = model(images)
loss = criterion(y_preds, labels)
avg_loss += loss.item() / len(train_loader)
loss.backward()
optimizer.step()
optimizer.zero_grad()
model.eval()
avg_val_loss = 0.
preds = np.zeros((len(valid_dataset)))
for i, (images, labels) in enumerate(valid_loader):
images = images.to(device)
labels = labels.to(device)
with torch.no_grad():
y_preds = model(images)
preds[i * BATCH_SIZE: (i+1) * BATCH_SIZE] = y_preds.argmax(1).to('cpu').numpy()
loss = criterion(y_preds, labels)
avg_val_loss += loss.item() / len(valid_loader)
scheduler.step()
score = f1_score(val_metadata['class_id'], preds, average='macro')
accuracy = accuracy_score(val_metadata['class_id'], preds)
elapsed = time.time() - start_time
print(f' Epoch {epoch+1} - avg_train_loss: {avg_loss:.4f} avg_val_loss: {avg_val_loss:.4f} F1: {score:.6f} Accuracy: {accuracy:.6f} time: {elapsed:.0f}s')
# In[11]:
torch.save(model.state_dict(), f'SnakeCLEF2021-EfficientNet-B0_224-50E.pth')
# In[ ]: