-
Notifications
You must be signed in to change notification settings - Fork 0
/
Feature_Vector.py
136 lines (77 loc) · 2.79 KB
/
Feature_Vector.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
128
129
130
131
132
#!/usr/bin/env python
# coding: utf-8
# In[42]:
import tensorflow as tf
from keras.applications.resnet50 import ResNet50
#from tensorflow.keras.applications.resnet50 import ResNet50
from keras.models import Model
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
from keras.layers import Flatten, Input
# In[39]:
from tensorflow.keras.models import Sequential
# In[3]:
filename = 'C:/Users/rania/Downloads/cifar10-pngs-in-folders/cifar10/cifar10/test/bird/0068.png'
# In[4]:
import matplotlib.pyplot as plt
import numpy as np
img = image.load_img(filename, target_size=(32, 32))
plt.imshow(img)
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
# In[105]:
base_model = ResNet50(weights='imagenet', pooling=max, include_top = False,input_shape=(32, 32, 3))
input = Input(shape=(32,32,3),name = 'img')
x = base_model(input)
print(x.shape)
x = Flatten()(x)
print(x.shape)
model = Model(inputs=input, outputs=x)
# In[102]:
#fetched properly of lastlayer(not the softmax)
features_old = model.predict(img)
features_old
# In[60]:
base_model.summary()
# In[72]:
#trying to fetch last but fourth layer(batch normalisation)
print(base_model.layers[-3].output)
# In[97]:
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers import InputLayer
base_model = tensorflow.keras.applications.resnet50.ResNet50(weights='imagenet', pooling=max, include_top = False)
#base_model = ResNet50(weights='imagenet', pooling=max, include_top = False)
#input = Input(shape=(32,32,3),name = 'img')
model = tensorflow.keras.Sequential()
model.add(InputLayer(input_shape=(32,32,3), name='img'))
for layer in base_model.layers[0:176]:
model.add(layer)
#intermediate_layer_model = Model(inputs=input,outputs = base_model.layers[-3].output)
# In[ ]:
features = model.predict(img)
features
# In[74]:
#from keras.models import Model
#base_model = ResNet50(weights='imagenet', pooling=max, include_top = False)
#model = base_model # include here your original model
#input = Input(shape=(32,32,3),name = 'img')
#print(input)
#layer_name = 'layer_!5'
#m = tf.keras.Sequential()
#m.add(base_model.layers[39].output)
#intermediate_layer_model = Model(inputs=input,
# outputs=model.get_layer(layer_name).output)
#intermediate_output = intermediate_layer_model.predict(data)
#model.summary()
# In[ ]:
#import tensorflow.compat.v1 as tf
#tf.disable_v2_behavior()
#intlayer = base_model.layers[100].output
#intlayer_reshape = tf.reshape(intlayer,shape = (1,1,1024))
#print(intlayer_reshape.get_shape())
#X_shape = tf.TensorShape([None]).concatenate(intlayer_reshape.get_shape()[0:])
#X = tf.placeholder_with_default(intlayer, shape=X_shape)
#X
# In[ ]: