-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathscreenshot_train.py
More file actions
executable file
·64 lines (48 loc) · 1.91 KB
/
screenshot_train.py
File metadata and controls
executable file
·64 lines (48 loc) · 1.91 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
#!/usr/bin/env python
"""
Generate tensorflow binary classification model for images, as model.h5.
Assume images are screenshots, and don't bother with augmentation.
Train the images found in in two subdirectories of training_set and test_set.
"""
import os
os.environ['KERAS_BACKEND'] = 'tensorflow'
import math
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
classifier = Sequential()
batch_size = 16
train_len = 72
test_len = 14
classifier.add(Convolution2D(batch_size, 3, 3, input_shape = (48, 54, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Convolution2D(batch_size, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Flatten())
classifier.add(Dense(output_dim = 128, activation = 'relu'))
classifier.add(Dense(output_dim = 1, activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
# Assume all originals are 1080p: 1920x1080 pixels, and scale by 40x horizontally, 20x vertically to 48x54
training_set = train_datagen.flow_from_directory(
'training_set',
target_size=(48, 54),
save_to_dir="tmp_resized_images",
batch_size=batch_size,
class_mode='binary')
test_set = test_datagen.flow_from_directory(
'test_set',
target_size=(48, 54),
batch_size=batch_size,
class_mode='binary')
classifier.fit_generator(
training_set,
steps_per_epoch=math.ceil(train_len / batch_size),
epochs=10,
validation_data=test_set,
validation_steps=math.ceil(test_len / batch_size))
classifier.save("model.h5")