EUNSEO56 commited on
Commit
16220d7
β€’
1 Parent(s): ec48086

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+ import torchvision.transforms as transforms
4
+ import torch.nn as nn
5
+ import torch.optim as optim
6
+
7
+ # κΈ°λ³Έ λͺ¨λΈ λ‘œλ“œ
8
+ base_model = torchvision.models.vqa_resnet_finetune(pretrained=True)
9
+
10
+ # OK-VQA 데이터셋 λ‘œλ“œ 및 μ „μ²˜λ¦¬
11
+ # (μ—¬κΈ°μ—μ„œλŠ” 데이터λ₯Ό λ‘œλ“œν•˜λŠ” μ½”λ“œμ™€ μ „μ²˜λ¦¬ 과정을 κ°„λž΅ν•˜κ²Œ ν‘œν˜„ν•©λ‹ˆλ‹€)
12
+
13
+ train_dataset = OKVQADataset('train_data.json', transform=transforms.Compose([transforms.Resize((224, 224)), transforms.ToTensor()]))
14
+ train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True)
15
+
16
+ # μƒˆλ‘œμš΄ λ ˆμ΄μ–΄ μΆ”κ°€
17
+ num_classes = len(train_dataset.classes) # μ˜ˆμ‹œμ—μ„œλŠ” λ°μ΄ν„°μ…‹μ˜ 클래슀 수λ₯Ό μ‚¬μš©
18
+ base_model.fc = nn.Linear(base_model.fc.in_features, num_classes)
19
+
20
+ # GPU μ‚¬μš© κ°€λŠ₯ μ‹œ GPU둜 λͺ¨λΈ 이동
21
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
22
+ base_model = base_model.to(device)
23
+
24
+ # Loss 및 Optimizer μ •μ˜
25
+ criterion = nn.CrossEntropyLoss()
26
+ optimizer = optim.Adam(base_model.parameters(), lr=0.001)
27
+
28
+ # Fine-tuning
29
+ num_epochs = 10
30
+ for epoch in range(num_epochs):
31
+ for inputs, labels in train_loader:
32
+ inputs, labels = inputs.to(device), labels.to(device)
33
+
34
+ optimizer.zero_grad()
35
+ outputs = base_model(inputs)
36
+ loss = criterion(outputs, labels)
37
+ loss.backward()
38
+ optimizer.step()
39
+
40
+ print(f'Epoch {epoch+1}/{num_epochs}, Loss: {loss.item()}')
41
+ torch.save(base_model.state_dict(), 'git-vqa-finetuned-on-ok-vqa.pth')