-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNN.py
62 lines (55 loc) · 1.87 KB
/
NN.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
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
import time
import random
import torch
import torchvision.models as models
#Loading images
train_folder_path_cats = r"/afhq/train/cat"
train_folder_path_dogs = r"/afhq/train/dog"
train_folder_path_wild = r"/afhq/train/wild"
images, targets = load_images(train_folder_path_cats, [1,0,0])
res = load_images(train_folder_path_dogs, [0,1,0])
images.extend(res[0])
targets.extend(res[1])
res = load_images(train_folder_path_wild, [0,0,1])
images.extend(res[0])
targets.extend(res[1])
def load_images(folder_path, class_name = None, size=(64, 64)):
images = []
image_class = []
for filename in os.listdir(folder_path):
img = cv2.imread(os.path.join(folder_path, filename))
if img is not None:
img = cv2.resize(img, size) # Resize image
images.append(img)
image_class.append(class_name)
return images, image_class #targets
#Reducing image size
images = [cv2.resize(img, (8, 8)) for img in images]
#Convert to grayscale
images = [cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) for img in images]
#Building the initial neural network
model = models.resnet18(pretrained=True)
#Training the network
model.fc = torch.nn.Linear(512, 3)
epochs = 3
criterion = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
compiled_model = torch.compile(model, criterion, optimizer, epochs)
for i in range(epochs):
compiled_model.train(images, targets)
#Results
print("Results:")
for i in range(len(images)):
img = images[i]
img = torch.tensor(img)
pred = compiled_model.predict(img)
pred = pred.detach().numpy()
pred = np.argmax(pred)
print("Image: ", i)
print("Prediction: ", pred)
print("Target: ", targets[i])
print("--------------------------------")