-
Notifications
You must be signed in to change notification settings - Fork 0
/
keras_transfer_catdog.py
128 lines (91 loc) · 4.06 KB
/
keras_transfer_catdog.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
from keras.preprocessing.image import ImageDataGenerator,img_to_array,array_to_img,load_img
import h5py
import os
import tensorflow as tf
tf.python.control_flow_ops = tf
import numpy as np
#import cv2, numpy as np
#装载对应层的weights
def load_weights(weights_path,model):
assert os.path.exists(weights_path), 'Model weights not found (see "weights_path" variable in script).'
f = h5py.File(weights_path)
for k in range(f.attrs['nb_layers']):
if k >= len(model.layers):
# we don't look at the last (fully-connected) layers in the savefile
break
g = f['layer_{}'.format(k)]
weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
model.layers[k].set_weights(weights)
f.close()
print('Model loaded.')
def VGG_16(weights_path=None):
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
#不要最后的FC层
model.add(Flatten())
if weights_path:
load_weights(weights_path,model)
return model
model = VGG_16("/home/max/下载/vgg16_weights.h5")
#不用rescale更好!!
#datagen = ImageDataGenerator(rescale=1./255)
datagen = ImageDataGenerator()
train_data = datagen.flow_from_directory("/home/max/data/train",target_size=(224,224)\
,shuffle=False,class_mode=None)
test_data = datagen.flow_from_directory("/home/max/data/test",target_size=(224,224)\
,shuffle=False,class_mode=None)
train_out = model.predict_generator(train_data,val_samples=2000)
test_out = model.predict_generator(test_data,val_samples=800)
np.save(open("/home/max/train.out",'w'),train_out)
np.save(open("/home/max/test.out",'w'),test_out)
def build_top():
train_feature = np.load("/home/max/train.out")
test_feature = np.load("/home/max/test.out")
train_label = np.array([0]*1000+[1]*1000)
test_label = np.array([0]*400+[1]*400)
model = Sequential()
#model.add(Flatten(input_shape = (1000,)))
model.add(Dense(256,activation='relu',input_shape=train_out.shape[1:]))
model.add(Dropout(0.5))
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])
model.fit(train_feature,train_label,validation_data=(test_feature,test_label),nb_epoch=50)