I'm trying to implement this segmentation problem.
[https://user-images.githubusercontent.com/91024790/178153192-040ab44c-7b9f-4cfd-8e11-a3cdca2070e9.png][1]
My dataset is composed by images and masks.
In order to create a sequences of images and fed them into the network I used TimeDistributedImageDataGenerator ( https://github.com/kivijoshi/TimeDistributedImageDataGenerator/blob/master/TimeDistributedImageDataGenerator/TimeDistributedImageDataGenerator.py)
Here attached my code:
'''
seed=42
from keras.preprocessing.image import ImageDataGenerator
img_data_gen_args = dict(rescale=1./255,
rotation_range=90,
zoom_range=0.2,
brightness_range=[0.3,0.9],
width_shift_range=0.3,
height_shift_range=0.3,
shear_range=0.5,
time_steps=3,
horizontal_flip=True,
vertical_flip=True,
fill_mode='constant')
mask_data_gen_args = dict(
rotation_range=90,
zoom_range=0.2,
brightness_range=[0.3,0.9],
width_shift_range=0.3,
height_shift_range=0.3,
shear_range=0.5,
time_steps=1,
horizontal_flip=True,
vertical_flip=True,
fill_mode='constant',
preprocessing_function = lambda x: np.where(x>0, 1, 0).astype(x.dtype)
) #Binarize the output again.
image_data_generator = TimeDistributedImageDataGenerator(**img_data_gen_args)
mask_data_generator = TimeDistributedImageDataGenerator(**mask_data_gen_args)
image_generator = image_data_generator.flow_from_directory(train_img_path,
seed=seed,
batch_size=batch_size,
color_mode = 'grayscale',
target_size=(256,256),
class_mode=None) #Very important to set this otherwise it returns multiple numpy arrays
#thinking class mode is binary.
mask_generator = mask_data_generator.flow_from_directory(train_mask_path,
seed=seed,
batch_size=batch_size,
color_mode = 'grayscale',
target_size=(256,256) , #Read masks in grayscale
class_mode=None)
valid_img_generator = image_data_generator.flow_from_directory(val_img_path,
seed=seed,
batch_size=batch_size,
color_mode = 'grayscale',
target_size=(256,256),
class_mode=None) #Default batch size 32, if not specified here
valid_mask_generator = mask_data_generator.flow_from_directory(val_mask_path,
seed=seed,
batch_size=batch_size,
target_size=(256,256),
color_mode = 'grayscale', #Read masks in grayscale
class_mode=None) #Default batch size 32, if not specified here
train_generator = zip(image_generator, mask_generator)
val_generator = zip(valid_img_generator, valid_mask_generator)
I used time_steps=3 for image_generator and time_steps=1 for mask_generator since i would predict just the last image of a sequence of three images ( as the image suggest).
Now my image generator has (3,3,256,256,1) as shape while mask generator (3,1,256,256,1) where the first dimension is the batch size the second one the time_steps and the last three are width, height and channels.
Then i built my segmentation model:
input_l = layers.Input(shape=(input_shape))
x = (layers.TimeDistributed(layers.Conv2D( 64, kernel_size=(3, 3),padding='same',strides=(1,1),activation='relu',kernel_initializer='he_normal' ) )) (input_l)
conv2 = layers.TimeDistributed( layers.Conv2D( 64, kernel_size=(3, 3),padding='same',strides=(1,1),activation='relu' ,kernel_initializer='he_normal' ) ) (x)
x=layers.TimeDistributed(layers.MaxPooling2D(pool_size=(2,2)))(conv2)
x = layers.TimeDistributed( layers.Conv2D( 128, kernel_size=(3, 3),padding='same',strides=(1,1),activation='relu' ,kernel_initializer='he_normal' ) ) (x)
conv5 = layers.TimeDistributed( layers.Conv2D( 128, kernel_size=(3, 3),padding='same',strides=(1,1),activation='relu' ,kernel_initializer='he_normal') ) (x)
x=layers.TimeDistributed(layers.MaxPooling2D(pool_size=(2,2)))(conv5)
x = layers.TimeDistributed( layers.Conv2D( 256, kernel_size=(3, 3),padding='same',strides=(1,1) ,activation='relu' ,kernel_initializer='he_normal' ) ) (x)
conv8 = layers.TimeDistributed( layers.Conv2D( 256, kernel_size=(3, 3),padding='same',strides=(1,1) ,activation='relu',kernel_initializer='he_normal' ) ) (x)
x=layers.TimeDistributed(layers.MaxPooling2D(pool_size=(2,2)))(conv8)
x=layers.Bidirectional(layers.ConvLSTM2D(256,kernel_size=(3,3),padding='same',strides=(1,1),return_sequences=True,recurrent_dropout=0.2))(x)
up1 = layers.TimeDistributed( layers.Conv2DTranspose( 512,kernel_size=(3,3),padding='same',strides=(2,2)))(x)
concat1 = layers.concatenate([up1, conv8])
x = layers.TimeDistributed( layers.Conv2D( 256, kernel_size=(3, 3),padding='same',strides=(1,1) ,activation='relu' ,kernel_initializer='he_normal' ) ) (concat1)
x = layers.TimeDistributed( layers.Conv2D( 256, kernel_size=(3, 3),padding='same',strides=(1,1) ,activation='relu' ,kernel_initializer='he_normal' ) ) (x)
up2 = layers.TimeDistributed( layers.Conv2DTranspose( 256,kernel_size=(3,3),padding='same',strides=(2,2)))(x)
concat2 = layers.concatenate([up2, conv5])
x = layers.TimeDistributed( layers.Conv2D( 128, kernel_size=(3, 3),padding='same',strides=(1,1),activation='relu',kernel_initializer='he_normal' ) ) (concat2)
x = layers.TimeDistributed( layers.Conv2D( 128, kernel_size=(3, 3),padding='same',strides=(1,1) ,activation='relu',kernel_initializer='he_normal' ) ) (x)
up3 = layers.TimeDistributed( layers.Conv2DTranspose( 128,kernel_size=(3,3),padding='same',strides=(2,2)))(x)
concat3 = layers.concatenate([up3, conv2])
x = layers.TimeDistributed( layers.Conv2D( 64, kernel_size=(3, 3),padding='same',strides=(1,1),activation='relu' ,kernel_initializer='he_normal' ) ) (concat3)
x=layers.Bidirectional(layers.ConvLSTM2D(32,kernel_size=(3,3),padding='same',strides=(1,1),return_sequences=False,recurrent_dropout=0.2))(x)
x=tf.expand_dims(x,axis=1)
out = layers.Conv2D( 1, kernel_size=(1, 1),padding='same',strides=(1,1), activation='sigmoid' ) (x)
model = models.Model(inputs=input_l, outputs=out)
model.summary()
Model: "model_1"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_2 (InputLayer) [(None, 3, 256, 256 0 []
, 1)]
time_distributed_17 (TimeDistr (None, 3, 256, 256, 640 ['input_2[0][0]']
ibuted) 64)
time_distributed_18 (TimeDistr (None, 3, 256, 256, 36928 ['time_distributed_17[0][0]']
ibuted) 64)
time_distributed_19 (TimeDistr (None, 3, 128, 128, 0 ['time_distributed_18[0][0]']
ibuted) 64)
time_distributed_20 (TimeDistr (None, 3, 128, 128, 73856 ['time_distributed_19[0][0]']
ibuted) 128)
time_distributed_21 (TimeDistr (None, 3, 128, 128, 147584 ['time_distributed_20[0][0]']
ibuted) 128)
time_distributed_22 (TimeDistr (None, 3, 64, 64, 1 0 ['time_distributed_21[0][0]']
ibuted) 28)
time_distributed_23 (TimeDistr (None, 3, 64, 64, 2 295168 ['time_distributed_22[0][0]']
ibuted) 56)
time_distributed_24 (TimeDistr (None, 3, 64, 64, 2 590080 ['time_distributed_23[0][0]']
ibuted) 56)
time_distributed_25 (TimeDistr (None, 3, 32, 32, 2 0 ['time_distributed_24[0][0]']
ibuted) 56)
bidirectional_2 (Bidirectional (None, 3, 32, 32, 5 9439232 ['time_distributed_25[0][0]']
) 12)
time_distributed_26 (TimeDistr (None, 3, 64, 64, 5 2359808 ['bidirectional_2[0][0]']
ibuted) 12)
concatenate_3 (Concatenate) (None, 3, 64, 64, 7 0 ['time_distributed_26[0][0]',
68) 'time_distributed_24[0][0]']
time_distributed_27 (TimeDistr (None, 3, 64, 64, 2 1769728 ['concatenate_3[0][0]']
ibuted) 56)
time_distributed_28 (TimeDistr (None, 3, 64, 64, 2 590080 ['time_distributed_27[0][0]']
ibuted) 56)
time_distributed_29 (TimeDistr (None, 3, 128, 128, 590080 ['time_distributed_28[0][0]']
ibuted) 256)
concatenate_4 (Concatenate) (None, 3, 128, 128, 0 ['time_distributed_29[0][0]',
384) 'time_distributed_21[0][0]']
time_distributed_30 (TimeDistr (None, 3, 128, 128, 442496 ['concatenate_4[0][0]']
ibuted) 128)
time_distributed_31 (TimeDistr (None, 3, 128, 128, 147584 ['time_distributed_30[0][0]']
ibuted) 128)
time_distributed_32 (TimeDistr (None, 3, 256, 256, 147584 ['time_distributed_31[0][0]']
ibuted) 128)
concatenate_5 (Concatenate) (None, 3, 256, 256, 0 ['time_distributed_32[0][0]',
192) 'time_distributed_18[0][0]']
time_distributed_33 (TimeDistr (None, 3, 256, 256, 110656 ['concatenate_5[0][0]']
ibuted) 64)
bidirectional_3 (Bidirectional (None, 256, 256, 64 221440 ['time_distributed_33[0][0]']
) )
tf.expand_dims_1 (TFOpLambda) (None, 1, 256, 256, 0 ['bidirectional_3[0][0]']
64)
conv2d_23 (Conv2D) (None, 1, 256, 256, 65 ['tf.expand_dims_1[0][0]']
1)
==================================================================================================
Total params: 16,963,009
Trainable params: 16,963,009
Non-trainable params: 0
Everything works,however dice coefficient is very low. I think that the main problem is a mismatch between masks and images.
it is possible that not having the same length of sequences the images and masks do not match
Is it possible that having different lenght of sequences results in a mismatch between images and masks? Any ideas? Thank you in advance
I used a 3DUnet with resblock to segment a CT image with input torch size of [1, 1, 96, 176, 176], but it throws the following error:
RuntimeError: Sizes of tensors must match except in dimension 2. Got 55 and 54 (The offending index is 0)
Hence I traced back, I found the error comes from
outputs = self.decoder_stage2(torch.cat([short_range6, long_range3], dim=1)) + short_range6
The short_range6 has torch.Size([1, 64, 24, 55, 40]) while the long_range3 has torch.Size([1, 128, 24, 54, 40]). I think this is because something not being a power of 2, but cannot find where to modify.
Below is the complete structure of the network, really thanks for any help!
class ResUNet(nn.Module):
def __init__(self, in_channel=1, out_channel=2 ,training=True):
super().__init__()
self.training = training
self.dorp_rate = 0.2
self.encoder_stage1 = nn.Sequential(
nn.Conv3d(in_channel, 16, 3, 1, padding=1),
nn.PReLU(16),
nn.Conv3d(16, 16, 3, 1, padding=1),
nn.PReLU(16),
)
self.encoder_stage2 = nn.Sequential(
nn.Conv3d(32, 32, 3, 1, padding=1),
nn.PReLU(32),
nn.Conv3d(32, 32, 3, 1, padding=1),
nn.PReLU(32),
nn.Conv3d(32, 32, 3, 1, padding=1),
nn.PReLU(32),
)
self.encoder_stage3 = nn.Sequential(
nn.Conv3d(64, 64, 3, 1, padding=1),
nn.PReLU(64),
nn.Conv3d(64, 64, 3, 1, padding=2, dilation=2),
nn.PReLU(64),
nn.Conv3d(64, 64, 3, 1, padding=4, dilation=4),
nn.PReLU(64),
)
self.encoder_stage4 = nn.Sequential(
nn.Conv3d(128, 128, 3, 1, padding=3, dilation=3),
nn.PReLU(128),
nn.Conv3d(128, 128, 3, 1, padding=4, dilation=4),
nn.PReLU(128),
nn.Conv3d(128, 128, 3, 1, padding=5, dilation=5),
nn.PReLU(128),
)
self.decoder_stage1 = nn.Sequential(
nn.Conv3d(128, 256, 3, 1, padding=1),
nn.PReLU(256),
nn.Conv3d(256, 256, 3, 1, padding=1),
nn.PReLU(256),
nn.Conv3d(256, 256, 3, 1, padding=1),
nn.PReLU(256),
)
self.decoder_stage2 = nn.Sequential(
nn.Conv3d(128 + 64, 128, 3, 1, padding=1),
nn.PReLU(128),
nn.Conv3d(128, 128, 3, 1, padding=1),
nn.PReLU(128),
nn.Conv3d(128, 128, 3, 1, padding=1),
nn.PReLU(128),
)
self.decoder_stage3 = nn.Sequential(
nn.Conv3d(64 + 32, 64, 3, 1, padding=1),
nn.PReLU(64),
nn.Conv3d(64, 64, 3, 1, padding=1),
nn.PReLU(64),
nn.Conv3d(64, 64, 3, 1, padding=1),
nn.PReLU(64),
)
self.decoder_stage4 = nn.Sequential(
nn.Conv3d(32 + 16, 32, 3, 1, padding=1),
nn.PReLU(32),
nn.Conv3d(32, 32, 3, 1, padding=1),
nn.PReLU(32),
)
self.down_conv1 = nn.Sequential(
nn.Conv3d(16, 32, 2, 2),
nn.PReLU(32)
)
self.down_conv2 = nn.Sequential(
nn.Conv3d(32, 64, 2, 2),
nn.PReLU(64)
)
self.down_conv3 = nn.Sequential(
nn.Conv3d(64, 128, 2, 2),
nn.PReLU(128)
)
self.down_conv4 = nn.Sequential(
nn.Conv3d(128, 256, 3, 1, padding=1),
nn.PReLU(256)
)
self.up_conv2 = nn.Sequential(
nn.ConvTranspose3d(256, 128, 2, 2),
nn.PReLU(128)
)
self.up_conv3 = nn.Sequential(
nn.ConvTranspose3d(128, 64, 2, 2),
nn.PReLU(64)
)
self.up_conv4 = nn.Sequential(
nn.ConvTranspose3d(64, 32, 2, 2),
nn.PReLU(32)
)
# 256*256
self.map4 = nn.Sequential(
nn.Conv3d(32, out_channel, 1, 1),
nn.Upsample(scale_factor=(1, 1, 1), mode='trilinear', align_corners=False),
nn.Softmax(dim=1)
)
# 128*128
self.map3 = nn.Sequential(
nn.Conv3d(64, out_channel, 1, 1),
nn.Upsample(scale_factor=(2, 2, 2), mode='trilinear', align_corners=False),
nn.Softmax(dim=1)
)
# 64*64
self.map2 = nn.Sequential(
nn.Conv3d(128, out_channel, 1, 1),
nn.Upsample(scale_factor=(4, 4, 4), mode='trilinear', align_corners=False),
nn.Softmax(dim=1)
)
# 32*32
self.map1 = nn.Sequential(
nn.Conv3d(256, out_channel, 1, 1),
nn.Upsample(scale_factor=(8, 8, 8), mode='trilinear', align_corners=False),
nn.Softmax(dim=1)
)
def forward(self, inputs):
long_range1 = self.encoder_stage1(inputs) + inputs
short_range1 = self.down_conv1(long_range1)
long_range2 = self.encoder_stage2(short_range1) + short_range1
long_range2 = F.dropout(long_range2, self.dorp_rate, self.training)
short_range2 = self.down_conv2(long_range2)
long_range3 = self.encoder_stage3(short_range2) + short_range2
long_range3 = F.dropout(long_range3, self.dorp_rate, self.training)
short_range3 = self.down_conv3(long_range3)
long_range4 = self.encoder_stage4(short_range3) + short_range3
long_range4 = F.dropout(long_range4, self.dorp_rate, self.training)
short_range4 = self.down_conv4(long_range4)
outputs = self.decoder_stage1(long_range4) + short_range4
outputs = F.dropout(outputs, self.dorp_rate, self.training)
output1 = self.map1(outputs)
short_range6 = self.up_conv2(outputs)
outputs = self.decoder_stage2(torch.cat([short_range6, long_range3], dim=1)) + short_range6
outputs = F.dropout(outputs, self.dorp_rate, self.training)
output2 = self.map2(outputs)
short_range7 = self.up_conv3(outputs)
outputs = self.decoder_stage3(torch.cat([short_range7, long_range2], dim=1)) + short_range7
outputs = F.dropout(outputs, self.dorp_rate, self.training)
output3 = self.map3(outputs)
short_range8 = self.up_conv4(outputs)
outputs = self.decoder_stage4(torch.cat([short_range8, long_range1], dim=1)) + short_range8
output4 = self.map4(outputs)
if self.training is True:
return output1, output2, output3, output4
else:
return output4```
You can pad your image's dimensions to be multiple of 32's. By doing this, you won't have to change the 3DUnet's parameters.
I will provide you a simple code to show you the way.
# I assume that you named your input image as img
padding1_mult = math.floor(img.shape[3] / 32) + 1
padding2_mult = math.floor(img.shape[4] / 32) + 1
pad1 = (32 * padding1_mult) - img.shape[3]
pad2 = (32 * padding2_mult) - img.shape[4]
padding = nn.ReplicationPad2d((0, pad2, pad1, 0, 0 ,0))
img = padding(img)
After this operation, your image shape must be torch.Size([1, 1, 96, 192, 192])
I have five tables containing lexical data. I want to display sentences from corpus to the given Icelandic lemma (including all word forms). Using following approach, it takes 2 seconds to find 5 sentences. I am looking for a solution that can display all sentences available.
The expected result:
The list of sentences that contain all word forms of given lemma specified in the query.
Current result:
Current results returns only the sentences that match the word with the keyword in basic form:
word_form w_id s_id pos sentence
hest 11484 794930 1 Sentence 1. .....
hest 11484 795623 12 Sentence 2 .....
Expected result:
word_form w_id s_id pos sentence
hest 11484 794930 1 Sentence 1. .....
hest 11484 795623 12 Sentence 2 .....
...
hestur .. .. .. Sentence 13.
hestur .. .. .. Sentence 14.
...
hesti .. .. .. Sentence 21.
...
Proposed query with changes, but ends with error.
SELECT w0.keyword, w.word_form, w3.w_id, w4.s_id, w4.pos, s.sentence
FROM `1_headword` w0
INNER JOIN `2_wordform` w ON w.keyword = w0.keyword
INNER JOIN `3_words` w3 ON w3.word = w.word_form
INNER JOIN `4_inv_w` w4 ON w4.w_id = w3.w_id
INNER JOIN `5_sentences` s
ON s.s_id = w4.s_id WHERE w0.keyword like 'hestur' group by w4.s_id
Notes:
Keyword is one, the basic form - in this case "hestur". The word forms are in this case - "hest", "hesti", "hestar" (see the Insert table) etc.
In other words, the query should take all wordform of given lemma and match it sentences in which the wordforms occur.
Update II.
Few observation.
1.The following simplified query to receive w_id for all word forms returns rows with repeated w_id of the first word form.
2. The word forms can have several rows in 3_words table.
SELECT w.keyword, w.word_form, w3.w_id FROM `2_wordform1` w
JOIN `3_words` w3
ON w3.word = w.keyword and w3.gram = w.gram
WHERE w.keyword like 'tala' and w.gram = 'f'
Rows
tala tala 8809
tala tala 89664
tala tala 97991
Tala Tala 8809
Tala Tala 89664
Tala Tala 97991
tala tölur 8809
tala tölur 89664
tala tölur 97991
Tables and data
table - headwords, 70000 rows
CREATE TABLE IF NOT EXISTS `1_headword` (
`id` int(9) NOT NULL,
`keyword` varchar(100) CHARACTER SET utf8 COLLATE utf8_icelandic_ci NOT NULL,
`num_keyword` int(9) NOT NULL DEFAULT '0',
`gram` varchar(40) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC AUTO_INCREMENT=55328 ;
ALTER TABLE `1_headword`
ADD PRIMARY KEY (`id`), ADD KEY `keyword` (`keyword`);
table - word forms - 700 000 rows
CREATE TABLE IF NOT EXISTS `2_wordform` (
`id` int(10) NOT NULL,
`keyword` varchar(120) CHARACTER SET utf8 COLLATE utf8_icelandic_ci NOT NULL,
`num_keyword` int(4) NOT NULL,
`word_form` varchar(120) CHARACTER SET utf8 COLLATE utf8_icelandic_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=678480 ;
ALTER TABLE `2_wordform`
ADD PRIMARY KEY (`id`), ADD KEY `word_form` (`word_form`);
table - word forms tagged from corpus with w_id (word id), 1 million of rows
CREATE TABLE `3_words` (
`w_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`word` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`gram` varchar(255) DEFAULT NULL,
`freq` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`w_id`),
KEY `word` (`word`),
KEY `w_id` (`w_id`)
) ENGINE=MyISAM AUTO_INCREMENT=800468 DEFAULT CHARSET=utf8;
table - w_id (word id) connected to s_id (sentence id), word can be found in several sentences, plus position in the sentence, 22 millions of rows
CREATE TABLE `4_inv_w` (
`w_id` int(10) unsigned NOT NULL DEFAULT '0',
`s_id` int(10) unsigned NOT NULL DEFAULT '0',
`pos` mediumint(2) unsigned NOT NULL DEFAULT '0',
KEY `w_id` (`w_id`),
KEY `s_id` (`s_id`),
KEY `w_s` (`w_id`,`s_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
table - s_id (sentence id) with sentence, 1 million of rows
CREATE TABLE `5_sentences` (
`s_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`sentence` text,
KEY `s_id` (`s_id`)
) ENGINE=MyISAM AUTO_INCREMENT=999953 DEFAULT CHARSET=utf8;
Process
select all word forms of given lemma f.e "hestur" (horse in English)
SELECT `word_form` FROM `2_wordform` WHERE `keyword` like 'hestur'
result consist from 16 to 50 results, now cycle the result as f.e. with accusative "hest" of "hestur"
SELECT `w_id` FROM `3_words` WHERE `word` like 'hest'
the result can contain several w_id, f.e. with '10138'
SELECT `s_id`, `pos` FROM `4_inv_w` WHERE `w_id` = '10138' group by `s_id`
the result can contain several sentences, to display f.e. sentence '7201'
SELECT `sentence` FROM `5_sentences` WHERE `s_id` = '7201'
Update
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42490, 'hestur', 0, 'hest');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42498, 'hestur', 0, 'hesta');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42501, 'hestur', 0, 'hestana');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42503, 'hestur', 0, 'hestanna');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42497, 'hestur', 0, 'hestar');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42500, 'hestur', 0, 'hestarnir');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42491, 'hestur', 0, 'hesti');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42494, 'hestur', 0, 'hestinn');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42495, 'hestur', 0, 'hestinum');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42492, 'hestur', 0, 'hests');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42496, 'hestur', 0, 'hestsins');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42499, 'hestur', 0, 'hestum');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42502, 'hestur', 0, 'hestunum');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42489, 'hestur', 0, 'hestur');
INSERT INTO 2_wordform (id, keyword, num_keyword, word_form) VALUES(42493, 'hestur', 0, 'hesturinn');
INSERT INTO 3_words (w_id, word, gram, freq) VALUES
(11484, 'hestur', 'nken', 122),
(60681, 'Hestur', 'nken', 15),
(484318, 'HESTUR', 'nken', 1),
(491111, 'Hestur', 'nken-s', 1);
INSERT INTO 3_words (w_id, word, gram, freq) VALUES
(10138, 'hest', 'nkeo', 141),
(159967, 'Hest', 'nkeo', 4),
(491114, 'Hest', 'ssm', 1);
INSERT INTO 4_inv_w (w_id, s_id, pos) VALUES
(11484, 2671, 4),
(11484, 22522, 7),
(11484, 30169, 8),
(11484, 32487, 4),
(11484, 33841, 9),
(11484, 38116, 5),
(11484, 40450, 6),
(11484, 42741, 32),
(11484, 45789, 10),
(11484, 58998, 3),
(11484, 74343, 4),
(11484, 76001, 3),
(11484, 99014, 9),
(11484, 99688, 6),
(11484, 109849, 21),
(11484, 119708, 21),
(11484, 131353, 34),
(11484, 147820, 6),
(11484, 148326, 25),
(11484, 160475, 40),
(11484, 167227, 2),
(11484, 170401, 3),
(11484, 178416, 18),
(11484, 197295, 12),
(11484, 197295, 6),
(11484, 198420, 19),
(11484, 203446, 28),
(11484, 204448, 1),
(11484, 215402, 1),
(11484, 237323, 4),
(11484, 249282, 4),
(11484, 263949, 1),
(11484, 263949, 22),
(11484, 266489, 27),
(11484, 270540, 5),
(11484, 272543, 5),
(11484, 272560, 1),
(11484, 272560, 8),
(11484, 282170, 20),
(11484, 284407, 26),
(11484, 290524, 6),
(11484, 291438, 10),
(11484, 293344, 6),
(11484, 294034, 49),
(11484, 317007, 7),
(11484, 325049, 22),
(11484, 328392, 14),
(11484, 368188, 47),
(11484, 391892, 14),
(11484, 401157, 11),
(11484, 412656, 24),
(11484, 421635, 17),
(11484, 439320, 3),
(11484, 467063, 5),
(11484, 469324, 23),
(11484, 477392, 2),
(11484, 480318, 4),
(11484, 487883, 1),
(11484, 490577, 42),
(11484, 499783, 9),
(11484, 500405, 23),
(11484, 501118, 15),
(11484, 527227, 3),
(11484, 539686, 25),
(11484, 543056, 9),
(11484, 544261, 3),
(11484, 547700, 20),
(11484, 555638, 19),
(11484, 570234, 2),
(11484, 592710, 2),
(11484, 616662, 1),
(11484, 619011, 16),
(11484, 632123, 2),
(11484, 633124, 2),
(11484, 636792, 8),
(11484, 636792, 3),
(11484, 646603, 17),
(11484, 664738, 4),
(11484, 670017, 4),
(11484, 685997, 4),
(11484, 686202, 1),
(11484, 691794, 12),
(11484, 698341, 2),
(11484, 715281, 3),
(11484, 715984, 37),
(11484, 716970, 10),
(11484, 716970, 4),
(11484, 752605, 36),
(11484, 756660, 19),
(11484, 760277, 3),
(11484, 776593, 3),
(11484, 785701, 24),
(11484, 789099, 3),
(11484, 794930, 1),
(11484, 795623, 12),
(11484, 802997, 6),
(11484, 812806, 6),
(11484, 814046, 21),
(11484, 820178, 6),
(11484, 823173, 22),
(11484, 843094, 3),
(11484, 844156, 1),
(11484, 844736, 24),
(11484, 853350, 18),
(11484, 869322, 3),
(11484, 885176, 2),
(11484, 899545, 22),
(11484, 904086, 16),
(11484, 907863, 9),
(11484, 909396, 9),
(11484, 912876, 3),
(11484, 919994, 4),
(11484, 927840, 24),
(11484, 927840, 5),
(11484, 934220, 40),
(11484, 936941, 11),
(11484, 952837, 13),
(11484, 969201, 11),
(11484, 970240, 1),
(11484, 970836, 19),
(11484, 972107, 1),
(11484, 990474, 6);
INSERT INTO 4_inv_w (w_id, s_id, pos) VALUES
(10138, 7201, 27),
(10138, 18772, 3),
(10138, 30001, 6),
(10138, 42089, 4),
(10138, 42089, 14),
(10138, 42234, 4),
(10138, 49383, 5),
(10138, 54795, 18),
(10138, 57564, 23),
(10138, 88542, 7),
(10138, 93027, 10),
(10138, 101097, 21),
(10138, 134312, 12),
(10138, 139116, 33),
(10138, 139522, 6),
(10138, 159109, 7),
(10138, 159109, 16),
(10138, 161497, 21),
(10138, 163948, 2),
(10138, 165301, 20),
(10138, 166478, 21),
(10138, 183452, 6),
(10138, 184390, 20),
(10138, 189930, 25),
(10138, 201629, 9),
(10138, 204590, 4),
(10138, 211374, 5),
(10138, 216483, 14),
(10138, 223617, 5),
(10138, 233652, 12),
(10138, 236571, 11),
(10138, 241302, 8),
(10138, 246485, 10),
(10138, 256910, 16),
(10138, 262349, 3),
(10138, 262925, 5),
(10138, 267047, 28),
(10138, 291988, 18),
(10138, 292680, 22),
(10138, 294814, 32),
(10138, 326917, 6),
(10138, 330019, 12),
(10138, 333411, 35),
(10138, 337880, 5),
(10138, 342003, 13),
(10138, 355325, 12),
(10138, 356409, 13),
(10138, 363795, 5),
(10138, 365735, 26),
(10138, 376570, 25),
(10138, 378214, 10),
(10138, 379159, 11),
(10138, 379236, 4),
(10138, 379533, 2),
(10138, 388753, 8),
(10138, 420633, 18),
(10138, 433121, 5),
(10138, 434645, 10),
(10138, 435895, 3),
(10138, 455575, 5),
(10138, 461900, 23),
(10138, 464040, 6),
(10138, 466657, 6),
(10138, 469848, 11),
(10138, 475569, 17),
(10138, 482701, 41),
(10138, 527708, 29),
(10138, 527708, 16),
(10138, 529426, 7),
(10138, 530753, 10),
(10138, 538071, 27),
(10138, 542685, 10),
(10138, 553742, 22),
(10138, 553742, 13),
(10138, 557216, 4),
(10138, 563747, 9),
(10138, 564716, 4),
(10138, 569146, 7),
(10138, 578368, 3),
(10138, 581713, 9),
(10138, 595890, 9),
(10138, 599015, 5),
(10138, 608570, 30),
(10138, 610218, 11),
(10138, 610218, 2),
(10138, 612099, 9),
(10138, 612568, 14),
(10138, 612894, 9),
(10138, 615361, 19),
(10138, 618001, 14),
(10138, 624969, 7),
(10138, 628252, 16),
(10138, 628635, 12),
(10138, 635977, 10),
(10138, 643675, 8),
(10138, 650487, 9),
(10138, 651489, 3),
(10138, 657552, 18),
(10138, 672884, 12),
(10138, 677130, 2),
(10138, 678841, 7),
(10138, 678841, 26),
(10138, 682904, 4),
(10138, 691251, 19),
(10138, 706325, 9),
(10138, 714680, 45),
(10138, 717460, 5),
(10138, 717489, 11),
(10138, 722393, 5),
(10138, 729972, 12),
(10138, 735745, 12),
(10138, 738334, 7),
(10138, 740791, 21),
(10138, 775696, 8),
(10138, 776984, 16),
(10138, 786073, 31),
(10138, 793185, 17),
(10138, 821475, 4),
(10138, 835234, 7),
(10138, 842713, 3),
(10138, 842730, 8),
(10138, 847372, 9),
(10138, 849612, 20),
(10138, 861768, 26),
(10138, 864231, 6),
(10138, 865927, 7),
(10138, 873939, 7),
(10138, 883591, 29),
(10138, 884260, 19),
(10138, 894952, 17),
(10138, 898453, 19),
(10138, 899290, 4),
(10138, 909225, 29),
(10138, 910173, 4),
(10138, 922447, 2),
(10138, 939319, 2),
(10138, 956278, 4),
(10138, 967342, 18),
(10138, 977090, 3),
(10138, 991346, 31),
(10138, 991346, 40);
INSERT INTO 5_sentences (s_id, sentence) VALUES
(2671, 'Hrímnir|nken-s frá|aþ Hrafnagili|nkeþ-s Glæsilegasti|lkenve hestur|nken aldar|nvee !|!');
INSERT INTO 5_sentences (s_id, sentence) VALUES
(7201, 'Hann|fpken heilsar|sfg3en öllum|fokfþ nema|c Braga|nkeþ-s sem|ct nú|aa dregur|sfg3en í|ao land|nheo og|c vill|sfg3en friðmælast|snm við|ao Loka|nkeo-s með|aþ loforði|nheþ um|ao góðar|lvfosf gjafir|nvfo ,|, sverð|nhfo ,|, hest|nkeo og|c hring|nkeo en|c hann|fpken svarar|sfg3en bara|aa með|aþ illu|lheþsf .|.');
Any sample of data would be very helpful together with expected result.
So far you can start form this attempt:
http://sqlfiddle.com/#!9/7110ef/4
SELECT w.word_form,
w3.w_id,
w4.s_id,
w4.pos,
s.sentence
FROM `2_wordform` w
INNER JOIN `3_words` w3
ON w3.`word` = w.keyword
INNER JOIN `4_inv_w` w4
ON w4.w_id = w3.w_id
INNER JOIN `5_sentences` s
ON s.s_id = w4.s_id
WHERE w.keyword like '%hestur%'
if you need only sentence you can:
SELECT DISTINCT s.sentence
FROM `2_wordform` w
INNER JOIN `3_words` w3
ON w3.`word` = w.keyword
INNER JOIN `4_inv_w` w4
ON w4.w_id = w3.w_id
INNER JOIN `5_sentences` s
ON s.s_id = w4.s_id
WHERE w.keyword like '%hestur%'
This is my code. I tried to build a VGG 11 layers network, with a mix of ReLu and ELu activation and many regularizations on kernels and activities. The result is really confusing: The code is at 10th epoch. My loss on both train and val have decreased from 2000 to 1.5, but my acc on both train and val remained the same at 50%. Can somebody explain to me?
# VGG 11
from keras.regularizers import l2
from keras.layers.advanced_activations import ELU
from keras.optimizers import Adam
model = Sequential()
model.add(Conv2D(64, (3, 3), kernel_initializer='he_normal',
kernel_regularizer=l2(0.0001), activity_regularizer=l2(0.0001),
input_shape=(1, 96, 96), activation='relu'))
model.add(Conv2D(64, (3, 3), kernel_initializer='he_normal',
kernel_regularizer=l2(0.0001), activity_regularizer=l2(0.0001),
activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3), kernel_initializer='he_normal',
kernel_regularizer=l2(0.0001),activity_regularizer=l2(0.0001),
activation='relu'))
model.add(Conv2D(128, (3, 3), kernel_initializer='he_normal',
kernel_regularizer=l2(0.0001), activity_regularizer=l2(0.0001),
activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, (3, 3), kernel_initializer='he_normal',
kernel_regularizer=l2(0.0001), activity_regularizer=l2(0.0001),
activation='relu'))
model.add(Conv2D(256, (3, 3), kernel_initializer='he_normal',
kernel_regularizer=l2(0.0001), activity_regularizer=l2(0.0001),
activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(512, (3, 3), kernel_initializer='he_normal',
kernel_regularizer=l2(0.0001), activity_regularizer=l2(0.0001),
activation='relu'))
model.add(Conv2D(512, (3, 3), kernel_initializer='he_normal',
kernel_regularizer=l2(0.0001), activity_regularizer=l2(0.0001),
activation='relu'))
model.add(Conv2D(512, (3, 3), kernel_initializer='he_normal',
kernel_regularizer=l2(0.0001), activity_regularizer=l2(0.0001),
activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# convert convolutional filters to flat so they can be feed to fully connected layers
model.add(Flatten())
model.add(Dense(2048, kernel_initializer='he_normal',
kernel_regularizer=l2(0.0001), activity_regularizer=l2(0.01)))
model.add(ELU(alpha=1.0))
model.add(Dropout(0.5))
model.add(Dense(1024, kernel_initializer='he_normal',
kernel_regularizer=l2(0.0001), activity_regularizer=l2(0.01)))
model.add(ELU(alpha=1.0))
model.add(Dropout(0.5))
model.add(Dense(2))
model.add(Activation('softmax'))
adammo = Adam(lr=0.0008, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(loss='categorical_crossentropy', optimizer=adammo, metrics=['accuracy'])
hist = model.fit(X_train, y_train, batch_size=48, epochs=20, verbose=1, validation_data=(X_val, y_val))
This is not a defect, in fact, it is entirely possible!
Categorical cross entropy loss does not require that accuracy go up with the loss decreasing. This is not a bug in keras or theano, but rather a network or data problem.
This network structure is probably over-complicated for what you might be trying to do. You should remove some of your regularization, use only ReLu, use less layers, use the standard adam optimizer, a larger batch, etc. Try first using one of keras' default models like VGG16,
If you want to see their implementation to edit it for a different VGG11 like structure. It is here:
def VGG_16(weights_path=None):
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax'))
if weights_path:
model.load_weights(weights_path)
return model
You can see it is much more simple. It only uses rely (which has gotten popular these days) has no regularization, different convolution structure, etc. Modify that to your needs!