Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- image_size = 128
- mean_pixel = [103.939, 116.779, 123.68]
- def get_img(animal, index):
- file_path = 'data/train/%ss/%s%03d.jpg' % (animal, animal, index)
- img = cv2.resize(cv2.imread(file_path), (image_size, image_size))
- for c in range(3):
- img[:, :, c] = img[:, :, c] - mean_pixel[c]
- return img
- def get_data():
- X = []
- Y = []
- for i in range(0, 10000):
- X.append(get_img("cat", i))
- Y.append([0, 1, 0, 0, 0, 0, 0, 0])
- X.append(get_img("dog", i))
- Y.append([1, 0, 0, 0, 0, 0, 0, 0])
- return np.array(X), np.array(Y)
- if __name__ == "__main__":
- vgg16 = VGG16(weights='imagenet', include_top=False)
- input = Input(shape=(image_size, image_size, 3), name='image_input')
- output_vgg16_conv = vgg16(input)
- x = Flatten(name='flatten')(output_vgg16_conv)
- x = Dense(4096, activation='relu', name='fc1')(x)
- x = Dense(4096, activation='relu', name='fc2')(x)
- x = Dense(8, activation='softmax', name='predictions')(x)
- my_model = Model(input=input, output=x)
- my_model.summary()
- weights_path = "my_vgg16_weights.h5"
- if os.path.isfile(weights_path):
- my_model.load_weights(weights_path)
- checkpoint = ModelCheckpoint(weights_path, monitor='val_acc', verbose=0, save_best_only=False, mode='max')
- my_model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])
- X, Y = get_data()
- my_model.fit(X, Y, epochs=20, verbose=1, callbacks=[checkpoint], shuffle=True)
- cat_img = get_img("cat", 10001)
- cat_im = np.expand_dims(cat_img, axis=0)
- print(my_model.predict(cat_im))
- dog_img = get_img("dog", 10001)
- dog_im = np.expand_dims(dog_img, axis=0)
- print(my_model.predict(dog_im))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement