-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize - single channel grey.py
99 lines (78 loc) · 2.67 KB
/
visualize - single channel grey.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
"""
visualize results for test image
"""
import numpy as np
# import matplotlib.pyplot as plt
from PIL import Image
import torch
import torch.nn as nn
import torch.nn.functional as F
import os
from torch.autograd import Variable
import transforms as transforms
# from skimage import io
# from skimage.transform import resize
# from models import *
from models.resnet_reg2 import ResNet18RegressionTwoOutputs
cut_size = 44
transform_test = transforms.Compose([
transforms.RandomCrop(cut_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
])
img = np.array(Image.open("images/9_3=Happy.jpg")) # Replace with the actual path to your image file
img = img.reshape(48, 48)
img = np.expand_dims(img, axis=2)
img = img.astype(np.uint8)
img = img[:, :, np.newaxis]
img = np.concatenate((img, img, img), axis=2)
img = img.astype(np.uint8)
img = Image.fromarray(img)
inputs = transform_test(img)
net = ResNet18RegressionTwoOutputs()
checkpoint = torch.load(os.path.join('FER2013_ResNet18RegressionTwoOutputs', 'PrivateTest_model.t7'))
net.load_state_dict(checkpoint['net'])
net.cuda()
net.eval()
c, h, w = np.shape(inputs)
inputs = inputs.view(-1, c, h, w)
inputs = inputs.cuda()
with torch.no_grad():
inputs = Variable(inputs, volatile=True)
outputs = net(inputs)
print(outputs)
# outputs_avg = outputs.view(ncrops, -1).mean(0) # avg over crops
'''
score = F.softmax(outputs_avg)
_, predicted = torch.max(outputs_avg.data, 0)
plt.rcParams['figure.figsize'] = (13.5,5.5)
axes=plt.subplot(1, 3, 1)
plt.imshow(raw_img)
plt.xlabel('Input Image', fontsize=16)
axes.set_xticks([])
axes.set_yticks([])
plt.tight_layout()
plt.subplots_adjust(left=0.05, bottom=0.2, right=0.95, top=0.9, hspace=0.02, wspace=0.3)
plt.subplot(1, 3, 2)
ind = 0.1+0.6*np.arange(len(class_names)) # the x locations for the groups
width = 0.4 # the width of the bars: can also be len(x) sequence
color_list = ['red','orangered','darkorange','limegreen','darkgreen','royalblue','navy']
for i in range(len(class_names)):
plt.bar(ind[i], score.data.cpu().numpy()[i], width, color=color_list[i])
plt.title("Classification results ",fontsize=20)
plt.xlabel(" Expression Category ",fontsize=16)
plt.ylabel(" Classification Score ",fontsize=16)
plt.xticks(ind, class_names, rotation=45, fontsize=14)
axes=plt.subplot(1, 3, 3)
emojis_img = io.imread('images/emojis/%s.png' % str(class_names[int(predicted.cpu().numpy())]))
plt.imshow(emojis_img)
plt.xlabel('Emoji Expression', fontsize=16)
axes.set_xticks([])
axes.set_yticks([])
plt.tight_layout()
# show emojis
#plt.show()
plt.savefig(os.path.join('images/results/l.png'))
plt.close()
print("The Expression is %s" %str(class_names[int(predicted.cpu().numpy())]))
'''