-
Notifications
You must be signed in to change notification settings - Fork 6
/
feature_extraction1.py
56 lines (42 loc) · 1.66 KB
/
feature_extraction1.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
# -*- coding: utf-8 -*-
"""Feature_extraction1.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/19ThC4NyRtLoSvMLw2gY-KdjK42VW6DfU
"""
from google.colab import drive
drive.mount("/content/drive", force_remount=True)
import os
os.chdir('/content/drive/MyDrive/Colab Notebooks/Mask_RCNN/samples/videos/save2')
import tensorflow as tf
from keras.applications.resnet50 import ResNet50
from keras.applications.vgg16 import VGG16
from keras.layers import Flatten, Input
from keras.models import Model
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
import numpy as np
import pandas as pd
import time
import cv2
model1 = ResNet50(weights='imagenet', pooling=max, include_top = False)
model2 = VGG16(weights='imagenet', include_top=False)
root_dir = os.getcwd()
features=[]
start = time.time()
for i in range(10):
path = os.path.join(root_dir, str(i))
for filename in os.listdir(path):
img_path = os.path.join(path,str(filename))
img = image.load_img(img_path, target_size=(224, 224))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
vgg16_feature = model2.predict(img_data)
features.append(vgg16_feature.squeeze())
end = time.time()
print('\ntime spend: ' , (end - start)/60 , ' minutes \n')
np.array(features).shape
extracted_features = np.array(features).reshape(810,-1)
df = pd.DataFrame(extracted_features)
df.to_csv('/content/drive/MyDrive/Colab Notebooks/Mask_RCNN/samples/videos/save2/extracted_features_vgg16.txt' , index=False, sep =' ')