-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
80 lines (60 loc) · 1.8 KB
/
Copy pathmodel.py
File metadata and controls
80 lines (60 loc) · 1.8 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
import os
import cv2
from tensorflow.keras.layers import Dense,Flatten
from tensorflow.keras.applications.vgg16 import VGG16 as pre_trained_model, preprocess_input
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
#setting the paths
current=os.getcwd()
train_path = current+'\\'+'Dataset'+'\\'+'train'
validation_path = current+'\\'+'Dataset'+'\\'+'validation'
#getting the image shape
img=cv2.imread(train_path+'\\'+'rock'+'\\'+'rock2.png')
img_size=list(img.shape)[:-1]
#for getting the number of classes
classes=len(os.listdir(train_path))
ptm=pre_trained_model(input_shape=img_size+[3]
,weights='imagenet'
,include_top=False)
ptm.trainable=False
x=Flatten()(ptm.output)
x=Dense(classes,activation='softmax')(x)
model=Model(inputs=ptm.input,outputs=x)
model.summary()
# instance image generator
gen=ImageDataGenerator(
rotation_range=20,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.1,
zoom_range=0.2,
horizontal_flip=True,
preprocessing_function=preprocess_input
)
Batch_size=128
train_generator=gen.flow_from_directory(
train_path,
shuffle=True,
target_size=img_size,
batch_size=Batch_size)
valid_generator=gen.flow_from_directory(
validation_path,
target_size=img_size,
batch_size=Batch_size)
model.compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
# adding early stopping
from tensorflow.keras.callbacks import EarlyStopping
early_stop=EarlyStopping(monitor='val_loss',patience=2)
#fit model
results=model.fit_generator(
train_generator,
validation_data=valid_generator,
epochs=25,
callbacks=[early_stop]
)
#accuracy= approx 90%
# model.save('rock_paper_scissor.h5')