Giter VIP home page Giter VIP logo

convnets-keras's Introduction

NOTE: This repo is outdated and no longer updated.

  • Keras now provides an easy way to load pre-trained models
  • A notebook describing how to build fully convolutional networks and heatmaps is available here
  • A rendered version with visualisations is available here

convnets-keras

This repo is regrouping some of of the most used CNN, pre-trained on the ImageNet Dataset, all of them implemented in Keras framework :

We also propose a heatmap option, which allow to detect the location of an object from a given synset.

Here, we detect all the objects linked to the synsets cars, and we produce a heatmap :

Install

The only dependencies are h5py, Theano and Keras. Run the following commands

pip install --user cython h5py
pip install --user git+https://github.com/Theano/Theano.git
pip install --user git+https://github.com/fchollet/keras.git

Then, you need to install the convnetskeras module :

git clone https://github.com/heuritech/convnets-keras.git
cd convnets-keras
sudo python setup.py install

Get the weights of the pre-trained networks

The weights can be found here :

How to use the convnets

BEWARE !! : Since the networks have been trained in different settings, the preprocessing is different for the differents networks :

  • For the AlexNet, the images (for the mode without the heatmap) have to be of shape (227,227). It is recommended to resize the images with a size of (256,256), and then do a crop of size (227,227). The colors are in RGB order.
from keras.optimizers import SGD
from convnetskeras.convnets import preprocess_image_batch, convnet

im = preprocess_image_batch(['examples/dog.jpg'],img_size=(256,256), crop_size=(227,227), color_mode="rgb")

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model = convnet('alexnet',weights_path="weights/alexnet_weights.h5", heatmap=False)
model.compile(optimizer=sgd, loss='mse')

out = model.predict(im)
  • For the VGG, the images (for the mode without the heatmap) have to be of shape (224,224). It is recommended to resize the images with a size of (256,256), and then do a crop of size (224,224). The colors are in BGR order.
from keras.optimizers import SGD
from convnetskeras.convnets import preprocess_image_batch, convnet

im = preprocess_image_batch(['examples/dog.jpg'],img_size=(256,256), crop_size=(224,224), color_mode="bgr")

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
## For the VGG16, use this command
model = convnet('vgg_16',weights_path="weights/vgg16_weights.h5", heatmap=False)
## For the VGG19, use this one instead
# model = convnet('vgg_19',weights_path="weights/vgg19_weights.h5", heatmap=False)
model.compile(optimizer=sgd, loss='mse')

out = model.predict(im)

Performances on ImageNet

The errors are tested on ImageNet validation set. The prediction time is computed on a GeForce GTX TITAN X, with a Theano backend, and a batch size of 64.

AlexNet has lower results than the two VGGs, but it is much more lighter and faster, so it can easily be run on a small GPU (like on AWS), or even on a CPU.

Networks                            | AlexNet     |     VGG16   |     VGG19   |
-------------------------------------------------------------------------------
Top 1 Error                         |   42,94%    |   32,93%    |   32,77%    |
Top 5 error                         |   20,09%    |   12,39%    |   12,17%    |
Top 10 error                        |   13,84%    |    7,77%    |    7,80%    |
Number of params                    |     61M     |     138M    |     144M    |
Prediction time, batch of 64 (GPU)  |   0.4101s   |   0.9645s   |   1.0370s   |
Prediction time, single image (CPU) |   0.6773s   |   1.3353s   |   1.5722s   |

How to use the heatmap

The heatmap are produced by converting the model into a fully convolutionize model. The fully connected layers are transformed into convolution layers (by using the same weights), so we are able to compute the output of the network on each sub-frame of size (227,227) (or (224,224)) of a bigger picture. This produces a heatmap for each label of the classifier.

Using the heatmap is almost the same thing than directly classify. We suppose that we want the heatmap of the all the synsets linked with dogs, which are all the children in Wordnet of the synset "n02084071" (see next section to know how to find how we can get all the labels linked with a given synset) :

from keras.optimizers import SGD
from convnetskeras.convnets import preprocess_image_batch, convnet
from convnetskeras.imagenet_tool import synset_to_dfs_ids

im = preprocess_image_batch(['examples/dog.jpg'], color_mode="bgr")

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model = convnet('alexnet',weights_path="weights/alexnet_weights.h5", heatmap=True)
model.compile(optimizer=sgd, loss='mse')

out = model.predict(im)

s = "n02084071"
ids = synset_to_dfs_ids(s)
heatmap = out[0,ids].sum(axis=0)

# Then, we can get the image
import matplotlib.pyplot as plt
plt.imsave("heatmap_dog.png",heatmap)

Useful functions for ImageNet

We propose a few utils function to link the index returned by the networks, and the synsets of ImageNet.

Converting synsets to ids

It can be usefull to use the ids of ImageNet (which can be found on this page , if you want to know the meaning of the classification. We have two functions : id_to_synset and synset_to_id

  • id_to_synset is taking an id of the output of the networks, and returning the WordNet synset
>>> from convnetskeras.imagenet_tool import id_to_synset
>>> id_to_synset(243)
'n03793489'
  • `synset_to_id is doing the inverse operation

Getting all the children of a synset

If you want to detect all cars, you might need to have a classification of higher level than the one given by the wordnets of ImageNet. Indeed, a lot of different synsets are present for different kinds of cars. We can then choose a synset in the tree, and select all the ids of its children :

>>>synset_to_dfs_ids("n04576211")
[670, 870, 880, 444, 671, 565, 705, 428, 791, 561, 757, 829, 866, 847, 547, 820, 408, 573, 575, 803, 407, 436, 468, 511, 609, 627, 656, 661, 751, 817, 665, 555, 569, 717, 864, 867, 675, 734, 656, 586, 847, 802, 660, 603, 612, 690]

Credits

  • For the AlexNet network, we have adapted the weights that can be found here : Taylor, Graham; Ding, Weiguang, 2015-03, "Theano-based large-scale visual recognition with multiple GPUs", hdl:10864/10911 University of Guelph Research Data Repository

  • For the VGG networks, we have adapted the code released by baraldilorenzo here : https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3 We changed it to have the "heatmap" option, and we modified the weights in the same way.

convnets-keras's People

Contributors

charlesollion avatar dvro avatar eloiz avatar ivolima avatar leonardblier avatar mohamed-ezz avatar ylogx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

convnets-keras's Issues

Using AlexNet on a different dataset

Hi,

I'm wondering if and how it would be possible to modify this AlexNet convnet to output to different results (i.e. how to change the last layer output from 1000 to an arbitrary number, and train it from this starting point to a new training data set)

Thanks,

J

Save Weight

This code runs perfect. Then I want to save weight by using
checkpointer = ModelCheckpoint(filepath = os.path.join(path,'weights.{epoch:03d}.h5'), verbose = 1, save_best_only = True)
and
model.fit_generator(train_generator, verbose=1, samples_per_epoch=train_generator.nb_sample, nb_epoch=nb_epoch, validation_data=val_generator, nb_val_samples=val_generator.nb_sample, callbacks=[checkpointer])

It has an error below.
I using python 2.7 on Windows 10. However, it is no problem when using Linux.
File "C:/Users/BI12/OneDrive/Documents/Spider/MyModel.py", line 156, in <module> callbacks=[checkpointer])
File "C:\Users\BI12\Anaconda2\lib\site-packages\keras\engine\training.py", line 1485, in fit_generator callbacks.on_epoch_end(epoch, epoch_logs)
File "C:\Users\BI12\Anaconda2\lib\site-packages\keras\callbacks.py", line 40, in on_epoch_end callback.on_epoch_end(epoch, logs)
File "C:\Users\BI12\Anaconda2\lib\site-packages\keras\callbacks.py", line 296, in on_epoch_end self.model.save(filepath, overwrite=True)
File "C:\Users\BI12\Anaconda2\lib\site-packages\keras\engine\topology.py", line 2423, in save save_model(self, filepath, overwrite)
File "C:\Users\BI12\Anaconda2\lib\site-packages\keras\models.py", line 52, in save_model 'config': model.get_config()
File "C:\Users\BI12\Anaconda2\lib\site-packages\keras\engine\topology.py", line 2285, in get_config layer_config = layer.get_config()
File "C:\Users\BI12\Anaconda2\lib\site-packages\keras\layers\core.py", line 560, in get_config function = func_dump(self.function)
File "C:\Users\BI12\Anaconda2\lib\site-packages\keras\utils\generic_utils.py", line 40, in func_dump code = marshal.dumps(func.__code__).decode('raw_unicode_escape')
UnicodeDecodeError: 'rawunicodeescape' codec can't decode bytes in position 524-525: truncated \uXXXX

preprocess_image_batch function error- TypeError: imread() got an unexpected keyword argument 'mode'

I have installed your library and all other necessary dependencies in my local system. I have used your example image dog.jpg for initial testing. But when I tried this

from keras.optimizers import SGD
from convnetskeras.convnets import preprocess_image_batch, convnet

im = preprocess_image_batch(['/media/disk_1/CNN/keras/dog.jpg'],img_size=(256,256), crop_size=(227,227), color_mode="rgb")

when prepossessing the image using prepocess_image_batch() this code gives out this error

TypeError: imread() got an unexpected keyword argument 'mode'

Heatmap has low resolution (43x73 in the dog example)

Hi, thank you for those networks, they're already really useful.

But I have a minor problem. While running the example with the dog, I noticed the heatmap has a very low resolution (43x73). This is the case also with other images (the resultion differ but is still very low) and I tried with the vgg16 and I have the same issue.

But the heatmap works and finds the dog.

Here is the the code i'm using:

if __name__ == "__main__":
    ### Here is a script to compute the heatmap of the dog synsets.
    ## We find the synsets corresponding to dogs on ImageNet website
    s = "n02084071"
    ids = synset_to_dfs_ids(s)
    # Most of the synsets are not in the subset of the synsets used in ImageNet recognition task.
    ids = np.array([id_ for id_ in ids if id_ is not None])

    im = preprocess_image_batch(['examples/dog.jpg'], color_mode="rgb")

    # Test pretrained model
    sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
    model = convnet('alexnet',weights_path="weights/alexnet_weights.h5", heatmap=True)
    model.compile(optimizer=sgd, loss='mse')


    out = model.predict(im)
    heatmap = out[0,ids,:,:].sum(axis=0)
    
    print(np.shape(heatmap))
    
    import matplotlib.pyplot as plt
    plt.imsave("heatmap_dog.png",heatmap)

the result:

(43, 73)

heatmap_dog

Thank you very much for your help.

Heatmap problem VGG

Hi,
The heatmap works fine for me for the example provided (see below), but it does not work if you change weights to VGG16 or VGG19. The heatmaps produced by VGG are blank and with strange coordinates.
im = preprocess_image_batch(['examples/dog.jpg'], color_mode="bgr")

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model = convnet('alexnet',weights_path="weights/alexnet_weights.h5", heatmap=True)
model.compile(optimizer=sgd, loss='mse')
out = model.predict(im)

s = "n02084071"
ids = synset_to_dfs_ids(s)
heatmap = out[0,ids].sum(axis=0)
plt.imshow(heatmap)

"negative dimensions are not allowed"

Since the last update of Keras, the code seems no longer to work properly.
I get an error trying the example code:

`from keras.optimizers import SGD
from convnetskeras.convnets import preprocess_image_batch, convnet

im = preprocess_image_batch(['examples/dog.jpg'], img_size=(256,256), crop_size=(227,227), color_mode="rgb")

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model = convnet('alexnet',weights_path="weights/alexnet_weights.h5", heatmap=False)
model.compile(optimizer=sgd, loss='mse')

out = model.predict(im)`

Full error log:
Using Theano backend. Traceback (most recent call last): File "D:/Meine Daten/Dropbox/studium/BSc Psychologie in IT/Bachelor Thesis/03 Software/burkhardt/convnets/test.py", line 7, in <module> model = convnet('alexnet',weights_path="weights/alexnet_weights.h5", heatmap=False) File "C:\Users\Michael\Anaconda3\lib\site-packages\convnetskeras\convnets.py", line 65, in convnet convnet = convnet_init(weights_path, heatmap=False) File "C:\Users\Michael\Anaconda3\lib\site-packages\convnetskeras\convnets.py", line 265, in AlexNet dense_1 = Dense(4096, activation='relu',name='dense_1')(dense_1) File "C:\Users\Michael\AppData\Roaming\Python\Python34\site-packages\keras\engine\topology.py", line 487, in __call__ self.build(input_shapes[0]) File "C:\Users\Michael\AppData\Roaming\Python\Python34\site-packages\keras\layers\core.py", line 695, in build name='{}_W'.format(self.name)) File "C:\Users\Michael\AppData\Roaming\Python\Python34\site-packages\keras\initializations.py", line 59, in glorot_uniform return uniform(shape, s, name=name) File "C:\Users\Michael\AppData\Roaming\Python\Python34\site-packages\keras\initializations.py", line 32, in uniform return K.random_uniform_variable(shape, -scale, scale, name=name) File "C:\Users\Michael\AppData\Roaming\Python\Python34\site-packages\keras\backend\theano_backend.py", line 140, in random_uniform_variable return variable(np.random.uniform(low=low, high=high, size=shape), File "mtrand.pyx", line 1568, in mtrand.RandomState.uniform (numpy\random\mtrand\mtrand.c:17350) File "mtrand.pyx", line 234, in mtrand.cont2_array_sc (numpy\random\mtrand\mtrand.c:3092) ValueError: negative dimensions are not allowed

As far as I could debug, the error occurs in the method convnet() and there in the method AlexNet and there in the dense_1 line. But I am not able to fix the error.

xrange error

Hi, I am trying your code with python 3. It complains about xrange() in convnets.py. The problem disappears when I change it with range(). May I suggest changing it to range() for good?

Import Error

Hi, I tried your code of transfer Alexnet. But it showed the following error:

File "D:\KERAS\convnets-keras\convnetskeras\customlayers.py", line 2, in
from keras.layers.core import Lambda, Merge

ImportError: cannot import name 'Merge'

Please help me to debug this.

Bug in splittensor

def splittensor(axis=1, ratio_split=1, id_split=0,**kwargs):
def f(X):
div = X.shape[axis] // ratio_split

    if axis == 0:
        output =  X[id_split*div:(id_split+1)*div,:,:,:]
    elif axis == 1:
        output =  X[:, id_split*div:(id_split+1)*div, :, :]
    elif axis == 2:
        output = X[:,:,id_split*div:(id_split+1)*div,:]
    elif axis == 3:
        output == X[:,:,:,id_split*div:(id_split+1)*div]
    else:
        raise ValueError("This axis is not possible")

    return output

I think that output == X[:,:,:,id_split_div:(id_split+1)_div] would be output = X[:,:,:,id_split_div:(id_split+1)_div]

Define AlexNet as sequential model

Hello,

I am trying to remove the last two layers of AlexNet and add some custom layers instead, but I am unable to do that because it's not an instance of Sequential(). Is there any reason for this?

Would it be possible to define AlexNet as Sequential()? Or, in case it's not, is there anyway I can replace these layers with custom ones? (Note that I want to replace these layers, not just add new ones).

Thanks

Are you interested in doubling the heatmap resolution? (4x more pixels)

Hi, i'm fairly new in github so it's the only place I found to ask this.
While I was working on a kaggle competition, I had to detect fishes in a image.
This lead me to try to increase the resultion of the heatmaps from this repository.
I managed to do it by changing the last pooling layer (stride of (1,1) instead of (2,2)) and also the next convolution layer accordingly. (I add zeros and doubled the size of the convolution to match the original architecture, as if it was a sliding window).
I works pretty well right now.
I was wondering if you were interested by this code if I were to take the time to make a proper pull request.
Thank you very much.

Softmax2D computes wrong output shape

In the Softmax2D layer below, the forward pass does not change the output shape, but get_output_shape_for does. It gave me an error using Tensorflow backend. I had to change get_output_shape_for to an identity function (just returning input_shape with no change)

def call(self, x,mask=None):
    e = K.exp(x - K.max(x, axis=self.axis, keepdims=True))
    s = K.sum(e, axis=self.axis, keepdims=True)
    return e / s

def get_output_shape_for(self, input_shape):
    axis_index = self.axis % len(input_shape)
    return tuple([input_shape[i] for i in range(len(input_shape)) \
                  if i != axis_index ])

error: Load weight file (ValueError: You are trying to load a weight file containing 11 layers into a model with 7 layers.)

Traceback (most recent call last):

File "", line 1, in
runfile('E:/Work_Space/zc_keras/workspace/test.py', wdir='E:/Work_Space/zc_keras/workspace')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "E:/Work_Space/zc_keras/workspace/test.py", line 24, in
model = convnet('alexnet',weights_path="alexnet_weights.h5", heatmap=False)

File "C:\ProgramData\Anaconda3\lib\site-packages\convnetskeras\convnets.py", line 69, in convnet
convnet = convnet_init(weights_path, heatmap=False)

File "C:\ProgramData\Anaconda3\lib\site-packages\convnetskeras\convnets.py", line 295, in AlexNet
model.load_weights(weights_path)

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 1180, in load_weights
f, self.layers, reshape=reshape)

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\saving.py", line 901, in load_weights_from_hdf5_group
str(len(filtered_layers)) + ' layers.')

ValueError: You are trying to load a weight file containing 11 layers into a model with 7 layers.

License

Can you please add a license?

I cannot reproduce your results.

I have not been able to reproduce your results with the given example code. The heatmap probabilities and label confidence seems way too low. What were the versions and settings of Keras and Theano?

This is what I get on Keras 1.2.2 and Theano 0.9.0 by running the below script:

from keras.optimizers import SGD
from convnetskeras.convnets import preprocess_image_batch, convnet
from convnetskeras.imagenet_tool import pprint_output
from convnetskeras.imagenet_tool import synset_to_dfs_ids

## AlexNet
im = preprocess_image_batch(['examples/dog.jpg'],img_size=(256,256), crop_size=(227,227), color_mode="rgb")

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)

## AlexNet
model = convnet('alexnet',weights_path="weights/alexnet_weights.h5", heatmap=False)

model.compile(optimizer=sgd, loss='mse')

out = model.predict(im)

# Image Classification
pprint_output(out[0])

My keras.json:

{
    "floatx": "float32",
    "image_dim_ordering": "th",
    "backend": "theano",
    "epsilon": 1e-07
}

Output:

10.33 : dingo, warrigal, warragal, Canis dingo
3.79 : cougar, puma, catamount, mountain lion, painter, panther, Felis concolor
3.63 : grey fox, gray fox, Urocyon cinereoargenteus
3.05 : red wolf, maned wolf, Canis rufus, Canis niger
2.98 : kit fox, Vulpes macrotis
2.95 : Eskimo dog, husky
2.65 : Siberian husky
2.64 : coyote, prairie wolf, brush wolf, Canis latrans
2.59 : timber wolf, grey wolf, gray wolf, Canis lupus
2.58 : red fox, Vulpes vulpes

Heatmap:
Heatmap

Input Dimension Mismatch

Hello all,

I am getting a consistent error when attempting to train or predict with the verbatim AlexNet model. Using the example from the readme (preprocess dog.jpg, initialize model and SGD, compile with mse loss, predict), I receive the following error:

ValueError: GpuElemwise. Input dimension mis-match. Input 3 (indices start at 0) has shape[1] == 95, but the output's size on that axis is 96.
Apply node that caused the error: GpuElemwise{Composite{(i0 + (i1 * i2) + (i1 * i3) + (i1 * i4) + (i1 * i5) + (i1 * i6))},no_inplace}(CudaNdarrayConstant{[[[[ 2.]]]]}, CudaNdarrayConstant{[[[[ 9.99999975e-05]]]]}, GpuDimShuffle{0,3,1,2}.0, GpuSubtensor{::, int64:int64:}.0, GpuSubtensor{::, int64:int64:}.0, GpuSubtensor{::, int64:int64:}.0, GpuSubtensor{::, int64:int64:}.0)
Toposort index: 156
Inputs types: [CudaNdarrayType(float32, (True, True, True, True)), CudaNdarrayType(float32, (True, True, True, True)), CudaNdarrayType(float32, 4D), CudaNdarrayType(float32, 4D), CudaNdarrayType(float32, 4D), CudaNdarrayType(float32, 4D), CudaNdarrayType(float32, 4D)]
Inputs shapes: [(1, 1, 1, 1), (1, 1, 1, 1), (1, 96, 27, 31), (1, 95, 27, 31), (1, 94, 27, 31), (1, 93, 27, 31), (1, 92, 27, 31)]
Inputs strides: [(0, 0, 0, 0), (0, 0, 0, 0), (0, 1, 2976, 96), (0, 1, 2976, 96), (0, 1, 2976, 96), (0, 1, 2976, 96), (0, 1, 2976, 96)]
Inputs values: [CudaNdarray([[[[ 2.]]]]), CudaNdarray([[[[ 9.99999975e-05]]]]), 'not shown', 'not shown', 'not shown', 'not shown', 'not shown']
Outputs clients: [[GpuElemwise{Composite{(i0 / (i1 ** i2))}}[(0, 0)](GpuDnnPool{mode='max'}.0, GpuElemwise{Composite{(i0 + (i1 * i2) + (i1 * i3) + (i1 * i4) + (i1 * i5) + (i1 * i6))},no_inplace}.0, CudaNdarrayConstant{[[[[ 0.75]]]]})]]

Does anyone else receive the same error? Based on the number 96 showing up, I wonder if it might have something to do with the output of conv1. Thanks in advance for any help.

Color order in preprocessing_image_batch

In the function preprocess_image_batch, the color normalization is done in RGB space (using mean vector [123.68, 116.779, 103.939]), but it occurs after the colors are permuted in BGR.

The normalization should be done before the color permutation.

Dimensions must be equal,

ValueError: Dimensions must be equal, but are 27 and 26 for 'add_2' (op: 'Add') with input shapes: [?,27,27,100], [?,26,27,100]

Traceback (most recent call last):

File "", line 1, in
runfile('C:/Users/Lab PC/Documents/PythonWorkspace/convnets-keras-master/test.py', wdir='C:/Users/Lab PC/Documents/PythonWorkspace/convnets-keras-master')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 678, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 106, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Lab PC/Documents/PythonWorkspace/convnets-keras-master/test.py", line 14, in
model = convnet('alexnet',weights_path="weights/alexnet_weights.h5", heatmap=False)

File "C:\Users\Lab PC\Documents\PythonWorkspace\convnets-keras-master\convnetskeras\convnets.py", line 83, in convnet
convnet = convnet_init(weights_path, heatmap=False)

File "C:\Users\Lab PC\Documents\PythonWorkspace\convnets-keras-master\convnetskeras\convnets.py", line 248, in AlexNet
conv_2 = crosschannelnormalization(name='convpool_1')(conv_2)

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\topology.py", line 569, in call
self.add_inbound_node(inbound_layers, node_indices, tensor_indices)

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\topology.py", line 632, in add_inbound_node
Node.create_node(self, inbound_layers, node_indices, tensor_indices)

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\topology.py", line 164, in create_node
output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\layers\core.py", line 596, in call
return self.function(x, **arguments)

File "C:\Users\Lab PC\Documents\PythonWorkspace\convnets-keras-master\convnetskeras\customlayers.py", line 24, in f
scale += alpha * extra_channels[:, i:i + ch, :, :]

File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\math_ops.py", line 979, in binary_op_wrapper
return func(x, y, name=name)

File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 296, in add
"Add", x=x, y=y, name=name)

File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)

File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3392, in create_op
op_def=op_def)

File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1734, in init
control_input_ops)

File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1570, in _create_c_op
raise ValueError(str(e))

ValueError: Dimensions must be equal, but are 27 and 26 for 'add_2' (op: 'Add') with input shapes: [?,27,27,100], [?,26,27,100].

How about overlapping pooling

Thank you for your kind upload
I tried your Alexnet, it seems nice

But how about the Overlapping pooling
In your code, maxpooling stride is (3, 3), I think is (2, 3) or something
Hope your reply

thank you a lot

Alexnet issue with Keras 2

The following line gives error in Keras 2.0.4

conv_2 = merge([Convolution2D(128, (5, 5), activation="relu", name='conv_2_' + str(i + 1))(
splittensor(ratio_split=2, id_split=i)(conv_2)
) for i in range(2)], mode='concat', concat_axis=1, name="conv_2")

Error:

ValueError: Input dimension mis-match. (input[0].shape[1] = 96, input[4].shape[1] = 95)
Apply node that caused the error: Elemwise{Composite{(i0 / ((i1 + (i2 * i3) + (i2 * i4) + (i2 * i5) + (i2 * i6) + (i2 * i7)) ** i8))}}[(0, 0)](Pool{ignore_border=True, mode='max', ndim=2}.0, TensorConstant{(1, 1, 1, 1) of 2.0}, TensorConstant{(1, 1, 1, .. of 0.0001}, InplaceDimShuffle{0,3,1,2}.0, Subtensor{::, int64:int64:}.0, Subtensor{::, int64:int64:}.0, Subtensor{::, int64:int64:}.0, Subtensor{::, int64:int64:}.0, TensorConstant{(1, 1, 1, 1) of 0.75})
Toposort index: 23
Inputs types: [TensorType(float32, 4D), TensorType(float32, (True, True, True, True)), TensorType(float32, (True, True, True, True)), TensorType(float32, 4D), TensorType(float32, 4D), TensorType(float32, 4D), TensorType(float32, 4D), TensorType(float32, 4D), TensorType(float32, (True, True, True, True))]
Inputs shapes: [(1, 96, 27, 27), (1, 1, 1, 1), (1, 1, 1, 1), (1, 96, 27, 29), (1, 95, 27, 29), (1, 94, 27, 29), (1, 93, 27, 29), (1, 92, 27, 29), (1, 1, 1, 1)]
Inputs strides: [(279936, 2916, 108, 4), (4, 4, 4, 4), (4, 4, 4, 4), (384, 4, 11136, 384), (384, 4, 11136, 384), (384, 4, 11136, 384), (384, 4, 11136, 384), (384, 4, 11136, 384), (4, 4, 4, 4)]
Inputs values: ['not shown', array([[[[ 2.]]]], dtype=float32), array([[[[ 9.99999975e-05]]]], dtype=float32), 'not shown', 'not shown', 'not shown', 'not shown', 'not shown', array([[[[ 0.75]]]], dtype=float32)]
Inputs type_num: [11, 11, 11, 11, 11, 11, 11, 11, 11]
Outputs clients: [[IncSubtensor{InplaceSet;::, ::, int64:int64:, int64:int64:}(Alloc.0, Elemwise{Composite{(i0 / ((i1 + (i2 * i3) + (i2 * i4) + (i2 * i5) + (i2 * i6) + (i2 * i7)) ** i8))}}[(0, 0)].0, Constant{2}, Constant{29}, Constant{2}, Constant{29})]]

Debugprint of the apply node:
Elemwise{Composite{(i0 / ((i1 + (i2 * i3) + (i2 * i4) + (i2 * i5) + (i2 * i6) + (i2 * i7)) ** i8))}}[(0, 0)] [id A] <TensorType(float32, 4D)> ''
|Pool{ignore_border=True, mode='max', ndim=2} [id B] <TensorType(float32, 4D)> ''
| |Elemwise{Composite{(i0 * ((i1 + i2) + Abs((i1 + i2))))}}[(0, 1)] [id C] <TensorType(float32, 4D)> ''
| | |TensorConstant{(1, 1, 1, 1) of 0.5} [id D] <TensorType(float32, (True, True, True, True))>
| | |CorrMM{valid, (4, 4), (1, 1)} [id E] <TensorType(float32, 4D)> ''
| | | |/input_1 [id F] <TensorType(float32, 4D)>
| | | |Subtensor{::, ::, ::int64, ::int64} [id G] <TensorType(float32, 4D)> ''
| | | |InplaceDimShuffle{3,2,0,1} [id H] <TensorType(float32, 4D)> ''
| | | | |conv_1/kernel [id I] <TensorType(float32, 4D)>
| | | |Constant{-1} [id J]
| | | |Constant{-1} [id J]
| | |InplaceDimShuffle{x,0,x,x} [id K] <TensorType(float32, (True, False, True, True))> ''
| | |conv_1/bias [id L] <TensorType(float32, vector)>
| |TensorConstant{(2,) of 3} [id M] <TensorType(int64, vector)>
| |TensorConstant{(2,) of 2} [id N] <TensorType(int64, vector)>
| |TensorConstant{(2,) of 0} [id O] <TensorType(int64, vector)>
|TensorConstant{(1, 1, 1, 1) of 2.0} [id P] <TensorType(float32, (True, True, True, True))>
|TensorConstant{(1, 1, 1, .. of 0.0001} [id Q] <TensorType(float32, (True, True, True, True))>
|InplaceDimShuffle{0,3,1,2} [id R] <TensorType(float32, 4D)> ''
| |IncSubtensor{InplaceSet;::, ::, int64:int64:, int64:int64:} [id S] <TensorType(float32, 4D)> ''
| |Alloc [id T] <TensorType(float32, 4D)> ''
| | |TensorConstant{0.0} [id U] <TensorType(float32, scalar)>
| | |Shape_i{0} [id V] <TensorType(int64, scalar)> ''
| | | |/input_1 [id F] <TensorType(float32, 4D)>
| | |TensorConstant{27} [id W] <TensorType(int64, scalar)>
| | |TensorConstant{29} [id X] <TensorType(int64, scalar)>
| | |TensorConstant{96} [id Y] <TensorType(int64, scalar)>
| |Elemwise{sqr,no_inplace} [id Z] <TensorType(float32, 4D)> ''
| | |InplaceDimShuffle{0,2,3,1} [id BA] <TensorType(float32, 4D)> ''
| | |Pool{ignore_border=True, mode='max', ndim=2} [id B] <TensorType(float32, 4D)> ''
| |Constant{0} [id BB]
| |Constant{27} [id BC]
| |Constant{0} [id BB]
| |Constant{96} [id BD]
|Subtensor{::, int64:int64:} [id BE] <TensorType(float32, 4D)> ''
| |InplaceDimShuffle{0,3,1,2} [id R] <TensorType(float32, 4D)> ''
| |Constant{1} [id BF]
| |Constant{97} [id BG]
|Subtensor{::, int64:int64:} [id BH] <TensorType(float32, 4D)> ''
| |InplaceDimShuffle{0,3,1,2} [id R] <TensorType(float32, 4D)> ''
| |Constant{2} [id BI]
| |Constant{98} [id BJ]
|Subtensor{::, int64:int64:} [id BK] <TensorType(float32, 4D)> ''
| |InplaceDimShuffle{0,3,1,2} [id R] <TensorType(float32, 4D)> ''
| |Constant{3} [id BL]
| |Constant{99} [id BM]
|Subtensor{::, int64:int64:} [id BN] <TensorType(float32, 4D)> ''
| |InplaceDimShuffle{0,3,1,2} [id R] <TensorType(float32, 4D)> ''
| |Constant{4} [id BO]
| |Constant{100} [id BP]
|TensorConstant{(1, 1, 1, 1) of 0.75} [id BQ] <TensorType(float32, (True, True, True, True))>

Transfer Learning with AlexNet

Hi,

First of all, many thanks for creating this library !

On training the alexnet architecture on a medical imaging dataset from scratch, I get ~90% accuracy. Now I am wanting to use the pre-trained weights and do finetuning. The problem I am facing is explained below -

  • While training alexnet from scratch, the only pre-processing I did was to scale the pixels by 255. So the pixel values belonged in [0,1]. The model converged beautifully while training.

  • While using the pre-trained weights, I've performed channelwise mean subtraction as specified in the code. However, the model fails to converge. My question is - Do I need to scale the pixels (by 255) after performing the mean subtraction?

It would be helpful if someone could explain the exact pre-processing steps that were carried out while training on the original images from imagenet.

why can't I reproduce your result?

I followed the code:

`
from keras.optimizers import SGD
from convnetskeras.convnets import preprocess_image_batch, convnet
from convnetskeras.imagenet_tool import synset_to_dfs_ids

im = preprocess_image_batch(['examples/dog.jpg'], color_mode="bgr")

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model = convnet('alexnet',weights_path="weights/alexnet_weights.h5", heatmap=True)
model.compile(optimizer=sgd, loss='mse')

out = model.predict(im)

s = "n02084071"
ids = synset_to_dfs_ids(s)
heatmap = out[0,ids].sum(axis=0)

import matplotlib.pyplot as plt
plt.imsave("heatmap_dog.png",heatmap)
`

and here is my keras.json:

{ "epsilon": 1e-07, "floatx": "float32", "image_data_format": "channels_first", "backend": "tensorflow" }

and the result:

heatmap_dog1

why? my result is size of 73x43, but yours is size of 74x44.

any suggestion?

Can't use vgg16 for heatmap

im = preprocess_image_batch(['examples/dog.jpg'],color_mode="bgr")

# Test pretrained model
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model = convnet('vgg_16', 'weights/vgg16_weights.h5', heatmap=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy')
out = model.predict(im)
heatmap = out[0,ids,:,:].sum(axis=0)


# Then, we can get the image
import matplotlib.pyplot as plt
plt.imsave("heatmap_dog.png",heatmap)

Using Theano backend.
Using gpu device 0: GeForce GTX 960 (CNMeM is enabled with initial size: 30.0% of memory, cuDNN not available)
Traceback (most recent call last):
File "convnets.py", line 334, in
model.compile(optimizer=sgd, loss='categorical_crossentropy')
File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 332, in compile
*_kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 584, in compile
sample_weight, mask)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 311, in weighted
score_array = fn(y_true, y_pred)
File "/usr/local/lib/python2.7/dist-packages/keras/objectives.py", line 36, in categorical_crossentropy
return K.categorical_crossentropy(y_pred, y_true)
File "/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py", line 728, in categorical_crossentropy
return T.nnet.categorical_crossentropy(output, target)
File "/usr/local/lib/python2.7/dist-packages/theano/tensor/nnet/nnet.py", line 2077, in categorical_crossentropy
return crossentropy_categorical_1hot(coding_dist, true_dist)
File "/usr/local/lib/python2.7/dist-packages/theano/gof/op.py", line 613, in call
node = self.make_node(_inputs, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/theano/tensor/nnet/nnet.py", line 1435, in make_node
raise TypeError('matrix required for argument: coding_dist')
TypeError: matrix required for argument: coding_dist

Script does not go further after import

My script chokes when I import convnets-keras:

from convnetskeras.convnets import preprocess_image_batch, convnet

There is no error-message or whatsoever. Any clues on how to solve this?

Update on the issue: It is currently working on my linux desktop, but the issue still exists if try to import convnets-keras on windows.

Share information about training

Hi,

I know you have abandoned that repo - I need a running version of AlexNet for baseline issues for my work and I have an implementation for keras2/tf available. However, when I train my models I achieve top-5-error rate 30.3% on ILSVRC2012 validation data, which is far off from the 20% you have published. Since I basically use your architecture with some minor modifications for keras 2 support I am wondering why that is. Could you share some information how you trained your models (optimizer, data augmentation etc.) so that I could train AlexNet by myself?
Thanks a lot!

Getting Label (classification) from predicted output (out)

Hello,

I have been trying to figure out how to extract the "name" of the object detected on the picture. I have come across the following issue:

When I run the simple Convnets code using the VGG_16 model with an image of a horse of size (4650x3260), I get an array "out(1, 1000)". I assumed that each index of the array represents an object (e.g. 85 = French Bulldog), and that each value of the array represents the probability of the image showing the class assigned to that index (e.g. 0.143~ 14.3% probability of Image containing a French Bulldog).

However, when I run the code and find out the highest value of the array "out", I get and index/id of 349. When I transform that index/id into synset, I get 'n02415577'. This all looked good until I checked the ImageNet classes here and found out that:
a) Index/ID 349 does not correspond to synset n02415577, and
b) neither the ID nor the synset are assigned to "horse" on ImageNet.

The code I have used to find out the Index ID is:

import numpy
max_index = numpy.argmax(out)

and to find the synset:

from convnetskeras.imagenet_tool import id_to_synset
wnid = id_to_synset(max_index)

Does anyone know why does it work fine for the given examples but performs so poorly with other images? Am I perhaps interpreting the prediction "out" completely wrong? Or does the image size play a significant role that could be causing problems?

Thanks

Problem retraining model with Softmax4D

I am trying to retrain the VGG16 for a specific set of classes.
It works with heatmap=False, (without Softmax4D), but I need to generate a heatmap.

Here is what I am doing:

  • Removing the last 2 layers
  • Freezing the remaining layers (trainable=False)
  • Adding new layers (3 classes)
    model.add(Convolution2D(3,1,1,name="dense_3"))
    model.add(Softmax4D(axis=1,name="softmax"))
  • Loading data and using preprocess_image_batch on images
  • Finally, training the network
model.fit(X_, y_, nb_epoch=10, batch_size=4)

However, I have a problem finding the adequate format for y_ (the classes).

I know that y_ must have 3 dimensions Exception: Error when checking model target: expected softmax to have 3 dimensions but not sure how to do that considering that my y_ currently has 1 dimension (values 0,1 and 2).

Even applying np_utils.to_categorical(y_, len(set(y_))), I still get only 2 dimensions.

Is the workflow correct? How can I solve this issue?

Thanks in advance,

Documentation bug in README.md

In the section 'How to use convnets', the first example explains how to use alexnet and the second example looks like it is supposed to explain how to use VGG, but duplicates the alexnet example with the exception of changing the color mode to bgr. I would expect the second example to specify 'vgg16' and load the vgg weights.

Unable to install h5py and convnets-keras

Hi Everyone,

I am unable to install the h5py and convnets-keras module using the following method, as given in the instruction page.
git clone https://github.com/heuritech/convnets-keras.git
cd convnets-keras
sudo python setup.py install

Where should I type this line: "git clone https://github.com/heuritech/convnets-keras.git"? In the python command prompt or the jupyter notebook? When I type this line in the python prompt, I find this error: 'git' is not recognized as an internal or external command, operable program or batch file.
whereas when I type it in jupyter notebook and run the kernel, I find "syntax error".

tensorflow backend

Hello,

It seems it does not work with the tensorflow backend. Is there any way to fix this?

vgg networks dont't produce heatmaps

Hi I've tried your code but the vgg networks don't actually produce heatmaps. Only alexnet does. Even with the flag heatmap=True the shape of the out from model.predict() on vgg networks is (1,1000, 1, 1) for the dog example that you have provided.

Load weights error in theano

I loaded the pretrained alexnet model in keras 2.06 and theano 0.9, got the error. Could anyone help me ?

The code :
model = convnet('alexnet',weights_path=".\model\alexnet_weights.h5", heatmap=False)

The error :

c:\DL\keras_test\a3rdparty\convnets_keras_master\convnetskeras\convnets.pyc in convnet(network, weights_path, heatmap, trainable)
63 elif network == 'alexnet':
64 convnet_init = AlexNet
---> 65 convnet = convnet_init(weights_path, heatmap=False)
66
67 if not heatmap:

c:\DL\keras_test\a3rdparty\convnets_keras_master\convnetskeras\convnets.pyc in AlexNet(weights_path, heatmap)
230
231 conv_2 = MaxPooling2D((3, 3), strides=(2,2))(conv_1)
--> 232 conv_2 = crosschannelnormalization(name="convpool_1")(conv_2)
233 conv_2 = ZeroPadding2D((2,2))(conv_2)
234 conv_2 = merge([

C:\Anaconda2\lib\site-packages\keras\engine\topology.pyc in call(self, inputs, **kwargs)
594
595 # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 596 output = self.call(inputs, **kwargs)
597 output_mask = self.compute_mask(inputs, previous_mask)
598

C:\Anaconda2\lib\site-packages\keras\layers\core.pyc in call(self, inputs, mask)
645 if has_arg(self.function, 'mask'):
646 arguments['mask'] = mask
--> 647 return self.function(inputs, **arguments)
648
649 def compute_mask(self, inputs, mask=None):

c:\DL\keras_test\a3rdparty\convnets_keras_master\convnetskeras\customlayers.pyc in f(X)
17 square = K.square(X)
18 extra_channels = K.spatial_2d_padding(K.permute_dimensions(square, (0,2,3,1))
---> 19 , (0,half))
20 extra_channels = K.permute_dimensions(extra_channels, (0,3,1,2))
21 scale = k

C:\Anaconda2\lib\site-packages\keras\backend\theano_backend.pyc in spatial_2d_padding(x, padding, data_format)
1036 """
1037 assert len(padding) == 2
-> 1038 assert len(padding[0]) == 2
1039 assert len(padding[1]) == 2
1040 top_pad, bottom_pad = padding[0]

TypeError: object of type 'int' has no len()

Evaluate pretrained model on validation and testing images using keras library

Sorry this is not an issue

Any idea of how once can evaluate convnets.py on all of the validation and testing images saved in my machine. I know in keras there is an evaluation function but I could not make it work.

im = preprocess_image_batch(['examples/dog.jpg'],img_size=(256,256), crop_size=(227,227), color_mode="rgb")
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model = convnet('alexnet',weights_path="weights/alexnet_weights.h5", heatmap=False)
model.compile(optimizer=sgd, loss='mse')
out = model.predict(im)

Now I want to evaluate the model on the validation image but I could not define x_test and y_test.

score = model.evaluate(x_test,y_test,verbose=0)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.