-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajorityobject.py
More file actions
121 lines (100 loc) · 2.76 KB
/
Copy pathmajorityobject.py
File metadata and controls
121 lines (100 loc) · 2.76 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
from voc_utils import list_image_sets,imgs_from_category_as_list,cat_name_to_cat_id,load_data_multilabel
from MajorityImageObject import Image
from cnn import Architectures
import random
random.seed(10)
import numpy as np
import sys
img_categories = list_image_sets()
imgsize = (227,227)
train_images = []
val_images = []
df = load_data_multilabel('train')
data = df.as_matrix()
for row in data:
imgobj = Image(row[0],imgsize[0],imgsize[1],row[1:].tolist())
train_images.append(imgobj)
df = load_data_multilabel('val')
data = df.as_matrix()
for row in data:
imgobj = Image(row[0],imgsize[0],imgsize[1],row[1:].tolist())
val_images.append(imgobj)
'''
all_images = train_images + val_images
trainl = int(0.9*len(all_images))
train_images = all_images[:trainl]
val_images = all_images[trainl:]
'''
del df
del data
def train_image_generator(batch_size=5):
while True:
random.shuffle(train_images)
inputcount = 0
features = []
target = []
for img in train_images:
if inputcount < batch_size-1:
feature,targetv = img.readData()
features.append(feature)
target.append(targetv)
inputcount+=1
else:
inputcount=0
feature,targetv = img.readData()
features.append(feature)
target.append(targetv)
yield [np.array(features),np.array(target)]
del features
del target
features = []
target = []
def val_image_generator(batch_size=5):
while True:
inputcount = 0
features = []
target = []
for img in val_images:
if inputcount < batch_size-1:
feature,targetv = img.readData()
features.append(feature)
target.append(targetv)
inputcount+=1
else:
inputcount=0
feature,targetv = img.readData()
features.append(feature)
target.append(targetv)
yield [np.array(features),np.array(target)]
del features
del target
features = []
target = []
def test_image_generator(batch_size=1000):
inputcount = 0
features = []
for img in test_images:
if inputcount < batch_size-1:
features.append(img.readData())
inputcount+=1
else:
inputcount=0
features.append(img.readData())
yield np.array(features)
del features
del names
features = []
names = []
if len(features)!=0:
yield np.array(features)
nnobj = Architectures()
#arch = nnobj.alexnetcam()
arch = nnobj.alexnet_branches()
#arch = nnobj.vgg16()
#arch = nnobj.vgg19()
traingen = train_image_generator(batch_size=100)
valgen = val_image_generator(batch_size=50)
#arch.fit_generator(traingen,samples_per_epoch=len(train_images)-17,validation_data=valgen ,nb_val_samples=len(val_images)-17,nb_epoch=15)
arch.fit_generator(traingen,samples_per_epoch=len(train_images)-17,nb_epoch=10)
arch.save_weights('newweights/alexvoc_branchavg.h5',overwrite=True)
#arch.save_weights('newweights/alexvoc_cam.h5',overwrite=True)