[df6751]: / utils.py

Download this file

47 lines (38 with data), 1.3 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
import torch
from torch.autograd import Variable
import matplotlib.pyplot as plt
from torchnet import meter
def plot_training(costs, accs):
'''
Plots curve of Cost vs epochs and Accuracy vs epochs for 'train' and 'valid' sets during training
'''
train_acc = accs['train']
valid_acc = accs['valid']
train_cost = costs['train']
valid_cost = costs['valid']
epochs = range(len(train_acc))
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1,)
plt.plot(epochs, train_acc)
plt.plot(epochs, valid_acc)
plt.legend(['train', 'valid'], loc='upper left')
plt.title('Accuracy')
plt.subplot(1, 2, 2)
plt.plot(epochs, train_cost)
plt.plot(epochs, valid_cost)
plt.legend(['train', 'valid'], loc='upper left')
plt.title('Cost')
plt.show()
def n_p(x):
'''convert numpy float to Variable tensor float'''
return Variable(torch.cuda.FloatTensor([x]), requires_grad=False)
def get_count(df, cat):
'''
Returns number of images in a study type dataframe which are of abnormal or normal
Args:
df -- dataframe
cat -- category, "positive" for abnormal and "negative" for normal
'''
return df[df['Path'].str.contains(cat)]['Count'].sum()
if __name__=='main':
pass