-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVGGNet.py
64 lines (45 loc) · 1.81 KB
/
VGGNet.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
import tensorflow as tf
from keras.applications.vgg16 import VGG16
from keras import backend as K
from keras.layers import Input
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense, Dropout, Add
from keras.models import load_model
from keras.layers import concatenate
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
vgg16_model = VGG16()
vgg16_model.summary()
model = Sequential()
for layer in vgg16_model.layers:
model.add(layer)
model.layers.pop()
for layer in model.layers:
layer.trainabel = False
model.add(Dense(2, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='adam')
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('ExperimentDataSet/TrainingData', # Path to the target directory. It should contain one subdirectory per class.
target_size = (64, 64),
batch_size = 20,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('ExperimentDataSet/ValidationData',
target_size = (64, 64),
batch_size = 25, #if the size of the dataset is not divisible by the batch size. The generator is expected to loop over its data indefinitely.
class_mode = 'binary')
model.fit_generator(training_set,
steps_per_epoch = 1744,#Total number of steps (batches of samples) to yield from generator before declaring one epoch finished and starting the next epoch
epochs = 25,
validation_data = test_set,
validation_steps = 155)
score = model.evaluate_generator(test_set, 155, workers=1)
scores = model.predict_generator(test_set, 155, workers=1)
correct = 0
model.save('VGGTrained_.h5')