-
Notifications
You must be signed in to change notification settings - Fork 7
/
trainTesnorCNN.py
120 lines (73 loc) · 2.45 KB
/
trainTesnorCNN.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
109
110
111
112
113
114
115
116
117
118
import dataset
import time
from datetime import timedelta
import math
import random
import numpy as np
from numpy.random import seed
from multiClassCNN import ConvNNTF
from databaseUtils import Database
from databaseUtils import getIntLabels
seed(1)
from tensorflow import set_random_seed
set_random_seed(2)
''' Here the things that we are going to do in this script:
1. Define your graph:
a) Create Convolutional Layers.
b) Create Fully-connected and Flatten Layers
c) Create Other optimizer, loss function and others
2. Prepare your input dataset for training.
3. Evaluating your own results.
'''
dataset = Database("vggfaces.json")
#dataset = Database("flowers17.json")
#dataset.createHDF5DatabaseGroups()
dataset.openDatabase()
trainset = dataset.getTrainset()
devset = dataset.getDevset()
testset = dataset.getTestset()
classNames = dataset.getClassNames()
print(classNames)
clsLabelDict = {}
labelClsDict = {}
# assign each class name a unique integer
lbl=0
for clsname in classNames:
clsLabelDict[clsname]= lbl
labelClsDict[lbl] = clsname
lbl+=1
trainStrLabels = trainset["strLabels"][:, 0]
trainLabels= getIntLabels(clsLabelDict,trainStrLabels)
print("trainLabels.shape",trainLabels.shape)
numBatch = len(trainset["index"])
start = trainset["index"][0,0]
end = trainset["index"][0,1]
imgsBatch1 = trainset["images"][start:end,:]
print(imgsBatch1.shape)
print(devset["images"].shape)
print("number of classes", len(clsLabelDict))
print("numbBatch = ", numBatch , " approx num of imgs in each batch = ", end-start)
#print(type(trainLabels))
#print(trainLabels.shape)
###
#print(TensorCNN._create_biases(2))
y= ConvNNTF._convert_to_one_hot(trainLabels, len(classNames))
print(y.shape)
numClasses=len(classNames)
print("numClasses =", numClasses)
numBatches= len(trainset["index"])
print(numBatches)
start = trainset["index"][numBatches-1,0]
end = trainset["index"][numBatches-1,1]
exampleMulticlassCNN = ConvNNTF();
#exampleMulticlassCNN.buildExpCNN(dataset.imageSize,
# dataset.numChannels,
# numClasses)
exampleMulticlassCNN.buildVggFaceSmall(dataset.imageSize,
dataset.numChannels,
numClasses)
cnnModelname="./model/" + dataset.datasetName
numIterations=400
#exampleMulticlassCNN.graphdisp()
exampleMulticlassCNN.train(trainset, devset, testset,clsLabelDict, cnnModelname, numIterations)
dataset.closeDatabase()