forked from deepak121993/ComputerVision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaltech_finetuning.py
104 lines (71 loc) · 3.39 KB
/
caltech_finetuning.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
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from pyImageSearch.preprocessing.imagetoarrayprocessor import ImageToArrayProcessor
from pyImageSearch.preprocessing.aspectawarepreprocess import AspectAwarePreprocessor
from pyImageSearch.dataset.simpleDatasetLoader import SimpleDatasetLoader
from pyImageSearch.preprocessing.simpleProcessor import SimplePreprocessor
from pyImageSearch.nn.conv.fcheadnet import FCHeadNet
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import RMSprop,SGD
from keras.applications import VGG16
from keras.layers import Input
from keras.models import Model
from imutils import paths
import numpy as np
import argparse
import os
ap = argparse.ArgumentParser()
ap.add_argument("-d","--dataset",required=True)
ap.add_argument("-m","--model",required=True)
args = vars(ap.parse_args())
aug = ImageDataGenerator(rotation_range=30,width_shift_range=0.1,\
height_shift_range=0.1,shear_range=0.2,zoom_range=0.2,horizontal_flip=True,fill_mode="nearest")
print("[INFO] loading images")
imagePaths = list(paths.list_images(args["dataset"]))
classNames = [pt.split(os.path.sep)[-2] for pt in imagePaths]
classNames = [str(x) for x in np.unique(classNames)]
print("len of images " , len(imagePaths))
print("class names ",classNames)
##initialize the preprocessing steps ::
sp = SimplePreprocessor(224,224)
aap = AspectAwarePreprocessor(224,224)
iap = ImageToArrayProcessor()
sdl = SimpleDatasetLoader(preprocessor=[sp,iap])
(data,label) = sdl.load(imagePaths,verbose=500)
print("shape ",data.shape )
data = data.astype("float32") / 255.0
# partition our data into training and test sets
(trainX, testX, trainY, testY) = train_test_split(data, label, test_size=0.25,
random_state=42)
# convert the labels from integers to vectors
trainY = LabelBinarizer().fit_transform(trainY)
testY = LabelBinarizer().fit_transform(testY)
print("[INFO] compiling model...")
baseModel = VGG16(weights="imagenet",include_top=False,input_tensor=Input(shape=(224,224,3)))
headModel = FCHeadNet.build(baseModel,len(classNames),256)
model = Model(inputs=baseModel.input,outputs=headModel)
for layer in baseModel.layers:
layer.trainable=False
print("[INFO] compiling model")
opt = RMSprop(lr=0.001)
model.compile(loss="categorical_crossentropy",optimizer=opt,metrics=["accuracy"])
print("[INFO] training head ...")
model.fit_generator(aug.flow(trainX,trainY,batch_size=32),validation_data=(testX,testY) ,epochs=25,\
steps_per_epoch=len(trainX)//32,verbose=1)
print("[INFO] evaluating after initilization..")
predictions = model.predict(testX,batch_size=32)
print(classification_report(testY.argmax(axis=1),predictions.argmax(axis=1),target_names=classNames))
for layer in baseModel.layers[15:]:
layer.trainable=True
print("[INFo] re-compiling model")
opt = SGD(lr=0.001)
model.compile(loss="categorical_crossentropy",optimizer=opt,metrics=["accuracy"])
print("[INFO] fine tunining model ...")
model.fit_generator(aug.flow(trainX,trainY,batch_size=32),validation_data=(testX,testY),epochs=10,\
steps_per_epoch=len(trainX)//32,verbose=1)
print("[INFO] evaluating after fine-tuning..")
predictions = model.predict(testX,batch_size=32)
print(classification_report(testY.argmax(axis=1),predictions.argmax(axis=1),target_names=classNames))
print("[INFO] Serilizing model...")
model.save(args["model"])