Comments (9)
Looks like there is come problem with the attributes of the h5 file. If you print the attributes of the h5 file that we are using, it do not seem to have the attribute nb_layers:
When i use the vgg16.h5 file created
import h5py
filename = 'vgg16.h5'
f = h5py.File(filename, 'r')
list(f.attrs)
output -> ['keras_version', 'backend', 'model_config']
As you see, it do not have the attribute nb_layers.
When I use a weight file downloaded from net:
File : vgg16_weights_tf_dim_ordering_tf_kernels.h5
import h5py
filename = 'vgg16_weights_tf_dim_ordering_tf_kernels.h5'
f = h5py.File(filename, 'r')
list(f.attrs)
Output -> ['layer_names']
So as you see, I cant see any attribute by name "nb_layers" in either files.
Author has used weights_path='../vgg16_weights.h5 but its not mentioned where he got this vgg16_weights.h5 file from Thats where the confusion is.
I am trying to do some workaround but will appreciate if anyone has a solution and can share.
Thanks!
from car-damage-detective.
@asharsid Have you solved the problem? I have the same situation! Could you tell me how to teal?
from car-damage-detective.
in my case, my vgg16_weights.h5 file has just 95 bytes.
so I downloaded new one from here.
https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3
from car-damage-detective.
@yuyifan1991 No, i was not able to find out the solution taking the "nb_layers" route. i ended up using a different approach to pop out the last layer of vgg16 and then inserting my own classifier.
To get a layer of a pretrained network as an input to your own model, use something like :
model.get_layer(layername).output function
Hope that helps. Thanks!
from car-damage-detective.
Apparently "nb_layers" refers to the number of layers, so instead you can use a work around.
In this case:
f = h5py.File(filename, 'r') nb_layers = len(f.attrs["layer_names"])
from car-damage-detective.
@Emadeldeen-24 - the work around above is not working. Throws me below error:
KeyError: "Can't open attribute (can't locate attribute: 'layer_names')".
Has anyone resolved this error by any other method/approach? If yes, please share insights. Thanks in advance.
from car-damage-detective.
nb_layers is not going to work. VGG!6 model has 16 layers in total, 13 convolution and 3 dense. The last dense layer in default trained VGG16 model has 1000 categories.
If you want to use the VGG16 model just for feature extraction, use the 13 convolution layers and maybe one dense layer. Pop out the last two layer or just the topmost layer. Once you extract the features, you can use any classifier(SVM or Logistic) to make predictions.
There are multiple ways of removing the top layers from VGG16. Search google.
Hope that helps.
from car-damage-detective.
Does anyone found a proper way to do it?
from car-damage-detective.
This is how I made it work. Please let me know if you still have any problems. I am basically not loading the weights into the load_vgg16() because of Keras 2, I am building the vgg16 network in the finetune_binary _model(). Check the following code.
def finetune_binary_model():
nb_epoch = 25
img_width, img_height = 256, 256
# model = load_vgg16()
# build the VGG16 network
base_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(3, img_width, img_height))
print('Model loaded.')
# build a classifier model to put on top of the convolutional model
top_model = Sequential()
top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))
# note that it is necessary to start with a fully-trained
# classifier, including the top classifier,
# in order to successfully do fine-tuning
top_model.load_weights(top_model_weights_path)
# add the model on top of the convolutional base
# model.add(top_model)
model = Model(inputs=base_model.input, outputs=top_model(base_model.output))
# set the first 25 layers (up to the last conv block)
# to non-trainable - weights will not be updated
for layer in model.layers[:25]:
layer.trainable=False
# compile the model with a SGD/momentum optimizer
# and a very slow learning rate
model.compile(loss='binary_crossentropy',
optimizer = optimizers.SGD(lr=0.00001, momentum=0.9), # reduced learning rate by 1/10
metrics=['accuracy'])
# prepare data augmentation configuration
train_datagen = ImageDataGenerator(rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator= train_datagen.flow_from_directory(train_data_dir,
target_size=(img_height, img_width),
batch_size=8,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(validation_data_dir,
target_size=(img_height, img_width),
batch_size=8,
class_mode='binary')
checkpoint = ModelCheckpoint(fine_tuned_model_path, monitor='val_acc',
verbose=1, save_best_only=True,
save_weights_only=False, mode='auto')
# fine-tune the model
fit = model.fit_generator(train_generator,
samples_per_epoch=nb_training_samples,
nb_epoch=nb_epoch,
validation_data=validation_generator,
nb_val_samples=nb_validation_samples,
verbose=1,
callbacks=[checkpoint])
with open(location+'/ft_history.txt', 'w') as f:
json.dump(fit.history, f)
return model, fit.history
from car-damage-detective.
Related Issues (17)
- Im not able to access the web app? HOT 1
- Python version?
- Unable to access data from google drive https://goo.gl/ZXBaQa
- Directory flow explanation
- Value error
- No d1_ft_model.h5 File
- Project Setup instructions needed. HOT 1
- how to run? HOT 1
- cant find bottleneck features.npy ( both training & validation)
- models are not available in static folder?
- NameError: global name 'Sequential' is not defined HOT 1
- IOError: [Errno 21] Is a directory: 'data0/.ipynb_checkpoints'
- some functions are missing HOT 8
- Can you explain the execution flow once please.
- Fine tune accuracy HOT 1
- Labels for datataset.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from car-damage-detective.