I tried to run ResNet50 computation graph provided in ZooModel of deeplearning4j but got following error while calling init() method of resNet. I have 10 classes and each image is 64*64.
Please suggest the correct way of initializing.
Code
ResNet50Builder builder = ResNet50.builder();
ResNet50 resNet = builder.numClasses(10).cacheMode(CacheMode.NONE)
.workspaceMode(WorkspaceMode.NONE).inputShape(new int[] {3, 64, 64}).build();
ComputationGraph computationGraph = resNet.init();
Exception Stack Trace:
Exception in thread "main" org.deeplearning4j.exception.DL4JInvalidConfigException: Invalid configuration for layer (idx=77, name=res4a_branch2b, type=ConvolutionLayer) for height dimension: Invalid input configuration for kernel height. Require 0 < kH <= inHeight + 2*padH; got (kH=3, inHeight=2, padH=0)
Input type = InputTypeConvolutional(h=2,w=2,c=256), kernel = [3, 3], strides = [1, 1], padding = [0, 0], layer size (output channels) = 256, convolution mode = Same
at org.deeplearning4j.nn.conf.layers.InputTypeUtil.getOutputTypeCnnLayers(InputTypeUtil.java:327)
at org.deeplearning4j.nn.conf.layers.ConvolutionLayer.getOutputType(ConvolutionLayer.java:191)
at org.deeplearning4j.nn.conf.graph.LayerVertex.getOutputType(LayerVertex.java:131)
at org.deeplearning4j.nn.conf.ComputationGraphConfiguration.getLayerActivationTypes(ComputationGraphConfiguration.java:536)
at org.deeplearning4j.nn.conf.ComputationGraphConfiguration.addPreProcessors(ComputationGraphConfiguration.java:449)
at org.deeplearning4j.nn.conf.ComputationGraphConfiguration$GraphBuilder.build(ComputationGraphConfiguration.java:1201)
at org.deeplearning4j.zoo.model.ResNet50.init(ResNet50.java:91)
at org.deeplearning4j.examples.convolution.ResNet.run(ResNet.java:145)
at org.deeplearning4j.examples.convolution.ResNet.main(ResNet.java:306)
I got this answer from other forum (https://community.konduit.ai/t/error-while-running-resnet50-zoomodel-example/417)
The image is too small, which results in filters being too large after the first few constitutional layers.
Related
I'm trying to convert ResNet50 encoder to ResNet18 encoder for U-Net model from this repository https://github.com/kevinlu1211/pytorch-unet-resnet-50-encoder/blob/master/u_net_resnet_50_encoder.py. Its all new to me, I don't understand why changing the network like this does not work.
self.bridge = Bridge(512, 512)
up_blocks.append(UpBlockForUNetWithResNet50(512, 256))
up_blocks.append(UpBlockForUNetWithResNet50(256, 128))
up_blocks.append(UpBlockForUNetWithResNet50(128,64))
The error received is:
Given groups=1, weight of size [128, 192, 3, 3], expected input[2, 256, 64, 64] to have 192 channels, but got 256 channels instead.
I am using colab 25Ram for implementing Human action recognition, all my models always crashed my session because i used all the memory (also if they are not so big).
Im trying now to use transfer learning of VGG16, i began with the real model and just added a TimeDistributed of my model to match it with my data and a Dense layer to check if that work, and my colab session also crashed.
video = Input(shape=(350, 288, 384, 3))
cnn_base = VGG16(input_shape=(288, 384, 3), weights="imagenet", include_top=False)
cnn_out = GlobalAveragePooling2D()(cnn_base.output)
cnn = Model(inputs=cnn_base.input, outputs=cnn_out)
cnn.trainable = False
x = TimeDistributed(cnn)(video)
x = Dense(1, activation="sigmoid")(x)
model = Model([video], x)
model.summary()
model.compile(optimizer='SGD', loss='binary_crossentropy',metrics=["accuracy"])
model.fit(x=X, y=y, batch_size=2, epochs=1)
All advices will help me, im already 2 weeks stuck with OOM problems on colab.Thanks
When using deep q-learning I am trying to capture motion by passing a number of grayscale frames as the input, each with the dimensions 90x90. There will be four 90x90 frames passed in to allow the network to detect motion. The multiple frames should be considered a single state rather than a batch of 4 states, how can I get a vector of actions as a result instead of a matrix?
I am using pytorch and it will return a matrix of 4x7 - a row of actions for each frame. here is the network:
self.conv1 = Conv2d(self.channels, 32, 8)
self.conv2 = Conv2d(32, 64, 4)
self.conv3 = Conv2d(64, 128, 3)
self.fc1 = Linear(128 * 52 * 52, 64)
self.fc2 = Linear(64, 32)
self.output = Linear(32, action_space)
Select the action with the highest value.
Let's call the output tensor be called action_values.
action=torch.argmax(action_values.data)
or
action=np.argmax(action_values.cpu().data.numpy())
In keras, is it possible to share weights between two layers, but to have other parameters differ? Consider the following (admittedly a bit contrived) example:
conv1 = Conv2D(64, 3, input_shape=input_shape, padding='same')
conv2 = Conv2D(64, 3, input_shape=input_shape, padding='valid')
Notice that the layers are identical except for the padding. Can I get keras to use the same weights for both? (i.e. also train the network accordingly?)
I've looked at the keras doc, and the section on shared layers seems to imply that sharing works only if the layers are completely identical.
To my knowledge, this cannot be done by the common "API level" of Keras usage.
However, if you dig a bit deeper, there are some (ugly) ways to share the weights.
First of all, the weights of the Conv2D layers are created inside the build() function, by calling add_weight():
self.kernel = self.add_weight(shape=kernel_shape,
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
For your provided usage (i.e., default trainable/constraint/regularizer/initializer), add_weight() does nothing special but appending the weight variables to _trainable_weights:
weight = K.variable(initializer(shape), dtype=dtype, name=name)
...
self._trainable_weights.append(weight)
Finally, since build() is only called inside __call__() if the layer hasn't been built, shared weights between layers can be created by:
Call conv1.build() to initialize the conv1.kernel and conv1.bias variables to be shared.
Call conv2.build() to initialize the layer.
Replace conv2.kernel and conv2.bias by conv1.kernel and conv1.bias.
Remove conv2.kernel and conv2.bias from conv2._trainable_weights.
Append conv1.kernel and conv1.bias to conv2._trainable_weights.
Finish model definition. Here conv2.__call__() will be called; however, since conv2 has already been built, the weights are not going to be re-initialized.
The following code snippet may be helpful:
def create_shared_weights(conv1, conv2, input_shape):
with K.name_scope(conv1.name):
conv1.build(input_shape)
with K.name_scope(conv2.name):
conv2.build(input_shape)
conv2.kernel = conv1.kernel
conv2.bias = conv1.bias
conv2._trainable_weights = []
conv2._trainable_weights.append(conv2.kernel)
conv2._trainable_weights.append(conv2.bias)
# check if weights are successfully shared
input_img = Input(shape=(299, 299, 3))
conv1 = Conv2D(64, 3, padding='same')
conv2 = Conv2D(64, 3, padding='valid')
create_shared_weights(conv1, conv2, input_img._keras_shape)
print(conv2.weights == conv1.weights) # True
# check if weights are equal after model fitting
left = conv1(input_img)
right = conv2(input_img)
left = GlobalAveragePooling2D()(left)
right = GlobalAveragePooling2D()(right)
merged = concatenate([left, right])
output = Dense(1)(merged)
model = Model(input_img, output)
model.compile(loss='binary_crossentropy', optimizer='adam')
X = np.random.rand(5, 299, 299, 3)
Y = np.random.randint(2, size=5)
model.fit(X, Y)
print([np.all(w1 == w2) for w1, w2 in zip(conv1.get_weights(), conv2.get_weights())]) # [True, True]
One drawback of this hacky weight-sharing is that the weights will not remain shared after model saving/loading. This will not affect prediction, but it may be problematic if you want to load the trained model for further fine-tuning.
i am basically trying to build a deep model which consists of many convolution2d layers followed by maxpooling 2d as follows :
model.add(Convolution2D(128, 54, 7, input_shape=(1, 54, 180)))
model.add(MaxPooling2D(pool_size=(1, 3)))
model.add(Convolution2D(128, 1, 7))
model.add(MaxPooling2D(pool_size=(1, 3)))
However , i am getting the following error :
File
"/home/user/anaconda2/lib/python2.7/site-packages/keras/engine/training.py",
line 100, in standardize_input_data
str(array.shape)) Exception: Error when checking model input: expected convolution2d_input_1 to have 4 dimensions, but got array
with shape (8000, 180, 54)
Blockquote
But i am following the (samples, channels, rows, cols) norm. Why is this happening ?
It seems like your input data has the wrong shape. You should print out the shape of the data that you are feeding into the network.
It seems like your array are gray input images and they normally only use 2 dimensions because they only have 1 channel. Therefore the np array is ordered without the third dimension. Normally you have to add this by using np.reshape or allocating your array in another way. When I get an error message like yours i would try:
X # training data
X = np.transpose(X, (0, 2, 1))
X = np.reshape(X, (X.shape[0], 1, X.shape[1], X.shape[2]))