def load_resnet152_notop_from_caffemodel(load_path='models/'): prototxt = 'train_resnet152_places365.prototxt' caffemodel = 'resnet152_places365.caffemodel' debug = False # display input shape etc. and saved to 'debug.prototxt' if True # Note this is old style ResNet 152 with ReLU on the main path such that gradient may explode/vanish with Timer("Converting caffe model .."): # 141 secs model_loaded_from_caffe = convert.caffe_to_keras(load_path + prototxt, load_path + caffemodel, debug=debug) # cut the top layer before average pooling model_loaded_from_caffe.layers.pop() # drop 'prob' Activation layer model_loaded_from_caffe.layers.pop() # drop 'fc365' Dense layer model_loaded_from_caffe.layers.pop() # drop 'fc365_flatten' Flatten layer model_loaded_from_caffe.layers.pop( ) # drop 'pool5' Average Pooling layer, sub with rr pooling # model.output is still: Softmax.0, revise by model_loaded_from_caffe.outputs = [ model_loaded_from_caffe.layers[-1].output ] model_loaded_from_caffe.output_layers = [ model_loaded_from_caffe.layers[-1] ] model_loaded_from_caffe.layers[-1].outbound_nodes = [] return model_loaded_from_caffe
def load_vgg16_notop_from_caffemodel(load_path='models/'): prototxt = 'train_vgg16_places365.prototxt' caffemodel = 'vgg16_places365.caffemodel' debug = False # display input shape etc. and saved to 'debug.prototxt' if True with Timer("Converting caffe model .."): # 151 secs model_loaded_from_caffe = convert.caffe_to_keras(load_path + prototxt, load_path + caffemodel, debug=debug) # prepare a vgg model_loaded_from_caffe without top layers img_width = 224 img_height = 224 img_size = (3, img_width, img_height) input_tensor = Input(batch_shape=(None, ) + img_size) model_vgg_places365_notop = vgg16.VGG16(input_tensor=input_tensor, include_top=False) model_vgg_places365_notop.get_layer('block1_conv1')\ .set_weights(model_loaded_from_caffe.get_layer('conv1_1').get_weights()) model_vgg_places365_notop.get_layer('block1_conv2') \ .set_weights(model_loaded_from_caffe.get_layer('conv1_2').get_weights()) model_vgg_places365_notop.get_layer('block2_conv1') \ .set_weights(model_loaded_from_caffe.get_layer('conv2_1').get_weights()) model_vgg_places365_notop.get_layer('block2_conv2') \ .set_weights(model_loaded_from_caffe.get_layer('conv2_2').get_weights()) model_vgg_places365_notop.get_layer('block3_conv1') \ .set_weights(model_loaded_from_caffe.get_layer('conv3_1').get_weights()) model_vgg_places365_notop.get_layer('block3_conv2') \ .set_weights(model_loaded_from_caffe.get_layer('conv3_2').get_weights()) model_vgg_places365_notop.get_layer('block3_conv3') \ .set_weights(model_loaded_from_caffe.get_layer('conv3_3').get_weights()) model_vgg_places365_notop.get_layer('block4_conv1') \ .set_weights(model_loaded_from_caffe.get_layer('conv4_1').get_weights()) model_vgg_places365_notop.get_layer('block4_conv2') \ .set_weights(model_loaded_from_caffe.get_layer('conv4_2').get_weights()) model_vgg_places365_notop.get_layer('block4_conv3') \ .set_weights(model_loaded_from_caffe.get_layer('conv4_3').get_weights()) model_vgg_places365_notop.get_layer('block5_conv1') \ .set_weights(model_loaded_from_caffe.get_layer('conv5_1').get_weights()) model_vgg_places365_notop.get_layer('block5_conv2') \ .set_weights(model_loaded_from_caffe.get_layer('conv5_2').get_weights()) model_vgg_places365_notop.get_layer('block5_conv3') \ .set_weights(model_loaded_from_caffe.get_layer('conv5_3').get_weights()) # TODO rename the model # model_vgg_places365_notop.get_config().__setitem__('name', 'vgg_places365_model') return model_vgg_places365_notop
def test_convertGoogleNet(): load_path = 'keras/caffe/models' store_path = 'keras/caffe/models' prototxt = 'train_val_for_keras.prototxt' caffemodel = 'bvlc_googlenet.caffemodel' # Convert model from caffe to keras model = convert.caffe_to_keras(load_path+'/'+prototxt, load_path+'/'+caffemodel, debug=False) assert(model.__class__.__name__ == 'Model') # Save converted model structure json_string = model.to_json() open(store_path + '/Keras_model_structure.json', 'w').write(json_string) # Save converted model weights model.save_weights(store_path + '/Keras_model_weights.h5', overwrite=True)
def test_convertGoogleNet(): load_path = "keras/caffe/models" store_path = "keras/caffe/models" prototxt = "train_val_for_keras.prototxt" caffemodel = "bvlc_googlenet.caffemodel" # Convert model from caffe to keras model = convert.caffe_to_keras(load_path + "/" + prototxt, load_path + "/" + caffemodel, debug=False) assert model.__class__.__name__ == "Graph" # Save converted model structure json_string = model.to_json() open(store_path + "/Keras_model_structure.json", "w").write(json_string) # Save converted model weights model.save_weights(store_path + "/Keras_model_weights.h5", overwrite=True)
def load_vgg16_notop_from_caffemodel(): prototxt = 'Caffe/prototxt/train_vgg16_places365_wtop.prototxt' caffemodel = 'Caffe/weights/vgg16_places365_wtop.caffemodel' model_loaded_from_caffe = convert.caffe_to_keras(prototxt, caffemodel, debug=False) # prepare a vgg model_loaded_from_caffe without top layers img_width = 224 img_height = 224 img_size = (3, img_width, img_height) input_tensor = Input(batch_shape=(None, ) + img_size) model_vgg16_places365_notop = vgg16.VGG16(input_tensor=input_tensor, include_top=False) model_vgg16_places365_notop.get_layer('block1_conv1')\ .set_weights(model_loaded_from_caffe.get_layer('conv1_1').get_weights()) model_vgg16_places365_notop.get_layer('block1_conv2') \ .set_weights(model_loaded_from_caffe.get_layer('conv1_2').get_weights()) model_vgg16_places365_notop.get_layer('block2_conv1') \ .set_weights(model_loaded_from_caffe.get_layer('conv2_1').get_weights()) model_vgg16_places365_notop.get_layer('block2_conv2') \ .set_weights(model_loaded_from_caffe.get_layer('conv2_2').get_weights()) model_vgg16_places365_notop.get_layer('block3_conv1') \ .set_weights(model_loaded_from_caffe.get_layer('conv3_1').get_weights()) model_vgg16_places365_notop.get_layer('block3_conv2') \ .set_weights(model_loaded_from_caffe.get_layer('conv3_2').get_weights()) model_vgg16_places365_notop.get_layer('block3_conv3') \ .set_weights(model_loaded_from_caffe.get_layer('conv3_3').get_weights()) model_vgg16_places365_notop.get_layer('block4_conv1') \ .set_weights(model_loaded_from_caffe.get_layer('conv4_1').get_weights()) model_vgg16_places365_notop.get_layer('block4_conv2') \ .set_weights(model_loaded_from_caffe.get_layer('conv4_2').get_weights()) model_vgg16_places365_notop.get_layer('block4_conv3') \ .set_weights(model_loaded_from_caffe.get_layer('conv4_3').get_weights()) model_vgg16_places365_notop.get_layer('block5_conv1') \ .set_weights(model_loaded_from_caffe.get_layer('conv5_1').get_weights()) model_vgg16_places365_notop.get_layer('block5_conv2') \ .set_weights(model_loaded_from_caffe.get_layer('conv5_2').get_weights()) model_vgg16_places365_notop.get_layer('block5_conv3') \ .set_weights(model_loaded_from_caffe.get_layer('conv5_3').get_weights()) # rename the model model_vgg16_places365_notop.name = 'vgg16_notop_places365_model' return model_vgg16_places365_notop
def test_convertGoogleNet(): load_path = 'keras/caffe/models' store_path = 'keras/caffe/models' prototxt = 'train_val_for_keras.prototxt' caffemodel = 'bvlc_googlenet.caffemodel' # Convert model from caffe to keras model = convert.caffe_to_keras(load_path + '/' + prototxt, load_path + '/' + caffemodel, debug=False) assert (model.__class__.__name__ == 'Model') # Save converted model structure json_string = model.to_json() open(store_path + '/Keras_model_structure.json', 'w').write(json_string) # Save converted model weights model.save_weights(store_path + '/Keras_model_weights.h5', overwrite=True)
def main(args): if(not args.store_path): store_path = args.load_path else: store_path = args.store_path print("Converting model...") model = convert.caffe_to_keras(args.load_path+'/'+args.prototxt, args.load_path+'/'+args.caffemodel, debug=args.debug) print("Finished converting model.") # Save converted model structure print("Storing model...") json_string = model.to_json() open(store_path + '/Keras_model_structure.json', 'w').write(json_string) # Save converted model weights model.save_weights(store_path + '/Keras_model_weights.h5', overwrite=True) print("Finished storing the converted model to "+ store_path)
def main(args): if (not args.store_path): store_path = args.load_path else: store_path = args.store_path print("Converting model...") model = convert.caffe_to_keras(args.load_path + '/' + args.prototxt, args.load_path + '/' + args.caffemodel, debug=args.debug) print("Finished converting model.") # Save converted model structure print("Storing model...") json_string = model.to_json() open(store_path + '/Keras_model_structure.json', 'w').write(json_string) # Save converted model weights model.save_weights(store_path + '/Keras_model_weights.h5', overwrite=True) print("Finished storing the converted model to " + store_path)
path + "/deploy.prototxt"), "Err. Couldn't find the debug.prototxt file" assert os.path.exists( path + "/horse.png"), "Err. Couldn't find the horse.png image file" if not os.path.exists(path + "/fcn8s-heavy-pascal.caffemodel"): call([ "wget http://dl.caffe.berkeleyvision.org/fcn8s-heavy-pascal.caffemodel -O " + "./" + path + "/fcn8s-heavy-pascal.caffemodel" ], shell=True) assert os.path.exists( path + "/fcn8s-heavy-pascal.caffemodel"), "Err. Cannot find .caffemodel file. \ please download file using command : wget http://dl.caffe.berkeleyvision.org/fcn8s-heavy-pascal.caffemodel " model = convert.caffe_to_keras(path + "/deploy.prototxt", path + "/fcn8s-heavy-pascal.caffemodel", debug=1) print "Yay!" # 1. load image img = misc.imread(path + "./horse.png") # modify it img = np.rollaxis(img, 2) img = np.expand_dims(img, 0) # 2. run forward pass op = model.predict(img) # 3. reshape output