|
a |
|
b/utils.py |
|
|
1 |
import torch |
|
|
2 |
from torch.autograd import Variable |
|
|
3 |
import matplotlib.pyplot as plt |
|
|
4 |
from torchnet import meter |
|
|
5 |
|
|
|
6 |
def plot_training(costs, accs): |
|
|
7 |
''' |
|
|
8 |
Plots curve of Cost vs epochs and Accuracy vs epochs for 'train' and 'valid' sets during training |
|
|
9 |
''' |
|
|
10 |
train_acc = accs['train'] |
|
|
11 |
valid_acc = accs['valid'] |
|
|
12 |
train_cost = costs['train'] |
|
|
13 |
valid_cost = costs['valid'] |
|
|
14 |
epochs = range(len(train_acc)) |
|
|
15 |
|
|
|
16 |
plt.figure(figsize=(10, 5)) |
|
|
17 |
|
|
|
18 |
plt.subplot(1, 2, 1,) |
|
|
19 |
plt.plot(epochs, train_acc) |
|
|
20 |
plt.plot(epochs, valid_acc) |
|
|
21 |
plt.legend(['train', 'valid'], loc='upper left') |
|
|
22 |
plt.title('Accuracy') |
|
|
23 |
|
|
|
24 |
plt.subplot(1, 2, 2) |
|
|
25 |
plt.plot(epochs, train_cost) |
|
|
26 |
plt.plot(epochs, valid_cost) |
|
|
27 |
plt.legend(['train', 'valid'], loc='upper left') |
|
|
28 |
plt.title('Cost') |
|
|
29 |
|
|
|
30 |
plt.show() |
|
|
31 |
|
|
|
32 |
def n_p(x): |
|
|
33 |
'''convert numpy float to Variable tensor float''' |
|
|
34 |
return Variable(torch.cuda.FloatTensor([x]), requires_grad=False) |
|
|
35 |
|
|
|
36 |
def get_count(df, cat): |
|
|
37 |
''' |
|
|
38 |
Returns number of images in a study type dataframe which are of abnormal or normal |
|
|
39 |
Args: |
|
|
40 |
df -- dataframe |
|
|
41 |
cat -- category, "positive" for abnormal and "negative" for normal |
|
|
42 |
''' |
|
|
43 |
return df[df['Path'].str.contains(cat)]['Count'].sum() |
|
|
44 |
|
|
|
45 |
|
|
|
46 |
if __name__=='main': |
|
|
47 |
pass |