-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshroom.py
108 lines (77 loc) · 3.35 KB
/
shroom.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
100
101
102
103
104
105
106
107
108
import pandas as pd
import numpy as np
from scipy import misc
import math
from matplotlib import pyplot as plt
def read_data(version):
DATASET_PATH = 'data/{}/'.format(version)
mushroom_classes = pd.read_json(DATASET_PATH + 'mushroom_classes.json', lines=True)
mushroom_imgs = pd.read_json(DATASET_PATH + 'mushroom_imgs.json', lines=True)
mushroom_info = mushroom_imgs.merge(mushroom_classes, how = "right", on = "name_latin")
def load_mushroom_images(folder_path, img_df):
img_dict = {}
for index, path in enumerate(img_df['file_path']):
img_dict[index] = misc.imread(folder_path + path)
return img_dict
img_dict = load_mushroom_images(DATASET_PATH, mushroom_imgs)
img_dict = format_images(img_dict)
X = np.stack(dict2list(img_dict))
y = mushroom_info.edibility.isin(("edible", "edible and good", "edible and excellent"))
y = pd.Series(y)
return X, y, mushroom_info
def format_images(img_dict):
#Format the pictures to (480,480,3) by padding them with the edge values
for img in img_dict:
height = 480 - img_dict[img].shape[0]
width = 480 - img_dict[img].shape[1]
if(height % 2 == 1 & width % 2 == 1):
height1,height2 = math.floor(height/2), math.floor(height/2) + 1
width1,width2 = math.floor(width/2), math.floor(width/2) +1
elif(width % 2 == 1):
width1,width2 = math.floor(width/2), math.floor(height/2) + 1
height1,height2 = int(height/2), int(height/2)
elif(height % 2 == 1):
height1,height2 = math.floor(height/2), math.floor(height/2) + 1
width1,width2 = int(width/2), int(width/2)
else:
height1,height2 = int(height/2), int(height/2)
width1,width2 = int(width/2), int(width/2)
if(height == 0):
img_dict[img] = np.lib.pad(img_dict[img], ((0,0),(width1, width2),(0,0)), 'edge')
elif (width == 0):
img_dict[img] = np.lib.pad(img_dict[img], ((height1, height2),(0,0),(0,0)), 'edge')
else:
img_dict[img] = np.lib.pad(img_dict[img], ((height1, height2),(width1, width2),(0,0)), 'edge')
return img_dict
def dict2list(dict):
X = []
for i in range(len(dict)):
X.append(dict[i])
return X
def draw_shroom(i, X, y):
plt.imshow(X[i])
plt.title("edible: " + str(y[i]))
plt.show()
def get_model(path = "models"):
from keras.models import model_from_json
import pickle
json_file = open(path + '/model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights(path + "/weights.h5")
with open(path + "/history", "rb") as f:
train_history = pickle.load(f)
return loaded_model, train_history
def precision_recall(decision_tresholds, probs, labels):
def recall(predictions, labels):
n = sum(labels)
k = sum(labels[predictions])
return k / n
def precision(predictions, labels):
n = sum(predictions)
k = sum(labels[predictions])
return k / n
recalls = [recall(probs > treshold, labels) for treshold in decision_tresholds]
precisions = [precision(probs > treshold, labels) for treshold in decision_tresholds]
return precisions, recalls