Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
150 lines (112 loc) · 4.61 KB
/
Copy pathmain.py
File metadata and controls
150 lines (112 loc) · 4.61 KB
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
# -*- coding: utf-8 -*-
"""Testing_saved_data.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1QnKd2Y5mdkVAqyxaVKN5bKmJvAT7bHQy
"""
import os
import torch
import torchvision
from torch.utils.data import random_split
import torchvision.models as models
import torch.nn as nn
import torch.nn.functional as F
# from google.colab import drive
# drive.mount('/content/drive', force_remount=True)
# data_dir = '/content/drive/MyDrive/Data Models/train'
# classes = os.listdir(data_dir)
# print(classes)
# dataset.classes equal ['Cardboard', 'Glass', 'Metal', 'Paper', 'Plastic', 'Trash']
classes= ['Cardboard', 'Coconut Shell', 'Face Mask', 'Glass', 'Metal', 'Organic', 'Paper', 'Plastic', 'Polythene', 'clothes','shoes']
from torchvision.datasets import ImageFolder
import torchvision.transforms as transforms
transformations = transforms.Compose([transforms.Resize((256, 256)), transforms.ToTensor()])
# dataset = ImageFolder(data_dir, transform = transformations)
def get_default_device():
"""Pick GPU if available, else CPU"""
if torch.cuda.is_available():
return torch.device('cuda')
else:
return torch.device('cpu')
def to_device(data, device):
"""Move tensor(s) to chosen device"""
if isinstance(data, (list,tuple)):
return [to_device(x, device) for x in data]
return data.to(device, non_blocking=True)
class DeviceDataLoader():
"""Wrap a dataloader to move data to a device"""
def __init__(self, dl, device):
self.dl = dl
self.device = device
def __iter__(self):
"""Yield a batch of data after moving it to device"""
for b in self.dl:
yield to_device(b, self.device)
def __len__(self):
"""Number of batches"""
return len(self.dl)
def accuracy(outputs, labels):
_, preds = torch.max(outputs, dim=1)
return torch.tensor(torch.sum(preds == labels).item() / len(preds))
class ImageClassificationBase(nn.Module):
def training_step(self, batch):
images, labels = batch
out = self(images) # Generate predictions
loss = F.cross_entropy(out, labels) # Calculate loss
return loss
def validation_step(self, batch):
images, labels = batch
out = self(images) # Generate predictions
loss = F.cross_entropy(out, labels) # Calculate loss
acc = accuracy(out, labels) # Calculate accuracy
return {'val_loss': loss.detach(), 'val_acc': acc}
def validation_epoch_end(self, outputs):
batch_losses = [x['val_loss'] for x in outputs]
epoch_loss = torch.stack(batch_losses).mean() # Combine losses
batch_accs = [x['val_acc'] for x in outputs]
epoch_acc = torch.stack(batch_accs).mean() # Combine accuracies
return {'val_loss': epoch_loss.item(), 'val_acc': epoch_acc.item()}
def epoch_end(self, epoch, result):
print("Epoch {}: train_loss: {:.4f}, val_loss: {:.4f}, val_acc: {:.4f}".format(
epoch+1, result['train_loss'], result['val_loss'], result['val_acc']))
class ResNet(ImageClassificationBase):
def __init__(self):
super().__init__()
# Use a pretrained model
self.network = models.resnet50(pretrained=True)
# Replace last layer
# num_ftrs = self.network.fc.in_features
# self.network.fc = nn.Linear(num_ftrs, len(dataset.classes))
def forward(self, xb):
return torch.sigmoid(self.network(xb))
model = ResNet()
device = get_default_device()
device
model = to_device(ResNet(), device)
# Model class must be defined somewhere
model = torch.load('Data Models/save5.h5',map_location=torch.device('cpu'))
model.eval()
def predict_image(img, model):
# Convert to a batch of 1
xb = to_device(img.unsqueeze(0), device)
# Get predictions from model
yb = model(xb)
# Pick index with highest probability
prob, preds = torch.max(yb, dim=1)
# Retrieve the class label
return classes[preds[0].item()]
loaded_model = model
from PIL import Image
from pathlib import Path
import matplotlib.pyplot as plt
def predict_external_image(image_name):
image = Image.open(Path('temp/' + image_name))
example_image = transformations(image)
plt.imshow(example_image.permute(1, 2, 0))
print("The image resembles", predict_image(example_image, loaded_model) + ".")
f = open("temp/res.txt", "w")
f.write(predict_image(example_image, loaded_model))
f.close()
predict_external_image('temp.jpg')
# predict_external_image('4.jpg')
# predict_external_image('metal408.jpg')