Shape mismatch in inner product layer Caffe - neural-network

I've trained a net and trying to apply it, but recieve following error:
Cannot copy param 0 weigths from layer 'ip1'; shape mismatch. Source
param shape is 384 72576 (27869184); target param shape is 384
224(86016). To learn this layer's parameters from scratch rather than
copying from a saved net, rename the layer.
Net config for this layer looks like this:
layer {
type: "Concat"
bottom: "conv5f"
bottom: "conv5_pf"
top: "feat"
name: "concat1"
}
layer {
name: "ip1"
type: "InnerProduct"
bottom: "feat"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 384
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
It is a joint place for Siamese network.
When I start training I recieve following output:
setting up concat
Top shape: 256 72576
...
Setting up ip1
Top shape: 256 384
Applying net:
setting up concat
Top shape: 256 224
...
Setting up ip1
Top shape: 256 384
I've used batch size of 256 while training, if it matters.
What is wrong here? I just can't see. I've copypasted my net from train.prototxt file to apply_net.prototxt, that's it

The mismatch is due to different blob sizes of the layers. You need to compute the output dimensions of each layer of your network. Use the formula given here.

Related

passing data from a dataset into a neural network

I am an absolute beginner in neural networks and I have problems passing data into the network.
This is the model:
model = keras.Sequential([keras.layers.Flatten(input_shape=(300,150,3)),keras.layers.Dense(128,activation='relu'),keras.layers.Dense(10,activation='softmax')])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
The data consists of images with labels as tuples. Its a mutli-label dataset, where each label, e.g. varroa_output, contains 1 if the characterisitic was present in the image and a 0 if it wasn't. This is the train_batch I tried to feed into the neural network:
<BatchDataset element_spec=(TensorSpec(shape=(None, 300, 150, 3), dtype=tf.uint8, name=None), {'cooling_output': TensorSpec(shape=(None,), dtype=tf.float64, name=None), 'pollen_output': TensorSpec(shape=(None,), dtype=tf.float64, name=None), 'varroa_output': TensorSpec(shape=(None,), dtype=tf.float64, name=None), 'wasps_output': TensorSpec(shape=(None,), dtype=tf.float64, name=None)})>
This is how I tried to train the model:
history = model.fit(train_batches,epochs=5,validation_data=validation_batches)
acc = history.history['accuracy']
print(acc)
And the following error occurs:
ValueError: Found unexpected losses or metrics that do not correspond to any Model output: dict_keys(['cooling_output', 'pollen_output', 'varroa_output', 'wasps_output']). Valid mode output names: ['dense_11']. Received struct is: {'cooling_output': <tf.Tensor 'IteratorGetNext:1' shape=(None,) dtype=float64>, 'pollen_output': <tf.Tensor 'IteratorGetNext:2' shape=(None,) dtype=float64>, 'varroa_output': <tf.Tensor 'IteratorGetNext:3' shape=(None,) dtype=float64>, 'wasps_output': <tf.Tensor 'IteratorGetNext:4' shape=(None,) dtype=float64>}.
I am sure there is an obvious explanation but I am an absolute beginner and would appreciate any help.

caffe check failed: kernel_size specified 2 times; 0 spatial dims

I have a caffe "Check failed" error as:
...
I0415 15:35:30.497133 39410 net.cpp:129] Top shape: 1 4096 (4096)
I0415 15:35:30.497135 39410 net.cpp:137] Memory required for data: 2898304
I0415 15:35:30.497138 39410 layer_factory.hpp:77] Creating layer conv1
I0415 15:35:30.497155 39410 net.cpp:84] Creating Layer conv1
I0415 15:35:30.497169 39410 net.cpp:406] conv1 <- ReLU0
I0415 15:35:30.497174 39410 net.cpp:380] conv1 -> conv1
F0415 15:35:30.497185 39410 base_conv_layer.cpp:35] Check failed: num_kernel_dims == 1 || num_kernel_dims == num_spatial_axes_ kernel_size must be specified once, or once per spatial dimension (kernel_size specified 2 times; 0 spatial dims).
and here is a little part of the proto.txt file:
...
layer {
name: "loss0"
type: "EuclideanLoss"
bottom: "ampl0"
bottom: "label_b4_noise"
top: "loss0"
}
layer {
name: "ReLU0"
type: "ReLU"
bottom: "ampl0"
top: "ReLU0"
relu_param {
negative_slope: 0
}
}
layer {
name: "conv1"
type: "Convolution"
bottom: "ReLU0"
top: "conv1"
param {
lr_mult: 1
decay_mult: 1
}
convolution_param {
num_output: 16
bias_term: false
pad: 0
pad: 0
kernel_size: 1
kernel_size: 5
group: 1
stride: 1
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
value: 0.0
}
axis: 1
}
}
...
could you please tell me why "Check failed"?
what "kernel_size specified 2 times; 0 spatial dims" means?
what num_spatial_axes_ kernel_size is here?
sorry if my question is trivial.
Look at the input for your layer ("ReLU0"):
I0415 15:35:30.497133 39410 net.cpp:129] Top shape: 1 4096 (4096)
Its dimensions are 1x4096 that is, it has 1 batch with 4096 channels with no width and no height (that is, width and height are singleton dimensions that are usually ignored).
Now you want to apply "conv1" a 1x5 kernel. How do you want to apply a convolution on a blob that has no spatial dimensions?!
This is the error you got from caffe: you specified 2 spatial dimensions for conv kernel (you specified kernel_size twice) but your input blob has no spatial dimensions at all, that is its num_spatial_axes_==0.

Keras: What is the correct data format for recurrent networks?

I am trying to build a recurrent network which classifies sequences (multidimensional data streams). I must be missing something, since while running my code:
from keras.models import Sequential
from keras.layers import LSTM, Dropout, Activation
import numpy as np
ils = 10 # input layer size
ilt = 11 # input layer time steps
hls = 12 # hidden layer size
nhl = 2 # number of hidden layers
ols = 1 # output layer size
p = 0.2 # dropout probability
f_a = 'relu' # activation function
opt = 'rmsprop' # optimizing function
#
# Building the model
#
model = Sequential()
# The input layer
model.add(LSTM(hls, input_shape=(ilt, ils), return_sequences=True))
model.add(Activation(f_a))
model.add(Dropout(p))
# Hidden layers
for i in range(nhl - 1):
model.add(LSTM(hls, return_sequences=True))
model.add(Activation(f_a))
model.add(Dropout(p))
# Output layer
model.add(LSTM(ols, return_sequences=False))
model.add(Activation('softmax'))
model.compile(optimizer=opt, loss='binary_crossentropy')
#
# Making test data and fitting the model
#
m_train, n_class = 1000, 2
data = np.array(np.random.random((m_train, ilt, ils)))
labels = np.random.randint(n_class, size=(m_train, 1))
model.fit(data, labels, nb_epoch=10, batch_size=32)
I get output (truncated):
Using Theano backend.
line 611, in __call__
node = self.make_node(*inputs, **kwargs)
File "/home/koala/.local/lib/python2.7/site-packages/theano/scan_module/scan_op.py", line 430, in make_node
new_inputs.append(format(outer_seq, as_var=inner_seq))
File "/home/koala/.local/lib/python2.7/site-packages/theano/scan_module/scan_op.py", line 422, in format
rval = tmp.filter_variable(rval)
File "/home/koala/.local/lib/python2.7/site-packages/theano/tensor/type.py", line 233, in filter_variable
self=self))
TypeError: Cannot convert Type TensorType(float32, 3D) (of Variable Subtensor{:int64:}.0) into Type TensorType(float32, (False, False, True)). You can try to manually convert Subtensor{:int64:}.0 into a TensorType(float32, (False, False, True)).
Is this a problem with the data format at all.
For me the problem was fixed when I went and tried it on my real dataset. The difference being that in the real dataset I have more than 1 label. So an example of dataset on which this code works is:
(...)
ols = 2 # Output layer size
(...)
m_train, n_class = 1000, ols
data = np.array(np.random.random((m_train, ilt, ils)))
labels = np.random.randint(n_class, size=(m_train, 1))
# Make labels onehot
onehot_labels = np.zeros(shape=(labels.shape[0], ols))
onehot_labels[np.arange(labels.shape[0]), labels.astype(np.int)] = 1

Error : H5LTfind_dataset(file_id, dataset_name_) Failed to find HDF5 dataset label

I want to use HDF5 file to input my data and labels in my CNN.
I created the hdf5 file with matlab.
Here is my code:
h5create(['uNetDataSet.h5'],'/home/alexandra/Documents/my-u-net/warwick_dataset/Warwick_Dataset/train/image',[522 775 3 numFrames]);
h5create(['uNetDataSet.h5'],'/home/alexandra/Documents/my-u-net/warwick_dataset/Warwick_Dataset/train/anno',[522 775 3 numFrames]);
h5create(['uNetDataSet.h5'],'/home/alexandra/Documents/my-u-net/warwick_dataset/Warwick_Dataset/label',[1 numFrames]);`
h5write(['uNetDataSet.h5'],'/home/alexandra/Documents/my-u-net/warwick_dataset/Warwick_Dataset/train/image',images);
h5write(['uNetDataSet.h5'],'/home/alexandra/Documents/my-u-net/warwick_dataset/Warwick_Dataset/train/anno',anno);
h5write(['uNetDataSet.h5'],'/home/alexandra/Documents/my-u-net/warwick_dataset/Warwick_Dataset/label',label);`
Where image, anno are 4D unit8 and label is a 1x85 unit16 vector.
When I display my .h5 file I got this:
HDF5 uNetDataSet.h5
Group '/'
Group '/home'
Group '/home/alexandra'
Group '/home/alexandra/Documents'
Group '/home/alexandra/Documents/my-u-net'
Group '/home/alexandra/Documents/my-u-net/warwick_dataset'
Group '/home/alexandra/Documents/my-u-net/warwick_dataset/Warwick_Dataset'
Dataset 'label'
Size: 1x85
MaxSize: 1x85
Datatype: H5T_IEEE_F64LE (double)
ChunkSize: []
Filters: none
FillValue: 0.000000
Group '/home/alexandra/Documents/my-u-net/warwick_dataset/Warwick_Dataset/train'
Dataset 'anno'
Size: 522x775x3x85
MaxSize: 522x775x3x85
Datatype: H5T_IEEE_F64LE (double)
ChunkSize: []
Filters: none
FillValue: 0.000000
Dataset 'image'
Size: 522x775x3x85
MaxSize: 522x775x3x85
Datatype: H5T_IEEE_F64LE (double)
ChunkSize: []
Filters: none
FillValue: 0.000000`
When I read the label dataset with h5read it works.
But when I try to train my network I got this error:
I0713 09:47:18.620510 4278 layer_factory.hpp:77] Creating layer loadMydata
I0713 09:47:18.620535 4278 net.cpp:91] Creating Layer loadMydata
I0713 09:47:18.620550 4278 net.cpp:399] loadMydata -> label
I0713 09:47:18.620580 4278 net.cpp:399] loadMydata -> anno
I0713 09:47:18.620600 4278 net.cpp:399] loadMydata -> image
I0713 09:47:18.620622 4278 hdf5_data_layer.cpp:79] Loading list of HDF5 filenames from: /home/alexandra/Documents/my-u-net/my_data.txt
I0713 09:47:18.620656 4278 hdf5_data_layer.cpp:93] Number of HDF5 files: 1
F0713 09:47:18.621317 4278 hdf5.cpp:14] Check failed: H5LTfind_dataset(file_id, dataset_name_) Failed to find HDF5 dataset label
*** Check failure stack trace: ***
# 0x7f2edf557daa (unknown)
# 0x7f2edf557ce4 (unknown)
# 0x7f2edf5576e6 (unknown)
# 0x7f2edf55a687 (unknown)
# 0x7f2edf908597 caffe::hdf5_load_nd_dataset_helper<>()
# 0x7f2edf907365 caffe::hdf5_load_nd_dataset<>()
# 0x7f2edf9579fe caffe::HDF5DataLayer<>::LoadHDF5FileData()
# 0x7f2edf956818 caffe::HDF5DataLayer<>::LayerSetUp()
# 0x7f2edf94fcbc caffe::Net<>::Init()
# 0x7f2edf950b45 caffe::Net<>::Net()
# 0x7f2edf91d08a caffe::Solver<>::InitTrainNet()
# 0x7f2edf91e18c caffe::Solver<>::Init()
# 0x7f2edf91e4ba caffe::Solver<>::Solver()
# 0x7f2edf930ed3 caffe::Creator_SGDSolver<>()
# 0x40e67e caffe::SolverRegistry<>::CreateSolver()
# 0x40794b train()
# 0x40590c main
# 0x7f2ede865f45 (unknown)
# 0x406041 (unknown)
# (nil) (unknown)
Aborted (core dumped)
In my .prototxt file :
layer {
top: 'label'
top:'anno'
top: 'image'
name: 'loadMydata'
type: "HDF5Data"
hdf5_data_param { source: '/home/alexandra/Documents/my-u-net/my_data.txt' batch_size: 1 }
include: { phase: TRAIN }
}
I don't know where I did something wrong, if anyone could help me it would be great !
your hdf5 file 'uNetDataSet.h5' does not have label in it.
What you have instead is '/home/alexandra/Documents/my-u-net/warwick_dataset/Warwick_Dataset/label' - I hope you can spot the difference.
Try creating the dataset with
h5create(['uNetDataSet.h5'],'/image',[522 775 3 numFrames]);
h5create(['uNetDataSet.h5'],'/anno',[522 775 3 numFrames]);
h5create(['uNetDataSet.h5'],'/label',[1 numFrames]);
Please see this answer for more details. Also note that you might need to permute the input data before saving it to hdf5 using matlab.

Check fail: how to use hdf5 data layer in deep layer?

I have the train and label data as data.mat. (I have 200 training data with 6000 features and labels are (-1, +1) that have saved in data.mat).
I am trying to convert my data (train and test) in hdf5 and run Caffe using:
load input.mat
hdf5write('my_data.h5', '/new_train_x', single( permute(reshape(new_train_x,[200, 6000, 1, 1]),[4:-1:1] ) ));
hdf5write('my_data.h5', '/label_train', single( permute(reshape(label_train,[200, 1, 1, 1]), [4:-1:1] ) ) , 'WriteMode', 'append' );
hdf5write('my_data_test.h5', '/test_x', single( permute(reshape(test_x,[77, 6000, 1, 1]),[4:-1:1] ) ));
hdf5write('my_data_test.h5', '/label_test', single( permute(reshape(label_test,[77, 1, 1, 1]), [4:-1:1] ) ) , 'WriteMode', 'append' );
(See this thread regarding converting mat-files to hdf5 in Matlab).
My train_val.prototxt is:
layer {
type: "HDF5Data"
name: "data"
top: "new_train_x" # note: same name as in HDF5
top: "label_train" #
hdf5_data_param {
source: "file.txt"
batch_size: 20
}
include { phase: TRAIN }
}
layer {
type: "HDF5Data"
name: "data"
top: "test_x" # note: same name as in HDF5
top: "label_test" #
hdf5_data_param {
source: "file_test.txt"
batch_size: 20
}
include { phase:TEST }
}
layer {
name: "ip1"
type: "InnerProduct"
bottom: "new_train_x"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 30
weight_filler {
type: "gaussian" # initialize the filters from a Gaussian
std: 0.01
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "tanh1"
type: "TanH"
bottom: "ip1"
top: "tanh1"
}
layer {
name: "ip2"
type: "InnerProduct"
bottom: "tanh1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 1
weight_filler {
type: "gaussian" # initialize the filters from a Gaussian
std: 0.01
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "loss"
type: "TanH"
bottom: "ip2"
bottom: "label_train"
top: "loss"
}
But I have a problem. It seems, it cannot read my input data.
I1227 10:27:21.880826 7186 layer_factory.hpp:76] Creating layer data
I1227 10:27:21.880851 7186 net.cpp:110] Creating Layer data
I1227 10:27:21.880866 7186 net.cpp:433] data -> new_train_x
I1227 10:27:21.880893 7186 net.cpp:433] data -> label_train
I1227 10:27:21.880915 7186 hdf5_data_layer.cpp:81] Loading list of HDF5 filenames from: file.txt
I1227 10:27:21.880965 7186 hdf5_data_layer.cpp:95] Number of HDF5 files: 1
I1227 10:27:21.962596 7186 net.cpp:155] Setting up data
I1227 10:27:21.962702 7186 net.cpp:163] Top shape: 20 6000 1 1 (120000)
I1227 10:27:21.962738 7186 net.cpp:163] Top shape: 20 1 1 1 (20)
I1227 10:27:21.962772 7186 layer_factory.hpp:76] Creating layer ip1
I1227 10:27:21.962838 7186 net.cpp:110] Creating Layer ip1
I1227 10:27:21.962873 7186 net.cpp:477] ip1 <- new_train_x
I1227 10:27:21.962918 7186 net.cpp:433] ip1 -> ip1
I1227 10:27:21.979375 7186 net.cpp:155] Setting up ip1
I1227 10:27:21.979434 7186 net.cpp:163] Top shape: 20 30 (600)
I1227 10:27:21.979478 7186 layer_factory.hpp:76] Creating layer tanh1
I1227 10:27:21.979529 7186 net.cpp:110] Creating Layer tanh1
I1227 10:27:21.979557 7186 net.cpp:477] tanh1 <- ip1
I1227 10:27:21.979583 7186 net.cpp:433] tanh1 -> tanh1
I1227 10:27:21.979620 7186 net.cpp:155] Setting up tanh1
I1227 10:27:21.979650 7186 net.cpp:163] Top shape: 20 30 (600)
I1227 10:27:21.979670 7186 layer_factory.hpp:76] Creating layer ip2
I1227 10:27:21.979696 7186 net.cpp:110] Creating Layer ip2
I1227 10:27:21.979720 7186 net.cpp:477] ip2 <- tanh1
I1227 10:27:21.979746 7186 net.cpp:433] ip2 -> ip2
I1227 10:27:21.979796 7186 net.cpp:155] Setting up ip2
I1227 10:27:21.979825 7186 net.cpp:163] Top shape: 20 1 (20)
I1227 10:27:21.979854 7186 layer_factory.hpp:76] Creating layer loss
I1227 10:27:21.979881 7186 net.cpp:110] Creating Layer loss
I1227 10:27:21.979909 7186 net.cpp:477] loss <- ip2
I1227 10:27:21.979931 7186 net.cpp:477] loss <- label_train
I1227 10:27:21.979962 7186 net.cpp:433] loss -> loss
F1227 10:27:21.980006 7186 layer.hpp:374] Check failed: ExactNumBottomBlobs() == bottom.size() (1 vs. 2) TanH Layer takes 1 bottom blob(s) as input.
*** Check failure stack trace: ***
# 0x7f44cbc68ea4 (unknown)
# 0x7f44cbc68deb (unknown)
# 0x7f44cbc687bf (unknown)
# 0x7f44cbc6ba35 (unknown)
# 0x7f44cbfd0ba8 caffe::Layer<>::CheckBlobCounts()
# 0x7f44cbfed9da caffe::Net<>::Init()
# 0x7f44cbfef108 caffe::Net<>::Net()
# 0x7f44cc03f71a caffe::Solver<>::InitTrainNet()
# 0x7f44cc040a51 caffe::Solver<>::Init()
# 0x7f44cc040db9 caffe::Solver<>::Solver()
# 0x41222d caffe::GetSolver<>()
# 0x408ed9 train()
# 0x406741 main
# 0x7f44ca997a40 (unknown)
# 0x406f69 _start
# (nil) (unknown)
Aborted (core dumped)
Now, if i change loss layer like this:
layer {
name: "loss"
type: "TanH"
bottom: "ip2"
top: "loss"
}
I have this problem:
F1227 10:53:17.884419 9102 insert_splits.cpp:35] Unknown bottom blob 'new_train_x' (layer 'ip1', bottom index 0)
*** Check failure stack trace: ***
# 0x7f502ab5dea4 (unknown)
# 0x7f502ab5ddeb (unknown)
# 0x7f502ab5d7bf (unknown)
# 0x7f502ab60a35 (unknown)
# 0x7f502af1d75b caffe::InsertSplits()
# 0x7f502aee19e9 caffe::Net<>::Init()
# 0x7f502aee4108 caffe::Net<>::Net()
# 0x7f502af35172 caffe::Solver<>::InitTestNets()
# 0x7f502af35abd caffe::Solver<>::Init()
# 0x7f502af35db9 caffe::Solver<>::Solver()
# 0x41222d caffe::GetSolver<>()
# 0x408ed9 train()
# 0x406741 main
# 0x7f502988ca40 (unknown)
# 0x406f69 _start
# (nil) (unknown)
Aborted (core dumped)
Many thanks!!!! Any advice would be appreciated!
Your data layer is only defined for phase: TRAIN I believe the error occurs when caffe attempts to construct the test-time net (i.e., the phase: TEST net).
You should have an additional layer with test data:
layer {
type: "HDF5Data"
name: "data"
top: "new_train_x" # note: same name as in HDF5
top: "label_train" #
hdf5_data_param {
source: "test_file.txt"
batch_size: 20
}
include { phase: TEST } # do not forget TEST phase
}
BTW, if you do not want to test your net during training, you can switch this option off. See this thread for more information.
Update:
Forgive me for being blunt, but you are making quite a mess.
"TanH" is not a loss layer - it's a neuron/activation layer. It serves as a non-linarity applied to a linear layer (conv/inner-product). As such, it accepts a single input (bottom blob) and outputs a single blob (top).
A loss layer computes a scalar loss value and usually requires two inputs: prediction and ground truth to compare to.
You did change your net and added a "HDF5Data" layer for the TEST phase as well, but this layer outputs a top: "test_x", no layer in your net expects a bottom: "test_x" you only have layers expecting "new_train_x"... same goes for "label_text".
I suggest you re-write your hdf5 files with more generic names (e.g., x and label) for both train and test. Just use different file names to distinguish between them. This way your net works with "x" and "label" in both phases and only loads the appropriate dataset according to phase.