-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel training and saving.py
55 lines (40 loc) · 1.75 KB
/
model training and saving.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
#download the dataset
'''
from pygoogle_image import image as pi
pi.download("su 30 mki", limit = 100)
'''
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
from keras.preprocessing.image import ImageDataGenerator
model = Sequential()
model.add(Conv2D(32, (3,3), input_shape=(64, 64, 3), activation= 'relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(units=128, activation= 'relu'))
model.add(Dense(units=1, activation= "sigmoid"))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics = ['accuracy'])
train_data = ImageDataGenerator(rescale=1/255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
val_data = ImageDataGenerator(rescale=1/255)
training_set = train_data.flow_from_directory("Dataset/train",
target_size=(64, 64),
batch_size=8,
class_mode= 'binary')
val_set = val_data.flow_from_directory("Dataset/vals",
target_size=(64, 64),
batch_size=8,
class_mode= 'binary')
model.fit_generator(training_set,
steps_per_epoch= 10,
epochs=100,
validation_data=val_set,
validation_steps=2)
model_json = model.to_json()
with open("model.json","w") as file:
file.write(model_json)
model.save_weights("model.h5")