-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel1.py
72 lines (62 loc) · 2.51 KB
/
model1.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
import os
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import transforms, datasets
from torch.utils.data import DataLoader
# Define data transformations
data_transforms = transforms.Compose([
transforms.Resize((224, 224)), # Resize images to 224x224
transforms.ToTensor(), # Convert images to tensors
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) # Normalize pixel values
])
# Load the dataset with custom dataset class
class CustomDataset(datasets.ImageFolder):
def __getitem__(self, index):
path, _ = self.samples[index]
denomination = os.path.basename(os.path.dirname(path))
return super().__getitem__(index), denomination
train_data = CustomDataset(root='C:\\Users\\rjsli\\Desktop\\hackathon\\Train', transform=data_transforms)
train_loader = DataLoader(train_data, batch_size=32, shuffle=True)
# Define your neural network architecture
class SimpleClassifier(nn.Module):
def __init__(self, num_classes):
super(SimpleClassifier, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
)
self.classifier = nn.Sequential(
nn.Linear(64 * 112 * 112, num_classes),
)
def forward(self, x):
x = self.features(x)
x = torch.flatten(x, 1)
x = self.classifier(x)
return x
# Instantiate the model
num_classes = len(train_data.classes)
model = SimpleClassifier(num_classes)
# Define loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# Training loop
num_epochs = 6
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
for epoch in range(num_epochs):
model.train() # Set model to training mode
running_loss = 0.0
for (inputs, labels), denomination in train_loader:
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item() * inputs.size(0)
epoch_loss = running_loss / len(train_data)
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {epoch_loss:.4f}')
# Once training is complete, you can save the model for later use
torch.save(model.state_dict(), 'model.pth')